Compare commits
1 Commits
efae786eed
...
93f8a37f21
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93f8a37f21 |
117
bushujb.sh
117
bushujb.sh
@@ -215,7 +215,7 @@ echo "rc.local 已写入 monitor VLAN 配置"
|
||||
# -----------------------
|
||||
echo "=== k3s 安装配置 ==="
|
||||
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
|
||||
ls -lh /usr/bin/k3s
|
||||
|
||||
@@ -231,9 +231,9 @@ fi
|
||||
|
||||
# 下载镜像
|
||||
[ -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" ] || \
|
||||
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
|
||||
chmod 644 /var/lib/rancher/agent/images/*.tar
|
||||
@@ -425,72 +425,70 @@ 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 install -y chrony linuxptp
|
||||
fi
|
||||
|
||||
# ===================================================
|
||||
# chrony 配置:由客户选择是否启用
|
||||
# ===================================================
|
||||
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
|
||||
# 由 bushu.sh 自动生成
|
||||
|
||||
EOF
|
||||
|
||||
for server in "${CHRONY_SERVERS[@]}"; do
|
||||
echo "server ${server}" >> /etc/chrony/chrony.conf
|
||||
echo "server ${server} iburst" >> /etc/chrony/chrony.conf
|
||||
done
|
||||
|
||||
cat >> /etc/chrony/chrony.conf << EOF
|
||||
|
||||
driftfile /var/lib/chrony/chrony.drift
|
||||
makestep 1.0 3
|
||||
makestep 1.0 -1
|
||||
rtcsync
|
||||
logdir /var/log/chrony
|
||||
EOF
|
||||
|
||||
systemctl enable chrony
|
||||
systemctl restart chrony
|
||||
systemctl enable --now chrony
|
||||
chronyc -a makestep || true
|
||||
sleep 3
|
||||
|
||||
chronyc sources || true
|
||||
chronyc sources -v || true
|
||||
chronyc tracking || true
|
||||
|
||||
echo "chrony 已启用"
|
||||
else
|
||||
echo "=== 未启用 chrony ==="
|
||||
systemctl disable chrony 2>/dev/null || true
|
||||
systemctl stop chrony 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# ===================================================
|
||||
# gPTP 配置:由客户选择是否启用
|
||||
# ===================================================
|
||||
if [[ "${ENABLE_GPTP}" == "true" ]]; then
|
||||
elif [[ "${ENABLE_GPTP}" == "true" ]]; then
|
||||
echo "=== 启用 gPTP 时间同步 ==="
|
||||
|
||||
: "${GPTP_IFACE:?请在 bushu.conf 中配置 GPTP_IFACE}"
|
||||
systemctl disable --now chrony 2>/dev/null || true
|
||||
|
||||
echo "gPTP 使用物理网卡: ${GPTP_IFACE}"
|
||||
: "${GPTP_IFACE:?请在 bushu.conf 中配置 GPTP_IFACE}"
|
||||
|
||||
mkdir -p /usr/local/bin
|
||||
mkdir -p /etc/linuxptp
|
||||
|
||||
# -----------------------
|
||||
# 创建 gPTP 配置文件
|
||||
# -----------------------
|
||||
cat > /etc/linuxptp/gptp.cfg << EOF
|
||||
# 由 bushu.sh 自动生成
|
||||
# gPTP / IEEE 802.1AS 配置
|
||||
|
||||
[global]
|
||||
gmCapable 1
|
||||
gmCapable 0
|
||||
priority1 248
|
||||
priority2 248
|
||||
logAnnounceInterval 0
|
||||
@@ -508,38 +506,29 @@ delay_mechanism P2P
|
||||
time_stamping hardware
|
||||
EOF
|
||||
|
||||
# -----------------------
|
||||
# 创建 gPTP 执行脚本
|
||||
# -----------------------
|
||||
cat > /usr/local/bin/start-gptp.sh << 'EOF'
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
GPTP_IFACE="${1:?Usage: start-gptp.sh <interface>}"
|
||||
|
||||
echo "gPTP interface: ${GPTP_IFACE}"
|
||||
|
||||
# 等待网卡出现
|
||||
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"
|
||||
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}"
|
||||
|
||||
# 启动 ptp4l,使用 gPTP 配置
|
||||
exec /usr/sbin/ptp4l \
|
||||
-i "${GPTP_IFACE}" \
|
||||
-f /etc/linuxptp/gptp.cfg \
|
||||
@@ -548,13 +537,9 @@ EOF
|
||||
|
||||
chmod +x /usr/local/bin/start-gptp.sh
|
||||
|
||||
# -----------------------
|
||||
# 创建 gPTP systemd 服务
|
||||
# -----------------------
|
||||
cat > /etc/systemd/system/gptp.service << EOF
|
||||
[Unit]
|
||||
Description=gPTP IEEE 802.1AS Service
|
||||
Documentation=man:ptp4l
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
@@ -569,16 +554,44 @@ User=root
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable gptp.service
|
||||
systemctl restart gptp.service
|
||||
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
|
||||
|
||||
echo "gPTP 服务已启用"
|
||||
systemctl status gptp.service --no-pager || true
|
||||
else
|
||||
echo "=== 未启用 gPTP ==="
|
||||
systemctl disable gptp.service 2>/dev/null || true
|
||||
systemctl stop gptp.service 2>/dev/null || true
|
||||
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
|
||||
|
||||
|
||||
@@ -596,4 +609,4 @@ done
|
||||
systemctl daemon-reload
|
||||
systemctl enable k3s
|
||||
systemctl start k3s
|
||||
echo "k3s 配置完成"
|
||||
echo "k3s 配置完成"
|
||||
Reference in New Issue
Block a user