From b473ce90d06f5166711918ecfaa49f96b56717f5 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 16:59:53 -0300 Subject: [PATCH 1/5] fix(ubuntu): lock the root image while genimage builds it packimage, rmimage and the EL genimage take the image operation lock of a root image, but the Ubuntu genimage did not. packimage or rmimage could work on a root image while genimage installed packages in it, and two genimage or geninitrd runs could change one root image at the same time. genimage now takes the lock before it changes the root image, for a full build and for the --onlyinitrd run of geninitrd, and holds it until it exits. --- xCAT-server/share/xcat/netboot/ubuntu/genimage | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xCAT-server/share/xcat/netboot/ubuntu/genimage b/xCAT-server/share/xcat/netboot/ubuntu/genimage index abfdc8216a..d97cf8ce1e 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/genimage +++ b/xCAT-server/share/xcat/netboot/ubuntu/genimage @@ -66,6 +66,7 @@ my $tempfile; my $prompt; my $noupdate; my $kernelimage; +my $lock; sub xdie { @@ -218,6 +219,15 @@ foreach (@ndrivers, @default_ndrivers) { my $uarch = xCAT::Utils->debian_arch($arch); +# The lock is named after the real path of the root image, which must exist. +mkpath($rootimg_dir) unless ($onlyinitrd); +my $lockerror; +($lockerror, $lock) = xCAT::Utils->acquire_lock_imageop($rootimg_dir); +if ($lockerror) { + print "Error: $lock\n"; + exit 1; +} + unless ($onlyinitrd) { @aptdirs = (); From 3e3ee8f108479cf46abd8f685fc232cc54b4bad8 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 18:39:24 -0300 Subject: [PATCH 2/5] fix(ubuntu): publish the newest kernel of a root image genimage took the last kernel of a sorted file list. When an image held 6.8.0-99-generic and 6.8.0-100-generic, it published -99 and built the initrds for it. The uname of the image build also reported -99 to the package scripts, and it ignored an explicit kernelver. genimage and that uname now compare kernel releases number by number, so both choose -100. An explicit kernelver decides for both. --- .../share/xcat/netboot/imgutils/imgutils.pm | 37 +++++++++++++++++++ .../share/xcat/netboot/ubuntu/genimage | 17 +-------- 2 files changed, 39 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..9af7c10c35 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -249,6 +249,43 @@ sub disable_auto_profiles { return; } +# A kernel release compares number by number, so 6.8.0-100-generic is newer than 6.8.0-99-generic. +sub newest_kernel_version { + my @releases = @_; + my $compare = sub { + my @left = split(/(\d+)/, $_[0]); + my @right = split(/(\d+)/, $_[1]); + while (@left && @right) { + my ($l, $r) = (shift(@left), shift(@right)); + my $order = ($l =~ /^\d+$/ && $r =~ /^\d+$/) ? $l <=> $r : $l cmp $r; + return $order if $order; + } + return @left <=> @right; + }; + my ($newest) = sort { $compare->($b, $a) } @releases; + return $newest; +} + +# Package scripts run this uname in the image, so -r names the kernel that genimage publishes. +sub uname_script { + my ($kernelver) = @_; + my $release = $kernelver ? "echo \"$kernelver\"" : 'ls /lib/modules | sort -V | tail -n 1'; + return "#!/bin/bash \n" + . "case \"\$1\" in \n" + . "\"-m\") \n" + . " ARCH=\"\$(dpkg --print-architecture || \n" + . " rpm -q kernel-\$(\"\$0\" -r) --qf '%{arch}')\" \n" + . " case \"\$ARCH\" in \n" + . " \"amd64\") ARCH=\"x86_64\" ;; \n" + . " \"ppc64el\") ARCH=\"ppc64le\" ;; \n" + . " esac \n" + . " echo \"\$ARCH\" ;; \n" + . "\"-r\") $release ;; \n" + . "\"-s\"|\"\") echo \"Linux\" ;; \n" + . "esac \n" + . "exit 0 \n"; +} + sub varsubinline{ my $line=shift; my $refvardict=shift; diff --git a/xCAT-server/share/xcat/netboot/ubuntu/genimage b/xCAT-server/share/xcat/netboot/ubuntu/genimage index d97cf8ce1e..1c2714b00c 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/genimage +++ b/xCAT-server/share/xcat/netboot/ubuntu/genimage @@ -733,7 +733,7 @@ foreach (@KVERS) { @KVERS = <$rootimg_dir/lib/modules/*> unless (scalar(@KVERS)); -$basekernelver = basename(pop @KVERS) if (scalar(@KVERS)); +$basekernelver = imgutils::newest_kernel_version(map { basename($_) } @KVERS) if (scalar(@KVERS)); $basekernelver = `uname -r` unless ($basekernelver); @@ -2269,20 +2269,7 @@ sub use_hackuname { # write the hacked uname script my $unamefile; open($unamefile, ">", "$rootimg_dir/bin/uname"); - print $unamefile "#!/bin/bash \n"; - print $unamefile "case \"\$1\" in \n"; - print $unamefile "\"-m\") \n"; - print $unamefile " ARCH=\"\$(dpkg --print-architecture || \n"; - print $unamefile " rpm -q kernel-\$(\"\$0\" -r) --qf '%{arch}')\" \n"; - print $unamefile " case \"\$ARCH\" in \n"; - print $unamefile " \"amd64\") ARCH=\"x86_64\" ;; \n"; - print $unamefile " \"ppc64el\") ARCH=\"ppc64le\" ;; \n"; - print $unamefile " esac \n"; - print $unamefile " echo \"\$ARCH\" ;; \n"; - print $unamefile "\"-r\") cd /lib/modules && for d in * ; do : ; done && echo \$d ;; \n"; - print $unamefile "\"-s\"|\"\") echo \"Linux\" ;; \n"; - print $unamefile "esac \n"; - print $unamefile "exit 0 \n"; + print $unamefile imgutils::uname_script($kernelver); close $unamefile; chmod(0755, "$rootimg_dir/bin/uname"); } From 38e00d0cf2cce47579fa9a3478e794f6386b30d0 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 18:39:24 -0300 Subject: [PATCH 3/5] test(ubuntu): cover the kernel that genimage publishes --- xCAT-test/unit/imgutils_kernel_version.t | 24 ++++++++++++++++ xCAT-test/unit/imgutils_uname_script.t | 35 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 xCAT-test/unit/imgutils_kernel_version.t create mode 100644 xCAT-test/unit/imgutils_uname_script.t diff --git a/xCAT-test/unit/imgutils_kernel_version.t b/xCAT-test/unit/imgutils_kernel_version.t new file mode 100644 index 0000000000..ff4fd9e3f9 --- /dev/null +++ b/xCAT-test/unit/imgutils_kernel_version.t @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +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; + +for my $case ( + [ [qw(6.8.0-99-generic 6.8.0-100-generic)], '6.8.0-100-generic', 'an ABI of 100 is newer than an ABI of 99' ], + [ [qw(6.10.0-1-generic 6.9.0-1-generic)], '6.10.0-1-generic', 'kernel 6.10 is newer than kernel 6.9' ], + [ [qw(6.8.0-100-generic 7.0.0-42-generic)], '7.0.0-42-generic', 'a newer kernel wins over a larger ABI' ], + [ [qw(7.0.0-42-generic)], '7.0.0-42-generic', 'an image with one kernel gets that kernel' ], +) { + my ($releases, $newest, $label) = @$case; + is(imgutils::newest_kernel_version(@$releases), $newest, $label); + is(imgutils::newest_kernel_version(reverse @$releases), $newest, "$label, in either order"); +} + +is(imgutils::newest_kernel_version(), undef, 'an image without a kernel gets no kernel release'); + +done_testing(); diff --git a/xCAT-test/unit/imgutils_uname_script.t b/xCAT-test/unit/imgutils_uname_script.t new file mode 100644 index 0000000000..c75603cca2 --- /dev/null +++ b/xCAT-test/unit/imgutils_uname_script.t @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Slurper qw(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 => 'bash is not installed' unless -x '/bin/bash'; + +# The script runs in the image, which lists its kernels in /lib/modules. This ls lists two of them. +my $bin = tempdir(CLEANUP => 1); +write_text("$bin/ls", "#!/bin/sh\n[ \"\$1\" = /lib/modules ] && printf '6.8.0-99-generic\\n6.8.0-100-generic\\n'\n"); +chmod(0755, "$bin/ls") or die "chmod: $!"; + +sub uname { + my ($kernelver, @options) = @_; + my $script = tempdir(CLEANUP => 1) . '/uname'; + write_text($script, imgutils::uname_script($kernelver)); + local $ENV{PATH} = "$bin:$ENV{PATH}"; + my $output = `/bin/bash $script @options`; + chomp($output); + return $output; +} + +is(uname('', '-r'), '6.8.0-100-generic', 'uname -r names the newest kernel of the image'); +is(uname('6.8.0-99-generic', '-r'), '6.8.0-99-generic', 'uname -r names an explicit kernelver'); +is(uname('', '-s'), 'Linux', 'uname -s names Linux'); +is(uname(''), 'Linux', 'uname without an option names Linux'); + +done_testing(); From 4ea954099ac6319271c888b1e01290850357b445 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 18:39:24 -0300 Subject: [PATCH 4/5] fix(ubuntu): reuse the root image of an earlier genimage run genimage ran debootstrap on every run. For Ubuntu 19.04 and later, debootstrap extracts with tar -k, so a second run on an existing root image failed at base-files and blamed the mirror. genimage now skips debootstrap when an earlier run finished it and updates the image with apt. The image holds the dummy resolv.conf and the otherpkgs apt sources of that run. Before the first apt command, genimage removes the apt sources that it writes again later and gives the image the hosts and resolv.conf of the build host. A .bak file that an interrupted run left is kept, so the image still gets its own file back. genimage stops with an error and leaves the root image unchanged in three cases. The root image holds another Ubuntu release or architecture, the debootstrap directory of an unfinished run, or the bin/uname-binary backup of an interrupted run. --- .../share/xcat/netboot/imgutils/imgutils.pm | 58 +++++++++++++++++++ .../share/xcat/netboot/ubuntu/genimage | 28 ++++----- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 9af7c10c35..0ecd365b1f 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -249,6 +249,64 @@ sub disable_auto_profiles { return; } +# debootstrap deletes $rootimg_dir/debootstrap when it finishes, and its tar -k fails over a finished root. +sub debootstrap_finished { + my ($rootimg_dir) = @_; + die("Error: $rootimg_dir holds an unfinished debootstrap. Remove $rootimg_dir and run genimage again.\n") + if -e "$rootimg_dir/debootstrap"; + return -e "$rootimg_dir/var/lib/dpkg/status" ? 1 : 0; +} + +# genimage moves the .bak files back, and a .bak that an interrupted run left holds the image's own file. +sub use_host_resolver { + my ($rootimg_dir) = @_; + foreach my $file ('hosts', 'resolv.conf') { + my $image = "$rootimg_dir/etc/$file"; + move($image, "$image.bak") unless -e "$image.bak" || -l "$image.bak"; + unlink($image); + File::Copy::copy("/etc/$file", $image); + } + return; +} + +# A finished image holds the dummy resolv.conf and the apt sources of the run that built it. +sub prepare_reused_root { + my ($rootimg_dir) = @_; + unlink(map { "$rootimg_dir/etc/apt/sources.list.d/$_" } qw(genimage.apt.list genimage1.apt.list)); + use_host_resolver($rootimg_dir); + return; +} + +# apt would move a root image of another release or architecture partway to the requested one. +sub check_reused_root { + my ($rootimg_dir, $suite, $arch) = @_; + my ($codename, $image_arch); + my $os_release = -e "$rootimg_dir/usr/lib/os-release" ? "$rootimg_dir/usr/lib/os-release" : "$rootimg_dir/etc/os-release"; + my %field; + foreach my $file ($os_release, "$rootimg_dir/etc/lsb-release") { + my $fh; + next if -l $file || !open($fh, '<', $file); + %field = (%field, map { /^(VERSION_CODENAME|UBUNTU_CODENAME|DISTRIB_CODENAME)=(["']?)(\S+?)\2\s*$/ ? ($1 => $3) : () } <$fh>); + close($fh); + } + # Ubuntu 16.04 images before base-files 9.4ubuntu4.3 have only UBUNTU_CODENAME, and 14.04 images only DISTRIB_CODENAME. + $codename = $field{VERSION_CODENAME} // $field{UBUNTU_CODENAME} // $field{DISTRIB_CODENAME}; + if (open(my $fh, '<', "$rootimg_dir/var/lib/dpkg/status")) { + local $/ = ''; + ($image_arch) = map { /^Package: dpkg\n(?:.+\n)*?Architecture: (\S+)/ ? $1 : () } <$fh>; + close($fh); + } + (my $release = $suite) =~ s/-(?:updates|security|backports|proposed)$//; + die("Error: genimage cannot tell the Ubuntu release and architecture of $rootimg_dir. Remove $rootimg_dir and run genimage again.\n") + unless $codename && $image_arch; + die("Error: $rootimg_dir holds Ubuntu $codename for $image_arch, not $release for $arch. Remove $rootimg_dir or use another rootimgdir.\n") + unless $codename eq $release && $image_arch eq $arch; + # dpkg owns bin/uname, so genimage cannot tell whether the backup or the file in place is current. + die("Error: $rootimg_dir holds bin/uname-binary, which an interrupted genimage run left. Restore bin/uname from it or remove $rootimg_dir, then run genimage again.\n") + if -e "$rootimg_dir/bin/uname-binary" || -l "$rootimg_dir/bin/uname-binary"; + return 1; +} + # A kernel release compares number by number, so 6.8.0-100-generic is newer than 6.8.0-99-generic. sub newest_kernel_version { my @releases = @_; diff --git a/xCAT-server/share/xcat/netboot/ubuntu/genimage b/xCAT-server/share/xcat/netboot/ubuntu/genimage index 1c2714b00c..ddcf0a607e 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/genimage +++ b/xCAT-server/share/xcat/netboot/ubuntu/genimage @@ -327,11 +327,6 @@ unless ($onlyinitrd) { close($aptconfig); mkpath "$rootimg_dir/etc"; - my $fd; - open($fd, ">>", "$rootimg_dir/etc/fstab"); - print $fd "#Dummy fstab for dpkg postscripts to see\n"; - close($fd); - my $non_interactive; if (!$prompt) { $non_interactive = "-y"; } @@ -369,8 +364,19 @@ unless ($onlyinitrd) { $aptcmd2 = "--verbose --arch $uarch $dist $rootimg_dir file://$srcdir"; } - print "Run cmd [$aptcmd1 $aptcmd2] to create rootimage bootstraps\n"; - my $rc = system("$aptcmd1 $aptcmd2"); + my $rc = 0; + if (imgutils::debootstrap_finished($rootimg_dir)) { + imgutils::check_reused_root($rootimg_dir, $dist, $uarch); + print "Updating the existing root image in $rootimg_dir\n"; + imgutils::prepare_reused_root($rootimg_dir); + } else { + my $fd; + open($fd, ">>", "$rootimg_dir/etc/fstab"); + print $fd "#Dummy fstab for dpkg postscripts to see\n"; + close($fd); + print "Run cmd [$aptcmd1 $aptcmd2] to create rootimage bootstraps\n"; + $rc = system("$aptcmd1 $aptcmd2"); + } if ($rc) { my $os=xCAT::Utils->osver("os"); if ($os ne 'ubuntu') { @@ -531,13 +537,7 @@ unless ($onlyinitrd) { } close($aptconfig); - #backup the /etc/hosts & /etc/resolv.conf - move("$rootimg_dir/etc/hosts", "$rootimg_dir/etc/hosts.bak"); - move("$rootimg_dir/etc/resolv.conf", "$rootimg_dir/etc/resolv.conf.bak"); - - #copy the mn's /etc/hosts & /etc/resolv.conf to the rootimage - copy("/etc/hosts", "$rootimg_dir/etc/hosts"); - copy("/etc/resolv.conf", "$rootimg_dir/etc/resolv.conf"); + imgutils::use_host_resolver($rootimg_dir); #Now let's handle extra packages unless ($imagename) { From 9d40a2e0d37410b84df0cabbe8524bde9b369c50 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 18:39:24 -0300 Subject: [PATCH 5/5] test(ubuntu): cover genimage reuse of a finished root image --- .../unit/imgutils_debootstrap_finished.t | 49 ++++++++ xCAT-test/unit/imgutils_host_resolver.t | 77 +++++++++++++ xCAT-test/unit/imgutils_reused_root.t | 51 +++++++++ xCAT-test/unit/imgutils_reused_root_release.t | 105 ++++++++++++++++++ 4 files changed, 282 insertions(+) create mode 100644 xCAT-test/unit/imgutils_debootstrap_finished.t create mode 100644 xCAT-test/unit/imgutils_host_resolver.t create mode 100644 xCAT-test/unit/imgutils_reused_root.t create mode 100644 xCAT-test/unit/imgutils_reused_root_release.t diff --git a/xCAT-test/unit/imgutils_debootstrap_finished.t b/xCAT-test/unit/imgutils_debootstrap_finished.t new file mode 100644 index 0000000000..bd7138308a --- /dev/null +++ b/xCAT-test/unit/imgutils_debootstrap_finished.t @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Path qw(make_path); +use File::Slurper qw(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; + +sub image_root { + my (@files) = @_; + my $root = tempdir(CLEANUP => 1) . '/rootimg'; + make_path($root); + for my $file (@files) { + if ($file =~ m{/$}) { + make_path("$root/$file"); + } else { + make_path("$root/" . ($file =~ s{/[^/]+$}{}r)); + write_text("$root/$file", ''); + } + } + return $root; +} + +is(imgutils::debootstrap_finished(tempdir(CLEANUP => 1) . '/rootimg'), 0, + 'a missing root image needs debootstrap'); +is(imgutils::debootstrap_finished(image_root('etc/fstab')), 0, + 'a root image with only the dummy fstab of genimage needs debootstrap'); +is(imgutils::debootstrap_finished(image_root('var/lib/dpkg/')), 0, + 'a root image without a dpkg status file needs debootstrap'); +is(imgutils::debootstrap_finished(image_root('etc/fstab', 'var/lib/dpkg/status')), 1, + 'a root image that debootstrap finished is reused'); + +for my $case ( + [ 'debootstrap/debootstrap.log', 'var/lib/dpkg/status' ], + [ 'debootstrap/' ], +) { + my $root = image_root(@$case); + my $finished = eval { imgutils::debootstrap_finished($root) }; + is($finished, undef, "an unfinished root image with @$case stops genimage"); + like($@, qr/^Error: \Q$root\E holds an unfinished debootstrap\. Remove \Q$root\E /, + "the error for a root image with @$case names the root image to remove"); +} + +done_testing(); diff --git a/xCAT-test/unit/imgutils_host_resolver.t b/xCAT-test/unit/imgutils_host_resolver.t new file mode 100644 index 0000000000..761e46024e --- /dev/null +++ b/xCAT-test/unit/imgutils_host_resolver.t @@ -0,0 +1,77 @@ +#!/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 => 'the build host has no /etc/hosts or /etc/resolv.conf' + unless -r '/etc/hosts' && -r '/etc/resolv.conf'; + +my %host = map { $_ => read_text("/etc/$_") } qw(hosts resolv.conf); +my $stub = '../run/systemd/resolve/stub-resolv.conf'; +my $dummy = '#Dummy resolv.conf to make boot cleaner'; + +# A root image as an earlier genimage run leaves it: resolv.conf links to the stub that holds the dummy. +sub image_root { + my $root = tempdir(CLEANUP => 1); + make_path("$root/etc", "$root/run/systemd/resolve"); + write_text("$root/etc/hosts", "127.0.0.1 localhost\n"); + write_text("$root/run/systemd/resolve/stub-resolv.conf", $dummy); + symlink($stub, "$root/etc/resolv.conf") or die "symlink: $!"; + return $root; +} + +sub has_host_files { + my ($root, $label) = @_; + for my $file (qw(hosts resolv.conf)) { + ok(!-l "$root/etc/$file", "$label: etc/$file is a file, not a link"); + is(read_text("$root/etc/$file"), $host{$file}, "$label: etc/$file holds the file of the build host"); + } + return; +} + +{ + my $root = image_root(); + for my $pass (1, 2) { + imgutils::use_host_resolver($root); + has_host_files($root, "pass $pass"); + is(read_text("$root/etc/hosts.bak"), "127.0.0.1 localhost\n", "pass $pass: hosts.bak holds the hosts of the image"); + is(readlink("$root/etc/resolv.conf.bak"), $stub, "pass $pass: resolv.conf.bak is the link of the image"); + } + is(read_text("$root/run/systemd/resolve/stub-resolv.conf"), $dummy, 'the stub that the link names is not written'); +} + +{ + my $root = image_root(); + my $outside = tempdir(CLEANUP => 1) . '/stub-resolv.conf'; + write_text($outside, "nameserver 192.0.2.1\n"); + unlink("$root/etc/resolv.conf"); + symlink($outside, "$root/etc/resolv.conf") or die "symlink: $!"; + imgutils::use_host_resolver($root); + is(readlink("$root/etc/resolv.conf.bak"), $outside, 'an absolute link is moved aside, not followed'); + + # A package script can turn resolv.conf back into a link while the .bak files exist. + unlink("$root/etc/resolv.conf"); + symlink($outside, "$root/etc/resolv.conf") or die "symlink: $!"; + imgutils::use_host_resolver($root); + has_host_files($root, 'after resolv.conf is linked again'); + is(readlink("$root/etc/resolv.conf.bak"), $outside, 'after resolv.conf is linked again: resolv.conf.bak still holds the link of the image'); + is(read_text($outside), "nameserver 192.0.2.1\n", 'a file outside the image is never written through a link'); +} + +{ + my $root = image_root(); + unlink("$root/etc/hosts"); + imgutils::use_host_resolver($root); + has_host_files($root, 'an image without hosts'); + ok(!-e "$root/etc/hosts.bak", 'an image without hosts gets no hosts.bak'); +} + +done_testing(); diff --git a/xCAT-test/unit/imgutils_reused_root.t b/xCAT-test/unit/imgutils_reused_root.t new file mode 100644 index 0000000000..dc7971e962 --- /dev/null +++ b/xCAT-test/unit/imgutils_reused_root.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 => 'the build host has no /etc/hosts or /etc/resolv.conf' + unless -r '/etc/hosts' && -r '/etc/resolv.conf'; + +sub sources { + my ($dir) = @_; + opendir(my $dh, $dir) or die "opendir $dir: $!"; + my @files = sort grep { !/^\./ } readdir($dh); + closedir($dh); + return \@files; +} + +my $root = tempdir(CLEANUP => 1); +my $sources = "$root/etc/apt/sources.list.d"; +my $site = "deb http://192.0.2.2/site ./\n"; +make_path($sources); +write_text("$sources/$_", "deb http://192.0.2.1/retired ./\n") for qw(genimage.apt.list genimage1.apt.list); +write_text("$sources/site.list", $site); +write_text("$root/etc/hosts", "127.0.0.1 localhost\n"); +write_text("$root/etc/resolv.conf", '#Dummy resolv.conf to make boot cleaner'); + +for my $pass (1, 2) { + imgutils::prepare_reused_root($root); + is_deeply(sources($sources), ['site.list'], "pass $pass: only the apt sources that genimage writes are removed"); + is(read_text("$sources/site.list"), $site, "pass $pass: a site source is kept as it is"); + is(read_text("$root/etc/resolv.conf"), read_text('/etc/resolv.conf'), + "pass $pass: the image gets the resolv.conf of the build host"); + is(read_text("$root/etc/resolv.conf.bak"), '#Dummy resolv.conf to make boot cleaner', + "pass $pass: resolv.conf.bak holds the resolv.conf of the image"); +} + +{ + my $bare = tempdir(CLEANUP => 1); + make_path("$bare/etc"); + imgutils::prepare_reused_root($bare); + is(read_text("$bare/etc/hosts"), read_text('/etc/hosts'), 'an image without apt sources still gets the hosts of the build host'); +} + +done_testing(); diff --git a/xCAT-test/unit/imgutils_reused_root_release.t b/xCAT-test/unit/imgutils_reused_root_release.t new file mode 100644 index 0000000000..04276341e6 --- /dev/null +++ b/xCAT-test/unit/imgutils_reused_root_release.t @@ -0,0 +1,105 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Find; +use File::Path qw(make_path); +use File::Slurper qw(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; + +# A finished root image with the os-release and the dpkg status that Ubuntu writes. +sub image_root { + my (%overrides) = @_; + my %image = (codename => 'resolute', arch => 'riscv64', field => 'VERSION_CODENAME', %overrides); + my $root = tempdir(CLEANUP => 1); + make_path("$root/usr/lib", "$root/etc", "$root/var/lib/dpkg"); + write_text("$root/usr/lib/os-release", "NAME=\"Ubuntu\"\nVERSION_ID=\"26.04\"\n$image{field}=$image{codename}\nID=ubuntu\n") + if defined $image{codename}; + symlink('../usr/lib/os-release', "$root/etc/os-release") or die "symlink: $!"; + write_text("$root/var/lib/dpkg/status", + "Package: debconf\nStatus: install ok installed\nArchitecture: all\n\n" + . "Package: dpkg\nEssential: yes\nStatus: install ok installed\nArchitecture: $image{arch}\nVersion: 1.23.5ubuntu1\n\n" + . "Package: dpkg-dev\nStatus: install ok installed\nArchitecture: all\n"); + return $root; +} + +sub snapshot { + my ($root) = @_; + my @entries; + find({ wanted => sub { push @entries, join(' ', $File::Find::name, (lstat)[1, 2, 7, 9]) }, no_chdir => 1 }, $root); + return [sort @entries]; +} + +for my $case ( + [ 'resolute', 'riscv64', 'the release and architecture that built the image' ], + [ 'resolute-updates', 'riscv64', 'the updates pocket of that release' ], + [ 'resolute-security', 'riscv64', 'the security pocket of that release' ], +) { + my ($suite, $arch, $label) = @$case; + is(eval { imgutils::check_reused_root(image_root(), $suite, $arch) }, 1, "a root image is reused for $label") or diag($@); +} + +{ + my $root = image_root(codename => '"resolute"'); + is(eval { imgutils::check_reused_root($root, 'resolute', 'riscv64') }, 1, 'a quoted VERSION_CODENAME is read') or diag($@); +} + +{ + my $root = image_root(codename => 'xenial', arch => 'amd64', field => 'UBUNTU_CODENAME'); + is(eval { imgutils::check_reused_root($root, 'xenial', 'amd64') }, 1, 'an Ubuntu 16.04 image with only UBUNTU_CODENAME is reused') or diag($@); +} + +{ + # Ubuntu 14.04 names its release only in lsb-release. + my $root = image_root(codename => undef, arch => 'amd64'); + unlink("$root/etc/os-release"); + write_text("$root/etc/os-release", "NAME=\"Ubuntu\"\nVERSION_ID=\"14.04\"\nID=ubuntu\n"); + write_text("$root/etc/lsb-release", "DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=14.04\nDISTRIB_CODENAME=trusty\n"); + is(eval { imgutils::check_reused_root($root, 'trusty', 'amd64') }, 1, 'an Ubuntu 14.04 image with only DISTRIB_CODENAME is reused') or diag($@); + is(eval { imgutils::check_reused_root($root, 'xenial', 'amd64') }, undef, 'an Ubuntu 14.04 image is not reused for Ubuntu 16.04'); + like($@, qr/holds Ubuntu trusty for amd64, not xenial for amd64/, 'the error names the release of the Ubuntu 14.04 image'); +} + +for my $case ( + [ [], 'noble', 'riscv64', qr/holds Ubuntu resolute for riscv64, not noble for riscv64\. Remove /, 'another release' ], + [ [], 'resolute', 'amd64', qr/holds Ubuntu resolute for riscv64, not resolute for amd64\. Remove /, 'another architecture' ], + [ [ codename => undef ], 'resolute', 'riscv64', qr/cannot tell the Ubuntu release and architecture/, 'no os-release' ], + [ [ arch => '' ], 'resolute', 'riscv64', qr/cannot tell the Ubuntu release and architecture/, 'no dpkg architecture' ], +) { + my ($image, $suite, $arch, $error, $label) = @$case; + my $root = image_root(@$image); + my $before = snapshot($root); + my $result = eval { imgutils::check_reused_root($root, $suite, $arch) }; + is($result, undef, "a root image is not reused for $label"); + like($@, $error, "the error for $label names the mismatch"); + is_deeply(snapshot($root), $before, "a root image refused for $label is left as it was"); +} + +{ + my $root = image_root(); + make_path("$root/bin"); + write_text("$root/bin/uname-binary", "uname\n"); + my $before = snapshot($root); + is(eval { imgutils::check_reused_root($root, 'resolute', 'riscv64') }, undef, + 'a root image with the uname backup of an interrupted run is not reused'); + like($@, qr/holds bin\/uname-binary, which an interrupted genimage run left\. Restore bin\/uname from it or remove /, + 'the error says how to recover the uname of the image'); + is_deeply(snapshot($root), $before, 'a root image with the uname backup is left as it was'); +} + +{ + # An absolute link would read the os-release of the build host, which can name the same release. + my $root = image_root(codename => undef); + my $host = tempdir(CLEANUP => 1) . '/os-release'; + write_text($host, "VERSION_CODENAME=resolute\n"); + unlink("$root/etc/os-release"); + symlink($host, "$root/etc/os-release") or die "symlink: $!"; + is(eval { imgutils::check_reused_root($root, 'resolute', 'riscv64') }, undef, 'an os-release link that leaves the image is not followed'); +} + +done_testing();