From f0f67afbcb01123c4f0cb86e163a82697dd5696f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E4=BD=B3=E5=B8=86?= <452531855@qq.com> Date: Mon, 7 Sep 2026 09:40:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(scripts):=20provision-host=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E2=80=94=E2=80=94DNS=20=E4=BB=85=E6=94=B6=E9=9B=86=20?= =?UTF-8?q?IPv4(=E5=8E=9F=E7=89=88=E5=A1=9E=E5=85=A5=20IPv6=20=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E8=A2=AB=20nmcli=20=E6=8B=92=E7=BB=9D=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=20set-e=20=E4=B8=AD=E6=AD=A2);=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=8E=9F=20netplan=20=E9=85=8D=E7=BD=AE=E4=BB=85=E5=88=87?= =?UTF-8?q?=E5=85=A8=E5=B1=80=20renderer(=E5=8E=9F=E7=89=88=E5=88=A0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=AF=BC=E8=87=B4=20WiFi=20=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E6=96=AD=E7=BD=91=E5=A4=B1=E8=81=94,netplan=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B8=B2=E6=9F=93=E7=AD=89=E5=80=BC=20NM=20p?= =?UTF-8?q?rofile=20=E5=90=AB=20SSID/PSK);WiFi=20=E4=B8=BB=E5=8F=A3?= =?UTF-8?q?=E8=B7=B3=E8=BF=87=20rescue-dhcp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/provision-host.sh | 43 ++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/scripts/provision-host.sh b/scripts/provision-host.sh index c621df3..a5db062 100644 --- a/scripts/provision-host.sh +++ b/scripts/provision-host.sh @@ -46,36 +46,28 @@ systemctl enable --now NetworkManager >/dev/null 2>&1 || true # ── 2. 识别主网口并读取当前参数 ─────────────────────────────────────────── PRIMARY_IF=$(ip -4 route show default | awk '{print $5; exit}') [[ -n "${PRIMARY_IF:-}" ]] || die "未找到默认路由网卡(主机当前无网络?)" +PRIMARY_TYPE=$(ip link show "$PRIMARY_IF" | grep -o 'link/[a-z]*' | head -1) CUR_ADDR=$(ip -4 addr show dev "$PRIMARY_IF" | awk '/inet /{print $2; exit}') # x.x.x.x/前缀 CUR_GW=$(ip -4 route show default | awk '{print $3; exit}') -CUR_DNS=$(grep -h '^nameserver' /run/systemd/resolve/resolv.conf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf 2>/dev/null \ - | awk '{print $2}' | grep -v '^127\.' | sort -u | tr '\n' ' ') -log "主网口 $PRIMARY_IF 当前: 地址=$CUR_ADDR 网关=$CUR_GW DNS=${CUR_DNS:-<无>}" +CUR_DNS4=$(grep -h '^nameserver' /run/systemd/resolve/resolv.conf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf 2>/dev/null \ + | awk '{print $2}' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | sort -u | tr '\n' ' ') +log "主网口 $PRIMARY_IF 当前: 地址=$CUR_ADDR 网关=$CUR_GW DNS4=${CUR_DNS4:-<无>}" -# ── 3. 预创建 NM 连接 ───────────────────────────────────────────────────── -# 3.1 主连接:等值当前参数的静态地址(切换 renderer 后地址不变) -if ! nmcli -g NAME,DEVICE connection show 2>/dev/null | grep -q ":${PRIMARY_IF}$"; then - log "创建主 NM 连接 mixingx-$PRIMARY_IF (静态,等值当前参数)" - nmcli connection add type ethernet ifname "$PRIMARY_IF" con-name "mixingx-$PRIMARY_IF" \ - ipv4.method manual \ - ipv4.addresses "$CUR_ADDR" \ - ipv4.gateway "$CUR_GW" \ - ${CUR_DNS:+ipv4.dns "${CUR_DNS// /,}"} \ - connection.autoconnect yes \ - connection.autoconnect-priority 0 >/dev/null +# ── 3. 救援连接(仅以太网主口;WiFi 主口跳过——救援 DHCP 对 WiFi 无意义) ──── +if [[ "$PRIMARY_TYPE" == "ethernet"* ]]; then + if ! nmcli connection show "rescue-dhcp" >/dev/null 2>&1; then + log "创建救援 DHCP 连接 rescue-dhcp (autoconnect-priority -100)" + nmcli connection add type ethernet ifname "$PRIMARY_IF" con-name "rescue-dhcp" \ + ipv4.method auto \ + connection.autoconnect yes \ + connection.autoconnect-priority -100 >/dev/null + fi fi -# 3.2 救援连接:优先级最低,主连接起不来时 DHCP 自动接管(网页配错 IP 的保命通道) -if ! nmcli connection show "rescue-dhcp" >/dev/null 2>&1; then - log "创建救援 DHCP 连接 rescue-dhcp (autoconnect-priority -100)" - nmcli connection add type ethernet ifname "$PRIMARY_IF" con-name "rescue-dhcp" \ - ipv4.method auto \ - connection.autoconnect yes \ - connection.autoconnect-priority -100 >/dev/null -fi - -# ── 4. netplan renderer 切换 → NetworkManager ───────────────────────────── +# ── 4. 全局切换 netplan renderer → NetworkManager ──────────────────────── +# 不删除原 netplan 配置(ethernet/wifi 定义、SSID/PSK 原样保留), +# netplan 会把原有连接定义自动渲染为等值的 NM profiles,无缝接管 if [[ ! -f /etc/netplan/90-mixingx-nm.yaml ]]; then log "备份原 netplan 配置 → $BACKUP_DIR/netplan/" mkdir -p "$BACKUP_DIR/netplan" @@ -85,8 +77,7 @@ if [[ ! -f /etc/netplan/90-mixingx-nm.yaml ]]; then mkdir -p /etc/cloud/cloud.cfg.d echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg - log "切换 netplan renderer → NetworkManager" - rm -f /etc/netplan/50-cloud-init.yaml /etc/netplan/01-netcfg.yaml /etc/netplan/99-installer-config.yaml + log "写入全局 renderer=NetworkManager" cat > /etc/netplan/90-mixingx-nm.yaml <<'EOF' network: version: 2