From d59668b663d46d88ccdb783e4e336dac7260aacb Mon Sep 17 00:00:00 2001 From: bin456789 Date: Tue, 5 Mar 2024 21:46:32 +0800 Subject: [PATCH] =?UTF-8?q?core:=20=E4=BF=AE=E5=A4=8D=20cloud-init=20?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E6=AD=A3=E7=A1=AE=E6=B8=B2=E6=9F=93=20onlink?= =?UTF-8?q?=20=E7=BD=91=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud-init-fix-onlink.sh | 200 +++++++++++++++++++++++++++++++++++++++ trans.sh | 69 ++++++++++---- 2 files changed, 249 insertions(+), 20 deletions(-) create mode 100644 cloud-init-fix-onlink.sh diff --git a/cloud-init-fix-onlink.sh b/cloud-init-fix-onlink.sh new file mode 100644 index 0000000..e9c474f --- /dev/null +++ b/cloud-init-fix-onlink.sh @@ -0,0 +1,200 @@ +#!/bin/bash +# 修复 cloud-init 没有正确渲染 onlink 网关 + +set -eE + +insert_into_file() { + file=$1 + location=$2 + regex_to_find=$3 + + if [ "$location" = head ]; then + bak=$(mktemp) + cp "$file" "$bak" + cat - "$bak" >"$file" + else + line_num=$(grep -E -n "$regex_to_find" "$file" | cut -d: -f1) + + found_count=$(echo "$line_num" | wc -l) + if [ ! "$found_count" -eq 1 ]; then + return 1 + fi + + case "$location" in + before) line_num=$((line_num - 1)) ;; + after) ;; + *) return 1 ;; + esac + + sed -i "${line_num}r /dev/stdin" "$file" + fi +} + +fix_netplan_conf() { + # 修改前 + # gateway4: 1.1.1.1 + # gateway6: ::1 + + # 修改后 + # routes: + # - to: 0.0.0.0/0 + # via: 1.1.1.1 + # on-link: true + # routes: + # - to: ::/0 + # via: ::1 + # on-link: true + conf=/etc/netplan/50-cloud-init.yaml + if ! [ -f $conf ]; then + return + fi + + # 判断 bug 是否已经修复 + if grep 'on-link:' "$conf"; then + return + fi + + # 获取网关 + gateways=$(grep 'gateway[4|6]:' $conf | awk '{print $2}') + if [ -z "$gateways" ]; then + return + fi + + # 获取缩进 + spaces=$(grep 'gateway[4|6]:' $conf | head -1 | grep -o '^[[:space:]]*') + + { + # 网关头部 + cat <>"$conf" + done + + # 重新应用配置 + # networkctl reload 不起作用 + systemctl restart systemd-networkd +} + +fix_wicked_conf() { + # https://github.com/openSUSE/wicked/wiki/FAQ#q-why-wicked-does-not-set-my-default-static-route + + # 修改前 + # default 1.1.1.1 - - + # default 2602::1 - - + + # 修改后 + # 1.1.1.1 - - + # 2602::1 - - + # default 1.1.1.1 - - + # default 2602::1 - - + + if ! confs=$(ls /etc/sysconfig/network/ifroute-*); then + return + fi + + for conf in $confs; do + # 判断 bug 是否已经修复 + if grep -v 'default' "$conf" | grep '-'; then + return + fi + + # 获取网关 + gateways=$(awk '$1=="default" {print $2}' "$conf") + if [ -z "$gateways" ]; then + return + fi + + # 创建新条目 + for gateway in $gateways; do + echo "$gateway - -" + done | insert_into_file "$conf" head + done + + # 重新应用配置 + systemctl restart wicked +} + +# debian 10/11/12: netplan + networkd/resolved +fix_netplan_conf + +# arch: networkd/resolved +# gentoo: networkd/resolved +fix_networkd_conf + +# opensuse 15.5: ifcfg + netconfig (dns) + wicked +fix_wicked_conf diff --git a/trans.sh b/trans.sh index 0c5baed..43402ef 100644 --- a/trans.sh +++ b/trans.sh @@ -1321,6 +1321,17 @@ modify_linux() { fi } + # 修复 onlink 网关 + add_onlink_script_if_need() { + if is_staticv4 || is_staticv6; then + fix_sh=cloud-init-fix-onlink.sh + download $confhome/$fix_sh $os_dir/$fix_sh + insert_into_file $ci_file after '^runcmd:' <