diff --git a/docs/source/advanced/ipxe/index.rst b/docs/source/advanced/ipxe/index.rst index a90f0f9246..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. @@ -49,7 +56,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/KVMUtils.pm b/perl-xCAT/xCAT/KVMUtils.pm index ff37997cd6..e1befd7493 100644 --- a/perl-xCAT/xCAT/KVMUtils.pm +++ b/perl-xCAT/xCAT/KVMUtils.pm @@ -61,9 +61,14 @@ sub default_storagemodel { arch and machine stay undef when libvirt is to use its own default for the hypervisor. machine is the unversioned alias; libvirt resolves it to the version the hypervisor has. + + 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. Arguments: $guest_arch - nodetype.arch of the node, or undef $hyp_cpumodel - the cpumodel of the hypervisor, or undef + $firmware - the firmware the node asks for in vm.othersettings, or undef Returns: a hash reference of the profile @@ -71,7 +76,7 @@ sub default_storagemodel { #------------------------------------------------------- sub guest_arch_profile { - my ($guest_arch, $hyp_cpumodel) = @_; + my ($guest_arch, $hyp_cpumodel, $firmware) = @_; my %profile = ( domtype => 'kvm', arch => undef, @@ -125,6 +130,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; } @@ -386,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/perl-xCAT/xCAT/SecureBoot.pm b/perl-xCAT/xCAT/SecureBoot.pm new file mode 100644 index 0000000000..bebdc81ec9 --- /dev/null +++ b/perl-xCAT/xCAT/SecureBoot.pm @@ -0,0 +1,80 @@ +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', +); + +# 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 + + 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 + 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 + + 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}; + + # 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} }) { + 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/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index 69ad029a34..73cefeaefe 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -391,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 { @@ -500,7 +409,8 @@ sub build_diskstruct { my $suffidx = 0; 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}); + $confdata->{ $confdata->{vm}->{$node}->[0]->{host} }->{cpumodel}, + 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}; @@ -695,16 +605,15 @@ sub build_xmldesc { $hypcputhreads = "1"; } - my $profile = xCAT::KVMUtils::guest_arch_profile($confdata->{nodetype}->{$node}->[0]->{arch}, $hypcpumodel); + 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}) { @@ -733,6 +642,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) { @@ -765,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) { diff --git a/xCAT-server/lib/xcat/plugins/mknb.pm b/xCAT-server/lib/xcat/plugins/mknb.pm index 098547edd4..0d483c4cd4 100644 --- a/xCAT-server/lib/xcat/plugins/mknb.pm +++ b/xCAT-server/lib/xcat/plugins/mknb.pm @@ -6,6 +6,7 @@ use xCAT::Utils; use xCAT::HTTPUtils qw(httpport_suffix); use xCAT::TableUtils; use xCAT::NodeRange; +use xCAT::SecureBoot; use File::Path; use File::Copy; use English qw(-no_match_vars); @@ -775,7 +776,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"); @@ -830,7 +831,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") { @@ -874,6 +877,12 @@ 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. + # 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"; close($cfg); diff --git a/xCAT-server/lib/xcat/plugins/xnba.pm b/xCAT-server/lib/xcat/plugins/xnba.pm index 86371ed74b..9df311f54d 100644 --- a/xCAT-server/lib/xcat/plugins/xnba.pm +++ b/xCAT-server/lib/xcat/plugins/xnba.pm @@ -14,6 +14,7 @@ require xCAT::BootUtils; use xCAT::TableUtils; use xCAT::ServiceNodeUtils; use xCAT::Usage; +use xCAT::SecureBoot; my $dhcpconf = "/etc/dhcpd.conf"; @@ -23,6 +24,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 = ( @@ -155,6 +159,7 @@ sub setstate { my %iscsihash = %{ shift() }; my $tftpdir = shift; my $linuximghashref = shift; + my $nodetyperef = shift || {}; my $kern = $bphash{$node}->[0]; my $imgaddkcmdline = $linuximghashref->{'addkcmdline'}; @@ -358,6 +363,14 @@ 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}, + kernel => $kern->{kernel}, + 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 { @@ -800,7 +813,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 = (); @@ -823,7 +836,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/autotest/testcase/nodeset/ipxe_secureboot_shim.sh b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh new file mode 100755 index 0000000000..224b378259 --- /dev/null +++ b/xCAT-test/autotest/testcase/nodeset/ipxe_secureboot_shim.sh @@ -0,0 +1,217 @@ +#!/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 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 + echo "IPXE_SHIM_FAIL: no node given" + exit 1 +fi + +# 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 +} + +# 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 + echo "IPXE_SHIM_FAIL: $node has arch=$arch, and only x86_64 has a UEFI shim" + exit 1 +fi + +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 +must_read node profile +profile=$lookup +must_read node currchain +currchain=$lookup + +must_read site tftpdir +tftpdir=$lookup +[ -n "$tftpdir" ] || tftpdir=/tftpboot +must_read site installdir +installdir=$lookup +[ -n "$installdir" ] || installdir=/install +uefi="$tftpdir/xcat/ipxe/nodes/$node.uefi" + +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. +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 + # 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 + # 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 + # 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" + restore_rc=1 + fi + 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 +# the stateful image of the node here. In a bundle the provisioning cases have already set it. +destiny=osimage +if [ -z "$provmethod" ]; then + 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" + exit 1 + fi + destiny="osimage=$image" +fi + +# 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 +fi +if [ ! -f "$uefi" ]; then + echo "IPXE_SHIM_FAIL: nodeset wrote no $uefi" + 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" + 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" + 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" + exit 1 + ;; +esac +if [ ! -f "$shimfile" ]; then + echo "IPXE_SHIM_FAIL: the script names $urlpath and $shimfile does not exist" + exit 1 +fi + +restore_state || exit 1 +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..9a709bdd35 --- /dev/null +++ b/xCAT-test/autotest/testcase/nodeset/nodeset_ipxe_secureboot_shim @@ -0,0 +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 +end 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..0d9062b38a --- /dev/null +++ b/xCAT-test/bats/ipxe_secureboot_shim_restore.bats @@ -0,0 +1,604 @@ +#!/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. 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. The fifth argument is osimage.provmethod, which +# nodeset boot reads, and it defaults to install. +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" + printf '%s' "${5:-install}" >"$dir/provmethod" +} + +node_attr() +{ + cat "$STUB_STORE/node/$1" +} + +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_CURRCHAIN= + 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 + # 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 + # 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" + 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" + 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 +# 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" + printf '%s' install >"$STUB_STORE/osimage/$STUB_OS-x86_64-install-compute/provmethod" + 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 + -t) type="$2"; shift 2 ;; + -o) obj="$2"; shift 2 ;; + -i) attr="$2"; shift 2 ;; + *) 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" ;; + 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 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. + # 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" +. "$STUB_LIB" +case "$2" in + stat) + [ "$STUB_STAT_RC" = 0 ] || { echo "Error: cannot reach the server" >&2; exit "$STUB_STAT_RC"; } + echo "$1: $(get currstate)" + exit 0 + ;; + 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)" + [ -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' + echo 'imgload kernel' + echo "$STUB_SHIM_LINE" + echo 'imgargs kernel quiet' + echo 'imgexec kernel' + } >"$STUB_UEFI" + exit 0 + ;; + *) + [ "$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 + shell | standby | runcmd=*) + printf 'kernel genesis.kernel.%s\ninitrd genesis.fs.%s\n' "$(get arch)" "$(get arch)" >"$STUB_BOOTCFG" + ;; + esac + exit 0 + ;; +esac +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 + ;; + *) + [ "$STUB_CHDEF_RC" = 0 ] || exit "$STUB_CHDEF_RC" + ;; +esac +shift 4 +for kv in "$@"; do + put "${kv%%=*}" "${kv#*=}" +done +exit 0 +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" + [ "$(node_attr provmethod)" = "" ] + [ "$(node_attr profile)" = compute ] + [ "$(node_attr currstate)" = boot ] +} + +@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" +} + +# 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" -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 + # 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* ]] + # 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 +# 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" + 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 + echo "exit 0 with no OK: arch=$arch netboot=$netboot noimage=$noimage" >&2 + echo "$output" >&2 + return 1 + fi + done + 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" + define_image "$OS-x86_64-netboot-compute" "$OS" x86_64 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* ]] + [ "$(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. +@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"* ]] +} + +# 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* ]] +} + +# 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 ] +} + +# 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)" = "" ] +} diff --git a/xCAT-test/unit/kvm_uefi_firmware.t b/xCAT-test/unit/kvm_uefi_firmware.t new file mode 100644 index 0000000000..e7a4c11f3e --- /dev/null +++ b/xCAT-test/unit/kvm_uefi_firmware.t @@ -0,0 +1,180 @@ +#!/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 FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; +use Test::More; +use XML::LibXML; + +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' ); + +# 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 ); +} + +sub os_of { + my ( $profile, %args ) = @_; + return xCAT::KVMUtils::guest_os( $profile, hyp_cpumodel => 'x86_64', %args ); +} + +# 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; +} + +# A node that asks for UEFI. +my $uefi = node_profile('firmware:efi'); +my $uefi_os = os_of($uefi); +is( $uefi_os->{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 ) = @_; + my ($node) = XML::LibXML->load_xml( string => $xml )->findnodes($xpath); + return undef unless $node; + return $node->getAttribute($name); +} + +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', + '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 = 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( 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. +{ + 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 $confdata->{vm}, 'and the read autovivified no vm table in confdata' ); +} +{ + 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 $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 $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(); diff --git a/xCAT-test/unit/netboot_ipxe_method.t b/xCAT-test/unit/netboot_ipxe_method.t index f28b4e9163..f86ded2eb7 100644 --- a/xCAT-test/unit/netboot_ipxe_method.t +++ b/xCAT-test/unit/netboot_ipxe_method.t @@ -49,7 +49,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_genesis_shim.t b/xCAT-test/unit/secureboot_genesis_shim.t new file mode 100644 index 0000000000..aad0cc20a2 --- /dev/null +++ b/xCAT-test/unit/secureboot_genesis_shim.t @@ -0,0 +1,110 @@ +#!/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' ); + +# 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' ); + +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..4c1ded8412 --- /dev/null +++ b/xCAT-test/unit/secureboot_ipxe_node_shim.t @@ -0,0 +1,217 @@ +#!/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 => $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) } ] + 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' +); + +# 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' +); + +# 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', + 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();