From d9936e4a6040493547694433f865ab84974098f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:42:44 -0300 Subject: [PATCH 1/6] fix(grub2): validate boot inputs before replacing configuration Rejected protocols, unresolved addresses and missing loaders can leave a partial configuration after removing the previous files. Check these inputs before replacing the node configuration and its aliases. --- xCAT-server/lib/xcat/plugins/grub2.pm | 80 +++++++++++++-------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/grub2.pm b/xCAT-server/lib/xcat/plugins/grub2.pm index ba048ff348..2d0f576b04 100644 --- a/xCAT-server/lib/xcat/plugins/grub2.pm +++ b/xCAT-server/lib/xcat/plugins/grub2.pm @@ -187,27 +187,9 @@ sub setstate { return (1, "Cannot determine current state for this node"); } - # remove the old boot configuration files and create a new one, but only if not offline directive - system("find $bootloader_root/ -inum \$(stat --printf \%i $bootloader_root/$node 2>/dev/null) -exec rm -f {} \\; 2>/dev/null"); - - my $pcfg; - if ($cref->{currstate} ne "offline") { - open($pcfg, '>', "$bootloader_root/" . $node); - print $pcfg "#" . $cref->{currstate} . "\n"; - - if (($::XCATSITEVALS{xcatdebugmode} eq "1") or ($::XCATSITEVALS{xcatdebugmode} eq "2")) { - print $pcfg "set debug=all\n"; - } - - print $pcfg "set timeout=5\n"; - } - - if ($cref->{currstate} eq "boot") { - close($pcfg); - } elsif ($kern and $kern->{kernel}) { - - #It's time to set grub configuration for this node to boot the kernel.. - #get tftpserver + my $serverip; + my $grub2protocol = "tftp"; + if ($cref->{currstate} ne "boot" and $kern and $kern->{kernel}) { my $tftpserver; if (defined($nrhash{$node}->[0]) && $nrhash{$node}->[0]->{'tftpserver'}) { $tftpserver = $nrhash{$node}->[0]->{'tftpserver'}; @@ -217,8 +199,6 @@ sub setstate { $tftpserver = ""; } - my $serverip; - if($tftpserver eq ""){ my @nxtsrvd = xCAT::NetworkUtils->my_ip_facing($node); unless ($nxtsrvd[0]) { @@ -239,19 +219,52 @@ sub setstate { } unless($serverip){ - close($pcfg); return (1, "Unable to determine the tftpserver for $node"); } - my $grub2protocol = "tftp"; if (defined($nrhash{$node}->[0]) && $nrhash{$node}->[0]->{'netboot'} && ($nrhash{$node}->[0]->{'netboot'} =~ /grub2-(.*)/)) { $grub2protocol = $1; } unless ($grub2protocol =~ /^(http|tftp)$/) { - close($pcfg); return (1, "Invalid netboot method, please check noderes.netboot for $node"); } + } + + unless ($nodearch) { + return (1, "No archictecture defined in nodetype table for the node."); + } + if ($nodearch =~ /ppc64/i) { + $nodearch = "ppc" + } + my $grub2bin = "$bootloader_root/grub2." . $nodearch; + unless (-e "$grub2bin") { + return (1, "Stop grub2 configuration for this node, \"$grub2bin\" does not exits."); + } + + my $ip = xCAT::NetworkUtils->getipaddr($node); + unless ($ip) { + return (1, "xCAT unable to resolve IP in grub2 plugin"); + } + + # Keep the previous configuration until the requested boot inputs pass validation. + system("find $bootloader_root/ -inum \$(stat --printf \%i $bootloader_root/$node 2>/dev/null) -exec rm -f {} \\; 2>/dev/null"); + + my $pcfg; + if ($cref->{currstate} ne "offline") { + open($pcfg, '>', "$bootloader_root/" . $node); + print $pcfg "#" . $cref->{currstate} . "\n"; + + if (($::XCATSITEVALS{xcatdebugmode} eq "1") or ($::XCATSITEVALS{xcatdebugmode} eq "2")) { + print $pcfg "set debug=all\n"; + } + + print $pcfg "set timeout=5\n"; + } + + if ($cref->{currstate} eq "boot") { + close($pcfg); + } elsif ($kern and $kern->{kernel}) { # write entries to boot config file, but only if not offline directive if ($cref and $cref->{currstate} ne "offline") { @@ -301,17 +314,6 @@ sub setstate { close($pcfg); } - unless ($nodearch) { - return (1, "No archictecture defined in nodetype table for the node."); - } - if ($nodearch =~ /ppc64/i) { - $nodearch = "ppc" - } - my $grub2bin = "$bootloader_root/grub2." . $nodearch; - unless (-e "$grub2bin") { - return (1, "Stop grub2 configuration for this node, \"$grub2bin\" does not exits."); - } - # grub2- stays for DHCP configurations that makedhcp did not write. my @loaders = ("grub2-$node"); my $dhcp_loader = xCAT::DHCP::BootPolicy->grub2_node_loader(node => $node, arch => $nodearch); @@ -335,10 +337,6 @@ sub setstate { } } - my $ip = xCAT::NetworkUtils->getipaddr($node); - unless ($ip) { - return (1, "xCAT unable to resolve IP in grub2 plugin"); - } #my $mactab = xCAT::Table->new('mac'); my %ipaddrs; my $macstring; From e5df3dde1b4e010529038966310683dfac760e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:44:45 -0300 Subject: [PATCH 2/6] test(grub2): preserve boot artifacts on rejected updates --- xCAT-test/unit/grub2_preserve_config.t | 118 +++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 xCAT-test/unit/grub2_preserve_config.t diff --git a/xCAT-test/unit/grub2_preserve_config.t b/xCAT-test/unit/grub2_preserve_config.t new file mode 100644 index 0000000000..bc46f92cc7 --- /dev/null +++ b/xCAT-test/unit/grub2_preserve_config.t @@ -0,0 +1,118 @@ +#!/usr/bin/env perl +use strict; +use warnings; +no warnings 'once'; + +use Cwd qw(getcwd); +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use FindBin; +use Test::More; +use lib "$FindBin::Bin/../lib"; +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use XCAT::Test::File qw(repo_path); + +my $root = tempdir(CLEANUP => 1); +local $ENV{XCATROOT} = repo_path('xCAT-server'); +local $ENV{XCATCFG} = "$root/config"; +make_path($ENV{XCATCFG}); +local %::XCATSITEVALS = (tftpdir => "$root/tftpboot", xcatdebugmode => 0); +require xCAT::Table; +require xCAT::NetworkUtils; + +no warnings 'redefine'; +local *xCAT::Table::new = sub { die 'Unexpected database access'; }; +require(repo_path('xCAT-server/lib/xcat/plugins/grub2.pm')); + +sub snapshot { + my ($directory) = @_; + opendir(my $dh, $directory) or die $!; + my %files; + for my $name (sort grep { !/^\./ } readdir($dh)) { + my $file = "$directory/$name"; + my @stat = lstat($file); + $files{$name} = { + inode => $stat[1], mode => $stat[2], links => $stat[3], + uid => $stat[4], gid => $stat[5], mtime => $stat[9], + value => -l $file ? readlink($file) : scalar read_text($file), + }; + } + closedir($dh) or die $!; + return \%files; +} + +my @cases = ( + { name => 'invalid protocol', protocol => 'grub2-ftp', + error => 'Invalid netboot method, please check noderes.netboot for node' }, + { name => 'unresolved server', server => 'missing-server', + error => 'xCAT unable to resolve missing-server' }, + { name => 'failed facing address', server => '', facing => [1, 'no facing network'], + error => 'no facing network' }, + { name => 'empty facing address', server => '', facing => [0, undef], + error => 'Unable to determine the tftpserver for node' }, + { name => 'unresolved node', node_ip => undef, + error => 'xCAT unable to resolve IP in grub2 plugin' }, + { name => 'unresolved boot node', state => 'boot', node_ip => undef, + error => 'xCAT unable to resolve IP in grub2 plugin' }, + { name => 'unresolved offline node', state => 'offline', node_ip => undef, + error => 'xCAT unable to resolve IP in grub2 plugin' }, + { name => 'missing architecture', missing_arch => 1, + error => 'No archictecture defined in nodetype table for the node.' }, + { name => 'missing loader', missing_loader => 1 }, + { name => 'missing state', state => '', + error => 'Cannot determine current state for this node' }, +); + +for my $arch (qw(x86_64 ppc64le aarch64 riscv64)) { + for my $case (@cases) { + subtest "$arch: $case->{name}" => sub { + my $tftpdir = tempdir(DIR => $root, CLEANUP => 1); + my $bootdir = "$tftpdir/boot/grub2"; + make_path($bootdir); + my $loader_arch = $arch eq 'ppc64le' ? 'ppc' : $arch; + my $loader = "$bootdir/grub2.$loader_arch"; + write_text($loader, "loader\n") unless $case->{missing_loader}; + write_text("$bootdir/grub2.previous", "previous loader\n"); + symlink('grub2.previous', "$bootdir/grub2-node") or die $!; + symlink('grub2.previous', "$bootdir/grub2-node.efi") or die $! + if $arch eq 'aarch64'; + write_text("$bootdir/node", "#shell\nset timeout=3\n"); + chmod 0640, "$bootdir/node" or die $!; + for my $alias (qw(grub.cfg-C0000202 grub.cfg-01-02-00-00-00-00-02 old-alias)) { + link("$bootdir/node", "$bootdir/$alias") or die $!; + } + write_text("$bootdir/unrelated", "#boot\n"); + my $before = snapshot($bootdir); + my $node_ip = exists $case->{node_ip} ? $case->{node_ip} : '192.0.2.2'; + local *xCAT::NetworkUtils::getipaddr = sub { + my ($class, $host) = @_; + return '192.0.2.1' if $host eq 'server'; + return $node_ip if $host eq 'node'; + return if $host eq 'missing-server'; + die "Unexpected hostname: $host"; + }; + local *xCAT::NetworkUtils::my_ip_facing = sub { + return @{ $case->{facing} || [0, '192.0.2.1'] }; + }; + my $cwd = getcwd(); + my @result = xCAT_plugin::grub2::setstate( + 'node', + {node => [{kernel => 'images/vmlinuz', initrd => 'images/initrd.img', kcmdline => 'quiet'}]}, + {node => [{currstate => $case->{state} // 'install'}]}, + {node => [{mac => '02:00:00:00:00:02'}]}, + $tftpdir, + {node => [{tftpserver => $case->{server} // 'server', netboot => $case->{protocol} // 'grub2-tftp'}]}, + {}, $case->{missing_arch} ? undef : $arch, 'linux', + ); + chdir($cwd) or die "Cannot restore working directory: $!"; + my $error = $case->{error} // "Stop grub2 configuration for this node, \"$loader\" does not exits."; + is_deeply(\@result, [1, $error], 'the requested update is rejected'); + is_deeply(snapshot($bootdir), $before, 'all prior artifacts and hard links remain unchanged'); + is(xCAT_plugin::grub2::getstate('node', $tftpdir), 'shell', 'reported state remains the previous state'); + }; + } +} + +done_testing(); From 97a0c8d7ac9ba9f07343163d02fe52976fcfae5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:45:22 -0300 Subject: [PATCH 3/6] fix(destiny): match complete states when advancing the chain Substring and regular-expression matches can mistake a different boot state or image name for a completed chain. Compare the full state values while preserving the completed boot exception. --- xCAT-server/lib/xcat/plugins/destiny.pm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xCAT-server/lib/xcat/plugins/destiny.pm b/xCAT-server/lib/xcat/plugins/destiny.pm index ea5114ea17..f16f1cd3a5 100644 --- a/xCAT-server/lib/xcat/plugins/destiny.pm +++ b/xCAT-server/lib/xcat/plugins/destiny.pm @@ -845,7 +845,10 @@ sub nextdestiny { unless ($ref->{currchain}) { #If no current chain, copy the default $ref->{currchain} = $ref->{chain}; } elsif ($ref->{currchain} !~ /[,;]/){ - if ($ref->{currstate} and ($ref->{currchain} =~ /$ref->{currstate}/)) { + my $completed_state = $ref->{currchain}; + # setdestiny records ondiscover without its command arguments. + $completed_state = 'ondiscover' if $completed_state =~ /^ondiscover=/; + if ($ref->{currstate} and $completed_state eq $ref->{currstate}) { # 'boot' is the steady state a completed provision leaves behind, # set by setdestiny once the install or netboot destiny is applied. # Some installers advance the destiny more than once, so it has to From 9b47f76ca2f034fe3d00830dff30375fd1c862d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:45:48 -0300 Subject: [PATCH 4/6] test(destiny): distinguish complete and overlapping chain states --- xCAT-test/unit/destiny_chain_advance.t | 82 +++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/xCAT-test/unit/destiny_chain_advance.t b/xCAT-test/unit/destiny_chain_advance.t index b946815c28..5279f935a4 100644 --- a/xCAT-test/unit/destiny_chain_advance.t +++ b/xCAT-test/unit/destiny_chain_advance.t @@ -17,11 +17,12 @@ BEGIN { package xCAT::Table; sub new { my ( $class, $table ) = @_; - die "Unexpected table $table" unless $table eq 'chain'; - return bless {}, $class; + die "Unexpected table $table" unless $table eq 'chain' || $table eq 'bootparams'; + return bless { table => $table }, $class; } sub getNodesAttribs { my ( $self, $nodes, @attributes ) = @_; + return {} if $self->{table} eq 'bootparams'; @attributes = @{ $attributes[0] } if ref($attributes[0]) eq 'ARRAY'; my %result; for my $node (@$nodes) { @@ -37,6 +38,14 @@ BEGIN { @{$rows{$node}}{keys %$copy} = values %$copy; return 0; } + sub setNodesAttribs { + my ( $self, $updates ) = @_; + for my $node (keys %$updates) { + $self->setNodeAttribs($node, $updates->{$node}); + } + return 0; + } + sub close { return; } $INC{'xCAT/Table.pm'} = __FILE__; package xCAT::NodeRange; @@ -99,6 +108,38 @@ sub capture_chain_handoffs { my $image = 'osimage=rhels9-x86_64-install-compute'; my @cases = ( + { + name => 'boot does not complete a new iSCSI boot step', + rows => { node1 => { currstate => 'boot', currchain => 'iscsiboot', chain => 'iscsiboot' } }, + steps => [ { transitions => [ [ 'node1', 'iscsiboot', 'iscsiboot' ] ], enact => ['node1'] } ], + }, + { + name => 'iSCSI boot does not complete a new local boot step', + rows => { node1 => { currstate => 'iscsiboot', currchain => 'boot', chain => 'boot' } }, + steps => [ { transitions => [ [ 'node1', 'boot', 'boot' ] ], enact => ['node1'] } ], + }, + { + name => 'an exactly completed iSCSI boot step retains the standby policy', + rows => { node1 => { currstate => 'iscsiboot', currchain => 'iscsiboot', chain => 'iscsiboot' } }, + steps => [ { transitions => [ [ 'node1', 'standby', 'standby' ] ] } ], + }, + { + name => 'an image-name prefix does not complete a different image', + rows => { node1 => { currstate => $image, currchain => "$image-extra", chain => "$image-extra" } }, + steps => [ { transitions => [ [ 'node1', "$image-extra", "$image-extra" ] ], enact => ['node1'] } ], + }, + { + name => 'dots in image names are literal', + rows => { node1 => { currstate => 'osimage=rhels9.6-compute', + currchain => 'osimage=rhels9x6-compute', chain => 'osimage=rhels9x6-compute' } }, + steps => [ { transitions => [ [ 'node1', 'osimage=rhels9x6-compute', 'osimage=rhels9x6-compute' ] ], enact => ['node1'] } ], + }, + { + name => 'an exactly completed dotted image retains the standby policy', + rows => { node1 => { currstate => 'osimage=rhels9.6-compute', + currchain => 'osimage=rhels9.6-compute', chain => 'osimage=rhels9.6-compute' } }, + steps => [ { transitions => [ [ 'node1', 'standby', 'standby' ] ] } ], + }, { name => 'repeated advances from boot preserve the state without enacting again', rows => { node1 => { currstate => 'boot', currchain => 'boot', chain => $image } }, @@ -245,4 +286,41 @@ for my $case (@cases) { }; } +subtest 'a final discovery step completes using the state recorded by setdestiny' => sub { + my $discovery = 'ondiscover=makegocons|rspconfig:sshcfg'; + %rows = (node1 => {currstate => 'boot', currchain => $discovery, chain => $discovery}); + my (@commands, @errors); + { + no warnings qw(once redefine); + local *xCAT::Utils::runcmd = sub { + push @commands, [@_]; + return; + }; + local *xCAT_plugin::destiny::getdestiny = sub { return; }; + xCAT_plugin::destiny::process_request( + {command => ['setdestiny'], node => ['node1'], arg => [$discovery]}, + sub { push @errors, dclone($_[0]); }, + sub { die 'Unexpected subrequest while applying discovery'; }, + ); + } + is_deeply(\@commands, [ + ['xCAT::Utils', 'makegocons node1 ', 0], + ['xCAT::Utils', 'rspconfig node1 sshcfg', 0], + ], 'the real applicator runs both discovery commands once'); + is_deeply(\@errors, [], 'applying discovery emits no errors'); + is($rows{node1}->{currstate}, 'ondiscover', 'the real applicator records the bare discovery state'); + is($rows{node1}->{currchain}, $discovery, 'the chain retains its command arguments'); + + my $result = capture_chain_handoffs({command => ['nextdestiny'], node => ['node1']}); + is_deeply($rows{node1}, {currstate => 'standby', currchain => 'standby', chain => $discovery}, + 'the recorded state completes the final discovery step'); + is_deeply($result->{handoffs}, [{ + request => {node => ['node1'], arg => ['standby']}, flag => 1, + chain_at_handoff => dclone($rows{node1}), + }], 'completion hands off standby instead of repeating discovery'); + is_deeply($result->{subrequests}, [], 'completed discovery does not enact again'); + is_deeply($result->{errors}, [], 'completion emits no callback error'); + is_deeply($result->{logs}, [], 'completion emits no syslog error'); +}; + done_testing(); From d83bf58d9eb3a5c8b766d97e2a921a409b33cf78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:46:20 -0300 Subject: [PATCH 5/6] fix(destiny): preserve enactment when other nodes have completed --- xCAT-server/lib/xcat/plugins/destiny.pm | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/destiny.pm b/xCAT-server/lib/xcat/plugins/destiny.pm index f16f1cd3a5..8626899192 100644 --- a/xCAT-server/lib/xcat/plugins/destiny.pm +++ b/xCAT-server/lib/xcat/plugins/destiny.pm @@ -830,9 +830,11 @@ sub nextdestiny { my $node; my $noupdate_flag = 0; + my @enactnodes; $chaintab = xCAT::Table->new('chain'); my $chainents = $chaintab->getNodesAttribs(\@nodes, [qw(currstate currchain chain)]); foreach $node (@nodes) { + my $enact = 1; unless ($chaintab) { syslog("local4|err", "ERROR: $node requested destiny update, no chain table"); return; #nothing to do... @@ -857,7 +859,7 @@ sub nextdestiny { unless ($ref->{currchain} eq 'boot') { $ref->{currchain} = 'standby'; } - $callnodeset = 0; + $enact = 0; } } my @chain = split /[,;]/, $ref->{currchain}; @@ -876,12 +878,13 @@ sub nextdestiny { { my @items = split /[:]/, $ref->{currstate}; $requ{arg} = \@items; - $noupdate_flag = 1; + $noupdate_flag = 1 if $enact; } setdestiny(\%requ, $flag + 1); + push @enactnodes, $node if $enact; } - if ($callnodeset) { + if ($callnodeset and @enactnodes) { my $args; if ($noupdate_flag) { @@ -892,7 +895,7 @@ sub nextdestiny { $args = ['enact']; } $subreq->({ command => ['nodeset'], - node => \@nodes, + node => \@enactnodes, arg => $args }); } From 2de8d308edd954f949bdb2fc05e44778b4075d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:46:45 -0300 Subject: [PATCH 6/6] test(destiny): cover mixed completed and active chain requests --- xCAT-test/unit/destiny_chain_advance.t | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/xCAT-test/unit/destiny_chain_advance.t b/xCAT-test/unit/destiny_chain_advance.t index 5279f935a4..f70d21469a 100644 --- a/xCAT-test/unit/destiny_chain_advance.t +++ b/xCAT-test/unit/destiny_chain_advance.t @@ -249,6 +249,49 @@ my @cases = ( }, ); +for my $order ([qw(node1 node2)], [qw(node2 node1)]) { + for my $option (0, 1) { + my $state = $option ? "$image:--noupdateinitrd" : 'shutdown'; + my %transitions = ( + node1 => ['node1', 'boot', 'boot'], + node2 => ['node2', $state, 'boot', $option ? [$image, '--noupdateinitrd'] : ['shutdown']], + ); + push @cases, { + name => "completed and active nodes in order @$order, initrd option=$option", + request => {command => ['nextdestiny'], node => $order}, + rows => { + node1 => {currstate => 'boot', currchain => 'boot', chain => 'boot'}, + node2 => {currstate => 'boot', currchain => "$state,boot", chain => "$state,boot"}, + }, + steps => [{ + transitions => [map { $transitions{$_} } @$order], + enact => ['node2'], enact_args => $option ? ['enact', '--noupdateinitrd'] : ['enact'], + }], + }; + } +} +push @cases, { + name => 'a completed middle node does not suppress either active neighbor', + request => {command => ['nextdestiny'], node => [qw(node3 node2 node1)]}, + rows => { + node1 => {currstate => 'offline', currchain => 'boot', chain => 'boot'}, + node2 => {currstate => 'boot', currchain => 'boot', chain => 'boot'}, + node3 => {currstate => 'boot', currchain => 'shutdown,boot', chain => 'shutdown,boot'}, + }, + steps => [{ + transitions => [['node3', 'shutdown', 'boot'], ['node2', 'boot', 'boot'], ['node1', 'boot', 'boot']], + enact => [qw(node3 node1)], + }], +}, { + name => 'a request containing only completed nodes does not enact', + request => {command => ['nextdestiny'], node => [qw(node1 node2)]}, + rows => { + node1 => {currstate => 'boot', currchain => 'boot', chain => 'boot'}, + node2 => {currstate => $image, currchain => $image, chain => $image}, + }, + steps => [{transitions => [['node1', 'boot', 'boot'], ['node2', 'standby', 'standby']]}], +}; + for my $case (@cases) { subtest $case->{name} => sub { %rows = %{ dclone($case->{rows}) };