Skip to content

Commit

Permalink
overlay.d/05core: add ifname= karg udev rule propagation
Browse files Browse the repository at this point in the history
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 coreos/fedora-coreos-tracker#553

[1] https://github.com/dracutdevs/dracut/blob/5c3d0a96473ac339fa2d1b25213b8f301c1cfd0d/modules.d/40network/ifname-genrules.sh
  • Loading branch information
dustymabe committed May 5, 2023
1 parent 8533119 commit 84bc230
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/kola/networking/ifname-karg/data/commonlib.sh
36 changes: 36 additions & 0 deletions tests/kola/networking/ifname-karg/data/ifname-karg-lib.sh
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions tests/kola/networking/ifname-karg/udev-rule-firstboot-propagation
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 84bc230

Please sign in to comment.