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..1e72f84e94 --- /dev/null +++ b/docs/source/developers/guides/code/genimage_boot_files.rst @@ -0,0 +1,58 @@ +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 +----- + +#. 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. +#. 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`` 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. + +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 diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 9c71367c18..ba612c6dec 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); @@ -131,27 +131,62 @@ sub skip_kdump_without_dump_memory { close($fh) or die "Cannot write $dropin_dir/xcat-dump-memory.conf: $!\n"; } -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"); - } - 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"); +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; +} + +# 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); + my %backup; + foreach my $file (@files) { + my ($name, $what) = @$file; + if (link("$destdir/$name", "$native_bootdir/previous-$name")) { + $backup{$name} = 1; + } elsif (!$!{ENOENT}) { + die("Error: failed to preserve the previous $what: $!\n"); } - die("Error: failed to publish the initial ramdisk for $mode: $error\n"); + } + my @published; + foreach my $file (@files) { + my ($name, $what) = @$file; + if (rename("$native_bootdir/$name", "$destdir/$name")) { + push @published, $file; + 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 (@attempted, reverse @published) { + my ($done_name, $done_what) = @$done; + 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; + } + 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; } diff --git a/xCAT-server/share/xcat/netboot/rh/genimage b/xCAT-server/share/xcat/netboot/rh/genimage index eb6782f712..924c15f7cd 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 @_; @@ -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 { @@ -719,26 +717,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 +799,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 +995,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 +1292,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 +2033,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($@); } diff --git a/xCAT-server/share/xcat/netboot/ubuntu/genimage b/xCAT-server/share/xcat/netboot/ubuntu/genimage index abfdc8216a..19cd733d3e 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/genimage +++ b/xCAT-server/share/xcat/netboot/ubuntu/genimage @@ -732,16 +732,10 @@ chomp($kernelver); #$updates{'kernelver'} = $kernelver if ($tempfile); -# copy the kernel to $destdir -if (-e "$rootimg_dir/boot/vmlinux-$kernelver") { - cp("$rootimg_dir/boot/vmlinux-$kernelver", "$destdir/kernel"); -} elsif (-e "$rootimg_dir/boot/vmlinuz-$kernelver") { - cp("$rootimg_dir/boot/vmlinuz-$kernelver", "$destdir/kernel"); -} elsif (-e "$rootimg_dir/boot/image-$kernelver") { - cp("$rootimg_dir/boot/image-$kernelver", "$destdir/kernel"); -} else { - xdie("couldn't find the kernel file matched $kernelver in $rootimg_dir/boot"); -} +# 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; +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(); @@ -938,6 +932,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 +1060,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 +1882,7 @@ 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"); - system("rm -rf /tmp/xcatinitrd.$$"); + imgutils::pack_initrd("/tmp/xcatinitrd.$$", "$native_bootdir/initrd-$mode.gz", $compress, $mode); } diff --git a/xCAT-test/native/openeuler_genimage_bootfiles.t b/xCAT-test/native/openeuler_genimage_bootfiles.t index 8ee520a1ad..a594995650 100644 --- a/xCAT-test/native/openeuler_genimage_bootfiles.t +++ b/xCAT-test/native/openeuler_genimage_bootfiles.t @@ -49,23 +49,39 @@ 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"); +} -for my $failure (qw(none kernel-copy dracut move-initrd)) { +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); - 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"); + 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}, 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"); } done_testing(); @@ -86,7 +102,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"); 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(); 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..2650607fca --- /dev/null +++ b/xCAT-test/unit/imgutils_staged_boot_files.t @@ -0,0 +1,155 @@ +#!/usr/bin/env perl +use strict; +use warnings; +## no critic (TestingAndDebugging::ProhibitNoWarnings, Variables::RequireLocalizedPunctuationVars) +use Errno qw(EACCES EIO); +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, with the faults that %case names. +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{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}")) { + unlink("$staging/previous-$case{lost}") if defined $case{lost}; + $! = 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"); +} + +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'); + 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'); +} + +{ + 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(); 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); -}