diff --git a/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot b/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot index 3f8b237cec..d4cd9c6dbb 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot +++ b/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot @@ -1,4 +1,46 @@ #!/bin/sh + +# xcat_static_ip6_fields -- print "
" for the +# static IPv6 form dracut takes, client:peer:gateway:prefix:hostname:interface:autoconf. +# +# An IPv6 literal carries colons, so the fields cannot be cut on a colon: dracut brackets +# the address and the gateway, and the brackets are what delimits them. +xcat_static_ip6_fields() +{ + local spec addr rest gw prefix + + spec="${1#ip=}" + case "$spec" in + \[*) ;; + *) return 1 ;; + esac + addr="${spec#\[}" + addr="${addr%%\]*}" + rest="${spec#*\]:}" + + # peer, which xCAT never sets + rest="${rest#*:}" + + case "$rest" in + \[*) + gw="${rest#\[}" + gw="${gw%%\]*}" + rest="${rest#*\]:}" + ;; + *) + gw="" + rest="${rest#*:}" + ;; + esac + prefix="${rest%%:*}" + + [ -n "$addr" ] || return 1 + case "$prefix" in + ''|*[!0-9]*) return 1 ;; + esac + printf '%s %s %s\n' "$addr" "$prefix" "$gw" +} + NEWROOT=$3 RWDIR=.statelite XCATMASTER=$XCAT @@ -264,8 +306,45 @@ if [ -z $STATEMNT ]; then fi if [ ! -z "$MACX" ] && [ ! -z "$ETHX" ]; then + # The booted system takes the address the node was given on the kernel command line + # when that address is IPv6. dhcp4 leaves an IPv6-only node with no address at all, and + # systemd-networkd-wait-online then fails. + XCAT_IP6_FIELDS="" + for xcat_cmdline_word in $(cat /proc/cmdline); do + case "$xcat_cmdline_word" in + ip=\[*) + XCAT_IP6_FIELDS=$(xcat_static_ip6_fields "$xcat_cmdline_word") || XCAT_IP6_FIELDS="" + break + ;; + esac + done + if [ -x $NEWROOT/usr/sbin/netplan ]; then mkdir -p $NEWROOT/etc/netplan + if [ -n "$XCAT_IP6_FIELDS" ]; then + set -- $XCAT_IP6_FIELDS + XCAT_IP6_ADDR=$1 + XCAT_IP6_PREFIX=$2 + XCAT_IP6_GW=$3 + cat > $NEWROOT/etc/netplan/90-xcat.yaml <> $NEWROOT/etc/netplan/90-xcat.yaml < $NEWROOT/etc/netplan/90-xcat.yaml < $NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX echo "BOOTPROTO=dhcp" >> $NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX diff --git a/xCAT-test/bats/ipv6_xcatroot_netplan.bats b/xCAT-test/bats/ipv6_xcatroot_netplan.bats new file mode 100644 index 0000000000..56ef89d336 --- /dev/null +++ b/xCAT-test/bats/ipv6_xcatroot_netplan.bats @@ -0,0 +1,68 @@ +#!/usr/bin/env bats +# +# The booted stateless system of an IPv6-only Ubuntu node. +# +# xcatroot wrote "dhcp4: true" into the netplan of every node, so an IPv6-only node came up +# with no address: systemd-networkd-wait-online failed, xcatpostinit failed, and the node +# answered no ssh. Measured on Ubuntu 24.04.5, which reached its login prompt with +# "[FAILED] Failed to start systemd-networkd-w... Wait for Network to be Configured." +# +# The address is on the kernel command line already, in the static form dracut takes. + +load 'helpers/shell_source' + +setup() +{ + XCATROOT_SCRIPT="$(require_repo_file \ + 'xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot')" + SNIPPET="${BATS_TEST_TMPDIR}/fields.sh" + extract_shell_function "$XCATROOT_SCRIPT" xcat_static_ip6_fields >"$SNIPPET" +} + +fields() +{ + ( + # shellcheck disable=SC1090 + . "$SNIPPET" >/dev/null 2>&1 + xcat_static_ip6_fields "$1" + ) +} + +@test "the address, prefix and gateway are read from the static form" { + run fields 'ip=[2001:db8:147::10]::[2001:db8:147::1]:64:::none' + [ "$status" -eq 0 ] + [ "$output" = '2001:db8:147::10 64 2001:db8:147::1' ] +} + +@test "a node with no gateway still yields its address and prefix" { + run fields 'ip=[2001:db8:147::10]:::64:::none' + [ "$status" -eq 0 ] + [ "$output" = '2001:db8:147::10 64 ' ] +} + +@test "an interface name in the parameter does not disturb the fields" { + run fields 'ip=[2001:db8:147::10]::[2001:db8:147::1]:64::eth0:none' + [ "$output" = '2001:db8:147::10 64 2001:db8:147::1' ] +} + +# The IPv4 forms must be refused, or an IPv4 node would be given an IPv6 netplan. +@test "the dracut dhcp form is refused" { + run fields 'ip=dhcp' + [ "$status" -ne 0 ] +} + +@test "the interface dhcp form is refused" { + run fields 'ip=eth0:dhcp' + [ "$status" -ne 0 ] +} + +@test "either6 is refused, because it carries no address" { + run fields 'ip=either6' + [ "$status" -ne 0 ] +} + +# A prefix that is not a number would reach the netplan as an address suffix. +@test "a parameter with no prefix is refused" { + run fields 'ip=[2001:db8:147::10]::[2001:db8:147::1]::::none' + [ "$status" -ne 0 ] +}