1 Commits

Author SHA1 Message Date
zhaoyukun
93f8a37f21 fix_time_select 2026-04-28 15:12:57 +08:00
2 changed files with 264 additions and 29 deletions

View File

@@ -37,6 +37,29 @@ NODE_NAME="rk3588"
NODE_IP="10.250.0.5" NODE_IP="10.250.0.5"
NODE_EXT_IP="10.250.0.5" NODE_EXT_IP="10.250.0.5"
# -----------------------
# 时间同步配置
# -----------------------
# 是否启用 chronytrue / false
ENABLE_CHRONY=true
# 是否启用 gPTPtrue / false
ENABLE_GPTP=true
# gPTP 使用的物理网卡
GPTP_IFACE="eth11"
# chrony 时间服务器
CHRONY_SERVERS=(
"10.250.0.1 iburst prefer"
"10.250.0.2 iburst"
"10.250.0.3 iburst"
)
# ------------------- # -------------------
# 域名解析列表 (每行一个条目) # 域名解析列表 (每行一个条目)
# 格式: "IP hostname1 hostname2 ..." # 格式: "IP hostname1 hostname2 ..."

View File

@@ -215,7 +215,7 @@ echo "rc.local 已写入 monitor VLAN 配置"
# ----------------------- # -----------------------
echo "=== k3s 安装配置 ===" echo "=== k3s 安装配置 ==="
echo "=== 下载 K3s 二进制到 /usr/bin/ ===" echo "=== 下载 K3s 二进制到 /usr/bin/ ==="
curl -L -o /usr/bin/k3s "https://file.piicloud.com/f/4108d2f3b3c047be85a8/?dl=1" curl -L -o /usr/bin/k3s "https://file.piicloud.com/f/acfd6bc97c7e42de8647/?dl=1"
chmod +x /usr/bin/k3s chmod +x /usr/bin/k3s
ls -lh /usr/bin/k3s ls -lh /usr/bin/k3s
@@ -231,9 +231,9 @@ fi
# 下载镜像 # 下载镜像
[ -f "/var/lib/rancher/agent/images/base.tar" ] || \ [ -f "/var/lib/rancher/agent/images/base.tar" ] || \
curl -L -o /var/lib/rancher/agent/images/base.tar "https://file.piicloud.com/f/11cce6158b9e4f59ba2c/?dl=1" curl -L -o /var/lib/rancher/agent/images/base.tar "https://file.piicloud.com/f/40e4c1cc37564dd8900d/?dl=1"
[ -f "/var/lib/rancher/agent/images/expend.tar" ] || \ [ -f "/var/lib/rancher/agent/images/expend.tar" ] || \
curl -L -o /var/lib/rancher/agent/images/expend.tar "https://file.piicloud.com/f/414c2646352945b282c0/?dl=1" curl -L -o /var/lib/rancher/agent/images/expend.tar "https://file.piicloud.com/f/c6cf4a7811d344ca9bf6/?dl=1"
chown root:root /var/lib/rancher/agent/images/*.tar chown root:root /var/lib/rancher/agent/images/*.tar
chmod 644 /var/lib/rancher/agent/images/*.tar chmod 644 /var/lib/rancher/agent/images/*.tar
@@ -301,9 +301,48 @@ mirrors:
EOF EOF
# ----------------------- # -----------------------
# NVMe 初始 # NVMe 分区与格式
# ----------------------- # -----------------------
# 检查磁盘是否存在
if [[ ! -b /dev/nvme0n1 ]]; then
echo "错误:/dev/nvme0n1 不存在"
exit 1
fi
# 如果分区不存在,则创建
if [[ ! -b /dev/nvme0n1p1 ]]; then
echo "/dev/nvme0n1p1 不存在,开始创建分区..."
if lsblk -nr /dev/nvme0n1 | awk '{print $6}' | grep -q '^part$'; then
echo "检测到已有分区,但没有 nvme0n1p1请手动检查"
exit 1
fi
parted -s /dev/nvme0n1 mklabel gpt
parted -s /dev/nvme0n1 mkpart primary 0% 100%
partprobe /dev/nvme0n1
sleep 2
if [[ ! -b /dev/nvme0n1p1 ]]; then
echo "错误:分区创建失败"
exit 1
fi
echo "分区创建成功:/dev/nvme0n1p1"
else
echo "/dev/nvme0n1p1 已存在,跳过创建分区"
fi
# 如果没有文件系统,则格式化
if ! blkid /dev/nvme0n1p1 >/dev/null 2>&1; then
echo "/dev/nvme0n1p1 没有文件系统,开始格式化为 ext4..."
mkfs.ext4 -F /dev/nvme0n1p1
else
echo "/dev/nvme0n1p1 已有文件系统,跳过格式化"
fi
# ----------------------- # -----------------------
# NVMe 挂载与绑定挂载 # NVMe 挂载与绑定挂载
# ----------------------- # -----------------------
@@ -319,6 +358,10 @@ else
echo "/mnt/nvme 已经挂载,跳过挂载" echo "/mnt/nvme 已经挂载,跳过挂载"
fi fi
# ✅ 确保源目录存在(关键修复点)
mkdir -p /mnt/nvme/rancher
mkdir -p /mnt/nvme/longhorn
# 确保绑定挂载目标目录存在 # 确保绑定挂载目标目录存在
mkdir -p /var/lib/rancher mkdir -p /var/lib/rancher
mkdir -p /var/lib/longhorn mkdir -p /var/lib/longhorn
@@ -339,49 +382,218 @@ else
echo "/var/lib/longhorn 已经挂载,跳过绑定挂载" echo "/var/lib/longhorn 已经挂载,跳过绑定挂载"
fi fi
# 更新 /etc/fstab保证开机自动挂载 # -----------------------
# 更新 /etc/fstab
# -----------------------
NVME_UUID=$(blkid -s UUID -o value /dev/nvme0n1p1) NVME_UUID=$(blkid -s UUID -o value /dev/nvme0n1p1)
NVME_FSTYPE=$(blkid -s TYPE -o value /dev/nvme0n1p1)
if [[ -z "$NVME_UUID" || -z "$NVME_FSTYPE" ]]; then
echo "错误:无法获取 UUID 或文件系统类型,停止写入 fstab"
exit 1
fi
# 主挂载(安全)
grep -qE "[[:space:]]/mnt/nvme[[:space:]]" /etc/fstab || \ grep -qE "[[:space:]]/mnt/nvme[[:space:]]" /etc/fstab || \
echo "UUID=${NVME_UUID} /mnt/nvme ext4 defaults,nofail 0 2" >> /etc/fstab echo "UUID=${NVME_UUID} /mnt/nvme ${NVME_FSTYPE} defaults,nofail,x-systemd.device-timeout=5 0 2" >> /etc/fstab
grep -qE "^/mnt/nvme/rancher[[:space:]]+/var/lib/rancher[[:space:]]+none[[:space:]]+bind" /etc/fstab || \ # bind mount安全
echo "/mnt/nvme/rancher /var/lib/rancher none bind 0 0" >> /etc/fstab grep -qE "[[:space:]]/var/lib/rancher[[:space:]]" /etc/fstab || \
echo "/mnt/nvme/rancher /var/lib/rancher none bind,nofail,x-systemd.requires-mounts-for=/mnt/nvme 0 0" >> /etc/fstab
grep -qE "^/mnt/nvme/longhorn[[:space:]]+/var/lib/longhorn[[:space:]]+none[[:space:]]+bind" /etc/fstab || \ grep -qE "[[:space:]]/var/lib/longhorn[[:space:]]" /etc/fstab || \
echo "/mnt/nvme/longhorn /var/lib/longhorn none bind 0 0" >> /etc/fstab echo "/mnt/nvme/longhorn /var/lib/longhorn none bind,nofail,x-systemd.requires-mounts-for=/mnt/nvme 0 0" >> /etc/fstab
# 显示挂载状态 # -----------------------
# 验证
# -----------------------
echo "测试 fstab..."
mount -a
echo "显示挂载状态"
findmnt /mnt/nvme findmnt /mnt/nvme
findmnt /var/lib/rancher findmnt /var/lib/rancher
findmnt /var/lib/longhorn findmnt /var/lib/longhorn
# ----------------------- # -----------------------
# 时间同步 # 时间同步
# ----------------------- # -----------------------
echo "=== 安装并配置 chrony ===" echo "=== 时间同步配置 ==="
: "${ENABLE_CHRONY:=true}"
: "${ENABLE_GPTP:=false}"
if [[ "${ENABLE_CHRONY}" == "true" && "${ENABLE_GPTP}" == "true" ]]; then
echo "错误ENABLE_CHRONY 和 ENABLE_GPTP 不能同时为 true"
exit 1
fi
echo "=== 从 RTC 恢复系统时间 ==="
hwclock -s 2>/dev/null || hwclock --hctosys 2>/dev/null || true
date
if [[ "${ENABLE_CHRONY}" == "true" || "${ENABLE_GPTP}" == "true" ]]; then
echo "安装时间同步相关组件..."
apt update apt update
apt install -y chrony linuxptp apt install -y chrony linuxptp
cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.bak 2>/dev/null fi
if [[ "${ENABLE_CHRONY}" == "true" ]]; then
echo "=== 启用 chrony 时间同步 ==="
systemctl disable --now gptp.service 2>/dev/null || true
systemctl disable --now phc2sys.service 2>/dev/null || true
pkill ptp4l 2>/dev/null || true
pkill phc2sys 2>/dev/null || true
cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.bak 2>/dev/null || true
cat > /etc/chrony/chrony.conf << EOF cat > /etc/chrony/chrony.conf << EOF
server 10.250.0.1 iburst prefer # 由 bushu.sh 自动生成
server 10.250.0.2 iburst EOF
server 10.250.0.3 iburst
for server in "${CHRONY_SERVERS[@]}"; do
echo "server ${server} iburst" >> /etc/chrony/chrony.conf
done
cat >> /etc/chrony/chrony.conf << EOF
driftfile /var/lib/chrony/chrony.drift driftfile /var/lib/chrony/chrony.drift
makestep 1.0 3 makestep 1.0 -1
rtcsync rtcsync
logdir /var/log/chrony logdir /var/log/chrony
EOF EOF
systemctl enable chrony
systemctl restart chrony
chronyc sources
chronyc tracking
echo "=== 启动 PTP 同步 ===" systemctl enable --now chrony
ptp4l -i $CLOUD_IFACE -m & chronyc -a makestep || true
phc2sys -s $CLOUD_IFACE -c CLOCK_REALTIME -m & sleep 3
chronyc sources -v || true
chronyc tracking || true
elif [[ "${ENABLE_GPTP}" == "true" ]]; then
echo "=== 启用 gPTP 时间同步 ==="
systemctl disable --now chrony 2>/dev/null || true
: "${GPTP_IFACE:?请在 bushu.conf 中配置 GPTP_IFACE}"
mkdir -p /usr/local/bin
mkdir -p /etc/linuxptp
cat > /etc/linuxptp/gptp.cfg << EOF
# 由 bushu.sh 自动生成
# gPTP / IEEE 802.1AS 配置
[global]
gmCapable 0
priority1 248
priority2 248
logAnnounceInterval 0
logSyncInterval -3
syncReceiptTimeout 3
neighborPropDelayThresh 800
min_neighbor_prop_delay -20000000
assume_two_step 1
path_trace_enabled 1
follow_up_info 1
transportSpecific 0x1
ptp_dst_mac 01:80:C2:00:00:0E
network_transport L2
delay_mechanism P2P
time_stamping hardware
EOF
cat > /usr/local/bin/start-gptp.sh << 'EOF'
#!/bin/bash
set -euo pipefail
GPTP_IFACE="${1:?Usage: start-gptp.sh <interface>}"
while ! ip link show "${GPTP_IFACE}" >/dev/null 2>&1; do
echo "等待网卡 ${GPTP_IFACE} 出现..."
sleep 1
done
ip link set dev "${GPTP_IFACE}" up
if ethtool -T "${GPTP_IFACE}" 2>/dev/null | grep -q "hardware-transmit"; then
TS_MODE="hardware"
else
echo "警告:${GPTP_IFACE} 不支持硬件时间戳,切换为 software timestamp"
TS_MODE="software"
sed -i 's/^time_stamping.*/time_stamping software/' /etc/linuxptp/gptp.cfg
fi
echo "gPTP timestamp mode: ${TS_MODE}"
exec /usr/sbin/ptp4l \
-i "${GPTP_IFACE}" \
-f /etc/linuxptp/gptp.cfg \
-m
EOF
chmod +x /usr/local/bin/start-gptp.sh
cat > /etc/systemd/system/gptp.service << EOF
[Unit]
Description=gPTP IEEE 802.1AS Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/start-gptp.sh ${GPTP_IFACE}
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/phc2sys.service << EOF
[Unit]
Description=Synchronize system clock from gPTP PHC
After=gptp.service
Requires=gptp.service
[Service]
Type=simple
ExecStartPre=/bin/sleep 10
ExecStart=/usr/sbin/phc2sys -s ${GPTP_IFACE} -c CLOCK_REALTIME -O 0 -m
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now gptp.service
systemctl enable --now phc2sys.service
else
echo "=== 未启用时间同步 ==="
systemctl disable --now chrony 2>/dev/null || true
systemctl disable --now gptp.service 2>/dev/null || true
systemctl disable --now phc2sys.service 2>/dev/null || true
fi
echo "=== 检查系统时间 ==="
date
timedatectl || true
YEAR=$(date +%Y)
if (( YEAR < 2024 )); then
echo "错误:系统时间异常,当前时间:$(date)"
echo "拒绝继续执行,避免 apt/curl/k3s TLS 失败"
exit 1
fi
# ----------------------- # -----------------------
# 域名解析 # 域名解析