mirror of
https://github.com/bin456789/reinstall.git
synced 2025-01-19 04:49:13 +08:00
core: 完善 slaac 下的 dns 配置
This commit is contained in:
parent
87af5d63b5
commit
22779c1dcd
153
trans.sh
153
trans.sh
@ -245,6 +245,7 @@ get_ra_to() {
|
|||||||
if [ -z "$_ra" ]; then
|
if [ -z "$_ra" ]; then
|
||||||
apk add ndisc6
|
apk add ndisc6
|
||||||
# 有时会重复收取,所以设置收一份后退出
|
# 有时会重复收取,所以设置收一份后退出
|
||||||
|
echo "Gathering network info..."
|
||||||
_ra="$(rdisc6 -1 eth0)"
|
_ra="$(rdisc6 -1 eth0)"
|
||||||
apk del ndisc6
|
apk del ndisc6
|
||||||
fi
|
fi
|
||||||
@ -253,7 +254,7 @@ get_ra_to() {
|
|||||||
|
|
||||||
get_netconf_to() {
|
get_netconf_to() {
|
||||||
case "$1" in
|
case "$1" in
|
||||||
slaac | dhcpv6 | rdnss | is_in_china) get_ra_to ra ;;
|
slaac | dhcpv6 | rdnss | other) get_ra_to ra ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
@ -261,6 +262,7 @@ get_netconf_to() {
|
|||||||
slaac) echo "$ra" | grep 'Autonomous address conf' | grep Yes && res=1 || res=0 ;;
|
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 ;;
|
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- | xargs) ;;
|
||||||
|
other) echo "$ra" | grep 'Stateful other conf' | grep Yes && res=1 || res=0 ;;
|
||||||
*) res=$(cat /dev/$1) ;;
|
*) res=$(cat /dev/$1) ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@ -315,6 +317,34 @@ is_dhcpv6() {
|
|||||||
[ "$dhcpv6" = 1 ]
|
[ "$dhcpv6" = 1 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_have_ipv6() {
|
||||||
|
is_slaac || is_dhcpv6 || is_staticv6
|
||||||
|
}
|
||||||
|
|
||||||
|
is_enable_other_flag() {
|
||||||
|
get_netconf_to other
|
||||||
|
# shellcheck disable=SC2154
|
||||||
|
[ "$other" = 1 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
is_have_rdnss() {
|
||||||
|
get_netconf_to rdnss
|
||||||
|
[ -n "$rdnss" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
is_need_manual_set_dnsv6() {
|
||||||
|
# 有没有可能是静态但是有 rdnss?
|
||||||
|
is_have_ipv6 && ! is_have_rdnss && ! is_dhcpv6 && ! is_enable_other_flag
|
||||||
|
}
|
||||||
|
|
||||||
|
get_current_dns_v4() {
|
||||||
|
grep '^nameserver' /etc/resolv.conf | awk '{print $2}' | grep '\.'
|
||||||
|
}
|
||||||
|
|
||||||
|
get_current_dns_v6() {
|
||||||
|
grep '^nameserver' /etc/resolv.conf | awk '{print $2}' | grep ':'
|
||||||
|
}
|
||||||
|
|
||||||
to_upper() {
|
to_upper() {
|
||||||
tr '[:lower:]' '[:upper:]'
|
tr '[:lower:]' '[:upper:]'
|
||||||
}
|
}
|
||||||
@ -432,50 +462,53 @@ iface lo inet loopback
|
|||||||
auto eth0
|
auto eth0
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
if is_staticv4; then
|
# ipv4
|
||||||
# 静态 ipv4
|
if is_dhcpv4; then
|
||||||
|
echo "iface eth0 inet dhcp" >>/etc/network/interfaces
|
||||||
|
|
||||||
|
elif is_staticv4; then
|
||||||
get_netconf_to ipv4_addr
|
get_netconf_to ipv4_addr
|
||||||
get_netconf_to ipv4_gateway
|
get_netconf_to ipv4_gateway
|
||||||
# shellcheck disable=SC2154
|
|
||||||
cat <<EOF >>/etc/network/interfaces
|
cat <<EOF >>/etc/network/interfaces
|
||||||
iface eth0 inet static
|
iface eth0 inet static
|
||||||
address $ipv4_addr
|
address $ipv4_addr
|
||||||
gateway $ipv4_gateway
|
gateway $ipv4_gateway
|
||||||
EOF
|
EOF
|
||||||
else
|
# dns
|
||||||
# 动态 ipv4
|
if list=$(get_current_dns_v4); then
|
||||||
echo "iface eth0 inet dhcp" >>/etc/network/interfaces
|
for dns in $list; do
|
||||||
|
cat <<EOF >>/etc/network/interfaces
|
||||||
|
dns-nameserver $dns
|
||||||
|
EOF
|
||||||
|
done
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if is_staticv6; then
|
# ipv6
|
||||||
# 静态 ipv6
|
if is_slaac; then
|
||||||
|
echo 'iface eth0 inet6 auto' >>/etc/network/interfaces
|
||||||
|
|
||||||
|
elif is_dhcpv6; then
|
||||||
|
echo 'iface eth0 inet6 dhcp' >>/etc/network/interfaces
|
||||||
|
|
||||||
|
elif is_staticv6; then
|
||||||
get_netconf_to ipv6_addr
|
get_netconf_to ipv6_addr
|
||||||
get_netconf_to ipv6_gateway
|
get_netconf_to ipv6_gateway
|
||||||
# shellcheck disable=SC2154
|
|
||||||
cat <<EOF >>/etc/network/interfaces
|
cat <<EOF >>/etc/network/interfaces
|
||||||
iface eth0 inet6 static
|
iface eth0 inet6 static
|
||||||
address $ipv6_addr
|
address $ipv6_addr
|
||||||
gateway $ipv6_gateway
|
gateway $ipv6_gateway
|
||||||
EOF
|
EOF
|
||||||
# 如果有rdnss,则删除自己添加的dns,再添加rdnss
|
|
||||||
# 也有可能 dhcpcd 会自动设置,但没环境测试
|
|
||||||
get_netconf_to rdnss
|
|
||||||
if [ -n "$rdnss" ]; then
|
|
||||||
sed -i '/^[[:blank:]]*nameserver[[:blank:]].*:/d' /etc/resolv.conf
|
|
||||||
echo "nameserver $rdnss" >>/etc/resolv.conf
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
# 动态 ipv6
|
|
||||||
# 实测不用写配置
|
|
||||||
if false; then
|
|
||||||
if is_slaac; then
|
|
||||||
echo 'iface eth0 inet6 auto' >>/etc/network/interfaces
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if is_dhcpv6; then
|
# dns
|
||||||
echo 'iface eth0 inet6 dhcp' >>/etc/network/interfaces
|
# 有 ipv6 但需设置 dns
|
||||||
fi
|
if is_need_manual_set_dnsv6 && list=$(get_current_dns_v6); then
|
||||||
fi
|
for dns in $list; do
|
||||||
|
cat <<EOF >>/etc/network/interfaces
|
||||||
|
dns-nameserver $dns
|
||||||
|
EOF
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 显示网络配置
|
# 显示网络配置
|
||||||
@ -749,20 +782,8 @@ create_cloud_init_network_config() {
|
|||||||
ci_file=$1
|
ci_file=$1
|
||||||
|
|
||||||
get_netconf_to mac_addr
|
get_netconf_to mac_addr
|
||||||
|
|
||||||
# TODO: 没获取到mac/ipv4或者ipv6就先跳过cloud-init网络
|
|
||||||
# 不然cloud-init配置文件有问题,网络不通
|
|
||||||
[ -z "$mac_addr" ] && return
|
|
||||||
|
|
||||||
get_netconf_to ipv4_addr
|
|
||||||
get_netconf_to ipv4_gateway
|
|
||||||
get_netconf_to ipv6_addr
|
|
||||||
get_netconf_to ipv6_gateway
|
|
||||||
|
|
||||||
apk add yq
|
apk add yq
|
||||||
dns_list=$(grep '^nameserver' /etc/resolv.conf | awk '{print $2}')
|
|
||||||
|
|
||||||
# 头部
|
|
||||||
yq -i "
|
yq -i "
|
||||||
.network.version=1 |
|
.network.version=1 |
|
||||||
.network.config[0].type=\"physical\" |
|
.network.config[0].type=\"physical\" |
|
||||||
@ -775,7 +796,10 @@ create_cloud_init_network_config() {
|
|||||||
if is_dhcpv4; then
|
if is_dhcpv4; then
|
||||||
yq -i ".network.config[0].subnets += [{\"type\": \"dhcp\"}]" $ci_file
|
yq -i ".network.config[0].subnets += [{\"type\": \"dhcp\"}]" $ci_file
|
||||||
|
|
||||||
elif [ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ]; then
|
elif is_staticv4; then
|
||||||
|
get_netconf_to ipv4_addr
|
||||||
|
get_netconf_to ipv4_gateway
|
||||||
|
|
||||||
yq -i "
|
yq -i "
|
||||||
.network.config[0].subnets += [{
|
.network.config[0].subnets += [{
|
||||||
\"type\": \"static\",
|
\"type\": \"static\",
|
||||||
@ -783,7 +807,7 @@ create_cloud_init_network_config() {
|
|||||||
\"gateway\": \"$ipv4_gateway\" }]
|
\"gateway\": \"$ipv4_gateway\" }]
|
||||||
" $ci_file
|
" $ci_file
|
||||||
|
|
||||||
if dns4_list=$(echo "$dns_list" | grep '\.'); then
|
if dns4_list=$(get_current_dns_v4); then
|
||||||
for cur in $dns4_list; do
|
for cur in $dns4_list; do
|
||||||
yq -i ".network.config[1].address += [\"$cur\"]" $ci_file
|
yq -i ".network.config[1].address += [\"$cur\"]" $ci_file
|
||||||
done
|
done
|
||||||
@ -792,12 +816,19 @@ create_cloud_init_network_config() {
|
|||||||
|
|
||||||
# ipv6
|
# ipv6
|
||||||
if is_slaac; then
|
if is_slaac; then
|
||||||
yq -i ".network.config[0].subnets += [{\"type\": \"ipv6_slaac\"}]" $ci_file
|
if is_enable_other_flag; then
|
||||||
|
type=ipv6_dhcpv6-stateless
|
||||||
|
else
|
||||||
|
type=ipv6_slaac
|
||||||
|
fi
|
||||||
|
yq -i ".network.config[0].subnets += [{\"type\": \"$type\"}]" $ci_file
|
||||||
|
|
||||||
elif is_dhcpv6; then
|
elif is_dhcpv6; then
|
||||||
yq -i ".network.config[0].subnets += [{\"type\": \"ipv6_dhcpv6-stateful\"}]" $ci_file
|
yq -i ".network.config[0].subnets += [{\"type\": \"ipv6_dhcpv6-stateful\"}]" $ci_file
|
||||||
|
|
||||||
elif [ -n "$ipv6_addr" ] && [ -n "$ipv6_gateway" ]; then
|
elif is_staticv6; then
|
||||||
|
get_netconf_to ipv6_addr
|
||||||
|
get_netconf_to ipv6_gateway
|
||||||
# centos7 不认识 static6,但可改成 static,作用相同
|
# centos7 不认识 static6,但可改成 static,作用相同
|
||||||
# https://github.com/canonical/cloud-init/commit/dacdd30080bd8183d1f1c1dc9dbcbc8448301529
|
# https://github.com/canonical/cloud-init/commit/dacdd30080bd8183d1f1c1dc9dbcbc8448301529
|
||||||
yq -i "
|
yq -i "
|
||||||
@ -806,13 +837,14 @@ create_cloud_init_network_config() {
|
|||||||
\"address\": \"$ipv6_addr\",
|
\"address\": \"$ipv6_addr\",
|
||||||
\"gateway\": \"$ipv6_gateway\" }]
|
\"gateway\": \"$ipv6_gateway\" }]
|
||||||
" $ci_file
|
" $ci_file
|
||||||
|
fi
|
||||||
|
|
||||||
if dns6_list=$(echo "$dns_list" | grep ':'); then
|
# 有 ipv6,且 rdnss 为空,手动添加 dns
|
||||||
|
if is_need_manual_set_dnsv6 && dns6_list=$(get_current_dns_v6); then
|
||||||
for cur in $dns6_list; do
|
for cur in $dns6_list; do
|
||||||
yq -i ".network.config[1].address += [\"$cur\"]" $ci_file
|
yq -i ".network.config[1].address += [\"$cur\"]" $ci_file
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
download_cloud_init_config() {
|
download_cloud_init_config() {
|
||||||
@ -959,6 +991,17 @@ modify_linux() {
|
|||||||
disable_selinux_kdump $os_dir
|
disable_selinux_kdump $os_dir
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# debian 10/11 默认不支持 rdnss,要安装 rdnssd 或者 nm
|
||||||
|
if [ -f $os_dir/etc/debian_version ] && grep -E '^(10|11)' $os_dir/etc/debian_version; then
|
||||||
|
mv $os_dir/etc/resolv.conf $os_dir/etc/resolv.conf.orig
|
||||||
|
cp -f /etc/resolv.conf $os_dir/etc/resolv.conf
|
||||||
|
mount_pseudo_fs $os_dir
|
||||||
|
chroot $os_dir apt update
|
||||||
|
chroot $os_dir apt install -y rdnssd
|
||||||
|
# 不会自动建立链接,因此不能删除
|
||||||
|
mv $os_dir/etc/resolv.conf.orig $os_dir/etc/resolv.conf
|
||||||
|
fi
|
||||||
|
|
||||||
# opensuse tumbleweed 需安装 wicked
|
# opensuse tumbleweed 需安装 wicked
|
||||||
if grep opensuse-tumbleweed $os_dir/etc/os-release; then
|
if grep opensuse-tumbleweed $os_dir/etc/os-release; then
|
||||||
cp -f /etc/resolv.conf $os_dir/etc/resolv.conf
|
cp -f /etc/resolv.conf $os_dir/etc/resolv.conf
|
||||||
@ -1400,13 +1443,7 @@ mount_part_for_install_mode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get_dns_list_for_win() {
|
get_dns_list_for_win() {
|
||||||
case "$1" in
|
if dns_list=$(get_current_dns_v$1); then
|
||||||
4) sign='\.' ;;
|
|
||||||
6) sign=':' ;;
|
|
||||||
*) return 1 ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
if dns_list=$(grep '^nameserver' /etc/resolv.conf | awk '{print $2}' | grep "$sign"); then
|
|
||||||
i=0
|
i=0
|
||||||
for dns in $dns_list; do
|
for dns in $dns_list; do
|
||||||
i=$((i + 1))
|
i=$((i + 1))
|
||||||
@ -1418,7 +1455,7 @@ get_dns_list_for_win() {
|
|||||||
create_win_set_netconf_script() {
|
create_win_set_netconf_script() {
|
||||||
target=$1
|
target=$1
|
||||||
|
|
||||||
if is_staticv4 || is_staticv6; then
|
if is_staticv4 || is_staticv6 || is_need_manual_set_dnsv6; then
|
||||||
get_netconf_to mac_addr
|
get_netconf_to mac_addr
|
||||||
echo "set mac_addr=$mac_addr" >$target
|
echo "set mac_addr=$mac_addr" >$target
|
||||||
|
|
||||||
@ -1438,10 +1475,15 @@ EOF
|
|||||||
if is_staticv6; then
|
if is_staticv6; then
|
||||||
get_netconf_to ipv6_addr
|
get_netconf_to ipv6_addr
|
||||||
get_netconf_to ipv6_gateway
|
get_netconf_to ipv6_gateway
|
||||||
ipv6_dns_list="$(get_dns_list_for_win 6)"
|
|
||||||
cat <<EOF >>$target
|
cat <<EOF >>$target
|
||||||
set ipv6_addr=$ipv6_addr
|
set ipv6_addr=$ipv6_addr
|
||||||
set ipv6_gateway=$ipv6_gateway
|
set ipv6_gateway=$ipv6_gateway
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 有 ipv6 但需设置 dns
|
||||||
|
if is_need_manual_set_dnsv6 && ipv6_dns_list="$(get_dns_list_for_win 6)"; then
|
||||||
|
cat <<EOF >>$target
|
||||||
$ipv6_dns_list
|
$ipv6_dns_list
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
@ -1924,4 +1966,7 @@ fi
|
|||||||
if [ "$sleep" = 2 ]; then
|
if [ "$sleep" = 2 ]; then
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# 等几秒让 web ssh 输出全部内容
|
||||||
|
sleep 5
|
||||||
reboot
|
reboot
|
||||||
|
@ -13,33 +13,40 @@ rem set ipv6_dns2=::2
|
|||||||
@echo off
|
@echo off
|
||||||
setlocal EnableDelayedExpansion
|
setlocal EnableDelayedExpansion
|
||||||
|
|
||||||
:: 关闭随机地址,防止ipv6地址和后台面板不一致
|
:: 禁用 IPv6 地址标识符的随机化,防止 IPv6 和后台面板不一致
|
||||||
netsh interface ipv6 set global randomizeidentifiers=disabled
|
netsh interface ipv6 set global randomizeidentifiers=disabled
|
||||||
|
|
||||||
if not defined mac_addr exit /b
|
:: 检查是否定义了 MAC 地址
|
||||||
for /f %%a in ('wmic nic where "MACAddress='%mac_addr%'" get InterfaceIndex ^| findstr [0-9]') do set id=%%a
|
if defined mac_addr (
|
||||||
if not defined id exit /b
|
for /f %%a in ('wmic nic where "MACAddress='%mac_addr%'" get InterfaceIndex ^| findstr [0-9]') do set id=%%a
|
||||||
|
if defined id (
|
||||||
if defined ipv4_addr if defined ipv4_gateway (
|
:: 配置静态 IPv4 地址和网关
|
||||||
|
if defined ipv4_addr if defined ipv4_gateway (
|
||||||
:: gwmetric 默认值为 1,自动跃点需设为 0
|
:: gwmetric 默认值为 1,自动跃点需设为 0
|
||||||
netsh interface ipv4 set address %id% static %ipv4_addr% gateway=%ipv4_gateway% gwmetric=0
|
netsh interface ipv4 set address !id! static !ipv4_addr! gateway=!ipv4_gateway! gwmetric=0
|
||||||
|
)
|
||||||
|
|
||||||
|
:: 配置静态 IPv4 DNS 服务器
|
||||||
for %%i in (1, 2) do (
|
for %%i in (1, 2) do (
|
||||||
if defined ipv4_dns%%i (
|
if defined ipv4_dns%%i (
|
||||||
netsh interface ipv4 add dnsservers %id% !ipv4_dns%%i! %%i no
|
netsh interface ipv4 add dnsservers !id! !ipv4_dns%%i! %%i no
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
if defined ipv6_addr if defined ipv6_gateway (
|
:: 配置 IPv6 地址和网关
|
||||||
netsh interface ipv6 set address %id% %ipv6_addr%
|
if defined ipv6_addr if defined ipv6_gateway (
|
||||||
netsh interface ipv6 add route prefix=::/0 %id% !ipv6_gateway!
|
netsh interface ipv6 set address !id! !ipv6_addr!
|
||||||
|
netsh interface ipv6 add route prefix=::/0 !id! !ipv6_gateway!
|
||||||
|
)
|
||||||
|
|
||||||
|
:: 配置 IPv6 DNS 服务器
|
||||||
for %%i in (1, 2) do (
|
for %%i in (1, 2) do (
|
||||||
if defined ipv6_dns%%i (
|
if defined ipv6_dns%%i (
|
||||||
netsh interface ipv6 add dnsservers %id% !ipv6_dns%%i! %%i no
|
netsh interface ipv6 add dnsservers !id! !ipv6_dns%%i! %%i no
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
:: 删除脚本文件
|
||||||
del "%~f0"
|
del "%~f0"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user