fix: select correct primary IPv6 address whose subnet contains the gateway (#561)

* fix: select correct primary IPv6 address whose subnet contains the gateway

When a system has multiple IPv6 addresses with different prefix lengths
(e.g., /44 and /96), the script previously selected the first address
as primary regardless of whether its subnet contained the default gateway.
This caused the gateway to be unreachable after reinstallation.

This change:
- Adds pure shell expand_ipv6 and ip_addr_contains_gw functions to
  determine which IPv6 subnet contains the gateway (no python dependency)
- Modifies collect_netconf to select the correct primary IPv6 address
- Stores remaining addresses as ipv6_extra_addrs
- Adds extra IPv6 addresses via post-up in create_ifupdown_config

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: address review feedback for IPv6 extra addrs and edge cases

- Pass ipv6_extra_addrs through initrd-network.sh (receive as $7,
  add extra addresses in add_missing_ipv6_config, write to netconf)
  so that trans.sh can read them via get_netconf_to
- Fix cut -c1-0 error when prefix_len < 4 in ip_addr_contains_gw
- Use grep -Fxve for exact string matching instead of regex

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: use subshell for IFS modification to avoid leaking to outer scope

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: avoid global IFS modification when iterating extra IPv6 addresses

Replace subshell IFS trick with tr+while read to split comma-separated
ipv6_extra_addrs, avoiding any modification of the IFS variable.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: use ip route get instead of subnet matching for IPv6 primary address selection

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
kelryry
2026-03-08 22:17:59 +08:00
committed by GitHub
parent ecacaab103
commit 5b0d91c633
3 changed files with 45 additions and 4 deletions

View File

@ -1121,6 +1121,16 @@ EOF
post-up ip route add default via $ipv6_gateway dev $ethx
EOF
fi
# 额外的 IPv6 地址(子网不含网关的地址)
get_netconf_to ipv6_extra_addrs
if [ -n "$ipv6_extra_addrs" ]; then
_old_ifs=$IFS; IFS=','
for _addr in $ipv6_extra_addrs; do
echo " post-up ip -6 addr add $_addr dev $ethx" >>$conf_file
done
IFS=$_old_ifs
fi
fi
# dns