From 7d0e4e6dab79bf5cb39da33a50cad9912b71a85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:27:10 -0300 Subject: [PATCH] refactor(netboot): share root image checksum matching Keep wget and curl download handling in each dracut consumer. Stage the checksum helper independently of the optional UFTP receiver and require it when building the initial ramdisk. --- .../share/xcat/netboot/imgutils/imgutils.pm | 15 +++++++++++++++ .../netboot/imgutils/xcat-rootimg-checksum.sh | 16 ++++++++++++++++ .../xcat/netboot/rh/dracut_033/install.netboot | 3 +++ .../share/xcat/netboot/rh/dracut_033/xcatroot | 17 ++++------------- .../xcat/netboot/rh/dracut_047/install.netboot | 3 +++ .../share/xcat/netboot/rh/dracut_047/xcatroot | 17 ++++------------- .../rh/dracut_105/stateless/module-setup.sh | 3 +++ .../netboot/rh/dracut_105/stateless/xcatroot | 17 ++++------------- xCAT-server/share/xcat/netboot/rh/genimage | 6 +++++- .../netboot/sles/dracut_033/install.netboot | 3 +++ .../share/xcat/netboot/sles/dracut_033/xcatroot | 17 ++++------------- xCAT-server/share/xcat/netboot/sles/genimage | 7 ++++++- .../xcat/netboot/ubuntu/dracut/install.netboot | 3 +++ .../share/xcat/netboot/ubuntu/dracut/xcatroot | 17 ++++------------- xCAT-server/share/xcat/netboot/ubuntu/genimage | 6 +++++- 15 files changed, 82 insertions(+), 68 deletions(-) create mode 100644 xCAT-server/share/xcat/netboot/imgutils/xcat-rootimg-checksum.sh diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index bcbfea0994..c1748f1d52 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)) { + 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 + 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 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 + 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 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 + if [ -r "$moddir/xcat-rootimg-fetch.sh" ]; then inst_simple "$moddir/xcat-rootimg-fetch.sh" "/lib/xcat-rootimg-fetch.sh" for uftpd in /usr/sbin/uftpd /usr/bin/uftpd; 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..4afdbe969b 100755 --- a/xCAT-server/share/xcat/netboot/sles/dracut_033/install.netboot +++ b/xCAT-server/share/xcat/netboot/sles/dracut_033/install.netboot @@ -8,6 +8,9 @@ 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. +[ -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 + 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 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 + 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 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