2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-12-04 08:07:04 +00:00

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.
This commit is contained in:
Vinícius Ferrão
2026-11-21 15:27:10 -03:00
parent a31b7be904
commit 7d0e4e6dab
15 changed files with 82 additions and 68 deletions
@@ -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) = @_;
@@ -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 </tmp/rootimg.sums
echo "$1 does not match $1.sha256"
return 1
}
@@ -9,6 +9,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
@@ -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 </tmp/rootimg.sums
echo "$1 does not match $1.sha256"
return 1
rootimg_checksum_error() {
echo "cannot download $1.sha256"
}
. /lib/xcat-rootimg-checksum.sh || exit 1
# Downloads the image at $1 to the file $2, over UFTP the first time when nodeset asks for it.
rootimg_download() {
if [ -n "$rootimg_uftp" ]; then
@@ -9,6 +9,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
@@ -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 </tmp/rootimg.sums
echo "$1 does not match $1.sha256"
return 1
rootimg_checksum_error() {
echo "cannot download $1.sha256 (HTTP $rootimg_status)"
}
. /lib/xcat-rootimg-checksum.sh || exit 1
# Downloads the image at $1 to the file $2, over UFTP the first time when nodeset asks for it.
rootimg_download() {
if [ -n "$rootimg_uftp" ]; then
@@ -29,6 +29,9 @@ install() {
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_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
@@ -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 </tmp/rootimg.sums
echo "$1 does not match $1.sha256"
return 1
rootimg_checksum_error() {
echo "cannot download $1.sha256 (HTTP $rootimg_status)"
}
. /lib/xcat-rootimg-checksum.sh || exit 1
# Downloads the image at $1 to the file $2, over UFTP the first time when nodeset asks for it.
rootimg_download() {
if [ -n "$rootimg_uftp" ]; then
+5 -1
View File
@@ -1074,7 +1074,11 @@ sub mkinitrd_dracut {
mkpath($dracutmpath);
# Only a stateless initial ramdisk that checks the root image receives it over UFTP.
my $uftp_receiver = eval { imgutils::add_uftp_receiver($rootimg_dir, $arch, $dracutmpath, $mode eq 'stateless' && $dracutver >= "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") {
@@ -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
@@ -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 </tmp/rootimg.sums
echo "$1 does not match $1.sha256"
return 1
rootimg_checksum_error() {
echo "cannot download $1.sha256"
}
. /lib/xcat-rootimg-checksum.sh || exit 1
# Downloads the image at $1 to the file $2, over UFTP the first time when nodeset asks for it.
rootimg_download() {
if [ -n "$rootimg_uftp" ]; then
+6 -1
View File
@@ -1058,7 +1058,12 @@ sub mkinitrd_dracut {
mkpath($dracutmpath);
# Only a stateless initial ramdisk that checks the root image receives it over UFTP.
my $uftp_receiver = eval { imgutils::add_uftp_receiver($rootimg_dir, $arch, $dracutmpath, $mode eq 'stateless' && $dracutdir eq 'dracut_033') };
my $uftp_receiver = eval {
my $wanted = $mode eq 'stateless' && $dracutdir eq 'dracut_033';
# packimage restores the stateless hook after the statelite build.
imgutils::stage_rootimg_helpers($dracutmpath, $dracutdir eq 'dracut_033');
imgutils::add_uftp_receiver($rootimg_dir, $arch, $dracutmpath, $wanted);
};
xdie($@) if $@;
my $perm = (stat("$fullpath/$dracutdir/check"))[2];
@@ -6,6 +6,9 @@ dracut_install mount.nfs
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
@@ -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 </tmp/rootimg.sums
echo "$1 does not match $1.sha256"
return 1
rootimg_checksum_error() {
echo "cannot download $1.sha256"
}
. /lib/xcat-rootimg-checksum.sh || exit 1
# Downloads the image at $1 to the file $2, over UFTP the first time when nodeset asks for it.
rootimg_download() {
if [ -n "$rootimg_uftp" ]; then
@@ -991,7 +991,11 @@ sub mkinitrd_dracut {
mkpath($dracutmpath);
# Only a stateless initial ramdisk that checks the root image receives it over UFTP.
my $uftp_receiver = eval { imgutils::add_uftp_receiver($rootimg_dir, $arch, $dracutmpath, $mode eq 'stateless') };
my $uftp_receiver = eval {
my $wanted = $mode eq 'stateless';
imgutils::stage_rootimg_helpers($dracutmpath, $wanted);
imgutils::add_uftp_receiver($rootimg_dir, $arch, $dracutmpath, $wanted);
};
xdie($@) if $@;
imgutils::stage_dracut_module("$fullpath/$dracutdir", $dracutmpath, $mode);