1
0
mirror of https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud.git synced 2026-06-21 00:07:16 +03:00

nocloud: add IPv6 autoconfiguration support

This commit is contained in:
Natanael Copa 2026-05-11 12:46:05 +02:00
parent 727963829e
commit 584551e331
2 changed files with 103 additions and 27 deletions

View File

@ -71,6 +71,9 @@ set_network_config_v2() {
local iface configured= target_name macaddress
for iface in $ethernets; do
local stanza= dhcp4= dhcp6= accept_ra=
local address= gateway= nameservers= server=
target_name="$(get_network_config ethernets/$iface/set-name)"
macaddress="$(get_network_config ethernets/$iface/match/macaddress)"
[ -n "$target_name" ] || target_name="$iface"
@ -79,36 +82,59 @@ set_network_config_v2() {
printf "%s %s\n" "$target_name" "$macaddress" >> "$ETC/iftab"
fi
case "$(get_network_config ethernets/$iface/dhcp4)" in
dhcp4="$(get_network_config ethernets/$iface/dhcp4)"
dhcp6="$(get_network_config ethernets/$iface/dhcp6)"
accept_ra="$(get_network_config ethernets/$iface/accept-ra)"
case "$dhcp4" in
true|yes)
printf "%s\n%s\n\t%s\n\n" \
"auto $target_name" \
"iface $target_name" \
"use dhcp" >> "$ETC/network/interfaces"
configured=1
;;
*)
local address gateway nameservers server
address="$(get_network_config ethernets/$iface/addresses/1)"
[ -n "$address" ] || continue
printf "%s\n%s\n\taddress %s\n" \
"auto $target_name" \
"iface $target_name" \
"$address" >> "$ETC/network/interfaces"
gateway="$(get_network_config ethernets/$iface/gateway4)"
[ -n "$gateway" ] || gateway="$(get_network_config ethernets/$iface/gateway6)"
if [ -n "$gateway" ]; then
printf "\tgateway %s\n" "$gateway" >> "$ETC/network/interfaces"
fi
printf "\n" >> "$ETC/network/interfaces"
nameservers="$(get_network_config ethernets/$iface/nameservers/addresses)"
for server in $nameservers; do
add_once "$ETC/resolv.conf" \
"nameserver $(get_network_config ethernets/$iface/nameservers/addresses/$server)"
done
configured=1
stanza="$stanza\tuse dhcp\n"
;;
esac
case "$dhcp6" in
true|yes)
case "$dhcp4" in
true|yes)
:
;;
*)
stanza="$stanza\tuse dhcp\n"
stanza="$stanza\tdhcp-program dhcpcd\n"
stanza="$stanza\tdhcp-opts -6\n"
;;
esac
;;
esac
case "$accept_ra" in
true|yes)
stanza="$stanza\tuse ipv6-ra\n"
;;
esac
address="$(get_network_config ethernets/$iface/addresses/1)"
if [ -n "$address" ]; then
stanza="$stanza\taddress $address\n"
gateway="$(get_network_config ethernets/$iface/gateway4)"
[ -n "$gateway" ] || gateway="$(get_network_config ethernets/$iface/gateway6)"
if [ -n "$gateway" ]; then
stanza="$stanza\tgateway $gateway\n"
fi
nameservers="$(get_network_config ethernets/$iface/nameservers/addresses)"
for server in $nameservers; do
add_once "$ETC/resolv.conf" \
"nameserver $(get_network_config ethernets/$iface/nameservers/addresses/$server)"
done
fi
[ -n "$stanza" ] || continue
printf "%s\n%s\n%b\n" \
"auto $target_name" \
"iface $target_name" \
"$stanza" >> "$ETC/network/interfaces"
configured=1
done
if [ -z "$configured" ]; then

View File

@ -16,6 +16,8 @@ init_tests \
set_network_config_v2_configures_macifrename_service \
set_network_config_v2_static_ipv4 \
set_network_config_v2_static_ipv6 \
set_network_config_v2_dhcp6 \
set_network_config_v2_accept_ra \
set_network_config_auto \
userdata_user_name \
userdata_user_homedir \
@ -251,6 +253,54 @@ set_network_config_v2_static_ipv6_body() {
cat etc/resolv.conf
}
set_network_config_v2_dhcp6_body() {
fake_network_config_nocloud <<-EOF
version: 2
ethernets:
net0:
match:
macaddress: 52:55:55:1d:2a:0b
set-name: lima0
dhcp6: true
EOF
atf_check \
-o ignore \
-e match:"set_network_interfaces: done" \
tiny-cloud boot
atf_check \
-o match:"auto lima0" \
-o match:"iface lima0" \
-o match:"use dhcp" \
-o match:"dhcp-program dhcpcd" \
-o match:"dhcp-opts -6" \
cat etc/network/interfaces
}
set_network_config_v2_accept_ra_body() {
fake_network_config_nocloud <<-EOF
version: 2
ethernets:
net0:
match:
macaddress: 52:55:55:1d:2a:0b
set-name: lima0
accept-ra: true
EOF
atf_check \
-o ignore \
-e match:"set_network_interfaces: done" \
tiny-cloud boot
atf_check \
-o match:"auto lima0" \
-o match:"iface lima0" \
-o match:"use ipv6-ra" \
cat etc/network/interfaces
}
set_network_config_auto_body() {
fake_metadata_nocloud <<-EOF
resolv_conf: