Skip to content

Commit 6bf1d9a

Browse files
authored
Merge pull request #7564 from VersatusHPC/fix/rh-genimage-dnf
fix: use dnf for EL8+ RH genimage installroot
2 parents fa17f70 + 57daff8 commit 6bf1d9a

2 files changed

Lines changed: 68 additions & 13 deletions

File tree

xCAT-server/share/xcat/netboot/rh/genimage

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ sub majversion {
9292
return $majorrel;
9393
}
9494

95+
sub el_major_version {
96+
my $version = shift;
97+
98+
if (defined($version)
99+
&& $version =~ /^(?:rhels?|centos|rocky|alma(?:linux)?|ol)\D*(\d+)/)
100+
{
101+
return $1;
102+
}
103+
104+
return;
105+
}
106+
107+
sub rpm_installroot_command {
108+
my $non_interactive = shift || "";
109+
my $majorrel = el_major_version($osver);
110+
my $pkgmgr = "yum";
111+
112+
# EL8 and newer are dnf-native. Keep yum as the fallback for legacy
113+
# systems and minimal environments that still provide only yum.
114+
if (defined($majorrel) && $majorrel > 7 && -x "/usr/bin/dnf") {
115+
$pkgmgr = "dnf";
116+
}
117+
118+
my $cmd = "$pkgmgr $non_interactive -c /tmp/genimage.$$.yum.conf --installroot=$rootimg_dir/ --disablerepo=* ";
119+
if (defined($majorrel) && $majorrel > 7) {
120+
$cmd .= "--releasever=" . $majorrel . " ";
121+
$cmd .= "--setopt=module_platform_id=platform:el" . $majorrel . " ";
122+
}
123+
124+
return $cmd;
125+
}
126+
95127
sub mount_chroot {
96128
my $rootimage_dir = shift;
97129

@@ -374,15 +406,7 @@ if($onlyinitrd){
374406
my $non_interactive;
375407
if (!$prompt) { $non_interactive = "-y"; }
376408

377-
my $yumcmd = "yum $non_interactive -c /tmp/genimage.$$.yum.conf --installroot=$rootimg_dir/ --disablerepo=* ";
378-
379-
if ($osver =~ /^(?:rhel|rocky|alma|ol)\D*(\d*)[.\d]*.*$/) {
380-
$majorrel = $1;
381-
if ($majorrel > 7) {
382-
$yumcmd .= "--releasever=" . $majorrel . " ";
383-
$yumcmd .= "--setopt=module_platform_id=platform:el" . $majorrel . " ";
384-
}
385-
}
409+
my $yumcmd = rpm_installroot_command($non_interactive);
386410

387411
foreach (0 .. $repnum) {
388412
$yumcmd .= "--enablerepo=$osver-$arch-$_ ";
@@ -479,7 +503,7 @@ if($onlyinitrd){
479503
print "$envlist $yumcmd install $pkgnames\n";
480504
my $rc = system("$envlist $yumcmd install $pkgnames");
481505
if ($rc) {
482-
print "yum invocation failed\n";
506+
print "RPM package manager invocation failed\n";
483507
umount_chroot($rootimg_dir);
484508
exit 1;
485509
}
@@ -548,7 +572,7 @@ if($onlyinitrd){
548572
}
549573
close($yumconfig);
550574
$index--;
551-
my $yumcmd_base = "yum $non_interactive -c /tmp/genimage.$$.yum.conf --installroot=$rootimg_dir/ --disablerepo=* ";
575+
my $yumcmd_base = rpm_installroot_command($non_interactive);
552576

553577
#yum/rpm/zypper has defect on calculating diskspace usage when installing rpm on a NFS mounted installroot
554578
if (isNFSdir("$rootimg_dir")) {
@@ -609,7 +633,7 @@ if($onlyinitrd){
609633
print "$envlist $yumcmd\n";
610634
my $rc = system("$envlist $yumcmd");
611635
if ($rc) {
612-
print "yum invocation failed\n";
636+
print "RPM package manager invocation failed\n";
613637

614638
# remove the hacked uname file
615639
unuse_hackuname();
@@ -2588,4 +2612,3 @@ sub usage {
25882612

25892613
return 0;
25902614
}
2591-
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env perl
2+
use strict;
3+
use warnings;
4+
5+
use FindBin;
6+
use File::Spec;
7+
use Test::More;
8+
9+
my $repo_root = File::Spec->catdir( $FindBin::Bin, '..', '..' );
10+
11+
sub read_file {
12+
my ($file) = @_;
13+
my $path = File::Spec->catfile( $repo_root, $file );
14+
15+
open( my $fh, '<', $path ) or die "Unable to read $path: $!";
16+
my $contents = do { local $/; <$fh> };
17+
close($fh);
18+
19+
return $contents;
20+
}
21+
22+
my $genimage = read_file('xCAT-server/share/xcat/netboot/rh/genimage');
23+
24+
like( $genimage, qr/sub el_major_version/, 'RH genimage has an EL major-version helper' );
25+
like( $genimage, qr/sub rpm_installroot_command/, 'RH genimage builds RPM installroot commands through one helper' );
26+
like( $genimage, qr/-x "\/usr\/bin\/dnf".*?\$pkgmgr = "dnf"/s, 'EL8+ genimage prefers dnf when it is available' );
27+
like( $genimage, qr/--releasever=.*?--setopt=module_platform_id=platform:el/s, 'EL8+ installroot commands keep releasever and module platform options' );
28+
like( $genimage, qr/my \$yumcmd = rpm_installroot_command\(\$non_interactive\);/, 'base package pass uses shared installroot command builder' );
29+
like( $genimage, qr/my \$yumcmd_base = rpm_installroot_command\(\$non_interactive\);/, 'otherpkgs pass uses shared installroot command builder' );
30+
unlike( $genimage, qr/my \$yumcmd(?:_base)? = "yum /, 'RH genimage no longer hardcodes yum in installroot command builders' );
31+
32+
done_testing();

0 commit comments

Comments
 (0)