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

Compare commits

...

2 Commits

Author SHA1 Message Date
Natanael Copa
922970be1c Merge branch 'nocloud-network-config-v2' into 'main'
Implement network-config v2

See merge request alpine/cloud/tiny-cloud!151
2026-05-12 14:06:10 +00:00
Natanael Copa
86b4ec351a refactor network-config handler
Move network-config parsing and interface generation into a shared
library so other cloud providers can reuse it instead of keeping the
logic embedded in NoCloud init.

This also creates a single dispatch point for future network-config
versions, which makes adding v1 support a focused follow-up instead of
another provider-specific expansion.
2026-05-12 16:03:01 +02:00
4 changed files with 170 additions and 149 deletions

View File

@ -23,6 +23,7 @@ core: $(MAN1PAGES) $(MAN5PAGES) $(MAN8PAGES)
install -Dm644 -t "$(PREFIX)"/usr/lib/tiny-cloud \
lib/tiny-cloud/common \
lib/tiny-cloud/init \
lib/tiny-cloud/network-config \
lib/tiny-cloud/tiny-cloud.conf
install -Dm644 -t "$(PREFIX)"/usr/lib/tiny-cloud/user-data \
lib/tiny-cloud/user-data/*

View File

@ -4,12 +4,6 @@
INIT_ACTIONS_BOOT="$(replace_word set_default_interfaces set_network_interfaces $INIT_ACTIONS_BOOT)"
get_network_config() {
IFS="/"
yx -f "$TINY_CLOUD_VAR/network-config" $1 2>/dev/null
unset IFS
}
set_resolv_conf() {
# resolv.conf
local nameservers="$(imds meta-data/resolv_conf/nameservers)"
@ -49,158 +43,18 @@ want_ephemeral_network() {
return 1
}
set_network_config_v2() {
[ -s "$TINY_CLOUD_VAR/network-config" ] || return 1
local version="$(get_network_config version)"
if [ "$version" != "2" ]; then
log -i -t "$phase" warning "$ACTION: unsupported NoCloud network-config version: ${version:-unknown}"
return 1
fi
local ethernets="$(get_network_config ethernets)"
if [ -z "$ethernets" ]; then
log -i -t "$phase" warning "$ACTION: NoCloud network-config v2 has no supported ethernets entries"
return 1
fi
printf "%s\n%s\n\n" \
"auto lo" \
"iface lo inet loopback" \
> "$ETC/network/interfaces"
rm -f "$ETC/iftab"
local iface configured= target_name macaddress
for iface in $ethernets; do
local stanza= dhcp4= dhcp6= accept_ra= dhcp_program= dhcp_opts=
local address= gateway= nameservers= server=
local route_metric=
target_name="$(get_network_config ethernets/$iface/set-name)"
macaddress="$(get_network_config ethernets/$iface/match/macaddress)"
[ -n "$target_name" ] || target_name="$iface"
if [ -n "$target_name" ] && [ -n "$macaddress" ]; then
printf "%s %s\n" "$target_name" "$macaddress" >> "$ETC/iftab"
fi
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)
stanza="$stanza\tuse dhcp\n"
route_metric="$(get_network_config ethernets/$iface/dhcp4-overrides/route-metric)"
if [ -n "$route_metric" ]; then
dhcp_program="dhcpcd"
dhcp_opts="$dhcp_opts -m $route_metric"
fi
;;
esac
case "$dhcp6" in
true|yes)
case "$dhcp4" in
true|yes)
route_metric="$(get_network_config ethernets/$iface/dhcp6-overrides/route-metric)"
if [ -n "$route_metric" ]; then
dhcp_program="dhcpcd"
dhcp_opts="$dhcp_opts -m $route_metric"
fi
;;
*)
stanza="$stanza\tuse dhcp\n"
dhcp_program="dhcpcd"
dhcp_opts="$dhcp_opts -6"
route_metric="$(get_network_config ethernets/$iface/dhcp6-overrides/route-metric)"
if [ -n "$route_metric" ]; then
dhcp_opts="$dhcp_opts -m $route_metric"
fi
;;
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
if [ -n "$dhcp_program" ]; then
stanza="$stanza\tdhcp-program $dhcp_program\n"
fi
if [ -n "$dhcp_opts" ]; then
stanza="$stanza\tdhcp-opts${dhcp_opts}\n"
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
log -i -t "$phase" warning "$ACTION: NoCloud network-config v2 has no supported ethernets entries"
return 1
fi
return 0
}
setup_macifrename() {
[ -s "$ETC/iftab" ] || return 0
if ! [ -e "$ETC/init.d/macifrename" ]; then
log -i -t "$phase" warning "$ACTION: macifrename service not installed; interface renames will not be applied"
return 0
fi
mkdir -p "$ETC/conf.d"
if [ -f "$ETC/conf.d/macifrename" ]; then
if grep -q '^MACIFRENAME_OPTS=' "$ETC/conf.d/macifrename"; then
sed -i -e 's|^MACIFRENAME_OPTS=.*|MACIFRENAME_OPTS="/etc/iftab"|' \
"$ETC/conf.d/macifrename"
else
printf '%s\n' 'MACIFRENAME_OPTS="/etc/iftab"' >> "$ETC/conf.d/macifrename"
fi
else
printf '%s\n' 'MACIFRENAME_OPTS="/etc/iftab"' > "$ETC/conf.d/macifrename"
fi
$MOCK rc-update add macifrename boot
$MOCK rc-service macifrename start
}
init__set_network_interfaces() {
local interfaces="$(imds meta-data/network-interfaces)"
local network_config="$TINY_CLOUD_VAR/network-config"
mkdir -p "$ETC"/network
if [ -n "$interfaces" ]; then
printf "%s\n" "$interfaces" > "$ETC"/network/interfaces
elif set_network_config_v2; then
elif network_config_set "$network_config" "NoCloud"; then
:
elif ! [ -f "$ETC"/network/interfaces ]; then
init__set_default_interfaces
fi
setup_macifrename
network_config_setup_macifrename
if ! grep -q dhcp "$ETC"/network/interfaces; then
set_resolv_conf
fi

View File

@ -6,6 +6,7 @@
: "${PREFIX:=/usr}"
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR/tiny-cloud/common"
. "$LIBDIR/tiny-cloud/network-config"
: "${SKIP_INIT_ACTIONS:=}"

View File

@ -0,0 +1,165 @@
# Tiny Cloud - Network Config Functions
# vim:set filetype=sh:
# shellcheck shell=sh
network_config_get() {
local file="$1" path="$2"
IFS="/"
yx -f "$file" $path 2>/dev/null
unset IFS
}
network_config_set_v2() {
local file="$1" source_name="${2:-network-config}"
[ -s "$file" ] || return 1
local ethernets="$(network_config_get "$file" ethernets)"
if [ -z "$ethernets" ]; then
log -i -t "$phase" warning "$ACTION: $source_name network-config v2 has no supported ethernets entries"
return 1
fi
printf "%s\n%s\n\n" \
"auto lo" \
"iface lo inet loopback" \
> "$ETC/network/interfaces"
rm -f "$ETC/iftab"
local iface configured= target_name macaddress
for iface in $ethernets; do
local stanza= dhcp4= dhcp6= accept_ra= dhcp_program= dhcp_opts=
local address= gateway= nameservers= server=
local route_metric=
target_name="$(network_config_get "$file" ethernets/$iface/set-name)"
macaddress="$(network_config_get "$file" ethernets/$iface/match/macaddress)"
[ -n "$target_name" ] || target_name="$iface"
if [ -n "$target_name" ] && [ -n "$macaddress" ]; then
printf "%s %s\n" "$target_name" "$macaddress" >> "$ETC/iftab"
fi
dhcp4="$(network_config_get "$file" ethernets/$iface/dhcp4)"
dhcp6="$(network_config_get "$file" ethernets/$iface/dhcp6)"
accept_ra="$(network_config_get "$file" ethernets/$iface/accept-ra)"
case "$dhcp4" in
true|yes)
stanza="$stanza\tuse dhcp\n"
route_metric="$(network_config_get "$file" ethernets/$iface/dhcp4-overrides/route-metric)"
if [ -n "$route_metric" ]; then
dhcp_program="dhcpcd"
dhcp_opts="$dhcp_opts -m $route_metric"
fi
;;
esac
case "$dhcp6" in
true|yes)
case "$dhcp4" in
true|yes)
route_metric="$(network_config_get "$file" ethernets/$iface/dhcp6-overrides/route-metric)"
if [ -n "$route_metric" ]; then
dhcp_program="dhcpcd"
dhcp_opts="$dhcp_opts -m $route_metric"
fi
;;
*)
stanza="$stanza\tuse dhcp\n"
dhcp_program="dhcpcd"
dhcp_opts="$dhcp_opts -6"
route_metric="$(network_config_get "$file" ethernets/$iface/dhcp6-overrides/route-metric)"
if [ -n "$route_metric" ]; then
dhcp_opts="$dhcp_opts -m $route_metric"
fi
;;
esac
;;
esac
case "$accept_ra" in
true|yes)
stanza="$stanza\tuse ipv6-ra\n"
;;
esac
address="$(network_config_get "$file" ethernets/$iface/addresses/1)"
if [ -n "$address" ]; then
stanza="$stanza\taddress $address\n"
gateway="$(network_config_get "$file" ethernets/$iface/gateway4)"
[ -n "$gateway" ] || gateway="$(network_config_get "$file" ethernets/$iface/gateway6)"
if [ -n "$gateway" ]; then
stanza="$stanza\tgateway $gateway\n"
fi
nameservers="$(network_config_get "$file" ethernets/$iface/nameservers/addresses)"
for server in $nameservers; do
add_once "$ETC/resolv.conf" \
"nameserver $(network_config_get "$file" ethernets/$iface/nameservers/addresses/$server)"
done
fi
if [ -n "$dhcp_program" ]; then
stanza="$stanza\tdhcp-program $dhcp_program\n"
fi
if [ -n "$dhcp_opts" ]; then
stanza="$stanza\tdhcp-opts${dhcp_opts}\n"
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
log -i -t "$phase" warning "$ACTION: $source_name network-config v2 has no supported ethernets entries"
return 1
fi
return 0
}
network_config_set() {
local file="$1" source_name="${2:-network-config}"
[ -s "$file" ] || return 1
local version="$(network_config_get "$file" version)"
case "$version" in
2)
network_config_set_v2 "$file" "$source_name"
;;
*)
log -i -t "$phase" warning "$ACTION: unsupported $source_name network-config version: ${version:-unknown}"
return 1
;;
esac
}
network_config_setup_macifrename() {
[ -s "$ETC/iftab" ] || return 0
if ! [ -e "$ETC/init.d/macifrename" ]; then
log -i -t "$phase" warning "$ACTION: macifrename service not installed; interface renames will not be applied"
return 0
fi
mkdir -p "$ETC/conf.d"
if [ -f "$ETC/conf.d/macifrename" ]; then
if grep -q '^MACIFRENAME_OPTS=' "$ETC/conf.d/macifrename"; then
sed -i -e 's|^MACIFRENAME_OPTS=.*|MACIFRENAME_OPTS="/etc/iftab"|' \
"$ETC/conf.d/macifrename"
else
printf '%s\n' 'MACIFRENAME_OPTS="/etc/iftab"' >> "$ETC/conf.d/macifrename"
fi
else
printf '%s\n' 'MACIFRENAME_OPTS="/etc/iftab"' > "$ETC/conf.d/macifrename"
fi
$MOCK rc-update add macifrename boot
$MOCK rc-service macifrename start
}