2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-21 09:52:17 +00:00
Files
xcat-core/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm
T
Daniel Hilst 21b24f1e7d fix(xcat-core): use_host_resolver dies on an undefined subroutine move
imgutils.pm imports File::Copy with an empty list, so the module exports no
function and a bare move() is not defined. use_host_resolver calls move()
unqualified, so it dies with "Undefined subroutine &imgutils::move called" the
first time genimage reaches it, before it has moved either file aside.

Every other call site in the file already qualifies the name:
File::Copy::cp at line 139, File::Copy::copy at 208 and at 302, the last of
them two lines below the defect. The call is now qualified the same way.

xCAT-test/unit/imgutils_host_resolver.t and imgutils_reused_root.t both die at
load on the unfixed tree and pass with the fix, 34 tests.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
2026-11-06 17:34:05 -03:00

709 lines
27 KiB
Perl

#!/usr/bin/perl -w
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
# #(C)IBM Corp
package imgutils;
use strict;
use warnings "all";
use File::Basename;
use File::Copy ();
use File::Find;
use File::Path;
use Cwd qw(realpath);
use lib dirname(__FILE__);
use xCAT::ProfileUtils;
use xCAT::SvrUtils;
sub el_major_version {
my $version = shift;
return if !defined($version);
my ($os_family, $os_major) = xCAT::SvrUtils::parseosver($version);
return
if !defined($os_family)
|| $os_family !~ /^(?:rhels?|rhelc|rhelhpc|centos(?:-stream)?|rocky|alma(?:linux)?|ol)$/;
return $os_major if length($os_major);
return;
}
sub rpm_installroot_command {
my ( $osver, $rootimg_dir, $non_interactive, $dnf_available ) = @_;
$non_interactive ||= "";
$dnf_available = -x "/usr/bin/dnf" unless defined($dnf_available);
my $majorrel = el_major_version($osver);
my $openeuler_release = openeuler_release_version($osver);
die "Unsupported openEuler image release: $osver\n"
if defined($osver) && $osver =~ /^openeuler/ && !defined($openeuler_release);
my $pkgmgr = "yum";
# EL8 and newer are dnf-native. Keep yum as the fallback for legacy
# systems and minimal environments that still provide only yum.
if (defined($majorrel) && $majorrel > 7 && $dnf_available) {
$pkgmgr = "dnf";
}
if (defined($openeuler_release)) {
die "openEuler image creation requires dnf\n" unless $dnf_available;
$pkgmgr = "dnf";
}
my $cmd = "$pkgmgr $non_interactive -c /tmp/genimage.$$.yum.conf --installroot=$rootimg_dir/ --disablerepo=* ";
if (defined($majorrel) && $majorrel > 7) {
$cmd .= "--releasever=" . $majorrel . " ";
$cmd .= "--setopt=module_platform_id=platform:el" . $majorrel . " ";
}
if (defined($openeuler_release)) {
$cmd .= "--releasever=$openeuler_release --setopt=strict=1 '--setopt=*.skip_if_unavailable=False' '--setopt=*.gpgcheck=1' ";
}
return $cmd;
}
sub openeuler_release_version {
my $osver = shift;
return undef unless defined($osver) && $osver =~ /^openeuler(.+)$/;
my $release = xCAT::Utils::normalize_openeuler_version($1);
return undef unless defined($release);
$release =~ s/sp([0-9]+)$/LTS_SP$1/ or $release .= 'LTS';
return $release;
}
sub rpm_trusted_keys {
open(my $rpm, '-|', 'rpm', '-qa', '--qf', "%{DESCRIPTION}\n", 'gpg-pubkey*')
or die "Cannot read trusted RPM keys: $!\n";
local $/;
my $keys = <$rpm>;
close($rpm) or die "Cannot read trusted RPM keys\n";
die "No trusted RPM signing keys are installed\n" unless defined($keys) && length($keys);
return $keys;
}
sub rpm_repository_config {
my ($osver, $id, $url, $gpgkey) = @_;
my $config = "[$id]\nname=$id\nbaseurl=$url\n";
if (defined(openeuler_release_version($osver))) {
die "openEuler repositories require trusted RPM signing keys\n"
unless defined($gpgkey) && length($gpgkey);
$config .= "gpgcheck=1\ngpgkey=$gpgkey\nskip_if_unavailable=False\n\n";
} elsif (defined($gpgkey) && length($gpgkey)) {
# Other RPM images verify signatures only when the caller opts in
# by supplying the trusted keys (linuximage.gpgcheck).
$config .= "gpgcheck=1\ngpgkey=$gpgkey\nskip_if_unavailable=True\n\n";
} else {
$config .= "gpgcheck=0\nskip_if_unavailable=True\n\n";
}
return $config;
}
sub otherpkgs_repository_urls {
my ($subdir, $local_dir, @urls) = @_;
my @repos = @urls;
# Without a local otherpkgdir there is no local repository to add; a
# file:///<subdir> entry would only produce a metadata download failure.
push @repos, "file://$local_dir/$subdir" if defined($local_dir) && length($local_dir);
return @repos;
}
sub disable_vendor_repositories {
my ($rootimg_dir) = @_;
my @internet_repo_file_list = ("oracle-linux-ol8.repo", "oracle-linux-ol9.repo", "oracle-linux-ol10.repo", "uek-ol8.repo", "uek-ol9.repo", "uek-ol10.repo", "Rocky-AppStream.repo", "Rocky-BaseOS.repo", "Rocky-Extras.repo", "rocky.repo", "rocky-extras.repo", "CentOS-Base.repo", "centos.repo", "centos-addons.repo", "almalinux-ha.repo", "almalinux-nfv.repo", "almalinux-plus.repo", "almalinux-powertools.repo", "almalinux.repo", "almalinux-resilientstorage.repo", "almalinux-rt.repo", "openEuler.repo");
foreach (@internet_repo_file_list) {
if (-e "$rootimg_dir/etc/yum.repos.d/$_") {
system("sed -i -e 's/^[[:space:]]*enabled[[:space:]]*=[[:space:]]*1/enabled=0/' $rootimg_dir/etc/yum.repos.d/$_");
}
}
}
# kdump.service fails without dump memory, which a stateless command line
# reserves only with crashkernel= or, on POWER, fadump=.
sub skip_kdump_without_dump_memory {
my ($rootimg_dir) = @_;
return unless -e "$rootimg_dir/usr/lib/systemd/system/kdump.service";
my $dropin_dir = "$rootimg_dir/etc/systemd/system/kdump.service.d";
mkpath($dropin_dir);
open(my $fh, '>', "$dropin_dir/xcat-dump-memory.conf")
or die "Cannot write $dropin_dir/xcat-dump-memory.conf: $!\n";
print $fh "[Unit]\nConditionKernelCommandLine=|crashkernel\nConditionKernelCommandLine=|fadump\n";
close($fh) or die "Cannot write $dropin_dir/xcat-dump-memory.conf: $!\n";
}
sub copy_kernel {
my ($rootimg_dir, $kernelver, $dir) = @_;
my ($kernel) = grep { -e $_ } map { "$rootimg_dir/$_" }
("boot/vmlinux-$kernelver", "boot/vmlinuz-$kernelver", "boot/image-$kernelver", "lib/modules/$kernelver/vmlinuz");
die("couldn't find the kernel file matched $kernelver in $rootimg_dir/boot\n") unless $kernel;
File::Copy::cp($kernel, "$dir/kernel") or die("Error: failed to copy the kernel to $dir: $!\n");
return 1;
}
# Removes $dir after packing it, also when a command of the pipeline fails.
sub pack_initrd {
my ($dir, $initrd, $compress, $mode) = @_;
my $command = "cd $dir && find . | cpio -H newc -o | $compress -9 -c - > $initrd";
print "\n$command\n";
my $rc = system('bash', '-c', "set -o pipefail; $command");
rmtree($dir);
die("Error: failed to generate the initial ramdisk for $mode.\n") if $rc;
return 1;
}
# nodeset copies the kernel and the initial ramdisks as one set, so a failure puts back every file already replaced.
sub publish_staged_boot_files {
my ($native_bootdir, $destdir, @modes) = @_;
my @files = ([ 'kernel', 'kernel' ], map { [ "initrd-$_.gz", "initial ramdisk for $_" ] } @modes);
my %backup;
foreach my $file (@files) {
my ($name, $what) = @$file;
if (link("$destdir/$name", "$native_bootdir/previous-$name")) {
$backup{$name} = 1;
} elsif (!$!{ENOENT}) {
die("Error: failed to preserve the previous $what: $!\n");
}
}
my @published;
foreach my $file (@files) {
my ($name, $what) = @$file;
if (rename("$native_bootdir/$name", "$destdir/$name")) {
push @published, $file;
next;
}
my $error = $!;
# NFS can apply a rename and still report a failure, so put back a staged file that is gone.
my @attempted = (-e "$native_bootdir/$name" || -l "$native_bootdir/$name") ? () : ($file);
my @failed;
foreach my $done (@attempted, reverse @published) {
my ($done_name, $done_what) = @$done;
my $restored = $backup{$done_name}
? rename("$native_bootdir/previous-$done_name", "$destdir/$done_name")
: unlink("$destdir/$done_name");
push @failed, "$done_what rollback failed: $!" unless $restored;
}
if (@failed) {
$native_bootdir->unlink_on_destroy(0);
die("Error: failed to publish the $what: $error; " . join('; ', @failed) . ". Boot files retained in $native_bootdir\n");
}
die("Error: failed to publish the $what: $error\n");
}
return 1;
}
# Puts the UFTP fetch helper in the xCAT dracut module of an initial ramdisk when $wanted is true and the image has the
# UFTP 5 receiver of xcat-dep for its architecture, and removes an older copy otherwise. Returns the receiver or undef.
sub add_uftp_receiver {
my ($rootimg_dir, $arch, $moduledir, $wanted) = @_;
require xCAT::UFTP;
my $copy = "$moduledir/$xCAT::UFTP::HELPER";
if (-e $copy || -l $copy) {
unlink($copy) or die("Error: cannot remove $copy: $!\n");
}
return unless $wanted;
my ($receiver, $warning) = xCAT::UFTP::usable_receiver($rootimg_dir, $arch);
print "Warning: $warning\n" if $warning;
return unless $receiver;
my $helper = dirname(__FILE__) . "/$xCAT::UFTP::HELPER";
File::Copy::copy($helper, $copy) && chmod(0644, $copy) or die("Error: cannot copy $helper to $copy: $!\n");
print "Adding the UFTP receiver $receiver to the initial ramdisk.\n";
return $receiver;
}
# 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) = @_;
my @listing = `chroot $rootimg_dir lsinitrd $initrd 2>&1`;
return $? ? undef : \@listing;
}
# Dies unless the initial ramdisk $initrd, a path inside the image, holds the UFTP receiver files.
sub check_uftp_initrd {
my ($rootimg_dir, $initrd, $mode) = @_;
require xCAT::UFTP;
my $listing = initrd_listing($rootimg_dir, $initrd);
my @missing = $listing ? xCAT::UFTP::initrd_missing_files($listing) : ('a readable file list');
die("Error: the initial ramdisk for $mode lacks the UFTP receiver files: @missing\n") if @missing;
return 1;
}
# Records next to a published initial ramdisk whether it can receive the root image over UFTP.
sub mark_uftp_receiver {
my ($initrd, $receiver) = @_;
require xCAT::UFTP;
my $error = $receiver ? xCAT::UFTP::write_initrd_marker($initrd) : xCAT::UFTP::remove_initrd_marker($initrd);
print "Warning: $error\n" if $error;
print "The initial ramdisk $initrd can receive the root image over UFTP.\n" if $receiver && !$error;
return !$error;
}
# The initial ramdisk configures $prinic, and bringing it up again breaks an NFS-based boot.
# EL10 reads no ifcfg files, so its images get keyfiles.
sub write_nic_profiles {
my ($rootimg_dir, $osver, $prinic, $othernics) = @_;
my @othernics = grep { length } split(/,/, $othernics // '');
my $scripts = "$rootimg_dir/etc/sysconfig/network-scripts";
my $keyfiles = "$rootimg_dir/etc/NetworkManager/system-connections";
my $major = el_major_version($osver);
my %profiles;
if (defined($major) && $major >= 10) {
if (-d $keyfiles) {
# The keyfiles of an earlier build can name interfaces that this build no longer requests.
opendir(my $dh, $keyfiles) or die("Error: cannot read $keyfiles: $!\n");
foreach my $old (grep { /^xcat-genimage-.*\.nmconnection$/ } readdir($dh)) {
unlink("$keyfiles/$old") or die("Error: cannot remove $keyfiles/$old: $!\n");
}
closedir($dh);
$profiles{"$keyfiles/xcat-genimage-$_.nmconnection"} =
"[connection]\nid=$_\ntype=ethernet\ninterface-name=$_\n\n[ipv4]\nmethod=auto\n" for @othernics;
}
} elsif (-d $scripts) {
$profiles{"$scripts/ifcfg-$prinic"} = "ONBOOT=no\nBOOTPROTO=dhcp\nDEVICE=$prinic\n" if $prinic;
$profiles{"$scripts/ifcfg-$_"} = "ONBOOT=yes\nBOOTPROTO=dhcp\nDEVICE=$_\n" for @othernics;
}
foreach my $file (sort keys %profiles) {
# NetworkManager ignores a keyfile that other users can read.
my $mode = $file =~ /\.nmconnection$/ ? oct('600') : oct('644');
my $fh;
open($fh, '>', $file) && chmod($mode, $file) && print($fh $profiles{$file}) && close($fh)
or die("Error: cannot write $file: $!\n");
}
return;
}
# NetworkManager otherwise makes a DHCP profile for every Ethernet port without one.
sub disable_auto_profiles {
my ($rootimg_dir, $osver) = @_;
my $major = el_major_version($osver);
my $confdir = "$rootimg_dir/etc/NetworkManager/conf.d";
return unless defined($major) && $major >= 10 && -d $confdir;
my $file = "$confdir/xcat.conf";
my $fh;
open($fh, '>', $file) && print($fh "[main]\nno-auto-default=*\n") && close($fh)
or die("Error: cannot write $file: $!\n");
return;
}
# debootstrap deletes $rootimg_dir/debootstrap when it finishes, and its tar -k fails over a finished root.
sub debootstrap_finished {
my ($rootimg_dir) = @_;
die("Error: $rootimg_dir holds an unfinished debootstrap. Remove $rootimg_dir and run genimage again.\n")
if -e "$rootimg_dir/debootstrap";
return -e "$rootimg_dir/var/lib/dpkg/status" ? 1 : 0;
}
# genimage moves the .bak files back, and a .bak that an interrupted run left holds the image's own file.
sub use_host_resolver {
my ($rootimg_dir) = @_;
foreach my $file ('hosts', 'resolv.conf') {
my $image = "$rootimg_dir/etc/$file";
File::Copy::move($image, "$image.bak") unless -e "$image.bak" || -l "$image.bak";
unlink($image);
File::Copy::copy("/etc/$file", $image);
}
return;
}
# A finished image holds the dummy resolv.conf and the apt sources of the run that built it.
sub prepare_reused_root {
my ($rootimg_dir) = @_;
unlink(map { "$rootimg_dir/etc/apt/sources.list.d/$_" } qw(genimage.apt.list genimage1.apt.list));
use_host_resolver($rootimg_dir);
return;
}
# apt would move a root image of another release or architecture partway to the requested one.
sub check_reused_root {
my ($rootimg_dir, $suite, $arch) = @_;
my ($codename, $image_arch);
my $os_release = -e "$rootimg_dir/usr/lib/os-release" ? "$rootimg_dir/usr/lib/os-release" : "$rootimg_dir/etc/os-release";
my %field;
foreach my $file ($os_release, "$rootimg_dir/etc/lsb-release") {
my $fh;
next if -l $file || !open($fh, '<', $file);
%field = (%field, map { /^(VERSION_CODENAME|UBUNTU_CODENAME|DISTRIB_CODENAME)=(["']?)(\S+?)\2\s*$/ ? ($1 => $3) : () } <$fh>);
close($fh);
}
# Ubuntu 16.04 images before base-files 9.4ubuntu4.3 have only UBUNTU_CODENAME, and 14.04 images only DISTRIB_CODENAME.
$codename = $field{VERSION_CODENAME} // $field{UBUNTU_CODENAME} // $field{DISTRIB_CODENAME};
if (open(my $fh, '<', "$rootimg_dir/var/lib/dpkg/status")) {
local $/ = '';
($image_arch) = map { /^Package: dpkg\n(?:.+\n)*?Architecture: (\S+)/ ? $1 : () } <$fh>;
close($fh);
}
(my $release = $suite) =~ s/-(?:updates|security|backports|proposed)$//;
die("Error: genimage cannot tell the Ubuntu release and architecture of $rootimg_dir. Remove $rootimg_dir and run genimage again.\n")
unless $codename && $image_arch;
die("Error: $rootimg_dir holds Ubuntu $codename for $image_arch, not $release for $arch. Remove $rootimg_dir or use another rootimgdir.\n")
unless $codename eq $release && $image_arch eq $arch;
# dpkg owns bin/uname, so genimage cannot tell whether the backup or the file in place is current.
die("Error: $rootimg_dir holds bin/uname-binary, which an interrupted genimage run left. Restore bin/uname from it or remove $rootimg_dir, then run genimage again.\n")
if -e "$rootimg_dir/bin/uname-binary" || -l "$rootimg_dir/bin/uname-binary";
return 1;
}
# A kernel release compares number by number, so 6.8.0-100-generic is newer than 6.8.0-99-generic.
sub newest_kernel_version {
my @releases = @_;
my $compare = sub {
my @left = split(/(\d+)/, $_[0]);
my @right = split(/(\d+)/, $_[1]);
while (@left && @right) {
my ($l, $r) = (shift(@left), shift(@right));
my $order = ($l =~ /^\d+$/ && $r =~ /^\d+$/) ? $l <=> $r : $l cmp $r;
return $order if $order;
}
return @left <=> @right;
};
my ($newest) = sort { $compare->($b, $a) } @releases;
return $newest;
}
# Package scripts run this uname in the image, so -r names the kernel that genimage publishes.
sub uname_script {
my ($kernelver) = @_;
my $release = $kernelver ? "echo \"$kernelver\"" : 'ls /lib/modules | sort -V | tail -n 1';
return "#!/bin/bash \n"
. "case \"\$1\" in \n"
. "\"-m\") \n"
. " ARCH=\"\$(dpkg --print-architecture || \n"
. " rpm -q kernel-\$(\"\$0\" -r) --qf '%{arch}')\" \n"
. " case \"\$ARCH\" in \n"
. " \"amd64\") ARCH=\"x86_64\" ;; \n"
. " \"ppc64el\") ARCH=\"ppc64le\" ;; \n"
. " esac \n"
. " echo \"\$ARCH\" ;; \n"
. "\"-r\") $release ;; \n"
. "\"-s\"|\"\") echo \"Linux\" ;; \n"
. "esac \n"
. "exit 0 \n";
}
sub varsubinline{
my $line=shift;
my $refvardict=shift;
my @varsinline= $line =~ /\$\{?(\w+)\}?/g;
my @unresolvedvars;
foreach my $var(@varsinline){
if(exists $refvardict->{$var}){
$line=~ s/\$\{$var\}/$refvardict->{$var}/g;
$line=~ s/\$$var/$refvardict->{$var}/g;
}else{
push @unresolvedvars,$var;
}
}
return $line;
}
sub _profile_lookup_osbase_list {
my $osver = shift;
# OS version on s390x can contain 'sp', e.g. sles11sp1
# If OS version contains 'sp', get the index of 'sp' instead of '.'
if ($osver =~ /sles/ && $osver =~ /sp/) {
my $dotpos = rindex($osver, "sp");
return (substr($osver, 0, $dotpos));
}
return grep { $_ ne $osver } xCAT::SvrUtils::get_os_search_list($osver);
}
sub resolver_library_paths {
my $arch = shift;
my $libdir = $arch =~ /x86_64|aarch64|riscv64/ ? 'lib64' : 'lib';
return ( "$libdir/libnss_dns.so.2", "$libdir/libresolv.so.2" );
}
sub get_profile_def_filename {
my $osver = shift;
my $profile = shift;
my $arch = shift;
my $tmp_base = shift;
my $base = realpath($tmp_base); #get the full path
if (!$base) { $base = $tmp_base; }
my $ext = shift;
my @osbase = _profile_lookup_osbase_list($osver);
my $fallbackos;
if ($osver =~ /^leap15/) {
$fallbackos = "sle15";
}
return xCAT::ProfileUtils::find_profile_asset(
$base, $profile, $arch, $ext, $osver, @osbase,
($fallbackos ? ($fallbackos) : ()));
}
sub include_file
{
my $file = shift;
my $idir = shift;
my @text = ();
$file=varsubinline($file,\%ENV);
unless ($file =~ /^\//) {
$file = $idir . "/" . $file;
}
open(INCLUDE, $file) ||
return "#INCLUDEBAD:cannot open $file#";
while (<INCLUDE>) {
chomp($_);
s/\s+$//; #remove trailing spaces
next if /^\s*$/; #-- skip empty lines
next
if (/^\s*#/
&& !/^\s*#INCLUDE:[^#^\n]+#/
&& !/^\s*#NEW_INSTALL_LIST#/
&& !/^\s*#ENV:[^#^\n]+#/); #-- skip comments
push(@text, $_);
}
close(INCLUDE);
return join(',', @text);
}
sub get_package_names {
my $plist_file_list = shift;
my %pkgnames = ();
my @plist_file_names = split ',', $plist_file_list;
foreach my $plist_file_name (@plist_file_names) {
# this variable needs to be cleaned when loop the pkglist files
my @tmp_array = ();
if ($plist_file_name && -r $plist_file_name) {
my $pkgfile;
open($pkgfile, "<", "$plist_file_name");
while (<$pkgfile>) {
chomp;
s/\s+$//; #remove trailing white spaces
next if /^\s*$/; #-- skip empty lines
next
if (/^\s*#/
&& !/^\s*#INCLUDE:[^#^\n]+#/
&& !/^\s*#NEW_INSTALL_LIST#/
&& !/^\s*#ENV:[^#^\n]+#/); #-- skip comments
push(@tmp_array, $_);
}
close($pkgfile);
if (@tmp_array > 0) {
my $pkgtext = join(',', @tmp_array);
#handle the #INLCUDE# tag recursively
my $idir = dirname($plist_file_name);
my $doneincludes = 0;
while (not $doneincludes) {
$doneincludes = 1;
if ($pkgtext =~ /#INCLUDE:[^#^\n]+#/) {
$doneincludes = 0;
$pkgtext =~ s/#INCLUDE:([^#^\n]+)#/include_file($1,$idir)/eg;
}
}
#print "\n\npkgtext=$pkgtext\n\n";
my @tmp = split(',', $pkgtext);
my $pass = 1;
foreach (@tmp) {
my $idir;
if (/^--/) {
$idir = "POST_REMOVE"; #line starts with -- means the package should be removed after otherpkgs are installed
s/^--//;
} elsif (/^-/) {
$idir = "PRE_REMOVE"; #line starts with single - means the package should be removed before otherpkgs are installed
s/^-//;
} elsif (/^#NEW_INSTALL_LIST#/) {
$pass++;
next;
} elsif (/^#ENV:([^#^\n]+)#/) {
my $pa = $pkgnames{$pass}{ENVLIST};
my $env = $1;
if (exists($pkgnames{$pass}{ENVLIST})) {
push(@$pa, $env);
} else {
$pkgnames{$pass}{ENVLIST} = [$env];
}
next;
} elsif (/^#INCLUDEBAD:([^#^\n]+)#/) {
my $pa = $pkgnames{$pass}{INCLUDEBAD};
my $file = $1;
if (exists($pkgnames{$pass}{INCLUDEBAD})) {
push(@$pa, $file);
} else {
$pkgnames{$pass}{INCLUDEBAD} = [$file];
}
next;
} elsif (/^#/) {
# ignore all other comment lines
next;
} else {
$idir = dirname($_);
}
my $fn = basename($_);
if (exists($pkgnames{$pass}{$idir})) {
my $pa = $pkgnames{$pass}{$idir};
push(@$pa, $fn);
} else {
$pkgnames{$pass}{$idir} = [$fn];
}
}
}
}
}
return %pkgnames;
}
sub default_net_drivers {
my ( $family, $arch ) = @_;
my %drivers = (
rh => {
x86 => [qw(tg3 bnx2 bnx2x e1000 e1000e igb mlx_en mlx5_core virtio_net be2net)],
x86_64 => [qw(tg3 bnx2 bnx2x e1000 e1000e igb mlx_en mlx5_core virtio_net be2net)],
aarch64 => [qw(tg3 bnx2 bnx2x e1000e igb mlx_en mlx5_core virtio_net)],
riscv64 => [qw(e1000 e1000e igb ixgbe i40e ice r8169 tg3 bnx2x mlx5_core virtio_net)],
ppc64 => [qw(e1000 e1000e igb ibmveth ehea)],
s390x => [qw(qdio ccwgroup)],
},
sles => {
x86 => [qw(tg3 bnx2 bnx2x e1000 e1000e virtio_net virtio_balloon igb mlx4_en mlx5_core be2net)],
x86_64 => [qw(tg3 bnx2 bnx2x e1000 e1000e virtio_net virtio_balloon igb mlx4_en mlx5_core be2net)],
ppc64 => [qw(tg3 e1000 e1000e igb ibmveth ehea be2net)],
s390x => [qw(qdio ccwgroup qeth qeth_l2 qeth_l3)],
},
ubuntu => {
x86 => [qw(tg3 bnx2 bnx2x e1000 e1000e igb mlx_en mlx5_core virtio_net overlay)],
x86_64 => [qw(tg3 bnx2 bnx2x e1000 e1000e igb mlx_en mlx5_core virtio_net overlay)],
ppc64el => [qw(tg3 bnx2 bnx2x e1000 e1000e igb ibmveth ehea mlx_en mlx4_en mlx5_core virtio_net overlay)],
ppc64 => [qw(e1000 e1000e igb ibmveth ehea)],
riscv64 => [qw(e1000 e1000e igb ixgbe i40e ice r8169 tg3 bnx2x mlx5_core virtio_net overlay)],
s390x => [qw(qdio ccwgroup)],
},
);
return () if !exists($drivers{$family}) || !exists($drivers{$family}{$arch});
return @{ $drivers{$family}{$arch} };
}
sub _kernel_module_name {
my $path = shift;
my $name = basename($path);
$name =~ s/\.ko(?:\.(?:gz|xz|zst))?$//x;
$name =~ tr/-/_/;
return $name;
}
sub target_kernel_module_availability {
my ( $rootimg_dir, $kernelver, @modules ) = @_;
my %requested = map { _kernel_module_name($_) => 1 } @modules;
my %available = map { $_ => 0 } keys %requested;
my $module_root = "$rootimg_dir/lib/modules/$kernelver";
my $modules_dep = "$module_root/modules.dep";
if (open(my $dep_fh, '<', $modules_dep)) {
while (my $line = <$dep_fh>) {
my ($path) = split /:/x, $line, 2;
my $name = _kernel_module_name($path);
if ($requested{$name} && -f "$module_root/$path") {
$available{$name} = 1;
}
}
close($dep_fh);
}
my $modules_builtin = "$module_root/modules.builtin";
if (open(my $builtin_fh, '<', $modules_builtin)) {
while (my $path = <$builtin_fh>) {
chomp($path);
my $name = _kernel_module_name($path);
$available{$name} = 1 if $requested{$name};
}
close($builtin_fh);
}
if (-d $module_root && grep { !$available{$_} } keys %requested) {
find(
{
no_chdir => 1,
wanted => sub {
return if !-f $File::Find::name;
return if $File::Find::name !~ /\.ko(?:\.(?:gz|xz|zst))?$/x;
my $name = _kernel_module_name($File::Find::name);
$available{$name} = 1 if $requested{$name};
},
},
$module_root,
);
}
return %available;
}
sub resolve_mellanox_default_net_drivers {
my ( $rootimg_dir, $kernelver, $requested_drivers, @default_drivers ) = @_;
my @drivers;
my %seen;
my $has_mellanox_defaults = grep {
_kernel_module_name($_) =~ /^mlx(?:_en|4_en|5_core)$/x
} @default_drivers;
my $has_mellanox_drivers = $has_mellanox_defaults || grep {
_kernel_module_name($_) =~ /^mlx(?:_en|4_en|5_core)$/x
} @{$requested_drivers};
if (!$has_mellanox_drivers) {
return grep { !$seen{$_}++ } (@{$requested_drivers}, @default_drivers);
}
my %available = target_kernel_module_availability(
$rootimg_dir,
$kernelver,
qw(mlx_en mlx4_en mlx5_core mlx4_ib mlx5_ib ib_ipoib),
);
# Keep unavailable explicit requests, except for the historical mlx4 alias.
foreach my $driver (@{$requested_drivers}) {
my $name = _kernel_module_name($driver);
if ($name eq 'mlx_en'
&& !$available{'mlx_en'}
&& $available{'mlx4_en'})
{
$driver =~ s/mlx_en/mlx4_en/;
}
push @drivers, $driver;
}
foreach my $driver (@default_drivers) {
my $name = _kernel_module_name($driver);
if ($name eq 'mlx_en') {
if (!$available{'mlx_en'}) {
next if !$available{'mlx4_en'};
$driver =~ s/mlx_en/mlx4_en/;
}
} elsif ($name eq 'mlx4_en' || $name eq 'mlx5_core') {
next if !$available{$name};
}
push @drivers, $driver;
}
if ($has_mellanox_defaults) {
my @mellanox_ib_drivers = grep { $available{$_} } qw(mlx4_ib mlx5_ib);
push @drivers, map { "$_.ko" } @mellanox_ib_drivers;
if (@mellanox_ib_drivers && $available{'ib_ipoib'}) {
push @drivers, 'ib_ipoib.ko';
}
}
return grep { !$seen{$_}++ } @drivers;
}
1;