2024-03-05 21:46:32 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
# 修复 cloud-init 没有正确渲染 onlink 网关
|
|
|
|
|
|
|
|
|
|
set -eE
|
2025-01-10 00:43:26 +08:00
|
|
|
|
os_dir=$1
|
|
|
|
|
|
|
|
|
|
# 该脚本也会在 alpine live 下调用
|
|
|
|
|
# 防止在 alpine live 下运行 systemctl netplan 报错
|
|
|
|
|
systemctl() {
|
|
|
|
|
if systemd-detect-virt --chroot; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
command systemctl "$@"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
netplan() {
|
|
|
|
|
if systemd-detect-virt --chroot; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
command netplan "$@"
|
|
|
|
|
}
|
2024-03-05 21:46:32 +08:00
|
|
|
|
|
|
|
|
|
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
|
2025-01-10 00:43:26 +08:00
|
|
|
|
conf=$os_dir/etc/netplan/50-cloud-init.yaml
|
|
|
|
|
if ! [ -f "$conf" ]; then
|
2024-03-05 21:46:32 +08:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 判断 bug 是否已经修复
|
2025-01-10 00:43:26 +08:00
|
|
|
|
if grep -q 'on-link:' "$conf"; then
|
2024-03-05 21:46:32 +08:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 获取网关
|
2025-01-10 00:43:26 +08:00
|
|
|
|
gateways=$(grep 'gateway[4|6]:' "$conf" | awk '{print $2}')
|
2024-03-05 21:46:32 +08:00
|
|
|
|
if [ -z "$gateways" ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 获取缩进
|
2025-01-10 00:43:26 +08:00
|
|
|
|
spaces=$(grep 'gateway[4|6]:' "$conf" | head -1 | grep -o '^[[:space:]]*')
|
2024-03-05 21:46:32 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
# 网关头部
|
|
|
|
|
cat <<EOF
|
|
|
|
|
${spaces}routes:
|
|
|
|
|
EOF
|
|
|
|
|
# 网关条目
|
|
|
|
|
for gateway in $gateways; do
|
2024-07-07 17:07:22 +08:00
|
|
|
|
# debian 11 的 netplan 不支持 to: default
|
2024-03-05 21:46:32 +08:00
|
|
|
|
case $gateway in
|
|
|
|
|
*.*) to='0.0.0.0/0' ;;
|
|
|
|
|
*:*) to='::/0' ;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
|
${spaces} - to: $to
|
|
|
|
|
${spaces} via: $gateway
|
|
|
|
|
${spaces} on-link: true
|
|
|
|
|
EOF
|
|
|
|
|
done
|
2025-01-10 00:43:26 +08:00
|
|
|
|
} | insert_into_file "$conf" before 'match:'
|
2024-03-05 21:46:32 +08:00
|
|
|
|
|
|
|
|
|
# 删除原来的条目
|
2025-01-10 00:43:26 +08:00
|
|
|
|
sed -i '/gateway[4|6]:/d' "$conf"
|
2024-03-05 21:46:32 +08:00
|
|
|
|
|
|
|
|
|
# 重新应用配置
|
2025-01-01 17:38:29 +08:00
|
|
|
|
if command -v netplan && {
|
2025-01-10 00:43:26 +08:00
|
|
|
|
systemctl -q is-enabled systemd-networkd || systemctl -q is-enabled NetworkManager
|
2025-01-01 17:38:29 +08:00
|
|
|
|
}; then
|
|
|
|
|
netplan apply
|
|
|
|
|
fi
|
2024-03-05 21:46:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fix_networkd_conf() {
|
|
|
|
|
# 修改前 gentoo
|
|
|
|
|
# [Route]
|
|
|
|
|
# Gateway=1.1.1.1
|
|
|
|
|
# Gateway=2602::1
|
|
|
|
|
|
|
|
|
|
# 修改前 arch
|
|
|
|
|
# [Route]
|
|
|
|
|
# Gateway=1.1.1.1
|
|
|
|
|
#
|
|
|
|
|
# [Route]
|
|
|
|
|
# Gateway=2602::1
|
|
|
|
|
|
|
|
|
|
# 修改后
|
|
|
|
|
# [Route]
|
|
|
|
|
# Gateway=1.1.1.1
|
|
|
|
|
# GatewayOnLink=yes
|
|
|
|
|
#
|
|
|
|
|
# [Route]
|
|
|
|
|
# Gateway=2602::1
|
|
|
|
|
# GatewayOnLink=yes
|
|
|
|
|
|
2025-01-10 00:43:26 +08:00
|
|
|
|
if ! confs=$(ls "$os_dir"/etc/systemd/network/10-cloud-init-*.network 2>/dev/null); then
|
2024-03-05 21:46:32 +08:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
for conf in $confs; do
|
|
|
|
|
# 判断 bug 是否已经修复
|
2025-01-10 00:43:26 +08:00
|
|
|
|
if grep -q '^GatewayOnLink=' "$conf"; then
|
2024-03-05 21:46:32 +08:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 获取网关
|
|
|
|
|
gateways=$(grep '^Gateway=' "$conf" | cut -d= -f2)
|
|
|
|
|
if [ -z "$gateways" ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 删除原来的条目
|
|
|
|
|
sed -i '/^\[Route\]/d; /^Gateway=/d; /^GatewayOnLink=/d' "$conf"
|
|
|
|
|
|
|
|
|
|
# 创建新条目
|
|
|
|
|
for gateway in $gateways; do
|
|
|
|
|
echo "
|
|
|
|
|
[Route]
|
|
|
|
|
Gateway=$gateway
|
|
|
|
|
GatewayOnLink=yes
|
|
|
|
|
"
|
|
|
|
|
done >>"$conf"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# 重新应用配置
|
|
|
|
|
# networkctl reload 不起作用
|
2025-01-10 00:43:26 +08:00
|
|
|
|
if systemctl -q is-enabled systemd-networkd; then
|
2025-01-01 17:38:29 +08:00
|
|
|
|
systemctl restart systemd-networkd
|
|
|
|
|
fi
|
2024-03-05 21:46:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 - -
|
|
|
|
|
|
2025-01-10 00:43:26 +08:00
|
|
|
|
if ! confs=$(ls "$os_dir/etc/sysconfig/network/ifroute-"* 2>/dev/null); then
|
2024-03-05 21:46:32 +08:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
for conf in $confs; do
|
|
|
|
|
# 判断 bug 是否已经修复
|
2025-01-10 00:43:26 +08:00
|
|
|
|
if grep -v 'default' "$conf" | grep -q '-'; then
|
2024-03-05 21:46:32 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# 重新应用配置
|
2025-01-10 00:43:26 +08:00
|
|
|
|
if systemctl -q is-enabled wicked; then
|
2025-01-01 17:38:29 +08:00
|
|
|
|
systemctl restart wicked
|
|
|
|
|
fi
|
2024-03-05 21:46:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 17:38:29 +08:00
|
|
|
|
# ubuntu 18.04 cloud-init 版本 23.1.2,因此不用处理
|
|
|
|
|
|
|
|
|
|
# debian 10/11 云镜像原本用 ifupdown + resolvconf,脚本改成用 netplan + networkd/resolved
|
|
|
|
|
# debian 12 云镜像: netplan + networkd/resolved
|
2024-05-22 22:32:43 +08:00
|
|
|
|
# 23.1.1 修复
|
2024-03-05 21:46:32 +08:00
|
|
|
|
fix_netplan_conf
|
|
|
|
|
|
|
|
|
|
# arch: networkd/resolved
|
|
|
|
|
# gentoo: networkd/resolved
|
2024-05-22 22:32:43 +08:00
|
|
|
|
# 24.2 修复
|
2025-01-01 17:38:29 +08:00
|
|
|
|
# 只需对云镜像处理
|
|
|
|
|
# 因为普通安装用的是 alpine 的 cloud-init,版本够新,不用处理
|
2024-03-05 21:46:32 +08:00
|
|
|
|
fix_networkd_conf
|
|
|
|
|
|
|
|
|
|
# opensuse 15.5: ifcfg + netconfig (dns) + wicked
|
|
|
|
|
fix_wicked_conf
|