diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index db8d3c4007..0d5515fad8 100644 --- a/.github/workflows/xcat_test.yml +++ b/.github/workflows/xcat_test.yml @@ -31,7 +31,7 @@ jobs: run: sudo apt-get install -y --no-install-recommends cpio gzip libdbd-sqlite3-perl libxml-simple-perl libnet-dns-perl - name: Exercise image driver selection if: ${{ !cancelled() }} - run: prove xCAT-test/native/genimage_network_drivers.t + run: prove xCAT-test/native/genimage_network_drivers.t xCAT-test/native/genimage_rootimg_helpers.t - name: Exercise image package policy if: ${{ !cancelled() }} run: prove xCAT-test/native/el10_image_package_policy.t diff --git a/xCAT-test/bats/dracut_rootimg_helpers.bats b/xCAT-test/bats/dracut_rootimg_helpers.bats new file mode 100644 index 0000000000..3b96cfe611 --- /dev/null +++ b/xCAT-test/bats/dracut_rootimg_helpers.bats @@ -0,0 +1,93 @@ +#!/usr/bin/env bats + +load 'helpers/script_sandbox' +load 'helpers/dracut_module' + +VARIANTS='rh/dracut_033 rh/dracut_047 rh/dracut_105/stateless sles/dracut_033 ubuntu/dracut' + +setup() +{ + setup_script_sandbox cp mkdir dirname grep cmp stat + mkdir -p "$fixture/module" "$fixture/out" "$fixture/etc/udev/rules.d" + mkdir -p "$fixture/sbin" + sandbox+=(--tmpfs /usr --ro-bind "$fixture/bin" /usr/bin --ro-bind "$fixture/sbin" /usr/sbin) + for directory in /usr/lib /usr/lib64; do + [ ! -d "$directory" ] || sandbox+=(--ro-bind "$directory" "$directory") + done + [ -L /sbin ] || sandbox+=(--ro-bind "$fixture/sbin" /sbin) + printf '# xcat\n' >"$fixture/etc/udev/rules.d/01-xcat.rules" + cat >"$fixture/dracut.sh" <<'SH' +inst() { + mkdir -p "/run/fixture/out$(dirname "$2")" && + cp -p "$1" "/run/fixture/out$2" +} +inst_simple() { inst "$@"; } +inst_script() { inst "$@"; } +inst_hook() { inst "$3" "/lib/dracut/hooks/$1/$2-${3##*/}"; } +dracut_install() { + for file do + case "$file" in /usr/*/uftpd) inst "$file" "$file" || return ;; esac + done + return 0 +} +SH +} + +stage_module() +{ + local variant=$1 + rm -rf "$fixture/module" "$fixture/out" + mkdir -p "$fixture/module" "$fixture/out" + stage_dracut_module "$variant" "$fixture/module" + cp "$(repo_path xCAT/postscripts/updateflag.awk)" "$fixture/module/xcat-updateflag" + hook=install.netboot + [ "$variant" != rh/dracut_105/stateless ] || hook=module-setup.sh +} + +install_module() +{ + run "${sandbox[@]}" /bin/bash -c ' + . /run/fixture/dracut.sh + moddir=/run/fixture/module + if [ "$1" = module-setup.sh ]; then + . "$moddir/$1" + install + else + . "$moddir/$1" + fi + ' install "$hook" +} + +@test 'all stateless hooks deliver the checksum helper without UFTP' { + for variant in $VARIANTS; do + stage_module "$variant" + install_module + [ "$status" -eq 0 ] || { echo "$variant: $output"; return 1; } + cmp "$fixture/module/xcat-rootimg-checksum.sh" "$fixture/out/lib/xcat-rootimg-checksum.sh" + [ ! -e "$fixture/out/lib/xcat-rootimg-fetch.sh" ] + [ ! -e "$fixture/out/usr/sbin/uftpd" ] + [ ! -e "$fixture/out/usr/bin/uftpd" ] + done +} + +@test 'all stateless hooks stop when the checksum helper cannot be installed' { + for variant in $VARIANTS; do + stage_module "$variant" + rm "$fixture/module/xcat-rootimg-checksum.sh" + install_module + [ "$status" -ne 0 ] || { echo "$variant accepted a missing checksum helper"; return 1; } + [[ "$output" == *xcat-rootimg-checksum.sh* ]] + [ ! -e "$fixture/out/lib/xcat-rootimg-checksum.sh" ] + done +} + +@test 'all stateless hooks reject a missing checksum helper even if inst reports success' { + printf '\ninst() { :; }\n' >>"$fixture/dracut.sh" + for variant in $VARIANTS; do + stage_module "$variant" + rm "$fixture/module/xcat-rootimg-checksum.sh" + install_module + [ "$status" -ne 0 ] || { echo "$variant accepted a missing checksum helper"; return 1; } + [[ "$output" == *"Cannot read /run/fixture/module/xcat-rootimg-checksum.sh"* ]] + done +} diff --git a/xCAT-test/bats/xcatroot_rootimg_checksum.bats b/xCAT-test/bats/xcatroot_rootimg_checksum.bats index 42b7329c50..63bc3932ef 100644 --- a/xCAT-test/bats/xcatroot_rootimg_checksum.bats +++ b/xCAT-test/bats/xcatroot_rootimg_checksum.bats @@ -182,7 +182,7 @@ run_xcatroot() done local checksum_helper checksum_helper=$(repo_path xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-checksum.sh) - [ ! -f "$checksum_helper" ] || sandbox+=(--ro-bind "$checksum_helper" /lib/xcat-rootimg-checksum.sh) + [ ! -f "$checksum_helper" ] || [ -n "$NO_CHECKSUM_HELPER" ] || sandbox+=(--ro-bind "$checksum_helper" /lib/xcat-rootimg-checksum.sh) # genimage puts the helper in every initial ramdisk that can receive the root image over UFTP. [ -n "$NO_UFTP_HELPER" ] || sandbox+=(--ro-bind "$(repo_path xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-fetch.sh)" /lib/xcat-rootimg-fetch.sh) echo "imgurl=$URL XCAT=192.0.2.1:3001 nonodestatus $CMDLINE $UFTP $KERNEL_ARGS" >"$FX/cmdline" @@ -193,6 +193,7 @@ run_xcatroot() timeout 60 "${sandbox[@]}" /bin/bash -c " [ ! -e /fx/stale ] || cp /fx/stale /rootimg.cpio.gz $shell /xcatroot eth0 xcat /sysroot /fx/console 2>&1 + echo \$? >/fx/xcatroot.status cat /sysroot/etc/marker >/fx/extracted 2>/dev/null cp -P /sysroot/etc/resolv.conf /fx/newroot-resolv.conf 2>/dev/null cp -P /sysroot/etc/hostname /fx/newroot-hostname 2>/dev/null @@ -218,6 +219,40 @@ check_each_script() check_each_script 'packed image' "$URL.sha256 $URL $URL.sha256 " 0 '' } +@test 'a missing checksum helper stops xcatroot before an image is downloaded or extracted' { + NO_CHECKSUM_HELPER=1 + while read -r script shell; do + run_xcatroot "$script" "$shell" + [ "$(cat "$FX/xcatroot.status")" -ne 0 ] + [ ! -e "$FX/downloads" ] + [ ! -s "$FX/extracted" ] + grep -q 'xcat-rootimg-checksum.sh' "$FX/console" + done <<<"$ALL_SCRIPTS" +} + +@test 'checksum-only boots do not require the UFTP helper' { + NO_UFTP_HELPER=1 + check_each_script 'packed image' "$URL.sha256 $URL $URL.sha256 " 0 '' +} + +@test 'curl checksum failures retain the HTTP status in the diagnostic' { + SCRIPTS='xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot bash +xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot bash' + CHECKSUM_PLAN='503 503' MAXWAITS=1 + check_each_script '' "$URL.sha256 $URL $URL.sha256 " 1 'cannot download .*sha256 (HTTP 503)' +} + +@test 'wget checksum failures retain the diagnostic without an HTTP status suffix' { + CHECKSUM_PLAN='503 503' MAXWAITS=1 + while read -r script shell; do + case "$script" in */dracut_047/*|*/dracut_105/*) continue ;; esac + run_xcatroot "$script" "$shell" + case "$script" in */ubuntu/*) log="$FX/console" ;; *) log="$FX/logger" ;; esac + grep -Fq "cannot download $URL.sha256, retrying..." "$log" + [ ! -s "$FX/extracted" ] + done <<<"$ALL_SCRIPTS" +} + @test 'an image that matches a later line of the checksum file is extracted, as while packimage replaces it' { { printf '%064d rootimg.cpio.gz\n' 0; cat "$FX/served/rootimg.cpio.gz.sha256"; } >"$FX/list" mv "$FX/list" "$FX/served/rootimg.cpio.gz.sha256" diff --git a/xCAT-test/native/genimage_rootimg_helpers.t b/xCAT-test/native/genimage_rootimg_helpers.t new file mode 100644 index 0000000000..40479c46e2 --- /dev/null +++ b/xCAT-test/native/genimage_rootimg_helpers.t @@ -0,0 +1,131 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Path qw(make_path); +use File::Slurper qw(read_binary); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::ImageSandbox; + +plan skip_all => 'Linux namespaces require Linux' unless $^O eq 'linux'; +die('Run the image caller tests as an unprivileged user') unless $>; + +for my $case ([rh => '033'], [rh => '047'], [rh => '105'], [sles => '033'], [ubuntu => '059']) { + my ($family, $version) = @$case; + subtest "$family dracut $version" => sub { + my $box = XCAT::Test::ImageSandbox->new(); + my $image = 'work/image/rootimg'; + make_path(map { "$box->{root}/$image/$_" } + qw(bin dev run tmp boot etc lib/modules/fixture usr/lib/dracut/modules.d)); + $box->write('etc/os-release', "ID=sles\nVERSION=\"15\"\n"); + $box->write('etc/lsb-release', "DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=24.04\n"); + $box->write("$image/boot/vmlinuz-fixture", "kernel\n"); + $box->write("$image/lib/modules/fixture/$_", '') for qw(modules.dep modules.builtin); + $box->write('install/postscripts/updateflag.awk', ''); + $box->write('work/dracut-version', "Version : $version\n"); + $box->command('rpm', <<'SH'); +case "$*" in + --version) echo 'RPM version 4.18.2' ;; + '--root /work/image/rootimg -qi dracut') cat /work/dracut-version ;; + *) echo "Unexpected rpm: $*" >&2; exit 97 ;; +esac +SH + $box->command('chroot', <<'SH'); +root=$1 +shift +case "$*" in + 'rpm --version') echo 'RPM version 4.18.2' ;; + 'rpm -qi dracut') cat /work/dracut-version ;; + 'dpkg-query -W dracut') echo 'dracut 059' ;; + 'bash -c type -p pigz') exit 1 ;; + 'depmod fixture') : ;; + dracut\ *) + index=1 + while [ -d "/work/staged-$index" ]; do index=$((index + 1)); done + cp -a "$root/usr/lib/dracut/modules.d" "/work/staged-$index" || exit + while [ "$#" -gt 0 ]; do + if [ "$1" = '-f' ]; then shift; echo initrd >"$root$1"; break; fi + shift + done ;; + *) echo "Unexpected chroot: $*" >&2; exit 97 ;; +esac +SH + $box->command('mount', <<'SH'); +case "$*" in + 'proc /work/image/rootimg/proc -t proc'|'sysfs /work/image/rootimg/sys -t sysfs'|'--bind /run /work/image/rootimg/run') : ;; + *) echo "Unexpected mount: $*" >&2; exit 97 ;; +esac +SH + $box->command('umount', "exit 0\n"); + my $os = $family eq 'rh' ? 'rhels10.1' : $family eq 'sles' ? 'sles15.6' : 'ubuntu24.04'; + my @args = ('--onlyinitrd', '-a', 'x86_64', '-o', $os, '-p', 'compute', + '-k', 'fixture', '-i', 'eth0', '--rootimgdir', '/work/image'); + push @args, '-g', 'fixture' if $family eq 'sles'; + my ($rc, $out, $err) = $box->run('perl', "/repo/xCAT-server/share/xcat/netboot/$family/genimage", + @args, 'fixture-image'); + is($rc, 0, 'complete genimage caller succeeds') or diag($out, $err); + my $module = $family eq 'rh' ? '98xcat' : '97xcat'; + my $stateless = $family eq 'ubuntu' ? 2 : 1; + for my $index (1, 2) { + my $path = "$box->{root}/work/staged-$index/$module"; + ok(-d $path, 'dracut receives the generated module'); + for my $name (qw(xcat-rootimg-checksum.sh)) { + if ($index == $stateless || $family eq 'sles') { + ok(-f "$path/$name", "dracut module retains $name without a UFTP receiver"); + is(scalar(read_binary("$path/$name")), + scalar(read_binary(repo_path("xCAT-server/share/xcat/netboot/imgutils/$name"))), + 'the delivered helper matches the packaged asset') if -f "$path/$name"; + } else { + ok(!-e "$path/$name", "statelite dracut receives no $name"); + } + } + ok(!-e "$path/xcat-rootimg-fetch.sh", 'an image without a receiver gets no UFTP fetch helper'); + } + if ($family eq 'sles') { + make_path("$box->{root}/work/xcat/lib/perl", "$box->{root}/work/xcat/share"); + symlink('/repo/perl-xCAT/xCAT', "$box->{root}/work/xcat/lib/perl/xCAT") or die $!; + symlink('/repo/xCAT-server/share/xcat', "$box->{root}/work/xcat/share/xcat") or die $!; + $box->write('install/postscripts/xcatdsklspost', "#!/bin/sh\nexit 0\n"); + $box->write("$image/etc/shadow", "root:*:1:0:99999:7:::\n"); + $box->write('work/pack.pl', <<'PERL'); +use strict; +use warnings; +use xCAT::Table; +require '/repo/xCAT-server/lib/xcat/plugins/packimage.pm'; +xCAT::Table->new('site', -create => 1)->setAttribs({ key => 'installdir' }, { value => '/install' }); +xCAT::Table->new('passwd', -create => 1)->setAttribs( + { key => 'system', username => 'root' }, { password => '*', cryptmethod => 'sha512' }); +xCAT::Table->new('osimage', -create => 1)->setAttribs({ imagename => 'fixture-image' }, + { osvers => 'sles15.6', osarch => 'x86_64', profile => 'compute', provmethod => 'netboot' }); +xCAT::Table->new('linuximage', -create => 1)->setAttribs( + { imagename => 'fixture-image' }, { rootimgdir => '/work/image' }); +my @errors; +xCAT_plugin::packimage::process_request( + { arg => ['--nosyncfiles', '-m', 'cpio', '-c', 'gzip', 'fixture-image'] }, + sub { push @errors, @{ $_[0]{error} || [] } }); +die join("\n", @errors) if @errors; +PERL + ($rc, $out, $err) = $box->run('env', 'XCATROOT=/work/xcat', 'perl', '/work/pack.pl'); + is($rc, 0, 'packimage packs the SLES root left by genimage') or diag($out, $err); + ($rc, $out, $err) = $box->run('bash', '-o', 'pipefail', '-c', <<'SH'); +mkdir -p /work/unpacked /work/initrd +cd /work/unpacked || exit +gzip -dc /work/image/rootimg.cpio.gz | cpio -id --quiet || exit +moddir=/work/unpacked/usr/lib/dracut/modules.d/97xcat +inst() { mkdir -p "/work/initrd$(dirname "$2")" && cp "$1" "/work/initrd$2"; } +inst_hook() { :; } +dracut_install() { :; } +. "$moddir/install" +SH + is($rc, 0, 'the restored stateless hook runs from the packed SLES root') or diag($out, $err); + my $checksum = "$box->{root}/work/initrd/lib/xcat-rootimg-checksum.sh"; + ok(-f $checksum, 'the restored hook delivers the checksum helper for a later dracut build'); + is(scalar(read_binary($checksum)), + scalar(read_binary(repo_path('xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-checksum.sh'))), + 'the later build receives the packaged checksum helper') if -f $checksum; + } + }; +} +done_testing(); diff --git a/xCAT-test/unit/imgutils_rootimg_helpers.t b/xCAT-test/unit/imgutils_rootimg_helpers.t new file mode 100644 index 0000000000..33d306b9e6 --- /dev/null +++ b/xCAT-test/unit/imgutils_rootimg_helpers.t @@ -0,0 +1,47 @@ +#!/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 File::Path qw(make_path); +use File::Slurper qw(read_binary write_binary); +use File::Temp qw(tempdir); +use Test::More; +use imgutils; + +my $tmp = tempdir(CLEANUP => 1); +my $module = "$tmp/module"; +make_path($module); +my $name = 'xcat-rootimg-checksum.sh'; +my $source = "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils/$name"; +my $copy = "$module/$name"; + +imgutils::stage_rootimg_helpers($module, 1); +is(scalar(read_binary($copy)), scalar(read_binary($source)), 'checksum-only images receive the helper'); +is((stat($copy))[2] & oct('7777'), oct('644'), 'the sourced helper is readable but not executable'); +write_binary($copy, 'old helper'); +chmod(oct('777'), $copy) or die $!; +imgutils::stage_rootimg_helpers($module, 1); +is(scalar(read_binary($copy)), scalar(read_binary($source)), 'a repeated build replaces the old helper'); +is((stat($copy))[2] & oct('7777'), oct('644'), 'a repeated build restores the helper mode'); + +imgutils::stage_rootimg_helpers($module, 0); +ok(!-e $copy, 'disabled staging removes the previous checksum helper'); +symlink("$tmp/missing", $copy) or die $!; +imgutils::stage_rootimg_helpers($module, 0); +ok(!-l $copy, 'a disabled build removes a dangling helper link'); +write_binary("$tmp/other", 'other file'); +symlink("$tmp/other", $copy) or die $!; +imgutils::stage_rootimg_helpers($module, 1); +is(scalar(read_binary("$tmp/other")), 'other file', 'staging replaces a link without modifying its target'); +ok(!-l $copy, 'the module gets its own helper file'); + +eval { imgutils::stage_rootimg_helpers("$tmp/absent/module", 1) }; +like($@, qr{cannot copy .*\Q$name\E to \Q$tmp/absent/module/$name\E}, 'a failed copy stops the build'); +unlink($copy) or die $!; +make_path("$copy/child"); +eval { imgutils::stage_rootimg_helpers($module, 0) }; +like($@, qr{cannot remove \Q$copy\E}, 'a failed removal stops the build'); +done_testing();