From e51774d4d88e2d0bad45d2a4250aad2ac5628098 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Mon, 21 Aug 2023 23:50:20 +0800 Subject: [PATCH] =?UTF-8?q?core:=20=E5=AE=8C=E6=88=90=20alpine=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E7=9A=84=E9=9D=99=E6=80=81ip/ipv6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alpine-network.sh | 93 +++++++++++++++++++++++++++++++++++++++++++++++ reinstall.sh | 49 ++++++++++++++++++++++++- trans.sh | 73 ++++++++++++++++++++++++++++++------- 3 files changed, 200 insertions(+), 15 deletions(-) create mode 100644 alpine-network.sh diff --git a/alpine-network.sh b/alpine-network.sh new file mode 100644 index 0000000..484bab7 --- /dev/null +++ b/alpine-network.sh @@ -0,0 +1,93 @@ +#!/bin/ash +# shellcheck shell=dash + +mac_addr=$1 +ipv4_addr=$2 +ipv4_gateway=$3 +ipv6_addr=$4 +ipv6_gateway=$5 +is_in_china=$6 + +if $is_in_china; then + ipv4_dns1='119.29.29.29' + ipv4_dns2='223.5.5.5' + ipv6_dns1='2402:4e00::' + ipv6_dns2='2400:3200::1' +else + ipv4_dns1='1.1.1.1' + ipv4_dns2='8.8.8.8' + ipv6_dns1='2606:4700:4700::1111' + ipv6_dns2='2001:4860:4860::8888' +fi + +# 检测是否有 dhcpv4 +has_ipv4=false +dhcpv4=false +ip -4 addr show scope global | grep inet && dhcpv4=true && has_ipv4=true + +# 检测是否有 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 | grep inet6 && slaac=true && has_ipv6=true && break + sleep 1 +done + +# 设置静态地址 +# udhcpc不支持dhcpv6,所以如果网络是 dhcpv6,也先设置成静态 +ip link set dev eth0 up +if ! $has_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 + 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 + + if ! $ipv4_test_complete && nslookup www.qq.com $ipv4_dns1; then + ipv4_has_internet=true + ipv4_test_complete=true + fi + if ! $ipv6_test_complete && nslookup www.qq.com $ipv6_dns1; then + ipv6_has_internet=true + ipv6_test_complete=true + fi + + if $ipv4_test_complete && $ipv6_test_complete; then + break + fi + sleep 1 +done + +# 等待 udhcpc 创建 /etc/resolv.conf +if { $dhcpv4 || $slaac; } && [ ! -e /etc/resolv.conf ]; then + sleep 3 +fi + +# 如果ipv4/ipv6不联网,则删除该协议的dns +if $ipv4_has_internet && ! $ipv6_has_internet; then + sed -i '/:/d' /etc/resolv.conf +elif ! $ipv4_has_internet && $ipv6_has_internet; then + sed -i '/\./d' /etc/resolv.conf +fi + +# 如果联网了,但没获取到默认 DNS,则添加我们的 DNS +if $ipv4_has_internet && ! grep '\.' /etc/resolv.conf; then + echo "nameserver $ipv4_dns1" >>/etc/resolv.conf + echo "nameserver $ipv4_dns2" >>/etc/resolv.conf +fi +if $ipv6_has_internet && ! grep ':' /etc/resolv.conf; then + echo "nameserver $ipv6_dns1" >>/etc/resolv.conf + echo "nameserver $ipv6_dns2" >>/etc/resolv.conf +fi diff --git a/reinstall.sh b/reinstall.sh index 7753d34..b773cbd 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -596,6 +596,28 @@ is_efi() { fi } +collect_netconf() { + if is_in_windows; then + # TODO: + echo + else + # TODO: 多网卡 单网卡多IP + nic_name=$(ip -o addr show scope global | head -1 | awk '{print $2}') + mac_addr=$(ip addr show scope global | grep link/ether | head -1 | awk '{print $2}') + ipv4_addr=$(ip -4 addr show scope global | grep inet | head -1 | awk '{print $2}') + ipv6_addr=$(ip -6 addr show scope global | grep inet6 | head -1 | awk '{print $2}') + + ipv4_gateway=$(ip -4 route show default dev $nic_name | awk '{print $3}') + ipv6_gateway=$(ip -6 route show default dev $nic_name | awk '{print $3}') + + echo 1 $mac_addr + echo 2 $ipv4_addr + echo 3 $ipv4_gateway + echo 4 $ipv6_addr + echo 5 $ipv6_gateway + fi +} + install_grub_win() { grub_cfg=$1 # /cygdrive/$c/grub.cfg @@ -897,6 +919,7 @@ mod_alpine_initrd() { # 预先下载脚本 curl -Lo $tmp_dir/trans.start $confhome/trans.sh + curl -Lo $tmp_dir/alpine-network.sh $confhome/alpine-network.sh # virt 内核添加 ipv6 模块 if virt_dir=$(ls -d $tmp_dir/lib/modules/*-virt 2>/dev/null); then @@ -916,23 +939,45 @@ mod_alpine_initrd() { fi fi - # hack 运行 trans.start + # hack 1 添加 ipv6 模块 + line_num=$(grep -E -n 'configure_ip\(\)' init | cut -d: -f1) + cat <\$sysroot/etc/local.d/trans.start # wget --no-check-certificate -O \$sysroot/etc/local.d/trans.start $confhome/trans.sh cp /trans.start \$sysroot/etc/local.d/trans.start chmod a+x \$sysroot/etc/local.d/trans.start ln -s /etc/init.d/local \$sysroot/etc/runlevels/default/ EOF + # 重建 # 注意要用 cpio -H newc 不要用 cpio -c ,不同版本的 -c 作用不一样,很坑 # -c Use the old portable (ASCII) archive format diff --git a/trans.sh b/trans.sh index eb1583f..c79d399 100644 --- a/trans.sh +++ b/trans.sh @@ -219,10 +219,69 @@ install_alpine() { rm -f /etc/local.d/trans.start rm -f /etc/runlevels/default/local + # 添加 virt-what 用到的社区仓库 + add_community_repo + + # 保存默认应用列表 + if [ ! -e /tmp/world.orig ]; then + cp /etc/apk/world /tmp/world.orig + fi + + # 如果是 vm 就用 virt 内核 + apk add virt-what + if [ -n "$(virt-what)" ]; then + kernel_opt="-k virt" + fi + # 网络 - setup-interfaces -a # 生成 /etc/network/interfaces + # 坑1 udhcpc下,ip -4 addr 无法知道是否是 dhcp + # 坑2 udhcpc不支持dhcpv6 + # 坑3 安装 dhcpcd,会强制使用dhcpv6,即使ra m=0,解决方法是配置文件设置auto rc-update add networking boot + # 生成 ipv4 配置 + if udhcpc -i eth0 -f -q -n; then + # 生成dhcpv4配置 + setup-interfaces -a + else + # 如果当前有ipv4地址,则会生成静态配置 + # 如果当前没有ipv4地址,则会生成dhcpv4配置 + echo | setup-interfaces + fi + + # 生成 ipv6 配置 + apk add ndisc6 + ra=$(rdisc6 eth0) + echo "$ra" | cat -n + echo "$ra" | grep 'Autonomous address conf' | grep Yes && is_slaac=true || is_slaac=false + echo "$ra" | grep 'Stateful address conf' | grep Yes && is_dhcpv6=true || is_dhcpv6=false + + # 删除临时安装的软件,不然会带到新系统 + # shellcheck disable=SC2046 + apk del $(diff /tmp/world.orig /etc/apk/world | grep '^+' | sed '1d' | sed 's/^+//' | xargs) + + if $is_slaac; then + echo 'iface eth0 inet6 auto' >>/etc/network/interfaces + fi + + if $is_dhcpv6; then + apk add dhcpcd + echo 'iface eth0 inet6 dhcp' >>/etc/network/interfaces + fi + + if ! $is_slaac && ! $is_dhcpv6; then + ipv6_addr=$(ip -6 addr show scope global dev eth0 | grep inet6 | head -1 | awk '{print $2}') + ipv6_gateway=$(ip -6 route show default dev eth0 | awk '{print $3}') + if [ -n "$ipv6_addr" ]; then + cat <>/etc/network/interfaces +iface eth0 inet6 static + address $ipv6_addr + gateway $ipv6_gateway +EOF + fi + fi + cat -n /etc/network/interfaces + # 设置 setup-keymap us us setup-timezone -i Asia/Shanghai @@ -242,18 +301,6 @@ install_alpine() { # crond | default # seedrng | boot - # 添加 virt-what 用到的社区仓库 - add_community_repo - - # 如果是 vm 就用 virt 内核 - cp /etc/apk/world /tmp/world.old - apk add virt-what - if [ -n "$(virt-what)" ]; then - kernel_opt="-k virt" - fi - # 删除 virt-what 和依赖,不然会带到新系统 - apk del "$(diff /tmp/world.old /etc/apk/world | grep '^+' | sed '1d' | sed 's/^+//')" - # 重置为官方仓库配置 true >/etc/apk/repositories setup-apkrepos -1