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

@ -11,6 +11,7 @@ ipv4_gateway=$3
ipv6_addr=$4
ipv6_gateway=$5
is_in_china=$6
ipv6_extra_addrs=$7
DHCP_TIMEOUT=15
DNS_FILE_TIMEOUT=5
@ -171,6 +172,15 @@ add_missing_ipv6_config() {
ip -6 route add default via "$ipv6_gateway" dev "$ethx" onlink
fi
fi
# 添加额外的 IPv6 地址(逗号分隔)
if [ -n "$ipv6_extra_addrs" ]; then
printf '%s\n' "$ipv6_extra_addrs" | tr ',' '\n' | while IFS= read -r addr; do
if [ -n "$addr" ]; then
ip -6 addr add "$addr" dev "$ethx" 2>/dev/null || true
fi
done
fi
fi
}
@ -506,5 +516,6 @@ echo "$ipv4_addr" >"$netconf/ipv4_addr"
echo "$ipv4_gateway" >"$netconf/ipv4_gateway"
echo "$ipv6_addr" >"$netconf/ipv6_addr"
echo "$ipv6_gateway" >"$netconf/ipv6_gateway"
echo "$ipv6_extra_addrs" >"$netconf/ipv6_extra_addrs"
$ipv4_has_internet && echo 1 >"$netconf/ipv4_has_internet" || echo 0 >"$netconf/ipv4_has_internet"
$ipv6_has_internet && echo 1 >"$netconf/ipv6_has_internet" || echo 0 >"$netconf/ipv6_has_internet"