core: 修复 alpine dhcpv6

This commit is contained in:
bin456789 2023-08-23 14:17:45 +08:00
parent c30c5ecc4c
commit 24befd78ba
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B
3 changed files with 66 additions and 24 deletions

View File

@ -20,40 +20,57 @@ else
ipv6_dns2='2001:4860:4860::8888'
fi
get_ipv4_entry() {
ip -4 addr show scope global dev eth0 | grep inet
}
get_ipv6_entry() {
ip -6 addr show scope global dev eth0 | grep inet6
}
is_have_ipv4() {
[ -n "$(get_ipv4_entry)" ]
}
is_have_ipv6() {
[ -n "$(get_ipv6_entry)" ]
}
# 开启 eth0
ip link set dev eth0 up
# 检测是否有 dhcpv4
has_ipv4=false
dhcpv4=false
ip -4 addr show scope global dev eth0 | grep inet && dhcpv4=true && has_ipv4=true
# 由于还没设置静态ip所以有条目表示有 dhcpv4
get_ipv4_entry && dhcpv4=true || dhcpv4=false
# 检测是否有 dhcpv6
# dhcpv4 肯定是 /128
get_ipv6_entry | grep /128 && dhcpv6=true || dhcpv6=false
# 检测是否有 slaac
has_ipv6=false
slaac=false
for i in $(seq 10 -1 0); do
echo waiting slaac for ${i}s
ip -6 addr show scope global dev eth0 | grep inet6 && slaac=true && has_ipv6=true && break
echo "waiting slaac for ${i}s"
get_ipv6_entry | grep -v /128 && slaac=true && break
sleep 1
done
# 设置静态地址
# udhcpc不支持dhcpv6所以如果网络是 dhcpv6也先设置成静态
ip link set dev eth0 up
if ! $has_ipv4 && [ -n "$ipv4_addr" ]; then
if ! is_have_ipv4 && [ -n "$ipv4_addr" ]; then
ip -4 addr add $ipv4_addr dev eth0
ip -4 route add default via $ipv4_gateway
has_ipv4=true
fi
if ! $has_ipv6 && [ -n "$ipv6_addr" ]; then
if ! is_have_ipv6 && [ -n "$ipv6_addr" ]; then
ip -6 addr add $ipv6_addr dev eth0
ip -6 route add default via $ipv6_gateway
has_ipv6=true
fi
# 检查 ipv4/ipv6 是否连接联网
ipv4_has_internet=false
ipv6_has_internet=false
for i in $(seq 10); do
$has_ipv4 && ipv4_test_complete=false || ipv4_test_complete=true
$has_ipv6 && ipv6_test_complete=false || ipv6_test_complete=true
is_have_ipv4 && ipv4_test_complete=false || ipv4_test_complete=true
is_have_ipv6 && ipv6_test_complete=false || ipv6_test_complete=true
if ! $ipv4_test_complete && nslookup www.qq.com $ipv4_dns1; then
ipv4_has_internet=true
@ -71,8 +88,10 @@ for i in $(seq 10); do
done
# 等待 udhcpc 创建 /etc/resolv.conf
if { $dhcpv4 || $slaac; } && [ ! -e /etc/resolv.conf ]; then
sleep 3
# 好像只有 dhcpv4 会创建 resolv.conf
if { $dhcpv4 || $dhcpv6 || $slaac; } && [ ! -e /etc/resolv.conf ]; then
echo "waiting for /etc/resolv.conf"
sleep 5
fi
# 如果ipv4/ipv6不联网则删除该协议的dns
@ -93,7 +112,7 @@ if $ipv6_has_internet && ! grep ':' /etc/resolv.conf; then
fi
# 传参给 trans.start
has_ipv4=true && echo 1>/dev/has_dhcpv4 || echo 0 >/sys/has_dhcpv4
$dhcpv4 && echo 1 >/dev/dhcpv4 || echo 0 >/dev/dhcpv4
echo $ipv4_addr >/dev/ipv4_addr
echo $ipv4_gateway >/dev/ipv4_gateway
echo $ipv6_addr >/dev/ipv6_addr

View File

@ -946,11 +946,35 @@ mod_alpine_initrd() {
modprobe ipv6
EOF
# hack 2 udhcpc添加 -n 参数中断静态ipv4时dhcp无限请求
# hack 2
# udhcpc 添加 -n 参数请求dhcp失败后退出
# 使用同样参数运行 udhcpc6
# TODO: digitalocean -i eth1?
# shellcheck disable=SC2016
sed -i '/$MOCK udhcpc/s/.*/& -n || true/' init
orig_cmd="$(grep '$MOCK udhcpc' init)"
mod_cmd4="$orig_cmd -n || true"
mod_cmd6="${mod_cmd4//udhcpc/udhcpc6}"
sed -i "/\$MOCK udhcpc/c$mod_cmd4 \n $mod_cmd6" init
# hack 3 网络配置
# hack 3 /usr/share/udhcpc/default.script
# 脚本被调用的顺序
# udhcpc: deconfig
# udhcpc: bound
# udhcpc6: deconfig
# udhcpc6: bound
line_num=$(grep -E -n 'deconfig\|renew\|bound' usr/share/udhcpc/default.script | cut -d: -f1)
cat <<EOF | sed -i "${line_num}r /dev/stdin" usr/share/udhcpc/default.script
if [ "\$1" = deconfig ]; then
return
fi
if [ "\$1" = bound ] && [ -n "\$ipv6" ]; then
ip -6 addr add \$ipv6 dev \$interface
ip link set dev \$interface up
return
fi
EOF
# hack 4 网络配置
collect_netconf
line_num=$(grep -E -n 'MAC_ADDRESS=' init | cut -d: -f1)
cat <<EOF | sed -i "${line_num}r /dev/stdin" init
@ -959,7 +983,7 @@ EOF
"$(is_in_china && echo true || echo false)"
EOF
# hack 4 运行 trans.start
# hack 5 运行 trans.start
# exec /bin/busybox switch_root $switch_root_opts $sysroot $chart_init "$KOPT_init" $KOPT_init_args # 3.17
# exec switch_root $switch_root_opts $sysroot $chart_init "$KOPT_init" $KOPT_init_args # 3.18
line_num=$(grep -E -n '^exec (/bin/busybox )?switch_root' init | cut -d: -f1)

View File

@ -251,8 +251,8 @@ EOF
# 生成 ipv4 配置
echo >>/etc/network/interfaces
# dhcpv4
if [ "$(get_netconf has_dhcpv4)" = 1 ]; then
cat <<EOF >/etc/network/interfaces
if [ "$(get_netconf dhcpv4)" = 1 ]; then
cat <<EOF >>/etc/network/interfaces
auto eth0
iface eth0 inet dhcp
EOF
@ -271,7 +271,6 @@ EOF
fi
# 生成 ipv6 配置
echo >>/etc/network/interfaces
apk add ndisc6
is_slaac=false
is_dhcpv6=false