diff --git a/perl-xCAT/xCAT/KVMUtils.pm b/perl-xCAT/xCAT/KVMUtils.pm index 16cec5280f..350748d0ae 100644 --- a/perl-xCAT/xCAT/KVMUtils.pm +++ b/perl-xCAT/xCAT/KVMUtils.pm @@ -6,6 +6,7 @@ package xCAT::KVMUtils; use strict; use warnings; +use XML::LibXML; #------------------------------------------------------- @@ -130,7 +131,7 @@ sub guest_arch_profile { $profile{bios} = 0; $profile{sound} = 0; } - if (defined($firmware) and $firmware eq 'efi' and not defined $profile{firmware}) { + if (defined($firmware) and $firmware =~ /^efi\b/ and not defined $profile{firmware}) { $profile{firmware} = 'efi'; if ($profile{x86_features}) { $profile{machine} = 'q35'; @@ -468,8 +469,83 @@ sub requested_firmware { #------------------------------------------------------- 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."; + return undef if !defined($firmware); + return undef if $firmware eq 'efi' or defined secure_boot_request($firmware); + return "vm.othersettings firmware:$firmware names no firmware libvirt can select for $node." + . " Use firmware:efi, firmware:efi-secureboot or firmware:efi-insecure."; +} + +#------------------------------------------------------- + +=head3 secure_boot_request + + Descriptions: + What the firmware: setting of a node asks of Secure Boot. + + firmware:efi asks nothing and leaves the choice to libvirt firmware + autoselection, which picks a descriptor by host policy. A node that must load + the unsigned grub2 image xCAT serves asks for efi-insecure, and a node that + must enforce Secure Boot asks for efi-secureboot. + Arguments: + $firmware - the value from requested_firmware, or undef + Returns: + 'yes', 'no', or undef when the node asks nothing + +=cut + +#------------------------------------------------------- + +sub secure_boot_request { + my ($firmware) = @_; + return undef unless defined $firmware; + return 'yes' if $firmware eq 'efi-secureboot'; + return 'no' if $firmware eq 'efi-insecure'; + return undef; +} + +#------------------------------------------------------- + +=head3 with_firmware_features + + Descriptions: + The guest XML with the element that states what the node asks of + Secure Boot. A node that asks nothing, or a guest that does not ask for EFI, + gets its XML back unchanged. + + enrolled-keys follows secure-boot: libvirt refuses a guest whose keys are + enrolled while Secure Boot is disabled. + Arguments: + $xml - the guest XML + $firmware - the value from requested_firmware, or undef + Returns: + the guest XML + +=cut + +#------------------------------------------------------- + +sub with_firmware_features { + my ($xml, $firmware) = @_; + + my $secure_boot = secure_boot_request($firmware); + return $xml unless defined $secure_boot; + + my $doc = eval { XML::LibXML->load_xml(string => $xml) } or return $xml; + my ($os) = $doc->findnodes('/domain/os'); + return $xml unless $os and $os->getAttribute('firmware'); + + foreach my $stale ($os->findnodes('./firmware')) { + $os->removeChild($stale); + } + my $element = $doc->createElement('firmware'); + foreach my $name (qw(secure-boot enrolled-keys)) { + my $feature = $doc->createElement('feature'); + $feature->setAttribute('enabled', $secure_boot); + $feature->setAttribute('name', $name); + $element->appendChild($feature); + } + $os->appendChild($element); + return $doc->toString(); } #------------------------------------------------------- diff --git a/xCAT-server/lib/xcat/plugins/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index 2454849260..61391de896 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -893,7 +893,10 @@ sub build_xmldesc { $xtree{devices}->{console}->{type} = 'pty'; $xtree{devices}->{console}->{target}->{port} = '1'; - return XMLout(\%xtree, RootName => "domain"); + # XML::Simple writes the firmware attribute of ; the element that states + # what the node asks of Secure Boot is a child of the same name, so it is added after. + return xCAT::KVMUtils::with_firmware_features( + XMLout(\%xtree, RootName => "domain"), $firmware); } sub refresh_vm {