From ada822deb3dcf62818e451f89392cef9c8b8098f Mon Sep 17 00:00:00 2001 From: bin456789 Date: Sat, 16 Mar 2024 23:06:38 +0800 Subject: [PATCH] =?UTF-8?q?debian:=20=E6=94=AF=E6=8C=81=E4=BC=A0=E7=BB=9F?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E6=A8=A1=E5=BC=8F=20+=20=E9=9D=99=E6=80=81?= =?UTF-8?q?=20IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alpine-network.sh | 16 +++-- reinstall.sh | 135 ++++++++++++++++++++++++++++++++------ trans.sh | 160 ++++++++++++++++++++++++++++------------------ 3 files changed, 223 insertions(+), 88 deletions(-) diff --git a/alpine-network.sh b/alpine-network.sh index 74a05e9..3c52978 100644 --- a/alpine-network.sh +++ b/alpine-network.sh @@ -1,5 +1,6 @@ #!/bin/ash # shellcheck shell=dash +# alpine / debian initrd 共用此脚本 mac_addr=$1 ipv4_addr=$2 @@ -9,7 +10,8 @@ ipv6_gateway=$5 is_in_china=$6 # 3.16-3.18 $device -# 3.19.1+ $iface +# 3.19 $iface +# debian $iface # shellcheck disable=SC2154 if [ -n "$iface" ]; then ethx="$iface" @@ -30,7 +32,12 @@ else fi get_first_ipv4_addr() { - ip -4 -o addr show scope global dev "$ethx" | head -1 | awk '{print $4}' + # debian 11 initrd 没有 awk + if false; then + ip -4 -o addr show scope global dev "$ethx" | head -1 | awk '{print $4}' + else + ip -4 -o addr show scope global dev "$ethx" | head -1 | grep -o '[0-9\.]*/[0-9]*' + fi } is_have_ipv4_addr() { @@ -120,12 +127,13 @@ is_need_test_ipv6() { test_internet() { echo 'Testing Internet Connection...' + # debian 没有 nslookup,因此用 ping for i in $(seq 5); do - if is_need_test_ipv4 && nslookup www.qq.com $ipv4_dns1 >/dev/null 2>&1; then + if is_need_test_ipv4 && ping -c1 -W5 $ipv4_dns1 >/dev/null 2>&1; then echo "IPv4 has internet." ipv4_has_internet=true fi - if is_need_test_ipv6 && nslookup www.qq.com $ipv6_dns1 >/dev/null 2>&1; then + if is_need_test_ipv6 && ping -c1 -W5 $ipv6_dns1 >/dev/null 2>&1; then echo "IPv6 has internet." ipv6_has_internet=true fi diff --git a/reinstall.sh b/reinstall.sh index 2738c88..b4d31ee 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -1368,22 +1368,96 @@ mkdir_clear() { mkdir -p $dir } -mod_alpine_initrd() { - # 修改 alpine 启动时运行我们的脚本 - info mod alpine initrd - install_pkg gzip cpio +mod_initrd_debian() { + # hack 1 + # 允许设置 ipv4 onlink 网关 + sed -Ei 's,&&( onlink=),||\1,' etc/udhcpc/default.script - # 解压 - # 先删除临时文件,避免之前运行中断有残留文件 - tmp_dir=/tmp/reinstall - mkdir_clear $tmp_dir - cd $tmp_dir - zcat /reinstall-initrd | cpio -idm + # hack 2 + # 修改 /var/lib/dpkg/info/netcfg.postinst 运行我们的脚本 + # shellcheck disable=SC1091,SC2317 + netcfg() { + #!/bin/sh + . /usr/share/debconf/confmodule + db_progress START 0 5 debian-installer/netcfg/title - # 预先下载脚本 - curl -Lo $tmp_dir/trans.start $confhome/trans.sh - curl -Lo $tmp_dir/alpine-network.sh $confhome/alpine-network.sh + # 找到主网卡 + # debian 11 initrd 没有 awk + if false; then + iface=$(ip -o link | grep "@mac_addr" | awk '{print $2}' | cut -d: -f1) + else + iface=$(ip -o link | grep "@mac_addr" | cut -d' ' -f2 | cut -d: -f1) + fi + db_progress STEP 1 + # dhcpv4 + db_progress INFO netcfg/dhcp_progress + udhcpc -i "$iface" -f -q -n + db_progress STEP 1 + + # slaac + dhcpv6 + db_progress INFO netcfg/slaac_wait_title + # https://salsa.debian.org/installer-team/netcfg/-/blob/master/autoconfig.c#L148 + cat </var/lib/netcfg/dhcp6c.conf +interface $iface { + send ia-na 0; + request domain-name-servers; + request domain-name; + script "/lib/netcfg/print-dhcp6c-info"; +}; + +id-assoc na 0 { +}; +EOF + dhcp6c -c /var/lib/netcfg/dhcp6c.conf "$iface" + sleep 10 + kill -9 "$(cat /var/run/dhcp6c.pid)" + db_progress STEP 1 + + # 静态 + 检测网络 + db_subst netcfg/link_detect_progress interface "$iface" + db_progress INFO netcfg/link_detect_progress + . /alpine-network.sh @netconf + db_progress STEP 1 + + # 运行trans.sh,保存配置 + db_progress INFO base-installer/progress/netcfg + . /trans.sh + db_progress STEP 1 + } + + collect_netconf + is_in_china && is_in_china=true || is_in_china=false + netconf="'$mac_addr' '$ipv4_addr' '$ipv4_gateway' '$ipv6_addr' '$ipv6_gateway' '$is_in_china'" + + get_function_content netcfg | + sed "s|@mac_addr|$mac_addr|" | + sed "s|@netconf|$netconf|" >var/lib/dpkg/info/netcfg.postinst + + # hack 3 + # 修改 trans.sh + # 1. 直接调用 create_ifupdown_config + insert_into_file $tmp_dir/trans.sh after ': main' < > + # 5. debian 11/12 initrd 无法识别 < < + # 6. debian 11 initrd 无法识别 set -E + # 7. debian 11 initrd 无法识别 trap ERR + # 删除或注释,可能会导致空方法而报错,因此改为替换成'\n: #' + replace='\n: #' + sed -Ei "s/> >/$replace/" $tmp_dir/trans.sh + sed -Ei "s/< /dev/null); then ipv6_dir=$virt_dir/kernel/net/ipv6 @@ -1410,13 +1484,14 @@ EOF # hack 2 设置 ethx # 3.16~3.18 ip_choose_if - # 3.19.1+ ethernets + # 3.19 ethernets if grep -q ip_choose_if init; then ethernets_func=ip_choose_if else ethernets_func=ethernets fi + # shellcheck disable=SC2317 ip_choose_if() { ip -o link | grep "@mac_addr" | awk '{print $2}' | cut -d: -f1 return @@ -1431,7 +1506,7 @@ EOF # 使用同样参数运行 udhcpc6 # udhcpc -i "$device" -f -q # v3.17 # $MOCK udhcpc -i "$device" -f -q # v3.18 - # $MOCK udhcpc -i "$iface" -f -q # v3.19.1 + # $MOCK udhcpc -i "$iface" -f -q # v3.19 search='udhcpc -i' orig_cmd="$(grep "$search" init)" mod_cmd4="$orig_cmd -n || true" @@ -1444,12 +1519,13 @@ EOF # udhcpc: bound # udhcpc6: deconfig # udhcpc6: bound - # shellcheck disable=SC2154 + # shellcheck disable=SC2317 udhcpc() { if [ "$1" = deconfig ]; then return fi if [ "$1" = bound ] && [ -n "$ipv6" ]; then + # shellcheck disable=SC2154 ip -6 addr add "$ipv6" dev "$interface" ip link set dev "$interface" up return @@ -1465,7 +1541,7 @@ EOF # hack 5 网络配置 is_in_china && is_in_china=true || is_in_china=false insert_into_file init after 'MAC_ADDRESS=' <\$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 + cp /trans.sh \$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 +} + +mod_initrd() { + info "mod $nextos_distro initrd" + install_pkg gzip cpio + + # 解压 + # 先删除临时文件,避免之前运行中断有残留文件 + tmp_dir=/tmp/reinstall + mkdir_clear $tmp_dir + cd $tmp_dir + zcat /reinstall-initrd | cpio -idm + + curl -Lo $tmp_dir/trans.sh $confhome/trans.sh + curl -Lo $tmp_dir/alpine-network.sh $confhome/alpine-network.sh + + mod_initrd_$nextos_distro # 重建 # 注意要用 cpio -H newc 不要用 cpio -c ,不同版本的 -c 作用不一样,很坑 @@ -1702,9 +1795,9 @@ else curl -Lo /reinstall-initrd $nextos_initrd fi -# 修改 alpine initrd -if [ "$nextos_distro" = alpine ]; then - mod_alpine_initrd +# 修改 alpine debian initrd +if [ "$nextos_distro" = alpine ] || [ "$nextos_distro" = debian ]; then + mod_initrd fi # 将内核/netboot.xyz.lkrn 放到正确的位置 diff --git a/trans.sh b/trans.sh index a73ef8e..10e2116 100644 --- a/trans.sh +++ b/trans.sh @@ -376,10 +376,11 @@ get_netconf_to() { esac # shellcheck disable=SC2154 + # debian initrd 没有 xargs case "$1" in slaac) echo "$ra" | grep 'Autonomous address conf' | grep Yes && res=1 || res=0 ;; dhcpv6) echo "$ra" | grep 'Stateful address conf' | grep Yes && res=1 || res=0 ;; - rdnss) res=$(echo "$ra" | grep 'Recursive DNS server' | cut -d: -f2- | xargs) ;; + rdnss) res=$(echo "$ra" | grep 'Recursive DNS server' | cut -d: -f2-) ;; other) echo "$ra" | grep 'Stateful other conf' | grep Yes && res=1 || res=0 ;; *) res=$(cat /dev/$1) ;; esac @@ -485,11 +486,21 @@ is_need_manual_set_dnsv6() { } get_current_dns_v4() { - grep '^nameserver' /etc/resolv.conf | awk '{print $2}' | grep '\.' + # debian 11 initrd 没有 awk + if false; then + grep '^nameserver' /etc/resolv.conf | awk '{print $2}' | grep '\.' + else + grep '^nameserver' /etc/resolv.conf | cut -d' ' -f2 | grep '\.' + fi } get_current_dns_v6() { - grep '^nameserver' /etc/resolv.conf | awk '{print $2}' | grep ':' + # debian 11 initrd 没有 awk + if false; then + grep '^nameserver' /etc/resolv.conf | awk '{print $2}' | grep ':' + else + grep '^nameserver' /etc/resolv.conf | cut -d' ' -f2 | grep ':' + fi } to_upper() { @@ -612,6 +623,83 @@ insert_into_file() { fi } +create_ifupdown_config() { + conf_file=$1 + + rm -f $conf_file + + # shellcheck disable=SC2154 + if [ "$distro" = debian ]; then + cat <>$conf_file +source /etc/network/interfaces.d/* + +EOF + fi + + # 生成 lo配置 + ethx头部 + get_netconf_to ethx + if [ -f /etc/network/devhotplug ] && grep -wo "$ethx" /etc/network/devhotplug; then + mode=allow-hotplug + else + mode=auto + fi + cat <>$conf_file +auto lo +iface lo inet loopback + +$mode $ethx +EOF + + # ipv4 + if is_dhcpv4; then + echo "iface $ethx inet dhcp" >>$conf_file + + elif is_staticv4; then + get_netconf_to ipv4_addr + get_netconf_to ipv4_gateway + cat <>$conf_file +iface $ethx inet static + address $ipv4_addr + gateway $ipv4_gateway +EOF + # dns + if list=$(get_current_dns_v4); then + for dns in $list; do + cat <>$conf_file + dns-nameservers $dns +EOF + done + fi + fi + + # ipv6 + if is_slaac; then + echo "iface $ethx inet6 auto" >>$conf_file + + elif is_dhcpv6; then + echo "iface $ethx inet6 dhcp" >>$conf_file + + elif is_staticv6; then + get_netconf_to ipv6_addr + get_netconf_to ipv6_gateway + cat <>$conf_file +iface $ethx inet6 static + address $ipv6_addr + gateway $ipv6_gateway +EOF + fi + + # dns + # 有 ipv6 但需设置 dns 的情况 + if is_need_manual_set_dnsv6 && list=$(get_current_dns_v6); then + for dns in $list; do + cat <>$conf_file + dns-nameserver $dns +EOF + done + fi +} + install_alpine() { hack_lowram_modloop=true hack_lowram_swap=true @@ -675,66 +763,8 @@ install_alpine() { sed -i '/^#slaac hwaddr/s/^#//' /etc/dhcpcd.conf rc-update add networking boot - get_netconf_to ethx - - # 生成 lo配置 + ethx头部 - cat </etc/network/interfaces -auto lo -iface lo inet loopback - -auto $ethx -EOF - - # ipv4 - if is_dhcpv4; then - echo "iface $ethx inet dhcp" >>/etc/network/interfaces - - elif is_staticv4; then - get_netconf_to ipv4_addr - get_netconf_to ipv4_gateway - cat <>/etc/network/interfaces -iface $ethx inet static - address $ipv4_addr - gateway $ipv4_gateway -EOF - # dns - if list=$(get_current_dns_v4); then - for dns in $list; do - cat <>/etc/network/interfaces - dns-nameserver $dns -EOF - done - fi - fi - - # ipv6 - if is_slaac; then - echo "iface $ethx inet6 auto" >>/etc/network/interfaces - - elif is_dhcpv6; then - echo "iface $ethx inet6 dhcp" >>/etc/network/interfaces - - elif is_staticv6; then - get_netconf_to ipv6_addr - get_netconf_to ipv6_gateway - cat <>/etc/network/interfaces -iface $ethx inet6 static - address $ipv6_addr - gateway $ipv6_gateway -EOF - fi - - # dns - # 有 ipv6 但需设置 dns 的情况 - if is_need_manual_set_dnsv6 && list=$(get_current_dns_v6); then - for dns in $list; do - cat <>/etc/network/interfaces - dns-nameserver $dns -EOF - done - fi - - # 显示网络配置 + # 网络配置 + create_ifupdown_config /etc/network/interfaces echo cat -n /etc/network/interfaces echo @@ -2504,6 +2534,10 @@ EOF } # 脚本入口 +# debian initrd 会寻找 : main +# 并调用本文件的 create_ifupdown_config 方法 +: main + # arm要手动从硬件同步时间,避免访问https出错 # do 机器第二次运行会报错 hwclock -s || true