From d69fe2436ef5bc71516ed39156488ae7c7e2c485 Mon Sep 17 00:00:00 2001 From: Dusty Mabe Date: Thu, 4 May 2023 15:58:53 -0400 Subject: [PATCH] overlay.d/05core: add ifname= karg udev rule propagation This adds to coreos-teardown-initramfs.{service,sh} the ability to propagate udev rules that were generated by ifname-genrules.sh [1] to the real root of the machine to allow for an ephemeral ifname= karg on the first boot to have persistent configuration effect (similar to our other networking kernel arguments). This also adds tests for both the firstboot and everyboot case for the ifname= karg. Fixes https://github.com/coreos/fedora-coreos-tracker/issues/553 [1] https://github.com/dracutdevs/dracut/blob/5c3d0a96473ac339fa2d1b25213b8f301c1cfd0d/modules.d/40network/ifname-genrules.sh --- .../coreos-teardown-initramfs.sh | 15 ++++++ .../networking/ifname-karg/data/commonlib.sh | 1 + .../ifname-karg/data/ifname-karg-lib.sh | 36 ++++++++++++++ .../everyboot-systemd-link-file/config.bu | 7 +++ .../data/commonlib.sh | 1 + .../data/ifname-karg-lib.sh | 1 + .../everyboot-systemd-link-file/test.sh | 43 +++++++++++++++++ .../udev-rule-firstboot-propagation | 47 +++++++++++++++++++ 8 files changed, 151 insertions(+) create mode 120000 tests/kola/networking/ifname-karg/data/commonlib.sh create mode 100644 tests/kola/networking/ifname-karg/data/ifname-karg-lib.sh create mode 100644 tests/kola/networking/ifname-karg/everyboot-systemd-link-file/config.bu create mode 120000 tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/commonlib.sh create mode 120000 tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/ifname-karg-lib.sh create mode 100755 tests/kola/networking/ifname-karg/everyboot-systemd-link-file/test.sh create mode 100755 tests/kola/networking/ifname-karg/udev-rule-firstboot-propagation diff --git a/overlay.d/05core/usr/lib/dracut/modules.d/35coreos-ignition/coreos-teardown-initramfs.sh b/overlay.d/05core/usr/lib/dracut/modules.d/35coreos-ignition/coreos-teardown-initramfs.sh index 7f52ff85d6..afe69a99b4 100755 --- a/overlay.d/05core/usr/lib/dracut/modules.d/35coreos-ignition/coreos-teardown-initramfs.sh +++ b/overlay.d/05core/usr/lib/dracut/modules.d/35coreos-ignition/coreos-teardown-initramfs.sh @@ -168,6 +168,20 @@ propagate_initramfs_hostname() { fi } +# Propagate the ifname= karg udev rules. The policy here is: +# +# - IF ifname karg udev rule file exists +# - THEN copy it to the real root +# +propagate_ifname_udev_rules() { + local udev_file='/etc/udev/rules.d/80-ifname.rules' + if [ -e "${udev_file}" ]; then + echo "info: propagating ifname karg udev rules to the real root" + cp -v "${udev_file}" "/sysroot/${udev_file}" + coreos-relabel "${udev_file}" + fi +} + main() { # Load libraries from dracut load_dracut_libs @@ -192,6 +206,7 @@ main() { else propagate_initramfs_hostname propagate_initramfs_networking + propagate_ifname_udev_rules fi # Configuration has been propagated, but we can't clean up diff --git a/tests/kola/networking/ifname-karg/data/commonlib.sh b/tests/kola/networking/ifname-karg/data/commonlib.sh new file mode 120000 index 0000000000..b8dcbdca1a --- /dev/null +++ b/tests/kola/networking/ifname-karg/data/commonlib.sh @@ -0,0 +1 @@ +../../../data/commonlib.sh \ No newline at end of file diff --git a/tests/kola/networking/ifname-karg/data/ifname-karg-lib.sh b/tests/kola/networking/ifname-karg/data/ifname-karg-lib.sh new file mode 100644 index 0000000000..8b6dd2f8dc --- /dev/null +++ b/tests/kola/networking/ifname-karg/data/ifname-karg-lib.sh @@ -0,0 +1,36 @@ +# This is a library created for our ifname-karg tests + +. $KOLA_EXT_DATA/commonlib.sh + +# check IP for given NIC name +check_ip() { + # User provides the NIC name. + local nic_name=$1 + # The expected IP is the first one in the range given out by QEMU + # user mode networking: https://www.qemu.org/docs/master/system/devices/net.html#using-the-user-mode-network-stack + local expected_ip="10.0.2.15" + # Verify the given nic name has the expected IP. + local nic_ip=$(get_ipv4_for_nic ${nic_name}) + if [ "${nic_ip}" != "${expected_ip}" ]; then + fatal "Error: get ${nic_name} ip = ${nic_ip}, expected is ${expected_ip}" + fi + ok "get ${nic_name} ip is ${expected_ip}" +} + +# simple file existence check +check_file_exists() { + local file=$1 + if [ ! -f $file ]; then + fatal "expected file ${file} doesn't exist on disk" + fi + ok "expected file ${file} exists on disk" +} + +# simple file non-existence check +check_file_not_exists() { + local file=$1 + if [ -f $file ]; then + fatal "expected file ${file} to not exist on disk" + fi + ok "file ${file} does not exist on disk" +} diff --git a/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/config.bu b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/config.bu new file mode 100644 index 0000000000..c79d3aa60e --- /dev/null +++ b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/config.bu @@ -0,0 +1,7 @@ +variant: fcos +version: 1.4.0 +kernel_arguments: + should_exist: + # Persistently set the ifname kernel argument to set the given MAC to the + # NIC named `kolatest`. The MAC address is the default one QEMU assigns. + - ifname=kolatest:52:54:00:12:34:56 diff --git a/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/commonlib.sh b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/commonlib.sh new file mode 120000 index 0000000000..7028449b11 --- /dev/null +++ b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/commonlib.sh @@ -0,0 +1 @@ +../../../../data/commonlib.sh \ No newline at end of file diff --git a/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/ifname-karg-lib.sh b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/ifname-karg-lib.sh new file mode 120000 index 0000000000..d50acc0fb4 --- /dev/null +++ b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/data/ifname-karg-lib.sh @@ -0,0 +1 @@ +../../data/ifname-karg-lib.sh \ No newline at end of file diff --git a/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/test.sh b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/test.sh new file mode 100755 index 0000000000..afc715f3f4 --- /dev/null +++ b/tests/kola/networking/ifname-karg/everyboot-systemd-link-file/test.sh @@ -0,0 +1,43 @@ +#!/bin/bash +## kola: +## description: Verify persistent ifname= karg works via systemd-network-generator. +## # appendFirstbootKernelArgs is only supported on QEMU +## platforms: qemu +## # Don't run the propagate code. With this test we want to +## # validate that the systemd.link file gets created by +## # systemd-network-generator. +## appendFirstbootKernelArgs: "coreos.no_persist_ip" +## # appendFirstbootKernelArgs doesn't work on s390x +## # https://github.com/coreos/coreos-assembler/issues/2776 +## architectures: "!s390x" + +set -xeuo pipefail + +. $KOLA_EXT_DATA/commonlib.sh +. $KOLA_EXT_DATA/ifname-karg-lib.sh + +nicname='kolatest' + +run_tests() { + # Make sure nothing was persisted from the initramfs + check_file_not_exists '/etc/udev/rules.d/80-ifname.rules' + # Make sure systemd-network-generator ran (from the real root) + check_file_exists "/run/systemd/network/90-${nicname}.link" + # Make sure the NIC is in use and got the expected IP address + check_ip "${nicname}" +} + +case "${AUTOPKGTEST_REBOOT_MARK:-}" in + "") + ok "first boot" + run_tests + /tmp/autopkgtest-reboot rebooted + ;; + + rebooted) + ok "second boot" + run_tests + ;; + + *) fatal "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}";; +esac diff --git a/tests/kola/networking/ifname-karg/udev-rule-firstboot-propagation b/tests/kola/networking/ifname-karg/udev-rule-firstboot-propagation new file mode 100755 index 0000000000..0d11c01691 --- /dev/null +++ b/tests/kola/networking/ifname-karg/udev-rule-firstboot-propagation @@ -0,0 +1,47 @@ +#!/bin/bash +## kola: +## description: Verify firstboot ifname= karg udev rule propoagation works. +## # appendFirstbootKernelArgs is only supported on QEMU +## platforms: qemu +## # Append ifname kernel argument to set the given MAC address to the NIC +## # named `kolatest`. The MAC address is the default one QEMU assigns. +## appendFirstbootKernelArgs: "ifname=kolatest:52:54:00:12:34:56" +## # appendFirstbootKernelArgs doesn't work on s390x +## # https://github.com/coreos/coreos-assembler/issues/2776 +## architectures: "!s390x" + +# Part of https://github.com/coreos/fedora-coreos-tracker/issues/553 + +set -xeuo pipefail + +. $KOLA_EXT_DATA/commonlib.sh +. $KOLA_EXT_DATA/ifname-karg-lib.sh + +nicname='kolatest' + +case "${AUTOPKGTEST_REBOOT_MARK:-}" in + "") + ok "first boot" + # Make sure the rules were persisted from the initramfs + check_file_exists '/etc/udev/rules.d/80-ifname.rules' + # On first boot we expect systemd-network-generator to run too + # because the ifname= karg was present, but only for first boot + check_file_exists "/run/systemd/network/90-${nicname}.link" + # Make sure the NIC is in use and got the expected IP address + check_ip "${nicname}" + /tmp/autopkgtest-reboot rebooted + ;; + + rebooted) + ok "second boot" + # Make sure the rules are still there + check_file_exists '/etc/udev/rules.d/80-ifname.rules' + # On second boot the ifname= karg isn't there so the file + # created by systemd-network-generator shouldn't exist. + check_file_not_exists "/run/systemd/network/90-${nicname}.link" + # Make sure the NIC is in use and got the expected IP address + check_ip "${nicname}" + ;; + + *) fatal "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}";; +esac