From 9f8f64140245703b0619fc829fe598b55d2f029a Mon Sep 17 00:00:00 2001 From: zhaoyukun Date: Mon, 27 Apr 2026 10:25:26 +0800 Subject: [PATCH] add_vlan_monitor --- bushu.conf | 8 +++++ bushujb.sh | 90 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/bushu.conf b/bushu.conf index 8144757..1a5fa69 100644 --- a/bushu.conf +++ b/bushu.conf @@ -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 配置 # ------------------- diff --git a/bushujb.sh b/bushujb.sh index b80cd60..f7a2b47 100644 --- a/bushujb.sh +++ b/bushujb.sh @@ -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 配置部分 # ----------------------- @@ -349,4 +397,4 @@ done systemctl daemon-reload systemctl enable k3s systemctl start k3s -echo "k3s 配置完成" \ No newline at end of file +echo "k3s 配置完成"