From 7b518ca7428f00027e540c00e4ae23689bb3cb9c Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:46:50 -0300 Subject: [PATCH 01/25] test(xcat-core): capture the missing Secure Boot shim in the iPXE boot scripts With netboot=ipxe a UEFI node loads snponly-shim.efi, which Microsoft signs, and reaches iPXE with Secure Boot on. The boot scripts then load the kernel with imgfetch and imgload, and the firmware refuses it: the signature database does not hold the keys of the distribution, and nothing signs the Genesis kernel. The node stops with "Verification failed: Security Policy Violation". The two tests drive nodeset and mknb into a scratch TFTP root and read the UEFI scripts they write. Four assertions of nine and two of seven fail on master: no script names a shim. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-test/unit/secureboot_genesis_shim.t | 103 ++++++++++++ xCAT-test/unit/secureboot_ipxe_node_shim.t | 179 +++++++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100644 xCAT-test/unit/secureboot_genesis_shim.t create mode 100644 xCAT-test/unit/secureboot_ipxe_node_shim.t diff --git a/xCAT-test/unit/secureboot_genesis_shim.t b/xCAT-test/unit/secureboot_genesis_shim.t new file mode 100644 index 0000000000..111eeebff1 --- /dev/null +++ b/xCAT-test/unit/secureboot_genesis_shim.t @@ -0,0 +1,103 @@ +#!/usr/bin/env perl +# An unknown UEFI client with the upstream iPXE loader reads its discovery script from +# xcat/ipxe/nets. Nothing signs the Genesis kernel, so with Secure Boot on the script must name a +# shim, which verifies the kernel against the MOK list that the site enrolls. mknb writes the +# scripts into a scratch TFTP root. Tables and name resolution are stand-ins. +use strict; +use warnings; +## no critic (Modules::RequireFilenameMatchesPackage) + +use File::Path qw(make_path); +use File::Slurper qw(read_text write_binary); +use File::Temp qw(tempdir); +use FindBin; +use Test::More; + +BEGIN { + package xCAT::Utils; + $INC{'xCAT/Utils.pm'} = __FILE__; + + package xCAT::TableUtils; + our $tftpdir; + sub getTftpDir { return $tftpdir } + sub get_site_attribute { return $_[-1] eq 'master' ? ('master.example.com') : () } + $INC{'xCAT/TableUtils.pm'} = __FILE__; + + package xCAT::NetworkUtils; + sub my_nets { return { '192.168.144.0/20' => ['192.168.148.10'] } } + sub my_hexnets { return { c0a89 => ['192.168.148.10'] } } + sub getipaddr { return ('203.0.113.10') } + sub get_nic_ip { return {} } + $INC{'xCAT/NetworkUtils.pm'} = __FILE__; + + package xCAT::MsgUtils; + sub trace { return } + $INC{'xCAT/MsgUtils.pm'} = __FILE__; + + package xCAT::Table; + sub new { return } + $INC{'xCAT/Table.pm'} = __FILE__; + + package xCAT::NodeRange; + sub import { + my $caller = caller; + no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict) + *{"${caller}::noderange"} = sub { return }; + return; + } + $INC{'xCAT/NodeRange.pm'} = __FILE__; + + package main; + $INC{'xCAT_monitoring/monitorctrl.pm'} = __FILE__; +} + +my $repo = "$FindBin::Bin/../.."; +unshift @INC, "$repo/perl-xCAT", "$repo/xCAT-server/lib/perl"; +require "$repo/xCAT-server/lib/xcat/plugins/mknb.pm"; ## no critic (Modules::RequireBarewordIncludes) + +my $tmp = tempdir( CLEANUP => 1 ); +$::XCATROOT = "$tmp/xcatroot"; +make_path("$::XCATROOT/share/xcat/netboot/genesis/x86_64"); + +# What mknb needs in the TFTP root before it writes a configuration: the payload it boots, and the +# Secure Boot shim that ipxe-xcat installs. +sub prepare { + my (%opt) = @_; + $xCAT::TableUtils::tftpdir = "$tmp/$opt{name}"; + make_path( "$xCAT::TableUtils::tftpdir/xcat", "$xCAT::TableUtils::tftpdir/etc" ); + write_binary( "$xCAT::TableUtils::tftpdir/xcat/genesis.kernel.x86_64", '' ); + write_binary( "$xCAT::TableUtils::tftpdir/xcat/genesis.fs.x86_64.gz", '' ); + if ( $opt{shim} ) { + make_path("$xCAT::TableUtils::tftpdir/xcat/ipxe/x86_64-sb"); + write_binary( "$xCAT::TableUtils::tftpdir/xcat/ipxe/x86_64-sb/shimx64.efi", 'shim' ); + } + my @errors; + xCAT_plugin::mknb::process_request( { arg => [ 'x86_64', '--configfileonly' ] }, + sub { push @errors, grep { ref eq 'HASH' && $_->{error} } @_; return } ); + is_deeply( \@errors, [], "mknb writes the x86_64 configuration with shim=" . ( $opt{shim} ? 1 : 0 ) ); + return; +} + +sub script { + my ($relative) = @_; + return -f "$xCAT::TableUtils::tftpdir/$relative" ? read_text("$xCAT::TableUtils::tftpdir/$relative") : ''; +} + +prepare( name => 'tftpboot-shim', shim => 1 ); +like( + script('xcat/ipxe/nets/192.168.144.0_20.uefi'), + qr{^imgload kernel\nshim http://\$\{next-server\}/tftpboot/xcat/ipxe/x86_64-sb/shimx64\.efi$}m, + 'the UEFI discovery script names the shim of ipxe-xcat, after it selects the Genesis kernel' +); +like( script('xcat/ipxe/nets/192.168.144.0_20.uefi'), qr{^shim \S+\nimgargs kernel }m, + 'and names it before the kernel runs' ); +unlike( script('xcat/ipxe/nets/192.168.144.0_20'), qr{^shim }m, + 'the BIOS discovery script names no shim' ); +unlike( script('xcat/ipxe/nets/192.168.144.0_20.elilo'), qr{^shim }m, + 'the elilo configuration names no shim' ); + +prepare( name => 'tftpboot-noshim', shim => 0 ); +unlike( script('xcat/ipxe/nets/192.168.144.0_20.uefi'), qr{^shim }m, + 'a server without the ipxe-xcat shim names no shim' ); + +done_testing(); diff --git a/xCAT-test/unit/secureboot_ipxe_node_shim.t b/xCAT-test/unit/secureboot_ipxe_node_shim.t new file mode 100644 index 0000000000..e8899d569e --- /dev/null +++ b/xCAT-test/unit/secureboot_ipxe_node_shim.t @@ -0,0 +1,179 @@ +#!/usr/bin/env perl +# A UEFI node with netboot=ipxe loads the shim that Microsoft signs, so it reaches iPXE with Secure +# Boot on and stops at the kernel. Its boot script must name a shim that verifies that kernel. +# nodeset writes the scripts through the real plugins into a scratch TFTP root. Tables, name +# resolution and the destiny subrequest are stand-ins. +use strict; +use warnings; + +use File::Path qw(make_path); +use File::Slurper qw(read_text write_binary); +use File::Temp qw(tempdir); +use FindBin; +use Test::More; + +my $repo = "$FindBin::Bin/../.."; +my $tftp = tempdir( CLEANUP => 1 ); +my $installroot = tempdir( CLEANUP => 1 ); +our %rows; + +BEGIN { + package xCAT::Table; + sub new { my ( $class, $table ) = @_; return bless { table => $table }, $class } + sub getNodesAttribs { + my ( $self, $nodes ) = @_; + return { map { $_ => [ $main::rows{ $self->{table} }{$_} ] } @$nodes }; + } + sub getNodeAttribs { my ( $self, $node ) = @_; return $main::rows{ $self->{table} }{$node} } + sub getAttribs { return } + sub close { return } + + package xCAT::NetworkUtils; + sub determinehostname { return ('mn') } + sub checkNodeIPaddress { return { ip => '192.0.2.10' } } + sub nodeonmynet { return 1 } + + package xCAT::TableUtils; + sub getTftpDir { return $tftp } + sub getInstallDir { return $installroot } + sub get_site_attribute { return $_[1] eq 'dhcpsetup' ? ('n') : () } # no makedhcp from nodeset + + package xCAT::MsgUtils; + sub trace { return } + sub message { return } + + package main; + for my $module (qw(Table NetworkUtils TableUtils MsgUtils Utils NodeRange Scope ServiceNodeUtils Usage BootUtils)) { + $INC{"xCAT/$module.pm"} = __FILE__; + } +} + +# xcatd loads the plugins from their files, and ipxe.pm requires xnba.pm by module name. +my $inc = tempdir( CLEANUP => 1 ); +symlink( "$repo/xCAT-server/lib/xcat/plugins", "$inc/xCAT_plugin" ) or die "symlink: $!"; +unshift @INC, $inc, "$repo/xCAT-server/lib/perl", "$repo/perl-xCAT"; +require xCAT_plugin::ipxe; + +# iPXE boots a kernel that carries an EFI stub as an EFI image, which is the path the shim verifies. +# xnba.pm reads the PE header of the file to recognize it. +sub kernel_with_efistub { + my ($relative) = @_; + ( my $dir = "$tftp/$relative" ) =~ s{/[^/]+$}{}; + make_path($dir); + write_binary( "$tftp/$relative", "MZ" . ( "\0" x 64 ) . pack( 'H*', '504500006486' ) ); + return $relative; +} + +sub install_source { + my ( $os, $arch, @shims ) = @_; + for my $shim (@shims) { + ( my $dir = "$installroot/$os/$arch/$shim" ) =~ s{/[^/]+$}{}; + make_path($dir); + write_binary( "$installroot/$os/$arch/$shim", 'shim' ); + } + return; +} + +sub nodeset { + my ( $plugin, $node, %attrib ) = @_; + $rows{noderes}{$node} = { tftpdir => $tftp, netboot => $plugin }; + $rows{chain}{$node} = { currstate => 'install rhels9-x86_64-compute' }; + $rows{nodetype}{$node} = + { provmethod => 'install', os => $attrib{os}, arch => $attrib{arch} }; + my $subreq = sub { + my ($request) = @_; + $request->{bootparams}{$node} = [ { map { $_ => $attrib{$_} } qw(kernel initrd kcmdline) } ] + if $request->{command}[0] eq 'setdestiny'; + return; + }; + "xCAT_plugin::$plugin"->can('process_request') + ->( { command => ['nodeset'], node => [$node], arg => ['osimage'] }, sub { }, $subreq ); + return; +} + +sub script { + my ($relative) = @_; + return -f "$tftp/$relative" ? read_text("$tftp/$relative") : ''; +} + +sub ipxe_shim_line { + my ($path) = @_; + return qr{^imgload kernel\nshim http://\$\{next-server\}\Q$path\E$}m; +} + +# An EL install source: copycds keeps the upper-case names of the DVD. +install_source( 'alma9.8', 'x86_64', 'EFI/BOOT/BOOTX64.EFI' ); +nodeset( + ipxe => 'cn01', + os => 'alma9.8', arch => 'x86_64', + kernel => kernel_with_efistub('xcat/osimage/alma9.8-install/vmlinuz'), + initrd => 'xcat/osimage/alma9.8-install/initrd.img', kcmdline => 'quiet', +); +like( + script('xcat/ipxe/nodes/cn01.uefi'), + ipxe_shim_line('/install/alma9.8/x86_64/EFI/BOOT/BOOTX64.EFI'), + 'the UEFI script of an ipxe node names the shim of its install source, after it selects the kernel' +); +like( script('xcat/ipxe/nodes/cn01.uefi'), qr{^shim \S+\nimgargs kernel }m, + 'and names it before the kernel runs' ); +unlike( script('xcat/ipxe/nodes/cn01'), qr{^shim }m, + 'the BIOS script of the same node names no shim' ); + +# An Ubuntu install source: the same files, in lower case. +install_source( 'ubuntu24.04.4', 'x86_64', 'EFI/boot/bootx64.efi' ); +nodeset( + ipxe => 'cn02', + os => 'ubuntu24.04.4', arch => 'x86_64', + kernel => kernel_with_efistub('xcat/osimage/ubuntu24.04.4-install/vmlinuz'), + initrd => 'xcat/osimage/ubuntu24.04.4-install/initrd.img', kcmdline => 'quiet', +); +like( + script('xcat/ipxe/nodes/cn02.uefi'), + ipxe_shim_line('/install/ubuntu24.04.4/x86_64/EFI/boot/bootx64.efi'), + 'an install source that names the shim in lower case is found as well' +); + +# No install source for this node. ipxe-xcat always installs its own Secure Boot shim, and every +# shim reads the MOK list from the firmware. +make_path("$tftp/xcat/ipxe/x86_64-sb"); +write_binary( "$tftp/xcat/ipxe/x86_64-sb/shimx64.efi", 'shim' ); +nodeset( + ipxe => 'cn03', + os => 'alma10.1', arch => 'x86_64', + kernel => kernel_with_efistub('xcat/osimage/alma10.1-install/vmlinuz'), + initrd => 'xcat/osimage/alma10.1-install/initrd.img', kcmdline => 'quiet', +); +like( + script('xcat/ipxe/nodes/cn03.uefi'), + ipxe_shim_line('/tftpboot/xcat/ipxe/x86_64-sb/shimx64.efi'), + 'a node whose install source carries no shim falls back to the shim of ipxe-xcat' +); + +# ppc64le has no UEFI shim, and netboot=xnba loads an unsigned loader that Secure Boot refuses. +nodeset( + ipxe => 'cn04', + os => 'alma9.8', arch => 'ppc64le', + kernel => kernel_with_efistub('xcat/osimage/alma9.8-ppc/vmlinuz'), + initrd => 'xcat/osimage/alma9.8-ppc/initrd.img', kcmdline => 'quiet', +); +unlike( script('xcat/ipxe/nodes/cn04.uefi'), qr{^shim }m, 'a ppc64le node names no shim' ); + +nodeset( + xnba => 'cn05', + os => 'alma9.8', arch => 'x86_64', + kernel => kernel_with_efistub('xcat/osimage/alma9.8-install/vmlinuz'), + initrd => 'xcat/osimage/alma9.8-install/initrd.img', kcmdline => 'quiet', +); +unlike( script('xcat/xnba/nodes/cn05.uefi'), qr{^shim }m, 'an xnba node names no shim' ); + +# Without an EFI stub the UEFI script chains elilo, which no key signs. +nodeset( + ipxe => 'cn06', + os => 'alma9.8', arch => 'x86_64', + kernel => 'xcat/osimage/alma9.8-noefi/vmlinuz', + initrd => 'xcat/osimage/alma9.8-noefi/initrd.img', kcmdline => 'quiet', +); +like( script('xcat/ipxe/nodes/cn06.uefi'), qr{elilo-x64\.efi}, 'a kernel without an EFI stub keeps the elilo chain' ); +unlike( script('xcat/ipxe/nodes/cn06.uefi'), qr{^shim }m, 'and names no shim' ); + +done_testing(); From 022a80f3518c638190ab82e9d1c10929e3b9bde5 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:57:07 -0300 Subject: [PATCH 02/25] feat(xcat-core): load the kernel of an ipxe node through a Secure Boot shim This PR adds a shim to the UEFI boot scripts of netboot=ipxe nodes, so a node with Secure Boot on reaches its kernel. The firmware signature database does not hold the keys of a Linux distribution. A UEFI node with netboot=ipxe loads snponly-shim.efi and snponly.efi, both signed, and then stops at the kernel with "Verification failed: Security Policy Violation". xCAT::SecureBoot::shim_url_path names a shim for one node. The shim of the install source comes first, because its vendor certificate signs the kernel of its own distribution. copycds keeps the names of the DVD, so the module tries EFI/BOOT/BOOTX64.EFI, EFI/BOOT/bootx64.efi and EFI/boot/bootx64.efi: EL and openEuler ship the first, openSUSE the second and Ubuntu the third. When no install source carries a shim, the module names the shim of ipxe-xcat, which verifies a kernel that a MOK key signs. xnba.pm writes the shim line after imgload in the UEFI script of an ipxe node, and mknb.pm writes it in the UEFI discovery scripts. iPXE fetches the shim only when it cannot load the kernel itself, so a node with Secure Boot off reads no shim. netboot=xnba is unchanged: no key signs xnba.efi. The elilo and ESXi paths and the Genesis kernel stay unsigned, and the iPXE documentation now says so. The validation matrix gets a row for a UEFI install, a netboot image and discovery with Secure Boot on. secureboot_ipxe_node_shim.t and secureboot_genesis_shim.t drive nodeset and mknb into a scratch TFTP root. Four assertions of ten and two of seven fail without this change. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- docs/source/advanced/ipxe/index.rst | 11 ++- .../code/dhcp_backend_validation_matrix.rst | 25 +++++-- perl-xCAT/xCAT/SecureBoot.pm | 69 +++++++++++++++++++ xCAT-server/lib/xcat/plugins/ipxe.pm | 4 ++ xCAT-server/lib/xcat/plugins/mknb.pm | 4 ++ xCAT-server/lib/xcat/plugins/xnba.pm | 16 ++++- xCAT-test/unit/mknb_exported_genesis.t | 1 + xCAT-test/unit/netboot_ipxe_method.t | 2 +- xCAT-test/unit/secureboot_ipxe_node_shim.t | 14 ++++ 9 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 perl-xCAT/xCAT/SecureBoot.pm diff --git a/docs/source/advanced/ipxe/index.rst b/docs/source/advanced/ipxe/index.rst index a90f0f9246..f605eabaaa 100644 --- a/docs/source/advanced/ipxe/index.rst +++ b/docs/source/advanced/ipxe/index.rst @@ -49,7 +49,16 @@ UEFI Secure Boot With Secure Boot on, the firmware loads the shim only when it trusts the Microsoft third-party UEFI CA, 2011 or 2023, that signs it. Some firmware turns that CA off by default: turn it on in the firmware setup. -Secure Boot covers the loader only. The boot scripts then load a Linux kernel, ``elilo-x64.efi`` or the Genesis kernel, and the firmware refuses each one that no key in its database signs. Install nodes and run Genesis with Secure Boot off. +The signature database of the firmware does not hold the keys of a Linux distribution, so the firmware refuses the kernel that the boot script loads. The UEFI boot scripts of ``netboot=ipxe`` nodes name a shim, and iPXE runs the kernel through it: + +* A node with an ``os`` value in its ``nodetype`` entry uses the shim of its install source, ``EFI/BOOT/BOOTX64.EFI`` or ``EFI/boot/bootx64.efi`` under ``/install//x86_64``. The vendor certificate of that shim signs the kernel of its own distribution. +* Every other node uses ``xcat/ipxe/x86_64-sb/shimx64.efi``, which ``ipxe-xcat`` installs. + +iPXE fetches a shim only when it cannot load the kernel itself, so a node with Secure Boot off reads neither file. + +A shim also accepts a kernel that a MOK key signs. Nothing signs the Genesis kernel, so a node boots Genesis with Secure Boot on only after the site signs ``xcat/genesis.kernel.x86_64`` with its own key and enrolls that key with ``mokutil``. The discovery scripts under ``xcat/ipxe/nets`` name the shim of ``ipxe-xcat`` for that purpose. + +Two paths stay Secure Boot off. A kernel without an EFI stub boots through ``elilo-x64.efi``, and ESXi boots through ``esxboot-x64.efi``. No key signs either file. Boot these nodes with Secure Boot off. Known limits of the upstream loader ----------------------------------- diff --git a/docs/source/developers/guides/code/dhcp_backend_validation_matrix.rst b/docs/source/developers/guides/code/dhcp_backend_validation_matrix.rst index 1c99229f96..d4ea26ff22 100644 --- a/docs/source/developers/guides/code/dhcp_backend_validation_matrix.rst +++ b/docs/source/developers/guides/code/dhcp_backend_validation_matrix.rst @@ -35,10 +35,11 @@ initrd, kernel, userspace, or packaging follow-up work. They must be tracked, but they do not turn the DHCP backend acceptance row red unless the failure is caused by DHCP policy, allocation, or boot option rendering. -Secure Boot is part of this matrix only for the upstream iPXE loader: the UEFI -Secure Boot row checks that the shim loads ``snponly.efi``. Every other row runs -with Secure Boot off. A Secure Boot OVMF build, such as ``OVMF_CODE.secboot.fd`` -with ``OVMF_VARS.secboot.fd``, can serve that row. +Secure Boot is part of this matrix for the upstream iPXE loader and for the +kernels it loads. Four rows run with Secure Boot on: the loader row, a UEFI +install, a netboot image and discovery. Every other row runs with Secure Boot +off. A Secure Boot OVMF build, such as ``OVMF_CODE.secboot.fd`` with +``OVMF_VARS.secboot.fd``, serves those four rows. Backend Policy -------------- @@ -248,6 +249,22 @@ statements of x86 nodes. - A UEFI node with Secure Boot on loads ``snponly-shim.efi``, the shim loads ``snponly.efi``, and iPXE fetches the node script from ``xcat/ipxe/nodes``. + * - UEFI install with Secure Boot on + - ``ISC`` or ``Kea`` + - A UEFI node with Secure Boot on and ``netboot=ipxe`` installs an + operating system. Its script under ``xcat/ipxe/nodes`` names the shim of + its install source, and the installer kernel starts. + * - Netboot image with Secure Boot on + - ``ISC`` or ``Kea`` + - A UEFI node with Secure Boot on boots a netboot osimage. The kernel of + the image carries the signature of its distribution, and the shim of the + install source verifies it. + * - Discovery with Secure Boot on + - ``ISC`` or ``Kea`` + - An unknown UEFI client with Secure Boot on starts Genesis. The site + signs ``xcat/genesis.kernel.x86_64`` and enrolls its key with + ``mokutil`` first. Without that key the shim refuses the kernel, which + is the expected result. * - Discovery - ``ISC`` or ``Kea`` - An unknown BIOS client and an unknown UEFI client load the upstream diff --git a/perl-xCAT/xCAT/SecureBoot.pm b/perl-xCAT/xCAT/SecureBoot.pm new file mode 100644 index 0000000000..3ba54f9a36 --- /dev/null +++ b/perl-xCAT/xCAT/SecureBoot.pm @@ -0,0 +1,69 @@ +package xCAT::SecureBoot; + +use strict; +use warnings; + +# The first-stage EFI binary of an install source, per architecture. copycds keeps the names that +# the DVD carries, and the distributions disagree on their case: EL and openEuler ship +# EFI/BOOT/BOOTX64.EFI, Ubuntu EFI/boot/bootx64.efi and openSUSE EFI/BOOT/bootx64.efi. +# Only x86_64 is here: the ipxe method boots x86 nodes, and the UEFI script carries a kernel that +# iPXE recognized as an x86_64 PE image. +my %INSTALL_SHIM = ( + x86_64 => [ 'EFI/BOOT/BOOTX64.EFI', 'EFI/BOOT/bootx64.efi', 'EFI/boot/bootx64.efi' ], +); + +# The shim of the ipxe-xcat package. Every shim reads the MOK list from the firmware, so this one +# verifies a kernel that the site signs with a key it enrolls. +my %LOADER_SHIM = ( + x86_64 => 'xcat/ipxe/x86_64-sb/shimx64.efi', +); + +#------------------------------------------------------------------------------- + +=head3 shim_url_path + + Descriptions: + The HTTP path of a shim that verifies the kernel of one node. The shim of the install + source comes first, because its vendor certificate signs the kernel of its own + distribution. The shim of ipxe-xcat is the answer for a kernel that the site signs + itself, such as the Genesis kernel. + + iPXE fetches the shim only when it cannot load the kernel directly, so a node with + Secure Boot off never reads the file. + + Arguments: + arch - the architecture of the node, as the nodetype table spells it + os - the operating system of the node, or undef for an unknown client + installroot - the install directory, /install by default + tftpdir - the TFTP directory of the node, /tftpboot by default + + Returns: + The path under the document root of the xCAT web server, or undef when no shim is + available. xcat.conf serves /install and /tftpboot at those names. + +=cut + +#------------------------------------------------------------------------------- +sub shim_url_path { + my ($class, %opts) = @_; + + my $arch = $opts{arch}; + return unless defined($arch) and $INSTALL_SHIM{$arch}; + + if (defined($opts{os}) and length($opts{os})) { + my $installroot = defined($opts{installroot}) ? $opts{installroot} : '/install'; + $installroot =~ s{/+$}{}; + foreach my $name (@{ $INSTALL_SHIM{$arch} }) { + return "/install/$opts{os}/$arch/$name" + if -f "$installroot/$opts{os}/$arch/$name"; + } + } + + my $tftpdir = defined($opts{tftpdir}) ? $opts{tftpdir} : '/tftpboot'; + $tftpdir =~ s{/+$}{}; + my $loader = $LOADER_SHIM{$arch}; + return "/tftpboot/$loader" if -f "$tftpdir/$loader"; + return; +} + +1; diff --git a/xCAT-server/lib/xcat/plugins/ipxe.pm b/xCAT-server/lib/xcat/plugins/ipxe.pm index d50e47ff23..070561c472 100644 --- a/xCAT-server/lib/xcat/plugins/ipxe.pm +++ b/xCAT-server/lib/xcat/plugins/ipxe.pm @@ -19,24 +19,28 @@ sub _xnba { sub preprocess_request { _xnba(); local $xCAT_plugin::xnba::SCRIPTS = 'xcat/ipxe'; + local $xCAT_plugin::xnba::METHOD = 'ipxe'; return xCAT_plugin::xnba::preprocess_request(@_); } sub process_request { _xnba(); local $xCAT_plugin::xnba::SCRIPTS = 'xcat/ipxe'; + local $xCAT_plugin::xnba::METHOD = 'ipxe'; return xCAT_plugin::xnba::process_request(@_); } sub getstate { _xnba(); local $xCAT_plugin::xnba::SCRIPTS = 'xcat/ipxe'; + local $xCAT_plugin::xnba::METHOD = 'ipxe'; return xCAT_plugin::xnba::getstate(@_); } sub getNodesetStates { _xnba(); local $xCAT_plugin::xnba::SCRIPTS = 'xcat/ipxe'; + local $xCAT_plugin::xnba::METHOD = 'ipxe'; return xCAT_plugin::xnba::getNodesetStates(@_); } diff --git a/xCAT-server/lib/xcat/plugins/mknb.pm b/xCAT-server/lib/xcat/plugins/mknb.pm index 38b75d1f28..95a302bc0e 100644 --- a/xCAT-server/lib/xcat/plugins/mknb.pm +++ b/xCAT-server/lib/xcat/plugins/mknb.pm @@ -5,6 +5,7 @@ use File::Temp qw(tempdir tempfile); use xCAT::Utils; use xCAT::TableUtils; use xCAT::NodeRange; +use xCAT::SecureBoot; use File::Path; use File::Copy; use English qw(-no_match_vars); @@ -859,6 +860,9 @@ sub process_request { open($cfg, ">", "$nets/$net.uefi"); print $cfg "#!gpxe\n"; print $cfg 'imgfetch -n kernel http://${next-server}'.$portsuffix.'/tftpboot/xcat/genesis.kernel.' . "$arch\nimgload kernel\n"; + # The shim command reads the selected image, so it comes after imgload. + my $shim = xCAT::SecureBoot->shim_url_path(arch => $arch, tftpdir => $tftpdir); + print $cfg 'shim http://${next-server}' . $portsuffix . $shim . "\n" if ($shim); print $cfg "imgargs kernel xcatd=" . $xcatd_address . ":$xcatdport $consolecmdline BOOTIF=01-" . '${netX/mac:hexhyp}' . " destiny=discover initrd=initrd\n"; print $cfg 'imgfetch -n initrd http://${next-server}'.$portsuffix . "$initrd_file\nimgexec kernel\n"; close($cfg); diff --git a/xCAT-server/lib/xcat/plugins/xnba.pm b/xCAT-server/lib/xcat/plugins/xnba.pm index 96e4ac21a6..245d1b06f8 100644 --- a/xCAT-server/lib/xcat/plugins/xnba.pm +++ b/xCAT-server/lib/xcat/plugins/xnba.pm @@ -13,6 +13,7 @@ require xCAT::BootUtils; use xCAT::TableUtils; use xCAT::ServiceNodeUtils; use xCAT::Usage; +use xCAT::SecureBoot; my $dhcpconf = "/etc/dhcpd.conf"; @@ -22,6 +23,9 @@ my $globaltftpdir = xCAT::TableUtils->getTftpDir(); # The directory of the boot scripts under the TFTP root. The ipxe method sets it to xcat/ipxe. our $SCRIPTS = 'xcat/xnba'; +# Only the loader of the ipxe method is signed, so only its nodes reach a kernel with Secure Boot on. +our $METHOD = 'xnba'; + #my $dhcpver = 3; my %usage = ( @@ -154,6 +158,7 @@ sub setstate { my %iscsihash = %{ shift() }; my $tftpdir = shift; my $linuximghashref = shift; + my $nodetyperef = shift || {}; my $kern = $bphash{$node}->[0]; my $imgaddkcmdline = $linuximghashref->{'addkcmdline'}; @@ -357,6 +362,13 @@ sub setstate { print $ucfg "#!gpxe\n"; print $ucfg "imgfetch -n kernel http://" . '${next-server}' . $portsuffix.'/tftpboot/' . $kern->{kernel} . "\n"; print $ucfg "imgload kernel\n"; + # The shim command reads the selected image, so it comes after imgload. + my $shim = ($METHOD eq 'ipxe') ? xCAT::SecureBoot->shim_url_path( + arch => $nodetyperef->{arch}, + os => $nodetyperef->{os}, + installroot => xCAT::TableUtils->getInstallDir(), + tftpdir => $tftpdir) : undef; + print $ucfg "shim http://" . '${next-server}' . $portsuffix . $shim . "\n" if ($shim); if ($kern->{kcmdline}) { print $ucfg "imgargs kernel " . $kern->{kcmdline} . ' BOOTIF=01-${netX/mac:hexhyp} initrd=initrd' . "\n"; } else { @@ -799,7 +811,7 @@ sub process_request { %iscsihash = %{ $iscsitab->getNodesAttribs(\@nodes, [qw(server target)]) }; } my $typetab = xCAT::Table->new('nodetype', -create => 1); - my $typehash = $typetab->getNodesAttribs(\@nodes, ['provmethod']); + my $typehash = $typetab->getNodesAttribs(\@nodes, [ 'provmethod', 'os', 'arch' ]); my $linuximgtab = xCAT::Table->new('linuximage', -create => 1); my @normalnodeset = (); @@ -822,7 +834,7 @@ sub process_request { unless ($osimgname =~ /^(install|netboot|statelite)$/) { $linuximghash = $linuximgtab->getAttribs({ imagename => $osimgname }, 'boottarget', 'addkcmdline'); } - ($rc, $errstr) = setstate($_, \%bphash, \%chainhash, \%machash, \%iscsihash, $tftpdir, $linuximghash); + ($rc, $errstr) = setstate($_, \%bphash, \%chainhash, \%machash, \%iscsihash, $tftpdir, $linuximghash, $ent); if ($rc) { $response{node}->[0]->{errorcode}->[0] = $rc; $response{node}->[0]->{error}->[0] = $errstr; diff --git a/xCAT-test/unit/mknb_exported_genesis.t b/xCAT-test/unit/mknb_exported_genesis.t index 3fd26df00c..f4b89d5922 100644 --- a/xCAT-test/unit/mknb_exported_genesis.t +++ b/xCAT-test/unit/mknb_exported_genesis.t @@ -7,6 +7,7 @@ use Digest::SHA qw(sha256_hex); use File::Path qw(make_path remove_tree); use File::Temp qw(tempdir); use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; use Test::More; BEGIN { diff --git a/xCAT-test/unit/netboot_ipxe_method.t b/xCAT-test/unit/netboot_ipxe_method.t index 1687797724..e3d2e6daa3 100644 --- a/xCAT-test/unit/netboot_ipxe_method.t +++ b/xCAT-test/unit/netboot_ipxe_method.t @@ -48,7 +48,7 @@ BEGIN { # the plugin directory of this checkout. my $inc = tempdir( CLEANUP => 1 ); symlink( "$repo/xCAT-server/lib/xcat/plugins", "$inc/xCAT_plugin" ) or die "symlink: $!"; -unshift @INC, $inc, "$repo/xCAT-server/lib/perl"; +unshift @INC, $inc, "$repo/xCAT-server/lib/perl", "$repo/perl-xCAT"; require xCAT_plugin::ipxe; require xCAT::SvrUtils; diff --git a/xCAT-test/unit/secureboot_ipxe_node_shim.t b/xCAT-test/unit/secureboot_ipxe_node_shim.t index e8899d569e..f775346980 100644 --- a/xCAT-test/unit/secureboot_ipxe_node_shim.t +++ b/xCAT-test/unit/secureboot_ipxe_node_shim.t @@ -149,6 +149,20 @@ like( 'a node whose install source carries no shim falls back to the shim of ipxe-xcat' ); +# The vendor certificate of the distribution shim signs the kernel of that distribution, so the +# install source answers before the shim of ipxe-xcat. +nodeset( + ipxe => 'cn07', + os => 'alma9.8', arch => 'x86_64', + kernel => kernel_with_efistub('xcat/osimage/alma9.8-again/vmlinuz'), + initrd => 'xcat/osimage/alma9.8-again/initrd.img', kcmdline => 'quiet', +); +like( + script('xcat/ipxe/nodes/cn07.uefi'), + ipxe_shim_line('/install/alma9.8/x86_64/EFI/BOOT/BOOTX64.EFI'), + 'the install source answers even when the shim of ipxe-xcat is also on disk' +); + # ppc64le has no UEFI shim, and netboot=xnba loads an unsigned loader that Secure Boot refuses. nodeset( ipxe => 'cn04', From 24701cc23e0d9275561568251d549f1272bfcde6 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 13:59:11 -0300 Subject: [PATCH 03/25] test(xcat-core): measure the shim line in the UEFI script of an ipxe node The unit tests in this branch call the plugins in their own process against a scratch TFTP root. No xcattest case reads a script that nodeset wrote on a management node, so a cell whose compute node has netboot=ipxe runs this code and asserts nothing about it. el9-x86_64-devel is such a cell. nodeset_ipxe_secureboot_shim runs nodeset against the compute node of the conf, reads /tftpboot/xcat/ipxe/nodes/.uefi, and reports whether the line after imgload names a shim that the web server serves. It puts the nodeset state back afterwards. A node that is not an x86_64 ipxe node prints IPXE_SHIM_SKIPPED and passes, so a bundle can carry the case on every cell. Where no provisioning case has set nodetype.provmethod, the case names the stateful image of the node. Measured on an AlmaLinux 9.8 management node: with the plugins of this branch the case reports the shim of the install source and passes. With xnba.pm and ipxe.pm from the branch point it reports imgargs kernel as the line after imgload and fails. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../testcase/nodeset/ipxe_secureboot_shim.sh | 119 ++++++++++++++++++ .../nodeset/nodeset_ipxe_secureboot_shim | 7 ++ 2 files changed, 126 insertions(+) create mode 100755 xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh create mode 100644 xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim diff --git a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh new file mode 100755 index 0000000000..346262a989 --- /dev/null +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -0,0 +1,119 @@ +#!/bin/bash +# Read back the UEFI boot script that nodeset writes for one netboot=ipxe node, and report +# whether it names a Secure Boot shim for the kernel it loads. +# +# Usage: ipxe_secureboot_shim.sh +# +# The caller asserts on one of three tokens, so no error message can carry the token of a pass: +# IPXE_SHIM_OK the script names a shim after imgload, and the file is served +# IPXE_SHIM_SKIPPED the node is not an x86_64 netboot=ipxe node +# IPXE_SHIM_FAIL the shim is missing, out of order, or not served + +node="$1" +if [ -z "$node" ]; then + echo "IPXE_SHIM_FAIL: no node given" + exit 1 +fi + +site_value() { + lsdef -t site -i "$1" 2>/dev/null | sed -n "s/^[[:space:]]*$1=//p" +} +node_value() { + lsdef -t node -o "$node" -i "$1" 2>/dev/null | sed -n "s/^[[:space:]]*$1=//p" +} + +netboot=$(node_value netboot) +arch=$(node_value arch) +if [ "$netboot" != "ipxe" ]; then + echo "IPXE_SHIM_SKIPPED: $node has netboot=$netboot, and only the ipxe method names a shim" + exit 0 +fi +if [ "$arch" != "x86_64" ]; then + echo "IPXE_SHIM_SKIPPED: $node has arch=$arch, and only x86_64 has a UEFI shim" + exit 0 +fi + +tftpdir=$(site_value tftpdir) +[ -n "$tftpdir" ] || tftpdir=/tftpboot +installdir=$(site_value installdir) +[ -n "$installdir" ] || installdir=/install +uefi="$tftpdir/xcat/ipxe/nodes/$node.uefi" + +before=$(nodeset "$node" stat 2>/dev/null | sed -n "s/^$node: *//p" | head -1) +provmethod=$(node_value provmethod) +echo "nodeset state of $node before this case: ${before:-unknown}, provmethod=${provmethod:-unset}" + +restore_state() { + if [ -z "$provmethod" ]; then + chdef -t node -o "$node" provmethod= > /dev/null + fi + case "${before%% *}" in + boot | offline | shell | standby) + nodeset "$node" "${before%% *}" || echo "could not put $node back to ${before%% *}" + ;; + esac +} + +# A hand run reaches this case before any provisioning case has set nodetype.provmethod, so name +# the stateful image of the node here. In a bundle the provisioning cases have already set it. +destiny=osimage +if [ -z "$provmethod" ]; then + image="$(node_value os)-$arch-install-compute" + if ! lsdef -t osimage -o "$image" > /dev/null 2>&1; then + echo "IPXE_SHIM_SKIPPED: $node has no provmethod and osimage $image is not defined" + exit 0 + fi + destiny="osimage=$image" +fi + +if ! nodeset "$node" "$destiny"; then + echo "IPXE_SHIM_FAIL: nodeset $node $destiny returned non-zero" + restore_state + exit 1 +fi +if [ ! -f "$uefi" ]; then + echo "IPXE_SHIM_FAIL: nodeset wrote no $uefi" + restore_state + exit 1 +fi +echo "--- $uefi ---" +cat "$uefi" +echo "--- end ---" + +# iPXE loads a kernel that carries an EFI stub with imgload. The shim command reads the image +# that imgload selected, so the shim line must follow it. +if ! grep -q '^imgload kernel$' "$uefi"; then + echo "IPXE_SHIM_FAIL: the UEFI script of $node selects no kernel image with imgload" + restore_state + exit 1 +fi +after=$(awk '/^imgload kernel$/ { if ((getline line) > 0) { print line } exit }' "$uefi") +case "$after" in + "shim http://"*) ;; + *) + echo "IPXE_SHIM_FAIL: the line after imgload is [$after], not a shim command" + restore_state + exit 1 + ;; +esac + +# iPXE fetches the shim over HTTP, so the path has to resolve to a file the web server serves. +urlpath=$(printf '%s\n' "$after" | sed -n 's|^shim http://[^/]*\(/.*\)$|\1|p') +case "$urlpath" in + /install/*) shimfile="$installdir/${urlpath#/install/}" ;; + /tftpboot/*) shimfile="$tftpdir/${urlpath#/tftpboot/}" ;; + *) + echo "IPXE_SHIM_FAIL: the shim path [$urlpath] is under neither /install nor /tftpboot" + restore_state + exit 1 + ;; +esac +if [ ! -f "$shimfile" ]; then + echo "IPXE_SHIM_FAIL: the script names $urlpath and $shimfile does not exist" + restore_state + exit 1 +fi + +restore_state +echo "IPXE_SHIM_OK: $after resolves to $shimfile" +exit 0 diff --git a/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim b/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim new file mode 100644 index 0000000000..3a81f4c361 --- /dev/null +++ b/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim @@ -0,0 +1,7 @@ +start:nodeset_ipxe_secureboot_shim +label:others,nodeset +description:verify the UEFI boot script of an ipxe node names a Secure Boot shim after imgload +cmd:/opt/xcat/share/xcat/tools/autotest/testcase/nodeset/ipxe_secureboot_shim.sh $$CN +check:rc==0 +check:output=~IPXE_SHIM_(OK|SKIPPED) +end From ebcc2e1d0e94080d9b046b0937bdfd238d59a0bd Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 14:49:28 -0300 Subject: [PATCH 04/25] test(xcat-core): capture the install source shim in the Genesis boot script The shell, discover, standby and runcmd destinies load the Genesis kernel through the same UEFI script as an osimage kernel. setstate asks for a shim with the os of the node, so an install source answers for a kernel that no distribution signed. The new node keeps the install source of the first node of the test, so both shims are on disk and the order decides the answer. Two assertions of twelve fail: the script names /install/alma9.8/x86_64/EFI/BOOT/BOOTX64.EFI. The nodeset helper takes currstate and provmethod, so the node reads as the shell destiny it is. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-test/unit/secureboot_ipxe_node_shim.t | 30 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/xCAT-test/unit/secureboot_ipxe_node_shim.t b/xCAT-test/unit/secureboot_ipxe_node_shim.t index f775346980..4c1ded8412 100644 --- a/xCAT-test/unit/secureboot_ipxe_node_shim.t +++ b/xCAT-test/unit/secureboot_ipxe_node_shim.t @@ -77,9 +77,13 @@ sub install_source { sub nodeset { my ( $plugin, $node, %attrib ) = @_; $rows{noderes}{$node} = { tftpdir => $tftp, netboot => $plugin }; - $rows{chain}{$node} = { currstate => 'install rhels9-x86_64-compute' }; - $rows{nodetype}{$node} = - { provmethod => 'install', os => $attrib{os}, arch => $attrib{arch} }; + $rows{chain}{$node} = + { currstate => $attrib{currstate} || 'install rhels9-x86_64-compute' }; + $rows{nodetype}{$node} = { + provmethod => exists( $attrib{provmethod} ) ? $attrib{provmethod} : 'install', + os => $attrib{os}, + arch => $attrib{arch}, + }; my $subreq = sub { my ($request) = @_; $request->{bootparams}{$node} = [ { map { $_ => $attrib{$_} } qw(kernel initrd kcmdline) } ] @@ -163,6 +167,26 @@ like( 'the install source answers even when the shim of ipxe-xcat is also on disk' ); +# The shell, discover, standby and runcmd destinies load the Genesis kernel through the same UEFI +# script. No distribution signs that kernel, so the vendor certificate of an install source cannot +# verify it and only the shim of ipxe-xcat can. cn08 keeps the install source of cn01, which does +# carry a shim. +nodeset( + ipxe => 'cn08', + os => 'alma9.8', arch => 'x86_64', + currstate => 'shell', + provmethod => '', + kernel => kernel_with_efistub('xcat/genesis.kernel.x86_64'), + initrd => 'xcat/genesis.fs.x86_64.gz', kcmdline => 'quiet destiny=shell', +); +like( + script('xcat/ipxe/nodes/cn08.uefi'), + ipxe_shim_line('/tftpboot/xcat/ipxe/x86_64-sb/shimx64.efi'), + 'a node that loads the Genesis kernel names the shim of ipxe-xcat' +); +unlike( script('xcat/ipxe/nodes/cn08.uefi'), qr{^shim \S*/install/}m, + 'and never the shim of an install source, which signs no Genesis kernel' ); + # ppc64le has no UEFI shim, and netboot=xnba loads an unsigned loader that Secure Boot refuses. nodeset( ipxe => 'cn04', From c10c30d34277024f4f0f09aab126e9d6ab3e8d4f Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 14:52:03 -0300 Subject: [PATCH 05/25] fix(xcat-core): verify the Genesis kernel with the shim of ipxe-xcat setstate writes the Genesis kernel into the UEFI script of a node for the shell, discover, standby and runcmd destinies. It asked for a shim with the os of the node, so a management node that ran copycds named the shim of that install source. That shim trusts the vendor certificate of the distribution, which signs no Genesis kernel, and the node stops at the same Security Policy Violation. shim_url_path takes the kernel of the boot script. A Genesis kernel skips the install source and takes the shim of ipxe-xcat, which reads the MOK list. An osimage kernel keeps the install source first. mknb already named the loader shim, because it passes no os. Two assertions of twelve in secureboot_ipxe_node_shim.t fail without this change. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- perl-xCAT/xCAT/SecureBoot.pm | 13 ++++++++++++- xCAT-server/lib/xcat/plugins/xnba.pm | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/perl-xCAT/xCAT/SecureBoot.pm b/perl-xCAT/xCAT/SecureBoot.pm index 3ba54f9a36..bebdc81ec9 100644 --- a/perl-xCAT/xCAT/SecureBoot.pm +++ b/perl-xCAT/xCAT/SecureBoot.pm @@ -18,6 +18,10 @@ my %LOADER_SHIM = ( x86_64 => 'xcat/ipxe/x86_64-sb/shimx64.efi', ); +# The kernel of the Genesis image, as the boot scripts name it. mknb builds it and the site signs +# it, or leaves it unsigned, so no vendor certificate of a distribution verifies it. +my $GENESIS_KERNEL = qr{(?:\A|/)genesis\.kernel\.}; + #------------------------------------------------------------------------------- =head3 shim_url_path @@ -34,6 +38,8 @@ my %LOADER_SHIM = ( Arguments: arch - the architecture of the node, as the nodetype table spells it os - the operating system of the node, or undef for an unknown client + kernel - the kernel the boot script loads, relative to the TFTP root. A Genesis + kernel takes the shim of ipxe-xcat whatever the os of the node is. installroot - the install directory, /install by default tftpdir - the TFTP directory of the node, /tftpboot by default @@ -50,7 +56,12 @@ sub shim_url_path { my $arch = $opts{arch}; return unless defined($arch) and $INSTALL_SHIM{$arch}; - if (defined($opts{os}) and length($opts{os})) { + # nodeset writes the Genesis kernel into the UEFI script of a node for the shell, discover, + # standby and runcmd destinies, beside the install source of the os of that node. The vendor + # certificate of that install source signs its own kernel and not this one. + my $genesis = defined($opts{kernel}) && $opts{kernel} =~ $GENESIS_KERNEL; + + if (!$genesis and defined($opts{os}) and length($opts{os})) { my $installroot = defined($opts{installroot}) ? $opts{installroot} : '/install'; $installroot =~ s{/+$}{}; foreach my $name (@{ $INSTALL_SHIM{$arch} }) { diff --git a/xCAT-server/lib/xcat/plugins/xnba.pm b/xCAT-server/lib/xcat/plugins/xnba.pm index 245d1b06f8..7bf677d5e1 100644 --- a/xCAT-server/lib/xcat/plugins/xnba.pm +++ b/xCAT-server/lib/xcat/plugins/xnba.pm @@ -366,6 +366,7 @@ sub setstate { my $shim = ($METHOD eq 'ipxe') ? xCAT::SecureBoot->shim_url_path( arch => $nodetyperef->{arch}, os => $nodetyperef->{os}, + kernel => $kern->{kernel}, installroot => xCAT::TableUtils->getInstallDir(), tftpdir => $tftpdir) : undef; print $ucfg "shim http://" . '${next-server}' . $portsuffix . $shim . "\n" if ($shim); From 7440ed33c9f34d6e472beacb7a28868850d85f66 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 15:08:20 -0300 Subject: [PATCH 06/25] test(xcat-core): keep the shim out of the xNBA discovery script mknb writes the UEFI discovery scripts of both netboot methods in one loop, over xcat/ipxe/nets and xcat/xnba/nets. The shim line reaches both. The loader of xnba does not know the shim command, so the line stops a legacy xNBA discovery even with Secure Boot off. secureboot_genesis_shim.t now reads the xnba script of the same run that reads the ipxe one. One assertion of nine fails: the xnba UEFI script names /tftpboot/xcat/ipxe/x86_64-sb/shimx64.efi. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-test/unit/secureboot_genesis_shim.t | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xCAT-test/unit/secureboot_genesis_shim.t b/xCAT-test/unit/secureboot_genesis_shim.t index 111eeebff1..aad0cc20a2 100644 --- a/xCAT-test/unit/secureboot_genesis_shim.t +++ b/xCAT-test/unit/secureboot_genesis_shim.t @@ -96,6 +96,13 @@ unlike( script('xcat/ipxe/nets/192.168.144.0_20'), qr{^shim }m, unlike( script('xcat/ipxe/nets/192.168.144.0_20.elilo'), qr{^shim }m, 'the elilo configuration names no shim' ); +# mknb writes the discovery scripts of both methods in one loop. The loader of xnba does not know +# the shim command, so the line stops that script whether Secure Boot is on or off. +like( script('xcat/xnba/nets/192.168.144.0_20.uefi'), qr{^imgload kernel$}m, + 'mknb writes the UEFI discovery script of the xnba method as well' ); +unlike( script('xcat/xnba/nets/192.168.144.0_20.uefi'), qr{^shim }m, + 'and that script names no shim, while the shim of ipxe-xcat is on disk' ); + prepare( name => 'tftpboot-noshim', shim => 0 ); unlike( script('xcat/ipxe/nets/192.168.144.0_20.uefi'), qr{^shim }m, 'a server without the ipxe-xcat shim names no shim' ); From e41ac50f12512b4c6991a9e0b1f15f944e4fab0d Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 15:08:26 -0300 Subject: [PATCH 07/25] fix(xcat-core): name the shim only in the iPXE discovery script process_request walks xcat/ipxe/nets and xcat/xnba/nets in one loop and wrote the shim line into the UEFI script of both. netboot=xnba is out of scope: no key signs xnba.efi, and its loader does not know the shim command, so the line stops the discovery of a node that never asked for Secure Boot. The loop now carries the method name beside the directory, and the shim line goes only into the script of the ipxe method. xnba.pm already gates the node scripts the same way. One assertion of nine in secureboot_genesis_shim.t fails without this change. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-server/lib/xcat/plugins/mknb.pm | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/mknb.pm b/xCAT-server/lib/xcat/plugins/mknb.pm index 95a302bc0e..5ec18144ff 100644 --- a/xCAT-server/lib/xcat/plugins/mknb.pm +++ b/xCAT-server/lib/xcat/plugins/mknb.pm @@ -761,7 +761,7 @@ sub process_request { my $cfgfile; # The upstream iPXE loader of an unknown x86 client fetches the discovery scripts from xcat/ipxe. # xcat/xnba keeps them for a server whose DHCP configuration still names it. - my @x86_nets = map { "$tftpdir/xcat/$_/nets" } qw(ipxe xnba); + my @x86_nets = map { { method => $_, dir => "$tftpdir/xcat/$_/nets" } } qw(ipxe xnba); if ($arch =~ /x86/) { mkpath("$tftpdir/xcat/xnba/nets"); chmod(0755, "$tftpdir/xcat/xnba"); @@ -816,7 +816,9 @@ sub process_request { next; } if ($arch =~ /x86/) { - foreach my $nets (@x86_nets) { + foreach my $netsent (@x86_nets) { + my $method = $netsent->{method}; + my $nets = $netsent->{dir}; $dopxe = 0; if ($arch =~ /x86_64/ and not $invisibletouch) { #only do pxe if just x86 or x86_64 and no x86 if (-r "$nets/$net") { @@ -861,7 +863,10 @@ sub process_request { print $cfg "#!gpxe\n"; print $cfg 'imgfetch -n kernel http://${next-server}'.$portsuffix.'/tftpboot/xcat/genesis.kernel.' . "$arch\nimgload kernel\n"; # The shim command reads the selected image, so it comes after imgload. - my $shim = xCAT::SecureBoot->shim_url_path(arch => $arch, tftpdir => $tftpdir); + # The loader of xnba does not know the shim command, and no key signs xnba.efi. + my $shim = ($method eq 'ipxe') + ? xCAT::SecureBoot->shim_url_path(arch => $arch, tftpdir => $tftpdir) + : undef; print $cfg 'shim http://${next-server}' . $portsuffix . $shim . "\n" if ($shim); print $cfg "imgargs kernel xcatd=" . $xcatd_address . ":$xcatdport $consolecmdline BOOTIF=01-" . '${netX/mac:hexhyp}' . " destiny=discover initrd=initrd\n"; print $cfg 'imgfetch -n initrd http://${next-server}'.$portsuffix . "$initrd_file\nimgexec kernel\n"; From 30cc10424b17912dd9d442bfa584e763b5897f17 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 15:08:38 -0300 Subject: [PATCH 08/25] test(xcat-core): capture a silent restoration failure in the ipxe shim case ipxe_secureboot_shim.sh calls nodeset to reconfigure one node for installation, reads the UEFI script, and then puts the nodeset state back. restore_state ignores the exit status of chdef and nodeset, so the case prints IPXE_SHIM_OK and returns 0 while the node is still configured for installation. The next case on that node measures the state this one left. The test drives the helper with lsdef, nodeset and chdef stubbed, and records every call. Two assertions of five fail: with either restoration command returning non-zero the helper exits 0 and prints IPXE_SHIM_OK. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../bats/ipxe_secureboot_shim_restore.bats | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 xCAT-test/bats/ipxe_secureboot_shim_restore.bats diff --git a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats new file mode 100644 index 0000000000..d28d17c620 --- /dev/null +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -0,0 +1,148 @@ +#!/usr/bin/env bats +# +# Drive ipxe_secureboot_shim.sh, the helper of the nodeset_ipxe_secureboot_shim case. The case +# calls nodeset to reconfigure one node for installation, reads the UEFI script, and puts the +# nodeset state back. A restoration that fails leaves the node configured for installation, and +# the next case on that node measures the state this one left. So the helper has to report a +# failed restoration instead of its own result. +# +# lsdef, nodeset and chdef are stubbed, and every call is recorded in a log the tests read. + +load 'helpers/shell_source' + +NODE=cn01 +OS=alma9.8 + +setup() +{ + SCRIPT="$(require_repo_file 'xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh')" + BIN="${BATS_TEST_TMPDIR}/bin" + TFTPDIR="${BATS_TEST_TMPDIR}/tftpboot" + INSTALLDIR="${BATS_TEST_TMPDIR}/install" + CALLS="${BATS_TEST_TMPDIR}/calls" + mkdir -p "$BIN" "$TFTPDIR/xcat/ipxe/nodes" "$INSTALLDIR/$OS/x86_64/EFI/BOOT" + : >"$CALLS" + # The shim the boot script names. The helper fails when the file is absent. + printf 'shim\n' >"$INSTALLDIR/$OS/x86_64/EFI/BOOT/BOOTX64.EFI" + + # The nodeset state the helper has to put back, and the provmethod it finds. An empty + # provmethod is what sends the helper through chdef on the way out. + export STUB_BEFORE=boot + export STUB_PROVMETHOD= + export STUB_NODESET_RESTORE_RC=0 + export STUB_CHDEF_RC=0 + export STUB_NO_OSIMAGE= + export STUB_UEFI="$TFTPDIR/xcat/ipxe/nodes/${NODE}.uefi" + export STUB_SHIM_LINE="shim http://\${next-server}/install/$OS/x86_64/EFI/BOOT/BOOTX64.EFI" + export STUB_CALLS="$CALLS" + export STUB_TFTPDIR="$TFTPDIR" + export STUB_INSTALLDIR="$INSTALLDIR" + export STUB_OS="$OS" + + cat >"$BIN/lsdef" <<'STUB' +#!/bin/bash +echo "lsdef $*" >>"$STUB_CALLS" +type=; obj=; attr= +while [ $# -gt 0 ]; do + case "$1" in + -t) type="$2"; shift 2 ;; + -o) obj="$2"; shift 2 ;; + -i) attr="$2"; shift 2 ;; + *) shift ;; + esac +done +case "$type/$attr" in + site/tftpdir) echo " tftpdir=$STUB_TFTPDIR" ;; + site/installdir) echo " installdir=$STUB_INSTALLDIR" ;; + node/netboot) echo " netboot=ipxe" ;; + node/arch) echo " arch=x86_64" ;; + node/os) echo " os=$STUB_OS" ;; + node/provmethod) [ -n "$STUB_PROVMETHOD" ] && echo " provmethod=$STUB_PROVMETHOD" ;; + osimage/) [ -n "$STUB_NO_OSIMAGE" ] && exit 1; exit 0 ;; + *) echo "unexpected lsdef $type $obj $attr" >&2; exit 1 ;; +esac +exit 0 +STUB + + # nodeset stat reports the state; osimage= writes the UEFI script; any destiny name is the + # restoration call, and its exit status is what these tests drive. + cat >"$BIN/nodeset" <<'STUB' +#!/bin/bash +echo "nodeset $*" >>"$STUB_CALLS" +case "$2" in + stat) + echo "$1: $STUB_BEFORE" + ;; + osimage=*) + { + echo '#!gpxe' + echo 'imgfetch -n kernel http://${next-server}/tftpboot/xcat/osimage/vmlinuz' + echo 'imgload kernel' + echo "$STUB_SHIM_LINE" + echo 'imgargs kernel quiet' + echo 'imgexec kernel' + } >"$STUB_UEFI" + ;; + *) + exit "$STUB_NODESET_RESTORE_RC" + ;; +esac +exit 0 +STUB + + cat >"$BIN/chdef" <<'STUB' +#!/bin/bash +echo "chdef $*" >>"$STUB_CALLS" +exit "$STUB_CHDEF_RC" +STUB + + chmod 0755 "$BIN"/* + export PATH="$BIN:$PATH" +} + +@test "a node whose state goes back reports the shim it found" { + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + grep -q "^nodeset $NODE boot$" "$CALLS" + grep -q "^chdef -t node -o $NODE provmethod=$" "$CALLS" +} + +@test "a nodeset that cannot put the state back fails the case" { + STUB_NODESET_RESTORE_RC=1 + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *IPXE_SHIM_FAIL* ]] + grep -q "^nodeset $NODE boot$" "$CALLS" +} + +@test "a chdef that cannot clear the provmethod fails the case" { + STUB_CHDEF_RC=1 + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *IPXE_SHIM_FAIL* ]] + grep -q "^chdef -t node -o $NODE provmethod=$" "$CALLS" +} + +# A control: the helper already restores the state when its own assertion fails. It holds the +# restoration on the exit path down, so a later exit cannot skip it. +@test "a script without a shim after imgload still puts the state back" { + STUB_SHIM_LINE='imgargs kernel quiet' + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" == *IPXE_SHIM_FAIL* ]] + grep -q "^nodeset $NODE boot$" "$CALLS" +} + +# A skip above the nodeset call changed nothing, so the helper must not call nodeset to put a +# state back it never left. An osimage that is not defined is such a skip. +@test "a node the helper skips calls no nodeset and no chdef" { + STUB_NO_OSIMAGE=1 + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_SKIPPED* ]] + refute_grep -q "^nodeset $NODE boot$" "$CALLS" + refute_grep -q "^chdef " "$CALLS" +} From 325504962af8eae9f20aa1e755bc2912fa71909d Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 15:08:45 -0300 Subject: [PATCH 09/25] fix(xcat-core): fail the ipxe shim case when the node stays unrestored restore_state dropped the exit status of chdef and nodeset, and the helper printed IPXE_SHIM_OK after it. A node left with provmethod set and nodeset osimage reads as a pass, and the next case on that node runs against the state this one set. restore_state now reports which command failed and returns non-zero, and the helper exits non-zero in place of the pass token. An EXIT trap holds the restoration, armed immediately before the nodeset call that changes the state, so no later branch can leave the node behind. A skip above that line changes nothing and calls neither command. Two assertions of five in ipxe_secureboot_shim_restore.bats fail without this change. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../testcase/nodeset/ipxe_secureboot_shim.sh | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh index 346262a989..148303fad8 100755 --- a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -43,15 +43,34 @@ before=$(nodeset "$node" stat 2>/dev/null | sed -n "s/^$node: *//p" | head -1) provmethod=$(node_value provmethod) echo "nodeset state of $node before this case: ${before:-unknown}, provmethod=${provmethod:-unset}" +# The case reconfigures the node for installation. A restoration that fails leaves it that way, +# so the failure has to reach the caller in place of the result of the case. +restore_done=0 +restore_rc=0 restore_state() { + [ "$restore_done" = 1 ] && return "$restore_rc" + restore_done=1 if [ -z "$provmethod" ]; then - chdef -t node -o "$node" provmethod= > /dev/null + if ! chdef -t node -o "$node" provmethod= > /dev/null; then + echo "IPXE_SHIM_FAIL: chdef -t node -o $node provmethod= returned non-zero, $node keeps the provmethod this case set" + restore_rc=1 + fi fi case "${before%% *}" in boot | offline | shell | standby) - nodeset "$node" "${before%% *}" || echo "could not put $node back to ${before%% *}" + if ! nodeset "$node" "${before%% *}"; then + echo "IPXE_SHIM_FAIL: nodeset $node ${before%% *} returned non-zero, $node stays configured for installation" + restore_rc=1 + fi ;; esac + return "$restore_rc" +} + +on_exit() { + status=$? + restore_state || status=1 + exit "$status" } # A hand run reaches this case before any provisioning case has set nodetype.provmethod, so name @@ -66,14 +85,16 @@ if [ -z "$provmethod" ]; then destiny="osimage=$image" fi +# Arm the restoration before the call that changes the state, and not earlier: a skip above this +# line has nothing to put back. Every exit after it passes through the restoration. +trap on_exit EXIT + if ! nodeset "$node" "$destiny"; then echo "IPXE_SHIM_FAIL: nodeset $node $destiny returned non-zero" - restore_state exit 1 fi if [ ! -f "$uefi" ]; then echo "IPXE_SHIM_FAIL: nodeset wrote no $uefi" - restore_state exit 1 fi echo "--- $uefi ---" @@ -84,7 +105,6 @@ echo "--- end ---" # that imgload selected, so the shim line must follow it. if ! grep -q '^imgload kernel$' "$uefi"; then echo "IPXE_SHIM_FAIL: the UEFI script of $node selects no kernel image with imgload" - restore_state exit 1 fi after=$(awk '/^imgload kernel$/ { if ((getline line) > 0) { print line } exit }' "$uefi") @@ -92,7 +112,6 @@ case "$after" in "shim http://"*) ;; *) echo "IPXE_SHIM_FAIL: the line after imgload is [$after], not a shim command" - restore_state exit 1 ;; esac @@ -104,16 +123,14 @@ case "$urlpath" in /tftpboot/*) shimfile="$tftpdir/${urlpath#/tftpboot/}" ;; *) echo "IPXE_SHIM_FAIL: the shim path [$urlpath] is under neither /install nor /tftpboot" - restore_state exit 1 ;; esac if [ ! -f "$shimfile" ]; then echo "IPXE_SHIM_FAIL: the script names $urlpath and $shimfile does not exist" - restore_state exit 1 fi -restore_state +restore_state || exit 1 echo "IPXE_SHIM_OK: $after resolves to $shimfile" exit 0 From fd3a21335bc34140c88f3f8c1b1cf0465d794995 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 19:08:58 -0300 Subject: [PATCH 10/25] test(xcat-core): capture a skipped ipxe shim case that reports a pass The case asserts output =~ IPXE_SHIM_(OK|SKIPPED), so the helper's skip path passes it. The skip is not rare: the case is in rhels_x86_daily_flat.bundle, which six confs use, and four of them declare netboot=xnba on their compute node. Those four cells pass the case without reading a boot script. Five tests capture it. The lsdef stub now reports the netboot method and the arch from variables, and the chdef stub can fail the netboot call alone. The tests require the helper to set the netboot method it needs, to measure, to put the method back before the restoring nodeset, to report a netboot that cannot be put back, and never to exit 0 without IPXE_SHIM_OK. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../bats/ipxe_secureboot_shim_restore.bats | 77 +++++++++++++++++-- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats index d28d17c620..f48218aa98 100644 --- a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -31,6 +31,11 @@ setup() export STUB_PROVMETHOD= export STUB_NODESET_RESTORE_RC=0 export STUB_CHDEF_RC=0 + export STUB_CHDEF_NETBOOT_RC=0 + # The netboot the node is declared with. Four of the six confs that carry this case declare + # their compute node netboot=xnba, so this is not a hypothetical value. + export STUB_NETBOOT=ipxe + export STUB_ARCH=x86_64 export STUB_NO_OSIMAGE= export STUB_UEFI="$TFTPDIR/xcat/ipxe/nodes/${NODE}.uefi" export STUB_SHIM_LINE="shim http://\${next-server}/install/$OS/x86_64/EFI/BOOT/BOOTX64.EFI" @@ -54,8 +59,8 @@ done case "$type/$attr" in site/tftpdir) echo " tftpdir=$STUB_TFTPDIR" ;; site/installdir) echo " installdir=$STUB_INSTALLDIR" ;; - node/netboot) echo " netboot=ipxe" ;; - node/arch) echo " arch=x86_64" ;; + node/netboot) echo " netboot=$STUB_NETBOOT" ;; + node/arch) echo " arch=$STUB_ARCH" ;; node/os) echo " os=$STUB_OS" ;; node/provmethod) [ -n "$STUB_PROVMETHOD" ] && echo " provmethod=$STUB_PROVMETHOD" ;; osimage/) [ -n "$STUB_NO_OSIMAGE" ] && exit 1; exit 0 ;; @@ -93,6 +98,9 @@ STUB cat >"$BIN/chdef" <<'STUB' #!/bin/bash echo "chdef $*" >>"$STUB_CALLS" +case "$*" in + *netboot=*) exit "$STUB_CHDEF_NETBOOT_RC" ;; +esac exit "$STUB_CHDEF_RC" STUB @@ -136,13 +144,68 @@ STUB grep -q "^nodeset $NODE boot$" "$CALLS" } -# A skip above the nodeset call changed nothing, so the helper must not call nodeset to put a -# state back it never left. An osimage that is not defined is such a skip. -@test "a node the helper skips calls no nodeset and no chdef" { +# An exit above the nodeset call changed nothing, so the helper must not call nodeset to put a +# state back it never left. +@test "a node with no osimage to name fails and calls no nodeset and no chdef" { STUB_NO_OSIMAGE=1 run "$SCRIPT" "$NODE" - [ "$status" -eq 0 ] - [[ "$output" == *IPXE_SHIM_SKIPPED* ]] + [ "$status" -ne 0 ] + [[ "$output" == *IPXE_SHIM_FAIL* ]] refute_grep -q "^nodeset $NODE boot$" "$CALLS" refute_grep -q "^chdef " "$CALLS" } + +# The case asserted output =~ IPXE_SHIM_(OK|SKIPPED), so a node the helper skipped PASSED the +# case. Four of the six confs that carry this case declare netboot=xnba on their compute node, +# so four cells passed it without reading a boot script at all. The helper now sets the netboot +# method it needs, measures, and puts the method back. +@test "a node declared netboot=xnba is measured, not skipped" { + STUB_NETBOOT=xnba + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + [[ "$output" != *IPXE_SHIM_SKIPPED* ]] + grep -q "^chdef -t node -o $NODE netboot=ipxe$" "$CALLS" + grep -q "^chdef -t node -o $NODE netboot=xnba$" "$CALLS" +} + +# The netboot method goes back before the restoring nodeset, or that nodeset writes the boot +# files of the wrong method. +@test "the netboot method goes back before the nodeset that restores the state" { + STUB_NETBOOT=xnba + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + back=$(grep -n "^chdef -t node -o $NODE netboot=xnba$" "$CALLS" | cut -d: -f1) + reset=$(grep -n "^nodeset $NODE boot$" "$CALLS" | cut -d: -f1) + [ -n "$back" ] && [ -n "$reset" ] && [ "$back" -lt "$reset" ] +} + +# A netboot method left as the case set it makes the next case on that node boot the wrong way, +# which is the same defect the provmethod and nodeset restorations already report. +@test "a netboot method that cannot be put back fails the case" { + STUB_NETBOOT=xnba + STUB_CHDEF_NETBOOT_RC=1 + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *IPXE_SHIM_FAIL* ]] +} + +# The case file now requires the OK token, so an exit 0 without it is a pass the run cannot +# tell from a measurement. Nothing the helper does may produce one. +@test "the helper never exits 0 without reporting IPXE_SHIM_OK" { + for arch in x86_64 ppc64le; do + for netboot in ipxe xnba; do + for noimage in "" 1; do + : >"$CALLS" + STUB_ARCH=$arch STUB_NETBOOT=$netboot STUB_NO_OSIMAGE=$noimage \ + run "$SCRIPT" "$NODE" + if [ "$status" -eq 0 ] && [[ "$output" != *IPXE_SHIM_OK* ]]; then + echo "exit 0 with no OK: arch=$arch netboot=$netboot noimage=$noimage" >&2 + echo "$output" >&2 + return 1 + fi + done + done + done +} From 0378d8b424a1ca04f2f957048504f823650e34ad Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 19:13:42 -0300 Subject: [PATCH 11/25] fix(xcat-core): the ipxe shim case reports a pass without reading a script The case accepted IPXE_SHIM_SKIPPED, so a cell whose compute node is not declared netboot=ipxe passed it without reading a boot script. Four of the six confs that carry the case declare netboot=xnba, so four cells reported a pass for a shim line nothing had measured. The case now asserts IPXE_SHIM_OK and declares arch:x86_64, which keeps it off another architecture through xcattest rather than through a token that passes. The helper sets netboot=ipxe when the node is declared another way, and puts the method back before the nodeset that restores the state. A netboot method that cannot be put back is reported like the provmethod and the nodeset state. The two conditions that used to skip now fail and name what the case could not read. xCAT-test/bats/ipxe_secureboot_shim_restore.bats fails 5 of 9 without this change. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../testcase/nodeset/ipxe_secureboot_shim.sh | 45 +++++++++++++------ .../nodeset/nodeset_ipxe_secureboot_shim | 3 +- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh index 148303fad8..33ee9b8142 100755 --- a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -4,10 +4,10 @@ # # Usage: ipxe_secureboot_shim.sh # -# The caller asserts on one of three tokens, so no error message can carry the token of a pass: -# IPXE_SHIM_OK the script names a shim after imgload, and the file is served -# IPXE_SHIM_SKIPPED the node is not an x86_64 netboot=ipxe node -# IPXE_SHIM_FAIL the shim is missing, out of order, or not served +# The caller asserts on IPXE_SHIM_OK, so no error message can carry the token of a pass: +# IPXE_SHIM_OK the script names a shim after imgload, and the file is served +# IPXE_SHIM_FAIL the shim is missing, out of order, or not served, or the case could not read +# a boot script at all node="$1" if [ -z "$node" ]; then @@ -24,13 +24,11 @@ node_value() { netboot=$(node_value netboot) arch=$(node_value arch) -if [ "$netboot" != "ipxe" ]; then - echo "IPXE_SHIM_SKIPPED: $node has netboot=$netboot, and only the ipxe method names a shim" - exit 0 -fi +# The case file declares arch:x86, so xcattest does not run this on another architecture. A hand +# run can still reach it, and a case that cannot measure must not report a pass. if [ "$arch" != "x86_64" ]; then - echo "IPXE_SHIM_SKIPPED: $node has arch=$arch, and only x86_64 has a UEFI shim" - exit 0 + echo "IPXE_SHIM_FAIL: $node has arch=$arch, and only x86_64 has a UEFI shim" + exit 1 fi tftpdir=$(site_value tftpdir) @@ -47,9 +45,18 @@ echo "nodeset state of $node before this case: ${before:-unknown}, provmethod=${ # so the failure has to reach the caller in place of the result of the case. restore_done=0 restore_rc=0 +netboot_set= restore_state() { [ "$restore_done" = 1 ] && return "$restore_rc" restore_done=1 + # Before the nodeset below, or that nodeset writes the boot files of the ipxe method on a node + # the cluster boots another way. + if [ -n "$netboot_set" ]; then + if ! chdef -t node -o "$node" "netboot=$netboot" > /dev/null; then + echo "IPXE_SHIM_FAIL: chdef -t node -o $node netboot=$netboot returned non-zero, $node keeps the netboot method this case set" + restore_rc=1 + fi + fi if [ -z "$provmethod" ]; then if ! chdef -t node -o "$node" provmethod= > /dev/null; then echo "IPXE_SHIM_FAIL: chdef -t node -o $node provmethod= returned non-zero, $node keeps the provmethod this case set" @@ -79,16 +86,28 @@ destiny=osimage if [ -z "$provmethod" ]; then image="$(node_value os)-$arch-install-compute" if ! lsdef -t osimage -o "$image" > /dev/null 2>&1; then - echo "IPXE_SHIM_SKIPPED: $node has no provmethod and osimage $image is not defined" - exit 0 + echo "IPXE_SHIM_FAIL: $node has no provmethod and osimage $image is not defined, so this" + echo "IPXE_SHIM_FAIL: case has no boot script to read" + exit 1 fi destiny="osimage=$image" fi -# Arm the restoration before the call that changes the state, and not earlier: a skip above this +# Arm the restoration before the call that changes the state, and not earlier: an exit above this # line has nothing to put back. Every exit after it passes through the restoration. trap on_exit EXIT +# Only the ipxe method writes a shim line, so the case sets the method it reads rather than +# reporting a pass on a node that is declared another way. Four of the six confs that carry this +# case declare netboot=xnba on their compute node. +if [ "$netboot" != "ipxe" ]; then + if ! chdef -t node -o "$node" netboot=ipxe > /dev/null; then + echo "IPXE_SHIM_FAIL: chdef -t node -o $node netboot=ipxe returned non-zero" + exit 1 + fi + netboot_set=1 +fi + if ! nodeset "$node" "$destiny"; then echo "IPXE_SHIM_FAIL: nodeset $node $destiny returned non-zero" exit 1 diff --git a/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim b/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim index 3a81f4c361..9a709bdd35 100644 --- a/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim +++ b/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim @@ -1,7 +1,8 @@ start:nodeset_ipxe_secureboot_shim label:others,nodeset +arch:x86_64 description:verify the UEFI boot script of an ipxe node names a Secure Boot shim after imgload cmd:/opt/xcat/share/xcat/tools/autotest/testcase/nodeset/ipxe_secureboot_shim.sh $$CN check:rc==0 -check:output=~IPXE_SHIM_(OK|SKIPPED) +check:output=~IPXE_SHIM_OK end From ad91ffee51d2c5d3570c1a0f679041b83237cc3a Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 23:20:57 -0300 Subject: [PATCH 12/25] test(kvm): no test can define an x86_64 KVM guest with UEFI firmware build_xmldesc reaches its UEFI path only for riscv64, so no test covers an x86_64 guest that asks for UEFI. The existing kvm unit tests cannot cover it either: they read kvm.pm, extract four routines with a regex and eval a copy, which keeps passing when the original changes. kvm_uefi_firmware.t loads the plugin itself, with Perl stubs for the database, the monitoring dispatcher, the thread helper and the libvirt binding. It calls build_xmldesc and reconfigvm, and reads the domain XML they return. process_request already set $parser, $node, $confdata and $updatetable before it called the domain builder. They are package variables now, so the test sets them the same way. The plugin reads them unchanged. Six checks fail on this commit. The guest states no firmware and no machine type, its disks stay on a bus q35 cannot carry, the rewritten guest asks for no firmware, and a firmware value libvirt cannot select is accepted in silence. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-server/lib/xcat/plugins/kvm.pm | 11 +- xCAT-test/unit/kvm_uefi_firmware.t | 247 ++++++++++++++++++++++++++++ 2 files changed, 254 insertions(+), 4 deletions(-) create mode 100644 xCAT-test/unit/kvm_uefi_firmware.t diff --git a/xCAT-server/lib/xcat/plugins/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index a58c0d8254..78196aab0c 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -36,10 +36,13 @@ my %vm_comm_pids; my %offlinehyps; my %hypstats; my %offlinevms; -my $parser; +# process_request sets $parser, $node, $confdata and $updatetable before it calls the domain +# builder. They are package variables so a unit test can set them the same way and call the +# builder itself, rather than extracting it from this file and testing a copy. +our $parser; my @destblacklist; -my $updatetable; #when a function is performing per-node operations, it can queue up a table update by populating parts of this hash -my $confdata; #a reference to serve as a common pointer betweer VMCommon functions and this plugin +our $updatetable; #when a function is performing per-node operations, it can queue up a table update by populating parts of this hash +our $confdata; #a reference to serve as a common pointer betweer VMCommon functions and this plugin my %allnodestatus; require Sys::Virt; @@ -97,7 +100,7 @@ my $hypconn; my $hyp; my $doreq; my %hyphash; -my $node; +our $node; my $vmtab; diff --git a/xCAT-test/unit/kvm_uefi_firmware.t b/xCAT-test/unit/kvm_uefi_firmware.t new file mode 100644 index 0000000000..5c34eacc22 --- /dev/null +++ b/xCAT-test/unit/kvm_uefi_firmware.t @@ -0,0 +1,247 @@ +#!/usr/bin/env perl +# An x86_64 KVM guest asks libvirt for UEFI with vm.othersettings "firmware:efi". A guest that +# does not ask keeps SeaBIOS, so a site that never wanted UEFI sees no change. +# +# Two things the guest already has must survive the firmware selection. +# puts the boot of the guest on the serial console, which is where the CI reads every boot +# signal. The SMBIOS block gives the guest bare-metal DMI strings; without them sequential +# discovery refuses the node as a virtual machine. +use strict; +use warnings; + +use File::Temp qw(tempdir); +use FindBin; +use Test::More; +use XML::LibXML; + +# The plugin package declares the variables below; this test names each of them once. +no warnings 'once'; + +my $repo = "$FindBin::Bin/../.."; + +BEGIN { + # kvm.pm loads the xCAT database, the monitoring dispatcher, the thread helper and the + # libvirt binding. None of them builds the domain XML. + package Sys::Virt; + our $VERSION = '11.0.0'; + + package Thread; + sub import { + my $caller = caller; + no strict 'refs'; + *{"${caller}::yield"} = sub { return }; + return; + } + + package xCAT::Utils; + sub genpassword { return 'password' } + sub genUUID { return '00000000-0000-0000-0000-000000000002' } + + sub import { + my ( undef, @names ) = @_; + my $caller = caller; + no strict 'refs'; + *{"${caller}::$_"} = \&{"xCAT::Utils::$_"} for @names; + return; + } + + package xCAT::VMCommon; + sub getMacAddresses { return ('52:54:00:00:00:01') } + + package xCAT::Table; + sub new { return } + + package xCAT::GlobalDef; + + package xCAT::NodeRange; + + package xCAT::Usage; + + package xCAT::TableUtils; + + package xCAT::ServiceNodeUtils; + + package xCAT::DBobjUtils; + + package xCAT::SvrUtils; + + package xCAT_monitoring::monitorctrl; + + package main; + $INC{'Sys/Virt.pm'} = __FILE__; + $INC{'Thread.pm'} = __FILE__; + $INC{'xCAT_monitoring/monitorctrl.pm'} = __FILE__; + for my $module ( + qw(GlobalDef NodeRange VMCommon Table Usage Utils TableUtils + ServiceNodeUtils DBobjUtils SvrUtils) + ) + { + $INC{"xCAT/$module.pm"} = __FILE__; + } + + # kvm.pm puts $XCATROOT/lib/perl in front of the checkout. Point it at an empty directory, + # so the test cannot measure a product installed weeks ago. + $ENV{XCATROOT} = File::Temp::tempdir( CLEANUP => 1 ); +} + +# xcatd loads the plugins from their files. +my $inc = tempdir( CLEANUP => 1 ); +symlink( "$repo/xCAT-server/lib/xcat/plugins", "$inc/xCAT_plugin" ) or die "symlink: $!"; +unshift @INC, $inc, "$repo/xCAT-server/lib/perl", "$repo/perl-xCAT"; +require xCAT_plugin::kvm; + +# One compute node on one hypervisor, the way process_request hands them to the domain builder. +sub node_tables { + my (%attrib) = @_; + return { + vm => { + cn1 => [ + { + host => 'hyp1', + memory => 4096, + cpus => 2, + nics => 'br0', + storage => '/var/lib/libvirt/images/cn1.img', + ( + defined $attrib{othersettings} + ? ( othersettings => $attrib{othersettings} ) + : () + ), + ( + defined $attrib{bootorder} + ? ( bootorder => $attrib{bootorder} ) + : () + ), + } + ] + }, + nodetype => { cn1 => [ { arch => $attrib{arch} || 'x86_64', os => 'alma10.0' } ] }, + vpd => { cn1 => [ { uuid => 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' } ] }, + hyp1 => { cpumodel => $attrib{cpumodel} || 'x86_64' }, + }; +} + +# Build the domain XML of that node. Returns the XML, or the refusal the builder returned. +sub domain_xml { + my (%attrib) = @_; + local $xCAT_plugin::kvm::node = 'cn1'; + local $xCAT_plugin::kvm::confdata = node_tables(%attrib); + local $xCAT_plugin::kvm::updatetable = {}; + my ( $xml, $errstr ); + my $chatter = ''; + { + open( my $capture, '>', \$chatter ) or die "capture stdout: $!"; + local *STDOUT = $capture; + ( $xml, $errstr ) = xCAT_plugin::kvm::build_xmldesc('cn1'); + } + return ( $xml, $errstr ); +} + +sub os_element { + my ($xml) = @_; + die "build_xmldesc returned no XML\n" unless defined $xml and !ref $xml; + my ($os) = XML::LibXML->load_xml( string => $xml )->findnodes('/domain/os'); + die "the domain XML carries no element\n" unless $os; + return $os; +} + +# The elements and the attributes of , without the indentation XML::Simple adds. +sub os_shape { + my ($xml) = @_; + ( my $shape = os_element($xml)->toString() ) =~ s/>\s+load_xml( string => $xml )->findnodes($xpath); + return undef unless $node; + return $node->getAttribute($name); +} + +# A node that asks for UEFI. +my ($uefi) = domain_xml( othersettings => 'firmware:efi' ); +is( attribute( $uefi, '/domain/os', 'firmware' ), 'efi', + 'firmware:efi puts firmware="efi" on , which is how libvirt selects a UEFI firmware' ); +is( attribute( $uefi, '/domain/os/type', 'machine' ), 'q35', + 'the UEFI node is placed on q35, the only machine type the x86_64 OVMF descriptors target' ); +is( attribute( $uefi, '/domain/os/bios', 'useserial' ), 'yes', + 'the UEFI node keeps , so its boot still reaches the serial console' ); + +# q35 has no IDE controller, so libvirt refuses a domain whose disks state the ide bus. +is( attribute( $uefi, '/domain/devices/disk[1]/target', 'bus' ), 'scsi', + 'the disk of the UEFI node is scsi, a bus q35 can carry' ); +like( attribute( $uefi, '/domain/devices/disk[2]/target', 'dev' ), qr/^sd/, + 'and its optical drive is named sd*, not hd*' ); + +# A node that names its own machine type keeps it. q35 is a default, not an override. +my ($named_machine) = domain_xml( othersettings => 'firmware:efi;machine:pc-q35-rhel9.4.0' ); +is( attribute( $named_machine, '/domain/os/type', 'machine' ), 'pc-q35-rhel9.4.0', + 'firmware:efi leaves a machine type the node names alone' ); + +# The SMBIOS block of a discovery guest. cluster-test.pl stores a masked domain XML in +# kvm_nodedata, and kvm.pm builds the guest from that stored XML through reconfigvm. Give +# reconfigvm the masked UEFI domain and a boot order to change, so it rewrites . +sub smbios_masked { + my ($xml) = @_; + my $doc = XML::LibXML->load_xml( string => $xml ); + my ($domain) = $doc->findnodes('/domain'); + my ($os) = $doc->findnodes('/domain/os'); + my $sysinfo = $doc->createElement('sysinfo'); + $sysinfo->setAttribute( 'type', 'smbios' ); + my $system = $doc->createElement('system'); + for my $pair ( [ manufacturer => 'Supermicro' ], [ product => 'Super Server' ] ) { + my $entry = $doc->createElement('entry'); + $entry->setAttribute( 'name', $pair->[0] ); + $entry->appendText( $pair->[1] ); + $system->appendChild($entry); + } + $sysinfo->appendChild($system); + $domain->appendChild($sysinfo); + my $mode = $doc->createElement('smbios'); + $mode->setAttribute( 'mode', 'sysinfo' ); + $os->appendChild($mode); + return $doc->toString(); +} + +sub reconfigured { + my ( $xml, %attrib ) = @_; + local $xCAT_plugin::kvm::node = 'cn1'; + local $xCAT_plugin::kvm::confdata = node_tables(%attrib); + local $xCAT_plugin::kvm::parser = XML::LibXML->new(); + my $rewritten = xCAT_plugin::kvm::reconfigvm( 'cn1', $xml ); + die "reconfigvm rewrote nothing, so the test measures no rewrite\n" unless $rewritten; + return $rewritten; +} + +my $rebuilt = reconfigured( smbios_masked($uefi), bootorder => 'hd,net' ); +is( attribute( $rebuilt, '/domain/sysinfo', 'type' ), 'smbios', + 'the rebuilt UEFI guest keeps the block with its DMI strings' ); +is( attribute( $rebuilt, '/domain/os/smbios', 'mode' ), 'sysinfo', + 'and keeps , which is what makes the guest read that block' ); +is( attribute( $rebuilt, '/domain/os', 'firmware' ), 'efi', + 'and still asks for UEFI after the rewrite' ); +is( attribute( $rebuilt, '/domain/os/bios', 'useserial' ), 'yes', + 'and still puts its boot on the serial console' ); + +# A node that asks for nothing. is the only element the firmware selection touches, so it +# is held down whole. +my ($plain) = domain_xml(); +is( os_shape($plain), + 'hvm', + 'an x86_64 node that asks for nothing states no firmware and no machine type' ); + +is( attribute( $plain, '/domain/devices/disk[1]/target', 'bus' ), 'ide', + 'and keeps the ide disk bus of the machine type it has always had' ); + +my ($other_keys) = domain_xml( othersettings => 'cpumode:host-passthrough' ); +is( attribute( $other_keys, '/domain/os', 'firmware' ), undef, + 'another vm.othersettings key does not select a firmware' ); + +# A value libvirt cannot select is refused by name. A typo must not quietly leave the node on +# SeaBIOS, because the node then fails a long way from here. +my ( $refused, $errstr ) = domain_xml( othersettings => 'firmware:uefi' ); +like( $errstr, qr/firmware:uefi/, + 'firmware:uefi is refused and the refusal names the value that was read' ); + +done_testing(); From bcc60a313275dce9523be5448d250587f939c5d7 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 23:43:59 -0300 Subject: [PATCH 13/25] fix(kvm): an x86_64 KVM guest cannot be defined with UEFI firmware guest_arch_profile reaches the firmware selection only for riscv64, so an x86_64 KVM guest always comes up on SeaBIOS. A node that must boot the UEFI shim has no way to ask for UEFI. vm.othersettings takes a sixth key, firmware:efi. requested_firmware reads it, and guest_arch_profile then puts firmware="efi" on . libvirt selects the firmware files from that attribute and writes and itself. The guest also moves to q35, because every x86_64 OVMF descriptor targets pc-q35-* and libvirt finds no efi firmware for the i440fx machine it defaults to. q35 has no IDE controller, so the disks and the optical drive move to scsi. A machine type the node names for itself still wins. A firmware value other than efi is refused by name. No node asks for a firmware by default, so a guest that does not ask comes out as before, on SeaBIOS with ide disks. stays in place for the UEFI guest, which keeps its boot on the serial console. kvm_guest_arch.t, kvm_diskstruct_bus.t and kvm_createstorage_model.t extract routines from this file by name and eval a copy, so each one names requested_firmware now. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-server/lib/xcat/plugins/kvm.pm | 36 ++++++++++++++++++++++-- xCAT-test/unit/kvm_createstorage_model.t | 2 +- xCAT-test/unit/kvm_diskstruct_bus.t | 2 +- xCAT-test/unit/kvm_guest_arch.t | 3 +- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index 78196aab0c..d7231984e5 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -502,7 +502,8 @@ sub build_diskstruct { my $suffidx = 0; my $storagemodel = $confdata->{vm}->{$node}->[0]->{storagemodel}; my $profile = guest_arch_profile($confdata->{nodetype}->{$node}->[0]->{arch}, - $confdata->{ $confdata->{vm}->{$node}->[0]->{host} }->{cpumodel}); + $confdata->{ $confdata->{vm}->{$node}->[0]->{host} }->{cpumodel}, + requested_firmware($confdata->{vm}->{$node}->[0]->{othersettings})); my $cachemethod = "none"; if ($confdata->{vm}->{$node}->[0]->{storagecache}) { $cachemethod = $confdata->{vm}->{$node}->[0]->{storagecache}; @@ -726,6 +727,18 @@ sub default_storagemodel { return 'scsi'; } +# requested_firmware: the firmware a node asks for in vm.othersettings, or undef. +# +# No node asks by default, so an existing guest keeps the firmware of its machine type. +sub requested_firmware { + my ($othersettings) = @_; + return undef unless defined $othersettings; + foreach my $setting (split /;/, $othersettings) { + return $1 if $setting =~ /^\s*firmware:(.*)/; + } + return undef; +} + # guest_arch_profile: the libvirt domain type and settings for one guest. # # The architecture of the guest comes from the node, not from the hypervisor. A node whose @@ -733,12 +746,15 @@ sub default_storagemodel { # domain type "qemu". riscv64 has no BIOS: the virt machine boots UEFI, and pae/acpi/apic # are x86 features that libvirt rejects there. # +# An x86_64 guest that asks for efi moves to q35, because every x86_64 OVMF descriptor targets +# pc-q35-* only. q35 has no IDE controller, so its disks and its optical drive move to scsi. +# # POWER keeps reading the hypervisor cpumodel. ppc64le hypervisors report "ppc64le" (not # "ppc64"); both are pseries guests whose libvirt arch is "ppc64". # # arch and machine stay undef when libvirt is to use its own default for the hypervisor. sub guest_arch_profile { - my ($guest_arch, $hyp_cpumodel) = @_; + my ($guest_arch, $hyp_cpumodel, $firmware) = @_; my %profile = ( domtype => 'kvm', arch => undef, @@ -771,6 +787,14 @@ sub guest_arch_profile { $profile{bios} = 0; $profile{sound} = 0; } + if (defined($firmware) and $firmware eq 'efi' and not defined $profile{firmware}) { + $profile{firmware} = 'efi'; + if ($profile{x86_features}) { + $profile{machine} = 'q35'; + $profile{disk_model} = 'scsi'; + $profile{cd_prefix} = 'sd'; + } + } return \%profile; } @@ -786,7 +810,12 @@ sub build_xmldesc { $hypcputhreads = "1"; } - my $profile = guest_arch_profile($confdata->{nodetype}->{$node}->[0]->{arch}, $hypcpumodel); + my $firmware = requested_firmware($confdata->{vm}->{$node}->[0]->{othersettings}); + if (defined $firmware and $firmware ne 'efi') { + return (-1, "vm.othersettings firmware:$firmware names no firmware libvirt can select for $node. Use firmware:efi."); + } + + my $profile = guest_arch_profile($confdata->{nodetype}->{$node}->[0]->{arch}, $hypcpumodel, $firmware); $xtree{type} = $profile->{domtype}; $xtree{name}->{content} = $node; @@ -824,6 +853,7 @@ sub build_xmldesc { #memory binding: "membind:" #cpu mode: "cpumode:" #machine type: "machine:" + #firmware: "firmware:efi" if ($advsettings) { my @tmp_array = split ";", $advsettings; foreach (@tmp_array) { diff --git a/xCAT-test/unit/kvm_createstorage_model.t b/xCAT-test/unit/kvm_createstorage_model.t index 7128f151ee..ff639ab9c6 100644 --- a/xCAT-test/unit/kvm_createstorage_model.t +++ b/xCAT-test/unit/kvm_createstorage_model.t @@ -15,7 +15,7 @@ close($source_fh) or die "close $source: $!"; my @routines; for my $name (qw(createstorage build_diskstruct guest_arch_profile getUnits - default_storagemodel)) { + default_storagemodel requested_firmware)) { my ($routine) = $content =~ /^(sub \Q$name\E\s*\{.*?^\})/ms; die("could not extract $name from kvm.pm") unless $routine; push(@routines, $routine); diff --git a/xCAT-test/unit/kvm_diskstruct_bus.t b/xCAT-test/unit/kvm_diskstruct_bus.t index a02c1c2b3c..6f55d72963 100644 --- a/xCAT-test/unit/kvm_diskstruct_bus.t +++ b/xCAT-test/unit/kvm_diskstruct_bus.t @@ -14,7 +14,7 @@ my $content = do { local $/; <$source_fh> }; close($source_fh) or die "close $source: $!"; my @routines; -for my $name (qw(build_diskstruct guest_arch_profile getUnits)) { +for my $name (qw(build_diskstruct guest_arch_profile getUnits requested_firmware)) { my ($routine) = $content =~ /^(sub \Q$name\E\s*\{.*?^\})/ms; die("could not extract $name from kvm.pm") unless $routine; push(@routines, $routine); diff --git a/xCAT-test/unit/kvm_guest_arch.t b/xCAT-test/unit/kvm_guest_arch.t index 34ba7649af..5fc705ccea 100644 --- a/xCAT-test/unit/kvm_guest_arch.t +++ b/xCAT-test/unit/kvm_guest_arch.t @@ -14,7 +14,8 @@ my $content = do { local $/; <$source_fh> }; close($source_fh) or die "close $source: $!"; my @routines; -for my $name (qw(build_xmldesc guest_arch_profile build_oshash build_diskstruct getUnits)) { +for my $name (qw(build_xmldesc guest_arch_profile build_oshash build_diskstruct getUnits + requested_firmware)) { my ($routine) = $content =~ /^(sub \Q$name\E\s*\{.*?^\})/ms; die("could not extract $name from kvm.pm") unless $routine; push(@routines, $routine); From 74954c9bc68b8bf1974acead91647481e414d547 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 8 Oct 2026 00:33:46 -0300 Subject: [PATCH 14/25] fix(kvm): reading vm.othersettings creates a vm row for a node that has none requested_firmware was called as requested_firmware($confdata->{vm}->{$node}->[0]->{othersettings}). An rvalue dereference chain autovivifies its intermediate links, so that read makes $confdata->{vm}->{$node} exist, as an arrayref holding an empty hashref, for a node with no vm row. A later truth test on that row then answers yes for a node that is not a VM. vm_othersettings reads the column one link at a time and returns undef at the first link that is absent, so the read leaves $confdata as it was. Both call sites use it. Three checks in kvm_uefi_firmware.t hold this down, and all three fail when vm_othersettings is changed back to the dereference chain: the read creates no vm table, creates no row for the node, and leaves confdata equal to its starting value. build_xmldesc and build_diskstruct still autovivify the same row through their own older reads of vm.storagemodel and vm.host. That is unchanged by this commit and is measurable: the confdata both routines leave for a node with no vm row is identical with and without the firmware code. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-server/lib/xcat/plugins/kvm.pm | 17 +++++++++++++++-- xCAT-test/unit/kvm_createstorage_model.t | 2 +- xCAT-test/unit/kvm_diskstruct_bus.t | 2 +- xCAT-test/unit/kvm_guest_arch.t | 2 +- xCAT-test/unit/kvm_uefi_firmware.t | 20 ++++++++++++++++++++ 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index d7231984e5..076ce68230 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -503,7 +503,7 @@ sub build_diskstruct { my $storagemodel = $confdata->{vm}->{$node}->[0]->{storagemodel}; my $profile = guest_arch_profile($confdata->{nodetype}->{$node}->[0]->{arch}, $confdata->{ $confdata->{vm}->{$node}->[0]->{host} }->{cpumodel}, - requested_firmware($confdata->{vm}->{$node}->[0]->{othersettings})); + requested_firmware(vm_othersettings($node))); my $cachemethod = "none"; if ($confdata->{vm}->{$node}->[0]->{storagecache}) { $cachemethod = $confdata->{vm}->{$node}->[0]->{storagecache}; @@ -727,6 +727,19 @@ sub default_storagemodel { return 'scsi'; } +# vm_othersettings: the vm.othersettings of one node, or undef when the node has no vm row. +# +# An rvalue dereference chain autovivifies its intermediate links, so reading the column as +# $confdata->{vm}->{$node}->[0]->{othersettings} creates a vm row for a node that has none. +sub vm_othersettings { + my ($nodename) = @_; + return undef unless ref($confdata) eq 'HASH'; + my $vm = $confdata->{vm} or return undef; + my $rows = $vm->{$nodename} or return undef; + my $row = $rows->[0] or return undef; + return $row->{othersettings}; +} + # requested_firmware: the firmware a node asks for in vm.othersettings, or undef. # # No node asks by default, so an existing guest keeps the firmware of its machine type. @@ -810,7 +823,7 @@ sub build_xmldesc { $hypcputhreads = "1"; } - my $firmware = requested_firmware($confdata->{vm}->{$node}->[0]->{othersettings}); + my $firmware = requested_firmware(vm_othersettings($node)); if (defined $firmware and $firmware ne 'efi') { return (-1, "vm.othersettings firmware:$firmware names no firmware libvirt can select for $node. Use firmware:efi."); } diff --git a/xCAT-test/unit/kvm_createstorage_model.t b/xCAT-test/unit/kvm_createstorage_model.t index ff639ab9c6..c56c8a24e8 100644 --- a/xCAT-test/unit/kvm_createstorage_model.t +++ b/xCAT-test/unit/kvm_createstorage_model.t @@ -15,7 +15,7 @@ close($source_fh) or die "close $source: $!"; my @routines; for my $name (qw(createstorage build_diskstruct guest_arch_profile getUnits - default_storagemodel requested_firmware)) { + default_storagemodel requested_firmware vm_othersettings)) { my ($routine) = $content =~ /^(sub \Q$name\E\s*\{.*?^\})/ms; die("could not extract $name from kvm.pm") unless $routine; push(@routines, $routine); diff --git a/xCAT-test/unit/kvm_diskstruct_bus.t b/xCAT-test/unit/kvm_diskstruct_bus.t index 6f55d72963..f3c2e90d58 100644 --- a/xCAT-test/unit/kvm_diskstruct_bus.t +++ b/xCAT-test/unit/kvm_diskstruct_bus.t @@ -14,7 +14,7 @@ my $content = do { local $/; <$source_fh> }; close($source_fh) or die "close $source: $!"; my @routines; -for my $name (qw(build_diskstruct guest_arch_profile getUnits requested_firmware)) { +for my $name (qw(build_diskstruct guest_arch_profile getUnits requested_firmware vm_othersettings)) { my ($routine) = $content =~ /^(sub \Q$name\E\s*\{.*?^\})/ms; die("could not extract $name from kvm.pm") unless $routine; push(@routines, $routine); diff --git a/xCAT-test/unit/kvm_guest_arch.t b/xCAT-test/unit/kvm_guest_arch.t index 5fc705ccea..0f4b3fd94e 100644 --- a/xCAT-test/unit/kvm_guest_arch.t +++ b/xCAT-test/unit/kvm_guest_arch.t @@ -15,7 +15,7 @@ close($source_fh) or die "close $source: $!"; my @routines; for my $name (qw(build_xmldesc guest_arch_profile build_oshash build_diskstruct getUnits - requested_firmware)) { + requested_firmware vm_othersettings)) { my ($routine) = $content =~ /^(sub \Q$name\E\s*\{.*?^\})/ms; die("could not extract $name from kvm.pm") unless $routine; push(@routines, $routine); diff --git a/xCAT-test/unit/kvm_uefi_firmware.t b/xCAT-test/unit/kvm_uefi_firmware.t index 5c34eacc22..ee56a05ab4 100644 --- a/xCAT-test/unit/kvm_uefi_firmware.t +++ b/xCAT-test/unit/kvm_uefi_firmware.t @@ -238,6 +238,26 @@ my ($other_keys) = domain_xml( othersettings => 'cpumode:host-passthrough' ); is( attribute( $other_keys, '/domain/os', 'firmware' ), undef, 'another vm.othersettings key does not select a firmware' ); +# Reading the setting must not create the row it reads. An rvalue dereference chain +# autovivifies its intermediate links, so $confdata->{vm}->{$node}->[0]->{othersettings} makes +# a vm row exist for a node that is not a VM, and a later truth test on that row answers yes. +{ + local $xCAT_plugin::kvm::confdata = { nodetype => { cn9 => [ { arch => 'x86_64' } ] } }; + is( xCAT_plugin::kvm::vm_othersettings('cn9'), undef, + 'vm_othersettings answers undef for a node with no vm row' ); + ok( !exists $xCAT_plugin::kvm::confdata->{vm}, + 'and the read autovivified no vm table in confdata' ); +} +{ + local $xCAT_plugin::kvm::confdata = { vm => { cn8 => [ { host => 'hyp1' } ] } }; + is( xCAT_plugin::kvm::vm_othersettings('cn9'), undef, + 'vm_othersettings answers undef for a node absent from a populated vm table' ); + ok( !exists $xCAT_plugin::kvm::confdata->{vm}->{cn9}, + 'and the read autovivified no row for that node' ); + is_deeply( $xCAT_plugin::kvm::confdata, { vm => { cn8 => [ { host => 'hyp1' } ] } }, + 'and left confdata as it was' ); +} + # A value libvirt cannot select is refused by name. A typo must not quietly leave the node on # SeaBIOS, because the node then fails a long way from here. my ( $refused, $errstr ) = domain_xml( othersettings => 'firmware:uefi' ); From 87cd06e4e53bfc08be956683ec08001e23262033 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 8 Oct 2026 13:16:56 -0300 Subject: [PATCH 15/25] test(xcat-core): the ipxe shim case reports a pass after leaving a node configured for installation restore_state in ipxe_secureboot_shim.sh matched the saved nodeset state against boot, offline, shell and standby. nodeset stat reports chain.currstate verbatim, so runcmd=bmcsetup, osimage=, runimage= and the deprecated install, netboot and statelite all fall past that list. The helper then restored nothing and still printed IPXE_SHIM_OK, so the node kept the installation the case configured and the next case on that node measured it. The lookups hide the same failure one layer down. lsdef and nodeset run in a pipeline, so the exit status comes from sed or head and is always zero, and an attribute that is unset prints the same empty string as a lookup that failed. The helper clears the provmethod and the netboot method it reads as unset, so a failed lookup makes it delete a value the node really had. The tests drive the helper through nine states and lookups. Against the unfixed helper, all four refusal cases exit 0 and print IPXE_SHIM_OK after calling nodeset osimage= on the node, and the provmethod case prints "provmethod=unset" for a provmethod the stub holds set. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../bats/ipxe_secureboot_shim_restore.bats | 130 +++++++++++++++++- 1 file changed, 127 insertions(+), 3 deletions(-) diff --git a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats index f48218aa98..a5b7d617eb 100644 --- a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -6,7 +6,10 @@ # the next case on that node measures the state this one left. So the helper has to report a # failed restoration instead of its own result. # -# lsdef, nodeset and chdef are stubbed, and every call is recorded in a log the tests read. +# lsdef, nodeset and chdef are stubbed, and every call is recorded in a log the tests read. A +# stub can also be told to fail: STUB_STAT_RC fails "nodeset stat", and STUB_LSDEF_FAIL_ATTR +# fails the lsdef of one attribute. Both failures print nothing on stdout, which is what an +# attribute that is unset prints too. load 'helpers/shell_source' @@ -29,6 +32,9 @@ setup() # provmethod is what sends the helper through chdef on the way out. export STUB_BEFORE=boot export STUB_PROVMETHOD= + export STUB_PROFILE=compute + export STUB_STAT_RC=0 + export STUB_LSDEF_FAIL_ATTR= export STUB_NODESET_RESTORE_RC=0 export STUB_CHDEF_RC=0 export STUB_CHDEF_NETBOOT_RC=0 @@ -56,6 +62,12 @@ while [ $# -gt 0 ]; do *) shift ;; esac done +# A failed lookup prints nothing and exits non-zero. An attribute that is unset prints nothing +# and exits zero, so only the exit status tells the two apart. +if [ -n "$STUB_LSDEF_FAIL_ATTR" ] && [ "$attr" = "$STUB_LSDEF_FAIL_ATTR" ]; then + echo "Error: could not read $attr" >&2 + exit 1 +fi case "$type/$attr" in site/tftpdir) echo " tftpdir=$STUB_TFTPDIR" ;; site/installdir) echo " installdir=$STUB_INSTALLDIR" ;; @@ -63,6 +75,7 @@ case "$type/$attr" in node/arch) echo " arch=$STUB_ARCH" ;; node/os) echo " os=$STUB_OS" ;; node/provmethod) [ -n "$STUB_PROVMETHOD" ] && echo " provmethod=$STUB_PROVMETHOD" ;; + node/profile) [ -n "$STUB_PROFILE" ] && echo " profile=$STUB_PROFILE" ;; osimage/) [ -n "$STUB_NO_OSIMAGE" ] && exit 1; exit 0 ;; *) echo "unexpected lsdef $type $obj $attr" >&2; exit 1 ;; esac @@ -76,6 +89,7 @@ STUB echo "nodeset $*" >>"$STUB_CALLS" case "$2" in stat) + [ "$STUB_STAT_RC" = 0 ] || { echo "Error: cannot reach the server" >&2; exit "$STUB_STAT_RC"; } echo "$1: $STUB_BEFORE" ;; osimage=*) @@ -113,7 +127,7 @@ STUB [ "$status" -eq 0 ] [[ "$output" == *IPXE_SHIM_OK* ]] grep -q "^nodeset $NODE boot$" "$CALLS" - grep -q "^chdef -t node -o $NODE provmethod=$" "$CALLS" + grep -q "^chdef -t node -o $NODE provmethod= profile=compute$" "$CALLS" } @test "a nodeset that cannot put the state back fails the case" { @@ -131,7 +145,7 @@ STUB [ "$status" -ne 0 ] [[ "$output" != *IPXE_SHIM_OK* ]] [[ "$output" == *IPXE_SHIM_FAIL* ]] - grep -q "^chdef -t node -o $NODE provmethod=$" "$CALLS" + grep -q "^chdef -t node -o $NODE provmethod= profile=compute$" "$CALLS" } # A control: the helper already restores the state when its own assertion fails. It holds the @@ -209,3 +223,113 @@ STUB done done } + + +# nodeset stat reports chain.currstate verbatim, and a state can carry an argument: +# "runcmd=bmcsetup" is one state, not the state "runcmd". The helper matched ${before%% *} +# against four names, so a node in any other state kept the installation this case configured +# and the case still printed IPXE_SHIM_OK. +@test "a runcmd state goes back with its command argument" { + STUB_BEFORE='runcmd=bmcsetup' + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + grep -q "^nodeset $NODE runcmd=bmcsetup$" "$CALLS" +} + +# The saved image is not the image the helper sets, or the assertion matches the helper's own +# forward call and cannot fail. +@test "an osimage state goes back with its image name" { + STUB_BEFORE="osimage=$OS-x86_64-netboot-compute" + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + grep -q "^nodeset $NODE osimage=$OS-x86_64-netboot-compute$" "$CALLS" +} + +# install, netboot and statelite are deprecated: destiny.pm answers "The options install, +# netboot and statelite have been deprecated" and sets errorabort, so replaying one fails and +# leaves the node configured for installation. The helper cannot put such a state back, so it +# must refuse while the node is still untouched. +@test "a saved state the helper cannot replay stops it before the first mutation" { + STUB_BEFORE=install + STUB_NETBOOT=xnba + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *"state [install]"* ]] + refute_grep -q "^chdef " "$CALLS" + refute_grep -q "^nodeset $NODE osimage" "$CALLS" +} + +# A pipeline hides the exit status of its first command, so "nodeset stat | sed | head" reported +# success with an empty state. The helper then read the node as stateless and restored nothing. +@test "a nodeset stat that fails stops the helper before the first mutation" { + STUB_STAT_RC=1 + STUB_NETBOOT=xnba + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *"nodeset $NODE stat returned non-zero"* ]] + refute_grep -q "^chdef " "$CALLS" + refute_grep -q "^nodeset $NODE osimage" "$CALLS" +} + +# lsdef prints nothing for an attribute that is unset, so a failed lookup and an unset attribute +# were the same empty string. The helper clears the provmethod it believes was unset, so a +# failed lookup made it delete the provmethod the node really had. +@test "a provmethod lookup that fails does not clear the provmethod" { + STUB_LSDEF_FAIL_ATTR=provmethod + STUB_PROVMETHOD=alma9.8-x86_64-install-compute + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *"-i provmethod"* ]] + refute_grep -q "^chdef " "$CALLS" + refute_grep -q "^nodeset $NODE osimage" "$CALLS" +} + +# The same read, the same damage: a netboot method read as empty is put back as empty, which +# clears the method the cluster boots the node with. +@test "a netboot lookup that fails does not change the netboot method" { + STUB_LSDEF_FAIL_ATTR=netboot + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *"-i netboot"* ]] + refute_grep -q "^chdef " "$CALLS" + refute_grep -q "^nodeset $NODE osimage" "$CALLS" +} + +# A control for the two above: an attribute that is readable and unset is not a failed lookup, +# and the helper still runs. Without this the tests above are satisfied by a helper that refuses +# every empty attribute. +@test "a provmethod that is unset but readable is not a failed lookup" { + STUB_PROVMETHOD= + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + grep -q "^nodeset $NODE osimage=$OS-x86_64-install-compute$" "$CALLS" +} + +# setdestiny writes nodetype.provmethod, profile, os and arch from the osimage row it resolved +# (destiny.pm, "my $updateattribs"). On the path where the helper names its own +# --install-compute image, os and arch come back as the node's own values by +# construction of that name, and profile does not: it becomes the row's profile. +@test "the profile that nodeset osimage overwrites goes back too" { + STUB_PROVMETHOD= + STUB_PROFILE=service + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + grep -q "^chdef -t node -o $NODE provmethod= profile=service$" "$CALLS" +} + +# An arch that cannot be read is not an arch that is wrong, and the message has to say which. +@test "an arch lookup that fails is reported as a failed lookup" { + STUB_LSDEF_FAIL_ATTR=arch + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" == *"-i arch"* ]] + [[ "$output" != *"only x86_64 has a UEFI shim"* ]] +} From 1b40f593489d3ae629f7cd6f742a178ec5b29eed Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 8 Oct 2026 13:27:49 -0300 Subject: [PATCH 16/25] fix(xcat-core): the ipxe shim case leaves a node configured for installation ipxe_secureboot_shim.sh reads every attribute and the nodeset state before the first mutation, refuses a state it cannot put back, and replays the saved state verbatim. read_attr keeps the exit status of lsdef instead of the status of the sed that follows it, so a lookup that failed no longer reads as an attribute that is unset. must_read ends the case on a failed lookup, before the chdef and the nodeset that change the node. This is what stops the restoration clearing a provmethod or a netboot method the node really had. The restoration replays the whole string nodeset stat reported, so a state that carries an argument goes back with it. A state the helper cannot replay ends the case while the node is untouched: install, netboot and statelite are deprecated and the restoring nodeset rejects them, and iscsiboot, image, winshell and sysclone read rows this case never saved. The restoring chdef also puts back nodetype.profile, which nodeset osimage= overwrites from the osimage row. xCAT-test/bats/ipxe_secureboot_shim_restore.bats covers it. Against the previous commit the four refusal cases exit 0 and print IPXE_SHIM_OK after calling nodeset osimage= on the node. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../testcase/nodeset/ipxe_secureboot_shim.sh | 94 ++++++++++++++----- 1 file changed, 71 insertions(+), 23 deletions(-) diff --git a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh index 33ee9b8142..414c82a00a 100755 --- a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -15,15 +15,33 @@ if [ -z "$node" ]; then exit 1 fi -site_value() { - lsdef -t site -i "$1" 2>/dev/null | sed -n "s/^[[:space:]]*$1=//p" -} -node_value() { - lsdef -t node -o "$node" -i "$1" 2>/dev/null | sed -n "s/^[[:space:]]*$1=//p" +# lsdef prints nothing for an attribute that is unset, and a pipeline reports the exit status of +# its last command, so "lsdef | sed" answered "unset" for a lookup that failed. The restoration +# writes back what it read, so a failed lookup made this case delete a value the node had. Keep +# the status of lsdef itself, and leave the value in $lookup. +lookup= +read_attr() { + local out + if [ "$1" = site ]; then + out=$(lsdef -t site -i "$2" 2>&1) || return 1 + else + out=$(lsdef -t node -o "$node" -i "$2" 2>&1) || return 1 + fi + lookup=$(printf '%s\n' "$out" | sed -n "s/^[[:space:]]*$2=//p" | head -1) + return 0 } -netboot=$(node_value netboot) -arch=$(node_value arch) +# Every lookup runs before the first mutation, so a failed one ends the case with nothing to put +# back. +must_read() { + read_attr "$1" "$2" && return 0 + echo "IPXE_SHIM_FAIL: lsdef -t $1 -i $2 for $node returned non-zero, and a lookup that failed" + echo "IPXE_SHIM_FAIL: is not an attribute that is unset, so this case changes nothing" + exit 1 +} + +must_read node arch +arch=$lookup # The case file declares arch:x86, so xcattest does not run this on another architecture. A hand # run can still reach it, and a case that cannot measure must not report a pass. if [ "$arch" != "x86_64" ]; then @@ -31,15 +49,49 @@ if [ "$arch" != "x86_64" ]; then exit 1 fi -tftpdir=$(site_value tftpdir) +must_read node netboot +netboot=$lookup +must_read node os +os=$lookup +must_read node provmethod +provmethod=$lookup +# nodeset osimage= writes nodetype.provmethod, profile, os and arch from the osimage row it +# resolved. On the path below the image is named --install-compute, so os and arch come +# back as the values read here; profile becomes the row's profile and has to be put back. +must_read node profile +profile=$lookup + +must_read site tftpdir +tftpdir=$lookup [ -n "$tftpdir" ] || tftpdir=/tftpboot -installdir=$(site_value installdir) +must_read site installdir +installdir=$lookup [ -n "$installdir" ] || installdir=/install uefi="$tftpdir/xcat/ipxe/nodes/$node.uefi" -before=$(nodeset "$node" stat 2>/dev/null | sed -n "s/^$node: *//p" | head -1) -provmethod=$(node_value provmethod) -echo "nodeset state of $node before this case: ${before:-unknown}, provmethod=${provmethod:-unset}" +if ! stat_out=$(nodeset "$node" stat 2>&1); then + echo "IPXE_SHIM_FAIL: nodeset $node stat returned non-zero, so this case cannot read the" + echo "IPXE_SHIM_FAIL: state it has to put back, and it changes nothing" + printf '%s\n' "$stat_out" + exit 1 +fi +before=$(printf '%s\n' "$stat_out" | sed -n "s/^$node: *//p" | head -1) +echo "nodeset state of $node before this case: ${before:-empty}, provmethod=${provmethod:-unset}" + +# nodeset stat reports chain.currstate verbatim, and a state can carry an argument, so the +# restoration replays the whole string. A state outside this list cannot be replayed: install, +# netboot and statelite are deprecated and the restoring nodeset rejects them, and iscsiboot, +# image, winshell and sysclone read rows this case never saved. The node would keep the +# installation this case configures, so refuse while it is still untouched. +case "$before" in + boot | offline | shell | shutdown | standby | osimage) ;; + osimage=?* | runcmd=?* | runimage=?*) ;; + *) + echo "IPXE_SHIM_FAIL: nodeset reports $node in state [${before:-empty}], which this case" + echo "IPXE_SHIM_FAIL: cannot put back, so it changes nothing" + exit 1 + ;; +esac # The case reconfigures the node for installation. A restoration that fails leaves it that way, # so the failure has to reach the caller in place of the result of the case. @@ -58,19 +110,15 @@ restore_state() { fi fi if [ -z "$provmethod" ]; then - if ! chdef -t node -o "$node" provmethod= > /dev/null; then - echo "IPXE_SHIM_FAIL: chdef -t node -o $node provmethod= returned non-zero, $node keeps the provmethod this case set" + if ! chdef -t node -o "$node" provmethod= "profile=$profile" > /dev/null; then + echo "IPXE_SHIM_FAIL: chdef -t node -o $node provmethod= profile=$profile returned non-zero, $node keeps the image attributes this case set" restore_rc=1 fi fi - case "${before%% *}" in - boot | offline | shell | standby) - if ! nodeset "$node" "${before%% *}"; then - echo "IPXE_SHIM_FAIL: nodeset $node ${before%% *} returned non-zero, $node stays configured for installation" - restore_rc=1 - fi - ;; - esac + if ! nodeset "$node" "$before"; then + echo "IPXE_SHIM_FAIL: nodeset $node $before returned non-zero, $node stays configured for installation" + restore_rc=1 + fi return "$restore_rc" } @@ -84,7 +132,7 @@ on_exit() { # the stateful image of the node here. In a bundle the provisioning cases have already set it. destiny=osimage if [ -z "$provmethod" ]; then - image="$(node_value os)-$arch-install-compute" + image="$os-$arch-install-compute" if ! lsdef -t osimage -o "$image" > /dev/null 2>&1; then echo "IPXE_SHIM_FAIL: $node has no provmethod and osimage $image is not defined, so this" echo "IPXE_SHIM_FAIL: case has no boot script to read" From 698bccf493e81e7c2e1cc61a3175bf34c29f4263 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Fri, 9 Oct 2026 13:30:47 -0300 Subject: [PATCH 17/25] fix(xcat-core): the netboot restore test cannot tell which chdef failed The bats stub returned one exit code for every "chdef netboot=", and the helper calls chdef twice: once to set the method to ipxe, and once to put the saved method back. With the stub failing both, "a netboot method that cannot be put back fails the case" passed on the FORWARD failure, so removing restore_rc=1 from the restoring branch left all 18 tests green. The test named a defect it could not detect. The stub now counts its netboot calls and fails the one the test names, so the forward chdef succeeds. The test asserts that forward call is in the call log, and matches the message only the restoring branch prints. Removing restore_rc=1 now fails test 8 and no other. docs/source/advanced/ipxe/index.rst said both methods run the same boot scripts, which stopped being true when the shim line became conditional on the method. It now says what differs, and states that Secure Boot reaches netboot=ipxe only: xnba.efi is unsigned, and grub2 nodes are out of scope. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- docs/source/advanced/ipxe/index.rst | 9 +++++++- .../bats/ipxe_secureboot_shim_restore.bats | 21 +++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/docs/source/advanced/ipxe/index.rst b/docs/source/advanced/ipxe/index.rst index f605eabaaa..40df83bcb3 100644 --- a/docs/source/advanced/ipxe/index.rst +++ b/docs/source/advanced/ipxe/index.rst @@ -6,7 +6,14 @@ xCAT boots x86 nodes with iPXE through two ``netboot`` methods: * ``ipxe``: the unmodified iPXE release in the ``ipxe-xcat`` package. * ``xnba``: the xCAT Network Boot Agent in the ``xnba-undi`` package, a patched iPXE. This method is deprecated, and a later release removes it. -Both methods run the same boot scripts. ``nodeset`` writes them under ``xcat/ipxe/nodes`` for ``ipxe`` and under ``xcat/xnba/nodes`` for ``xnba``. The xCAT packages install both ``ipxe-xcat`` and ``xnba-undi``. +The two methods share the script writer, so the scripts are the same except where the method +decides otherwise. ``nodeset`` writes them under ``xcat/ipxe/nodes`` for ``ipxe`` and under +``xcat/xnba/nodes`` for ``xnba``. The xCAT packages install both ``ipxe-xcat`` and ``xnba-undi``. + +**Secure Boot applies to ``netboot=ipxe`` only.** A node with ``netboot=ipxe`` gets a ``shim`` +line before the kernel, so the firmware verifies what it loads. A node with ``netboot=xnba`` +does not: ``xnba.efi`` is unsigned, and no shim can chain to it. ``grub2`` and ``grub2-http`` +nodes, which is every ppc64le, aarch64 and riscv64 node, are outside this chapter. New x86 nodes get ``netboot=ipxe``: node discovery, profile-based node definitions and the x86 node templates of ``mkdef --template`` set it. diff --git a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats index a5b7d617eb..614dcdf6ef 100644 --- a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -37,7 +37,9 @@ setup() export STUB_LSDEF_FAIL_ATTR= export STUB_NODESET_RESTORE_RC=0 export STUB_CHDEF_RC=0 - export STUB_CHDEF_NETBOOT_RC=0 + # Which chdef netboot= call to fail, counted from 1. The helper sets the method and later + # puts it back, so one return code for both cannot say which call failed. + export STUB_CHDEF_NETBOOT_FAIL_NTH=0 # The netboot the node is declared with. Four of the six confs that carry this case declare # their compute node netboot=xnba, so this is not a hypothetical value. export STUB_NETBOOT=ipxe @@ -46,6 +48,7 @@ setup() export STUB_UEFI="$TFTPDIR/xcat/ipxe/nodes/${NODE}.uefi" export STUB_SHIM_LINE="shim http://\${next-server}/install/$OS/x86_64/EFI/BOOT/BOOTX64.EFI" export STUB_CALLS="$CALLS" + export STUB_NETBOOT_COUNT="$BATS_TEST_TMPDIR/netboot-count" export STUB_TFTPDIR="$TFTPDIR" export STUB_INSTALLDIR="$INSTALLDIR" export STUB_OS="$OS" @@ -113,7 +116,12 @@ STUB #!/bin/bash echo "chdef $*" >>"$STUB_CALLS" case "$*" in - *netboot=*) exit "$STUB_CHDEF_NETBOOT_RC" ;; + *netboot=*) + n=$(( $(cat "$STUB_NETBOOT_COUNT" 2>/dev/null || echo 0) + 1 )) + echo "$n" >"$STUB_NETBOOT_COUNT" + [ "$n" = "$STUB_CHDEF_NETBOOT_FAIL_NTH" ] && exit 1 + exit 0 + ;; esac exit "$STUB_CHDEF_RC" STUB @@ -198,11 +206,16 @@ STUB # which is the same defect the provmethod and nodeset restorations already report. @test "a netboot method that cannot be put back fails the case" { STUB_NETBOOT=xnba - STUB_CHDEF_NETBOOT_RC=1 + # Fail the SECOND netboot chdef, which is the restoring one. Failing both cannot tell a + # broken restoration from a forward chdef that never ran. + STUB_CHDEF_NETBOOT_FAIL_NTH=2 run "$SCRIPT" "$NODE" [ "$status" -ne 0 ] [[ "$output" != *IPXE_SHIM_OK* ]] - [[ "$output" == *IPXE_SHIM_FAIL* ]] + # The forward chdef must have succeeded, or this measures the wrong call. + grep -q "^chdef -t node -o $NODE netboot=ipxe$" "$CALLS" + # Only the restoring branch prints this; the forward one does not. + [[ "$output" == *"keeps the netboot method this case set"* ]] } # The case file now requires the OK token, so an exit 0 without it is a pass the run cannot From 18880ca619338406a966d4b034961e6ee4f9cff9 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Tue, 13 Oct 2026 12:03:48 -0300 Subject: [PATCH 18/25] test(xcat-core): the ipxe shim case loses the pending chain and image attributes of a node nodeset osimage= sets chain.currchain to boot and copies os, arch and profile from the osimage row into the node. The cleanup of ipxe_secureboot_shim.sh replays only chain.currstate, and restores profile only when provmethod was empty. A node at runcmd=bmcsetup with an osimage action queued after it loses that action, and a node whose image row differs from its own os, arch or profile keeps the values of the row. The case still prints IPXE_SHIM_OK. The stubs now keep the node attributes and osimage rows in a store, and the tests assert the values the node ends with. Five tests fail on the current helper. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../bats/ipxe_secureboot_shim_restore.bats | 185 ++++++++++++++++-- 1 file changed, 164 insertions(+), 21 deletions(-) diff --git a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats index 614dcdf6ef..265cc3aa1c 100644 --- a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -6,16 +6,34 @@ # the next case on that node measures the state this one left. So the helper has to report a # failed restoration instead of its own result. # -# lsdef, nodeset and chdef are stubbed, and every call is recorded in a log the tests read. A -# stub can also be told to fail: STUB_STAT_RC fails "nodeset stat", and STUB_LSDEF_FAIL_ATTR -# fails the lsdef of one attribute. Both failures print nothing on stdout, which is what an -# attribute that is unset prints too. +# lsdef, nodeset and chdef are stubbed, and every call is recorded in a log the tests read. The +# stubs share a store of node attributes and osimage rows in the test directory, so a test can +# assert the values the node ends with. On its first call a stub fills the store from the STUB_* +# variables. A stub can also be told to fail: STUB_STAT_RC fails "nodeset stat", and +# STUB_LSDEF_FAIL_ATTR fails the lsdef of one attribute. Both failures print nothing on stdout, +# which is what an attribute that is unset prints too. load 'helpers/shell_source' NODE=cn01 OS=alma9.8 +# Write one osimage row. nodeset osimage= copies osvers, osarch and profile from this row into +# the node, whatever the image name says. +define_image() +{ + local dir="$STUB_STORE/osimage/$1" + mkdir -p "$dir" + printf '%s' "$2" >"$dir/osvers" + printf '%s' "$3" >"$dir/osarch" + printf '%s' "$4" >"$dir/profile" +} + +node_attr() +{ + cat "$STUB_STORE/node/$1" +} + setup() { SCRIPT="$(require_repo_file 'xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh')" @@ -31,6 +49,7 @@ setup() # The nodeset state the helper has to put back, and the provmethod it finds. An empty # provmethod is what sends the helper through chdef on the way out. export STUB_BEFORE=boot + export STUB_CURRCHAIN= export STUB_PROVMETHOD= export STUB_PROFILE=compute export STUB_STAT_RC=0 @@ -52,10 +71,38 @@ setup() export STUB_TFTPDIR="$TFTPDIR" export STUB_INSTALLDIR="$INSTALLDIR" export STUB_OS="$OS" + export STUB_STORE="$BATS_TEST_TMPDIR/store" + export STUB_LIB="$BIN/../stublib.sh" + + cat >"$STUB_LIB" <<'STUB' +# The node starts with the STUB_* values. A test that defines its own osimage rows before the +# first call keeps them, and the default row is added only when STUB_NO_OSIMAGE is empty. +store_init() { + [ -d "$STUB_STORE/node" ] && return 0 + mkdir -p "$STUB_STORE/node" "$STUB_STORE/osimage" + printf '%s' "$STUB_NETBOOT" >"$STUB_STORE/node/netboot" + printf '%s' "$STUB_ARCH" >"$STUB_STORE/node/arch" + printf '%s' "$STUB_OS" >"$STUB_STORE/node/os" + printf '%s' "$STUB_PROVMETHOD" >"$STUB_STORE/node/provmethod" + printf '%s' "$STUB_PROFILE" >"$STUB_STORE/node/profile" + printf '%s' "$STUB_BEFORE" >"$STUB_STORE/node/currstate" + printf '%s' "$STUB_CURRCHAIN" >"$STUB_STORE/node/currchain" + if [ -z "$STUB_NO_OSIMAGE" ] && [ ! -d "$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute" ]; then + mkdir -p "$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute" + printf '%s' "$STUB_OS" >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/osvers" + printf '%s' x86_64 >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/osarch" + printf '%s' compute >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/profile" + fi +} +get() { cat "$STUB_STORE/node/$1" 2>/dev/null; } +put() { printf '%s' "$2" >"$STUB_STORE/node/$1"; } +store_init +STUB cat >"$BIN/lsdef" <<'STUB' #!/bin/bash echo "lsdef $*" >>"$STUB_CALLS" +. "$STUB_LIB" type=; obj=; attr= while [ $# -gt 0 ]; do case "$1" in @@ -74,28 +121,41 @@ fi case "$type/$attr" in site/tftpdir) echo " tftpdir=$STUB_TFTPDIR" ;; site/installdir) echo " installdir=$STUB_INSTALLDIR" ;; - node/netboot) echo " netboot=$STUB_NETBOOT" ;; - node/arch) echo " arch=$STUB_ARCH" ;; - node/os) echo " os=$STUB_OS" ;; - node/provmethod) [ -n "$STUB_PROVMETHOD" ] && echo " provmethod=$STUB_PROVMETHOD" ;; - node/profile) [ -n "$STUB_PROFILE" ] && echo " profile=$STUB_PROFILE" ;; - osimage/) [ -n "$STUB_NO_OSIMAGE" ] && exit 1; exit 0 ;; + node/netboot | node/arch | node/os | node/provmethod | node/profile | node/currchain) + v=$(get "$attr") + [ -n "$v" ] && echo " $attr=$v" + ;; + osimage/) [ -d "$STUB_STORE/osimage/$obj" ] || exit 1 ;; *) echo "unexpected lsdef $type $obj $attr" >&2; exit 1 ;; esac exit 0 STUB - # nodeset stat reports the state; osimage= writes the UEFI script; any destiny name is the - # restoration call, and its exit status is what these tests drive. + # nodeset osimage[=] does what setdestiny does to the node: it writes provmethod, os, + # arch and profile from the osimage row, sets currchain to boot, and writes the UEFI script. + # Any other destiny sets currstate, and the actions after its first comma become currchain. cat >"$BIN/nodeset" <<'STUB' #!/bin/bash echo "nodeset $*" >>"$STUB_CALLS" +. "$STUB_LIB" case "$2" in stat) [ "$STUB_STAT_RC" = 0 ] || { echo "Error: cannot reach the server" >&2; exit "$STUB_STAT_RC"; } - echo "$1: $STUB_BEFORE" + echo "$1: $(get currstate)" + exit 0 ;; - osimage=*) + osimage | osimage=*) + image=${2#osimage} + image=${image#=} + [ -n "$image" ] || image=$(get provmethod) + row="$STUB_STORE/osimage/$image" + [ -n "$image" ] && [ -d "$row" ] || { echo "Error: Cannot find the OS image $image" >&2; exit 1; } + put provmethod "$image" + put os "$(cat "$row/osvers")" + put arch "$(cat "$row/osarch")" + put profile "$(cat "$row/profile")" + put currchain boot + put currstate "install $(get os)-$(get arch)-$(get profile)" { echo '#!gpxe' echo 'imgfetch -n kernel http://${next-server}/tftpboot/xcat/osimage/vmlinuz' @@ -104,26 +164,36 @@ case "$2" in echo 'imgargs kernel quiet' echo 'imgexec kernel' } >"$STUB_UEFI" + exit 0 ;; *) - exit "$STUB_NODESET_RESTORE_RC" + [ "$STUB_NODESET_RESTORE_RC" = 0 ] || exit "$STUB_NODESET_RESTORE_RC" + put currstate "${2%%,*}" + case "$2" in *,*) put currchain "${2#*,}" ;; esac + exit 0 ;; esac -exit 0 STUB cat >"$BIN/chdef" <<'STUB' #!/bin/bash echo "chdef $*" >>"$STUB_CALLS" +. "$STUB_LIB" case "$*" in *netboot=*) n=$(( $(cat "$STUB_NETBOOT_COUNT" 2>/dev/null || echo 0) + 1 )) echo "$n" >"$STUB_NETBOOT_COUNT" [ "$n" = "$STUB_CHDEF_NETBOOT_FAIL_NTH" ] && exit 1 - exit 0 + ;; + *) + [ "$STUB_CHDEF_RC" = 0 ] || exit "$STUB_CHDEF_RC" ;; esac -exit "$STUB_CHDEF_RC" +shift 4 +for kv in "$@"; do + put "${kv%%=*}" "${kv#*=}" +done +exit 0 STUB chmod 0755 "$BIN"/* @@ -135,7 +205,9 @@ STUB [ "$status" -eq 0 ] [[ "$output" == *IPXE_SHIM_OK* ]] grep -q "^nodeset $NODE boot$" "$CALLS" - grep -q "^chdef -t node -o $NODE provmethod= profile=compute$" "$CALLS" + [ "$(node_attr provmethod)" = "" ] + [ "$(node_attr profile)" = compute ] + [ "$(node_attr currstate)" = boot ] } @test "a nodeset that cannot put the state back fails the case" { @@ -153,7 +225,7 @@ STUB [ "$status" -ne 0 ] [[ "$output" != *IPXE_SHIM_OK* ]] [[ "$output" == *IPXE_SHIM_FAIL* ]] - grep -q "^chdef -t node -o $NODE provmethod= profile=compute$" "$CALLS" + grep -q "^chdef -t node -o $NODE provmethod=" "$CALLS" } # A control: the helper already restores the state when its own assertion fails. It holds the @@ -225,6 +297,7 @@ STUB for netboot in ipxe xnba; do for noimage in "" 1; do : >"$CALLS" + rm -rf "$STUB_STORE" STUB_ARCH=$arch STUB_NETBOOT=$netboot STUB_NO_OSIMAGE=$noimage \ run "$SCRIPT" "$NODE" if [ "$status" -eq 0 ] && [[ "$output" != *IPXE_SHIM_OK* ]]; then @@ -254,6 +327,7 @@ STUB # forward call and cannot fail. @test "an osimage state goes back with its image name" { STUB_BEFORE="osimage=$OS-x86_64-netboot-compute" + define_image "$OS-x86_64-netboot-compute" "$OS" x86_64 compute run "$SCRIPT" "$NODE" [ "$status" -eq 0 ] [[ "$output" == *IPXE_SHIM_OK* ]] @@ -335,7 +409,8 @@ STUB run "$SCRIPT" "$NODE" [ "$status" -eq 0 ] [[ "$output" == *IPXE_SHIM_OK* ]] - grep -q "^chdef -t node -o $NODE provmethod= profile=service$" "$CALLS" + [ "$(node_attr profile)" = service ] + [ "$(node_attr provmethod)" = "" ] } # An arch that cannot be read is not an arch that is wrong, and the message has to say which. @@ -346,3 +421,71 @@ STUB [[ "$output" == *"-i arch"* ]] [[ "$output" != *"only x86_64 has a UEFI shim"* ]] } + +# nodeset osimage= sets chain.currchain to boot. Replaying the saved currstate does not touch +# currchain, so a node at runcmd=bmcsetup with an installation queued after it lost that step and +# the case still reported a pass. +@test "a pending chain after a runcmd state goes back" { + STUB_BEFORE='runcmd=bmcsetup' + STUB_CURRCHAIN="osimage=$OS-x86_64-install-compute" + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + [ "$(node_attr currstate)" = runcmd=bmcsetup ] + [ "$(node_attr currchain)" = "osimage=$OS-x86_64-install-compute" ] +} + +# With a provmethod set, the helper runs nodeset osimage on that image, and the osimage row +# writes its own profile into the node. The node profile is not the profile of its image here. +@test "the profile goes back when the provmethod image has another profile" { + define_image "$OS-x86_64-install-service" "$OS" x86_64 service + STUB_PROVMETHOD="$OS-x86_64-install-service" + STUB_PROFILE=compute + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + [ "$(node_attr provmethod)" = "$OS-x86_64-install-service" ] + [ "$(node_attr profile)" = compute ] + [ "$(node_attr os)" = "$OS" ] + [ "$(node_attr arch)" = x86_64 ] + [ "$(node_attr currstate)" = boot ] + [ "$(node_attr currchain)" = "" ] +} + +# nodeset copies osvers and osarch from the osimage row, not from the image name. An image named +# -x86_64-install-compute can carry another osvers, and the node os must still go back. +@test "os and arch go back when the image row differs from its name" { + define_image "$OS-x86_64-install-compute" rhels9.6 x86_64 service + STUB_PROVMETHOD= + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + [ "$(node_attr os)" = "$OS" ] + [ "$(node_attr arch)" = x86_64 ] + [ "$(node_attr profile)" = compute ] + [ "$(node_attr provmethod)" = "" ] +} + +# The same row with a provmethod already set: both paths restore the image attributes. +@test "os goes back on the provmethod path when the image row differs from its name" { + define_image "$OS-x86_64-install-compute" rhels9.6 x86_64 compute + STUB_PROVMETHOD="$OS-x86_64-install-compute" + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + [ "$(node_attr os)" = "$OS" ] + [ "$(node_attr provmethod)" = "$OS-x86_64-install-compute" ] +} + +# A restoration of the image attributes or the chain that fails leaves the node changed, so it +# fails the case like the other restorations do. +@test "a chdef that cannot restore the pending chain fails the case" { + STUB_BEFORE='runcmd=bmcsetup' + STUB_CURRCHAIN="osimage=$OS-x86_64-install-compute" + STUB_PROVMETHOD="$OS-x86_64-install-compute" + STUB_CHDEF_RC=1 + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + [[ "$output" == *IPXE_SHIM_FAIL* ]] +} From dad3d6c6d3b711f5b07f4ee735054af54072d732 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Tue, 13 Oct 2026 12:04:54 -0300 Subject: [PATCH 19/25] fix(xcat-core): the ipxe shim case loses the pending chain and image attributes of a node nodeset osimage= sets chain.currchain to boot and copies os, arch and profile from the osimage row into the node. The cleanup replayed only chain.currstate, and restored profile only when provmethod was empty. A node at runcmd=bmcsetup lost the osimage action queued after it, and a node whose image row differed from its own values kept the values of the row, while the case printed IPXE_SHIM_OK. The helper now reads currchain with the other attributes before the first mutation. After the replay it writes back provmethod, os, arch, profile and currchain as it read them, on both paths. A failed write fails the case without IPXE_SHIM_OK. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../testcase/nodeset/ipxe_secureboot_shim.sh | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh index 414c82a00a..2a65af9ce6 100755 --- a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -53,13 +53,14 @@ must_read node netboot netboot=$lookup must_read node os os=$lookup +# nodeset osimage= writes provmethod, os, arch and profile from the osimage row, and sets +# chain.currchain to boot. The replay of the saved state writes neither back. must_read node provmethod provmethod=$lookup -# nodeset osimage= writes nodetype.provmethod, profile, os and arch from the osimage row it -# resolved. On the path below the image is named --install-compute, so os and arch come -# back as the values read here; profile becomes the row's profile and has to be put back. must_read node profile profile=$lookup +must_read node currchain +currchain=$lookup must_read site tftpdir tftpdir=$lookup @@ -109,16 +110,17 @@ restore_state() { restore_rc=1 fi fi - if [ -z "$provmethod" ]; then - if ! chdef -t node -o "$node" provmethod= "profile=$profile" > /dev/null; then - echo "IPXE_SHIM_FAIL: chdef -t node -o $node provmethod= profile=$profile returned non-zero, $node keeps the image attributes this case set" - restore_rc=1 - fi - fi if ! nodeset "$node" "$before"; then echo "IPXE_SHIM_FAIL: nodeset $node $before returned non-zero, $node stays configured for installation" restore_rc=1 fi + # After the replay, because a replayed osimage= state writes the image attributes again. + attrs="provmethod=$provmethod os=$os arch=$arch profile=$profile currchain=$currchain" + if ! chdef -t node -o "$node" "provmethod=$provmethod" "os=$os" "arch=$arch" \ + "profile=$profile" "currchain=$currchain" > /dev/null; then + echo "IPXE_SHIM_FAIL: chdef -t node -o $node $attrs returned non-zero, $node keeps the image attributes and chain this case set" + restore_rc=1 + fi return "$restore_rc" } From a55db29fa6b62b4a310c0d100fa82553b5187192 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Tue, 13 Oct 2026 16:05:48 -0300 Subject: [PATCH 20/25] test(xcat-core): cover Genesis boot files written with the image arch When the forward nodeset osimage writes the image attributes and then fails, the node keeps the arch of the image. ipxe_secureboot_shim.sh replays the saved shell, standby or runcmd state before it writes the saved arch back, so nodeset writes the Genesis kernel and initrd of the image arch. The chdef that follows corrects the database, not those boot files. The nodeset stub now records the Genesis kernel and initrd that a shell, standby or runcmd state selects from the current node arch, and can fail nodeset osimage after it updates the attributes. Two cases start an x86_64 node at shell and at runcmd=bmcsetup with a ppc64le image, and assert the x86_64 kernel and initrd. Both fail against the current helper. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../bats/ipxe_secureboot_shim_restore.bats | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats index 265cc3aa1c..d311abea18 100644 --- a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -56,6 +56,8 @@ setup() export STUB_LSDEF_FAIL_ATTR= export STUB_NODESET_RESTORE_RC=0 export STUB_CHDEF_RC=0 + # Fail the forward nodeset osimage after it wrote the image attributes into the node. + export STUB_NODESET_FORWARD_FAIL= # Which chdef netboot= call to fail, counted from 1. The helper sets the method and later # puts it back, so one return code for both cannot say which call failed. export STUB_CHDEF_NETBOOT_FAIL_NTH=0 @@ -73,6 +75,7 @@ setup() export STUB_OS="$OS" export STUB_STORE="$BATS_TEST_TMPDIR/store" export STUB_LIB="$BIN/../stublib.sh" + export STUB_BOOTCFG="$BATS_TEST_TMPDIR/bootcfg" cat >"$STUB_LIB" <<'STUB' # The node starts with the STUB_* values. A test that defines its own osimage rows before the @@ -134,6 +137,7 @@ STUB # nodeset osimage[=] does what setdestiny does to the node: it writes provmethod, os, # arch and profile from the osimage row, sets currchain to boot, and writes the UEFI script. # Any other destiny sets currstate, and the actions after its first comma become currchain. + # shell, standby and runcmd also write a Genesis boot configuration for the current arch. cat >"$BIN/nodeset" <<'STUB' #!/bin/bash echo "nodeset $*" >>"$STUB_CALLS" @@ -156,6 +160,7 @@ case "$2" in put profile "$(cat "$row/profile")" put currchain boot put currstate "install $(get os)-$(get arch)-$(get profile)" + [ -z "$STUB_NODESET_FORWARD_FAIL" ] || { echo "Error: cannot write the boot files" >&2; exit 1; } { echo '#!gpxe' echo 'imgfetch -n kernel http://${next-server}/tftpboot/xcat/osimage/vmlinuz' @@ -170,6 +175,13 @@ case "$2" in [ "$STUB_NODESET_RESTORE_RC" = 0 ] || exit "$STUB_NODESET_RESTORE_RC" put currstate "${2%%,*}" case "$2" in *,*) put currchain "${2#*,}" ;; esac + # shell, standby and runcmd boot Genesis, and nodeset selects its kernel and initrd from + # the arch of the node at the time of the call. + case "${2%%,*}" in + shell | standby | runcmd=*) + printf 'kernel genesis.kernel.%s\ninitrd genesis.fs.%s\n' "$(get arch)" "$(get arch)" >"$STUB_BOOTCFG" + ;; + esac exit 0 ;; esac @@ -489,3 +501,39 @@ STUB [[ "$output" != *IPXE_SHIM_OK* ]] [[ "$output" == *IPXE_SHIM_FAIL* ]] } + +# nodeset osimage writes the image attributes before it writes the boot files, so a forward call +# that fails can leave the node with the arch of the image. A replay of shell, standby or runcmd +# then writes the Genesis kernel and initrd of that arch, and a later chdef of the database does +# not rewrite those files. +@test "a Genesis state goes back with the kernel and initrd of the original arch" { + define_image "$OS-ppc64le-install-compute" "$OS" ppc64le compute + STUB_BEFORE=shell + STUB_PROVMETHOD="$OS-ppc64le-install-compute" + STUB_NODESET_FORWARD_FAIL=1 + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + grep -q "^nodeset $NODE shell$" "$CALLS" + grep -qx 'kernel genesis.kernel.x86_64' "$STUB_BOOTCFG" + grep -qx 'initrd genesis.fs.x86_64' "$STUB_BOOTCFG" + [ "$(node_attr arch)" = x86_64 ] + [ "$(node_attr provmethod)" = "$OS-ppc64le-install-compute" ] + [ "$(node_attr currstate)" = shell ] +} + +# The same for a runcmd state, which also restores the pending chain after the replay. +@test "a runcmd state goes back with the kernel and initrd of the original arch" { + define_image "$OS-ppc64le-install-compute" "$OS" ppc64le compute + STUB_BEFORE='runcmd=bmcsetup' + STUB_CURRCHAIN=standby + STUB_PROVMETHOD="$OS-ppc64le-install-compute" + STUB_NODESET_FORWARD_FAIL=1 + run "$SCRIPT" "$NODE" + [ "$status" -ne 0 ] + [[ "$output" != *IPXE_SHIM_OK* ]] + grep -qx 'kernel genesis.kernel.x86_64' "$STUB_BOOTCFG" + grep -qx 'initrd genesis.fs.x86_64' "$STUB_BOOTCFG" + [ "$(node_attr arch)" = x86_64 ] + [ "$(node_attr currchain)" = standby ] +} From 9ba75fc9aa0ea857d5cc41612b8e6df515272f63 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Tue, 13 Oct 2026 16:06:27 -0300 Subject: [PATCH 21/25] fix(xcat-core): the ipxe shim case writes Genesis boot files for the image arch When the forward nodeset osimage wrote the image attributes and then failed, the restoration replayed a saved shell, standby or runcmd state while the node still had the arch of the image. nodeset then wrote the Genesis kernel and initrd of that arch. The chdef after the replay corrected the database, not the boot files. restore_state now writes provmethod, os, arch and profile back before the replay. After the replay it writes them again only for an osimage state, which sets them from the image row, and then writes currchain. A failed chdef still makes the case fail without IPXE_SHIM_OK. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../testcase/nodeset/ipxe_secureboot_shim.sh | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh index 2a65af9ce6..5cbb56678d 100755 --- a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -99,6 +99,14 @@ esac restore_done=0 restore_rc=0 netboot_set= +restore_image_attrs() { + local attrs="provmethod=$provmethod os=$os arch=$arch profile=$profile" + if ! chdef -t node -o "$node" "provmethod=$provmethod" "os=$os" "arch=$arch" \ + "profile=$profile" > /dev/null; then + echo "IPXE_SHIM_FAIL: chdef -t node -o $node $attrs returned non-zero, $node keeps the image attributes this case set" + restore_rc=1 + fi +} restore_state() { [ "$restore_done" = 1 ] && return "$restore_rc" restore_done=1 @@ -110,15 +118,20 @@ restore_state() { restore_rc=1 fi fi + # Before the replay too: shell, standby and runcmd write Genesis boot files for the node arch, + # and a forward nodeset osimage that failed can leave the arch of the image. + restore_image_attrs if ! nodeset "$node" "$before"; then echo "IPXE_SHIM_FAIL: nodeset $node $before returned non-zero, $node stays configured for installation" restore_rc=1 fi - # After the replay, because a replayed osimage= state writes the image attributes again. - attrs="provmethod=$provmethod os=$os arch=$arch profile=$profile currchain=$currchain" - if ! chdef -t node -o "$node" "provmethod=$provmethod" "os=$os" "arch=$arch" \ - "profile=$profile" "currchain=$currchain" > /dev/null; then - echo "IPXE_SHIM_FAIL: chdef -t node -o $node $attrs returned non-zero, $node keeps the image attributes and chain this case set" + # A replayed osimage state writes the image attributes again. + case "$before" in + osimage | osimage=*) restore_image_attrs ;; + esac + # Last, because a replayed state can set the chain. + if ! chdef -t node -o "$node" "currchain=$currchain" > /dev/null; then + echo "IPXE_SHIM_FAIL: chdef -t node -o $node currchain=$currchain returned non-zero, $node keeps the chain this case set" restore_rc=1 fi return "$restore_rc" From dd3f24770d3e1a41272e1c73de46bd417ce8cd7a Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 14 Oct 2026 12:40:48 -0300 Subject: [PATCH 22/25] test(xcat-core): the ipxe shim case loses the provmethod of a node in boot nodeset boot clears nodetype.provmethod when the image of the node is a netboot or statelite image. The helper restores the image attributes after the replay of an osimage state only, so a node in boot loses its image assignment and the case still reports IPXE_SHIM_OK. The nodeset stub now clears provmethod on boot unless the osimage row resolves to install or sysclone, as destiny.pm does. Two tests start a node in boot with a netboot and a statelite image and assert that the image attributes and the pending chain survive. A control test holds the stub to the product behavior for an install image. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../bats/ipxe_secureboot_shim_restore.bats | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats index d311abea18..0d9062b38a 100644 --- a/xCAT-test/bats/ipxe_secureboot_shim_restore.bats +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -19,7 +19,8 @@ NODE=cn01 OS=alma9.8 # Write one osimage row. nodeset osimage= copies osvers, osarch and profile from this row into -# the node, whatever the image name says. +# the node, whatever the image name says. The fifth argument is osimage.provmethod, which +# nodeset boot reads, and it defaults to install. define_image() { local dir="$STUB_STORE/osimage/$1" @@ -27,6 +28,7 @@ define_image() printf '%s' "$2" >"$dir/osvers" printf '%s' "$3" >"$dir/osarch" printf '%s' "$4" >"$dir/profile" + printf '%s' "${5:-install}" >"$dir/provmethod" } node_attr() @@ -95,6 +97,7 @@ store_init() { printf '%s' "$STUB_OS" >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/osvers" printf '%s' x86_64 >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/osarch" printf '%s' compute >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/profile" + printf '%s' install >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/provmethod" fi } get() { cat "$STUB_STORE/node/$1" 2>/dev/null; } @@ -138,6 +141,8 @@ STUB # arch and profile from the osimage row, sets currchain to boot, and writes the UEFI script. # Any other destiny sets currstate, and the actions after its first comma become currchain. # shell, standby and runcmd also write a Genesis boot configuration for the current arch. + # boot clears nodetype.provmethod unless it resolves, through the osimage row, to install or + # sysclone (destiny.pm, "blank out the nodetype.provmethod"). cat >"$BIN/nodeset" <<'STUB' #!/bin/bash echo "nodeset $*" >>"$STUB_CALLS" @@ -175,6 +180,17 @@ case "$2" in [ "$STUB_NODESET_RESTORE_RC" = 0 ] || exit "$STUB_NODESET_RESTORE_RC" put currstate "${2%%,*}" case "$2" in *,*) put currchain "${2#*,}" ;; esac + if [ "${2%%,*}" = boot ] && [ -n "$(get provmethod)" ]; then + pm=$(get provmethod) + case "$pm" in + install | netboot | statelite) ;; + *) [ -s "$STUB_STORE/osimage/$pm/provmethod" ] && pm=$(cat "$STUB_STORE/osimage/$pm/provmethod") ;; + esac + case "$pm" in + install | sysclone) ;; + *) put provmethod "" ;; + esac + fi # shell, standby and runcmd boot Genesis, and nodeset selects its kernel and initrd from # the arch of the node at the time of the call. case "${2%%,*}" in @@ -537,3 +553,52 @@ STUB [ "$(node_attr arch)" = x86_64 ] [ "$(node_attr currchain)" = standby ] } + +# nodeset boot clears nodetype.provmethod when the image of the node is a netboot or statelite +# image. The helper restored the image attributes after the replay of an osimage state only, so +# a node in boot lost its image assignment and the case still reported a pass. +@test "a node in boot keeps its netboot image" { + define_image "$OS-x86_64-netboot-compute" "$OS" x86_64 compute netboot + STUB_BEFORE=boot + STUB_CURRCHAIN=standby + STUB_PROVMETHOD="$OS-x86_64-netboot-compute" + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + grep -q "^nodeset $NODE boot$" "$CALLS" + [ "$(node_attr provmethod)" = "$OS-x86_64-netboot-compute" ] + [ "$(node_attr os)" = "$OS" ] + [ "$(node_attr arch)" = x86_64 ] + [ "$(node_attr profile)" = compute ] + [ "$(node_attr currstate)" = boot ] + [ "$(node_attr currchain)" = standby ] +} + +@test "a node in boot keeps its statelite image" { + define_image "$OS-x86_64-statelite-compute" "$OS" x86_64 compute statelite + STUB_BEFORE=boot + STUB_CURRCHAIN=standby + STUB_PROVMETHOD="$OS-x86_64-statelite-compute" + run "$SCRIPT" "$NODE" + [ "$status" -eq 0 ] + [[ "$output" == *IPXE_SHIM_OK* ]] + grep -q "^nodeset $NODE boot$" "$CALLS" + [ "$(node_attr provmethod)" = "$OS-x86_64-statelite-compute" ] + [ "$(node_attr os)" = "$OS" ] + [ "$(node_attr arch)" = x86_64 ] + [ "$(node_attr profile)" = compute ] + [ "$(node_attr currstate)" = boot ] + [ "$(node_attr currchain)" = standby ] +} + +# A control for the stub: nodeset boot keeps the provmethod of an install image, so the two +# tests above fail on the clearing, not on a stub that clears every provmethod. +@test "the nodeset stub keeps the provmethod of an install image on boot" { + nodeset "$NODE" osimage="$OS-x86_64-install-compute" + nodeset "$NODE" boot + [ "$(node_attr provmethod)" = "$OS-x86_64-install-compute" ] + define_image "$OS-x86_64-netboot-compute" "$OS" x86_64 compute netboot + nodeset "$NODE" osimage="$OS-x86_64-netboot-compute" + nodeset "$NODE" boot + [ "$(node_attr provmethod)" = "" ] +} From 954162404d287419279022f805ba51c580c32e1b Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 14 Oct 2026 12:41:01 -0300 Subject: [PATCH 23/25] fix(xcat-core): the ipxe shim case loses the provmethod of a node in boot nodeset boot clears nodetype.provmethod when the image of the node is a netboot or statelite image. The helper restored the image attributes after the replay of an osimage state only, so a node in boot lost its image assignment while the case reported IPXE_SHIM_OK. The helper now restores the image attributes before the replay and again after every replay, then restores currchain last. A failed restoration still fails the case. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../autotest/testcase/nodeset/ipxe_secureboot_shim.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh index 5cbb56678d..224b378259 100755 --- a/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -125,10 +125,9 @@ restore_state() { echo "IPXE_SHIM_FAIL: nodeset $node $before returned non-zero, $node stays configured for installation" restore_rc=1 fi - # A replayed osimage state writes the image attributes again. - case "$before" in - osimage | osimage=*) restore_image_attrs ;; - esac + # A replayed osimage state writes the image attributes, and boot clears the provmethod of a + # netboot or statelite image. + restore_image_attrs # Last, because a replayed state can set the chain. if ! chdef -t node -o "$node" "currchain=$currchain" > /dev/null; then echo "IPXE_SHIM_FAIL: chdef -t node -o $node currchain=$currchain returned non-zero, $node keeps the chain this case set" From 72e3dd86020633f92841dfe6b6e3726cf7ce9557 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:38:33 -0300 Subject: [PATCH 24/25] refactor(kvm): move the UEFI firmware decisions into xCAT::KVMUtils kvm.pm made $parser, $node, $confdata and $updatetable package variables so that kvm_uefi_firmware.t could set them and call build_xmldesc and reconfigvm. That exposes plugin state to make it testable, which xCAT::KVMUtils replaced for the other guest decisions. xCAT::KVMUtils now holds vm_othersettings, requested_firmware, firmware_error, guest_os, clock_offset and reconfigure_domain. Each one takes its inputs as arguments. guest_os replaces build_oshash and the edits in build_xmldesc. reconfigure_domain replaces the XML rewrite in reconfigvm and loads XML::LibXML when it is called. kvm.pm calls these subs, and the four variables are file lexicals again. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- perl-xCAT/xCAT/KVMUtils.pm | 212 ++++++++++++++++++++++++++++ xCAT-server/lib/xcat/plugins/kvm.pm | 162 +++------------------ 2 files changed, 231 insertions(+), 143 deletions(-) diff --git a/perl-xCAT/xCAT/KVMUtils.pm b/perl-xCAT/xCAT/KVMUtils.pm index d2856bf7ad..e1befd7493 100644 --- a/perl-xCAT/xCAT/KVMUtils.pm +++ b/perl-xCAT/xCAT/KVMUtils.pm @@ -399,4 +399,216 @@ sub cdrom_prefix { return $profile->{cd_prefix}; } +#------------------------------------------------------- + +=head3 vm_othersettings + + Descriptions: + The vm.othersettings of one node. + + The read creates no vm table and no vm row. An rvalue dereference chain + autovivifies its intermediate links. + Arguments: + $confdata - the table data of the kvm plugin, or undef + $node - the node name + Returns: + the othersettings value, or undef when the node has no vm row + +=cut + +#------------------------------------------------------- +sub vm_othersettings { + my ($confdata, $node) = @_; + return undef unless ref($confdata) eq 'HASH'; + my $vm = $confdata->{vm} or return undef; + my $rows = $vm->{$node} or return undef; + my $row = $rows->[0] or return undef; + return $row->{othersettings}; +} + +#------------------------------------------------------- + +=head3 requested_firmware + + Descriptions: + The firmware a node asks for in vm.othersettings. No node asks by default, so an + existing guest keeps the firmware of its machine type. + Arguments: + $othersettings - vm.othersettings of the node, or undef + Returns: + the value of the firmware: setting, or undef + +=cut + +#------------------------------------------------------- +sub requested_firmware { + my ($othersettings) = @_; + return undef unless defined $othersettings; + foreach my $setting (split /;/, $othersettings) { + return $1 if $setting =~ /^\s*firmware:(.*)/; + } + return undef; +} + +#------------------------------------------------------- + +=head3 firmware_error + + Descriptions: + The refusal of a firmware value that libvirt cannot select. A node with a typo must + not stay on SeaBIOS without a message. + Arguments: + $firmware - the value from requested_firmware, or undef + $node - the node name + Returns: + the error message, or undef when the value is efi or undef + +=cut + +#------------------------------------------------------- +sub firmware_error { + my ($firmware, $node) = @_; + return undef if !defined($firmware) or $firmware eq 'efi'; + return "vm.othersettings firmware:$firmware names no firmware libvirt can select for $node. Use firmware:efi."; +} + +#------------------------------------------------------- + +=head3 guest_os + + Descriptions: + The element of one guest, in the hash form XML::Simple XMLout writes. + + A ppc64le hypervisor gets no . A profile without bios drops it too. + A machine type from vm.othersettings replaces the machine type of the profile. + Arguments: + $profile - the hash reference from guest_arch_profile + %args - hyp_cpumodel: the cpumodel of the hypervisor; bootorder: vm.bootorder of the + node; machine: the machine: setting of vm.othersettings + Returns: + a hash reference for the element + +=cut + +#------------------------------------------------------- +sub guest_os { + my ($profile, %args) = @_; + my %os = (type => { content => 'hvm' }); + unless (defined($args{hyp_cpumodel}) and $args{hyp_cpumodel} eq 'ppc64le') { + $os{bios}->{useserial} = 'yes'; + } + if (defined $args{bootorder}) { + foreach my $dev (split(/[:,]/, $args{bootorder})) { + $dev = 'network' if ($dev eq 'net' or $dev eq 'n'); + push @{ $os{boot} }, { dev => $dev }; + } + } else { + $os{boot} = [ { dev => 'network' }, { dev => 'hd' } ]; + } + $os{type}->{arch} = $profile->{arch} if defined $profile->{arch}; + $os{type}->{machine} = $profile->{machine} if defined $profile->{machine}; + $os{firmware} = $profile->{firmware} if defined $profile->{firmware}; + delete $os{bios} unless $profile->{bios}; + $os{type}->{machine} = $args{machine} if defined $args{machine}; + return \%os; +} + +#------------------------------------------------------- + +=head3 clock_offset + + Descriptions: + The clock offset of one guest. vm.clockoffset wins. Otherwise a Windows guest uses + localtime and every other guest uses utc. + Arguments: + $clockoffset - vm.clockoffset of the node, or undef + $os - nodetype.os of the node, or undef + Returns: + the clock offset + +=cut + +#------------------------------------------------------- +sub clock_offset { + my ($clockoffset, $os) = @_; + return $clockoffset if defined $clockoffset; + return 'localtime' if (defined($os) and $os =~ /win.*/); + return 'utc'; +} + +#------------------------------------------------------- + +=head3 reconfigure_domain + + Descriptions: + Apply the clock offset, memory, CPU count and boot order of a node to the XML of + an existing domain. Every other element of the domain stays as it is. + + XML::LibXML is loaded here, not by this module, so the other subs need only core Perl. + Arguments: + $xml - the domain XML + %args - clock_offset: the result of clock_offset; memory: the memory in KiB, or + undef; vcpus: the vcpus value of the vm row, or undef; bootorder: + vm.bootorder, or undef + Returns: + the new domain XML, or 0 when the domain needs no change + +=cut + +#------------------------------------------------------- +sub reconfigure_domain { + my ($xml, %args) = @_; + require XML::LibXML; + my $parser = XML::LibXML->new(); + my $domdesc = $parser->parse_string($xml); + my @bootdevs = $domdesc->findnodes("/domain/os/boot"); + my $needfixin = 0; + my $clock = $domdesc->findnodes("/domain/clock")->[0]; + if ($clock->getAttribute("offset") ne $args{clock_offset}) { + $needfixin = 1; + $clock->setAttribute("offset", $args{clock_offset}); + } + if (defined $args{memory}) { + $needfixin = 1; + $domdesc->findnodes("/domain/memory/text()")->[0]->setData($args{memory}); + foreach ($domdesc->findnodes("/domain/currentMemory/text()")) { + $_->setData($args{memory}); + } + } + if (defined $args{vcpus}) { + $needfixin = 1; + $domdesc->findnodes("/domain/vcpu/text()")->[0]->setData($args{vcpus}); + } + if (defined $args{bootorder}) { + my @oldbootdevs; + my @expectedorder = split(/[:,]/, $args{bootorder}); + foreach (@expectedorder) { + my $currdev = shift @bootdevs; + if ("net" eq $_ or "n" eq $_) { + $_ = "network"; + } + unless ($currdev and $currdev->getAttribute("dev") eq $_) { + $needfixin = 1; + } + if ($currdev) { + push @oldbootdevs, $currdev; + } + } + if (scalar(@bootdevs)) { + $needfixin = 1; + push @oldbootdevs, @bootdevs; + } + return 0 unless $needfixin; + + foreach (@oldbootdevs) { + $_->parentNode->removeChild($_); + } + my $osnode = $domdesc->findnodes("/domain/os")->[0]; + foreach (@expectedorder) { + $osnode->appendChild($parser->parse_balanced_chunk('')); + } + } + return $needfixin ? $domdesc->toString() : 0; +} + 1; diff --git a/xCAT-server/lib/xcat/plugins/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index 055f6ef810..73cefeaefe 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -37,13 +37,10 @@ my %vm_comm_pids; my %offlinehyps; my %hypstats; my %offlinevms; -# process_request sets $parser, $node, $confdata and $updatetable before it calls the domain -# builder. They are package variables so a unit test can set them the same way and call the -# builder itself, rather than extracting it from this file and testing a copy. -our $parser; +my $parser; my @destblacklist; -our $updatetable; #when a function is performing per-node operations, it can queue up a table update by populating parts of this hash -our $confdata; #a reference to serve as a common pointer betweer VMCommon functions and this plugin +my $updatetable; #when a function is performing per-node operations, it can queue up a table update by populating parts of this hash +my $confdata; #a reference to serve as a common pointer betweer VMCommon functions and this plugin my %allnodestatus; require Sys::Virt; @@ -101,7 +98,7 @@ my $hypconn; my $hyp; my $doreq; my %hyphash; -our $node; +my $node; my $vmtab; @@ -394,104 +391,13 @@ sub waitforack { sub reconfigvm { $node = shift; - my $xml = shift; - my $domdesc = $parser->parse_string($xml); - my @bootdevs = $domdesc->findnodes("/domain/os/boot"); - my $curroffset = $domdesc->findnodes("/domain/clock")->[0]->getAttribute("offset"); - my $newoffset; - my $needfixin = 0; - if (defined($confdata->{vm}->{$node}->[0]->{clockoffset})) { - - #If user requested a specific behavior, give it - $newoffset = $confdata->{vm}->{$node}->[0]->{clockoffset}; - } else { - - #Otherwise, only do local time for things that look MS - if (defined($confdata->{nodetype}->{$node}->[0]->{os}) and $confdata->{nodetype}->{$node}->[0]->{os} =~ /win.*/) { - $newoffset = 'localtime'; - } else { #For everyone else, utc is preferred generally - $newoffset = 'utc'; - } - } - if ($curroffset ne $newoffset) { - $needfixin = 1; - $domdesc->findnodes("/domain/clock")->[0]->setAttribute("offset", $newoffset); - } - my @oldbootdevs; - if (defined $confdata->{vm}->{$node}->[0]->{memory}) { - $needfixin = 1; - $domdesc->findnodes("/domain/memory/text()")->[0]->setData(getUnits($confdata->{vm}->{$node}->[0]->{memory}, "M", 1024)); - foreach ($domdesc->findnodes("/domain/currentMemory/text()")) { - $_->setData(getUnits($confdata->{vm}->{$node}->[0]->{memory}, "M", 1024)); - } - } - if (defined $confdata->{vm}->{$node}->[0]->{vcpus}) { - $needfixin = 1; - $domdesc->findnodes("/domain/vcpu/text()")->[0]->setData($confdata->{vm}->{$node}->[0]->{vcpus}); - } - if (defined $confdata->{vm}->{$node}->[0]->{bootorder}) { - my @expectedorder = split(/[:,]/, $confdata->{vm}->{$node}->[0]->{bootorder}); - foreach (@expectedorder) { #this loop will check for changes and fix 'n' and 'net' - my $currdev = shift @bootdevs; - if ("net" eq $_ or "n" eq $_) { - $_ = "network"; - } - unless ($currdev and $currdev->getAttribute("dev") eq $_) { - $needfixin = 1; - } - if ($currdev) { - push @oldbootdevs, $currdev; - } - } - if (scalar(@bootdevs)) { - $needfixin = 1; - push @oldbootdevs, @bootdevs; - } - unless ($needfixin) { return 0; } - - #ok, we need to remove all 'boot' nodes from current xml, and put in new ones in the order we like - foreach (@oldbootdevs) { - $_->parentNode->removeChild($_); - } - - #now to add what we want... - my $osnode = $domdesc->findnodes("/domain/os")->[0]; - foreach (@expectedorder) { - my $fragment = $parser->parse_balanced_chunk(''); - $osnode->appendChild($fragment); - } - } - if ($needfixin) { - return $domdesc->toString(); - } else { return 0; } -} - -sub build_oshash { - my %rethash; - $rethash{type}->{content} = 'hvm'; - - my $hypcpumodel = $confdata->{ $confdata->{vm}->{$node}->[0]->{host} }->{cpumodel}; - unless (defined($hypcpumodel) and $hypcpumodel eq "ppc64le") { - $rethash{bios}->{useserial} = 'yes'; - } - - if (defined $confdata->{vm}->{$node}->[0]->{bootorder}) { - my $bootorder = $confdata->{vm}->{$node}->[0]->{bootorder}; - my @bootdevs = split(/[:,]/, $bootorder); - my $bootnum = 0; - foreach (@bootdevs) { - if ("net" eq $_ or "n" eq $_) { - $rethash{boot}->[$bootnum]->{dev} = "network"; - } else { - $rethash{boot}->[$bootnum]->{dev} = $_; - } - $bootnum++; - } - } else { - $rethash{boot}->[0]->{dev} = 'network'; - $rethash{boot}->[1]->{dev} = 'hd'; - } - return \%rethash; + my $xml = shift; + my $vm = $confdata->{vm}->{$node}->[0]; + return xCAT::KVMUtils::reconfigure_domain($xml, + clock_offset => xCAT::KVMUtils::clock_offset($vm->{clockoffset}, $confdata->{nodetype}->{$node}->[0]->{os}), + memory => (defined $vm->{memory} ? getUnits($vm->{memory}, "M", 1024) : undef), + vcpus => $vm->{vcpus}, + bootorder => $vm->{bootorder}); } sub build_diskstruct { @@ -504,7 +410,7 @@ sub build_diskstruct { my $storagemodel = $confdata->{vm}->{$node}->[0]->{storagemodel}; my $profile = xCAT::KVMUtils::guest_arch_profile($confdata->{nodetype}->{$node}->[0]->{arch}, $confdata->{ $confdata->{vm}->{$node}->[0]->{host} }->{cpumodel}, - requested_firmware(vm_othersettings($node))); + xCAT::KVMUtils::requested_firmware(xCAT::KVMUtils::vm_othersettings($confdata, $node))); my $cachemethod = "none"; if ($confdata->{vm}->{$node}->[0]->{storagecache}) { $cachemethod = $confdata->{vm}->{$node}->[0]->{storagecache}; @@ -687,31 +593,6 @@ sub getUnits { } } -# vm_othersettings: the vm.othersettings of one node, or undef when the node has no vm row. -# -# An rvalue dereference chain autovivifies its intermediate links, so reading the column as -# $confdata->{vm}->{$node}->[0]->{othersettings} creates a vm row for a node that has none. -sub vm_othersettings { - my ($nodename) = @_; - return undef unless ref($confdata) eq 'HASH'; - my $vm = $confdata->{vm} or return undef; - my $rows = $vm->{$nodename} or return undef; - my $row = $rows->[0] or return undef; - return $row->{othersettings}; -} - -# requested_firmware: the firmware a node asks for in vm.othersettings, or undef. -# -# No node asks by default, so an existing guest keeps the firmware of its machine type. -sub requested_firmware { - my ($othersettings) = @_; - return undef unless defined $othersettings; - foreach my $setting (split /;/, $othersettings) { - return $1 if $setting =~ /^\s*firmware:(.*)/; - } - return undef; -} - sub build_xmldesc { my $node = shift; my %args = @_; @@ -724,21 +605,15 @@ sub build_xmldesc { $hypcputhreads = "1"; } - my $firmware = requested_firmware(vm_othersettings($node)); - if (defined $firmware and $firmware ne 'efi') { - return (-1, "vm.othersettings firmware:$firmware names no firmware libvirt can select for $node. Use firmware:efi."); - } + my $firmware = xCAT::KVMUtils::requested_firmware(xCAT::KVMUtils::vm_othersettings($confdata, $node)); + my $firmware_error = xCAT::KVMUtils::firmware_error($firmware, $node); + return (-1, $firmware_error) if $firmware_error; my $profile = xCAT::KVMUtils::guest_arch_profile($confdata->{nodetype}->{$node}->[0]->{arch}, $hypcpumodel, $firmware); $xtree{type} = $profile->{domtype}; $xtree{name}->{content} = $node; $xtree{uuid}->{content} = getNodeUUID($node); - $xtree{os} = build_oshash(); - $xtree{os}->{type}->{arch} = $profile->{arch} if defined $profile->{arch}; - $xtree{os}->{type}->{machine} = $profile->{machine} if defined $profile->{machine}; - $xtree{os}->{firmware} = $profile->{firmware} if defined $profile->{firmware}; - delete $xtree{os}->{bios} unless $profile->{bios}; if ($args{memory}) { $xtree{memory}->{content} = getUnits($args{memory}, "M", 1024); if ($confdata->{vm}->{$node}->[0]->{memory}) { @@ -800,9 +675,10 @@ sub build_xmldesc { } # Support VM machine types specified through vm.othersettings, e.g., q35, pseries-rhel7.4.0. - if (defined $machine_type) { - $xtree{os}->{type}->{machine} = $machine_type; - } + $xtree{os} = xCAT::KVMUtils::guest_os($profile, + hyp_cpumodel => $hypcpumodel, + bootorder => $confdata->{vm}->{$node}->[0]->{bootorder}, + machine => $machine_type); #prepare the xml hash for memory binding if (defined $memnumanodes) { From fc020b6976fa986c08013abccd40dd49bc20a243 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:38:33 -0300 Subject: [PATCH 25/25] test(kvm): test the UEFI firmware decisions through xCAT::KVMUtils kvm_uefi_firmware.t loaded the whole kvm plugin behind stubs and set its package variables. It now loads only xCAT::KVMUtils and keeps each assertion: the UEFI and SeaBIOS element, the disk bus and optical drive prefix, the SMBIOS block across a reconfigure, the refusal of an unknown firmware value, and the vm.othersettings read that creates no vm row. It also covers the clock offset, memory and CPU count that reconfigure_domain writes. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-test/unit/kvm_uefi_firmware.t | 325 +++++++++++------------------ 1 file changed, 119 insertions(+), 206 deletions(-) diff --git a/xCAT-test/unit/kvm_uefi_firmware.t b/xCAT-test/unit/kvm_uefi_firmware.t index ee56a05ab4..e7a4c11f3e 100644 --- a/xCAT-test/unit/kvm_uefi_firmware.t +++ b/xCAT-test/unit/kvm_uefi_firmware.t @@ -9,148 +9,85 @@ use strict; use warnings; -use File::Temp qw(tempdir); use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; use Test::More; use XML::LibXML; -# The plugin package declares the variables below; this test names each of them once. -no warnings 'once'; +my %before = %INC; +require xCAT::KVMUtils; +my @xcat_loaded = sort grep { m{^xCAT} and !exists $before{$_} } keys %INC; +is_deeply( \@xcat_loaded, ['xCAT/KVMUtils.pm'], 'xCAT::KVMUtils loads no other xCAT module' ); -my $repo = "$FindBin::Bin/../.."; - -BEGIN { - # kvm.pm loads the xCAT database, the monitoring dispatcher, the thread helper and the - # libvirt binding. None of them builds the domain XML. - package Sys::Virt; - our $VERSION = '11.0.0'; - - package Thread; - sub import { - my $caller = caller; - no strict 'refs'; - *{"${caller}::yield"} = sub { return }; - return; - } - - package xCAT::Utils; - sub genpassword { return 'password' } - sub genUUID { return '00000000-0000-0000-0000-000000000002' } - - sub import { - my ( undef, @names ) = @_; - my $caller = caller; - no strict 'refs'; - *{"${caller}::$_"} = \&{"xCAT::Utils::$_"} for @names; - return; - } - - package xCAT::VMCommon; - sub getMacAddresses { return ('52:54:00:00:00:01') } - - package xCAT::Table; - sub new { return } - - package xCAT::GlobalDef; - - package xCAT::NodeRange; - - package xCAT::Usage; - - package xCAT::TableUtils; - - package xCAT::ServiceNodeUtils; - - package xCAT::DBobjUtils; - - package xCAT::SvrUtils; - - package xCAT_monitoring::monitorctrl; - - package main; - $INC{'Sys/Virt.pm'} = __FILE__; - $INC{'Thread.pm'} = __FILE__; - $INC{'xCAT_monitoring/monitorctrl.pm'} = __FILE__; - for my $module ( - qw(GlobalDef NodeRange VMCommon Table Usage Utils TableUtils - ServiceNodeUtils DBobjUtils SvrUtils) - ) - { - $INC{"xCAT/$module.pm"} = __FILE__; - } - - # kvm.pm puts $XCATROOT/lib/perl in front of the checkout. Point it at an empty directory, - # so the test cannot measure a product installed weeks ago. - $ENV{XCATROOT} = File::Temp::tempdir( CLEANUP => 1 ); -} - -# xcatd loads the plugins from their files. -my $inc = tempdir( CLEANUP => 1 ); -symlink( "$repo/xCAT-server/lib/xcat/plugins", "$inc/xCAT_plugin" ) or die "symlink: $!"; -unshift @INC, $inc, "$repo/xCAT-server/lib/perl", "$repo/perl-xCAT"; -require xCAT_plugin::kvm; - -# One compute node on one hypervisor, the way process_request hands them to the domain builder. -sub node_tables { - my (%attrib) = @_; - return { - vm => { - cn1 => [ - { - host => 'hyp1', - memory => 4096, - cpus => 2, - nics => 'br0', - storage => '/var/lib/libvirt/images/cn1.img', - ( - defined $attrib{othersettings} - ? ( othersettings => $attrib{othersettings} ) - : () - ), - ( - defined $attrib{bootorder} - ? ( bootorder => $attrib{bootorder} ) - : () - ), - } - ] - }, - nodetype => { cn1 => [ { arch => $attrib{arch} || 'x86_64', os => 'alma10.0' } ] }, - vpd => { cn1 => [ { uuid => 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' } ] }, - hyp1 => { cpumodel => $attrib{cpumodel} || 'x86_64' }, +# The firmware and the profile that build_xmldesc and build_diskstruct read for one x86_64 node. +sub node_profile { + my ($othersettings) = @_; + my $confdata = { + vm => { cn1 => [ { host => 'hyp1', othersettings => $othersettings } ] }, + nodetype => { cn1 => [ { arch => 'x86_64' } ] }, }; + my $firmware = + xCAT::KVMUtils::requested_firmware( xCAT::KVMUtils::vm_othersettings( $confdata, 'cn1' ) ); + return xCAT::KVMUtils::guest_arch_profile( 'x86_64', 'x86_64', $firmware ); } -# Build the domain XML of that node. Returns the XML, or the refusal the builder returned. -sub domain_xml { - my (%attrib) = @_; - local $xCAT_plugin::kvm::node = 'cn1'; - local $xCAT_plugin::kvm::confdata = node_tables(%attrib); - local $xCAT_plugin::kvm::updatetable = {}; - my ( $xml, $errstr ); - my $chatter = ''; - { - open( my $capture, '>', \$chatter ) or die "capture stdout: $!"; - local *STDOUT = $capture; - ( $xml, $errstr ) = xCAT_plugin::kvm::build_xmldesc('cn1'); - } - return ( $xml, $errstr ); +sub os_of { + my ( $profile, %args ) = @_; + return xCAT::KVMUtils::guest_os( $profile, hyp_cpumodel => 'x86_64', %args ); } -sub os_element { - my ($xml) = @_; - die "build_xmldesc returned no XML\n" unless defined $xml and !ref $xml; - my ($os) = XML::LibXML->load_xml( string => $xml )->findnodes('/domain/os'); - die "the domain XML carries no element\n" unless $os; - return $os; +# The bus build_diskstruct gives the first disk, and the prefix of the optical drive. +sub disk_bus { + my ($profile) = @_; + my ( undef, $model ) = xCAT::KVMUtils::split_disk_spec( '/var/lib/libvirt/images/cn1.img', + undef, $profile->{disk_model} ); + return $model; } -# The elements and the attributes of , without the indentation XML::Simple adds. -sub os_shape { - my ($xml) = @_; - ( my $shape = os_element($xml)->toString() ) =~ s/>\s+{firmware}, 'efi', + 'firmware:efi puts firmware="efi" on , which is how libvirt selects a UEFI firmware' ); +is( $uefi_os->{type}->{machine}, 'q35', + 'the UEFI node is placed on q35, the only machine type the x86_64 OVMF descriptors target' ); +is( $uefi_os->{bios}->{useserial}, 'yes', + 'the UEFI node keeps , so its boot still reaches the serial console' ); + +# q35 has no IDE controller, so libvirt refuses a domain whose disks state the ide bus. +is( disk_bus($uefi), 'scsi', 'the disk of the UEFI node is scsi, a bus q35 can carry' ); +is( xCAT::KVMUtils::cdrom_prefix( $uefi, undef ), 'sd', + 'and its optical drive is named sd*, not hd*' ); + +# A node that names its own machine type keeps it. q35 is a default, not an override. +my $named = node_profile('firmware:efi;machine:pc-q35-rhel9.4.0'); +is( os_of( $named, machine => 'pc-q35-rhel9.4.0' )->{type}->{machine}, 'pc-q35-rhel9.4.0', + 'firmware:efi leaves a machine type the node names alone' ); + +# The SMBIOS block of a discovery guest. cluster-test.pl stores a masked domain XML in +# kvm_nodedata, and kvm.pm builds the guest from that stored XML through reconfigvm. Give +# reconfigure_domain the masked UEFI domain and a boot order to change, so it rewrites . +my $masked = <<'XML'; + + cn1 + 4194304 + 2 + + hvm + + + + + + + + + Supermicro + Super Server + + + +XML sub attribute { my ( $xml, $xpath, $name ) = @_; @@ -159,62 +96,16 @@ sub attribute { return $node->getAttribute($name); } -# A node that asks for UEFI. -my ($uefi) = domain_xml( othersettings => 'firmware:efi' ); -is( attribute( $uefi, '/domain/os', 'firmware' ), 'efi', - 'firmware:efi puts firmware="efi" on , which is how libvirt selects a UEFI firmware' ); -is( attribute( $uefi, '/domain/os/type', 'machine' ), 'q35', - 'the UEFI node is placed on q35, the only machine type the x86_64 OVMF descriptors target' ); -is( attribute( $uefi, '/domain/os/bios', 'useserial' ), 'yes', - 'the UEFI node keeps , so its boot still reaches the serial console' ); - -# q35 has no IDE controller, so libvirt refuses a domain whose disks state the ide bus. -is( attribute( $uefi, '/domain/devices/disk[1]/target', 'bus' ), 'scsi', - 'the disk of the UEFI node is scsi, a bus q35 can carry' ); -like( attribute( $uefi, '/domain/devices/disk[2]/target', 'dev' ), qr/^sd/, - 'and its optical drive is named sd*, not hd*' ); - -# A node that names its own machine type keeps it. q35 is a default, not an override. -my ($named_machine) = domain_xml( othersettings => 'firmware:efi;machine:pc-q35-rhel9.4.0' ); -is( attribute( $named_machine, '/domain/os/type', 'machine' ), 'pc-q35-rhel9.4.0', - 'firmware:efi leaves a machine type the node names alone' ); - -# The SMBIOS block of a discovery guest. cluster-test.pl stores a masked domain XML in -# kvm_nodedata, and kvm.pm builds the guest from that stored XML through reconfigvm. Give -# reconfigvm the masked UEFI domain and a boot order to change, so it rewrites . -sub smbios_masked { - my ($xml) = @_; - my $doc = XML::LibXML->load_xml( string => $xml ); - my ($domain) = $doc->findnodes('/domain'); - my ($os) = $doc->findnodes('/domain/os'); - my $sysinfo = $doc->createElement('sysinfo'); - $sysinfo->setAttribute( 'type', 'smbios' ); - my $system = $doc->createElement('system'); - for my $pair ( [ manufacturer => 'Supermicro' ], [ product => 'Super Server' ] ) { - my $entry = $doc->createElement('entry'); - $entry->setAttribute( 'name', $pair->[0] ); - $entry->appendText( $pair->[1] ); - $system->appendChild($entry); - } - $sysinfo->appendChild($system); - $domain->appendChild($sysinfo); - my $mode = $doc->createElement('smbios'); - $mode->setAttribute( 'mode', 'sysinfo' ); - $os->appendChild($mode); - return $doc->toString(); -} - -sub reconfigured { - my ( $xml, %attrib ) = @_; - local $xCAT_plugin::kvm::node = 'cn1'; - local $xCAT_plugin::kvm::confdata = node_tables(%attrib); - local $xCAT_plugin::kvm::parser = XML::LibXML->new(); - my $rewritten = xCAT_plugin::kvm::reconfigvm( 'cn1', $xml ); - die "reconfigvm rewrote nothing, so the test measures no rewrite\n" unless $rewritten; - return $rewritten; -} - -my $rebuilt = reconfigured( smbios_masked($uefi), bootorder => 'hd,net' ); +my $rebuilt = xCAT::KVMUtils::reconfigure_domain( $masked, + clock_offset => xCAT::KVMUtils::clock_offset( undef, 'alma10.0' ), + bootorder => 'hd,net' ); +die "reconfigure_domain rewrote nothing, so the test measures no rewrite\n" unless $rebuilt; +is_deeply( + [ map { $_->getAttribute('dev') } + XML::LibXML->load_xml( string => $rebuilt )->findnodes('/domain/os/boot') ], + [ 'hd', 'network' ], + 'reconfigure_domain writes the boot order of the node' +); is( attribute( $rebuilt, '/domain/sysinfo', 'type' ), 'smbios', 'the rebuilt UEFI guest keeps the block with its DMI strings' ); is( attribute( $rebuilt, '/domain/os/smbios', 'mode' ), 'sysinfo', @@ -226,42 +117,64 @@ is( attribute( $rebuilt, '/domain/os/bios', 'useserial' ), 'yes', # A node that asks for nothing. is the only element the firmware selection touches, so it # is held down whole. -my ($plain) = domain_xml(); -is( os_shape($plain), - 'hvm', - 'an x86_64 node that asks for nothing states no firmware and no machine type' ); +my $plain = node_profile(undef); +is_deeply( + os_of($plain), + { + type => { content => 'hvm' }, + bios => { useserial => 'yes' }, + boot => [ { dev => 'network' }, { dev => 'hd' } ], + }, + 'an x86_64 node that asks for nothing states no firmware and no machine type' +); +is( disk_bus($plain), 'ide', 'and keeps the ide disk bus of the machine type it has always had' ); -is( attribute( $plain, '/domain/devices/disk[1]/target', 'bus' ), 'ide', - 'and keeps the ide disk bus of the machine type it has always had' ); - -my ($other_keys) = domain_xml( othersettings => 'cpumode:host-passthrough' ); -is( attribute( $other_keys, '/domain/os', 'firmware' ), undef, +is( os_of( node_profile('cpumode:host-passthrough') )->{firmware}, undef, 'another vm.othersettings key does not select a firmware' ); # Reading the setting must not create the row it reads. An rvalue dereference chain # autovivifies its intermediate links, so $confdata->{vm}->{$node}->[0]->{othersettings} makes # a vm row exist for a node that is not a VM, and a later truth test on that row answers yes. { - local $xCAT_plugin::kvm::confdata = { nodetype => { cn9 => [ { arch => 'x86_64' } ] } }; - is( xCAT_plugin::kvm::vm_othersettings('cn9'), undef, + my $confdata = { nodetype => { cn9 => [ { arch => 'x86_64' } ] } }; + is( xCAT::KVMUtils::vm_othersettings( $confdata, 'cn9' ), undef, 'vm_othersettings answers undef for a node with no vm row' ); - ok( !exists $xCAT_plugin::kvm::confdata->{vm}, - 'and the read autovivified no vm table in confdata' ); + ok( !exists $confdata->{vm}, 'and the read autovivified no vm table in confdata' ); } { - local $xCAT_plugin::kvm::confdata = { vm => { cn8 => [ { host => 'hyp1' } ] } }; - is( xCAT_plugin::kvm::vm_othersettings('cn9'), undef, + my $confdata = { vm => { cn8 => [ { host => 'hyp1' } ] } }; + is( xCAT::KVMUtils::vm_othersettings( $confdata, 'cn9' ), undef, 'vm_othersettings answers undef for a node absent from a populated vm table' ); - ok( !exists $xCAT_plugin::kvm::confdata->{vm}->{cn9}, - 'and the read autovivified no row for that node' ); - is_deeply( $xCAT_plugin::kvm::confdata, { vm => { cn8 => [ { host => 'hyp1' } ] } }, + ok( !exists $confdata->{vm}->{cn9}, 'and the read autovivified no row for that node' ); + is_deeply( $confdata, { vm => { cn8 => [ { host => 'hyp1' } ] } }, 'and left confdata as it was' ); } +# reconfigvm takes the clock offset, memory and CPU count of the node into the stored domain. +is( xCAT::KVMUtils::clock_offset( undef, 'win2019' ), 'localtime', 'a Windows guest keeps localtime' ); +is( xCAT::KVMUtils::clock_offset( undef, 'alma10.0' ), 'utc', 'every other guest uses utc' ); +is( xCAT::KVMUtils::clock_offset( 'localtime', 'alma10.0' ), 'localtime', 'and vm.clockoffset wins' ); +my $resized = xCAT::KVMUtils::reconfigure_domain( $masked, + clock_offset => 'localtime', memory => 2097152, vcpus => 4 ); +is( attribute( $resized, '/domain/clock', 'offset' ), 'localtime', + 'reconfigure_domain writes the clock offset' ); +my $resized_doc = XML::LibXML->load_xml( string => $resized ); +is( $resized_doc->findvalue('/domain/memory'), '2097152', 'and the memory' ); +is( $resized_doc->findvalue('/domain/vcpu'), '4', 'and the CPU count' ); +is( xCAT::KVMUtils::reconfigure_domain( $masked, clock_offset => 'utc', bootorder => 'net,hd' ), + 0, 'a domain that already matches the node is not rewritten' ); +is( xCAT::KVMUtils::reconfigure_domain( $masked, clock_offset => 'utc' ), + 0, 'and neither is one for a node with no boot order' ); + +# A ppc64le hypervisor gets no SeaBIOS serial option. +ok( !exists xCAT::KVMUtils::guest_os( $plain, hyp_cpumodel => 'ppc64le' )->{bios}, + 'a guest on a ppc64le hypervisor states no ' ); + # A value libvirt cannot select is refused by name. A typo must not quietly leave the node on # SeaBIOS, because the node then fails a long way from here. -my ( $refused, $errstr ) = domain_xml( othersettings => 'firmware:uefi' ); -like( $errstr, qr/firmware:uefi/, +my $uefi_typo = xCAT::KVMUtils::requested_firmware('firmware:uefi'); +like( xCAT::KVMUtils::firmware_error( $uefi_typo, 'cn1' ), qr/firmware:uefi/, 'firmware:uefi is refused and the refusal names the value that was read' ); +is( xCAT::KVMUtils::firmware_error( 'efi', 'cn1' ), undef, 'firmware:efi is not refused' ); done_testing();