diff --git a/perl-xCAT/xCAT/KVMUtils.pm b/perl-xCAT/xCAT/KVMUtils.pm index 737dac1bf5..6214737cb4 100644 --- a/perl-xCAT/xCAT/KVMUtils.pm +++ b/perl-xCAT/xCAT/KVMUtils.pm @@ -528,11 +528,12 @@ sub firmware_machine_error { 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. - A profile with secureboot states . libvirt reads it while it - selects the firmware and keeps only the descriptors that carry secure-boot, so a - hypervisor with no such firmware refuses the domain. Without the element the guest - takes whichever firmware the hypervisor prefers, and the domain says nothing about - whether that firmware verifies what it boots. + A profile with secureboot states . libvirt keeps only the + descriptors that carry the secure-boot feature while it selects the firmware, so a + hypervisor with no such firmware refuses the domain. The element says nothing about + the UEFI keys, so firmware_features asks for enrolled-keys beside it. Without the + element the guest takes whichever firmware the hypervisor prefers, and the domain + says nothing about whether that firmware verifies what it boots. Arguments: $profile - the hash reference from guest_arch_profile %args - hyp_cpumodel: the cpumodel of the hypervisor; bootorder: vm.bootorder of the @@ -568,6 +569,79 @@ sub guest_os { #------------------------------------------------------- +=head3 firmware_features + + Descriptions: + The firmware autoselection features of one guest, in the form states them. + + A guest with secureboot asks for secure-boot and for enrolled-keys. secure-boot names + a firmware that can verify what it boots. It does not name the UEFI keys that decide + what passes, and a firmware with secure-boot and no enrolled keys boots an unsigned + binary. The two x86_64 descriptors of an EL 10 host show the difference: + 30-edk2-ovmf-x64-sb-enrolled.json and 40-edk2-ovmf-x64-sb.json name the same + OVMF_CODE.secboot.fd and differ in the nvram template, OVMF_VARS.secboot.fd against + OVMF_VARS.fd. Only the first template holds keys. + + libvirt reads as the secure-boot feature alone, and picks the + descriptor of highest priority that carries it. A host that ships the 40- descriptor + and not the 30- one satisfies that domain with an empty key store. + Arguments: + $profile - the hash reference from guest_arch_profile, or undef + Returns: + an array reference of { name, enabled } pairs, empty when the guest asks for none + +=cut + +#------------------------------------------------------- +sub firmware_features { + my ($profile) = @_; + return [] unless ref($profile) eq 'HASH' and $profile->{secureboot}; + return [ + { name => 'secure-boot', enabled => 'yes' }, + { name => 'enrolled-keys', enabled => 'yes' }, + ]; +} + +#------------------------------------------------------- + +=head3 add_firmware_features + + Descriptions: + Add with the features firmware_features names to the of one domain. + + guest_os cannot carry this element. The firmware attribute of and the + child of are one key of one hash for XMLout, which writes a scalar as an + attribute and a reference as an element, so the hash holds one of the two. The + attribute is the one that starts the firmware selection. + Arguments: + $xml - the domain XML + $features - the array reference from firmware_features + Returns: + the new domain XML, or $xml when the guest asks for no feature + +=cut + +#------------------------------------------------------- +sub add_firmware_features { + my ($xml, $features) = @_; + return $xml unless ref($features) eq 'ARRAY' and scalar(@$features); + require XML::LibXML; + my $domdesc = XML::LibXML->new()->parse_string($xml); + my ($os) = $domdesc->findnodes('/domain/os'); + return $xml unless $os; + my $element = $domdesc->createElement('firmware'); + foreach my $feature (@$features) { + my $child = $domdesc->createElement('feature'); + $child->setAttribute('enabled', $feature->{enabled}); + $child->setAttribute('name', $feature->{name}); + $element->appendChild($child); + } + $os->appendChild($element); + return $domdesc->toString(); +} + +#------------------------------------------------------- + =head3 clock_offset Descriptions: diff --git a/xCAT-server/lib/xcat/plugins/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index c7da387e00..e0e70bff6e 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -889,7 +889,8 @@ sub build_xmldesc { $xtree{devices}->{console}->{type} = 'pty'; $xtree{devices}->{console}->{target}->{port} = '1'; - return XMLout(\%xtree, RootName => "domain"); + return xCAT::KVMUtils::add_firmware_features(XMLout(\%xtree, RootName => "domain"), + xCAT::KVMUtils::firmware_features($profile)); } sub refresh_vm { diff --git a/xCAT-test/unit/kvm_secureboot_firmware.t b/xCAT-test/unit/kvm_secureboot_firmware.t index 5ebe5e2ff6..58de679aaf 100644 --- a/xCAT-test/unit/kvm_secureboot_firmware.t +++ b/xCAT-test/unit/kvm_secureboot_firmware.t @@ -8,16 +8,26 @@ # neither. A node that must boot signed binaries has to say so, so that a hypervisor without a # Secure Boot firmware refuses the domain instead of starting it unverified. # -# is the requirement libvirt reads during firmware selection. Measured -# with libvirt 11.10.0 on rome01: with that element resolves to -# OVMF_CODE.secboot.fd and , and the same domain with -# resolves to OVMF_CODE.fd. +# is not that requirement on its own. libvirt reads it as the secure-boot +# feature, and a firmware with secure-boot and an empty key store boots an unsigned binary. The +# two x86_64 descriptors of an EL 10 host name the same OVMF_CODE.secboot.fd and differ in the +# nvram template: 30-edk2-ovmf-x64-sb-enrolled.json takes OVMF_VARS.secboot.fd, which holds the +# keys, and 40-edk2-ovmf-x64-sb.json takes OVMF_VARS.fd, which does not. Only descriptor +# priority picks the first one, and priority is host policy. +# +# So the domain asks for enrolled-keys as well. Measured with libvirt 11.10.0 on rome01, with +# the 30- descriptor masked for one connection: the domain that states +# alone starts on OVMF_VARS.fd with enrolled-keys resolved to no, and the domain that asks for +# enrolled-keys is refused with "Unable to find 'efi' firmware that is compatible with the +# current configuration". use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../../perl-xCAT"; use Test::More; +use XML::LibXML; +use XML::Simple qw(XMLout); require xCAT::KVMUtils; @@ -105,4 +115,65 @@ is( xCAT::KVMUtils::firmware_machine_error( undef, 'pc', 'x86_64', 'cn1' ), is( xCAT::KVMUtils::firmware_machine_error( 'efi', 'virt', 'riscv64', 'cn1' ), undef, 'the machine types of another architecture are not judged here' ); +# carries the features libvirt filters the descriptors by. It is not in the hash +# guest_os returns: the firmware attribute of and the child of are one key +# of one hash for XMLout, and the attribute is the one that starts the selection. So +# add_firmware_features adds the element to the domain XMLout wrote, which is what kvm.pm does. +my $features = xCAT::KVMUtils::firmware_features($sb); +is_deeply( + $features, + [ { name => 'secure-boot', enabled => 'yes' }, { name => 'enrolled-keys', enabled => 'yes' } ], + 'a Secure Boot node asks for secure-boot and for enrolled-keys' +); +is_deeply( xCAT::KVMUtils::firmware_features( node_profile('firmware:efi') ), + [], 'firmware:efi asks for no firmware feature' ); +is_deeply( xCAT::KVMUtils::firmware_features( node_profile(undef) ), + [], 'and neither does a node that asks for no firmware' ); +is_deeply( xCAT::KVMUtils::firmware_features(undef), [], 'and neither does an undefined profile' ); + +# The domain as kvm.pm writes it: the of guest_os, then the features. +sub domain_of { + my ($profile) = @_; + my $os = XMLout( os_of($profile), RootName => 'os' ); + $os =~ s/^<\?xml[^>]*\?>\s*//; + return xCAT::KVMUtils::add_firmware_features( "\n$os\n\n", + xCAT::KVMUtils::firmware_features($profile) ); +} + +sub feature_enabled { + my ( $xml, $name ) = @_; + my ($node) = + XML::LibXML->load_xml( string => $xml ) + ->findnodes("/domain/os/firmware/feature[\@name='$name']"); + return defined($node) ? $node->getAttribute('enabled') : undef; +} + +my $sb_domain = domain_of($sb); +is( feature_enabled( $sb_domain, 'enrolled-keys' ), + 'yes', 'the domain of a Secure Boot node asks for a firmware whose keys are enrolled' ); +is( feature_enabled( $sb_domain, 'secure-boot' ), + 'yes', 'and for one that verifies what it boots' ); +is( XML::LibXML->load_xml( string => $sb_domain )->findnodes('/domain/os')->[0] + ->getAttribute('firmware'), + 'efi', 'and it keeps firmware="efi" on , which is what starts the firmware selection' ); +is( XML::LibXML->load_xml( string => $sb_domain )->findnodes('/domain/os/loader')->[0] + ->getAttribute('secure'), + 'yes', 'and keeps beside the features' ); + +# A node that asked only for UEFI gets no , so it keeps taking the descriptor its +# hypervisor prefers. +foreach my $othersettings ( 'firmware:efi', undef ) { + my $name = defined($othersettings) ? $othersettings : 'no firmware'; + my $domain = domain_of( node_profile($othersettings) ); + is_deeply( + [ XML::LibXML->load_xml( string => $domain )->findnodes('/domain/os/firmware') ], + [], "$name states no firmware feature" ); +} + +is( xCAT::KVMUtils::add_firmware_features( "", [] ), + "", + 'a domain that asks for no feature is returned unchanged, not re-serialised' ); +is( xCAT::KVMUtils::add_firmware_features( "", $features ), + "", 'a domain with no is returned unchanged' ); + done_testing();