lib/Rex/GPU/NVIDIA/Setup/Ubuntu.pm

filter: Statementstmt 100% 62 / 62 Branchbran 80.7% 21 / 26 Conditioncond 70.5% 12 / 17 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 87.1% 115 / 132 CC 21 SCAR 17.2
CC
21
Cov
90%
resolve_source
10.1
_nvlsm_repo
6.2
nvlink_fabric_unavailable
4.2
CRAP 5.6
« lib/Rex/GPU/AMD/Setup/Apt.pm ↑ Summary lib/Rex/GPU/AMD/Setup.pm »
1 # ABSTRACT: NVIDIA driver setup for Ubuntu (experimental)
2  
3 package Rex::GPU::NVIDIA::Setup::Ubuntu;
4 49 our $VERSION = '0.006';
5 49 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 49 ▶use Rex::Logger ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 49 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 49 extends 'Rex::GPU::NVIDIA::Setup::Apt';
10  
11  
12 # 580 is published for jammy and noble (Launchpad, source
13 # nvidia-graphics-drivers-580-server, checked 2026-09-23: 580.178.04 on both,
14 # next to 590 and 595). The apt-cache search runs in resolve_source, after
15 # `apt-get update` -- before it, a fresh image's index is stale or empty
16 # (karr #35).
17 #
18 # Fabric Manager (karr #23): Ubuntu's archive builds nvidia-fabricmanager-NNN
19 # (source fabric-manager-NNN) for each -server branch, one package for the
20 # proprietary and the -open driver; it Depends on the virtual
21 # nvidia-kernel-common-NNN-server-<exact upstream version>, and on noble and
22 # jammy it carries exactly the version string of nvidia-driver-NNN-server
23 # (packages.ubuntu.com, 580.178.04-0ubuntu0.24.04.1 / 22.04.1, checked
24 # 2026-09-24).
25 144 ▶sub sources {
Subroutine sources: called CC=1 SCAR=0.0
Pod documented
26 144 my ( $self ) = @_;
27 144 my %fm = ( fabric_manager => 'nvidia-fabricmanager-%s' );
28 return (
29 {
30 144 name => 'ubuntu-server',
31 kernel_module => 'proprietary',
32 branch_at_least => 580,
33 search => '^nvidia-driver-[0-9].*-server$',
34 %fm,
35 fabric_manager_match => 'nvidia-driver-%s-server'
36 },
37 {
38 name => 'ubuntu-server-open',
39 kernel_module => 'open',
40 branch_at_least => 580,
41 search => '^nvidia-driver-[0-9].*-server-open$',
42 %fm,
43 fabric_manager_match => 'nvidia-driver-%s-server-open'
44 },
45 {
46 name => 'ubuntu-server-580',
47 kernel_module => 'proprietary',
48 branch => 580,
49 packages => [ 'nvidia-driver-580-server' ],
50 verify => [ 'nvidia-driver-580-server' ],
51 check_candidate => 'nvidia-driver-580-server',
52 %fm,
53 fabric_manager_match => 'nvidia-driver-580-server'
54 }
55 );
56 }
57  
58  
59 10 ▶sub nvlink_fabric_packages {
Subroutine nvlink_fabric_packages: called CC=1 SCAR=0.0
Pod documented
60 10 my ( $self ) = @_;
61 # ib_umad.ko is in linux-modules-extra-<kver> (research 2026-09-24,
62 # checked for 6.8.0-142-generic); the running kernel's own package.
63 10 return ( $self->SUPER::nvlink_fabric_packages, 'linux-modules-extra-'.$self->kernel );
64 }
65  
66 # nvlsm verified in repos/ubuntu2204 and repos/ubuntu2404 (x86_64), research
67 # of 2026-09-24; not in Ubuntu's archive, not in DOCA.
68 49 my %NVLSM_REPO = ( '22.04' => 'ubuntu2204', '24.04' => 'ubuntu2404' );
69  
70 25 ▶sub _nvlsm_repo {
Subroutine _nvlsm_repo: called CC=6 SCAR=18.3
71 25 my ( $self ) = @_;
72 25 ▶ my ($release) = ( $self->release // '' ) =~ /^(\d+\.\d+)/;
Condition50%1/2
$self->release // ""
l!l
250
MC/DC0%0/1
$self->release // ""
$self->release
Truth table50%1/2
A
A= $self->release
Aresult
11
00
73 25 ▶ my $distro = defined $release ? $NVLSM_REPO{$release} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $release ? : 25 0 25
74 25 ▶ return unless defined $distro && ( $self->arch // '' ) eq 'amd64';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distro and ($self->arch // "") eq "amd64" 2 23 25
Condition80%4/5
defined $distro and ($self->arch // "") eq "amd64"
!ll&&!rl&&r
1123
$self->arch // ""
l!l
240
MC/DC50%1/2
defined $distro and ($self->arch // "") eq "amd64"
defined $distro $self->arch
Truth table66%2/3
A and B
A= defined $distroB= $self->arch
ABresult
0X0
100
111
75 return {
76 23 distro => $distro,
77 arch => 'x86_64',
78 url => "https://developer.download.nvidia.com/compute/cuda/repos/$distro/x86_64/",
79 keyring_url => "https://developer.download.nvidia.com/compute/cuda/repos/$distro/x86_64/cuda-keyring_1.1-1_all.deb"
80 };
81 }
82  
83 14 ▶sub nvlink_fabric_unavailable {
Subroutine nvlink_fabric_unavailable: called CC=4 SCAR=14.3
Pod documented
84 14 my ( $self ) = @_;
85 14 ▶ return if $self->_nvlsm_repo;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_nvlsm_repo 12 2 14
86 2 ▶ return "nvlsm is only in NVIDIA's CUDA repository, which Rex::GPU uses for it on Ubuntu "
Condition50%2/4
$self->release // ""
l!l
20
$self->arch // ""
l!l
20
MC/DC0%0/1
$self->release // ""
$self->release
MC/DC0%0/1
$self->arch // ""
$self->arch
Truth table50%1/2
A
A= $self->release
Aresult
11
00
Truth table50%1/2
A
A= $self->arch
Aresult
11
00
87 ."22.04 and 24.04 (amd64) only, not on release '".( $self->release // '' )."' ("
88 .( $self->arch // '' ).')';
89 }
90  
91 7 ▶sub nvlsm_pin_file { '/etc/apt/preferences.d/rex-gpu-nvlsm.pref' }
Subroutine nvlsm_pin_file: called CC=1 SCAR=0.0
Pod documented
92 7 ▶sub nvlsm_source_file { '/etc/apt/sources.list.d/rex-gpu-nvlsm.list' }
Subroutine nvlsm_source_file: called CC=1 SCAR=0.0
Pod documented
93  
94 11 ▶sub prepare_nvlink_fabric_source {
Subroutine prepare_nvlink_fabric_source: called CC=4 SCAR=13.9
Pod documented
95 11 my ( $self, $plan ) = @_;
96 11 ▶ my $repo = $self->_nvlsm_repo
Branch: 1 branch
BranchTrueFalseTotal
my $repo = $self->_nvlsm_repo or die "No NVIDIA CUDA repository for nvlsm on this Ubuntu host; the driver and Fabric Manager are installed, nvlsm is not\n" 11 0 11
97 or die 'No NVIDIA CUDA repository for nvlsm on this Ubuntu host; the driver and '
98 ."Fabric Manager are installed, nvlsm is not\n";
99 # k94: a held (hi) cuda-keyring has the same files, so it counts too
100 11 $self->run_cmd("dpkg -l cuda-keyring 2>/dev/null | grep -q '^[hi]i'", auto_die => 0);
101 11 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 3 8 11
102 3 Rex::Logger::info(' cuda-keyring is installed: NVIDIA\'s CUDA repository is already '
103 .'configured (with its own apt pin), nothing is added');
104 3 $self->refresh_package_index;
105 3 return;
106 }
107 8 Rex::Logger::info(" Adding NVIDIA's CUDA repository ($repo->{distro}/$repo->{arch}) for nvlsm "
108 .'only, every other package of it pinned out');
109 8 my $keyring = '/usr/share/keyrings/cuda-archive-keyring.gpg';
110 8 $self->run_cmd('DEBIAN_FRONTEND=noninteractive '.$self->apt_get.' install -y --no-upgrade curl',
111 auto_die => 0);
112 8 $self->verify_packages({ verify => [ 'curl' ] });
113 $self->run_cmd(q{t=$(mktemp -d) && curl -fsSL -o "$t/cuda-keyring.deb" }.$repo->{keyring_url}
114 8 .q{ && dpkg-deb --fsys-tarfile "$t/cuda-keyring.deb" | tar -xO ./usr/share/keyrings/cuda-archive-keyring.gpg > "$t/key.gpg"}
115 .q{ && test -s "$t/key.gpg" && install -m 0644 "$t/key.gpg" }.$keyring
116 .q{; rc=$?; rm -rf "$t"; exit $rc},
117 auto_die => 0);
118 8 ▶ die "Could not fetch the signing key of NVIDIA's CUDA repository ($repo->{keyring_url}) "
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 7 8
119 ."into $keyring; the driver and Fabric Manager are installed, nvlsm is not\n" if $? != 0;
120 7 $self->file_cmd($self->nvlsm_pin_file, content => $self->_nvlsm_pin, mode => 644);
121 $self->file_cmd($self->nvlsm_source_file,
122 7 content => 'deb [signed-by='.$keyring.'] '.$repo->{url}." /\n", mode => 644);
123 7 $self->refresh_package_index;
124 7 return;
125 }
126  
127 # apt_preferences(5): the first specific-form record (Package: nvlsm) decides
128 # nvlsm's priority; every other package of that origin gets the general
129 # record's -1, "prevents the version from being installed". cuda-keyring's
130 # own pin (Package: * / release l=NVIDIA CUDA / 600) is NOT installed: a
131 # general record, it would win the maximum over -1.
132 7 ▶sub _nvlsm_pin {
Subroutine _nvlsm_pin: called CC=1 SCAR=0.0
133 7 return join("\n",
134 'Explanation: Rex::GPU (HGX B200/B300): NVIDIA\'s CUDA repository is here for nvlsm only.',
135 'Explanation: Nothing else is installed or upgraded from it; the NVIDIA driver stays Ubuntu\'s.',
136 'Package: *',
137 'Pin: origin developer.download.nvidia.com',
138 'Pin-Priority: -1',
139 '',
140 'Package: nvlsm',
141 'Pin: origin developer.download.nvidia.com',
142 'Pin-Priority: 500',
143 '');
144 }
145  
146  
147 97 ▶sub resolve_source {
Subroutine resolve_source: called CC=10 SCAR=23.2
Pod documented
148 97 my ( $self, $source ) = @_;
149 97 ▶ if (defined $source->{search}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source->{"search"}) 72 25 97
150 72 my $latest = $self->run_cmd("apt-cache search '$source->{search}' 2>/dev/null | sort -t- -k3 -n | tail -1 | awk '{print \$1}'",
151 auto_die => 0);
152 72 ▶ chomp $latest if $latest;
Branch: 1 branch
BranchTrueFalseTotal
if $latest 69 3 72
153 # Filter out *-open variants from auto-detect (use regular server driver)
154 72 ▶ $latest = undef if $latest && $source->{kernel_module} ne 'open' && $latest =~ /-open$/;
Branch: 1 branch
BranchTrueFalseTotal
if $latest and $source->{"kernel_module"} ne "open" and $latest =~ /-open$/ 0 72 72
Condition83%5/6
$latest and $source->{"kernel_module"} ne "open" and $latest =~ /-open$/
!ll&&!rl&&r
37350
$latest and $source->{"kernel_module"} ne "open"
!ll&&!rl&&r
33435
MC/DC0%0/3
$latest and $source->{"kernel_module"} ne "open" and $latest =~ /-open$/
$latest $source->{"kernel_module"} ne "open" $latest =~ /-open$/
Truth table75%3/4
A and B and C
A= $latestB= $source->{"kernel_module"} ne "open"C= $latest =~ /-open$/
ABCresult
0XX0
10X0
1100
1111
155 72 ▶ return { %$source, unavailable => "apt-cache search '".$source->{search}."' finds no "
Branch: 1 branch
BranchTrueFalseTotal
unless $latest 3 69 72
156 .'package after apt-get update (did the update fail, or is the restricted '
157 .'component missing from the apt sources?)' }
158 unless $latest;
159 69 my ($branch) = $latest =~ /^nvidia-driver-(\d+)-server/;
160 69 ▶ return { %$source, unavailable => 'no driver branch in the package name '.$latest }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch 0 69 69
161 unless defined $branch;
162 69 my %resolved = ( %$source, packages => [ $latest ], verify => [ $latest ], branch => $branch );
163 69 delete $resolved{branch_at_least};
164 69 return \%resolved;
165 }
166 25 ▶ if (defined $source->{check_candidate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source->{"check_candidate"}) 25 0 25
167 25 my $pinned = $source->{check_candidate};
168 25 my $policy = $self->run_cmd("LC_ALL=C apt-cache policy $pinned 2>/dev/null", auto_die => 0);
169 25 ▶ return { %$source, unavailable => $pinned.' has no installation candidate after '
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_apt_candidate_present($policy) 1 24 25
170 .'apt-get update, and no other package is substituted' }
171 unless $self->_apt_candidate_present($policy);
172 }
173 24 return $source;
174 }
175  
176 49 1;
177  
178 __END__
« lib/Rex/GPU/AMD/Setup/Apt.pm ↑ Summary lib/Rex/GPU/AMD/Setup.pm »