From acc8089c2afea7b0335f71eb430e5bd0a772155b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Thu, 5 Nov 2026 21:38:54 -0300 Subject: [PATCH 01/17] refactor(genimage): publish a staged kernel with several initial ramdisks publish_boot_files publishes one kernel with one initial ramdisk, so a genimage that builds the statelite and the stateless ramdisk cannot use it. publish_staged_boot_files publishes the kernel and the ramdisks of several modes from one staging directory. When a rename fails, it puts back every file that it already replaced. publish_boot_files keeps its interface for the openEuler build. --- .../share/xcat/netboot/imgutils/imgutils.pm | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 9c71367c18..611098d17a 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -135,23 +135,41 @@ sub publish_boot_files { my ($native_bootdir, $destdir, $initrd, $mode) = @_; move($initrd, "$native_bootdir/initrd-$mode.gz") or die("Error: failed to move the initial ramdisk for $mode: $!\n"); - my $old_kernel = -e "$destdir/kernel" || -l "$destdir/kernel"; - if ($old_kernel) { - link("$destdir/kernel", "$native_bootdir/previous-kernel") - or die("Error: failed to preserve the previous kernel: $!\n"); + return publish_staged_boot_files($native_bootdir, $destdir, $mode); +} + +# nodeset copies the kernel and the initial ramdisks as one set, so a failure puts back every file already replaced. +sub publish_staged_boot_files { + my ($native_bootdir, $destdir, @modes) = @_; + my @files = ([ 'kernel', 'kernel' ], map { [ "initrd-$_.gz", "initial ramdisk for $_" ] } @modes); + foreach my $file (@files) { + my ($name, $what) = @$file; + next unless -e "$destdir/$name" || -l "$destdir/$name"; + link("$destdir/$name", "$native_bootdir/previous-$name") + or die("Error: failed to preserve the previous $what: $!\n"); } - rename("$native_bootdir/kernel", "$destdir/kernel") - or die("Error: failed to publish the kernel: $!\n"); - unless (rename("$native_bootdir/initrd-$mode.gz", "$destdir/initrd-$mode.gz")) { - my $error = $!; - my $restored = $old_kernel - ? rename("$native_bootdir/previous-kernel", "$destdir/kernel") - : unlink("$destdir/kernel"); - unless ($restored) { - $native_bootdir->unlink_on_destroy(0); - die("Error: failed to publish the initial ramdisk: $error; kernel rollback failed: $!. Boot files retained in $native_bootdir\n"); + my @published; + foreach my $file (@files) { + my ($name, $what) = @$file; + if (rename("$native_bootdir/$name", "$destdir/$name")) { + push @published, $file; + next; } - die("Error: failed to publish the initial ramdisk for $mode: $error\n"); + my $error = $!; + my @failed; + foreach my $done (reverse @published) { + my ($done_name, $done_what) = @$done; + my $previous = "$native_bootdir/previous-$done_name"; + my $restored = (-e $previous || -l $previous) + ? rename($previous, "$destdir/$done_name") + : unlink("$destdir/$done_name"); + push @failed, "$done_what rollback failed: $!" unless $restored; + } + if (@failed) { + $native_bootdir->unlink_on_destroy(0); + die("Error: failed to publish the $what: $error; " . join('; ', @failed) . ". Boot files retained in $native_bootdir\n"); + } + die("Error: failed to publish the $what: $error\n"); } return 1; } From fe3740f9dc2b3dac1dae29620b2d281c07f739ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Thu, 5 Nov 2026 21:38:54 -0300 Subject: [PATCH 02/17] test(genimage): cover the publication of a kernel with several ramdisks --- xCAT-test/unit/imgutils_staged_boot_files.t | 126 ++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 xCAT-test/unit/imgutils_staged_boot_files.t diff --git a/xCAT-test/unit/imgutils_staged_boot_files.t b/xCAT-test/unit/imgutils_staged_boot_files.t new file mode 100644 index 0000000000..44b1787c3e --- /dev/null +++ b/xCAT-test/unit/imgutils_staged_boot_files.t @@ -0,0 +1,126 @@ +#!/usr/bin/env perl +use strict; +use warnings; +## no critic (TestingAndDebugging::ProhibitNoWarnings, Variables::RequireLocalizedPunctuationVars) +use Errno qw(EACCES); +use FindBin; +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Test::More; + +BEGIN { + *CORE::GLOBAL::rename = sub { return CORE::rename($_[0], $_[1]); }; + *CORE::GLOBAL::link = sub { return CORE::link($_[0], $_[1]); }; +} +use imgutils; + +my @modes = qw(statelite stateless); +my @names = ('kernel', map { "initrd-$_.gz" } @modes); + +# Publishes a staged kernel and two ramdisks over an older set, failing the named link, rename, or rollback. +sub publish_case { + my (%case) = @_; + my $dir = tempdir(CLEANUP => 1); + my $staging = File::Temp->newdir('.genimage-boot.XXXXXX', DIR => $dir, CLEANUP => 1); + write_text("$staging/$_", "new $_\n") for @names; + if ($case{old}) { + write_text("$dir/$_", "old $_\n") for @names; + } + my ($ok, $error); + { + no warnings 'redefine'; + local *CORE::GLOBAL::link = sub { + if (defined $case{link} && $_[0] eq "$dir/$case{link}") { + $! = EACCES; + return 0; + } + return CORE::link($_[0], $_[1]); + }; + local *CORE::GLOBAL::rename = sub { + if ((defined $case{rename} && $_[0] eq "$staging/$case{rename}") + || (defined $case{rollback} && $_[0] eq "$staging/previous-$case{rollback}")) { + $! = EACCES; + return 0; + } + return CORE::rename($_[0], $_[1]); + }; + $ok = eval { imgutils::publish_staged_boot_files($staging, $dir, @modes) }; + $error = $@; + } + undef $staging; + my %state = map { $_ => (-e "$dir/$_" ? read_text("$dir/$_") : undef) } @names; + return ($ok, $error, $dir, \%state); +} + +sub set_of { + my ($age) = @_; + return { map { $_ => "$age $_\n" } @names }; +} + +for my $old (1, 0) { + my $label = $old ? 'over an older set' : 'on a first build'; + my ($ok, $error, $dir, $state) = publish_case(old => $old); + ok($ok, "publication $label completes") or diag($error); + is_deeply($state, set_of('new'), "publication $label puts the new kernel and both ramdisks in place"); + is_deeply([ glob("$dir/.genimage*") ], [], "publication $label removes the staging directory"); +} + +for my $name (@names) { + my ($ok, $error, $dir, $state) = publish_case(old => 1, link => $name); + ok(!$ok, "a failed backup of $name stops the publication"); + like($error, qr/^Error: failed to preserve the previous (?:kernel|initial ramdisk for \w+): /, "the error names the $name backup"); + is_deeply($state, set_of('old'), "a failed backup of $name keeps the older set"); +} + +for my $name (@names) { + my ($ok, $error, $dir, $state) = publish_case(old => 1, rename => $name); + ok(!$ok, "a failed rename of $name stops the publication"); + like($error, qr/^Error: failed to publish the (?:kernel|initial ramdisk for \w+): /, "the error names the $name rename"); + is_deeply($state, set_of('old'), "a failed rename of $name puts the older set back"); + is_deeply([ glob("$dir/.genimage*") ], [], "a failed rename of $name removes the staging directory"); +} + +{ + my ($ok, $error, $dir, $state) = publish_case(old => 0, rename => 'initrd-stateless.gz'); + ok(!$ok, 'a failed rename on a first build stops the publication'); + is_deeply($state, { map { $_ => undef } @names }, 'a failed rename on a first build leaves no kernel or ramdisk'); +} + +SKIP: { + skip 'link() of a symbolic link is checked on Linux', 2 unless $^O eq 'linux'; + my $dir = tempdir(CLEANUP => 1); + my $staging = File::Temp->newdir('.genimage-boot.XXXXXX', DIR => $dir, CLEANUP => 1); + write_text("$staging/$_", "new $_\n") for @names; + symlink('missing-kernel', "$dir/kernel") or die "symlink: $!"; + write_text("$dir/$_", "old $_\n") for grep { $_ ne 'kernel' } @names; + my $ok; + { + no warnings 'redefine'; + local *CORE::GLOBAL::rename = sub { + if ($_[0] eq "$staging/initrd-stateless.gz") { + $! = EACCES; + return 0; + } + return CORE::rename($_[0], $_[1]); + }; + $ok = eval { imgutils::publish_staged_boot_files($staging, $dir, @modes) }; + } + ok(!$ok, 'a failed rename over a dangling kernel link stops the publication'); + is(readlink("$dir/kernel"), 'missing-kernel', 'a dangling kernel link is put back instead of removed'); +} + +{ + my ($ok, $error, $dir, $state) = publish_case(old => 1, rename => 'initrd-stateless.gz', rollback => 'initrd-statelite.gz'); + ok(!$ok, 'a failed rollback stops the publication'); + like($error, qr/initial ramdisk for statelite rollback failed: .*Boot files retained in \Q$dir\E\/\.genimage-boot\./, + 'the error names the file that could not be put back and the directory that keeps it'); + is($state->{kernel}, "old kernel\n", 'the kernel is still put back after a failed rollback of a ramdisk'); + my ($kept) = glob("$dir/.genimage-boot.*/previous-initrd-statelite.gz"); + ok(defined $kept, 'the staging directory keeps the previous ramdisk that could not be put back'); + is(defined $kept ? read_text($kept) : undef, "old initrd-statelite.gz\n", 'the kept file is the previous ramdisk'); +} + +done_testing(); From 35b7e5a91f36ee653424476dab1eab2b2f32d7ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Thu, 5 Nov 2026 21:38:54 -0300 Subject: [PATCH 03/17] fix(ubuntu): publish the kernel and ramdisks of genimage together genimage copied the kernel to the image directory before it built the initial ramdisks. When dracut failed for either ramdisk, a new kernel stayed next to the old ramdisks, and nodeset copied that set. The legacy ramdisk ignored a failed cpio pipeline and published what the compressor wrote. genimage now builds the kernel and both ramdisks in a staging directory in the image directory. It publishes them together with publish_staged_boot_files after both ramdisks are built, and writes the root image check and UFTP markers after that. A failure leaves the previous kernel and ramdisks in place. --- .../share/xcat/netboot/ubuntu/genimage | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/xCAT-server/share/xcat/netboot/ubuntu/genimage b/xCAT-server/share/xcat/netboot/ubuntu/genimage index abfdc8216a..8b4142dad8 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/genimage +++ b/xCAT-server/share/xcat/netboot/ubuntu/genimage @@ -732,16 +732,20 @@ chomp($kernelver); #$updates{'kernelver'} = $kernelver if ($tempfile); -# copy the kernel to $destdir +# The kernel and the initial ramdisks wait here until genimage publishes them together. +my $native_bootdir = File::Temp->newdir('.genimage-boot.XXXXXX', DIR => $destdir, CLEANUP => 1); +my $stateless_uftp_receiver; +my $kernel_copy; if (-e "$rootimg_dir/boot/vmlinux-$kernelver") { - cp("$rootimg_dir/boot/vmlinux-$kernelver", "$destdir/kernel"); + $kernel_copy = cp("$rootimg_dir/boot/vmlinux-$kernelver", "$native_bootdir/kernel"); } elsif (-e "$rootimg_dir/boot/vmlinuz-$kernelver") { - cp("$rootimg_dir/boot/vmlinuz-$kernelver", "$destdir/kernel"); + $kernel_copy = cp("$rootimg_dir/boot/vmlinuz-$kernelver", "$native_bootdir/kernel"); } elsif (-e "$rootimg_dir/boot/image-$kernelver") { - cp("$rootimg_dir/boot/image-$kernelver", "$destdir/kernel"); + $kernel_copy = cp("$rootimg_dir/boot/image-$kernelver", "$native_bootdir/kernel"); } else { xdie("couldn't find the kernel file matched $kernelver in $rootimg_dir/boot"); } +die("Error: failed to copy the kernel to $native_bootdir: $!\n") unless $kernel_copy; # Load driver update disk, and copy them to the root image my @dd_drivers = &load_dd(); @@ -938,6 +942,11 @@ if ($dracutmode) { push @ndrivers, @drivers; mkinitrd("stateless"); } +eval { imgutils::publish_staged_boot_files($native_bootdir, $destdir, 'statelite', 'stateless') } or die($@); +if ($dracutmode) { + mark_rootimg_check('stateless'); + imgutils::mark_uftp_receiver("$destdir/initrd-stateless.gz", $stateless_uftp_receiver); +} sub getlibs { my $file = shift; @@ -1061,10 +1070,9 @@ sub mkinitrd_dracut { or die("Error: failed to generate the initial ramdisk for $mode.\n"); eval { imgutils::check_uftp_initrd($rootimg_dir, "/tmp/initrd.$$.gz", $mode) } or xdie($@) if $uftp_receiver; print "the initial ramdisk for $mode is generated successfully.\n"; - if (move("$rootimg_dir/tmp/initrd.$$.gz", "$destdir/initrd-$mode.gz")) { - mark_rootimg_check($mode); - imgutils::mark_uftp_receiver("$destdir/initrd-$mode.gz", $uftp_receiver) if $mode eq 'stateless'; - } + move("$rootimg_dir/tmp/initrd.$$.gz", "$native_bootdir/initrd-$mode.gz") + or die("Error: failed to stage the initial ramdisk for $mode: $!\n"); + $stateless_uftp_receiver = $uftp_receiver if $mode eq 'stateless'; } # nodeset warns unless this marker says that the stateless initial ramdisk checks the root image checksum. @@ -1884,9 +1892,11 @@ EOMS } #copy("$rootimg_dir/lib/modules/*d","/tmp/xcatinitrd.$$/$_"); - print "\ncd /tmp/xcatinitrd.$$;find .|cpio -H newc -o|$compress -9 -c - > $destdir/initrd-$mode.gz \n"; - system("cd /tmp/xcatinitrd.$$;find .|cpio -H newc -o|$compress -9 -c - > $destdir/initrd-$mode.gz"); + my $initrd = "$native_bootdir/initrd-$mode.gz"; + print "\ncd /tmp/xcatinitrd.$$;find .|cpio -H newc -o|$compress -9 -c - > $initrd \n"; + my $rc = system('bash', '-c', "set -o pipefail; cd /tmp/xcatinitrd.$$ && find . | cpio -H newc -o | $compress -9 -c - > $initrd"); system("rm -rf /tmp/xcatinitrd.$$"); + die("Error: failed to generate the initial ramdisk for $mode.\n") if $rc; } From 555732abf9388f2b191abcbdc809e60c1404a117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:52:56 -0300 Subject: [PATCH 04/17] refactor(genimage): share the kernel copy and the cpio packing The Ubuntu and the Red Hat-family genimage each had their own code to find and copy the kernel of the image and to pack a cpio initial ramdisk. imgutils now has copy_kernel and pack_initrd for this, and the Ubuntu genimage uses them. copy_kernel also takes lib/modules//vmlinuz, which the Red Hat-family genimage already takes. --- .../share/xcat/netboot/imgutils/imgutils.pm | 20 +++++++++++++++++++ .../share/xcat/netboot/ubuntu/genimage | 18 ++--------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 611098d17a..0eb10c6d3f 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -131,6 +131,26 @@ sub skip_kdump_without_dump_memory { close($fh) or die "Cannot write $dropin_dir/xcat-dump-memory.conf: $!\n"; } +sub copy_kernel { + my ($rootimg_dir, $kernelver, $dir) = @_; + my ($kernel) = grep { -e $_ } map { "$rootimg_dir/$_" } + ("boot/vmlinux-$kernelver", "boot/vmlinuz-$kernelver", "boot/image-$kernelver", "lib/modules/$kernelver/vmlinuz"); + die("couldn't find the kernel file matched $kernelver in $rootimg_dir/boot\n") unless $kernel; + File::Copy::cp($kernel, "$dir/kernel") or die("Error: failed to copy the kernel to $dir: $!\n"); + return 1; +} + +# Removes $dir after packing it, also when a command of the pipeline fails. +sub pack_initrd { + my ($dir, $initrd, $compress, $mode) = @_; + my $command = "cd $dir && find . | cpio -H newc -o | $compress -9 -c - > $initrd"; + print "\n$command\n"; + my $rc = system('bash', '-c', "set -o pipefail; $command"); + rmtree($dir); + die("Error: failed to generate the initial ramdisk for $mode.\n") if $rc; + return 1; +} + sub publish_boot_files { my ($native_bootdir, $destdir, $initrd, $mode) = @_; move($initrd, "$native_bootdir/initrd-$mode.gz") diff --git a/xCAT-server/share/xcat/netboot/ubuntu/genimage b/xCAT-server/share/xcat/netboot/ubuntu/genimage index 8b4142dad8..19cd733d3e 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/genimage +++ b/xCAT-server/share/xcat/netboot/ubuntu/genimage @@ -735,17 +735,7 @@ chomp($kernelver); # The kernel and the initial ramdisks wait here until genimage publishes them together. my $native_bootdir = File::Temp->newdir('.genimage-boot.XXXXXX', DIR => $destdir, CLEANUP => 1); my $stateless_uftp_receiver; -my $kernel_copy; -if (-e "$rootimg_dir/boot/vmlinux-$kernelver") { - $kernel_copy = cp("$rootimg_dir/boot/vmlinux-$kernelver", "$native_bootdir/kernel"); -} elsif (-e "$rootimg_dir/boot/vmlinuz-$kernelver") { - $kernel_copy = cp("$rootimg_dir/boot/vmlinuz-$kernelver", "$native_bootdir/kernel"); -} elsif (-e "$rootimg_dir/boot/image-$kernelver") { - $kernel_copy = cp("$rootimg_dir/boot/image-$kernelver", "$native_bootdir/kernel"); -} else { - xdie("couldn't find the kernel file matched $kernelver in $rootimg_dir/boot"); -} -die("Error: failed to copy the kernel to $native_bootdir: $!\n") unless $kernel_copy; +eval { imgutils::copy_kernel($rootimg_dir, $kernelver, $native_bootdir) } or xdie($@); # Load driver update disk, and copy them to the root image my @dd_drivers = &load_dd(); @@ -1892,11 +1882,7 @@ EOMS } #copy("$rootimg_dir/lib/modules/*d","/tmp/xcatinitrd.$$/$_"); - my $initrd = "$native_bootdir/initrd-$mode.gz"; - print "\ncd /tmp/xcatinitrd.$$;find .|cpio -H newc -o|$compress -9 -c - > $initrd \n"; - my $rc = system('bash', '-c', "set -o pipefail; cd /tmp/xcatinitrd.$$ && find . | cpio -H newc -o | $compress -9 -c - > $initrd"); - system("rm -rf /tmp/xcatinitrd.$$"); - die("Error: failed to generate the initial ramdisk for $mode.\n") if $rc; + imgutils::pack_initrd("/tmp/xcatinitrd.$$", "$native_bootdir/initrd-$mode.gz", $compress, $mode); } From 173ab102e547988691f0406539db9ab90ea9bf06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:52:57 -0300 Subject: [PATCH 05/17] test(genimage): cover the kernel copy and the cpio packing --- xCAT-test/unit/imgutils_boot_file_build.t | 105 ++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 xCAT-test/unit/imgutils_boot_file_build.t diff --git a/xCAT-test/unit/imgutils_boot_file_build.t b/xCAT-test/unit/imgutils_boot_file_build.t new file mode 100644 index 0000000000..b18db9d807 --- /dev/null +++ b/xCAT-test/unit/imgutils_boot_file_build.t @@ -0,0 +1,105 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Test::More; +use imgutils; + +my $release = '6.12.0-124.el10.x86_64'; + +# Copies the kernel of $release from an image root that holds the named files. +sub copy_case { + my (@files) = @_; + my $root = tempdir(CLEANUP => 1); + for my $file (@files) { + make_path("$root/" . ($file =~ s{/[^/]+$}{}r)); + write_text("$root/$file", "$file\n"); + chmod(0755, "$root/$file"); + } + my $dir = tempdir(CLEANUP => 1); + my $ok = eval { imgutils::copy_kernel($root, $release, $dir) }; + return ($ok, $@, $dir); +} + +for my $files ( + [ "boot/vmlinux-$release", "boot/vmlinuz-$release", "boot/image-$release", "lib/modules/$release/vmlinuz" ], + [ "boot/vmlinuz-$release", "boot/image-$release", "lib/modules/$release/vmlinuz" ], + [ "boot/image-$release", "lib/modules/$release/vmlinuz" ], + [ "lib/modules/$release/vmlinuz" ], +) { + my ($ok, $error, $dir) = copy_case(@$files); + ok($ok, "copies a kernel from an image with @$files") or diag($error); + is(read_text("$dir/kernel"), "$files->[0]\n", "copies $files->[0] from an image with @$files"); + is((stat("$dir/kernel"))[2] & oct(7777), oct(755), "keeps the mode of $files->[0]"); +} + +{ + my ($ok, $error, $dir) = copy_case('boot/vmlinuz-6.12.0-55.el10.x86_64', 'lib/modules/6.12.0-55.el10.x86_64/vmlinuz'); + ok(!$ok, 'fails when the image has no kernel of the release'); + like($error, qr/couldn't find the kernel file matched \Q$release\E/, 'names the release of the missing kernel'); + ok(!-e "$dir/kernel", 'copies no kernel of another release'); +} + +{ + my $root = tempdir(CLEANUP => 1); + make_path("$root/boot"); + write_text("$root/boot/vmlinuz-$release", "kernel\n"); + my $ok = eval { imgutils::copy_kernel($root, $release, "$root/missing") }; + ok(!$ok, 'fails when the kernel cannot be copied'); + like($@, qr/^Error: failed to copy the kernel to \Q$root\E\/missing: /, 'reports the failed copy'); +} + +# Packs a directory of two files, with each named command replaced by one that fails without output. +sub pack_case { + my (@failing) = @_; + my $dir = tempdir(CLEANUP => 1); + my $src = "$dir/initrd"; + make_path("$src/bin", "$dir/bin"); + write_text("$src/init", "init\n"); + write_text("$src/bin/netstart", "netstart\n"); + for my $command (@failing) { + write_text("$dir/bin/$command", "#!/bin/sh\nexit 1\n"); + chmod(0755, "$dir/bin/$command"); + } + my ($ok, $error); + { + local $ENV{PATH} = "$dir/bin:$ENV{PATH}"; + local *STDOUT; + open(STDOUT, '>', \my $printed) or die "STDOUT: $!"; + $ok = eval { imgutils::pack_initrd($src, "$dir/initrd.gz", 'gzip', 'stateless') }; + $error = $@; + } + return ($ok, $error, $dir, $src); +} + +{ + my ($ok, $error, $dir, $src) = pack_case(); + ok($ok, 'packs the initial ramdisk') or diag($error); + my @listing = sort map { chomp; $_ } `gzip -dc $dir/initrd.gz | cpio -it 2>/dev/null`; + is_deeply(\@listing, [ '.', 'bin', 'bin/netstart', 'init' ], 'the initial ramdisk holds the packed directory'); + ok(!-e $src, 'removes the packed directory'); +} + +for my $command (qw(find cpio gzip)) { + my ($ok, $error, $dir, $src) = pack_case($command); + ok(!$ok, "fails when $command fails"); + is($error, "Error: failed to generate the initial ramdisk for stateless.\n", "reports the failed $command"); + ok(!-e $src, "removes the packed directory when $command fails"); +} + +{ + my $dir = tempdir(CLEANUP => 1); + local *STDOUT; + open(STDOUT, '>', \my $printed) or die "STDOUT: $!"; + my $ok = eval { imgutils::pack_initrd("$dir/missing", "$dir/initrd.gz", 'gzip', 'statelite') }; + ok(!$ok, 'fails when the directory to pack does not exist'); + is($@, "Error: failed to generate the initial ramdisk for statelite.\n", 'reports the missing directory'); +} + +done_testing(); From 1f6f56e559aed11ce254ad7ec0f000979a9ee30e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:54:20 -0300 Subject: [PATCH 06/17] test(genimage): drop the checks of the per-ramdisk publication imgutils_staged_boot_files.t covers the publication of boot files. The remaining checks of the unit test openeuler_genimage_bootfiles.t cover disable_vendor_repositories, so the file now has that name. The native EL cases checked that genimage keeps copying the kernel into place before dracut runs, which the next change removes. --- .../native/openeuler_genimage_bootfiles.t | 9 -- xCAT-test/unit/imgutils_vendor_repositories.t | 51 +++++++ xCAT-test/unit/openeuler_genimage_bootfiles.t | 140 ------------------ 3 files changed, 51 insertions(+), 149 deletions(-) create mode 100644 xCAT-test/unit/imgutils_vendor_repositories.t delete mode 100644 xCAT-test/unit/openeuler_genimage_bootfiles.t diff --git a/xCAT-test/native/openeuler_genimage_bootfiles.t b/xCAT-test/native/openeuler_genimage_bootfiles.t index 8ee520a1ad..e5d14173ec 100644 --- a/xCAT-test/native/openeuler_genimage_bootfiles.t +++ b/xCAT-test/native/openeuler_genimage_bootfiles.t @@ -59,15 +59,6 @@ is(read_text("$case/image/initrd-stateless.gz"), "old initrd\n", 'lock refusal p is_deeply($state->{commands}, [], 'lock refusal does not run mount cleanup against the active build'); is_deeply([glob("$case/image/.genimage*")], [], 'lock refusal creates no staging directory'); -for my $failure (qw(none kernel-copy dracut move-initrd)) { - ($rc, $state, $output, $case) = run_case('rhels9.6', $failure, 'vmlinuz', 1); - is($rc == 0 ? 0 : 1, $failure eq 'dracut' ? 1 : 0, "EL $failure retains its exit behavior") or diag($output); - is(read_text("$case/image/kernel"), $failure eq 'kernel-copy' ? "old kernel\n" : "new kernel\n", - "EL $failure retains kernel copy behavior"); - is($state->{dracut_calls}, $failure eq 'dracut' ? 1 : 2, "EL $failure retains both initrd modes"); - is($state->{lock_calls}, 0, "EL $failure retains its existing onlyinitrd lock behavior"); -} - done_testing(); sub run_case { diff --git a/xCAT-test/unit/imgutils_vendor_repositories.t b/xCAT-test/unit/imgutils_vendor_repositories.t new file mode 100644 index 0000000000..23be6ab85d --- /dev/null +++ b/xCAT-test/unit/imgutils_vendor_repositories.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Test::More; +use imgutils; + +plan skip_all => 'requires Linux sed' unless $^O eq 'linux'; +my $dir = tempdir(CLEANUP => 1); + +my @legacy_repositories = qw(Rocky-BaseOS.repo almalinux.repo CentOS-Base.repo oracle-linux-ol10.repo); +my @custom_repositories = qw(local-repository-0.repo xCAT-custom.repo administrator.repo); +my $enabled = "[fixture]\nenabled=1\ngpgcheck=1\nbaseurl=https://repo.invalid/\n"; +my $disabled = "[fixture]\nenabled=0\ngpgcheck=1\nbaseurl=https://repo.invalid/\n"; +my $native = "[OS]\nenabled=1\ngpgcheck=1\n[update]\n \tenabled = 1\n[disabled]\nenabled=0\n#enabled=1\n"; +my $native_disabled = "[OS]\nenabled=0\ngpgcheck=1\n[update]\nenabled=0\n[disabled]\nenabled=0\n#enabled=1\n"; +for my $repository_case ( + ['enabled', $native, $native_disabled], + ['already-disabled', $native_disabled, $native_disabled], + ['absent', undef, undef], +) { + my ($name, $input, $expected) = @$repository_case; + subtest $name => sub { + my $root = tempdir(DIR => $dir, CLEANUP => 1); + my $repos = "$root/etc/yum.repos.d"; + make_path($repos); + write_text("$repos/$_", $enabled) for (@legacy_repositories, @custom_repositories); + write_text("$repos/openEuler.repo", $input) if defined $input; + for my $pass (1, 2) { + imgutils::disable_vendor_repositories($root); + if (defined $expected) { + is(read_text("$repos/openEuler.repo"), $expected, + "pass $pass disables only active native repository assignments"); + } else { + ok(!-e "$repos/openEuler.repo", "pass $pass does not create an absent vendor file"); + } + is(read_text("$repos/$_"), $disabled, "pass $pass preserves policy for $_") + for @legacy_repositories; + is(read_text("$repos/$_"), $enabled, "pass $pass leaves $_ unchanged") + for @custom_repositories; + } + }; +} + +done_testing(); diff --git a/xCAT-test/unit/openeuler_genimage_bootfiles.t b/xCAT-test/unit/openeuler_genimage_bootfiles.t deleted file mode 100644 index a4cbbcaa07..0000000000 --- a/xCAT-test/unit/openeuler_genimage_bootfiles.t +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env perl -use strict; -use warnings; -use Errno qw(EACCES EIO); -use FindBin; -use File::Path qw(make_path); -use File::Slurper qw(read_text write_text); -use File::Temp qw(tempdir); -use lib "$FindBin::Bin/../../perl-xCAT"; -use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; -use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; -use Test::More; - -BEGIN { - *CORE::GLOBAL::rename = sub { return CORE::rename($_[0], $_[1]); }; - *CORE::GLOBAL::link = sub { return CORE::link($_[0], $_[1]); }; -} -use imgutils; - -my $dir = tempdir(CLEANUP => 1); - -for my $mode (qw(stateless statelite)) { - for my $failure (qw(none move-initrd backup-kernel publish-kernel publish-initrd)) { - my ($ok, $error, $case) = publish_case($mode, $failure, 1); - my $label = "$mode $failure"; - if ($failure eq 'none') { - ok($ok, "$label completes") or diag($error); - is(read_text("$case/kernel"), "new kernel\n", "$label publishes the kernel"); - is(read_text("$case/initrd-$mode.gz"), "new initrd\n", "$label publishes the initrd"); - } else { - ok(!$ok, "$label fails"); - like($error, qr/Error: failed to (?:move|publish|preserve).*?(?:kernel|ramdisk)/i, - "$label reports the failed boot file operation"); - is(read_text("$case/kernel"), "old kernel\n", "$label preserves the previous kernel"); - is(read_text("$case/initrd-$mode.gz"), "old initrd\n", "$label preserves the previous initrd"); - } - is_deeply([glob("$case/.genimage*")], [], "$label removes boot staging files"); - } -} - -my ($ok, $error, $case) = publish_case('stateless', 'publish-initrd', 0); -ok(!$ok, 'first build publication failure is fatal'); -ok(!-e "$case/kernel", 'first build failure removes the unpaired kernel'); -ok(!-e "$case/initrd-stateless.gz", 'first build failure does not expose an initrd'); -is_deeply([glob("$case/.genimage*")], [], 'first build failure removes boot staging files'); - -($ok, $error, $case) = publish_case('stateless', 'rollback', 1); -ok(!$ok, 'rollback failure is fatal'); -my @backups = glob("$case/.genimage*/previous-kernel"); -is(scalar(@backups), 1, 'rollback failure retains one previous kernel backup'); -is(read_text($backups[0]), "old kernel\n", 'retained backup contains the previous kernel'); -like($error, qr/kernel rollback failed: .*Boot files retained in \Q$case\E\/\.genimage/, - 'rollback failure reports the recoverable backup directory'); -is(read_text("$case/initrd-stateless.gz"), "old initrd\n", 'rollback failure preserves previous initrd'); - -subtest 'vendor repositories' => sub { - plan skip_all => 'requires Linux sed' unless $^O eq 'linux'; - my @legacy_repositories = qw(Rocky-BaseOS.repo almalinux.repo CentOS-Base.repo oracle-linux-ol10.repo); - my @custom_repositories = qw(local-repository-0.repo xCAT-custom.repo administrator.repo); - my $enabled = "[fixture]\nenabled=1\ngpgcheck=1\nbaseurl=https://repo.invalid/\n"; - my $disabled = "[fixture]\nenabled=0\ngpgcheck=1\nbaseurl=https://repo.invalid/\n"; - my $native = "[OS]\nenabled=1\ngpgcheck=1\n[update]\n \tenabled = 1\n[disabled]\nenabled=0\n#enabled=1\n"; - my $native_disabled = "[OS]\nenabled=0\ngpgcheck=1\n[update]\nenabled=0\n[disabled]\nenabled=0\n#enabled=1\n"; - for my $repository_case ( - ['enabled', $native, $native_disabled], - ['already-disabled', $native_disabled, $native_disabled], - ['absent', undef, undef], - ) { - my ($name, $input, $expected) = @$repository_case; - subtest $name => sub { - my $root = tempdir(DIR => $dir, CLEANUP => 1); - my $repos = "$root/etc/yum.repos.d"; - make_path($repos); - write_text("$repos/$_", $enabled) for (@legacy_repositories, @custom_repositories); - write_text("$repos/openEuler.repo", $input) if defined $input; - for my $pass (1, 2) { - imgutils::disable_vendor_repositories($root); - if (defined $expected) { - is(read_text("$repos/openEuler.repo"), $expected, - "pass $pass disables only active native repository assignments"); - } else { - ok(!-e "$repos/openEuler.repo", "pass $pass does not create an absent vendor file"); - } - is(read_text("$repos/$_"), $disabled, "pass $pass preserves policy for $_") - for @legacy_repositories; - is(read_text("$repos/$_"), $enabled, "pass $pass leaves $_ unchanged") - for @custom_repositories; - } - }; - } -}; - -done_testing(); - -sub publish_case { - my ($mode, $failure, $old) = @_; - my $case = tempdir(DIR => $dir, CLEANUP => 1); - make_path("$case/tmp"); - my $staging = File::Temp->newdir('.genimage-boot.XXXXXX', DIR => $case, CLEANUP => 1); - write_text("$staging/kernel", "new kernel\n"); - write_text("$case/tmp/initrd.gz", "new initrd\n"); - if ($old) { - write_text("$case/kernel", "old kernel\n"); - write_text("$case/initrd-$mode.gz", "old initrd\n"); - } - - my $move = \&imgutils::move; - my ($ok, $error); - { - no warnings 'redefine'; - local *imgutils::move = sub { - if ($failure eq 'move-initrd') { - $! = EIO; - return 0; - } - return $move->(@_); - }; - local *CORE::GLOBAL::link = sub { - if ($failure eq 'backup-kernel') { - $! = EACCES; - return 0; - } - return CORE::link($_[0], $_[1]); - }; - local *CORE::GLOBAL::rename = sub { - if (($failure =~ /^(?:publish-initrd|rollback)$/ - && $_[1] eq "$case/initrd-$mode.gz") - || ($failure eq 'publish-kernel' && $_[1] eq "$case/kernel") - || ($failure eq 'rollback' && $_[0] =~ m{/previous-kernel$})) { - $! = EACCES; - return 0; - } - return CORE::rename($_[0], $_[1]); - }; - $ok = eval { imgutils::publish_boot_files($staging, $case, "$case/tmp/initrd.gz", $mode); }; - $error = $@; - } - undef $staging; - return ($ok, $error, $case); -} From 876ff7bfe0be19a686097bf2dfb20b71ef034717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:54:46 -0300 Subject: [PATCH 07/17] fix(genimage): publish the EL kernel and ramdisks together The Red Hat-family genimage staged and published the boot files only for openEuler. For the other releases, it copied the kernel into the image directory before it built the initial ramdisks, and it ignored a failed kernel copy and a failed move of a ramdisk. When dracut failed, the new kernel stayed next to the ramdisks of the earlier build. On images without dracut, a failed cpio replaced the ramdisks with empty archives. genimage now builds the kernel and both ramdisks in a temporary directory inside the image directory for every release, and publishes them with publish_staged_boot_files after both ramdisks are built. It stops when the kernel copy, dracut, the move of a ramdisk or any command of the cpio pipeline fails. openEuler keeps its single stateless ramdisk. --- .../share/xcat/netboot/imgutils/imgutils.pm | 9 +-- xCAT-server/share/xcat/netboot/rh/genimage | 56 ++++++------------- 2 files changed, 17 insertions(+), 48 deletions(-) diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 0eb10c6d3f..2b7653defd 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -7,7 +7,7 @@ use strict; use warnings "all"; use File::Basename; -use File::Copy qw(move); +use File::Copy (); use File::Find; use File::Path; use Cwd qw(realpath); @@ -151,13 +151,6 @@ sub pack_initrd { return 1; } -sub publish_boot_files { - my ($native_bootdir, $destdir, $initrd, $mode) = @_; - move($initrd, "$native_bootdir/initrd-$mode.gz") - or die("Error: failed to move the initial ramdisk for $mode: $!\n"); - return publish_staged_boot_files($native_bootdir, $destdir, $mode); -} - # nodeset copies the kernel and the initial ramdisks as one set, so a failure puts back every file already replaced. sub publish_staged_boot_files { my ($native_bootdir, $destdir, @modes) = @_; diff --git a/xCAT-server/share/xcat/netboot/rh/genimage b/xCAT-server/share/xcat/netboot/rh/genimage index eb6782f712..80cab0c764 100755 --- a/xCAT-server/share/xcat/netboot/rh/genimage +++ b/xCAT-server/share/xcat/netboot/rh/genimage @@ -72,11 +72,11 @@ my $lock; my $envar; my $gpgcheck; my $native_bootdir; +my $stateless_uftp_receiver; sub xdie { - unlink("$rootimg_dir/tmp/initrd.$$.gz") - if $rootimg_dir && defined(imgutils::openeuler_release_version($osver)); + unlink("$rootimg_dir/tmp/initrd.$$.gz") if $rootimg_dir; system("rm -rf /tmp/xcatinitrd.$$"); umount_chroot($rootimg_dir); die @_; @@ -719,26 +719,9 @@ chomp($kernelver); #$updates{'kernelver'} = $kernelver if ($tempfile); -# copy the kernel to $destdir -my $kernel_output = "$destdir/kernel"; -if (defined(imgutils::openeuler_release_version($osver))) { - $native_bootdir = File::Temp->newdir('.genimage-boot.XXXXXX', DIR => $destdir, CLEANUP => 1); - $kernel_output = "$native_bootdir/kernel"; -} -my $kernel_copy; -if (-e "$rootimg_dir/boot/vmlinux-$kernelver") { - $kernel_copy = cp("$rootimg_dir/boot/vmlinux-$kernelver", $kernel_output); -} elsif (-e "$rootimg_dir/boot/vmlinuz-$kernelver") { - $kernel_copy = cp("$rootimg_dir/boot/vmlinuz-$kernelver", $kernel_output); -} elsif (-e "$rootimg_dir/boot/image-$kernelver") { - $kernel_copy = cp("$rootimg_dir/boot/image-$kernelver", $kernel_output); -} elsif (-e "$rootimg_dir/lib/modules/$kernelver/vmlinuz") { - $kernel_copy = cp("$rootimg_dir/lib/modules/$kernelver/vmlinuz", $kernel_output); -} else { - xdie("couldn't find the kernel file matched $kernelver in $rootimg_dir/boot"); -} -xdie("Error: failed to copy the kernel to $kernel_output: $!\n") - if $native_bootdir && !$kernel_copy; +# The kernel and the initial ramdisks wait here until genimage publishes them together. +$native_bootdir = File::Temp->newdir('.genimage-boot.XXXXXX', DIR => $destdir, CLEANUP => 1); +eval { imgutils::copy_kernel($rootimg_dir, $kernelver, $native_bootdir) } or xdie($@); # Load driver update disk, and copy them to the root image my @dd_drivers = &load_dd(); @@ -818,7 +801,7 @@ if ((-d "$rootimg_dir/usr/share/dracut") or (-d "$rootimg_dir/usr/lib/dracut")) print "Enter the dracut mode. Dracut version: $dracutver. Dracut directory: $dracutdir.\n"; } xdie("Error: failed to generate the initial ramdisk: native image has no dracut installation\n") - if $native_bootdir && !$dracutmode; + if !$dracutmode && defined(imgutils::openeuler_release_version($osver)); imgutils::disable_vendor_repositories($rootimg_dir); imgutils::skip_kdump_without_dump_memory($rootimg_dir); @@ -1014,6 +997,12 @@ if ($dracutmode) { mkinitrd("statelite"); mkinitrd("stateless"); } +my @initrd_modes = defined(imgutils::openeuler_release_version($osver)) ? ('stateless') : ('statelite', 'stateless'); +eval { imgutils::publish_staged_boot_files($native_bootdir, $destdir, @initrd_modes) } or xdie($@); +if ($dracutmode) { + mark_rootimg_check('stateless'); + mark_uftp_receiver('stateless', $stateless_uftp_receiver); +} sub getlibs { my $file = shift; @@ -1305,22 +1294,10 @@ sub mkinitrd_dracut { or xdie("Error: failed to generate the initial ramdisk for $mode.\n"); umount_chroot($rootimg_dir); eval { imgutils::check_uftp_initrd($rootimg_dir, "/tmp/initrd.$$.gz", $mode) } or xdie($@) if $uftp_receiver; - if ($native_bootdir) { - eval { - imgutils::publish_boot_files($native_bootdir, $destdir, - "$rootimg_dir/tmp/initrd.$$.gz", $mode); - } or xdie($@); - print "the initial ramdisk for $mode is generated successfully.\n"; - mark_rootimg_check($mode); - mark_uftp_receiver($mode, $uftp_receiver); - return; - } + move("$rootimg_dir/tmp/initrd.$$.gz", "$native_bootdir/initrd-$mode.gz") + or xdie("Error: failed to move the initial ramdisk for $mode: $!\n"); print "the initial ramdisk for $mode is generated successfully.\n"; - print("Move $rootimg_dir/tmp/initrd.$$.gz -> $destdir/initrd-$mode.gz\n"); - if (move("$rootimg_dir/tmp/initrd.$$.gz", "$destdir/initrd-$mode.gz")) { - mark_rootimg_check($mode); - mark_uftp_receiver($mode, $uftp_receiver); - } + $stateless_uftp_receiver = $uftp_receiver if $mode eq 'stateless'; } # nodeset warns unless this marker says that the stateless initial ramdisk checks the root image checksum. @@ -2058,8 +2035,7 @@ EOMS system("mkdir -p /tmp/xcatinitrd.$$/var/run/netreport"); #copy("$rootimg_dir/lib/modules/*d","/tmp/xcatinitrd.$$/$_"); - system("cd /tmp/xcatinitrd.$$;find .|cpio -H newc -o|gzip -9 -c - > $destdir/initrd-$mode.gz"); - system("rm -rf /tmp/xcatinitrd.$$"); + eval { imgutils::pack_initrd("/tmp/xcatinitrd.$$", "$native_bootdir/initrd-$mode.gz", 'gzip', $mode) } or xdie($@); } From 6b2c183246bbb0de274979858e8ae0506031fb14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:08:51 -0300 Subject: [PATCH 08/17] test(genimage): cover the staged publication of EL boot files --- .../native/openeuler_genimage_bootfiles.t | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/xCAT-test/native/openeuler_genimage_bootfiles.t b/xCAT-test/native/openeuler_genimage_bootfiles.t index e5d14173ec..fb53044b10 100644 --- a/xCAT-test/native/openeuler_genimage_bootfiles.t +++ b/xCAT-test/native/openeuler_genimage_bootfiles.t @@ -59,6 +59,27 @@ is(read_text("$case/image/initrd-stateless.gz"), "old initrd\n", 'lock refusal p is_deeply($state->{commands}, [], 'lock refusal does not run mount cleanup against the active build'); is_deeply([glob("$case/image/.genimage*")], [], 'lock refusal creates no staging directory'); +my %el_dracut_calls = (none => 2, 'kernel-copy' => 0, dracut => 1, 'move-initrd' => 1, 'publish-initrd' => 2); +for my $failure (qw(none kernel-copy dracut move-initrd publish-initrd)) { + ($rc, $state, $output, $case) = run_case('rhels9.6', $failure, 'vmlinuz', 1); + my $label = "EL $failure"; + my $set = $failure eq 'none' ? 'new' : 'old'; + if ($failure eq 'none') { + is($rc, 0, "$label completes") or diag($output); + } else { + isnt($rc, 0, "$label fails"); + like($output, qr/Error: failed to (?:copy|generate|move|publish).*?(?:kernel|ramdisk)/i, + "$label reports the failed boot file operation"); + } + is(read_text("$case/image/kernel"), "$set kernel\n", "$label leaves the $set kernel"); + is(read_text("$case/image/initrd-$_.gz"), "$set initrd\n", "$label leaves the $set $_ initrd") + for qw(statelite stateless); + is($state->{dracut_calls}, $el_dracut_calls{$failure}, "$label stops at the first failed step"); + is($state->{lock_calls}, 0, "$label retains its existing onlyinitrd lock behavior"); + is_deeply([glob("$case/image/rootimg/tmp/initrd.*.gz")], [], "$label removes temporary dracut output"); + is_deeply([glob("$case/image/.genimage*")], [], "$label removes boot staging files"); +} + done_testing(); sub run_case { @@ -77,7 +98,7 @@ sub run_case { chmod 0000, $kernel_file if $failure eq 'kernel-copy'; if ($old) { write_text("$case/image/kernel", "old kernel\n"); - write_text("$case/image/initrd-stateless.gz", "old initrd\n"); + write_text("$case/image/initrd-$_.gz", "old initrd\n") for qw(statelite stateless); } if ($repositories) { make_path("$root/etc/yum.repos.d"); From 4fc0d3e211d29cc0d91911f39d5f13e3f7304339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:10:27 -0300 Subject: [PATCH 09/17] docs(genimage): describe how genimage builds and publishes boot files The page also names the genimage scripts that do not follow the rules yet. --- .../guides/code/genimage_boot_files.rst | 53 +++++++++++++++++++ docs/source/developers/guides/code/index.rst | 1 + 2 files changed, 54 insertions(+) create mode 100644 docs/source/developers/guides/code/genimage_boot_files.rst diff --git a/docs/source/developers/guides/code/genimage_boot_files.rst b/docs/source/developers/guides/code/genimage_boot_files.rst new file mode 100644 index 0000000000..8c075d3947 --- /dev/null +++ b/docs/source/developers/guides/code/genimage_boot_files.rst @@ -0,0 +1,53 @@ +genimage Boot Files +=================== + +Every ``genimage`` script builds a kernel and one or more initial ramdisks for +a diskless image, and writes them to the image directory, the ``rootimgdir`` of +the osimage. ``geninitrd`` runs ``genimage --onlyinitrd``, which takes the +same steps. +``nodeset`` copies the kernel and the ramdisks as one set, so the files in the +image directory must always come from the same build. + +Rules +----- + +#. Build the boot files in a staging directory inside the image directory. + Create it with ``File::Temp->newdir`` in the image directory, so that each + file moves into place with a ``rename`` on the same file system. +#. Copy the kernel of the image with ``imgutils::copy_kernel``. +#. Pack a ramdisk without dracut with ``imgutils::pack_initrd``. It stops + when any command of the ``find``, ``cpio`` and compression pipeline fails. +#. Stop when a step fails: the kernel copy, dracut, the move of a ramdisk into + the staging directory, or the cpio pipeline. Exit with a status other than + 0, and leave the earlier boot files in the image directory. +#. Publish the kernel and all the ramdisks with + ``imgutils::publish_staged_boot_files`` after the last ramdisk is built. If + a move fails, it puts back the files that it already replaced. +#. Write the ``rootimgcheck`` and UFTP markers after the publication. They + describe the published stateless ramdisk. +#. Put code that more than one ``genimage`` script needs in ``imgutils.pm``. + Do not copy it into another script. When a fix applies to more than one + release, do not limit it to one release. + +Scripts +------- + +``rh/genimage`` follows these rules. It is the ``genimage`` of RHEL, +AlmaLinux, Rocky Linux, CentOS, Oracle Linux, Scientific Linux, Fedora and +openEuler. ``ubuntu/genimage`` also follows them. + +The ``sles``, ``suse``, ``debian`` and ``fedora12`` scripts still write the +kernel and the ramdisks directly into the image directory. + +Tests +----- + +``xCAT-test/unit/imgutils_boot_file_build.t`` and +``xCAT-test/unit/imgutils_staged_boot_files.t`` cover the ``imgutils`` +functions. ``xCAT-test/native/openeuler_genimage_bootfiles.t`` runs +``rh/genimage --onlyinitrd`` for openEuler and EL images, with a failure in +each step. + +A change to the boot file steps of a ``genimage`` script also needs a +``geninitrd`` run on a real image with a failed dracut. After the run, the +image directory must hold the earlier kernel and ramdisks. diff --git a/docs/source/developers/guides/code/index.rst b/docs/source/developers/guides/code/index.rst index 968bf79795..9ec74159bd 100644 --- a/docs/source/developers/guides/code/index.rst +++ b/docs/source/developers/guides/code/index.rst @@ -9,5 +9,6 @@ Code Development debug.rst dhcp_backend_validation_matrix.rst genesis_openembedded_plan.rst + genimage_boot_files.rst kea_dhcp_backend_plan.rst tips.rst From 9a02347d973c17cb55cfbebf7826ca530464b9d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:19:24 -0300 Subject: [PATCH 10/17] fix(genimage): put back a boot file that a failed rename moved On NFS, a rename can take effect and still report a failure after the client retries the request. publish_staged_boot_files then put back only the files that it published before, so the new file stayed next to the older set, and the staging cleanup removed its backup. When the staged copy of the file is gone after a failed rename, publish_staged_boot_files now puts back that file too. --- xCAT-server/share/xcat/netboot/imgutils/imgutils.pm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 2b7653defd..653a0b8254 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -169,8 +169,10 @@ sub publish_staged_boot_files { next; } my $error = $!; + # NFS can apply a rename and still report a failure, so put back a staged file that is gone. + my @attempted = (-e "$native_bootdir/$name" || -l "$native_bootdir/$name") ? () : ($file); my @failed; - foreach my $done (reverse @published) { + foreach my $done (@attempted, reverse @published) { my ($done_name, $done_what) = @$done; my $previous = "$native_bootdir/previous-$done_name"; my $restored = (-e $previous || -l $previous) From 7f51e1da883b0e86f37d333fe06fad36099010c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:19:24 -0300 Subject: [PATCH 11/17] test(genimage): cover a rename that fails after it moved the file --- xCAT-test/unit/imgutils_staged_boot_files.t | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/xCAT-test/unit/imgutils_staged_boot_files.t b/xCAT-test/unit/imgutils_staged_boot_files.t index 44b1787c3e..7bdd85d6d8 100644 --- a/xCAT-test/unit/imgutils_staged_boot_files.t +++ b/xCAT-test/unit/imgutils_staged_boot_files.t @@ -2,7 +2,7 @@ use strict; use warnings; ## no critic (TestingAndDebugging::ProhibitNoWarnings, Variables::RequireLocalizedPunctuationVars) -use Errno qw(EACCES); +use Errno qw(EACCES EIO); use FindBin; use File::Slurper qw(read_text write_text); use File::Temp qw(tempdir); @@ -20,7 +20,7 @@ use imgutils; my @modes = qw(statelite stateless); my @names = ('kernel', map { "initrd-$_.gz" } @modes); -# Publishes a staged kernel and two ramdisks over an older set, failing the named link, rename, or rollback. +# Publishes a staged kernel and two ramdisks over an older set, failing the named link, rename, applied rename, or rollback. sub publish_case { my (%case) = @_; my $dir = tempdir(CLEANUP => 1); @@ -40,6 +40,11 @@ sub publish_case { return CORE::link($_[0], $_[1]); }; local *CORE::GLOBAL::rename = sub { + if (defined $case{applied} && $_[0] eq "$staging/$case{applied}") { + CORE::rename($_[0], $_[1]); + $! = EIO; + return 0; + } if ((defined $case{rename} && $_[0] eq "$staging/$case{rename}") || (defined $case{rollback} && $_[0] eq "$staging/previous-$case{rollback}")) { $! = EACCES; @@ -83,6 +88,19 @@ for my $name (@names) { is_deeply([ glob("$dir/.genimage*") ], [], "a failed rename of $name removes the staging directory"); } +for my $name (@names) { + my ($ok, $error, $dir, $state) = publish_case(old => 1, applied => $name); + ok(!$ok, "a rename of $name that reports a failure after it moved the file stops the publication"); + is_deeply($state, set_of('old'), "a rename of $name that reports a failure after it moved the file puts the older set back"); + is_deeply([ glob("$dir/.genimage*") ], [], "a rename of $name that reports a failure after it moved the file removes the staging directory"); +} + +{ + my ($ok, $error, $dir, $state) = publish_case(old => 0, applied => 'initrd-stateless.gz'); + ok(!$ok, 'a rename that reports a failure after it moved the file on a first build stops the publication'); + is_deeply($state, { map { $_ => undef } @names }, 'a rename that reports a failure after it moved the file on a first build leaves no kernel or ramdisk'); +} + { my ($ok, $error, $dir, $state) = publish_case(old => 0, rename => 'initrd-stateless.gz'); ok(!$ok, 'a failed rename on a first build stops the publication'); From 8a059f4665fe9e87db23cb5540368346c831ade7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:25:36 -0300 Subject: [PATCH 12/17] fix(genimage): restore boot files from the recorded backups publish_staged_boot_files looked for the backup of a file during the rollback to decide whether to restore it or remove the published file. When that lookup failed, it removed the published file, and the staging cleanup then removed the backup. publish_staged_boot_files now records each backup that it makes and rolls back from that record. A file without an earlier copy is one for which link reports ENOENT. Any other link error stops the publication. --- .../share/xcat/netboot/imgutils/imgutils.pm | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 653a0b8254..ba612c6dec 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -155,11 +155,14 @@ sub pack_initrd { sub publish_staged_boot_files { my ($native_bootdir, $destdir, @modes) = @_; my @files = ([ 'kernel', 'kernel' ], map { [ "initrd-$_.gz", "initial ramdisk for $_" ] } @modes); + my %backup; foreach my $file (@files) { my ($name, $what) = @$file; - next unless -e "$destdir/$name" || -l "$destdir/$name"; - link("$destdir/$name", "$native_bootdir/previous-$name") - or die("Error: failed to preserve the previous $what: $!\n"); + if (link("$destdir/$name", "$native_bootdir/previous-$name")) { + $backup{$name} = 1; + } elsif (!$!{ENOENT}) { + die("Error: failed to preserve the previous $what: $!\n"); + } } my @published; foreach my $file (@files) { @@ -174,9 +177,8 @@ sub publish_staged_boot_files { my @failed; foreach my $done (@attempted, reverse @published) { my ($done_name, $done_what) = @$done; - my $previous = "$native_bootdir/previous-$done_name"; - my $restored = (-e $previous || -l $previous) - ? rename($previous, "$destdir/$done_name") + my $restored = $backup{$done_name} + ? rename("$native_bootdir/previous-$done_name", "$destdir/$done_name") : unlink("$destdir/$done_name"); push @failed, "$done_what rollback failed: $!" unless $restored; } From 454fe5978472e6b78fae958d820d191ca3a635be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:25:36 -0300 Subject: [PATCH 13/17] test(genimage): cover a rollback whose backup is gone --- xCAT-test/unit/imgutils_staged_boot_files.t | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/xCAT-test/unit/imgutils_staged_boot_files.t b/xCAT-test/unit/imgutils_staged_boot_files.t index 7bdd85d6d8..2650607fca 100644 --- a/xCAT-test/unit/imgutils_staged_boot_files.t +++ b/xCAT-test/unit/imgutils_staged_boot_files.t @@ -20,7 +20,7 @@ use imgutils; my @modes = qw(statelite stateless); my @names = ('kernel', map { "initrd-$_.gz" } @modes); -# Publishes a staged kernel and two ramdisks over an older set, failing the named link, rename, applied rename, or rollback. +# Publishes a staged kernel and two ramdisks over an older set, with the faults that %case names. sub publish_case { my (%case) = @_; my $dir = tempdir(CLEANUP => 1); @@ -47,6 +47,7 @@ sub publish_case { } if ((defined $case{rename} && $_[0] eq "$staging/$case{rename}") || (defined $case{rollback} && $_[0] eq "$staging/previous-$case{rollback}")) { + unlink("$staging/previous-$case{lost}") if defined $case{lost}; $! = EACCES; return 0; } @@ -141,4 +142,14 @@ SKIP: { is(defined $kept ? read_text($kept) : undef, "old initrd-statelite.gz\n", 'the kept file is the previous ramdisk'); } +{ + my ($ok, $error, $dir, $state) = publish_case(old => 1, rename => 'initrd-stateless.gz', lost => 'kernel'); + ok(!$ok, 'a rollback whose backup is gone stops the publication'); + like($error, qr/kernel rollback failed: .*Boot files retained in \Q$dir\E\/\.genimage-boot\./, + 'the error names the kernel whose backup is gone'); + is($state->{kernel}, "new kernel\n", 'the published kernel is not removed when its backup is gone'); + is($state->{'initrd-statelite.gz'}, "old initrd-statelite.gz\n", 'the ramdisk with a backup is still put back'); + ok(scalar(glob("$dir/.genimage-boot.*")), 'the staging directory is kept when a backup is gone'); +} + done_testing(); From cb750451140f713977ef6db71ddd3d4016c96fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:31:38 -0300 Subject: [PATCH 14/17] test(genimage): stop checking that EL geninitrd runs without the lock --- xCAT-test/native/openeuler_genimage_bootfiles.t | 1 - 1 file changed, 1 deletion(-) diff --git a/xCAT-test/native/openeuler_genimage_bootfiles.t b/xCAT-test/native/openeuler_genimage_bootfiles.t index fb53044b10..d8ad2d048e 100644 --- a/xCAT-test/native/openeuler_genimage_bootfiles.t +++ b/xCAT-test/native/openeuler_genimage_bootfiles.t @@ -75,7 +75,6 @@ for my $failure (qw(none kernel-copy dracut move-initrd publish-initrd)) { is(read_text("$case/image/initrd-$_.gz"), "$set initrd\n", "$label leaves the $set $_ initrd") for qw(statelite stateless); is($state->{dracut_calls}, $el_dracut_calls{$failure}, "$label stops at the first failed step"); - is($state->{lock_calls}, 0, "$label retains its existing onlyinitrd lock behavior"); is_deeply([glob("$case/image/rootimg/tmp/initrd.*.gz")], [], "$label removes temporary dracut output"); is_deeply([glob("$case/image/.genimage*")], [], "$label removes boot staging files"); } From a53b65a96d770d6e2c78fad5af913248b5f63cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:31:39 -0300 Subject: [PATCH 15/17] fix(genimage): take the image lock for every EL geninitrd run The Red Hat-family genimage took the image operation lock with --onlyinitrd only for openEuler. Two geninitrd runs on another release, or a geninitrd run during packimage, could publish and roll back boot files over each other. genimage now takes the lock with --onlyinitrd for every release, as it already does for a full build. --- xCAT-server/share/xcat/netboot/rh/genimage | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/xCAT-server/share/xcat/netboot/rh/genimage b/xCAT-server/share/xcat/netboot/rh/genimage index 80cab0c764..924c15f7cd 100755 --- a/xCAT-server/share/xcat/netboot/rh/genimage +++ b/xCAT-server/share/xcat/netboot/rh/genimage @@ -303,13 +303,11 @@ if($onlyinitrd){ print("the root image directory \"$rootimg_dir\"does not exist, please run \"genimage $imagename\" to generate the root image directory first!\n"); exit 1; } - if (defined(imgutils::openeuler_release_version($osver))) { - my $retcode; - ($retcode, $lock) = xCAT::Utils->acquire_lock_imageop($rootimg_dir); - if ($retcode) { - print "Could not acquire image operation lock for $rootimg_dir\n"; - exit 1; - } + my $retcode; + ($retcode, $lock) = xCAT::Utils->acquire_lock_imageop($rootimg_dir); + if ($retcode) { + print "Could not acquire image operation lock for $rootimg_dir\n"; + exit 1; } }else { From 9df65310a8332e11a6cc4e4faa2a31362c9b715c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:32:09 -0300 Subject: [PATCH 16/17] test(genimage): cover the image lock of EL geninitrd --- .../native/openeuler_genimage_bootfiles.t | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/xCAT-test/native/openeuler_genimage_bootfiles.t b/xCAT-test/native/openeuler_genimage_bootfiles.t index d8ad2d048e..a594995650 100644 --- a/xCAT-test/native/openeuler_genimage_bootfiles.t +++ b/xCAT-test/native/openeuler_genimage_bootfiles.t @@ -49,15 +49,19 @@ for my $kernel (qw(vmlinux image modules)) { is(read_text("$case/image/kernel"), "new kernel\n", "$kernel layout publishes its kernel"); is(read_text("$case/image/initrd-stateless.gz"), "new initrd\n", "$kernel layout publishes its initrd"); } -my ($rc, $state, $output, $case) = run_case('openeuler24.03sp3', 'lock', 'vmlinuz', 1); -isnt($rc, 0, 'lock refusal is fatal'); -like($output, qr/Could not acquire image operation lock/, 'lock refusal reports the contested image'); -is($state->{lock_calls}, 1, 'lock refusal uses the existing lock owner'); -is($state->{dracut_calls}, 0, 'lock refusal does not run dracut'); -is(read_text("$case/image/kernel"), "old kernel\n", 'lock refusal preserves kernel'); -is(read_text("$case/image/initrd-stateless.gz"), "old initrd\n", 'lock refusal preserves initrd'); -is_deeply($state->{commands}, [], 'lock refusal does not run mount cleanup against the active build'); -is_deeply([glob("$case/image/.genimage*")], [], 'lock refusal creates no staging directory'); +my ($rc, $state, $output, $case); +for my $os (qw(openeuler24.03sp3 rhels9.6)) { + ($rc, $state, $output, $case) = run_case($os, 'lock', 'vmlinuz', 1); + isnt($rc, 0, "$os lock refusal is fatal"); + like($output, qr/Could not acquire image operation lock/, "$os lock refusal reports the contested image"); + is($state->{lock_calls}, 1, "$os lock refusal uses the existing lock owner"); + is($state->{dracut_calls}, 0, "$os lock refusal does not run dracut"); + is(read_text("$case/image/kernel"), "old kernel\n", "$os lock refusal preserves kernel"); + is(read_text("$case/image/initrd-$_.gz"), "old initrd\n", "$os lock refusal preserves the $_ initrd") + for qw(statelite stateless); + is_deeply($state->{commands}, [], "$os lock refusal does not run mount cleanup against the active build"); + is_deeply([glob("$case/image/.genimage*")], [], "$os lock refusal creates no staging directory"); +} my %el_dracut_calls = (none => 2, 'kernel-copy' => 0, dracut => 1, 'move-initrd' => 1, 'publish-initrd' => 2); for my $failure (qw(none kernel-copy dracut move-initrd publish-initrd)) { @@ -75,6 +79,7 @@ for my $failure (qw(none kernel-copy dracut move-initrd publish-initrd)) { is(read_text("$case/image/initrd-$_.gz"), "$set initrd\n", "$label leaves the $set $_ initrd") for qw(statelite stateless); is($state->{dracut_calls}, $el_dracut_calls{$failure}, "$label stops at the first failed step"); + is($state->{lock_calls}, 1, "$label acquires the image operation lock"); is_deeply([glob("$case/image/rootimg/tmp/initrd.*.gz")], [], "$label removes temporary dracut output"); is_deeply([glob("$case/image/.genimage*")], [], "$label removes boot staging files"); } From cec5fd2557ba7448aca59b88ad5e5fde623428d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:32:09 -0300 Subject: [PATCH 17/17] docs(genimage): require the image lock for boot file publication --- docs/source/developers/guides/code/genimage_boot_files.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/source/developers/guides/code/genimage_boot_files.rst b/docs/source/developers/guides/code/genimage_boot_files.rst index 8c075d3947..1e72f84e94 100644 --- a/docs/source/developers/guides/code/genimage_boot_files.rst +++ b/docs/source/developers/guides/code/genimage_boot_files.rst @@ -11,6 +11,10 @@ image directory must always come from the same build. Rules ----- +#. Hold the image operation lock of the root image, + ``xCAT::Utils->acquire_lock_imageop``, also with ``--onlyinitrd``. The + rollback of a failed publication restores earlier files, so two runs on + the same image must not overlap. #. Build the boot files in a staging directory inside the image directory. Create it with ``File::Temp->newdir`` in the image directory, so that each file moves into place with a ``rename`` on the same file system. @@ -34,7 +38,8 @@ Scripts ``rh/genimage`` follows these rules. It is the ``genimage`` of RHEL, AlmaLinux, Rocky Linux, CentOS, Oracle Linux, Scientific Linux, Fedora and -openEuler. ``ubuntu/genimage`` also follows them. +openEuler. ``ubuntu/genimage`` follows them, except that it does not take +the image operation lock. The ``sles``, ``suse``, ``debian`` and ``fedora12`` scripts still write the kernel and the ramdisks directly into the image directory.