diff --git a/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/10-xcat-genesis.conf b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/10-xcat-genesis.conf index d4a805a0de..17d9cfc02f 100644 --- a/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/10-xcat-genesis.conf +++ b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/10-xcat-genesis.conf @@ -6,5 +6,8 @@ rc-manager=symlink [connection] ipv4.dhcp-timeout=20 +# A node with no SMBIOS UUID gets a hw-address reservation, and Kea reads the hardware +# address out of a DUID-LL. genesis-duid overrides this from 15-xcat-genesis-duid.conf when +# the node does have an SMBIOS UUID, because then the reservation holds a DUID-UUID. ipv6.dhcp-duid=ll ipv6.dhcp-timeout=20 diff --git a/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/genesis-duid b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/genesis-duid new file mode 100755 index 0000000000..c9341ea032 --- /dev/null +++ b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/genesis-duid @@ -0,0 +1,60 @@ +#!/bin/bash +# +# Write the DHCPv6 client identifier that the xCAT reservation holds. +# +# makedhcp builds a DUID-UUID (RFC 6355) from vpd.uuid, and a DHCPv6 host reservation +# carries either a duid or a hw-address, never both. A DUID-LL therefore matches no +# reservation for a node that reported an SMBIOS UUID: the node gets a pool address and no +# bootfile-url. NetworkManager cannot derive a DUID from the SMBIOS UUID. Its own DUID-UUID +# forms come from the machine-id, so write the DUID as a literal hexadecimal value. +set -u + +LC_ALL=C +export LC_ALL + +uuid_file=${XCAT_PRODUCT_UUID_FILE:-/sys/devices/virtual/dmi/id/product_uuid} +conf_dir=${XCAT_NM_CONF_DIR:-/etc/NetworkManager/conf.d} +# NetworkManager reads conf.d in name order and a later file wins, so this name has to sort +# after 10-xcat-genesis.conf, which carries the DUID-LL default. +duid_file=$conf_dir/15-xcat-genesis-duid.conf + +log() { + logger -t xcat-genesis-duid -- "$*" || true +} + +# Without an SMBIOS UUID the node reports a synthetic uuid that makedhcp cannot turn into a +# DUID, so the reservation holds a hw-address and Kea reads the hardware address out of a +# DUID-LL. Leave 10-xcat-genesis.conf in charge. +if [ ! -r "$uuid_file" ]; then + log "no SMBIOS UUID at $uuid_file, keeping the DUID-LL default" + exit 0 +fi + +uuid=$(tr -d -c '[:xdigit:]' <"$uuid_file" | tr 'A-F' 'a-f') +if [ "${#uuid}" -ne 32 ]; then + log "no usable SMBIOS UUID at $uuid_file, keeping the DUID-LL default" + exit 0 +fi + +# SMBIOS 2.6 and later store the first three UUID fields little-endian, and EDK2 copies +# those bytes into its DUID. product_uuid prints the fields in canonical order, so the +# first three go back into firmware order here. doxcat and makedhcp do the same swap. +octets=${uuid:6:2}:${uuid:4:2}:${uuid:2:2}:${uuid:0:2} +octets=$octets:${uuid:10:2}:${uuid:8:2} +octets=$octets:${uuid:14:2}:${uuid:12:2} +for offset in 16 18 20 22 24 26 28 30; do + octets=$octets:${uuid:$offset:2} +done + +if ! mkdir -p "$conf_dir"; then + log "unable to create $conf_dir" + exit 1 +fi + +# NetworkManager reads conf.d at startup only, so this unit runs before NetworkManager. +if ! printf '%s\n' '[connection]' "ipv6.dhcp-duid=00:04:$octets" >"$duid_file"; then + log "unable to write $duid_file" + exit 1 +fi + +log "DHCPv6 client identifier is 00:04:$octets" diff --git a/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/xcat-genesis-duid.service b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/xcat-genesis-duid.service new file mode 100644 index 0000000000..dfef03c43d --- /dev/null +++ b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/files/xcat-genesis-duid.service @@ -0,0 +1,14 @@ +[Unit] +Description=Write the xCAT Genesis DHCPv6 client identifier +ConditionKernelCommandLine=xcatd +After=systemd-udev-trigger.service +Before=NetworkManager.service xcat-genesis-network-state.service + +[Service] +Type=oneshot +ExecStart=/usr/libexec/xcat/genesis-duid +TimeoutStartSec=30 +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target diff --git a/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/xcat-genesis-network_1.0.bb b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/xcat-genesis-network_1.0.bb index 849a601bb1..3126792221 100644 --- a/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/xcat-genesis-network_1.0.bb +++ b/xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network/xcat-genesis-network_1.0.bb @@ -2,15 +2,34 @@ SUMMARY = "xCAT Genesis network configuration" LICENSE = "EPL-1.0" LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/EPL-1.0;md5=57f8d5e2b3e98ac6e088986c12bf94e6" -SRC_URI = "file://10-xcat-genesis.conf" +SRC_URI = "file://10-xcat-genesis.conf \ + file://genesis-duid \ + file://xcat-genesis-duid.service \ +" S = "${UNPACKDIR}" -RDEPENDS:${PN} = "networkmanager-daemon networkmanager-nmcli" +inherit systemd + +RDEPENDS:${PN} = "bash coreutils networkmanager-daemon networkmanager-nmcli util-linux-logger" + +SYSTEMD_SERVICE:${PN} = "xcat-genesis-duid.service" +SYSTEMD_AUTO_ENABLE = "enable" do_install() { install -d ${D}${sysconfdir}/NetworkManager/conf.d install -m 0644 ${UNPACKDIR}/10-xcat-genesis.conf \ ${D}${sysconfdir}/NetworkManager/conf.d/10-xcat-genesis.conf + + install -d ${D}${libexecdir}/xcat + install -m 0755 ${UNPACKDIR}/genesis-duid \ + ${D}${libexecdir}/xcat/genesis-duid + + install -d ${D}${systemd_system_unitdir} + install -m 0644 ${UNPACKDIR}/xcat-genesis-duid.service \ + ${D}${systemd_system_unitdir}/xcat-genesis-duid.service } -FILES:${PN} = "${sysconfdir}/NetworkManager/conf.d/10-xcat-genesis.conf" +FILES:${PN} = "${sysconfdir}/NetworkManager/conf.d/10-xcat-genesis.conf \ + ${libexecdir}/xcat/genesis-duid \ + ${systemd_system_unitdir}/xcat-genesis-duid.service \ +" diff --git a/xCAT-test/bats/genesis_openembedded_duid.bats b/xCAT-test/bats/genesis_openembedded_duid.bats new file mode 100644 index 0000000000..1c30ca8e1a --- /dev/null +++ b/xCAT-test/bats/genesis_openembedded_duid.bats @@ -0,0 +1,101 @@ +#!/usr/bin/env bats +# +# genesis-duid runs in the OpenEmbedded Genesis image before NetworkManager. It turns the +# SMBIOS UUID into the DUID-UUID that the xCAT DHCPv6 reservation holds, and writes it into +# a NetworkManager conf.d drop-in that sorts after the packaged DUID-LL default. +# +# The expected DUIDs are the same literals as xCAT-test/bats/genesis_duid_uuid.bats, which +# holds the legacy image to the same byte order, and as the Kea reservation in +# xCAT-test/unit/dhcp_kea_plugin_intent.t. logger is a scratch script that logs its +# arguments. + +load 'helpers/shell_source' + +NETWORK='xCAT-genesis-base/oe/meta-xcat-genesis/recipes-connectivity/xcat-genesis-network' + +setup() +{ + # repo_path, not require_repo_file: this repository ships the script, so an absent one + # has to fail rather than skip. + DUID_SCRIPT="$(repo_path "$NETWORK/files/genesis-duid")" + PACKAGED_CONF="$(require_repo_file "$NETWORK/files/10-xcat-genesis.conf")" + bin="$BATS_TEST_TMPDIR/bin" + mkdir -p "$bin" + printf '#!/bin/sh\nprintf "logger %%s\\n" "$*" >>"$XCAT_TEST_LOG"\n' >"$bin/logger" + chmod 0755 "$bin"/* + + export PATH="$bin:$PATH" + export XCAT_PRODUCT_UUID_FILE="$BATS_TEST_TMPDIR/product_uuid" + export XCAT_NM_CONF_DIR="$BATS_TEST_TMPDIR/conf.d" + export XCAT_TEST_LOG="$BATS_TEST_TMPDIR/commands.log" + mkdir -p "$XCAT_NM_CONF_DIR" + cp "$PACKAGED_CONF" "$XCAT_NM_CONF_DIR/10-xcat-genesis.conf" + : >"$XCAT_TEST_LOG" + drop_in="$XCAT_NM_CONF_DIR/15-xcat-genesis-duid.conf" +} + +# Run genesis-duid against a product_uuid. $status is its exit status. +run_duid() +{ + printf '%s\n' "$1" >"$XCAT_PRODUCT_UUID_FILE" + run /bin/bash "$DUID_SCRIPT" +} + +# The conf.d file names, in the order NetworkManager reads them. +conf_order() +{ + (cd "$XCAT_NM_CONF_DIR" && printf '%s\n' * | LC_ALL=C sort) +} + +@test "the DUID carries the first three SMBIOS fields in firmware order" { + run_duid 00112233-4455-6677-8899-aabbccddeeff + [ "$status" -eq 0 ] + [ "$(cat "$drop_in")" = '[connection] +ipv6.dhcp-duid=00:04:33:22:11:00:55:44:77:66:88:99:aa:bb:cc:dd:ee:ff' ] +} + +@test "a second UUID gives its own DUID, swapped the same way" { + run_duid aabbccdd-eeff-0011-2233-445566778899 + [ "$status" -eq 0 ] + [ "$(cat "$drop_in")" = '[connection] +ipv6.dhcp-duid=00:04:dd:cc:bb:aa:ff:ee:11:00:22:33:44:55:66:77:88:99' ] +} + +@test "an uppercase SMBIOS UUID gives the same DUID as its lowercase form" { + run_duid 00112233-4455-6677-8899-AABBCCDDEEFF + [ "$status" -eq 0 ] + [ "$(cat "$drop_in")" = '[connection] +ipv6.dhcp-duid=00:04:33:22:11:00:55:44:77:66:88:99:aa:bb:cc:dd:ee:ff' ] +} + +@test "the drop-in overrides the packaged DUID-LL, because it is read later" { + run_duid 00112233-4455-6677-8899-aabbccddeeff + [ "$status" -eq 0 ] + [ "$(conf_order)" = '10-xcat-genesis.conf +15-xcat-genesis-duid.conf' ] + grep -Fx 'ipv6.dhcp-duid=ll' "$XCAT_NM_CONF_DIR/10-xcat-genesis.conf" +} + +@test "a machine with no SMBIOS UUID keeps the packaged DUID-LL" { + rm -f "$XCAT_PRODUCT_UUID_FILE" + run /bin/bash "$DUID_SCRIPT" + [ "$status" -eq 0 ] + [ "$(conf_order)" = 10-xcat-genesis.conf ] + grep -F 'keeping the DUID-LL default' "$XCAT_TEST_LOG" +} + +@test "a machine whose SMBIOS UUID is not sixteen bytes keeps the packaged DUID-LL" { + run_duid 'Not Settable' + [ "$status" -eq 0 ] + [ "$(conf_order)" = 10-xcat-genesis.conf ] + grep -F 'no usable SMBIOS UUID' "$XCAT_TEST_LOG" +} + +@test "a conf.d directory that cannot be created fails loudly" { + # A regular file in the way, so mkdir -p fails for root as well. + printf 'not a directory\n' >"$BATS_TEST_TMPDIR/blocked" + XCAT_NM_CONF_DIR="$BATS_TEST_TMPDIR/blocked/conf.d" run_duid \ + 00112233-4455-6677-8899-aabbccddeeff + [ "$status" -eq 1 ] + grep -F 'unable to create' "$XCAT_TEST_LOG" +} diff --git a/xCAT-test/native/genesis_metadata.py b/xCAT-test/native/genesis_metadata.py index b45e4bc2b9..ab5f7ae0f3 100644 --- a/xCAT-test/native/genesis_metadata.py +++ b/xCAT-test/native/genesis_metadata.py @@ -195,7 +195,7 @@ class GenesisMetadata(unittest.TestCase): def test_runtime_dependencies(self): for recipe, expected in { - "xcat-genesis-network": "networkmanager-daemon networkmanager-nmcli", + "xcat-genesis-network": "bash coreutils networkmanager-daemon networkmanager-nmcli util-linux-logger", "xcat-genesis-protocol": "bash openssl-bin", "xcat-genesis-extensions": "bash coreutils jq openssl-bin systemd xcat-genesis-init", "xcat-genesis-hardware-control": "bash coreutils jq mstflint nvme-cli util-linux-flock", @@ -366,7 +366,18 @@ class GenesisMetadata(unittest.TestCase): config = self.unit(file) self.assertEqual(config["connection"]["ipv4.dhcp-timeout"], "20") self.assertEqual(config["connection"]["ipv6.dhcp-timeout"], "20") + # A node with no SMBIOS UUID gets a hw-address reservation, which Kea matches by + # reading the hardware address out of a DUID-LL. genesis-duid overrides this from + # 15-xcat-genesis-duid.conf, which NetworkManager reads later. self.assertEqual(config["connection"]["ipv6.dhcp-duid"], "ll") + self.installed(data, root, "libexecdir", "xcat/genesis-duid", 0o755) + service = self.unit(self.installed(data, root, "systemd_system_unitdir", + "xcat-genesis-duid.service", 0o644)) + self.assertEqual(service["Service"]["ExecStart"], "/usr/libexec/xcat/genesis-duid") + self.assertEqual(set(service["Unit"]["Before"].split()), + {"NetworkManager.service", "xcat-genesis-network-state.service"}) + self.assertEqual(self.words(data, "SYSTEMD_SERVICE:" + data.getVar("PN")), + {"xcat-genesis-duid.service"}) def test_installed_hardware_providers(self): data, root = self.install("xcat-genesis-hardware-control") diff --git a/xCAT-test/unit/genesis_openembedded_duid.t b/xCAT-test/unit/genesis_openembedded_duid.t new file mode 100644 index 0000000000..bf64fb2f9e --- /dev/null +++ b/xCAT-test/unit/genesis_openembedded_duid.t @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use Test::More; + +use XCAT::Test::File qw(repo_path slurp_repo_file); + +# The systemd unit and the recipe are the artifact. genesis-duid is shell and is tested in +# xCAT-test/bats/genesis_openembedded_duid.bats. + +my $META = 'xCAT-genesis-base/oe/meta-xcat-genesis'; +my $NETWORK = "$META/recipes-connectivity/xcat-genesis-network"; + +#--- +# =head3 contents_of +# +# Descriptions: Read a repository file, or return an empty string when it is absent. +# +# Arguments: +# $relative - the path of the file, relative to the checkout +# +# Returns: the contents of the file, or an empty string +# +# A missing file has to fail each assertion that names it. slurp_repo_file dies instead, +# which names no assertion at all. +#--- +sub contents_of { + my ($relative) = @_; + + return -f repo_path($relative) ? slurp_repo_file($relative) : ''; +} + +my $service_contents = contents_of("$NETWORK/files/xcat-genesis-duid.service"); +like( $service_contents, qr/^ConditionKernelCommandLine=xcatd$/m, + 'the client identifier service requires a Genesis boot' ); +like( $service_contents, qr/^Before=.*\bNetworkManager\.service\b/m, + 'the client identifier is written before NetworkManager reads conf.d' ); +like( $service_contents, qr/^Before=.*\bxcat-genesis-network-state\.service\b/m, + 'the client identifier is written before management network selection' ); +like( $service_contents, qr{^ExecStart=/usr/libexec/xcat/genesis-duid$}m, + 'the service runs the client identifier script' ); +like( $service_contents, qr/^Type=oneshot$/m, + 'the client identifier is written once per boot' ); + +my $recipe_contents = contents_of("$NETWORK/xcat-genesis-network_1.0.bb"); +like( $recipe_contents, qr/^SYSTEMD_SERVICE:\$\{PN\} = "xcat-genesis-duid\.service"$/m, + 'the recipe enables the client identifier service' ); +like( $recipe_contents, qr{\$\{D\}\$\{libexecdir\}/xcat/genesis-duid$}m, + 'the recipe installs the client identifier script' ); + +ok( -f repo_path("$NETWORK/files/genesis-duid"), + 'the client identifier script is in the OpenEmbedded layer' ); + +done_testing();