diff --git a/docs/source/advanced/ipxe/index.rst b/docs/source/advanced/ipxe/index.rst index 40df83bcb3..6fb85c9666 100644 --- a/docs/source/advanced/ipxe/index.rst +++ b/docs/source/advanced/ipxe/index.rst @@ -15,6 +15,11 @@ line before the kernel, so the firmware verifies what it loads. A node with ``ne 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. +xCAT 2.20 supports Secure Boot on Enterprise Linux and on ``x86_64``. The shim chain itself is +UEFI and distribution independent, and a node of another distribution can boot through it, but no +other distribution is measured and none is supported. The CI names the Secure Boot case on its EL +``x86_64`` cells only. + New x86 nodes get ``netboot=ipxe``: node discovery, profile-based node definitions and the x86 node templates of ``mkdef --template`` set it. The upstream loader @@ -36,6 +41,8 @@ An x86 client without a node definition, such as a node in discovery, gets the u On a server that serves x86 discovery, ``makedhcp -n`` and ``makedhcp -a`` warn when an upstream loader file is missing from the local TFTP directory, or when ``xcat/ipxe/nets`` has no network boot script. +A client in discovery boots the Genesis kernel, which carries no signature, so discovery requires Secure Boot off on the node. See `UEFI Secure Boot`_. + Move nodes to the ipxe method ----------------------------- @@ -63,10 +70,30 @@ The signature database of the firmware does not hold the keys of a Linux distrib 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. +What this path supports +~~~~~~~~~~~~~~~~~~~~~~~ + +A node with ``netboot=ipxe`` boots the kernel of a distribution with Secure Boot on, for a disk installation and for a netboot image. The shim of the install source verifies that kernel. + +**Genesis requires Secure Boot off on the node.** ``xCAT-genesis-base`` ships ``xcat/genesis.kernel.x86_64`` unsigned, and a shim verifies a kernel only against the signature database of the firmware or a key in the MOK list. So a node that boots Genesis with Secure Boot on reports ``Verification failed: Security Policy Violation`` for the kernel, and the shim then stops in its own dialog. This covers node discovery, the Genesis shell, and the ``standby`` and ``runcmd`` destinies, which all boot that kernel. Turn Secure Boot off on a node for those tasks, and on again afterwards for a disk installation or a netboot image. + +A shim also accepts a kernel that a MOK key signs, so a site that signs ``xcat/genesis.kernel.x86_64`` with its own key and enrolls that key with ``mokutil`` boots Genesis with Secure Boot on. xCAT ships no key and no signature. The scripts under ``xcat/ipxe/nets`` name the shim of ``ipxe-xcat``, which reads the MOK list. 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. +A KVM node takes Secure Boot from ``vm.othersettings`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``vm.othersettings`` of a KVM node takes ``firmware:efi-secureboot``. ``mkvm`` then asks libvirt for a firmware with the ``secure-boot`` feature and with the UEFI keys enrolled, and places the node on ``q35``, the only machine type the x86_64 OVMF descriptors target:: + + chdef cn1 vmothersetting=firmware:efi-secureboot + +Both features are asked for, because the two are separate. ``secure-boot`` names a firmware that can verify what it boots, and ``enrolled-keys`` names the key store that decides what passes. A firmware with ``secure-boot`` and an empty key store boots an unsigned binary. On an EL 10 host the descriptors ``30-edk2-ovmf-x64-sb-enrolled.json`` and ``40-edk2-ovmf-x64-sb.json`` name the same OVMF binary and differ only in that key store. + +A hypervisor that ships no such firmware refuses the domain with ``Unable to find 'efi' firmware that is compatible with the current configuration``, rather than starting the node on a firmware that verifies nothing. + +``firmware:efi`` keeps its meaning and asks for no feature, so a node that asks only for UEFI takes the firmware its hypervisor prefers. + Known limits of the upstream loader ----------------------------------- diff --git a/perl-xCAT/xCAT/KVMUtils.pm b/perl-xCAT/xCAT/KVMUtils.pm index e1befd7493..d459b1453f 100644 --- a/perl-xCAT/xCAT/KVMUtils.pm +++ b/perl-xCAT/xCAT/KVMUtils.pm @@ -65,6 +65,10 @@ sub default_storagemodel { 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. + + efi-secureboot also sets secureboot, which guest_os turns into the requirement + libvirt reads. efi alone states no requirement, so libvirt takes the descriptor of + highest priority that fits the guest, and that order belongs to the hypervisor. Arguments: $guest_arch - nodetype.arch of the node, or undef $hyp_cpumodel - the cpumodel of the hypervisor, or undef @@ -82,6 +86,7 @@ sub guest_arch_profile { arch => undef, machine => undef, firmware => undef, + secureboot => 0, cpu_mode => undef, cpu_model => undef, pseries => 0, @@ -130,12 +135,15 @@ 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'; + if (defined($firmware) and $firmware =~ /^efi(?:-secureboot)?$/) { + $profile{secureboot} = 1 if $firmware eq 'efi-secureboot'; + unless (defined $profile{firmware}) { + $profile{firmware} = 'efi'; + if ($profile{x86_features}) { + $profile{machine} = 'q35'; + $profile{disk_model} = 'scsi'; + $profile{cd_prefix} = 'sd'; + } } } return \%profile; @@ -433,6 +441,8 @@ sub vm_othersettings { 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. + + efi asks for UEFI. efi-secureboot asks for UEFI that verifies what it boots. Arguments: $othersettings - vm.othersettings of the node, or undef Returns: @@ -456,20 +466,59 @@ sub requested_firmware { Descriptions: The refusal of a firmware value that libvirt cannot select. A node with a typo must - not stay on SeaBIOS without a message. + not stay on SeaBIOS without a message, nor lose a Secure Boot requirement it asked + for. 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 + the error message, or undef when the value is efi, efi-secureboot 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."; + return undef if !defined($firmware) or $firmware =~ /^efi(?:-secureboot)?$/; + return "vm.othersettings firmware:$firmware names no firmware libvirt can select for $node. Use firmware:efi or firmware:efi-secureboot."; +} + +#------------------------------------------------------- + +=head3 firmware_machine_error + + Descriptions: + The refusal of a machine type that cannot carry the firmware the node asks for. + + Every x86_64 OVMF descriptor targets pc-q35-* only, and guest_os writes a machine: + setting of vm.othersettings over the machine type guest_arch_profile chose. Without + this refusal libvirt answers "Unable to find 'efi' firmware that is compatible with + the current configuration", which names neither the node nor either setting. + + Only the i440fx family is judged, and only on x86_64. xCAT does not know which + machine types a hypervisor offers, so any other value goes to libvirt, which reports + a machine type it cannot use. + + build_xmldesc returns the message, and both mkvm and makedom call build_xmldesc, so + the refusal reaches mkvm and the rpower boot of a node whose domain is not yet built. + Arguments: + $firmware - the result of requested_firmware, or undef + $machine - the machine: setting of vm.othersettings, or undef + $arch - nodetype.arch of the node, or undef + $node - the node name + Returns: + the error message, or undef when the pair can boot + +=cut + +#------------------------------------------------------- +sub firmware_machine_error { + my ($firmware, $machine, $arch, $node) = @_; + return undef unless defined($firmware) and defined($machine); + return undef unless defined($arch) and $arch eq 'x86_64'; + return undef unless $machine eq 'pc' or $machine =~ /^(?:pc-)?i440fx/; + return "vm.othersettings firmware:$firmware needs a q35 machine type, and $node names" + . " machine:$machine. Use machine:q35, or leave the machine type out."; } #------------------------------------------------------- @@ -481,6 +530,13 @@ sub firmware_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 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 @@ -508,6 +564,7 @@ sub guest_os { $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}; + $os{loader}->{secure} = 'yes' if $profile->{secureboot}; delete $os{bios} unless $profile->{bios}; $os{type}->{machine} = $args{machine} if defined $args{machine}; return \%os; @@ -515,6 +572,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/perl-xCAT/xCAT/MediaPerms.pm b/perl-xCAT/xCAT/MediaPerms.pm new file mode 100644 index 0000000000..ce3f06a278 --- /dev/null +++ b/perl-xCAT/xCAT/MediaPerms.pm @@ -0,0 +1,89 @@ +package xCAT::MediaPerms; + +use strict; +use warnings; + +use File::Find (); + +#------------------------------------------------------------------------------- + +=head1 NAME + +xCAT::MediaPerms - the modes of a copied install tree + +=head1 DESCRIPTION + +xCAT serves the install directory over HTTP, and copycds copies the media with the modes the +media carries. EL media ships the EFI binaries of its boot loader mode 0700, so the web server +cannot read them and a node that fetches one over HTTP is answered 403. + +=cut + +#------------------------------------------------------------------------------- + +=head3 readable_mode + + Descriptions: + The mode one path needs so that the web server can read it. + + Read permission for the group and for others. A directory also needs search + permission, because a mode without it hides the files under it. No write permission, + and no execute permission on a file that has none. + Arguments: + $mode - the current mode, as stat reports it + $is_dir - true for a directory + Returns: + the new mode, or undef when the path needs no change + +=cut + +#------------------------------------------------------------------------------- +sub readable_mode { + my ($mode, $is_dir) = @_; + return undef unless defined $mode; + $mode &= 07777; + my $wanted = $mode | 0044; + $wanted |= 0011 if $is_dir; + return undef if $wanted == $mode; + return $wanted; +} + +#------------------------------------------------------------------------------- + +=head3 make_readable + + Descriptions: + Give every path under one directory the mode readable_mode names. The directory + itself is included. + Arguments: + $dir - the directory, which need not exist + Returns: + the number of paths whose mode changed + +=cut + +#------------------------------------------------------------------------------- +sub make_readable { + my ($dir) = @_; + return 0 unless defined($dir) and -d $dir; + + my $changed = 0; + File::Find::find( + { + no_chdir => 1, + wanted => sub { + my $path = $File::Find::name; + # A symbolic link carries no mode of its own, and chmod would follow it out of + # the tree. + return if -l $path; + my @st = stat $path or return; + my $new = readable_mode($st[2], -d _) or return; + $changed++ if chmod $new, $path; + }, + }, + $dir + ); + return $changed; +} + +1; diff --git a/xCAT-server/lib/xcat/plugins/anaconda.pm b/xCAT-server/lib/xcat/plugins/anaconda.pm index dcc6c3859e..b695ba2a57 100644 --- a/xCAT-server/lib/xcat/plugins/anaconda.pm +++ b/xCAT-server/lib/xcat/plugins/anaconda.pm @@ -19,6 +19,7 @@ use xCAT::MsgUtils; use xCAT::SvrUtils; use xCAT::UFTP; use xCAT::Yum; +use xCAT::MediaPerms; use xCAT::SELinux; use Getopt::Long; @@ -2658,6 +2659,11 @@ sub copycd #my $rc = system("cd $path;rsync -a . $installroot/$distname/$arch/"); chmod 0755, "$path"; + # The media carries its own modes, and EL media ships the EFI binaries of its boot loader + # mode 0700. xCAT serves the install directory over HTTP, so the web server answers 403 for + # one, and a node with Secure Boot on cannot fetch the shim that verifies its kernel. + xCAT::MediaPerms::make_readable($path); + #append the fingerprint to the .fingerprint file to indicate that the os media has been copied in unless ($disccopiedin) { diff --git a/xCAT-server/lib/xcat/plugins/debian.pm b/xCAT-server/lib/xcat/plugins/debian.pm index 1b93307d0b..7145e63562 100644 --- a/xCAT-server/lib/xcat/plugins/debian.pm +++ b/xCAT-server/lib/xcat/plugins/debian.pm @@ -10,6 +10,7 @@ use Storable qw(dclone); use Sys::Syslog; use File::Temp qw/tempdir/; use xCAT::Table; +use xCAT::MediaPerms; use xCAT::Utils; use xCAT::SvrUtils; use xCAT::UFTP; @@ -673,6 +674,10 @@ sub copycd # ); chmod 0755, "$installroot/$distname/$arch"; + # The media carries its own modes. xCAT serves the install directory over HTTP, so a file + # the web server cannot read is answered 403 and the node that fetches it cannot boot. + xCAT::MediaPerms::make_readable("$installroot/$distname/$arch"); + # Need to do this otherwise there will be warning about corrupt Packages file # when installing a system diff --git a/xCAT-server/lib/xcat/plugins/kvm.pm b/xCAT-server/lib/xcat/plugins/kvm.pm index 73cefeaefe..e0e70bff6e 100644 --- a/xCAT-server/lib/xcat/plugins/kvm.pm +++ b/xCAT-server/lib/xcat/plugins/kvm.pm @@ -642,7 +642,7 @@ sub build_xmldesc { #memory binding: "membind:" #cpu mode: "cpumode:" #machine type: "machine:" - #firmware: "firmware:efi" + #firmware: "firmware:efi" or "firmware:efi-secureboot" if ($advsettings) { my @tmp_array = split ";", $advsettings; foreach (@tmp_array) { @@ -674,6 +674,10 @@ sub build_xmldesc { } } + my $machine_error = xCAT::KVMUtils::firmware_machine_error($firmware, $machine_type, + $confdata->{nodetype}->{$node}->[0]->{arch}, $node); + return (-1, $machine_error) if $machine_error; + # Support VM machine types specified through vm.othersettings, e.g., q35, pseries-rhel7.4.0. $xtree{os} = xCAT::KVMUtils::guest_os($profile, hyp_cpumodel => $hypcpumodel, @@ -885,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/autotest/testcase/commoncmd/vm_machine_type.sh b/xCAT-test/autotest/testcase/commoncmd/vm_machine_type.sh index 0f5914bdd2..5495540643 100755 --- a/xCAT-test/autotest/testcase/commoncmd/vm_machine_type.sh +++ b/xCAT-test/autotest/testcase/commoncmd/vm_machine_type.sh @@ -33,12 +33,19 @@ vm_machine_type_private_dir() fi } -# alias -- the machine name qemu keeps pointed at the newest machine of a family. +# alias [] -- the machine name qemu keeps pointed at the newest machine +# of a family. An x86_64 node that asks for a UEFI firmware takes q35: every x86_64 OVMF +# descriptor targets pc-q35-*, and libvirt refuses firmware efi on i440fx. vm_machine_type_alias() { case "$1" in ppc64*) echo 'pseries' ;; - x86_64) echo 'pc' ;; + x86_64) + case ";${2-}" in + *';firmware:efi'*) echo 'q35' ;; + *) echo 'pc' ;; + esac + ;; riscv64) echo 'virt' ;; aarch64) echo 'virt' ;; *) return 1 ;; @@ -84,19 +91,22 @@ vm_machine_type_default() sed -n 's|.*\(.*\).*|\1|p' | head -n 1 } -# for_arch [] -- the machine type to write, as "machine:". +# for_arch [] [] -- the machine type to write, as +# "machine:". # # The vmhost is what refuses the domain, so its libvirt picks the name: the plain # alias when that libvirt accepts it, its own default when it does not, and the # alias again when the vmhost cannot be asked. A versioned name does not survive # a host upgrade -- no qemu on AlmaLinux 10 has a pseries-rhel machine type. +# q35 does not fall back to the default: the x86_64 default is i440fx, which UEFI cannot use. vm_machine_type_for_arch() { local arch="$1" vmhost="${2-}" generic libvirt_arch name - generic="$(vm_machine_type_alias "$arch")" || return 1 + generic="$(vm_machine_type_alias "$arch" "${3-}")" || return 1 libvirt_arch="$(vm_machine_type_libvirt_arch "$arch")" || return 1 - if vm_machine_type_supported "$vmhost" "$libvirt_arch" "$generic"; then + if [ "$generic" = q35 ] || + vm_machine_type_supported "$vmhost" "$libvirt_arch" "$generic"; then printf 'machine:%s\n' "$generic" return fi @@ -176,7 +186,7 @@ vm_machine_type_apply() chdef "$node" "vmothersetting=$original" return fi - if ! machine="$(vm_machine_type_for_arch "$arch" "$(vm_machine_type_vmhost "$node")")"; then + if ! machine="$(vm_machine_type_for_arch "$arch" "$(vm_machine_type_vmhost "$node")" "$original")"; then echo "vm_machine_type.sh: no machine type for arch '$arch'" >&2 return 1 fi diff --git a/xCAT-test/bats/vm_machine_type.bats b/xCAT-test/bats/vm_machine_type.bats index 3d37c76d80..269ae36475 100644 --- a/xCAT-test/bats/vm_machine_type.bats +++ b/xCAT-test/bats/vm_machine_type.bats @@ -149,6 +149,23 @@ assert_arch() assert_arch x86_64 pc 'cpumode:host-passthrough' } +# An x86_64 node that asks for UEFI needs q35: every x86_64 OVMF descriptor targets pc-q35-*, +# and libvirt refuses a domain that asks for firmware efi on i440fx with "Unable to find 'efi' +# firmware that is compatible with the current configuration". +@test "x86_64 with firmware:efi: the case sets and clears q35" { + assert_arch x86_64 q35 'firmware:efi' + assert_arch x86_64 q35 'cpumode:host-passthrough;firmware:efi' +} + +@test "x86_64 with firmware:efi-secureboot: the case sets and clears q35" { + assert_arch x86_64 q35 'firmware:efi-secureboot' + assert_arch x86_64 q35 'cpumode:host-passthrough;firmware:efi-secureboot' +} + +@test "round trip: x86_64 with firmware:efi-secureboot" { + assert_round_trip x86_64 'cpumode:host-passthrough;firmware:efi-secureboot' q35 +} + @test "riscv64: the case sets and clears virt" { assert_arch riscv64 virt '' assert_arch riscv64 virt 'cpumode:host-passthrough' @@ -237,10 +254,19 @@ assert_arch() [ "$output" = 'machine:pc' ] } -# assert_round_trip -- the case ends with the exact value it started with. +@test "x86_64 with firmware:efi: q35 stays when the vmhost refuses the alias" { + MACHINES='pc-i440fx-rhel10.0.0' + DEFAULT_MACHINE='pc-i440fx-rhel10.0.0' + run vm_machine_type_for_arch x86_64 192.0.2.10 'firmware:efi' + [ "$status" -eq 0 ] + [ "$output" = 'machine:q35' ] +} + +# assert_round_trip [] -- the case ends with the exact value it +# started with. is the machine type apply is to have written, when the caller knows it. assert_round_trip() { - local arch="$1" original="$2" machine + local arch="$1" original="$2" expected="$3" machine set_vmother "$original" vm_machine_type_main invalid cn1 @@ -249,6 +275,10 @@ assert_round_trip() [ "$machine" = "machine:invalid" ] vm_machine_type_main apply cn1 "$arch" + if [ -n "$expected" ]; then + machine="$(vmother | tr ';' '\n' | grep '^machine:' | tail -n 1)" + [ "$machine" = "machine:$expected" ] + fi vm_machine_type_main restore cn1 "$arch" [ "$(vmother)" = "$original" ] [ -z "$(ls -A "$VM_MACHINE_TYPE_STATE_DIR")" ] @@ -263,7 +293,7 @@ assert_round_trip() } @test "round trip: cpumode:host-passthrough" { - assert_round_trip x86_64 'cpumode:host-passthrough' + assert_round_trip x86_64 'cpumode:host-passthrough' pc } @test "round trip: x86_64 with a versioned machine type" { diff --git a/xCAT-test/debian/install b/xCAT-test/debian/install index 14a97a1eba..5510397e7a 100644 --- a/xCAT-test/debian/install +++ b/xCAT-test/debian/install @@ -7,3 +7,4 @@ unit opt/xcat/share/xcat/tools/autotest integration opt/xcat/share/xcat/tools/autotest blackboxtest opt/xcat/share/xcat/tools/autotest lib/xCAT/Test/OS.pm opt/xcat/lib/perl/xCAT/Test +lib/xCAT/Test/Conf.pm opt/xcat/lib/perl/xCAT/Test diff --git a/xCAT-test/lib/xCAT/Test/Conf.pm b/xCAT-test/lib/xCAT/Test/Conf.pm new file mode 100644 index 0000000000..b5e74e7177 --- /dev/null +++ b/xCAT-test/lib/xCAT/Test/Conf.pm @@ -0,0 +1,181 @@ +package xCAT::Test::Conf; + +use strict; +use warnings; + +#------------------------------------------------------------------------------- + +=head1 NAME + +xCAT::Test::Conf - the lines and commands of an xcattest configuration file + +=head1 DESCRIPTION + +xcattest reads a configuration file and applies its [Object_*] and [Table_*] blocks to the +xCAT database before the first case runs. The routines here read one line of such a block and +build the command that applies it. Each one takes its input and returns its answer, so the +reader and the command can be measured without a database. + +=cut + +#------------------------------------------------------------------------------- + +=head3 object_attr + + Descriptions: + The attribute and value of one line of an [Object_*] block. + + The semicolon is in the value, because vm.othersettings separates its settings with + one. chdef_command quotes what this routine returns. + Arguments: + $line - the line, with or without leading whitespace + Returns: + ($attr, $value), or the empty list when the line holds no assignment + +=cut + +#------------------------------------------------------------------------------- +sub object_attr { + my ($line) = @_; + return () unless defined $line; + return ($1, $2) if $line =~ /(\w+)\s*=\s*([:,;\w\.\-\/]+)/; + return (); +} + +#------------------------------------------------------------------------------- + +=head3 table_attr + + Descriptions: + The attribute and value of one line of a [Table_*] block. + Arguments: + $line - the line, with or without leading whitespace + Returns: + ($attr, $value), or the empty list when the line holds no assignment + +=cut + +#------------------------------------------------------------------------------- +sub table_attr { + my ($line) = @_; + return () unless defined $line; + # The same value class as object_attr. A table value holds the same kinds of thing an object + # value does: an IPv6 prefix (2001:db8::/64), a path (/install), a comma-separated list of + # name servers. The narrower ([\w\.\-]+) this replaces stopped at the first colon or slash and + # wrote the truncated value, so `net=2001:db8:176::/64` reached chtab as `net=2001` and + # xcattest re-applied that row on every run. Nothing warned. + # + # The class holds no whitespace and no '#', so a trailing inline comment is still excluded. + return ($1, $2) if $line =~ /(\w+)\s*=\s*([:,;\w\.\-\/]+)/; + return (); +} + +#------------------------------------------------------------------------------- + +=head3 var_attr + + Descriptions: + The name and value of one line of the [System] or [Custom] block. + Arguments: + $line - the line, with or without leading whitespace + Returns: + ($name, $value), or the empty list when the line holds no assignment + +=cut + +#------------------------------------------------------------------------------- +sub var_attr { + my ($line) = @_; + return () unless defined $line; + return ($1, $2) if $line =~ /(\w+)\s*=\s*([\w\.\-\+\ \/:]+)/; + return (); +} + +#------------------------------------------------------------------------------- + +=head3 chdef_command + + Descriptions: + The chdef command that applies one object of an [Object_*] block. + + xcattest runs every command through /bin/bash -c, so each word is quoted. A + vm.othersettings value carries a semicolon, which bash reads as a command separator. + + The assignments are sorted, so one conf builds one command. + Arguments: + $type - the object type, as the section name spells it + $name - the object name + $attrs - a hash reference of attribute to value + Returns: + the command + +=cut + +#------------------------------------------------------------------------------- +sub chdef_command { + my ($type, $name, $attrs) = @_; + my @words = ($type, $name); + foreach my $attr (sort keys %$attrs) { + my $value = defined $attrs->{$attr} ? $attrs->{$attr} : ''; + push @words, "$attr=$value"; + } + my ($qtype, $qname, @qattrs) = map { _quote($_) } @words; + return join ' ', 'chdef', '-t', $qtype, '-o', $qname, @qattrs; +} + +#------------------------------------------------------------------------------- + +=head3 chtab_command + + Descriptions: + The chtab command that applies one row of a [Table_*] block. + + xcattest runs every command through /bin/bash -c, so each word is quoted, the row + selector too. A vm.othersettings value carries a semicolon. + + The assignments are sorted, so one conf builds one command. + Arguments: + $table - the table name, as the section name spells it + $key - the key column of the row + $name - the value of the key column + $attrs - a hash reference of column to value + Returns: + the command + +=cut + +#------------------------------------------------------------------------------- +sub chtab_command { + my ($table, $key, $name, $attrs) = @_; + my @words = ("$key=$name"); + foreach my $attr (sort keys %$attrs) { + next if $attr eq '__KEY__'; + my $value = defined $attrs->{$attr} ? $attrs->{$attr} : ''; + push @words, "$table.$attr=$value"; + } + return join ' ', 'chtab', map { _quote($_) } @words; +} + +#------------------------------------------------------------------------------- + +=head3 _quote + + Descriptions: + One word of a command for /bin/bash -c. Single quotes stop the shell from reading any + character of the word, and a single quote inside the word closes and reopens them. + Arguments: + $word - the word + Returns: + the quoted word + +=cut + +#------------------------------------------------------------------------------- +sub _quote { + my ($word) = @_; + $word = '' unless defined $word; + $word =~ s/'/'\\''/g; + return "'$word'"; +} + +1; diff --git a/xCAT-test/unit/autotest_check_lines_are_understood.t b/xCAT-test/unit/autotest_check_lines_are_understood.t index 40b6ea3175..5d7a278a64 100644 --- a/xCAT-test/unit/autotest_check_lines_are_understood.t +++ b/xCAT-test/unit/autotest_check_lines_are_understood.t @@ -3,6 +3,7 @@ use strict; use warnings; use File::Copy qw(copy); +use File::Basename qw(basename); use File::Find qw(find); use File::Path qw(make_path); use File::Temp qw(tempdir); @@ -57,8 +58,12 @@ my $root = tempdir(CLEANUP => 1); make_path("$root/bin", "$root/cases", "$root/lib/perl/xCAT/Test"); copy($program, "$root/bin/xcattest") or die("copy xcattest: $!"); chmod 0755, "$root/bin/xcattest"; -copy("$FindBin::Bin/../lib/xCAT/Test/OS.pm", "$root/lib/perl/xCAT/Test/OS.pm") - or die("copy xCAT/Test/OS.pm: $!"); +# Every module under xCAT-test/lib/xCAT, because xcattest loads them from lib/perl beside its +# own directory and a missing one stops it before the first case. +foreach my $module (glob("$FindBin::Bin/../lib/xCAT/Test/*.pm")) { + my $name = basename($module); + copy($module, "$root/lib/perl/xCAT/Test/$name") or die("copy xCAT/Test/$name: $!"); +} open(my $fixture_fh, '>', "$root/cases/fixture") or die("write the fixture case: $!"); print $fixture_fh $fixture; close($fixture_fh) or die("close the fixture case: $!"); diff --git a/xCAT-test/unit/kvm_secureboot_firmware.t b/xCAT-test/unit/kvm_secureboot_firmware.t new file mode 100644 index 0000000000..51e97d2140 --- /dev/null +++ b/xCAT-test/unit/kvm_secureboot_firmware.t @@ -0,0 +1,189 @@ +#!/usr/bin/env perl +# An x86_64 KVM guest asks libvirt for UEFI with Secure Boot on with vm.othersettings +# "firmware:efi-secureboot". +# +# firmware:efi alone states no requirement about Secure Boot. libvirt then takes the firmware +# descriptor of highest priority that fits the guest, and that order is host policy: the same +# node boots with Secure Boot on on one hypervisor and off on another, and the domain reports +# 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 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; + +# The firmware and the profile that build_xmldesc reads 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) = @_; + return xCAT::KVMUtils::guest_os( $profile, hyp_cpumodel => 'x86_64' ); +} + +is( xCAT::KVMUtils::requested_firmware('firmware:efi-secureboot'), + 'efi-secureboot', 'firmware:efi-secureboot is read whole, not truncated to efi' ); +is( xCAT::KVMUtils::firmware_error( 'efi-secureboot', 'cn1' ), + undef, 'and it is a firmware libvirt can select, so it is not refused' ); + +my $sb = node_profile('firmware:efi-secureboot'); +my $sb_os = os_of($sb); +is( $sb_os->{firmware}, 'efi', + 'firmware:efi-secureboot puts firmware="efi" on , which starts the firmware selection' ); +is( $sb_os->{loader}->{secure}, 'yes', + 'and , which narrows that selection to a Secure Boot firmware' ); +is( $sb_os->{type}->{machine}, 'q35', + 'the Secure Boot node is placed on q35, the only machine type the x86_64 OVMF descriptors target' ); +is( $sb->{disk_model}, 'scsi', 'and its disks take the scsi bus, because q35 has no IDE controller' ); +is( $sb_os->{bios}->{useserial}, 'yes', + 'and it keeps , so its boot still reaches the serial console' ); + +# firmware:efi must keep stating nothing about Secure Boot. A site that asked only for UEFI +# gets the firmware its hypervisor prefers, as it did before. +my $uefi_os = os_of( node_profile('firmware:efi') ); +is( $uefi_os->{firmware}, 'efi', 'firmware:efi still asks for UEFI' ); +is( $uefi_os->{loader}, undef, 'and states no Secure Boot requirement' ); +is( os_of( node_profile(undef) )->{loader}, + undef, 'a node that asks for no firmware states no Secure Boot requirement either' ); + +# A value libvirt cannot select is refused by name, and the refusal names both values a node +# may ask for. A typo must not leave the node on a firmware it did not ask for. +my $error = xCAT::KVMUtils::firmware_error( 'efi-secure', 'cn1' ); +like( $error, qr/firmware:efi-secure\b/, 'a value that is not a firmware is refused by name' ); +like( $error, qr/firmware:efi-secureboot/, + 'and the refusal names firmware:efi-secureboot, so the typo can be corrected from it' ); + +# vm.othersettings can name a machine type, and guest_os writes it over the one the profile +# chose. An x86_64 node that asks for efi is moved to q35 by guest_arch_profile, because every +# x86_64 OVMF descriptor targets pc-q35-* only, so a machine: setting naming i440fx takes the +# node to a machine type no UEFI firmware fits. libvirt then answers "Unable to find 'efi' +# firmware that is compatible with the current configuration", which names neither the node nor +# either setting. +my $conflict = xCAT::KVMUtils::firmware_machine_error( 'efi-secureboot', 'pc', 'x86_64', 'cn1' ); +like( $conflict, qr/\bcn1\b/, 'the refusal names the node' ); +like( $conflict, qr/firmware:efi-secureboot/, 'and the firmware the node asked for' ); +like( $conflict, qr/machine:pc\b/, 'and the machine type that cannot carry it' ); +like( $conflict, qr/q35/, 'and the machine type that can, so the conf can be corrected from it' ); + +like( xCAT::KVMUtils::firmware_machine_error( 'efi', 'pc-i440fx-rhel10.0.0', 'x86_64', 'cn1' ), + qr/machine:pc-i440fx-rhel10\.0\.0/, 'a versioned i440fx is refused by name' ); + +is( xCAT::KVMUtils::firmware_machine_error( 'efi-secureboot', 'q35', 'x86_64', 'cn1' ), + undef, 'q35 is not refused' ); + +# xCAT does not know which machine types a hypervisor offers, so it judges only the i440fx +# family, whose descriptors carry no UEFI firmware. libvirt reports any other machine type it +# cannot use, and reg_linux_diskless_installation_flat relies on mkvm accepting machine:invalid +# so that it can measure the provisioning failure that follows. +is( xCAT::KVMUtils::firmware_machine_error( 'efi-secureboot', 'invalid', 'x86_64', 'cn1' ), + undef, 'a machine type xCAT does not know is left to libvirt' ); +is( xCAT::KVMUtils::firmware_machine_error( 'efi', 'pc-q35-rhel10.2.0', 'x86_64', 'cn1' ), + undef, 'and so is a machine type that carries a UEFI firmware' ); +is( xCAT::KVMUtils::firmware_machine_error( 'efi', 'pc-q35-rhel9.4.0', 'x86_64', 'cn1' ), + undef, 'a versioned q35 is not refused' ); +is( xCAT::KVMUtils::firmware_machine_error( 'efi-secureboot', undef, 'x86_64', 'cn1' ), + undef, 'a node that names no machine type is not refused' ); +is( xCAT::KVMUtils::firmware_machine_error( undef, 'pc', 'x86_64', 'cn1' ), + undef, 'a node that asks for no firmware keeps i440fx' ); +# Only x86_64 is judged. The i440fx family and the q35 the refusal names belong to that +# architecture, so a node of another one goes to libvirt whatever it names. The machine type +# has to be one the x86_64 branch would refuse, or the test measures the i440fx match instead +# of the architecture guard. +is( xCAT::KVMUtils::firmware_machine_error( 'efi', 'pc', 'ppc64le', 'cn1' ), + undef, 'a machine type of the i440fx family is not judged on another architecture' ); +is( xCAT::KVMUtils::firmware_machine_error( 'efi', 'pc-i440fx-rhel10.0.0', 'aarch64', 'cn1' ), + undef, 'and neither is a versioned one' ); +is( xCAT::KVMUtils::firmware_machine_error( 'efi', 'pc', undef, 'cn1' ), + undef, 'a node whose nodetype.arch is unset is not judged either' ); +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(); diff --git a/xCAT-test/unit/media_perms_readable.t b/xCAT-test/unit/media_perms_readable.t new file mode 100644 index 0000000000..472549cc1c --- /dev/null +++ b/xCAT-test/unit/media_perms_readable.t @@ -0,0 +1,90 @@ +#!/usr/bin/env perl +# copycds copies the install media with the modes the media carries. EL media ships the EFI +# binaries of its boot loader mode 0700, and xCAT serves /install over HTTP, so the web server +# cannot read them. Measured on an AlmaLinux 10 tree copied by copycds: EFI/BOOT/grub.cfg is +# 0444 and answers 200, EFI/BOOT/BOOTX64.EFI and EFI/BOOT/mmx64.efi are 0700 and answer 403. +# +# make_readable adds read permission for the group and for others, and search permission on a +# directory. It never adds write permission, and it never adds execute permission to a file. +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; +use File::Path qw(make_path); +use File::Temp qw(tempdir); +use Test::More; + +my %before = %INC; +require xCAT::MediaPerms; +my @xcat_loaded = sort grep { m{^xCAT} and !exists $before{$_} } keys %INC; +is_deeply( \@xcat_loaded, ['xCAT/MediaPerms.pm'], 'xCAT::MediaPerms loads no other xCAT module' ); + +sub mode_of { + my ($path) = @_; + return sprintf '%04o', ( ( stat $path )[2] & 07777 ); +} + +my $root = tempdir( CLEANUP => 1 ); +make_path("$root/EFI/BOOT/fonts"); +foreach my $file (qw(EFI/BOOT/BOOTX64.EFI EFI/BOOT/mmx64.efi EFI/BOOT/grub.cfg EFI/BOOT/fonts/unicode.pf2)) { + open my $fh, '>', "$root/$file" or die "write $file: $!"; + print $fh 'x'; + close $fh or die "close $file: $!"; +} +# make_path takes the mode of the umask of the caller, so the modes of the two directories it +# creates are those of whoever runs the test. The count below is a count of the paths that +# change, so every mode in the fixture is set here. +chmod 0755, "$root/EFI", "$root/EFI/BOOT"; +chmod 0700, "$root/EFI/BOOT/BOOTX64.EFI", "$root/EFI/BOOT/mmx64.efi"; +chmod 0444, "$root/EFI/BOOT/grub.cfg"; +chmod 0600, "$root/EFI/BOOT/fonts/unicode.pf2"; +chmod 0700, "$root/EFI/BOOT/fonts"; + +# A symbolic link out of the tree. chmod follows a link, so make_readable has to skip it: the +# install directory of one osimage must not change the mode of a path in another. +my $outside = tempdir( CLEANUP => 1 ); +open my $out_fh, '>', "$outside/vmlinuz" or die "write vmlinuz: $!"; +close $out_fh or die "close vmlinuz: $!"; +chmod 0600, "$outside/vmlinuz"; +symlink "$outside/vmlinuz", "$root/EFI/BOOT/vmlinuz" or die "symlink: $!"; + +my $changed = xCAT::MediaPerms::make_readable("$root/EFI"); + +is( mode_of("$outside/vmlinuz"), '0600', + 'a symbolic link is skipped, so the path it names outside the tree keeps its mode' ); + +is( mode_of("$root/EFI/BOOT/BOOTX64.EFI"), '0744', + 'a boot loader the media ships 0700 becomes readable, and keeps its owner execute bit' ); +is( mode_of("$root/EFI/BOOT/mmx64.efi"), '0744', 'and so does the MOK manager beside it' ); +is( mode_of("$root/EFI/BOOT/fonts/unicode.pf2"), '0644', + 'a file with no execute bit stays without one' ); +is( mode_of("$root/EFI/BOOT/fonts"), '0755', + 'a directory the media ships 0700 gains search permission, so the files under it can be read' ); +is( mode_of("$root/EFI/BOOT/grub.cfg"), '0444', 'a file that is already readable is left alone' ); +is( $changed, 4, 'make_readable reports the number of paths it changed' ); + +is( xCAT::MediaPerms::make_readable("$root/EFI"), 0, + 'a second call changes nothing, so copycds can be re-run' ); + +# An osimage whose media carries no EFI directory is the common case, and copycds calls +# make_readable for it too. Without the -d test File::Find warns "Can't stat", which reaches +# the output of copycds as an error the admin cannot act on. +my @warnings; +my $absent = do { + local $SIG{__WARN__} = sub { push @warnings, $_[0] }; + xCAT::MediaPerms::make_readable("$root/no-such-directory"); +}; +is( $absent, 0, 'a directory that does not exist changes nothing' ); +is_deeply( \@warnings, [], 'and it is not reported, because copycds calls it for every osimage' ); + +# No write permission is ever added. /install holds the media of every osimage on the +# management node. +my $writable = tempdir( CLEANUP => 1 ); +open my $fh, '>', "$writable/kernel" or die "write kernel: $!"; +close $fh or die "close kernel: $!"; +chmod 0700, "$writable/kernel"; +xCAT::MediaPerms::make_readable($writable); +is( mode_of("$writable/kernel"), '0744', 'the file is readable and writable by its owner alone' ); + +done_testing(); diff --git a/xCAT-test/unit/xcattest_conf_chtab_command.t b/xCAT-test/unit/xcattest_conf_chtab_command.t new file mode 100644 index 0000000000..6fd3110593 --- /dev/null +++ b/xCAT-test/unit/xcattest_conf_chtab_command.t @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# xcattest applies each row of a [Table_*] block with chtab, and runs the command through +# /bin/bash -c. A vm.othersettings value separates its settings with a semicolon: +# +# othersettings=cpumode:host-passthrough;firmware:efi-secureboot +# +# Unquoted, bash ends the chtab command at the semicolon and runs the second setting as a +# command. These checks split the command the way the shell does and look at the words chtab +# receives. +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use Test::More; +use Text::ParseWords qw(parse_line); + +require xCAT::Test::Conf; + +# The words bash gives to chtab, or undef when the command holds an unquoted shell operator. +sub chtab_words { + my ($cmd) = @_; + my @words = parse_line(qr/\s+/, 0, $cmd); + return undef if grep { !defined $_ } @words; + my @tokens = parse_line(qr/\s+/, 1, $cmd); + return undef if grep { !/^'.*'$/s && /[;&|<>`\$()*?\[\]]/ } @tokens; + return \@words; +} + +my $cmd = xCAT::Test::Conf::chtab_command('vm', 'node', 'cn1', + { __KEY__ => 'node', othersettings => 'cpumode:host-passthrough;firmware:efi-secureboot' }); +is_deeply( + chtab_words($cmd), + [ 'chtab', 'node=cn1', 'vm.othersettings=cpumode:host-passthrough;firmware:efi-secureboot' ], + 'chtab receives both settings of a vm.othersettings value in one argument' +); + +$cmd = xCAT::Test::Conf::chtab_command('site', 'key', 'domain name', + { __KEY__ => 'key', value => 'a&b' }); +is_deeply( + chtab_words($cmd), + [ 'chtab', 'key=domain name', 'site.value=a&b' ], + 'the row selector is quoted like the assignments' +); + +$cmd = xCAT::Test::Conf::chtab_command('site', 'key', 'motd', + { __KEY__ => 'key', value => "it's here" }); +is_deeply( + chtab_words($cmd), + [ 'chtab', 'key=motd', "site.value=it's here" ], + 'a single quote in a value does not end the quoting' +); + +# The values a table already held pass through unchanged. +$cmd = xCAT::Test::Conf::chtab_command('networks', 'netname', 'prov6', + { __KEY__ => 'netname', net => '2001:db8:176::/64', mask => '/64', dynamicrange => 'a-b' }); +is_deeply( + chtab_words($cmd), + [ 'chtab', 'netname=prov6', 'networks.dynamicrange=a-b', 'networks.mask=/64', + 'networks.net=2001:db8:176::/64' ], + 'the key column is not written as an attribute, and the assignments are sorted' +); + +done_testing(); diff --git a/xCAT-test/unit/xcattest_conf_object_attr.t b/xCAT-test/unit/xcattest_conf_object_attr.t new file mode 100644 index 0000000000..f083536dfe --- /dev/null +++ b/xCAT-test/unit/xcattest_conf_object_attr.t @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# The [Object_*] blocks of an xcattest conf declare xCAT node attributes, and xcattest applies +# them with chdef. vm.othersettings holds several settings separated by a semicolon, which is the +# syntax the kvm plugin reads: +# +# vmothersetting=cpumode:host-passthrough;firmware:efi-secureboot +# +# Two things have to hold for that line to reach the node. The reader has to keep the whole +# value, and the chdef command has to quote it, because xcattest runs every command through +# /bin/bash -c and an unquoted semicolon ends the command there. +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use Test::More; + +require xCAT::Test::Conf; + +# The reader keeps every setting of a multi-setting value. +is_deeply( + [ xCAT::Test::Conf::object_attr(' vmothersetting=cpumode:host-passthrough;firmware:efi-secureboot') ], + [ 'vmothersetting', 'cpumode:host-passthrough;firmware:efi-secureboot' ], + 'an [Object_*] value keeps the settings after its first semicolon' +); + +# The values the reader already took stay as they are. +is_deeply( + [ xCAT::Test::Conf::object_attr(' vmnics=virbr-p147,virbr-p147,virbr-p147') ], + [ 'vmnics', 'virbr-p147,virbr-p147,virbr-p147' ], + 'a comma-separated value is unchanged' +); +is_deeply( + [ xCAT::Test::Conf::object_attr(' vmstorage=dir:///var/lib/libvirt/images/') ], + [ 'vmstorage', 'dir:///var/lib/libvirt/images/' ], + 'a value with a colon and slashes is unchanged' +); +is_deeply( [ xCAT::Test::Conf::object_attr('Name=xcat147-cn-tmp-243') ], + [ 'Name', 'xcat147-cn-tmp-243' ], 'the Name line is read like any other attribute' ); +is_deeply( [ xCAT::Test::Conf::object_attr('this line has no equals sign') ], + [], 'a line with no assignment yields no attribute' ); + +# chdef runs under /bin/bash -c, so each assignment is quoted. Without the quoting the shell +# reads the semicolon as a command separator and chdef gets half the value. +my $cmd = xCAT::Test::Conf::chdef_command( 'node', 'cn1', + { vmothersetting => 'cpumode:host-passthrough;firmware:efi-secureboot' } ); +like( $cmd, qr/^chdef -t 'node' -o 'cn1' /, 'chdef_command names the type and the object' ); +like( + $cmd, + qr/'vmothersetting=cpumode:host-passthrough;firmware:efi-secureboot'/, + 'and quotes the assignment, so bash does not read the semicolon as a command separator' +); + +# A single quote inside a value must not end the quoting. +like( + xCAT::Test::Conf::chdef_command( 'node', 'cn1', { usercomment => "it's here" } ), + qr/'usercomment=it'\\''s here'/, + 'a single quote in a value is escaped inside the quoted assignment' +); + +# The order of the assignments is fixed, so two runs of one conf build the same command. +is( xCAT::Test::Conf::chdef_command( 'node', 'cn1', { b => '2', a => '1', c => '3' } ), + "chdef -t 'node' -o 'cn1' 'a=1' 'b=2' 'c=3'", + 'the assignments are sorted by attribute name' ); + +done_testing(); diff --git a/xCAT-test/unit/xcattest_conf_table_value_class.t b/xCAT-test/unit/xcattest_conf_table_value_class.t new file mode 100644 index 0000000000..a1590085b9 --- /dev/null +++ b/xCAT-test/unit/xcattest_conf_table_value_class.t @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# A [Table_*] VALUE MAY CONTAIN A COLON OR A SLASH. table_attr DROPS THE REST OF IT. +# +# xcattest applies a conf's [Table_*] blocks itself, once, before the first case runs +# (setup_env_by_configure_file), so whatever table_attr returns is what reaches chtab. Its value +# class is ([\w\.\-]+) while object_attr, beside it in the same module, already allows ":,;/" -- +# so an [Object_node] ip= carries an IPv6 address and a [Table_networks] net= silently cannot. +# +# The narrow class is faithful to what it replaced: upstream xcattest still matches tables with +# ([\w\.\-]+) at line 817 and objects with ([\w\.\-\+\ \/:]+) at line 854. Moving the pair into +# xCAT::Test::Conf is what made the asymmetry visible and testable. +# +# Measured 2026-10-23 on a management node provisioning over IPv6: `net=2001:db8:176::/64` reached +# chtab as `net=2001`, and xcattest re-applied that junk row on every run. [Table_site] value= has +# the same hole, so site.master cannot be an IPv6 address either. Nothing warns: the line parses, +# the truncated value is written, and the case fails later for a reason that looks unrelated. +# +# The same hole swallows any path or time range a table value needs. +use strict; +use warnings; +use Test::More; +use FindBin; +use lib "$FindBin::Bin/../lib"; +use xCAT::Test::Conf (); + +my $table = xCAT::Test::Conf->can('table_attr'); +ok($table, 'xCAT::Test::Conf provides table_attr') or die "nothing to test\n"; + +# One pair per thing a table value legitimately holds. The value is what must come back WHOLE. +my @cases = ( + ['net=2001:db8:176::/64', 'net', '2001:db8:176::/64', 'an IPv6 prefix'], + [' gateway=2001:db8:176::1', 'gateway', '2001:db8:176::1', 'an IPv6 address'], + ['value=fd00::1', 'value', 'fd00::1', 'a unique-local address'], + ['master=192.168.201.76', 'master', '192.168.201.76', 'an IPv4 address, unchanged'], + ['installdir=/install', 'installdir', '/install', 'an absolute path'], + ['nameservers=2001:db8::1,2001:db8::2', 'nameservers', '2001:db8::1,2001:db8::2', + 'a comma-separated list'], + ['domain=xcat76.lab', 'domain', 'xcat76.lab', 'a dotted name, unchanged'], + ['key=system', 'key', 'system', 'a bare word, unchanged'], +); +for my $c (@cases) { + my ($line, $attr, $value, $what) = @$c; + my ($got_attr, $got_value) = $table->($line); + is($got_attr, $attr, "$what: the attribute is $attr"); + is($got_value, $value, "$what: the value survives whole"); +} + +# A line with no assignment is still the empty list, and an inline comment is still excluded -- +# the class holds no whitespace and no '#', so widening it does not start swallowing comments. +is_deeply([ $table->('# a comment') ], [], 'a comment line yields no assignment'); +is_deeply([ $table->('[Table_site]') ], [], 'a section header yields no assignment'); +my ($a, $v) = $table->('value=fd00::1 # the management address'); +is($v, 'fd00::1', 'an inline comment is not part of the value'); + +# CONTROL: object_attr already accepts these, which is why the asymmetry is the defect rather +# than the design. Without this a reader cannot tell whether colons were meant to be refused. +my $object = xCAT::Test::Conf->can('object_attr'); +ok($object, 'and object_attr, for comparison'); +if ($object) { + my (undef, $ov) = $object->('ip=2001:db8:176::20'); + is($ov, '2001:db8:176::20', 'control: an [Object_*] value already keeps an IPv6 address'); +} + +done_testing; diff --git a/xCAT-test/unit/xcattest_report_every_check.t b/xCAT-test/unit/xcattest_report_every_check.t index 44372e9f4e..69336fcc71 100644 --- a/xCAT-test/unit/xcattest_report_every_check.t +++ b/xCAT-test/unit/xcattest_report_every_check.t @@ -4,6 +4,7 @@ use warnings; use FindBin; use File::Copy qw(copy); +use File::Basename qw(basename); use File::Path qw(make_path); use File::Temp qw(tempdir); use Test::More; @@ -31,8 +32,12 @@ sub run_harness { make_path("$root/bin", "$root/cases", "$root/lib/perl/xCAT/Test"); copy($program, "$root/bin/xcattest") or die("copy xcattest: $!"); chmod 0755, "$root/bin/xcattest"; - copy("$FindBin::Bin/../lib/xCAT/Test/OS.pm", "$root/lib/perl/xCAT/Test/OS.pm") - or die("copy xCAT/Test/OS.pm: $!"); + # Every module under xCAT-test/lib/xCAT, because xcattest loads them from lib/perl beside + # its own directory and a missing one stops it before the first case. + foreach my $module (glob("$FindBin::Bin/../lib/xCAT/Test/*.pm")) { + my $name = basename($module); + copy($module, "$root/lib/perl/xCAT/Test/$name") or die("copy xCAT/Test/$name: $!"); + } open(my $case_fh, '>', "$root/cases/fixture") or die("write the fixture case: $!"); print $case_fh $case_text; diff --git a/xCAT-test/xCAT-test.spec b/xCAT-test/xCAT-test.spec index 6c1d859820..abb562554f 100644 --- a/xCAT-test/xCAT-test.spec +++ b/xCAT-test/xCAT-test.spec @@ -63,7 +63,9 @@ cp xcattest $RPM_BUILD_ROOT/%{prefix}/bin chmod 755 $RPM_BUILD_ROOT/%{prefix}/bin/* cp lib/xCAT/Test/OS.pm $RPM_BUILD_ROOT/%{prefix}/lib/perl/xCAT/Test +cp lib/xCAT/Test/Conf.pm $RPM_BUILD_ROOT/%{prefix}/lib/perl/xCAT/Test chmod 644 $RPM_BUILD_ROOT/%{prefix}/lib/perl/xCAT/Test/OS.pm +chmod 644 $RPM_BUILD_ROOT/%{prefix}/lib/perl/xCAT/Test/Conf.pm cp restapitest $RPM_BUILD_ROOT/%{prefix}/sbin chmod 755 $RPM_BUILD_ROOT/%{prefix}/sbin/* diff --git a/xCAT-test/xcattest b/xCAT-test/xcattest index a424ec6f1f..0f9f79512a 100755 --- a/xCAT-test/xcattest +++ b/xCAT-test/xcattest @@ -17,6 +17,7 @@ BEGIN } use lib "$::XCATROOT/lib/perl"; use xCAT::Test::OS; +use xCAT::Test::Conf; #--------------global attributes---------------- my $program_path = dirname(File::Spec->rel2abs(__FILE__)); @@ -814,9 +815,7 @@ sub load_config_file { $type = "Varible"; } elsif ($type eq "Table") { ##TABLE BLOCK## - if ($line =~ /(\w+)\s*=\s*([\w\.\-]+)/) { - $attr = $1; - $value = $2; + if ((($attr, $value) = xCAT::Test::Conf::table_attr($line)) == 2) { if ($name && ($$config_ref{table}{$sub_type}{$name}{__KEY__} ne $attr)) { $$config_ref{table}{$sub_type}{$name}{$attr} = $value; } else { @@ -826,9 +825,7 @@ sub load_config_file { } } elsif ($type eq "Object") { ##OBJECT BLOCK## - if ($line =~ /(\w+)\s*=\s*([:,\w\.\-\/]+)/) { - $attr = $1; - $value = $2; + if ((($attr, $value) = xCAT::Test::Conf::object_attr($line)) == 2) { if ($attr eq "Name") { $name = $value; } elsif (!defined($name)) { @@ -851,8 +848,8 @@ sub load_config_file { } } elsif ($type eq "Varible") { ##NODE_BLOCK## - if ($line =~ /(\w+)\s*=\s*([\w\.\-\+\ \/:]+)/) { - $$config_ref{var}{$1} = $2; + if ((my ($var, $val) = xCAT::Test::Conf::var_attr($line)) == 2) { + $$config_ref{var}{$var} = $val; } } } @@ -1581,10 +1578,8 @@ sub setup_env_by_configure_file { if (exists $$config_ref{object}) { foreach my $type (keys %{ $$config_ref{object} }) { foreach my $name (keys %{ $$config_ref{object}{$type} }) { - $cmd = "chdef -t $type -o $name"; - foreach my $attr (keys %{ $$config_ref{object}{$type}{$name} }) { - $cmd = $cmd . " $attr=$$config_ref{object}{$type}{$name}{$attr}"; - } + $cmd = xCAT::Test::Conf::chdef_command($type, $name, + $$config_ref{object}{$type}{$name}); log_this($running_log_fd, "$cmd"); runcmd($cmd); if ($::RUNCMD_RC != 0) { @@ -1598,12 +1593,9 @@ sub setup_env_by_configure_file { if (exists $$config_ref{table}) { foreach my $type (keys %{ $$config_ref{table} }) { foreach my $name (keys %{ $$config_ref{table}{$type} }) { - $cmd = "chtab $$config_ref{table}{$type}{$name}{__KEY__}=$name"; - foreach my $attr (keys %{ $$config_ref{table}{$type}{$name} }) { - if ($attr ne '__KEY__') { - $cmd = $cmd . " $type.$attr=$$config_ref{table}{$type}{$name}{$attr}"; - } - } + $cmd = xCAT::Test::Conf::chtab_command($type, + $$config_ref{table}{$type}{$name}{__KEY__}, $name, + $$config_ref{table}{$type}{$name}); log_this($running_log_fd, "$cmd"); &runcmd($cmd); if ($::RUNCMD_RC != 0) {