diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index aa059baf5f..2a9d321cfe 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-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index bcbfea0994..8d9067b8ba 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -210,6 +210,21 @@ sub add_uftp_receiver { return $receiver; } +sub stage_rootimg_helpers { + my ($moduledir, $wanted) = @_; + foreach my $name (qw(xcat-rootimg-checksum.sh xcat-rootimg-install.sh)) { + my $copy = "$moduledir/$name"; + if (-e $copy || -l $copy) { + unlink($copy) or die("Error: cannot remove $copy: $!\n"); + } + next unless $wanted; + my $helper = dirname(__FILE__) . "/$name"; + File::Copy::copy($helper, $copy) && chmod(oct('644'), $copy) + or die("Error: cannot copy $helper to $copy: $!\n"); + } + return; +} + # Returns the lsinitrd lines of the initial ramdisk $initrd, a path inside the image, or undef when the image cannot list it. sub initrd_listing { my ($rootimg_dir, $initrd) = @_; diff --git a/xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-checksum.sh b/xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-checksum.sh new file mode 100644 index 0000000000..1b6baa2def --- /dev/null +++ b/xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-checksum.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +# Callers provide rootimg_fetch_checksum and rootimg_checksum_error for their download command. +# Callers log failures printed for the image URL ($1) and downloaded file ($2). +rootimg_matches_checksum() { + command -v sha256sum >/dev/null 2>&1 || { echo "this initial ramdisk has no sha256sum to check $1"; return 1; } + rootimg_fetch_checksum "$1" /tmp/rootimg.sha256 || { rootimg_checksum_error "$1"; return 1; } + rootimg_sum=$(sha256sum <"$2") || { echo "sha256sum cannot read $2"; return 1; } + # A download that overlaps packimage can match either the current checksum or the one fetched before it. + cat /tmp/rootimg.sha256 /tmp/rootimg.sha256.before >/tmp/rootimg.sums 2>/dev/null + while read -r rootimg_expected rootimg_name; do + [ "${rootimg_sum%% *}" = "$rootimg_expected" ] && return 0 + done &2; exit 1; } +inst "$moddir/xcat-rootimg-checksum.sh" "/lib/xcat-rootimg-checksum.sh" || exit 1 + +. "$moddir/xcat-rootimg-install.sh" || exit 1 +xcat_install_uftp_receiver inst for file in /etc/udev/rules.d/*;do grep -qi xcat $file && inst $file $file done diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_033/xcatroot b/xCAT-server/share/xcat/netboot/rh/dracut_033/xcatroot index 8724653db5..50ef53bf2e 100755 --- a/xCAT-server/share/xcat/netboot/rh/dracut_033/xcatroot +++ b/xCAT-server/share/xcat/netboot/rh/dracut_033/xcatroot @@ -37,21 +37,12 @@ rootimg_fetch_checksum() { wget -q -O "$2" "$1.sha256" } -# Prints why the file $2 cannot be used, unless it matches the SHA-256 checksum published for the image at $1. -rootimg_matches_checksum() { - command -v sha256sum >/dev/null 2>&1 || { echo "this initial ramdisk has no sha256sum to check $1"; return 1; } - rootimg_fetch_checksum "$1" /tmp/rootimg.sha256 || { echo "cannot download $1.sha256"; return 1; } - rootimg_sum=$(sha256sum <"$2") || { echo "sha256sum cannot read $2"; return 1; } - # While packimage replaces the archive, the checksum file lists the new and the previous archive. - # A download that began before the replacement matches the checksum file fetched before it. - cat /tmp/rootimg.sha256 /tmp/rootimg.sha256.before >/tmp/rootimg.sums 2>/dev/null - while read -r rootimg_expected rootimg_name; do - [ "${rootimg_sum%% *}" = "$rootimg_expected" ] && return 0 - done &2; exit 1; } +inst "$moddir/xcat-rootimg-checksum.sh" "/lib/xcat-rootimg-checksum.sh" || exit 1 + +. "$moddir/xcat-rootimg-install.sh" || exit 1 +xcat_install_uftp_receiver inst for file in /etc/udev/rules.d/*;do grep -qi xcat $file && inst $file $file done diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot b/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot index c426ebcd9a..8f281b6525 100755 --- a/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot +++ b/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot @@ -38,21 +38,12 @@ rootimg_fetch_checksum() { [ "$rootimg_status" = 200 ] } -# Prints why the file $2 cannot be used, unless it matches the SHA-256 checksum published for the image at $1. -rootimg_matches_checksum() { - command -v sha256sum >/dev/null 2>&1 || { echo "this initial ramdisk has no sha256sum to check $1"; return 1; } - rootimg_fetch_checksum "$1" /tmp/rootimg.sha256 || { echo "cannot download $1.sha256 (HTTP $rootimg_status)"; return 1; } - rootimg_sum=$(sha256sum <"$2") || { echo "sha256sum cannot read $2"; return 1; } - # While packimage replaces the archive, the checksum file lists the new and the previous archive. - # A download that began before the replacement matches the checksum file fetched before it. - cat /tmp/rootimg.sha256 /tmp/rootimg.sha256.before >/tmp/rootimg.sums 2>/dev/null - while read -r rootimg_expected rootimg_name; do - [ "${rootimg_sum%% *}" = "$rootimg_expected" ] && return 0 - done &2; exit 1; } + inst_simple "$moddir/xcat-rootimg-checksum.sh" "/lib/xcat-rootimg-checksum.sh" || exit 1 + + . "$moddir/xcat-rootimg-install.sh" || exit 1 + xcat_install_uftp_receiver inst_simple # udev rules with "xcat" for file in /etc/udev/rules.d/*; do diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot index 2fb2db8aa5..c1d110a049 100755 --- a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot +++ b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot @@ -203,21 +203,12 @@ rootimg_fetch_checksum() { [ "$rootimg_status" = 200 ] } -# Prints why the file $2 cannot be used, unless it matches the SHA-256 checksum published for the image at $1. -rootimg_matches_checksum() { - command -v sha256sum >/dev/null 2>&1 || { echo "this initial ramdisk has no sha256sum to check $1"; return 1; } - rootimg_fetch_checksum "$1" /tmp/rootimg.sha256 || { echo "cannot download $1.sha256 (HTTP $rootimg_status)"; return 1; } - rootimg_sum=$(sha256sum <"$2") || { echo "sha256sum cannot read $2"; return 1; } - # While packimage replaces the archive, the checksum file lists the new and the previous archive. - # A download that began before the replacement matches the checksum file fetched before it. - cat /tmp/rootimg.sha256 /tmp/rootimg.sha256.before >/tmp/rootimg.sums 2>/dev/null - while read -r rootimg_expected rootimg_name; do - [ "${rootimg_sum%% *}" = "$rootimg_expected" ] && return 0 - done = "033") }; + my $uftp_receiver = eval { + my $wanted = $mode eq 'stateless' && $dracutver >= "033"; + imgutils::stage_rootimg_helpers($dracutmpath, $wanted); + imgutils::add_uftp_receiver($rootimg_dir, $arch, $dracutmpath, $wanted); + }; xdie($@) if $@; if ($dracutver >= "105") { diff --git a/xCAT-server/share/xcat/netboot/sles/dracut_033/install.netboot b/xCAT-server/share/xcat/netboot/sles/dracut_033/install.netboot index 940d5e1753..94ffb21f0b 100755 --- a/xCAT-server/share/xcat/netboot/sles/dracut_033/install.netboot +++ b/xCAT-server/share/xcat/netboot/sles/dracut_033/install.netboot @@ -7,13 +7,8 @@ dracut_install parted mke2fs bc mkswap swapon chmod mkfs mkfs.ext4 mkfs.xfs xfs_ inst "$moddir/xcat-updateflag" "/tmp/updateflag" inst "$moddir/xcatroot" "/sbin/xcatroot" inst_hook cmdline 10 "$moddir/xcat-cmdline.sh" -# genimage adds the UFTP helper only when the image has a UFTP 5 receiver. -if [ -r "$moddir/xcat-rootimg-fetch.sh" ]; then - inst "$moddir/xcat-rootimg-fetch.sh" "/lib/xcat-rootimg-fetch.sh" - for uftpd in /usr/sbin/uftpd /usr/bin/uftpd; do - if [ -x "$uftpd" ]; then - dracut_install "$uftpd" - break - fi - done -fi +[ -r "$moddir/xcat-rootimg-checksum.sh" ] || { echo "Cannot read $moddir/xcat-rootimg-checksum.sh" >&2; exit 1; } +inst "$moddir/xcat-rootimg-checksum.sh" "/lib/xcat-rootimg-checksum.sh" || exit 1 + +. "$moddir/xcat-rootimg-install.sh" || exit 1 +xcat_install_uftp_receiver inst diff --git a/xCAT-server/share/xcat/netboot/sles/dracut_033/xcatroot b/xCAT-server/share/xcat/netboot/sles/dracut_033/xcatroot index 3d5f5cd848..dd31f14e9d 100755 --- a/xCAT-server/share/xcat/netboot/sles/dracut_033/xcatroot +++ b/xCAT-server/share/xcat/netboot/sles/dracut_033/xcatroot @@ -42,21 +42,12 @@ rootimg_fetch_checksum() { wget -q -O "$2" "$1.sha256" } -# Prints why the file $2 cannot be used, unless it matches the SHA-256 checksum published for the image at $1. -rootimg_matches_checksum() { - command -v sha256sum >/dev/null 2>&1 || { echo "this initial ramdisk has no sha256sum to check $1"; return 1; } - rootimg_fetch_checksum "$1" /tmp/rootimg.sha256 || { echo "cannot download $1.sha256"; return 1; } - rootimg_sum=$(sha256sum <"$2") || { echo "sha256sum cannot read $2"; return 1; } - # While packimage replaces the archive, the checksum file lists the new and the previous archive. - # A download that began before the replacement matches the checksum file fetched before it. - cat /tmp/rootimg.sha256 /tmp/rootimg.sha256.before >/tmp/rootimg.sums 2>/dev/null - while read -r rootimg_expected rootimg_name; do - [ "${rootimg_sum%% *}" = "$rootimg_expected" ] && return 0 - done &2; exit 1; } +inst "$moddir/xcat-rootimg-checksum.sh" "/lib/xcat-rootimg-checksum.sh" || exit 1 + +. "$moddir/xcat-rootimg-install.sh" || exit 1 +xcat_install_uftp_receiver inst # install netroot dispatcher if available (dracut 051+ with network-legacy) if [ -f "$moddir/../40network/netroot.sh" ]; then inst "$moddir/../40network/netroot.sh" "/sbin/netroot" diff --git a/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot b/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot index 1194f87786..37cde5c03b 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot +++ b/xCAT-server/share/xcat/netboot/ubuntu/dracut/xcatroot @@ -87,21 +87,12 @@ rootimg_fetch_checksum() { wget -q -O "$2" "$1.sha256" } -# Prints why the file $2 cannot be used, unless it matches the SHA-256 checksum published for the image at $1. -rootimg_matches_checksum() { - command -v sha256sum >/dev/null 2>&1 || { echo "this initial ramdisk has no sha256sum to check $1"; return 1; } - rootimg_fetch_checksum "$1" /tmp/rootimg.sha256 || { echo "cannot download $1.sha256"; return 1; } - rootimg_sum=$(sha256sum <"$2") || { echo "sha256sum cannot read $2"; return 1; } - # While packimage replaces the archive, the checksum file lists the new and the previous archive. - # A download that began before the replacement matches the checksum file fetched before it. - cat /tmp/rootimg.sha256 /tmp/rootimg.sha256.before >/tmp/rootimg.sums 2>/dev/null - while read -r rootimg_expected rootimg_name; do - [ "${rootimg_sum%% *}" = "$rootimg_expected" ] && return 0 - done "$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" +} + +receiver() +{ + printf '#!/bin/sh\n# %s receiver\nexit 0\n' "$1" >"$fixture/$1/uftpd" + chmod 755 "$fixture/$1/uftpd" +} + +add_fetch_helper() +{ + cp "$(repo_path xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-fetch.sh)" "$fixture/module/" +} + +@test 'all stateless hooks deliver the checksum helper without UFTP' { + receiver bin + receiver sbin + 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/lib/xcat-rootimg-install.sh" ] + [ ! -e "$fixture/out/usr/sbin/uftpd" ] + [ ! -e "$fixture/out/usr/bin/uftpd" ] + done +} + +@test 'all stateless hooks prefer the sbin receiver when both locations are executable' { + receiver bin + receiver sbin + for variant in $VARIANTS; do + stage_module "$variant" + add_fetch_helper + install_module + [ "$status" -eq 0 ] || { echo "$variant: $output"; return 1; } + cmp "$fixture/module/xcat-rootimg-fetch.sh" "$fixture/out/lib/xcat-rootimg-fetch.sh" + cmp "$fixture/sbin/uftpd" "$fixture/out/usr/sbin/uftpd" + [ ! -e "$fixture/out/usr/bin/uftpd" ] + done +} + +@test 'all stateless hooks install the bin receiver when the sbin receiver is absent or not executable' { + receiver bin + for variant in $VARIANTS; do + for sbin in absent nonexecutable; do + rm -f "$fixture/sbin/uftpd" + if [ "$sbin" = nonexecutable ]; then + receiver sbin + chmod 644 "$fixture/sbin/uftpd" + fi + stage_module "$variant" + add_fetch_helper + install_module + [ "$status" -eq 0 ] || { echo "$variant: $output"; return 1; } + cmp "$fixture/bin/uftpd" "$fixture/out/usr/bin/uftpd" + [ ! -e "$fixture/out/usr/sbin/uftpd" ] + done + done +} + +@test 'all stateless hooks retain the fetch helper when neither receiver location is executable' { + for variant in $VARIANTS; do + stage_module "$variant" + add_fetch_helper + install_module + [ "$status" -eq 0 ] || { echo "$variant: $output"; return 1; } + cmp "$fixture/module/xcat-rootimg-fetch.sh" "$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 installation policy helper is missing' { + for variant in $VARIANTS; do + stage_module "$variant" + rm "$fixture/module/xcat-rootimg-install.sh" + install_module + [ "$status" -ne 0 ] || { echo "$variant accepted a missing installation helper"; return 1; } + [[ "$output" == *xcat-rootimg-install.sh* ]] + 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/helpers/dracut_module.bash b/xCAT-test/bats/helpers/dracut_module.bash new file mode 100644 index 0000000000..64b683e0d9 --- /dev/null +++ b/xCAT-test/bats/helpers/dracut_module.bash @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +load "${BASH_SOURCE[0]%/*}/shell_source.bash" + +stage_dracut_module() +{ + local variant=$1 destination=$2 helper source + mkdir -p "$destination" + cp -a "$(repo_path "xCAT-server/share/xcat/netboot/$variant")/." "$destination/" + for helper in xcat-rootimg-checksum.sh xcat-rootimg-install.sh; do + source=$(repo_path "xCAT-server/share/xcat/netboot/imgutils/$helper") + [ ! -f "$source" ] || cp "$source" "$destination/" + done +} diff --git a/xCAT-test/bats/ubuntu_netboot_initqueue.bats b/xCAT-test/bats/ubuntu_netboot_initqueue.bats index 785838c6c5..92588b7022 100644 --- a/xCAT-test/bats/ubuntu_netboot_initqueue.bats +++ b/xCAT-test/bats/ubuntu_netboot_initqueue.bats @@ -5,12 +5,12 @@ # - the boot NIC gets a DHCPv4 lease, and # - the initqueue has a finished condition, or it ends before it runs the settled hooks. -load 'helpers/shell_source' +load 'helpers/dracut_module' setup() { - INSTALL="$(require_repo_file 'xCAT-server/share/xcat/netboot/ubuntu/dracut/install.netboot')" - MOD="$(dirname "$INSTALL")" + MOD="${BATS_TEST_TMPDIR}/module" + stage_dracut_module ubuntu/dracut "$MOD" LOG="${BATS_TEST_TMPDIR}/installed" : >"$LOG" HOOKS="${BATS_TEST_TMPDIR}/hooks" diff --git a/xCAT-test/bats/ubuntu_netboot_root_handler.bats b/xCAT-test/bats/ubuntu_netboot_root_handler.bats index a542636a40..84dfd7b8cf 100644 --- a/xCAT-test/bats/ubuntu_netboot_root_handler.bats +++ b/xCAT-test/bats/ubuntu_netboot_root_handler.bats @@ -5,12 +5,12 @@ # On 26.04 the initramfs then fetches no root: measured on xcat32-cn, zero DHCP from the initrd, # zero httpd requests for rootimg.cpio.gz, and dracut in emergency mode 3.3 seconds in. -load 'helpers/shell_source' +load 'helpers/dracut_module' setup() { - INSTALL="$(require_repo_file 'xCAT-server/share/xcat/netboot/ubuntu/dracut/install.netboot')" - MOD="$(dirname "$INSTALL")" + MOD="${BATS_TEST_TMPDIR}/module" + stage_dracut_module ubuntu/dracut "$MOD" LOG="${BATS_TEST_TMPDIR}/installed" : >"$LOG" } diff --git a/xCAT-test/bats/xcatroot_rootimg_checksum.bats b/xCAT-test/bats/xcatroot_rootimg_checksum.bats index 4bd76f1beb..57545f0f33 100644 --- a/xCAT-test/bats/xcatroot_rootimg_checksum.bats +++ b/xCAT-test/bats/xcatroot_rootimg_checksum.bats @@ -162,16 +162,27 @@ run_xcatroot() local script="$1" shell="$2" shift 2 local sandbox=("$bwrap" --unshare-all --die-with-parent --new-session - --ro-bind /usr /usr --ro-bind /etc /etc --symlink usr/bin /bin --symlink usr/sbin /sbin + --ro-bind /usr /usr --ro-bind /etc /etc --dir /lib --proc /proc --dev /dev --tmpfs /tmp --dir /sysroot --bind "$FX" /fx --ro-bind "$FX/lib/dracut-lib.sh" /lib/dracut-lib.sh --ro-bind "$(repo_path "$script")" /xcatroot --chdir / --setenv PATH /fx/bin:/usr/bin:/bin --setenv imgurl "$URL" --setenv XCAT 192.0.2.1:3001 --setenv rootimgchecksum "${CMDLINE#rootimgchecksum=}" --setenv XCAT_UFTP_POLL 60 "$@") - [ ! -e /usr/lib64 ] || sandbox+=(--symlink usr/lib64 /lib64) + local directory + for directory in bin sbin; do + if [ -L "/$directory" ]; then + sandbox+=(--symlink "usr/$directory" "/$directory") + else + sandbox+=(--ro-bind "/$directory" "/$directory") + fi + done + [ ! -e /lib64 ] || sandbox+=(--ro-bind /lib64 /lib64) for multiarch in /usr/lib/*-linux-gnu; do [ ! -d "$multiarch" ] || sandbox+=(--ro-bind "$multiarch" "/lib/${multiarch##*/}") done + local checksum_helper + checksum_helper=$(repo_path xCAT-server/share/xcat/netboot/imgutils/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" @@ -182,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 @@ -207,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..fe3a83809c --- /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 xcat-rootimg-install.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/native/openeuler_xcatroot.t b/xCAT-test/native/openeuler_xcatroot.t index 478365134a..923599d6fb 100644 --- a/xCAT-test/native/openeuler_xcatroot.t +++ b/xCAT-test/native/openeuler_xcatroot.t @@ -74,6 +74,9 @@ getarg() } SH copy($source, "$root/xcatroot") or die $!; + my $checksum_helper = "$netboot/../imgutils/xcat-rootimg-checksum.sh"; + copy($checksum_helper, "$root/lib/xcat-rootimg-checksum.sh") or die $! + if -f $checksum_helper; my $archive = "$root/rootimg.$method.gz"; my @pack = $method eq 'cpio' ? ('/bin/bash', '-o', 'pipefail', '-c', 'cd "$1" && find . -print0 | cpio -0 -o -H newc | gzip > "$2"', 'pack', $payload, $archive) diff --git a/xCAT-test/unit/imgutils_rootimg_helpers.t b/xCAT-test/unit/imgutils_rootimg_helpers.t new file mode 100644 index 0000000000..6e273ac147 --- /dev/null +++ b/xCAT-test/unit/imgutils_rootimg_helpers.t @@ -0,0 +1,53 @@ +#!/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'); +my $installer = "$module/xcat-rootimg-install.sh"; +is(scalar(read_binary($installer)), + scalar(read_binary("$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-install.sh")), + 'the build helper is staged independently of UFTP eligibility'); +is((stat($installer))[2] & oct('7777'), oct('644'), 'the build 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'); +ok(!-e $installer, 'a disabled build removes the previous installation 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();