diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot index ae001e1f46..2fb2db8aa5 100755 --- a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot +++ b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot @@ -110,6 +110,60 @@ EOF fi } +# Prints the name of the provisioning NIC, from the kernel command line. dracut names the +# interface in ifname= and in netdev=; BOOTIF carries a MAC only, so sysfs gives the name. +xcat_boot_netif() { + local ifname="$1" + local netdev="$2" + local bootif="$3" + local sysnet="${4:-/sys/class/net}" + local mac + local devpath + + if [ -n "$ifname" ]; then + printf '%s\n' "${ifname%%:*}" + return 0 + fi + + if [ -n "$netdev" ]; then + printf '%s\n' "$netdev" + return 0 + fi + + [ -n "$bootif" ] || return 1 + mac=$(printf '%s' "${bootif#01-}" | tr 'A-Z-' 'a-z:') + for devpath in "$sysnet"/*; do + [ -r "$devpath/address" ] || continue + if [ "$(cat "$devpath/address")" = "$mac" ]; then + printf '%s\n' "${devpath##*/}" + return 0 + fi + done + + return 1 +} + +# Writes the IPv6 configuration of $netif into $newroot. A node that is not IPv6-only needs +# nothing, so a non-zero status means the configuration of an IPv6-only node is lost. The +# booted system does not repeat the DHCPv6 exchange of the initial ramdisk, and the +# reservation answers the DUID that exchange used, so such a node comes up with no address. +xcat_persist_ipv6_boot() { + local newroot="$1" + local netif="$2" + local bootip="$3" + local sysnet="${4:-/sys/class/net}" + local sources=("${@:5}") + local mac + + [ -n "$netif" ] || return 0 + xcat_is_ipv6_boot "$bootip" "$netif" || return 0 + + mac=$(cat "$sysnet/$netif/address" 2>/dev/null) + [ -n "$mac" ] || return 1 + + persist_ipv6_nm_connection "$newroot" "$netif" "$mac" "${sources[@]}" +} + log_label="xcat.deployment" NEWROOT=$3 @@ -479,81 +533,6 @@ else fi cd / -function getdevfrommac() { - boothwaddr=$1 - ip link show | while read line - do - dev=`echo $line | egrep "^[0-9]+: [0-9A-Za-z]+" | cut -d ' ' -f 2 | cut -d ':' -f 1` - if [ "X$dev" != "X" ]; then - devname=$dev - fi - - if [ "X$devname" != "X" ]; then - hwaddr=`echo $line | egrep "^[ ]*link" | awk '{print $2}'` - if [ "X$hwaddr" = "X$boothwaddr" ]; then - echo $devname - fi - fi - done -} - - -if [ -z "$STATEMNT" ]; then - for lf in /tmp/dhclient.*.lease; do - netif=${lf#*.} - netif=${netif%.*} - cp $lf "$NEWROOT/var/lib/dhclient/dhclient-$netif.leases" - [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "Saving $NEWROOT/var/lib/dhclient/dhclient-$netif.leases" - done - - ifname="$(getarg ifname=)" - netdev="$(getarg netdev=)" - BOOTIF="$(getarg BOOTIF=)" - if [ ! -z "$ifname" ]; then - MACX=${ifname#*:} - ETHX=${ifname%:$MACX*} - elif [ ! -z "$netdev" ]; then - ETHX=$netdev - MACX=`ip link show $netdev | grep ether | awk '{print $2}'` - elif [ ! -z "$BOOTIF" ]; then - MACX=$BOOTIF - MACX=${MACX#01-} - MACX=${MACX//-/:} - ETHX=$(getdevfrommac $MACX) - fi - - - if [ -n "$MACX" ] && [ -n "$ETHX" ]; then - BOOT_IP="$(getarg ip=)" - IPV6_BOOT=0 - if xcat_is_ipv6_boot "$BOOT_IP" "$ETHX"; then - IPV6_BOOT=1 - [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "Persisting the NetworkManager connection for IPv6 interface $ETHX" - if ! persist_ipv6_nm_connection "$NEWROOT" "$ETHX" "$MACX"; then - logger -t $log_label -p local4.error "Unable to persist the static IPv6 configuration for $ETHX" - exit 1 - fi - fi - - if [ "$IPV6_BOOT" -eq 1 ]; then - # EL10 uses NetworkManager keyfiles. Do not leave a competing - # legacy ifcfg profile that would start a new DHCPv6 identity. - rm -f "$NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" - else - if [ ! -e "$NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" ]; then - [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "Creating $NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" - mkdir -p "$NEWROOT/etc/sysconfig/network-scripts" - touch "$NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" - fi - [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "Writing $NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX: DEVICE=$ETHX;BOOTPROTO=dhcp;HWADDR=$MACX;ONBOOT=yes" - echo "DEVICE=$ETHX" > "$NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" - echo "BOOTPROTO=dhcp" >> "$NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" - echo "HWADDR=$MACX" >> "$NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" - echo "ONBOOT=yes" >> "$NEWROOT/etc/sysconfig/network-scripts/ifcfg-$ETHX" - fi - fi -fi - [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "Saving $NEWROOT/etc/resolv.conf" cp /etc/resolv.conf "$NEWROOT/etc/" @@ -562,6 +541,14 @@ if [ -d "$NEWROOT/etc/sysconfig" -a ! -e "$NEWROOT/etc/sysconfig/selinux" ]; the echo "SELINUX=disabled" >> "$NEWROOT/etc/sysconfig/selinux" fi +if [ -z "$STATEMNT" ]; then + BOOT_NETIF="$(xcat_boot_netif "$(getarg ifname=)" "$(getarg netdev=)" "$(getarg BOOTIF=)")" + if ! xcat_persist_ipv6_boot "$NEWROOT" "$BOOT_NETIF" "$(getarg ip=)"; then + logger "${SYSLOGHOST[@]}" -t $log_label -p local4.error "Unable to persist the static IPv6 configuration for $BOOT_NETIF" + exit 1 + fi +fi + logger "${SYSLOGHOST[@]}" -t $log_label -p local4.info "Exiting xcatroot..." # inject new exit_if_exists echo 'settle_exit_if_exists="--exit-if-exists=/dev/root"; rm "$job"' > $hookdir/initqueue/xcat.sh diff --git a/xCAT-test/bats/ipv6_xcatroot_el_stateless.bats b/xCAT-test/bats/ipv6_xcatroot_el_stateless.bats index cb90afac7e..fba55a8886 100644 --- a/xCAT-test/bats/ipv6_xcatroot_el_stateless.bats +++ b/xCAT-test/bats/ipv6_xcatroot_el_stateless.bats @@ -17,7 +17,12 @@ setup() 'xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot')" LIB="${BATS_TEST_TMPDIR}/xcatroot.sh" { extract_shell_function "$XCATROOT_SCRIPT" 'xcat_is_ipv6_boot' - extract_shell_function "$XCATROOT_SCRIPT" 'persist_ipv6_nm_connection'; } >"$LIB" + extract_shell_function "$XCATROOT_SCRIPT" 'persist_ipv6_nm_connection' + extract_shell_function "$XCATROOT_SCRIPT" 'xcat_boot_netif' + extract_shell_function "$XCATROOT_SCRIPT" 'xcat_persist_ipv6_boot'; } >"$LIB" + + SYSNET="${BATS_TEST_TMPDIR}/sys-class-net" + mkdir -p "$SYSNET" BIN="${BATS_TEST_TMPDIR}/bin" mkdir -p "$BIN" @@ -135,3 +140,121 @@ persist() [ "$status" -ne 0 ] refute_grep -q . <(ls "$root/etc/NetworkManager/system-connections") } + +# add_netdev -- an interface in the scratch sysfs tree +add_netdev() +{ + mkdir -p "$SYSNET/$1" + printf '%s\n' "$2" >"$SYSNET/$1/address" +} + +# boot_netif -- run the real derivation +boot_netif() +{ + bash -c ' + . "$1" + xcat_boot_netif "$2" "$3" "$4" "$5" + ' bash "$LIB" "$1" "$2" "$3" "$SYSNET" +} + +@test "ifname= names the interface, and its MAC is not part of the name" { + run boot_netif 'ens7:aa:bb:cc:dd:ee:ff' '' '' + [ "$status" -eq 0 ] + [ "$output" = 'ens7' ] +} + +@test "netdev= names the interface when ifname= is absent" { + run boot_netif '' ens7 '' + [ "$status" -eq 0 ] + [ "$output" = 'ens7' ] +} + +# BOOTIF carries the MAC in the PXE form, upper case and hyphen separated, and no name at all. +@test "BOOTIF is matched to an interface by the MAC sysfs reports" { + add_netdev lo '00:00:00:00:00:00' + add_netdev ens7 'aa:bb:cc:dd:ee:ff' + run boot_netif '' '' '01-AA-BB-CC-DD-EE-FF' + [ "$status" -eq 0 ] + [ "$output" = 'ens7' ] +} + +@test "a BOOTIF that matches no interface names no interface" { + add_netdev ens7 'aa:bb:cc:dd:ee:ff' + run boot_netif '' '' '01-00-11-22-33-44-55' + [ "$status" -ne 0 ] + [ -z "$output" ] +} + +# nm_value
-- the value a keyfile gives a key inside one section +nm_value() +{ + awk -v section="[$2]" -v key="$3" -F= ' + $0 == section { inside = 1; next } + /^\[/ { inside = 0 } + inside && $1 == key { print $2; exit } + ' "$1" +} + +# persist_boot -- run the whole handoff +# +# The last argument names the directory the handoff reads live profiles from. The test gives +# one that does not exist, so the profiles of the host this test runs on are never read. +persist_boot() +{ + PATH="$BIN:$PATH" BATS_TEST_TMPDIR="$BATS_TEST_TMPDIR" bash -c ' + lib="$1"; shift + . "$lib" + xcat_persist_ipv6_boot "$1" "$2" "$3" "$4" "$5" + ' bash "$LIB" "$1" "$2" "$3" "$SYSNET" "${BATS_TEST_TMPDIR}/absent" +} + +@test "an IPv6-only boot leaves the address and the route in a NetworkManager keyfile" { + local root="${BATS_TEST_TMPDIR}/root" profile + mkdir -p "$root" + add_netdev ens7 'aa:bb:cc:dd:ee:ff' + printf '3: ens7 inet6 2001:db8:149::20/64 scope global\n' >"${BATS_TEST_TMPDIR}/addr6" + printf 'default via 2001:db8:149::1 dev ens7 metric 100\n' >"${BATS_TEST_TMPDIR}/route6" + run persist_boot "$root" ens7 '[2001:db8:149::20]::::::none' + [ "$status" -eq 0 ] + profile="$root/etc/NetworkManager/system-connections/xcat-ens7.nmconnection" + [ -f "$profile" ] + [ "$(nm_value "$profile" ethernet mac-address)" = 'aa:bb:cc:dd:ee:ff' ] + [ "$(nm_value "$profile" ipv6 address1)" = '2001:db8:149::20/64' ] + [ "$(nm_value "$profile" ipv6 gateway)" = '2001:db8:149::1' ] + [ "$(nm_value "$profile" ipv6 method)" = 'manual' ] + [ "$(nm_value "$profile" ipv4 method)" = 'disabled' ] + [ "$(stat -c '%a' "$profile")" = '600' ] +} + +# The node is reached over IPv4, which NetworkManager configures from the dhcp4 lease of the +# booted system. A keyfile here would pin an address the node is not meant to keep. +@test "a dual-stack boot writes no keyfile and is not a failure" { + local root="${BATS_TEST_TMPDIR}/root" + mkdir -p "$root" + add_netdev ens7 'aa:bb:cc:dd:ee:ff' + printf '3: ens7 inet6 2001:db8:149::20/64 scope global\n' >"${BATS_TEST_TMPDIR}/addr6" + printf '3: ens7 inet 192.0.2.20/24 scope global\n' >"${BATS_TEST_TMPDIR}/addr4" + run persist_boot "$root" ens7 'dhcp' + [ "$status" -eq 0 ] + [ ! -d "$root/etc/NetworkManager/system-connections" ] +} + +# NetworkManager refuses a keyfile whose mac-address is empty, and the interface is then left +# unmanaged. A loud failure sends the node to the panic path instead, which says why. +@test "an interface sysfs gives no MAC is a failure, not a keyfile without one" { + local root="${BATS_TEST_TMPDIR}/root" + mkdir -p "$root" + printf '3: ens7 inet6 2001:db8:149::20/64 scope global\n' >"${BATS_TEST_TMPDIR}/addr6" + run persist_boot "$root" ens7 'dhcp6' + [ "$status" -ne 0 ] + [ ! -f "$root/etc/NetworkManager/system-connections/xcat-ens7.nmconnection" ] +} + +# dracut gives no interface name for a node booted with none of ifname=, netdev= and BOOTIF=. +# There is nothing to persist and nothing is wrong, so the handoff must not fail the boot. +@test "no interface named is not a failure" { + local root="${BATS_TEST_TMPDIR}/root" + mkdir -p "$root" + run persist_boot "$root" '' 'dhcp6' + [ "$status" -eq 0 ] +}