core: 不再写死 eth0

This commit is contained in:
bin456789 2023-12-05 21:53:24 +08:00
parent 36b20d4f7a
commit 8d546f5a23
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B
3 changed files with 46 additions and 29 deletions

View File

@ -8,6 +8,9 @@ ipv6_addr=$4
ipv6_gateway=$5
is_in_china=$6
# shellcheck disable=SC2154
ethx="$device"
if $is_in_china; then
ipv4_dns1='119.29.29.29'
ipv4_dns2='223.5.5.5'
@ -21,23 +24,23 @@ else
fi
get_first_ipv4_addr() {
ip -4 -o addr show scope global dev eth0 | head -1 | awk '{print $4}'
ip -4 -o addr show scope global dev "$ethx" | head -1 | awk '{print $4}'
}
is_have_ipv4_addr() {
ip -4 addr show scope global dev eth0 | grep -q inet
ip -4 addr show scope global dev "$ethx" | grep -q inet
}
is_have_ipv6_addr() {
ip -6 addr show scope global dev eth0 | grep -q inet6
ip -6 addr show scope global dev "$ethx" | grep -q inet6
}
is_have_ipv4_gateway() {
ip -4 route show default dev eth0 | grep -q .
ip -4 route show default dev "$ethx" | grep -q .
}
is_have_ipv6_gateway() {
ip -6 route show default dev eth0 | grep -q .
ip -6 route show default dev "$ethx" | grep -q .
}
is_have_ipv4() {
@ -48,8 +51,8 @@ is_have_ipv6() {
is_have_ipv6_addr && is_have_ipv6_gateway
}
# 开启 eth0
ip link set dev eth0 up
# 开启 ethx
ip link set dev "$ethx" up
# 等待slaac
# 有ipv6地址就跳过不管是slaac或者dhcpv6
@ -69,12 +72,12 @@ is_have_ipv6_addr && dhcpv6_or_slaac=true || dhcpv6_or_slaac=false
add_missing_ipv4_config() {
if [ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ]; then
if ! is_have_ipv4_addr; then
ip -4 addr add "$ipv4_addr" dev eth0
ip -4 addr add "$ipv4_addr" dev "$ethx"
fi
if ! is_have_ipv4_gateway; then
# 如果 dhcp 无法设置onlink网关那么在这里设置
ip -4 route add default via "$ipv4_gateway" dev eth0 onlink
ip -4 route add default via "$ipv4_gateway" dev "$ethx" onlink
fi
fi
}
@ -82,12 +85,12 @@ add_missing_ipv4_config() {
add_missing_ipv6_config() {
if [ -n "$ipv6_addr" ] && [ -n "$ipv6_gateway" ]; then
if ! is_have_ipv6_addr; then
ip -6 addr add "$ipv6_addr" dev eth0
ip -6 addr add "$ipv6_addr" dev "$ethx"
fi
if ! is_have_ipv6_gateway; then
# 如果 dhcp 无法设置onlink网关那么在这里设置
ip -6 route add default via "$ipv6_gateway" dev eth0 onlink
ip -6 route add default via "$ipv6_gateway" dev "$ethx" onlink
fi
fi
}
@ -135,8 +138,8 @@ test_internet
if $dhcpv4 && ! $ipv4_has_internet &&
! [ "$ipv4_addr" = "$(get_first_ipv4_addr)" ]; then
echo "DHCPv4 can't access Internet. And not match static IPv4."
ip -4 addr flush scope global dev eth0
ip -4 route flush dev eth0
ip -4 addr flush scope global dev "$ethx"
ip -4 route flush dev "$ethx"
add_missing_ipv4_config
test_internet
if $ipv4_has_internet; then
@ -157,10 +160,10 @@ fi
# 此时alpine只会用ipv6下载apk而不用会ipv4下载
# 2 有ipv4地址但没有ipv4网关的情况(vultr)aria2会用ipv4下载
if $ipv4_has_internet && ! $ipv6_has_internet; then
echo 0 >/proc/sys/net/ipv6/conf/eth0/accept_ra
ip -6 addr flush scope global dev eth0
echo 0 >"/proc/sys/net/ipv6/conf/$ethx/accept_ra"
ip -6 addr flush scope global dev "$ethx"
elif ! $ipv4_has_internet && $ipv6_has_internet; then
ip -4 addr flush scope global dev eth0
ip -4 addr flush scope global dev "$ethx"
fi
# 如果联网了,但没获取到默认 DNS则添加我们的 DNS
@ -176,6 +179,7 @@ fi
# 传参给 trans.start
$dhcpv4 && echo 1 >/dev/dhcpv4 || echo 0 >/dev/dhcpv4
$is_in_china && echo 1 >/dev/is_in_china || echo 0 >/dev/is_in_china
echo "$ethx" >/dev/ethx
echo "$mac_addr" >/dev/mac_addr
echo "$ipv4_addr" >/dev/ipv4_addr
echo "$ipv4_gateway" >/dev/ipv4_gateway

View File

@ -1160,7 +1160,16 @@ mod_alpine_initrd() {
modprobe ipv6
EOF
# hack 2
# hack 2 设置 ethx
ip_choose_if() {
ip -o link | grep "@mac_addr" | awk '{print $2}' | cut -d: -f1
return
}
collect_netconf
get_function_content ip_choose_if | sed "s/@mac_addr/$mac_addr/" | insert_into_file init after 'ip_choose_if\(\)'
# hack 3
# udhcpc 添加 -n 参数请求dhcp失败后退出
# 使用同样参数运行 udhcpc6
# TODO: digitalocean -i eth1?
@ -1172,7 +1181,7 @@ EOF
mod_cmd6="${mod_cmd4//udhcpc/udhcpc6}"
sed -i "/$search/c$mod_cmd4 \n $mod_cmd6" init
# hack 3 /usr/share/udhcpc/default.script
# hack 4 /usr/share/udhcpc/default.script
# 脚本被调用的顺序
# udhcpc: deconfig
# udhcpc: bound
@ -1196,8 +1205,7 @@ EOF
# 允许设置 ipv4 onlink 网关
sed -Ei 's,(0\.0\.0\.0\/0),"\1 onlink",' usr/share/udhcpc/default.script
# hack 4 网络配置
collect_netconf
# hack 5 网络配置
is_in_china && is_in_china=true || is_in_china=false
insert_into_file init after 'MAC_ADDRESS=' <<EOF
source /alpine-network.sh \

View File

@ -287,7 +287,9 @@ get_ra_to() {
apk add ndisc6
# 有时会重复收取,所以设置收一份后退出
echo "Gathering network info..."
_ra="$(rdisc6 -1 eth0)"
get_netconf_to ethx
# shellcheck disable=SC2154
_ra="$(rdisc6 -1 "$ethx")"
apk del ndisc6
# 显示网络配置
@ -535,23 +537,25 @@ install_alpine() {
sed -i '/^#slaac hwaddr/s/^#//' /etc/dhcpcd.conf
rc-update add networking boot
# 生成 lo配置 + eth0头部
get_netconf_to ethx
# 生成 lo配置 + ethx头部
cat <<EOF >/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
auto $ethx
EOF
# ipv4
if is_dhcpv4; then
echo "iface eth0 inet dhcp" >>/etc/network/interfaces
echo "iface $ethx inet dhcp" >>/etc/network/interfaces
elif is_staticv4; then
get_netconf_to ipv4_addr
get_netconf_to ipv4_gateway
cat <<EOF >>/etc/network/interfaces
iface eth0 inet static
iface $ethx inet static
address $ipv4_addr
gateway $ipv4_gateway
EOF
@ -567,16 +571,16 @@ EOF
# ipv6
if is_slaac; then
echo 'iface eth0 inet6 auto' >>/etc/network/interfaces
echo "iface $ethx inet6 auto" >>/etc/network/interfaces
elif is_dhcpv6; then
echo 'iface eth0 inet6 dhcp' >>/etc/network/interfaces
echo "iface $ethx inet6 dhcp" >>/etc/network/interfaces
elif is_staticv6; then
get_netconf_to ipv6_addr
get_netconf_to ipv6_gateway
cat <<EOF >>/etc/network/interfaces
iface eth0 inet6 static
iface $ethx inet6 static
address $ipv6_addr
gateway $ipv6_gateway
EOF
@ -918,13 +922,14 @@ mount_pseudo_fs() {
create_cloud_init_network_config() {
ci_file=$1
get_netconf_to ethx
get_netconf_to mac_addr
apk add yq
# shellcheck disable=SC2154
yq -i ".network.version=1 |
.network.config[0].type=\"physical\" |
.network.config[0].name=\"eth0\" |
.network.config[0].name=\"$ethx\" |
.network.config[0].mac_address=\"$mac_addr\" |
.network.config[1].type=\"nameserver\"
" $ci_file