-
-
Notifications
You must be signed in to change notification settings - Fork 551
/
cloud-init-fix-onlink.sh
200 lines (165 loc) · 4.09 KB
/
cloud-init-fix-onlink.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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 <<EOF
${spaces}routes:
EOF
# 网关条目
for gateway in $gateways; do
# debian 11 的 netplan 不支持 to: default
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
} | insert_into_file $conf before 'match:'
# 删除原来的条目
sed -i '/gateway[4|6]:/d' $conf
# 重新应用配置
netplan apply
systemctl restart systemd-networkd
}
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
if ! confs=$(ls /etc/systemd/network/10-cloud-init-*.network 2>/dev/null); then
return
fi
for conf in $confs; do
# 判断 bug 是否已经修复
if grep '^GatewayOnLink=' "$conf"; then
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 不起作用
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-* 2>/dev/null); 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 11/12: netplan + networkd/resolved
# 23.1.1 修复
fix_netplan_conf
# arch: networkd/resolved
# gentoo: networkd/resolved
# 24.2 修复
fix_networkd_conf
# opensuse 15.5: ifcfg + netconfig (dns) + wicked
fix_wicked_conf