diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 048ec29115..9a2bf36590 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -284,6 +284,33 @@ sub disable_auto_profiles { return; } +# The netboot dracut.conf names an exclusive module list, so a module nothing in the list depends +# on is left out of the initial ramdisk. dracut reads the list and nothing else. +# +# dracut-ng removed the network-legacy module. Without it 40network selects systemd-networkd, which +# depends on the systemd module, and the base module installs the shell /init only when systemd is +# out. systemd as /init needs initrd.target, which the systemd-initrd module installs, and the +# dracut hooks need the units of the dracut-systemd module. The EL genimage names the same three. +sub dracut_netboot_modules { + my ($moduledir, $wicked) = @_; + my $legacy = -d "$moduledir/35network-legacy" ? 1 : 0; + my $netmod = ($legacy && !$wicked) ? 'network-legacy' : 'network'; + my @modules = ('xcat', 'nfs', 'base', $netmod, 'kernel-modules'); + return join(' ', @modules) if $legacy; + my %present; + if (opendir(my $dh, $moduledir)) { + + # A dracut module directory carries a number that orders it, and the number moves between + # dracut releases. The name after the number is what dracut.conf names. + foreach my $entry (readdir($dh)) { + $present{$1} = 1 if $entry =~ /^[0-9]+(.+)$/ && -d "$moduledir/$entry"; + } + closedir($dh); + } + push @modules, grep { $present{$_} } qw(systemd systemd-initrd dracut-systemd); + return join(' ', @modules); +} + # debootstrap deletes $rootimg_dir/debootstrap when it finishes, and its tar -k fails over a finished root. sub debootstrap_finished { my ($rootimg_dir) = @_; diff --git a/xCAT-server/share/xcat/netboot/ubuntu/genimage b/xCAT-server/share/xcat/netboot/ubuntu/genimage index 91be7e7282..cd8a306549 100755 --- a/xCAT-server/share/xcat/netboot/ubuntu/genimage +++ b/xCAT-server/share/xcat/netboot/ubuntu/genimage @@ -1019,13 +1019,10 @@ sub mkinitrd_dracut { $perm = (stat("$fullpath/$dracutdir/xcat-prepivot.sh"))[2]; chmod($perm & 07777, "$dracutmpath/xcat-prepivot.sh"); - # update etc/dracut.conf - use network-legacy if 40network requires wicked - my $netmod = "network"; - if (-d "$dracutmpath/../35network-legacy" && ! -x "$rootimg_dir/usr/sbin/wicked") { - $netmod = "network-legacy"; - } + # update etc/dracut.conf + my $dracutmodulelist = imgutils::dracut_netboot_modules(dirname($dracutmpath), -x "$rootimg_dir/usr/sbin/wicked"); open($DRACUTCONF, '>', "$rootimg_dir/etc/dracut.conf"); - print $DRACUTCONF qq{dracutmodules+=" xcat nfs base $netmod kernel-modules "\n}; + print $DRACUTCONF qq{dracutmodules+=" $dracutmodulelist "\n}; print $DRACUTCONF qq{add_drivers+=" $add_drivers "\n}; print $DRACUTCONF qq{filesystems+=" nfs "\n}; close $DRACUTCONF; @@ -1053,13 +1050,10 @@ sub mkinitrd_dracut { $perm = (stat("$fullpath/$dracutdir/installkernel"))[2]; chmod($perm & 07777, "$dracutmpath/installkernel"); - # update etc/dracut.conf - use network-legacy if 40network requires wicked - my $netmod = "network"; - if (-d "$dracutmpath/../35network-legacy" && ! -x "$rootimg_dir/usr/sbin/wicked") { - $netmod = "network-legacy"; - } + # update etc/dracut.conf + my $dracutmodulelist = imgutils::dracut_netboot_modules(dirname($dracutmpath), -x "$rootimg_dir/usr/sbin/wicked"); open($DRACUTCONF, '>', "$rootimg_dir/etc/dracut.conf"); - print $DRACUTCONF qq{dracutmodules+=" xcat nfs base $netmod kernel-modules "\n}; + print $DRACUTCONF qq{dracutmodules+=" $dracutmodulelist "\n}; print $DRACUTCONF qq{add_drivers+=" $add_drivers "\n}; close $DRACUTCONF; } else { diff --git a/xCAT-test/unit/imgutils_dracut_netboot_modules.t b/xCAT-test/unit/imgutils_dracut_netboot_modules.t new file mode 100644 index 0000000000..4c102c5474 --- /dev/null +++ b/xCAT-test/unit/imgutils_dracut_netboot_modules.t @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Path qw(make_path); +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; + +# Builds a modules.d that holds the named dracut modules and nothing else. +sub modules_dir { + my (@modules) = @_; + my $dir = tempdir(CLEANUP => 1); + make_path("$dir/$_") for @modules; + return $dir; +} + +# dracut 060 on noble. 40network takes network-legacy, so dracut writes the shell /init. +{ + my $dir = modules_dir(qw(35network-legacy 10systemd 11systemd-initrd 77dracut-systemd 97xcat)); + is(imgutils::dracut_netboot_modules($dir, 0), 'xcat nfs base network-legacy kernel-modules', + 'a dracut that holds network-legacy keeps the module list it had'); +} + +# dracut 110 on resolute. network-legacy is gone, so 40network takes systemd-networkd and dracut +# writes systemd as /init. systemd needs initrd.target, which the systemd-initrd module installs. +{ + my $dir = modules_dir(qw(10systemd 11systemd-initrd 77dracut-systemd 40network 74nfs 97xcat)); + is(imgutils::dracut_netboot_modules($dir, 0), + 'xcat nfs base network kernel-modules systemd systemd-initrd dracut-systemd', + 'a dracut without network-legacy gets the systemd modules its initial ramdisk needs'); +} + +# The module names carry no version prefix, so a number the next dracut picks must not matter. +{ + my $dir = modules_dir(qw(00systemd 01systemd-initrd 98dracut-systemd 97xcat)); + is(imgutils::dracut_netboot_modules($dir, 0), + 'xcat nfs base network kernel-modules systemd systemd-initrd dracut-systemd', + 'the systemd modules are found under the numbers an older dracut uses'); +} + +# A module that is not in the image cannot be put in the list. +{ + my $dir = modules_dir(qw(10systemd 97xcat)); + is(imgutils::dracut_netboot_modules($dir, 0), 'xcat nfs base network kernel-modules systemd', + 'only the systemd modules the image holds are named'); +} + +# 35network-legacy requires wicked, which a Debian image does not have. +{ + my $dir = modules_dir(qw(35network-legacy 10systemd 11systemd-initrd 77dracut-systemd 97xcat)); + is(imgutils::dracut_netboot_modules($dir, 1), 'xcat nfs base network kernel-modules', + 'an image with wicked keeps the network module and the list it had'); +} + +# genimage calls this before dracut runs, and the path comes from the image. +{ + my $dir = tempdir(CLEANUP => 1); + is(imgutils::dracut_netboot_modules("$dir/absent", 0), 'xcat nfs base network kernel-modules', + 'a modules directory that does not exist returns the plain list'); +} + +done_testing();