2 Commits

Author SHA1 Message Date
zhaoyukun
b401387ad9 add_time_select 2026-04-27 11:12:23 +08:00
zhaoyukun
9f8f641402 add_vlan_monitor 2026-04-27 10:35:33 +08:00
2 changed files with 254 additions and 36 deletions

View File

@@ -20,6 +20,14 @@ YEWU_DNS="192.168.46.44"
YEWU_IFACE="yewu-peer"
YEWU_MAC="02:aa:bb:cc:dd:01"
# monitor VLAN 配置
MONITOR_VLAN_ID=100
MONITOR_PHY_IFACE="eth11"
MONITOR_ALIAS="lsa"
# -------------------
# k3s 配置
# -------------------
@@ -29,6 +37,29 @@ NODE_NAME="rk3588"
NODE_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 ..."

View File

@@ -66,15 +66,6 @@ ConfigureWithoutCarrier=yes
RequiredForOnline=no-carrier
EOF
# cloud VLAN 子接口
# cat > /etc/systemd/network/10-monitor.netdev << EOF
# [NetDev]
# Name=cloud-monitor
# Kind=vlan
# [VLAN]
# Id=$MONITOR_VLAN_ID
# EOF
cat > /etc/systemd/network/00-$CLOUD_IFACE.network << EOF
[Match]
@@ -94,17 +85,6 @@ PVID=1
EgressUntagged=1
EOF
# cat > /etc/systemd/network/10-monitor.network << EOF
# # monitor
# [Match]
# Name=cloud-monitor
# [Network]
# ConfigureWithoutCarrier=yes
# [Link]
# RequiredForOnline=no-carrier
# EOF
echo "cloud 网桥配置已完成"
@@ -162,6 +142,74 @@ echo "正在重新加载网络配置..."
networkctl reload
echo "网络配置已生效"
# ===================================================
# 新增部分monitor VLAN 子接口配置
# ===================================================
echo "=== 开始配置 monitor VLAN 子接口 ==="
: "${MONITOR_VLAN_ID:?请在 bushu.conf 中配置 MONITOR_VLAN_ID}"
: "${MONITOR_PHY_IFACE:?请在 bushu.conf 中配置 MONITOR_PHY_IFACE}"
: "${MONITOR_ALIAS:=lsa}"
MONITOR_VLAN_IFACE="${MONITOR_PHY_IFACE}.${MONITOR_VLAN_ID}"
echo "monitor 物理网卡: ${MONITOR_PHY_IFACE}"
echo "monitor VLAN ID: ${MONITOR_VLAN_ID}"
echo "monitor VLAN 子接口: ${MONITOR_VLAN_IFACE}"
echo "monitor VLAN alias: ${MONITOR_ALIAS}"
modprobe 8021q || true
if ip link show "${MONITOR_VLAN_IFACE}" >/dev/null 2>&1; then
echo "VLAN 子接口 ${MONITOR_VLAN_IFACE} 已存在,跳过创建"
else
echo "正在创建 VLAN 子接口 ${MONITOR_VLAN_IFACE} ..."
ip link add link "${MONITOR_PHY_IFACE}" name "${MONITOR_VLAN_IFACE}" type vlan id "${MONITOR_VLAN_ID}"
fi
ip link set dev "${MONITOR_VLAN_IFACE}" alias "${MONITOR_ALIAS}"
ip link set dev "${MONITOR_VLAN_IFACE}" up
echo "monitor VLAN 子接口 ${MONITOR_VLAN_IFACE} 当前已生效"
# -----------------------
# 新增部分:写入 rc.local保证重启后自动创建
# -----------------------
echo "=== 写入 rc.local配置 monitor VLAN 开机自动创建 ==="
if [[ ! -f /etc/rc.local ]]; then
cat > /etc/rc.local << 'EOF'
#!/bin/bash
exit 0
EOF
chmod +x /etc/rc.local
fi
sed -i '/# BEGIN MONITOR VLAN/,/# END MONITOR VLAN/d' /etc/rc.local
sed -i "/^exit 0/i\\
# BEGIN MONITOR VLAN\\
modprobe 8021q || true\\
while ! ip link show ${MONITOR_PHY_IFACE} >/dev/null 2>&1; do\\
sleep 1\\
done\\
ip link show ${MONITOR_VLAN_IFACE} >/dev/null 2>&1 || ip link add link ${MONITOR_PHY_IFACE} name ${MONITOR_VLAN_IFACE} type vlan id ${MONITOR_VLAN_ID}\\
ip link set dev ${MONITOR_VLAN_IFACE} alias ${MONITOR_ALIAS}\\
ip link set dev ${MONITOR_VLAN_IFACE} up\\
# END MONITOR VLAN\\
" /etc/rc.local
chmod +x /etc/rc.local
systemctl enable rc-local 2>/dev/null || true
systemctl enable rc-local.service 2>/dev/null || true
echo "rc.local 已写入 monitor VLAN 配置"
# -----------------------
# k3s 配置部分
# -----------------------
@@ -312,28 +360,167 @@ findmnt /var/lib/longhorn
# -----------------------
# 时间同步
# -----------------------
echo "=== 安装并配置 chrony ==="
echo "=== 时间同步配置 ==="
: "${ENABLE_CHRONY:=true}"
: "${ENABLE_GPTP:=false}"
if [[ "${ENABLE_CHRONY}" == "true" || "${ENABLE_GPTP}" == "true" ]]; then
echo "安装时间同步相关组件..."
apt update
apt install -y chrony linuxptp
cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.bak 2>/dev/null
fi
# ===================================================
# chrony 配置:由客户选择是否启用
# ===================================================
if [[ "${ENABLE_CHRONY}" == "true" ]]; then
echo "=== 启用 chrony 时间同步 ==="
cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.bak 2>/dev/null || true
cat > /etc/chrony/chrony.conf << EOF
server 10.250.0.1 iburst prefer
server 10.250.0.2 iburst
server 10.250.0.3 iburst
# 由 bushu.sh 自动生成
EOF
for server in "${CHRONY_SERVERS[@]}"; do
echo "server ${server}" >> /etc/chrony/chrony.conf
done
cat >> /etc/chrony/chrony.conf << EOF
driftfile /var/lib/chrony/chrony.drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
EOF
systemctl enable chrony
systemctl restart chrony
chronyc sources
chronyc tracking
echo "=== 启动 PTP 同步 ==="
ptp4l -i $CLOUD_IFACE -m &
phc2sys -s $CLOUD_IFACE -c CLOCK_REALTIME -m &
chronyc sources || 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
echo "=== 启用 gPTP 时间同步 ==="
: "${GPTP_IFACE:?请在 bushu.conf 中配置 GPTP_IFACE}"
echo "gPTP 使用物理网卡: ${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
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
# -----------------------
# 创建 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"
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 \
-m
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
[Service]
Type=simple
ExecStart=/usr/local/bin/start-gptp.sh ${GPTP_IFACE}
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable gptp.service
systemctl restart gptp.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
fi
# -----------------------
# 域名解析