mirror of
https://github.com/bin456789/reinstall.git
synced 2025-01-18 20:39:14 +08:00
core: 完成 alpine 环境的静态ip/ipv6配置
This commit is contained in:
parent
23a6a5c2cc
commit
e51774d4d8
93
alpine-network.sh
Normal file
93
alpine-network.sh
Normal file
@ -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
|
49
reinstall.sh
49
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 <<EOF | sed -i "${line_num}r /dev/stdin" init
|
||||
depmod
|
||||
modprobe ipv6
|
||||
EOF
|
||||
|
||||
# hack 2 udhcpc添加 -n 参数,中断静态ipv4时dhcp无限请求
|
||||
# shellcheck disable=SC2016
|
||||
sed -i '/$MOCK udhcpc/s/.*/& -n || true/' init
|
||||
|
||||
# hack 3 网络配置
|
||||
collect_netconf
|
||||
line_num=$(grep -E -n 'MAC_ADDRESS=' init | cut -d: -f1)
|
||||
cat <<EOF | sed -i "${line_num}r /dev/stdin" init
|
||||
source /alpine-network.sh \
|
||||
"$mac_addr" "$ipv4_addr" "$ipv4_gateway" "$ipv6_addr" "$ipv6_gateway" \
|
||||
"$(is_in_china && echo true || echo false)"
|
||||
EOF
|
||||
|
||||
# hack 4 运行 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)
|
||||
line_num=$((line_num - 1))
|
||||
# 1. alpine arm initramfs 时间问题 要添加 --no-check-certificate
|
||||
# 2. 神奇的 aws t4g arm 会出现bad header错误, 但甲骨文arm就正常,所以只能预先下载
|
||||
# 2. aws t4g arm 如果没设置console=ttyx,在initramfs里面wget https会出现bad header错误,chroot后正常
|
||||
# Connecting to raw.githubusercontent.com (185.199.108.133:443)
|
||||
# 60C0BB2FFAFF0000:error:0A00009C:SSL routines:ssl3_get_record:http request:ssl/record/ssl3_record.c:345:
|
||||
# ssl_client: SSL_connect
|
||||
# wget: bad header line: <20>
|
||||
cat <<EOF | sed -i "${line_num}r /dev/stdin" init
|
||||
# echo "wget --no-check-certificate -O- $confhome/trans.sh | /bin/ash" >\$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
|
||||
|
73
trans.sh
73
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 <<EOF >>/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
|
||||
|
Loading…
x
Reference in New Issue
Block a user