lib/Rex/GPU/NVIDIA.pm

filter: Statementstmt 98.8% 507 / 513 Branchbran 94.4% 255 / 270 Conditioncond 81.1% 125 / 154 MC/DCmcdc 66.6% 58 / 87 Subroutinesub 100% 67 / 67 Podpod 100% 13 / 13 totaltotal 92.8% 1025 / 1104 CC 203 SCAR 21.2
CC
203
Cov
95%
install_driver
23.0
generate_cdi_specs
14.0
install_container_toolkit
14.0
CRAP 8.3
« blib/lib/Rex/GPU.pm ↑ Summary lib/Rex/GPU/NVIDIA/Setup/Debian.pm »
1 # ABSTRACT: NVIDIA GPU driver and container toolkit management
2  
3 package Rex::GPU::NVIDIA;
4 36 our $VERSION = '0.005';
5 36 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 36 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 36 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶use Rex::Commands::Pkg;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 36 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 36 ▶use Rex::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 36 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 36 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 36 ▶use Module::Runtime qw( is_module_name module_notional_filename use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 36 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 36 ▶use Rex::GPU::NVIDIA::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 36 ▶use Rex::GPU::NVIDIA::Setup::Apt;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 36 ▶use Rex::GPU::NVIDIA::Setup::Debian;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶use Rex::GPU::NVIDIA::Setup::RHEL;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Rex::GPU::NVIDIA::Setup::SUSE;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶use Rex::GPU::NVIDIA::Setup::Ubuntu;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 36 ▶use Rex::GPU::NVIDIA::Setup::UbuntuDrivers;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 36 require Rex::Exporter;
28 36 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 36 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 36 @EXPORT = qw(
33 install_driver
34 install_container_toolkit
35 configure_containerd
36 verify_nvidia
37 generate_cdi_specs
38 );
39  
40  
41 1131 ▶sub install_driver {
Subroutine install_driver: called CC=23 SCAR=31.4
Pod documented
42 1131 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->install_driver(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->install_driver(@_))
l!l
567564
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->install_driver(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
43 567 my ( undef, %opts ) = @_;
44  
45 die "install_driver: pass gpu or gpus, not both\n"
46 567 ▶ if defined $opts{gpu} && defined $opts{gpus};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"gpu"} and defined $opts{"gpus"} 1 566 567
Condition100%3/3
defined $opts{"gpu"} and defined $opts{"gpus"}
!ll&&!rl&&r
2393271
MC/DC100%2/2
defined $opts{"gpu"} and defined $opts{"gpus"}
defined $opts{"gpu"} defined $opts{"gpus"}
Truth table100%3/3
A and B
A= defined $opts{"gpu"}B= defined $opts{"gpus"}
ABresult
0X0
100
111
47 566 ▶ my $gpus = $opts{gpus} // [ defined $opts{gpu} ? $opts{gpu} : () ];
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'gpu'} ? : 327 13 340
Condition100%2/2
$opts{"gpus"} // [defined $opts{"gpu"} ? $opts{"gpu"} : ()]
l!l
226340
MC/DC100%1/1
$opts{"gpus"} // [defined $opts{"gpu"} ? $opts{"gpu"} : ()]
$opts{"gpus"}
Truth table100%2/2
A
A= $opts{"gpus"}
Aresult
11
00
48 566 ▶ die "install_driver: gpus must be an arrayref of GPU hashrefs\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gpus eq "ARRAY" 0 566 566
49 unless ref $gpus eq 'ARRAY';
50 die "install_driver: nvswitches must be an arrayref\n"
51 566 ▶ if defined $opts{nvswitches} && ref $opts{nvswitches} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"nvswitches"} and ref $opts{"nvswitches"} ne "ARRAY" 0 566 566
Condition66%2/3
defined $opts{"nvswitches"} and ref $opts{"nvswitches"} ne "ARRAY"
!ll&&!rl&&r
529370
MC/DC0%0/2
defined $opts{"nvswitches"} and ref $opts{"nvswitches"} ne "ARRAY"
defined $opts{"nvswitches"} ref $opts{"nvswitches"} ne "ARRAY"
Truth table66%2/3
A and B
A= defined $opts{"nvswitches"}B= ref $opts{"nvswitches"} ne "ARRAY"
ABresult
0X0
100
111
52 566 ▶ my @nvswitches = defined $opts{nvswitches} ? ( nvswitches => $opts{nvswitches} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'nvswitches'} ? : 37 529 566
53  
54 # Every supported OS runs through a Setup class (epic karr #25, T2/T3): the
55 # already-installed short-circuit, the Kepler rejection, the multi-GPU
56 # requirement, package selection, install, verification and the nouveau
57 # blacklist are its steps. Which class: setup =>, set gpu_nvidia_setup, or
58 # the OS (karr #34) -- resolved before anything touches the host.
59 566 ▶ my @extra = defined $opts{requirement} ? ( extra_requirement => $opts{requirement} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'requirement'} ? : 4 562 566
60 566 my $setup = $class->setup_for(gpus => $gpus, setup => $opts{setup}, @extra, @nvswitches);
61 556 ▶ unless ($setup) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($setup) 4 552 556
62 # No class for this OS. Same order as before the move: a working driver
63 # still short-circuits and a Kepler or a GPU conflict still gets its own
64 # message (both via the base class, read-only), then the OS is refused.
65 4 my $probe = $class->setup_base_class->new(gpus => $gpus, @extra);
66 4 ▶ return if $probe->already_installed;
Branch: 1 branch
BranchTrueFalseTotal
if $probe->already_installed 1 3 4
67 3 $probe->plan;
68 1 die "Unsupported OS for NVIDIA driver installation: ".$probe->os."\n";
69 }
70 552 ▶ unless ($setup->install) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($setup->install) 60 353 413
71 # Already installed (karr #50): Fabric Manager only if the host's own
72 # package sources offer it at the loaded driver's exact version -- no
73 # source is added, the driver is not touched; otherwise it only warns.
74 # The driver runs, so a Fabric Manager installed now can start now.
75 60 ▶ if ($setup->fabric_manager_needed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setup->fabric_manager_needed) 24 36 60
76 24 my $fm = $setup->retrofit_fabric_manager;
77 # HGX B200/B300 (karr #56): nvlsm & co. from the host's own sources
78 22 ▶ my $fabric = $setup->nvlink_fabric_needed ? $setup->retrofit_nvlink_fabric : 0;
Branch: 1 branch
BranchTrueFalseTotal
$setup->nvlink_fabric_needed ? : 2 20 22
79 22 ▶ run "systemctl start ".$setup->fabric_manager_service, auto_die => 0
Branch: 1 branch
BranchTrueFalseTotal
if $fm or $fabric 6 16 22
Condition100%3/3
$fm or $fabric
l!l&&r!l&&!r
5116
MC/DC100%2/2
$fm or $fabric
$fm $fabric
Truth table100%3/3
A or B
A= $fmB= $fabric
ABresult
1X1
011
000
80 if $fm || $fabric;
81 22 my $active = $class->_check_fabric_manager($setup);
82 22 ▶ $setup->check_nvlink_fabric($active) if $setup->nvlink_fabric_needed;
Branch: 1 branch
BranchTrueFalseTotal
if $setup->nvlink_fabric_needed 2 20 22
83 }
84 58 $class->_note_nvlink_platforms($setup);
85 58 return;
86 }
87  
88 353 ▶ if ($opts{reboot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'reboot'}) { } 4 349 353
89 4 $class->_reboot_and_wait;
90 }
91 else {
92 349 run "modprobe nvidia", auto_die => 0;
93 # Enabled by the setup, not started (karr #23): only now can the module
94 # be loaded. Fails harmlessly while nouveau still holds the GPUs.
95 349 ▶ run "systemctl start ".$setup->fabric_manager_service, auto_die => 0
Branch: 1 branch
BranchTrueFalseTotal
if $setup->fabric_manager_needed 15 334 349
96 if $setup->fabric_manager_needed;
97 }
98 353 ▶ my $fm_active = $setup->fabric_manager_needed ? $class->_check_fabric_manager($setup) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$setup->fabric_manager_needed ? : 15 338 353
99  
100 # Driver only (karr #42): the toolkit comes after this step in gpu_setup,
101 # so verify_nvidia's nvidia-ctk check could only warn here.
102 353 $class->verify_nvidia_driver(setup => $setup);
103 # HGX B200/B300 (karr #56): Fabric State of every GPU; warns, never dies
104 353 ▶ $setup->check_nvlink_fabric($fm_active) if $setup->nvlink_fabric_needed;
Branch: 1 branch
BranchTrueFalseTotal
if $setup->nvlink_fabric_needed 10 343 353
105 353 $class->_note_nvlink_platforms($setup);
106  
107 353 Rex::Logger::info("NVIDIA driver installation complete");
108 }
109  
110  
111 # Ubuntu is recognised by its OS name exactly as the old $os eq 'Ubuntu'
112 # branch of install_driver did; every other is_debian host (Debian,
113 # derivatives) gets the Debian class. is_debian, is_redhat, is_suse are asked
114 # in the order the old install_driver branches asked them (epic karr #25;
115 # user selection is T5).
116 570 ▶sub setup_class_for_os {
Subroutine setup_class_for_os: called CC=6 SCAR=17.9
Pod documented
117 570 my ( $class ) = @_;
118 570 ▶ if (is_debian()) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_debian()) 244 326 570
119 244 ▶ return operating_system() eq 'Ubuntu'
Branch: 1 branch
BranchTrueFalseTotal
operating_system() eq 'Ubuntu' ? : 136 108 244
120 ? 'Rex::GPU::NVIDIA::Setup::Ubuntu'
121 : 'Rex::GPU::NVIDIA::Setup::Debian';
122 }
123 326 ▶ return 'Rex::GPU::NVIDIA::Setup::RHEL' if is_redhat();
Branch: 1 branch
BranchTrueFalseTotal
if is_redhat() 148 178 326
124 178 ▶ return 'Rex::GPU::NVIDIA::Setup::SUSE' if is_suse();
Branch: 1 branch
BranchTrueFalseTotal
if is_suse() 90 88 178
125 88 ▶ return 'Rex::GPU::NVIDIA::Setup::RHEL' if $class->_rhel_family_name(operating_system());
Branch: 1 branch
BranchTrueFalseTotal
if $class->_rhel_family_name(operating_system()) 80 8 88
126 8 return;
127 }
128  
129 # karr #39: with lsb_release installed, Rex 1.16 names the OS by
130 # `lsb_release -s -i`, and its is_redhat list misses the RHEL rebuilds'
131 # distributor IDs. Checked against the lsb_release scripts themselves
132 # (2026-09-24): redhat-lsb-core 4.1 (EL8, EL9 devel repos) prints Rocky,
133 # AlmaLinux, CentOSStream, RedHatEnterprise; EPEL 9's lsb_release 3.2 (reads
134 # /etc/os-release) prints RockyLinux, AlmaLinux, CentOS, RedHatEnterprise.
135 # Without lsb_release Rex reports Redhat (RHEL, Rocky, Alma) or CentOS, which
136 # is_redhat knows, as it knows RedHatEnterprise. Only the missing ones are
137 # listed, as exact names: an OS we cannot name is still refused, and nothing
138 # is read from the host to decide (the class is chosen before any probe).
139 106 ▶sub _rhel_family_name {
Subroutine _rhel_family_name: called CC=3 SCAR=11.1
140 106 my ( $class, $os ) = @_;
141 106 ▶ return ($os // '') =~ /^(?:Rocky|RockyLinux|AlmaLinux|CentOSStream)$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($os // '') =~ /^(?:Rocky|RockyLinux|AlmaLinux|CentOSStream)$/u ? : 88 18 106
Condition50%1/2
$os // ""
l!l
1060
MC/DC0%0/1
$os // ""
$os
Truth table50%1/2
A
A= $os
Aresult
11
00
142 }
143  
144  
145 581 ▶sub setup_for {
Subroutine setup_for: called CC=9 SCAR=22.0
Pod documented
146 581 my ( $class, %opt ) = @_;
147 581 ▶ my $gpus = $opt{gpus} // [];
Condition100%2/2
$opt{"gpus"} // []
l!l
5738
MC/DC100%1/1
$opt{"gpus"} // []
$opt{"gpus"}
Truth table100%2/2
A
A= $opt{"gpus"}
Aresult
11
00
148 my @extra = defined $opt{extra_requirement}
149 581 ▶ ? ( extra_requirement => $opt{extra_requirement} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'extra_requirement'} ? : 6 575 581
150 581 ▶ push @extra, ( nvswitches => $opt{nvswitches} ) if defined $opt{nvswitches};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"nvswitches"} 37 544 581
151 581 my $chosen = $class->custom_setup($opt{setup});
152 579 ▶ Rex::Logger::info('NVIDIA driver setup: '.( ref $chosen ? ref($chosen).' object' : $chosen ))
Branch: 2 branches
BranchTrueFalseTotal
if $chosen 28 551 579
ref $chosen ? : 11 17 28
153 if $chosen;
154 579 ▶ return $chosen->adopt(gpus => $gpus, @extra) if blessed $chosen;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($chosen) 11 568 579
155 568 ▶ my $setup_class = $chosen // $class->setup_class_for_os // return;
Condition100%5/5
($chosen // $class->setup_class_for_os) // (return)
l!l
5644
$chosen // $class->setup_class_for_os
l!l&&r!l&&!r
175474
MC/DC100%2/2
($chosen // $class->setup_class_for_os) // (return)
$chosen $class->setup_class_for_os
Truth table100%3/3
A // B
A= $chosenB= $class->setup_class_for_os
ABresult
1X1
011
000
156 564 return $setup_class->new(gpus => $gpus, @extra);
157 }
158  
159 626 ▶sub custom_setup {
Subroutine custom_setup: called CC=8 SCAR=20.8
Pod documented
160 626 my ( $class, $setup ) = @_;
161 626 my $origin = 'setup =>';
162 626 ▶ unless ($class->_setup_given($setup)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_setup_given($setup)) 589 37 626
163 589 $setup = Rex::Config->get('gpu_nvidia_setup');
164 589 $origin = 'set gpu_nvidia_setup';
165 589 ▶ return unless $class->_setup_given($setup);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_setup_given($setup) 583 6 589
166 }
167 43 my $base = $class->setup_base_class;
168 # The setting is shared by every host of the Rexfile; an object caches one
169 # host's facts (os, kernel, GPUs), so only a class name is taken there.
170 43 ▶ croak 'set gpu_nvidia_setup takes a class name, not '.$setup.': the setting '
Branch: 1 branch
BranchTrueFalseTotal
if ref $setup and $origin ne "setup =>" 1 42 43
Condition100%3/3
ref $setup and $origin ne "setup =>"
!ll&&!rl&&r
29131
MC/DC100%2/2
ref $setup and $origin ne "setup =>"
ref $setup $origin ne "setup =>"
Truth table100%3/3
A and B
A= ref $setupB= $origin ne "setup =>"
ABresult
0X0
100
111
171 .'is shared by every host, and a setup object holds the facts of one. Pass '
172 .'the object per call as setup => instead. Nothing was changed on the host'
173 if ref $setup && $origin ne 'setup =>';
174 42 ▶ if (ref $setup) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $setup) 13 29 42
175 13 ▶ croak 'NVIDIA driver setup from '.$origin.' must be a class name or a '.$base
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($setup) and $setup->isa($base) 2 11 13
Condition100%3/3
builtin::blessed($setup) and $setup->isa($base)
!ll&&!rl&&r
1111
MC/DC100%2/2
builtin::blessed($setup) and $setup->isa($base)
builtin::blessed($setup) $setup->isa($base)
Truth table100%3/3
A and B
A= builtin::blessed($setup)B= $setup->isa($base)
ABresult
0X0
100
111
176 .' object, not '.$setup.'. Nothing was changed on the host'
177 unless blessed($setup) && $setup->isa($base);
178 11 return $setup;
179 }
180 29 return $class->load_user_class($setup, base => $base, what => 'NVIDIA driver setup',
181 origin => $origin, hint => 'extends it, or one of the ::Setup::* classes');
182 }
183  
184  
185 49 ▶sub setup_base_class { 'Rex::GPU::NVIDIA::Setup' }
Subroutine setup_base_class: called CC=1 SCAR=0.0
Pod documented
186  
187 39 ▶sub toolkit_setup_class {
Subroutine toolkit_setup_class: called CC=3 SCAR=11.1
Pod documented
188 39 my ( $class, $family ) = @_;
189 39 ▶ return $family eq 'debian' ? 'Rex::GPU::NVIDIA::Setup::Apt'
Branch: 2 branches
BranchTrueFalseTotal
$family eq 'debian' ? : 31 8 39
$family eq 'suse' ? : 8 0 8
190 : $family eq 'suse' ? 'Rex::GPU::NVIDIA::Setup::SUSE'
191 : undef;
192 }
193  
194  
195 733 ▶sub configured_class {
Subroutine configured_class: called CC=6 SCAR=18.0
Pod documented
196 733 my ( $class, $nvidia ) = @_;
197 733 my $origin = 'nvidia =>';
198 733 ▶ unless (defined $nvidia && $nvidia ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nvidia and $nvidia ne "") 728 5 733
Condition66%2/3
defined $nvidia and $nvidia ne ""
!ll&&!rl&&r
72805
MC/DC50%1/2
defined $nvidia and $nvidia ne ""
defined $nvidia $nvidia ne ""
Truth table66%2/3
A and B
A= defined $nvidiaB= $nvidia ne ""
ABresult
0X0
100
111
199 728 $nvidia = Rex::Config->get('gpu_nvidia_class');
200 728 $origin = 'set gpu_nvidia_class';
201 728 ▶ return __PACKAGE__ unless defined $nvidia && $nvidia ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nvidia and $nvidia ne "" 724 4 728
Condition100%3/3
defined $nvidia and $nvidia ne ""
!ll&&!rl&&r
127124
MC/DC100%2/2
defined $nvidia and $nvidia ne ""
defined $nvidia $nvidia ne ""
Truth table100%3/3
A and B
A= defined $nvidiaB= $nvidia ne ""
ABresult
0X0
100
111
202 }
203 9 ▶ croak 'NVIDIA class from '.$origin.' must be a class name, not '.$nvidia
Branch: 1 branch
BranchTrueFalseTotal
if ref $nvidia 0 9 9
204 .'. Nothing was changed on the host'
205 if ref $nvidia;
206 9 return $class->load_user_class($nvidia,
207 base => __PACKAGE__, what => 'NVIDIA class', origin => $origin);
208 }
209  
210 54 ▶sub load_user_class {
Subroutine load_user_class: called CC=5 SCAR=16.1
Pod documented
211 54 my ( $class, $package, %arg ) = @_;
212 54 my ( $base, $what, $origin ) = @arg{qw( base what origin )};
213 54 ▶ croak "$what from $origin: '$package' is not a Perl package name. "
Branch: 1 branch
BranchTrueFalseTotal
unless &is_module_name($package) 3 51 54
214 .'Nothing was changed on the host'
215 unless is_module_name($package);
216 51 ▶ $class->_load_class($package, $what, $origin) unless $class->_package_defined($package);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_package_defined($package) 11 40 51
217 croak $what.' from '.$origin.': '.$package.' is not a subclass of '.$base
218 42 ▶ .' ('.( $arg{hint} // 'extends it' ).'). Nothing was changed on the host'
Branch: 1 branch
BranchTrueFalseTotal
unless $package->isa($base) 4 38 42
Condition100%2/2
$arg{"hint"} // "extends it"
l!l
22
MC/DC100%1/1
$arg{"hint"} // "extends it"
$arg{"hint"}
Truth table100%2/2
A
A= $arg{"hint"}
Aresult
11
00
219 unless $package->isa($base);
220 38 return $package;
221 }
222  
223 1215 ▶sub _setup_given {
Subroutine _setup_given: called CC=4 SCAR=13.9
224 1215 my ( $class, $setup ) = @_;
225 1215 ▶ return ref $setup || ( defined $setup && $setup ne '' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $setup || defined $setup && $setup ne '' ? : 43 1172 1215
Condition100%6/6
ref $setup or defined $setup and $setup ne ""
l!l&&r!l&&!r
14291172
defined $setup and $setup ne ""
!ll&&!rl&&r
59557729
MC/DC100%3/3
ref $setup or defined $setup and $setup ne ""
ref $setup defined $setup $setup ne ""
Truth table100%4/4
A or B and C
A= ref $setupB= defined $setupC= $setup ne ""
ABCresult
1XX1
0111
00X0
0100
226 }
227  
228 # Is the package there already -- written into the Rexfile, or loaded? Then
229 # it is not looked up as a file (the Rexfile's own package has none).
230 51 ▶sub _package_defined {
Subroutine _package_defined: called CC=6 SCAR=21.5
231 51 my ( $class, $package ) = @_;
232 51 ▶ return 1 if $INC{ module_notional_filename($package) };
Branch: 1 branch
BranchTrueFalseTotal
if $INC{&module_notional_filename($package)} 30 21 51
233 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 21 ▶ return 0 unless %{ $package.'::' };
Branch: 1 branch
BranchTrueFalseTotal
unless %{$package . '::';} 11 10 21
235 10 ▶ return 1 if @{ $package.'::ISA' };
Branch: 1 branch
BranchTrueFalseTotal
if @{$package . '::ISA';} 10 0 10
236 0 ▶ return ( grep { !/::\z/ && defined &{ $package.'::'.$_ } } keys %{ $package.'::' } ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({not /::\z/u and defined &{$package . '::' . $_;};} keys %{$package . '::';}) ? : 0 0 0
Condition0%0/3
!/::\z/u && defined &{$package . '::' . $_;}
!ll&&!rl&&r
000
MC/DC0%0/2
!/::\z/u && defined &{$package . '::' . $_;}
!/::\z/u defined &{$package . '::' . $_;}
Truth table0%0/3
not(A) && B
A= !/::\z/uB= defined &{$package . '::' . $_;}
ABresult
0X0
100
111
237 }
238  
239 11 ▶sub _load_class {
Subroutine _load_class: called CC=3 SCAR=11.0
240 11 my ( $class, $package, $what, $origin ) = @_;
241 11 ▶ return if eval { use_module($package); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &use_module($package); 1 } } 2 9 11
242 9 my $error = $@;
243 9 my $file = module_notional_filename($package);
244 9 ▶ croak $what.' from '.$origin.': '.$package.' not found -- no '.$file
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ /^Can't locate \Q$file\E in \@INC/u 8 1 9
245 .' in @INC. Put it at lib/'.$file.' next to your Rexfile (Rex adds that lib/ to '
246 .'@INC) or define the package in the Rexfile. Nothing was changed on the host'
247 if $error =~ /^Can't locate \Q$file\E in \@INC/;
248 1 $error =~ s/\s+\z//;
249 1 croak $what.' from '.$origin.': loading '.$package.' failed: '.$error
250 .' -- Nothing was changed on the host';
251 }
252  
253 # Called as a method (Rex::GPU::NVIDIA->install_driver, My::GPU::NVIDIA->...):
254 # the invocant, a class name or object that isa Rex::GPU::NVIDIA. Called as a
255 # function (install_driver(...) from a Rexfile, Rex::GPU::NVIDIA::install_driver
256 # from Rex::Rancher, configure_containerd('rke2')): undef -- a first argument
257 # such as 'reboot' or 'rke2' is no package of ours, and nothing is loaded to
258 # find out. The public functions then pass the call on, unchanged, to
259 # configured_class as a method.
260 1736 ▶sub _invocant {
Subroutine _invocant: called CC=5 SCAR=16.5
261 1736 my ( $first ) = @_;
262 1736 ▶ return unless defined $first && ( !ref $first || blessed $first );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and !ref($first) || builtin::blessed($first) 81 1655 1736
Condition50%3/6
defined $first and !ref($first) || builtin::blessed($first)
!ll&&!rl&&r
8101655
not ref $first or builtin::blessed($first)
l!l&&r!l&&!r
165500
MC/DC33%1/3
defined $first and !ref($first) || builtin::blessed($first)
defined $first not ref $first builtin::blessed($first)
Truth table50%2/4
A and not(B) or C
A= defined $firstB= not ref $firstC= builtin::blessed($first)
ABCresult
0XX0
1000
11X1
1011
263 1655 ▶ return $first if eval { $first->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $first->isa(__PACKAGE__) } } 1048 607 1655
264 607 return;
265 }
266  
267  
268 146 ▶sub install_container_toolkit {
Subroutine install_container_toolkit: called CC=14 SCAR=26.4
Pod documented
269 146 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->install_container_toolkit(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->install_container_toolkit(@_))
l!l
7472
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->install_container_toolkit(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
270 74 my ( undef, @args ) = @_;
271  
272 # karr #74: the one option, checked before the host is asked anything. A
273 # misspelt binaries_suffice would otherwise fall through to the repository
274 # setup it exists to avoid.
275 74 ▶ die "install_container_toolkit: options are key => value pairs\n" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 73 74
276 73 my %opts = @args;
277 73 my @unknown = grep { $_ ne 'binaries_suffice' } sort keys %opts;
278 73 ▶ die 'install_container_toolkit: unknown option '.join(', ', @unknown)." (valid: binaries_suffice)\n"
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 2 71 73
279 if @unknown;
280  
281 # No package manager is asked, so no OS family is needed either.
282 71 ▶ return if $opts{binaries_suffice} && $class->_toolkit_binaries_present;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"binaries_suffice"} and $class->_toolkit_binaries_present 8 63 71
Condition100%3/3
$opts{"binaries_suffice"} and $class->_toolkit_binaries_present
!ll&&!rl&&r
5858
MC/DC100%2/2
$opts{"binaries_suffice"} and $class->_toolkit_binaries_present
$opts{"binaries_suffice"} $class->_toolkit_binaries_present
Truth table100%3/3
A and B
A= $opts{"binaries_suffice"}B= $class->_toolkit_binaries_present
ABresult
0X0
100
111
283  
284 63 my $os = operating_system();
285  
286 63 ▶ my $family = is_debian() ? 'debian'
Branch: 4 branches
BranchTrueFalseTotal
is_debian() ? : 33 30 63
is_redhat() ? : 12 18 30
is_suse() ? : 9 9 18
$class->_rhel_family_name($os) ? : 4 5 9
287 : is_redhat() ? 'redhat'
288 : is_suse() ? 'suse'
289 : $class->_rhel_family_name($os) ? 'redhat'
290 : undef;
291 63 ▶ die "Unsupported OS for NVIDIA Container Toolkit: $os\n" unless $family;
Branch: 1 branch
BranchTrueFalseTotal
unless $family 5 58 63
292  
293 58 ▶ return if $class->_toolkit_present($family);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_toolkit_present($family) 4 54 58
294  
295 54 Rex::Logger::info("Installing NVIDIA Container Toolkit");
296  
297 54 ▶ if ($family eq 'debian') {
Branch: 2 branches
BranchTrueFalseTotal
if ($family eq 'debian') { } 31 23 54
elsif ($family eq 'redhat') { } 15 8 23
298 31 $class->_install_toolkit_debian;
299 }
300 elsif ($family eq 'redhat') {
301 15 $class->_install_toolkit_redhat;
302 }
303 else {
304 8 $class->_install_toolkit_suse;
305 }
306  
307 42 Rex::Logger::info("NVIDIA Container Toolkit installed");
308 }
309  
310  
311 # The runtime names configure_containerd knows; gpu_setup also takes 'none'
312 # (karr #66).
313 36 our @CONTAINERD_RUNTIMES = qw( rke2 k3s containerd );
314  
315 66 ▶sub _check_containerd_runtime {
Subroutine _check_containerd_runtime: called CC=2 SCAR=6.9
316 66 my ( $class, $runtime, @also ) = @_;
317 66 my @valid = (@CONTAINERD_RUNTIMES, @also);
318 66 ▶ return if grep { $_ eq $runtime } @valid;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $runtime;} @valid 58 8 66
319 8 die "Unknown containerd runtime: $runtime (valid: " . join(', ', @valid) . ")\n";
320 }
321  
322 56 ▶sub configure_containerd {
Subroutine configure_containerd: called CC=6 SCAR=17.9
Pod documented
323 56 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->configure_containerd(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->configure_containerd(@_))
l!l
2927
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->configure_containerd(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
324 29 my ( undef, $runtime ) = @_;
325 29 ▶ $runtime //= 'rke2';
Condition100%2/2
$runtime //= "rke2"
l!l
272
MC/DC100%1/1
$runtime //= "rke2"
$runtime
Truth table100%2/2
A
A= $runtime
Aresult
11
00
326  
327 # The name first (karr #66): without the runtime binary a typo would
328 # otherwise return quietly below.
329 29 $class->_check_containerd_runtime($runtime);
330  
331 24 ▶ return unless can_run("nvidia-container-runtime");
Branch: 1 branch
BranchTrueFalseTotal
unless can_run("nvidia-container-runtime") 4 20 24
332  
333 20 Rex::Logger::info("Configuring containerd for NVIDIA GPU (runtime: $runtime)");
334  
335 20 ▶ if ($runtime eq 'rke2' || $runtime eq 'k3s') {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime eq 'rke2' or $runtime eq 'k3s') { } 18 2 20
Condition100%3/3
$runtime eq "rke2" or $runtime eq "k3s"
l!l&&r!l&&!r
1172
MC/DC100%2/2
$runtime eq "rke2" or $runtime eq "k3s"
$runtime eq "rke2" $runtime eq "k3s"
Truth table100%3/3
A or B
A= $runtime eq "rke2"B= $runtime eq "k3s"
ABresult
1X1
011
000
336 18 $class->_configure_containerd_rke2($runtime);
337 }
338 else {
339 2 $class->_configure_containerd_standalone;
340 }
341  
342 20 Rex::Logger::info("Containerd configured with NVIDIA runtime");
343 }
344  
345  
346 18 ▶sub verify_nvidia {
Subroutine verify_nvidia: called CC=4 SCAR=13.9
Pod documented
347 18 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->verify_nvidia(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->verify_nvidia(@_))
l!l
99
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->verify_nvidia(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
348 9 Rex::Logger::info("Verifying NVIDIA installation...");
349 9 my $ok = $class->_verify_module_and_smi;
350  
351 9 ▶ if (can_run("nvidia-ctk")) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_run('nvidia-ctk')) { } 8 1 9
352 8 Rex::Logger::info(" [ok] nvidia-container-toolkit installed");
353 }
354 else {
355 1 Rex::Logger::info("nvidia-container-toolkit not found", "warn");
356 1 $ok = 0;
357 }
358  
359 # Warns only, never changes $ok (karr #79).
360 9 $class->_warn_old_crun;
361  
362 9 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 8 9
363 1 Rex::Logger::info("GPU verification incomplete — some features may not work until reboot", "warn");
364 }
365  
366 9 return $ok;
367 }
368  
369  
370 357 ▶sub verify_nvidia_driver {
Subroutine verify_nvidia_driver: called CC=5 SCAR=16.1
Pod documented
371 357 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->verify_nvidia_driver(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->verify_nvidia_driver(@_))
l!l
3552
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->verify_nvidia_driver(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
372 355 my ( undef, %opts ) = @_;
373 355 ▶ my $setup = $opts{setup} // $class->setup_base_class;
Condition66%2/3
$opts{"setup"} // $class->setup_base_class
l!l&&r!l&&!r
35320
MC/DC0%0/2
$opts{"setup"} // $class->setup_base_class
$opts{"setup"} $class->setup_base_class
Truth table66%2/3
A // B
A= $opts{"setup"}B= $class->setup_base_class
ABresult
1X1
011
000
374 355 Rex::Logger::info("Verifying NVIDIA driver...");
375 355 my $ok = $class->_verify_module_and_smi;
376  
377 355 run($setup->libcuda_command, auto_die => 0);
378 355 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 2 353 355
379 2 Rex::Logger::info(" [ok] libcuda.so.1 in the linker cache");
380 }
381 else {
382 353 Rex::Logger::info("libcuda.so.1 not in the linker cache (ldconfig -p) — CUDA programs "
383 ."cannot run, and the next install_driver installs again", "warn");
384 353 $ok = 0;
385 }
386  
387 355 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 354 1 355
388 354 Rex::Logger::info("NVIDIA driver verification incomplete — some features may not work until reboot", "warn");
389 }
390  
391 355 return $ok;
392 }
393  
394 # karr #79: a crun older than 1.15 (1.14.1 seen broken live) rejects the OCI
395 # spec once the GPU Operator's toolkit makes the crun runtime first ("unknown
396 # version specified"): no GPU sandbox starts. Warning only, nothing is changed;
397 # no crun on the host => silent. The bound is conservative, only 1.14.1 is
398 # known broken. Compared per numeric component (1.9 < 1.15), not as strings.
399 9 ▶sub _warn_old_crun {
Subroutine _warn_old_crun: called CC=9 SCAR=22.1
400 9 my ( $class ) = @_;
401 9 ▶ return unless can_run("crun");
Branch: 1 branch
BranchTrueFalseTotal
unless can_run("crun") 3 6 9
402 6 my $out = run "crun --version 2>&1", auto_die => 0;
403 6 ▶ return unless defined $out && $out =~ /\bcrun version (\d+(?:\.\d+)*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out and $out =~ /\bcrun version (\d+(?:\.\d+)*)/u 0 6 6
Condition33%1/3
defined $out and $out =~ /\bcrun version (\d+(?:\.\d+)*)/u
!ll&&!rl&&r
006
MC/DC0%0/2
defined $out and $out =~ /\bcrun version (\d+(?:\.\d+)*)/u
defined $out $out =~ /\bcrun version (\d+(?:\.\d+)*)/u
Truth table33%1/3
A and B
A= defined $outB= $out =~ /\bcrun version (\d+(?:\.\d+)*)/u
ABresult
0X0
100
111
404 6 my $version = $1;
405 6 my @have = split /\./, $version;
406 6 my @min = ( 1, 15 );
407 6 my $older = 0;
408 6 for my $i ( 0 .. $#min ) {
409 11 ▶ my $h = $have[$i] // 0;
Condition50%1/2
$have[$i] // 0
l!l
110
MC/DC0%0/1
$have[$i] // 0
$have[$i]
Truth table50%1/2
A
A= $have[$i]
Aresult
11
00
410 11 ▶ last if $h > $min[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $h > $min[$i] 2 9 11
411 9 ▶ if ($h < $min[$i]) { $older = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($h < $min[$i]) 2 7 9
412 }
413 6 ▶ return unless $older;
Branch: 1 branch
BranchTrueFalseTotal
unless $older 4 2 6
414 2 Rex::Logger::info("crun $version found: older than 1.15 (known broken: 1.14.1). "
415 ."With the GPU Operator's NVIDIA toolkit making the crun runtime first, "
416 ."GPU sandboxes fail with 'OCI runtime create failed: unknown version specified'. "
417 ."Update or remove crun, or keep runc as the first runtime. Nothing was changed.", "warn");
418 2 return;
419 }
420  
421 # The kernel module and nvidia-smi checks both verify_* share; warns per
422 # failure, returns 1/0.
423 364 ▶sub _verify_module_and_smi {
Subroutine _verify_module_and_smi: called CC=7 SCAR=19.7
424 364 my ( $class ) = @_;
425 364 my $ok = 1;
426  
427 364 my $lsmod = run "lsmod | grep '^nvidia '", auto_die => 0;
428 364 ▶ if ($? != 0 || !$lsmod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0 or not $lsmod) { } 353 11 364
Condition66%2/3
$? != 0 or not $lsmod
l!l&&r!l&&!r
353011
MC/DC50%1/2
$? != 0 or not $lsmod
$? != 0 not $lsmod
Truth table66%2/3
A or not(B)
A= $? != 0B= not $lsmod
ABresult
1X1
011
000
429 353 Rex::Logger::info("nvidia kernel module not loaded (reboot may be needed)", "warn");
430 353 $ok = 0;
431 }
432 else {
433 11 Rex::Logger::info(" [ok] nvidia kernel module loaded");
434 }
435  
436 364 my $smi = run "nvidia-smi -L 2>&1", auto_die => 0;
437 364 ▶ chomp $smi if defined $smi;
Branch: 1 branch
BranchTrueFalseTotal
if defined $smi 364 0 364
438 364 ▶ if (defined $smi && $smi =~ /GPU \d+:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $smi and $smi =~ /GPU \d+:/u) { } 23 341 364
Condition66%2/3
defined $smi and $smi =~ /GPU \d+:/u
!ll&&!rl&&r
034123
MC/DC50%1/2
defined $smi and $smi =~ /GPU \d+:/u
defined $smi $smi =~ /GPU \d+:/u
Truth table66%2/3
A and B
A= defined $smiB= $smi =~ /GPU \d+:/u
ABresult
0X0
100
111
439 23 Rex::Logger::info(" [ok] $smi");
440 }
441 else {
442 341 ▶ Rex::Logger::info("nvidia-smi not working: " . ($smi // 'no output'), "warn");
Condition50%1/2
$smi // "no output"
l!l
3410
MC/DC0%0/1
$smi // "no output"
$smi
Truth table50%1/2
A
A= $smi
Aresult
11
00
443 341 $ok = 0;
444 }
445  
446 364 return $ok;
447 }
448  
449 # NVSwitch host (karr #23) or HGX B200/B300 (karr #56): is Fabric Manager
450 # running? Warns, never dies, like verify_nvidia. Returns 1/0.
451 37 ▶sub _check_fabric_manager {
Subroutine _check_fabric_manager: called CC=2 SCAR=6.9
452 37 my ( $class, $setup ) = @_;
453 37 my $unit = $setup->fabric_manager_service;
454 37 my $label = $setup->fabric_label;
455 37 run "systemctl is-active --quiet $unit", auto_die => 0;
456 37 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 33 4 37
457 33 Rex::Logger::info(" [ok] $unit active ($label)");
458 33 return 1;
459 }
460 4 Rex::Logger::info("$label present but $unit is not active: CUDA fails with "
461 ."cudaErrorSystemNotReady until NVIDIA Fabric Manager of the driver's exact version runs. "
462 ."After the reboot that loads the NVIDIA driver: systemctl start $unit; if it is not "
463 ."installed (install_driver installs it with the driver, or for an existing driver only "
464 ."from the host's own package sources), install it yourself", "warn");
465 4 return 0;
466 }
467  
468 # NVLink platforms Rex::GPU does not set up (karr #49), by GPU device ID
469 # (Setup nvlink_platforms): a note only, nothing installed, verify unaffected,
470 # no host command. HGX B200/B300 are set up since karr #56 (Setup
471 # install_nvlink_fabric / check_nvlink_fabric), so only NVL72 is left here.
472 411 ▶sub _note_nvlink_platforms {
Subroutine _note_nvlink_platforms: called CC=3 SCAR=11.0
473 411 my ( $class, $setup ) = @_;
474 411 for my $platform ($setup->nvlink_platforms) {
475 13 ▶ if ($platform eq 'nvl72') {
Branch: 1 branch
BranchTrueFalseTotal
if ($platform eq "nvl72") 1 12 13
476 1 Rex::Logger::info("GB200/GB300 NVL72 compute tray: multi-node NVLink needs nvidia-imex "
477 ."and its configuration -- not part of Rex::GPU");
478 }
479 }
480 411 return;
481 }
482  
483 # ============================================================
484 # Container toolkit installation
485 # ============================================================
486  
487 # karr #42: a toolkit that is already there -- a re-run, or an image that
488 # ships it (DGX OS installs it from NVIDIA's apt repository) -- is left as it
489 # is. Both signals are required: nvidia-ctk runs, AND the package manager has
490 # the package. nvidia-ctk alone is not enough: configure_containerd points
491 # containerd at /usr/bin/nvidia-container-runtime, which only the package
492 # guarantees, and a copy unpacked outside the package manager (the GPU
493 # Operator's /usr/local/nvidia/toolkit) must still get the package. dpkg
494 # 'hi' (held, installed) counts as installed.
495 58 ▶sub _toolkit_present {
Subroutine _toolkit_present: called CC=7 SCAR=19.5
496 58 my ( $class, $family ) = @_;
497 58 ▶ return 0 unless can_run("nvidia-ctk");
Branch: 1 branch
BranchTrueFalseTotal
unless can_run("nvidia-ctk") 51 7 58
498 7 my $version = run "nvidia-ctk --version 2>&1", auto_die => 0;
499 7 ▶ return 0 if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 6 7
500 6 ▶ ($version) = split /\n/, ($version // '');
Condition50%1/2
$version // ""
l!l
60
MC/DC0%0/1
$version // ""
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
501 6 ▶ $version //= 'nvidia-ctk';
Condition50%1/2
$version //= "nvidia-ctk"
l!l
60
MC/DC0%0/1
$version //= "nvidia-ctk"
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
502 6 ▶ run(($family eq 'debian'
Branch: 1 branch
BranchTrueFalseTotal
$family eq 'debian' ? : 3 3 6
503 ? "dpkg -l nvidia-container-toolkit 2>/dev/null | grep -q '^[hi]i'"
504 : "rpm -q nvidia-container-toolkit 2>&1"),
505 auto_die => 0);
506 6 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 4 6
507 2 Rex::Logger::info("nvidia-ctk runs ($version) but the nvidia-container-toolkit package is not installed — installing it");
508 2 return 0;
509 }
510 4 Rex::Logger::info("NVIDIA Container Toolkit already present — skipping repository setup and install ($version)");
511 4 return 1;
512 }
513  
514 # karr #74, binaries_suffice: the binaries a CRI execs, whoever put them
515 # there -- a vendor image (DGX Spark) ships them outside the package manager,
516 # and RKE2/K3s find nvidia-container-runtime on PATH themselves. The package
517 # manager is not asked. `command -v` through run, as Detect's _has_lspci (karr
518 # #46), not can_run: can_run runs the same `command -v` but then stats the
519 # path through the file interface (SFTP on SSH/OpenSSH); run answers under
520 # the PATH the later `run "nvidia-ctk ..."` calls get. nvidia-ctk must run,
521 # as in _toolkit_present (karr #42): a broken one is no toolkit. Anything
522 # less returns 0, and install_container_toolkit goes on as without the
523 # option.
524 11 ▶sub _toolkit_binaries_present {
Subroutine _toolkit_binaries_present: called CC=9 SCAR=22.1
525 11 my ( $class ) = @_;
526 11 my %path;
527 11 for my $bin (qw( nvidia-container-runtime nvidia-ctk )) {
528 20 my $out = run "command -v $bin 2>/dev/null", auto_die => 0;
529 20 my $found = $? == 0;
530 20 ▶ ( $path{$bin} ) = split /\n/, ( $out // '' );
Condition50%1/2
$out // ""
l!l
200
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
531 20 ▶ next if $found && defined $path{$bin} && length $path{$bin};
Branch: 1 branch
BranchTrueFalseTotal
if $found and defined $path{$bin} and length $path{$bin} 17 3 20
Condition83%5/6
$found and defined $path{$bin} and length $path{$bin}
!ll&&!rl&&r
3017
$found and defined $path{$bin}
!ll&&!rl&&r
2117
MC/DC66%2/3
$found and defined $path{$bin} and length $path{$bin}
$found defined $path{$bin} length $path{$bin}
Truth table75%3/4
A and B and C
A= $foundB= defined $path{$bin}C= length $path{$bin}
ABCresult
0XX0
10X0
1100
1111
532 3 Rex::Logger::info("binaries_suffice: $bin not found on the PATH — checking for the nvidia-container-toolkit package as without the option");
533 3 return 0;
534 }
535 8 my $version = run "nvidia-ctk --version 2>&1", auto_die => 0;
536 8 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 7 8
537 1 Rex::Logger::info("binaries_suffice: $path{'nvidia-ctk'} does not run (nvidia-ctk --version failed) — checking for the nvidia-container-toolkit package as without the option", "warn");
538 1 return 0;
539 }
540 7 ▶ ($version) = split /\n/, ($version // '');
Condition50%1/2
$version // ""
l!l
70
MC/DC0%0/1
$version // ""
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
541 7 ▶ $version //= 'nvidia-ctk';
Condition50%1/2
$version //= "nvidia-ctk"
l!l
70
MC/DC0%0/1
$version //= "nvidia-ctk"
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
542 7 Rex::Logger::info("NVIDIA Container Toolkit already present (binaries_suffice: $path{'nvidia-container-runtime'}, $path{'nvidia-ctk'}, $version) — skipping repository setup and install; the package manager was not asked");
543 7 return 1;
544 }
545  
546 31 ▶sub _install_toolkit_debian {
Subroutine _install_toolkit_debian: called CC=1 SCAR=0.0
547 31 my ( $class ) = @_;
548 31 my $keyring = '/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg';
549  
550 # The driver path's apt layer (karr #37): the apt timers stopped before the
551 # first apt-get, DPkg::Lock::Timeout on every one, install run directly and
552 # dpkg -l ^ii as the only evidence. After the first-deploy reboot
553 # apt-daily/cloud-init hold the dpkg lock; Rex::Pkg (pkg) has no lock
554 # timeout and stopping the timers does not release a lock cloud-init holds,
555 # so curl/gnupg go through apt-get too. --no-upgrade keeps pkg's
556 # ensure => present meaning: an installed curl or gnupg is not upgraded.
557 31 my $apt = $class->toolkit_setup_class('debian')->new;
558 31 $apt->prepare_host({ packages => [ 'curl', 'gnupg', 'nvidia-container-toolkit' ] });
559 31 $apt->run_cmd('DEBIAN_FRONTEND=noninteractive '.$apt->apt_get.' install -y --no-upgrade curl gnupg',
560 auto_die => 0);
561 31 $apt->verify_packages({ verify => [ 'curl', 'gnupg' ] });
562  
563 30 $class->_install_toolkit_keyring($keyring);
564  
565 27 file "/etc/apt/sources.list.d/nvidia-container-toolkit.list",
566 content => 'deb [signed-by='.$keyring.'] https://nvidia.github.io/libnvidia-container/stable/deb/$(ARCH) /' . "\n";
567  
568 27 $apt->prepare_source({});
569 27 $apt->install_packages({ packages => [ 'nvidia-container-toolkit' ] });
570 27 $apt->verify_packages({ verify => [ 'nvidia-container-toolkit' ] });
571 }
572  
573 # karr #38: gpg --dearmor -o on an existing file without --yes fails with no
574 # TTY, so a re-run kept the old key forever and the error was swallowed. The
575 # key is dearmored to a temporary file that replaces the keyring only when it
576 # is non-empty (gpg can leave an empty file on bad input), readable by apt's
577 # _apt sandbox user; any failure dies instead of carrying on to an apt-get
578 # update that cannot verify the repository.
579 30 ▶sub _install_toolkit_keyring {
Subroutine _install_toolkit_keyring: called CC=4 SCAR=13.9
580 30 my ( $class, $keyring ) = @_;
581 30 my $asc = "$keyring.asc.tmp";
582 30 my $tmp = "$keyring.tmp";
583  
584 30 run "curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey -o $asc", auto_die => 0;
585 30 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 29 30
586 1 run "rm -f $asc", auto_die => 0;
587 1 die "Could not download the NVIDIA Container Toolkit GPG key (https://nvidia.github.io/libnvidia-container/gpgkey)\n";
588 }
589 29 run "gpg --batch --yes --dearmor -o $tmp $asc && test -s $tmp && chmod 0644 $tmp && mv -f $tmp $keyring",
590 auto_die => 0;
591 29 my $failed = $? != 0;
592 29 run "rm -f $asc $tmp", auto_die => 0;
593 29 ▶ die "Could not dearmor the NVIDIA Container Toolkit GPG key into $keyring\n" if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 1 28 29
594  
595 28 run "test -s $keyring", auto_die => 0;
596 28 ▶ die "NVIDIA Container Toolkit keyring $keyring is missing or empty\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 27 28
597 }
598  
599 # karr #44, the k38 pattern for the dnf .repo: `curl -s -L | tee` wrote an
600 # HTTP error page into /etc/yum.repos.d and dnf failed later with a parse
601 # error. curl -f fails on an HTTP error; the download lands in a .repo.tmp
602 # next to the target (dnf reads only *.repo) and replaces it only when it has
603 # the [nvidia-container-toolkit] section. Every failure dies, leaving an
604 # existing .repo as it was.
605 15 ▶sub _install_toolkit_repo_file {
Subroutine _install_toolkit_repo_file: called CC=3 SCAR=11.0
606 15 my ( $class, $repo ) = @_;
607 15 my $url = 'https://nvidia.github.io/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo';
608 15 my $tmp = "$repo.tmp";
609  
610 15 run "curl -fsSL $url -o $tmp", auto_die => 0;
611 15 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 14 15
612 1 run "rm -f $tmp", auto_die => 0;
613 1 die "Could not download the NVIDIA Container Toolkit repository file ($url)\n";
614 }
615 14 run "grep -q '^\\[nvidia-container-toolkit\\]' $tmp && chmod 0644 $tmp && mv -f $tmp $repo",
616 auto_die => 0;
617 14 my $failed = $? != 0;
618 14 run "rm -f $tmp", auto_die => 0;
619 14 ▶ die "The NVIDIA Container Toolkit repository file from $url has no [nvidia-container-toolkit] section or could not be moved into place; $repo was not changed\n"
Branch: 1 branch
BranchTrueFalseTotal
if $failed 1 13 14
620 if $failed;
621 }
622  
623 15 ▶sub _install_toolkit_redhat {
Subroutine _install_toolkit_redhat: called CC=2 SCAR=6.9
624 15 my ( $class ) = @_;
625 15 $class->_install_toolkit_repo_file('/etc/yum.repos.d/nvidia-container-toolkit.repo');
626 13 run "dnf clean expire-cache", auto_die => 0;
627 13 run "dnf install -y nvidia-container-toolkit", auto_die => 0;
628 13 my $check = run "rpm -q nvidia-container-toolkit 2>&1", auto_die => 0;
629 13 ▶ die "nvidia-container-toolkit not installed\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 2 11 13
630 }
631  
632 8 ▶sub _install_toolkit_suse {
Subroutine _install_toolkit_suse: called CC=3 SCAR=11.0
633 8 my ( $class ) = @_;
634 8 my $suse = $class->toolkit_setup_class('suse');
635 # The .repo file URL is yum/dnf format — zypper needs the baseurl directly.
636 # Remove any stale entry (possibly added with the wrong URL) before re-adding.
637 8 my $arch = run "uname -m", auto_die => 0;
638 8 chomp $arch;
639 8 ▶ $arch ||= 'x86_64';
Condition50%1/2
$arch ||= "x86_64"
l!l
80
MC/DC0%0/1
$arch ||= "x86_64"
$arch
Truth table50%1/2
A
A= $arch
Aresult
11
00
640  
641 8 run "rpm --import https://nvidia.github.io/libnvidia-container/gpgkey 2>/dev/null",
642 auto_die => 0;
643 # karr #52: rr + addrepo + refresh, dying on a failed addrepo or refresh.
644 8 $suse->add_repo('nvidia-container-toolkit',
645 "https://nvidia.github.io/libnvidia-container/stable/rpm/$arch");
646  
647 # zypper's exit code is not the evidence (karr #27): rpm -q is, as on RHEL.
648 # karr #53: waits for the zypp lock, as add_repo does.
649 6 my $zypper = $suse->zypper;
650 6 run "$zypper install -y nvidia-container-toolkit", auto_die => 0;
651 6 my $check = run "rpm -q nvidia-container-toolkit 2>&1", auto_die => 0;
652 6 ▶ die "nvidia-container-toolkit not installed\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 5 6
653 }
654  
655 # ============================================================
656 # Containerd configuration
657 # ============================================================
658  
659 21 ▶sub _rke2_base_dir {
Subroutine _rke2_base_dir: called CC=3 SCAR=11.0
660 21 my ( $class, $runtime ) = @_;
661 21 ▶ $runtime //= 'rke2';
Condition100%2/2
$runtime //= "rke2"
l!l
201
MC/DC100%1/1
$runtime //= "rke2"
$runtime
Truth table100%2/2
A
A= $runtime
Aresult
11
00
662 21 ▶ my $dist = ($runtime eq 'k3s') ? 'k3s' : 'rke2';
Branch: 1 branch
BranchTrueFalseTotal
$runtime eq 'k3s' ? : 8 13 21
663 21 return "/var/lib/rancher/$dist/agent/etc/containerd";
664 }
665  
666 # Decide, from the effective containerd state, HOW to register the nvidia
667 # runtime. Pure (regex + booleans only, no I/O) so it is unit-testable offline.
668 #
669 # config => contents of the RKE2/K3s-generated config.toml (or undef)
670 # has_v3_tmpl => a config-v3.toml.tmpl base template is present
671 # has_v3_dir => a config-v3.toml.d/ drop-in directory is present
672 #
673 # Returns one of:
674 # 'present' — the distro already wired an nvidia runtime (leave it alone)
675 # 'v3' — modern containerd 2.x / config v3: use an additive drop-in
676 # 'v2' — legacy containerd 1.x / config v2: extend the base template
677 #
678 # Ordering is load-bearing: a distro that auto-detected nvidia-container-runtime
679 # on PATH wires the runtime itself, so 'present' must win before any write path.
680 24 ▶sub _containerd_nvidia_action {
Subroutine _containerd_nvidia_action: called CC=9 SCAR=22.0
681 24 my ( $class, %s ) = @_;
682 24 ▶ my $config = $s{config} // '';
Condition100%2/2
$s{"config"} // ""
l!l
231
MC/DC100%1/1
$s{"config"} // ""
$s{"config"}
Truth table100%2/2
A
A= $s{"config"}
Aresult
11
00
683  
684 # Already wired (RKE2/K3s auto-detect, or a prior additive drop-in) — no-op.
685 24 ▶ return 'present' if $config =~ /runtimes\.'?nvidia'?[.\]]/;
Branch: 1 branch
BranchTrueFalseTotal
if $config =~ /runtimes\.'?nvidia'?[.\]]/u 4 20 24
686  
687 # Modern config v3: additive drop-in in config-v3.toml.d/.
688 20 ▶ return 'v3' if $s{has_v3_tmpl} || $s{has_v3_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $s{"has_v3_tmpl"} or $s{"has_v3_dir"} 2 18 20
Condition100%3/3
$s{"has_v3_tmpl"} or $s{"has_v3_dir"}
l!l&&r!l&&!r
1118
MC/DC100%2/2
$s{"has_v3_tmpl"} or $s{"has_v3_dir"}
$s{"has_v3_tmpl"} $s{"has_v3_dir"}
Truth table100%3/3
A or B
A= $s{"has_v3_tmpl"}B= $s{"has_v3_dir"}
ABresult
1X1
011
000
689 18 ▶ return 'v3' if $config =~ /^\s*version\s*=\s*3\b/m;
Branch: 1 branch
BranchTrueFalseTotal
if $config =~ /^\s*version\s*=\s*3\b/mu 3 15 18
690 15 ▶ return 'v3' if $config =~ /config-v3\.toml\.d/;
Branch: 1 branch
BranchTrueFalseTotal
if $config =~ /config-v3\.toml\.d/u 2 13 15
691  
692 # Legacy config v2 (containerd 1.x): base-extending config.toml.tmpl.
693 13 ▶ return 'v2'
Branch: 1 branch
BranchTrueFalseTotal
if $config =~ /^\s*version\s*=\s*2\b/mu or $config =~ /io\.containerd\.grpc\.v1\.cri/u 5 8 13
Condition66%2/3
$config =~ /^\s*version\s*=\s*2\b/mu or $config =~ /io\.containerd\.grpc\.v1\.cri/u
l!l&&r!l&&!r
508
MC/DC50%1/2
$config =~ /^\s*version\s*=\s*2\b/mu or $config =~ /io\.containerd\.grpc\.v1\.cri/u
$config =~ /^\s*version\s*=\s*2\b/mu $config =~ /io\.containerd\.grpc\.v1\.cri/u
Truth table66%2/3
A or B
A= $config =~ /^\s*version\s*=\s*2\b/muB= $config =~ /io\.containerd\.grpc\.v1\.cri/u
ABresult
1X1
011
000
694 if $config =~ /^\s*version\s*=\s*2\b/m
695 || $config =~ /io\.containerd\.grpc\.v1\.cri/;
696  
697 # No generated config yet and no version markers: default to the modern
698 # v3 drop-in (the current RKE2/K3s norm). Caller warns; see POD.
699 8 return 'v3';
700 }
701  
702 # Modern (containerd 2.x / config v3) additive drop-in. RKE2/K3s import
703 # config-v3.toml.d/*.toml into their generated config.toml, so this ADDS the
704 # nvidia runtime without touching the base — SystemdCgroup, the pinned sandbox
705 # image, snapshotter opts and the certs.d config_path all survive. Uses the
706 # v3 CRI plugin path (io.containerd.cri.v1.runtime), matching what the distro
707 # auto-wires. SystemdCgroup=true keeps the cgroup driver aligned with kubelet.
708 16 ▶sub _nvidia_containerd_dropin_v3 {
Subroutine _nvidia_containerd_dropin_v3: called CC=1 SCAR=0.0
709 16 my ( $class ) = @_;
710 16 return <<'TOML';
711 version = 3
712  
713 [plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.'nvidia']
714 runtime_type = "io.containerd.runc.v2"
715  
716 [plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.'nvidia'.options]
717 BinaryName = "/usr/bin/nvidia-container-runtime"
718 SystemdCgroup = true
719 TOML
720 }
721  
722 # Legacy (containerd 1.x / config v2) base-extending template. RKE2/K3s render
723 # config.toml.tmpl if present; {{ template "base" . }} emits the full default
724 # config first, then we ADD the nvidia runtime under the v2 CRI plugin path.
725 # NEVER a bare full-config tmpl (that replaced the base and was karr #9).
726 6 ▶sub _nvidia_containerd_tmpl_v2 {
Subroutine _nvidia_containerd_tmpl_v2: called CC=1 SCAR=0.0
727 6 my ( $class ) = @_;
728 6 return <<'TOML';
729 {{ template "base" . }}
730  
731 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes."nvidia"]
732 runtime_type = "io.containerd.runc.v2"
733  
734 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes."nvidia".options]
735 BinaryName = "/usr/bin/nvidia-container-runtime"
736 SystemdCgroup = true
737 TOML
738 }
739  
740 46 ▶sub _path_exists {
Subroutine _path_exists: called CC=2 SCAR=6.9
741 46 my ( $class, $flag, $path ) = @_;
742 46 run "test $flag $path", auto_die => 0;
743 46 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 1 45 46
744 }
745  
746 # Write the modern (config v3) additive nvidia drop-in under $base. Shared by
747 # the normal 'v3' action and the karr #13 clobber-heal path so both emit the
748 # exact same additive wiring. RKE2/K3s import config-v3.toml.d/*.toml into the
749 # config.toml they generate, so this ADDS the nvidia runtime without touching
750 # the base.
751 12 ▶sub _write_nvidia_v3_dropin {
Subroutine _write_nvidia_v3_dropin: called CC=1 SCAR=0.0
752 12 my ( $class, $base ) = @_;
753 12 file "$base/config-v3.toml.d", ensure => 'directory';
754 12 file "$base/config-v3.toml.d/99-nvidia.toml",
755 content => $class->_nvidia_containerd_dropin_v3;
756 12 Rex::Logger::info(
757 " wrote additive nvidia drop-in: $base/config-v3.toml.d/99-nvidia.toml");
758 }
759  
760 # Pure predicate (karr #13): does this config.toml.tmpl content match the EXACT
761 # full-config clobber that rex-gpu 0.002 wrote (pre-karr #9)? That code wrote a
762 # bare template — literally:
763 #
764 # imports = ["/etc/containerd/conf.d/*.toml"]
765 # version = 2
766 #
767 # which REPLACED the RKE2/K3s base config (no `{{ template "base" . }}`, hence
768 # no SystemdCgroup / pinned sandbox image / certs.d config_path in the rendered
769 # config.toml). Matching this — and ONLY this — is what lets the heal remove it
770 # so the distro regenerates its native config.
771 #
772 # Removing a file on a live remote root shell is the top risk here, so the match
773 # is deliberately narrow. Returns 1 ONLY when, ignoring blank lines and #
774 # comments, the content is exactly an `imports =` line PLUS a `version = 2` line
775 # and nothing else. Any `{{ template "base" ... }}` directive (the karr #9
776 # base-extending tmpl, or any base-rendering template) => 0. Any other
777 # substantive line — a [plugins...] section, a real base key, any further
778 # content — means this carries actual config (a user's own tmpl, or something
779 # that is not the bare clobber) => 0. When in doubt, 0.
780 #
781 # Pure (regex/string only, no run/file) so it is unit-testable offline, like
782 # _containerd_nvidia_action / _cdi_managed_source_present /
783 # Rex::GPU::NVIDIA::Setup->_driver_present.
784 33 ▶sub _is_rke2_clobber_tmpl {
Subroutine _is_rke2_clobber_tmpl: called CC=11 SCAR=24.0
785 33 my ( $class, $content ) = @_;
786 33 ▶ return 0 unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 18 15 33
Condition100%3/3
defined $content and length $content
!ll&&!rl&&r
11715
MC/DC100%2/2
defined $content and length $content
defined $content length $content
Truth table100%3/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
787  
788 # The base-extending tmpl (#9) and any base-rendering template carry the
789 # `{{ template "base" . }}` directive — the signature of the SAFE tmpl.
790 15 ▶ return 0 if $content =~ /\{\{\s*template\s+["']base["']/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\{\{\s*template\s+["']base["']/u 2 13 15
791  
792 13 ▶ my @lines = grep { /\S/ && !/^\s*#/ } split /\n/, $content;
Condition100%3/3
/\S/u && !/^\s*#/u
!ll&&!rl&&r
7231
MC/DC100%2/2
/\S/u && !/^\s*#/u
/\S/u !/^\s*#/u
Truth table100%3/3
A && not(B)
A= /\S/uB= !/^\s*#/u
ABresult
0X0
100
111
793 13 ▶ return 0 unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 1 12 13
794  
795 12 my ($imports, $version2) = (0, 0);
796 12 for my $l (@lines) {
797 22 ▶ if ($l =~ /^\s*imports\s*=/) { $imports = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($l =~ /^\s*imports\s*=/u) { } 9 13 22
elsif ($l =~ /^\s*version\s*=\s*2\s*$/u) { } 9 4 13
798 9 elsif ($l =~ /^\s*version\s*=\s*2\s*$/) { $version2 = 1 }
799 4 else { return 0 }
800 }
801 8 ▶ return ($imports && $version2) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$imports && $version2 ? : 6 2 8
Condition100%3/3
$imports and $version2
!ll&&!rl&&r
116
MC/DC100%2/2
$imports and $version2
$imports $version2
Truth table100%3/3
A and B
A= $importsB= $version2
ABresult
0X0
100
111
802 }
803  
804 18 ▶sub _configure_containerd_rke2 {
Subroutine _configure_containerd_rke2: called CC=7 SCAR=19.5
805 18 my ( $class, $runtime ) = @_;
806 18 ▶ $runtime //= 'rke2';
Condition50%1/2
$runtime //= "rke2"
l!l
180
MC/DC0%0/1
$runtime //= "rke2"
$runtime
Truth table50%1/2
A
A= $runtime
Aresult
11
00
807  
808 18 my $base = $class->_rke2_base_dir($runtime);
809 18 my $config_file = "$base/config.toml";
810 18 my $tmpl_file = "$base/config.toml.tmpl";
811  
812 # Heal an EARLIER clobber (karr #13). rex-gpu 0.002 (pre-karr #9) wrote a bare
813 # full-config config.toml.tmpl that REPLACED the RKE2/K3s base config. Detect
814 # and remove THAT exact artifact BEFORE trusting the generated config.toml
815 # below: the generated config IS the clobber's output — it already carries an
816 # nvidia runtime, so the 'already-wired' (present) check would no-op and leave
817 # the clobber (missing SystemdCgroup / pinned sandbox / certs.d) in place
818 # forever. Removing the tmpl is what lets the distro regenerate its native
819 # config on the next restart. _is_rke2_clobber_tmpl matches ONLY the bare
820 # clobber, never the #9 base-extending tmpl or a user's own custom tmpl.
821 18 my $tmpl = run "cat $tmpl_file 2>/dev/null", auto_die => 0;
822 18 ▶ if ($class->_is_rke2_clobber_tmpl($tmpl)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_rke2_clobber_tmpl($tmpl)) 2 16 18
823 2 Rex::Logger::info(
824 " removing legacy rex-gpu full-config clobber $tmpl_file so $runtime "
825 . "regenerates its native containerd config (karr #13)", "warn");
826 2 run "rm -f $tmpl_file", auto_die => 0;
827  
828 # The live config.toml is STILL the clobber's output until $runtime restarts
829 # and regenerates it. We deliberately do NOT restart rke2/k3s here: that
830 # bounces the node's containerd and its workloads as a side effect of GPU
831 # setup, and the node is no worse than before (it was already clobbered).
832 # The heal completes on the next $runtime restart / node reboot — warn so
833 # the operator triggers it. (config.toml is NOT self-healing.)
834 2 Rex::Logger::info(
835 " restart $runtime (or reboot the node) to regenerate the native "
836 . "containerd config — config.toml stays clobbered until then", "warn");
837  
838 # Wire nvidia additively into the config $runtime will regenerate. The
839 # real-world clobber target is modern RKE2/K3s (containerd 2.x / config v3),
840 # whose native config imports config-v3.toml.d/*.toml; a base-extending
841 # config.toml.tmpl would just recreate the file we just removed.
842 2 $class->_write_nvidia_v3_dropin($base);
843 2 return;
844 }
845  
846 # RKE2/K3s regenerate config.toml on every startup; it reflects the effective
847 # merged runtime config, including any nvidia runtime the distro auto-wired
848 # after finding nvidia-container-runtime on PATH.
849 16 my $config = run "cat $config_file 2>/dev/null", auto_die => 0;
850  
851 16 my $action = $class->_containerd_nvidia_action(
852 config => $config,
853 has_v3_tmpl => $class->_path_exists("-f", "$base/config-v3.toml.tmpl"),
854 has_v3_dir => $class->_path_exists("-d", "$base/config-v3.toml.d"),
855 );
856  
857 16 ▶ if ($action eq 'present') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "present") 2 14 16
858 2 Rex::Logger::info(
859 " $runtime already wired the nvidia runtime natively — leaving containerd config untouched");
860 2 return;
861 }
862  
863 14 ▶ if ($action eq 'v3') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq 'v3') { } 10 4 14
864 10 ▶ Rex::Logger::info(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config and length $config 6 4 10
Condition66%2/3
defined $config and length $config
!ll&&!rl&&r
064
MC/DC50%1/2
defined $config and length $config
defined $config length $config
Truth table66%2/3
A and B
A= defined $configB= length $config
ABresult
0X0
100
111
865 " no generated $config_file yet and no config version markers — "
866 . "assuming modern (config v3) $runtime", "warn")
867 unless defined $config && length $config;
868  
869 10 $class->_write_nvidia_v3_dropin($base);
870 }
871 else {
872 4 file $base, ensure => 'directory';
873 4 file "$base/config.toml.tmpl", content => $class->_nvidia_containerd_tmpl_v2;
874 4 Rex::Logger::info(
875 " wrote base-extending config.toml.tmpl (legacy v2): $base/config.toml.tmpl");
876 }
877 }
878  
879 2 ▶sub _configure_containerd_standalone {
Subroutine _configure_containerd_standalone: called CC=1 SCAR=0.0
880 2 my ( $class ) = @_;
881 2 run "nvidia-ctk runtime configure --runtime=containerd 2>&1", auto_die => 0;
882 2 run "systemctl restart containerd 2>/dev/null", auto_die => 0;
883 }
884  
885 # ============================================================
886 # CDI spec generation
887 # ============================================================
888  
889  
890 28 ▶sub generate_cdi_specs {
Subroutine generate_cdi_specs: called CC=14 SCAR=26.4
Pod documented
891 28 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->generate_cdi_specs(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->generate_cdi_specs(@_))
l!l
1414
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->generate_cdi_specs(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
892 14 Rex::Logger::info("Generating NVIDIA CDI specs...");
893  
894 # Hand off to a managed CDI source if one owns the runtime scan dir. Modern
895 # nvidia-container-toolkit ships nvidia-cdi-refresh.path/.service, which keep
896 # /run/cdi/nvidia.yaml fresh across driver updates. /etc/cdi and /run/cdi are
897 # BOTH default CDI scan dirs, so writing a static /etc/cdi/nvidia.yaml
898 # alongside the managed /run/cdi copy defines the same device kind
899 # (nvidia.com/gpu) twice — a duplicate-device load error in CDI consumers —
900 # and the static copy drifts against the refreshed one over driver updates.
901 # See _cdi_managed_source_present for the signal choice (karr #11).
902 14 my $enabled = run "systemctl is-enabled nvidia-cdi-refresh.path 2>/dev/null", auto_die => 0;
903 14 ▶ chomp $enabled if defined $enabled;
Branch: 1 branch
BranchTrueFalseTotal
if defined $enabled 14 0 14
904 14 my $active = run "systemctl is-active nvidia-cdi-refresh.path 2>/dev/null", auto_die => 0;
905 14 ▶ chomp $active if defined $active;
Branch: 1 branch
BranchTrueFalseTotal
if defined $active 14 0 14
906  
907 14 my $managed = 0;
908 14 ▶ if ($class->_cdi_managed_source_present(
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_cdi_managed_source_present("enabled_state", $enabled, "active_state", $active, "run_cdi", $class->_path_exists("-f", "/run/cdi/nvidia.yaml"))) 5 9 14
909 enabled_state => $enabled,
910 active_state => $active,
911 run_cdi => $class->_path_exists("-f", "/run/cdi/nvidia.yaml"),
912 )) {
913 5 Rex::Logger::info(
914 " nvidia-cdi-refresh manages CDI in /run/cdi — not writing a static "
915 . "/etc/cdi/nvidia.yaml (avoids a duplicate nvidia.com/gpu across scan dirs)");
916 # Kick the managed generator once so /run/cdi is populated NOW: /run is
917 # tmpfs and empty after the first-deploy reboot, and the .path watcher may
918 # not have fired yet (no driver-file change since it was armed). Best-effort
919 # — the unit re-runs itself on the next driver change; if the unit name
920 # differs (detected via the /run/cdi file), this is a harmless no-op.
921 5 run "systemctl start nvidia-cdi-refresh.service 2>/dev/null", auto_die => 0;
922 5 $managed = 1;
923 }
924  
925 # k76: what resolves decides, not which files exist. GB10/DGX OS with
926 # toolkit 1.19 has no nvidia-cdi-refresh and an empty /run/cdi after a
927 # reboot, and nothing there writes management.nvidia.com/gpu=all, which the
928 # GPU Operator validator asks for. Only a device that does not resolve gets
929 # a spec under /etc/cdi; a working spec (also one of nvidia-cdi-refresh's
930 # under /run/cdi) is left alone, so no kind appears twice.
931 14 my $list = run "nvidia-ctk cdi list 2>/dev/null", auto_die => 0;
932 14 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 3 11 14
933 3 Rex::Logger::info(" nvidia-ctk cdi list failed — cannot check which CDI devices "
934 . "resolve; management.nvidia.com/gpu=all is not generated", "warn");
935 3 ▶ $class->_write_cdi_spec('nvidia.com/gpu=all') unless $managed;
Branch: 1 branch
BranchTrueFalseTotal
unless $managed 2 1 3
936 3 return;
937 }
938  
939 11 my @missing = $class->_cdi_missing_devices($list, $class->_cdi_required_devices);
940 11 ▶ unless (@missing) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@missing) 3 8 11
941 3 Rex::Logger::info(" [ok] CDI devices resolve: " . join(', ', $class->_cdi_required_devices));
942 3 return;
943 }
944  
945 8 my $wrote = 0;
946 8 for my $device (@missing) {
947 12 ▶ if ($managed && $device eq 'nvidia.com/gpu=all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($managed and $device eq "nvidia.com/gpu=all") 1 11 12
Condition100%3/3
$managed and $device eq "nvidia.com/gpu=all"
!ll&&!rl&&r
921
MC/DC100%2/2
$managed and $device eq "nvidia.com/gpu=all"
$managed $device eq "nvidia.com/gpu=all"
Truth table100%3/3
A and B
A= $managedB= $device eq "nvidia.com/gpu=all"
ABresult
0X0
100
111
948 # A static /etc/cdi/nvidia.yaml would define the kind the refresh unit
949 # owns a second time (karr #11) -- say so instead.
950 1 Rex::Logger::info(" nvidia-cdi-refresh did not make $device resolvable — not writing "
951 . "a static /etc/cdi/nvidia.yaml next to it; check "
952 . "`systemctl status nvidia-cdi-refresh.service`", "warn");
953 1 next;
954 }
955 11 ▶ run "mkdir -p /etc/cdi", auto_die => 0 unless $wrote++;
Branch: 1 branch
BranchTrueFalseTotal
unless $wrote++ 8 3 11
956 11 $class->_write_cdi_spec($device, mkdir => 0);
957 }
958 8 ▶ return unless $wrote;
Branch: 1 branch
BranchTrueFalseTotal
unless $wrote 0 8 8
959  
960 8 my @still = $class->_cdi_missing_devices(
961 scalar(run "nvidia-ctk cdi list 2>/dev/null", auto_die => 0),
962 $class->_cdi_required_devices);
963 8 ▶ Rex::Logger::info(" CDI devices still not resolvable: " . join(', ', @still)
Branch: 1 branch
BranchTrueFalseTotal
if @still 4 4 8
964 . " — CDI consumers (GPU Operator validator, device plugin) will fail on them", "warn")
965 if @still;
966 }
967  
968 # The CDI devices generate_cdi_specs makes resolvable (k76): the GPU kind the
969 # device plugin hands out and the management kind the GPU Operator validator
970 # requests.
971 22 ▶sub _cdi_required_devices { ( 'nvidia.com/gpu=all', 'management.nvidia.com/gpu=all' ) }
Subroutine _cdi_required_devices: called CC=1 SCAR=0.0
972  
973 # Write the /etc/cdi spec that provides $device. nvidia.com/gpu=all is the
974 # plain `nvidia-ctk cdi generate`. The management spec needs --vendor (alias
975 # --cdi-vendor) and --class: without --vendor, --mode=management writes kind
976 # nvidia.com/gpu, whose `all` collides with nvidia.yaml's (toolkit 1.19.1;
977 # flag names identical in 1.20.1). A CLI without the management mode answers
978 # "invalid discovery mode" -- a warning, never a die.
979 13 ▶sub _write_cdi_spec {
Subroutine _write_cdi_spec: called CC=11 SCAR=24.3
980 13 my ( $class, $device, %opt ) = @_;
981 13 ▶ run "mkdir -p /etc/cdi", auto_die => 0 if $opt{mkdir} // 1;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"mkdir"} // 1 2 11 13
Condition100%2/2
$opt{"mkdir"} // 1
l!l
112
MC/DC100%1/1
$opt{"mkdir"} // 1
$opt{"mkdir"}
Truth table100%2/2
A
A= $opt{"mkdir"}
Aresult
11
00
982 13 ▶ if ($device eq 'nvidia.com/gpu=all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($device eq "nvidia.com/gpu=all") 6 7 13
983 6 my $out = run "nvidia-ctk cdi generate --output=/etc/cdi/nvidia.yaml 2>/dev/null", auto_die => 0;
984 6 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 4 6
985 # stderr stays discarded (the emitted command is unchanged): only stdout
986 # and the exit code can be reported.
987 2 my $code = $? >> 8;
988 2 ▶ $out //= '';
Condition50%1/2
$out //= ""
l!l
20
MC/DC0%0/1
$out //= ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
989 2 $out =~ s/\s+\z//;
990 2 ▶ Rex::Logger::info(" could not generate the CDI spec for $device (nvidia-ctk cdi generate: "
Branch: 1 branch
BranchTrueFalseTotal
$out eq '' ? : 1 1 2
991 . ($out eq '' ? 'failed, exit ' . $code : $out) . ")", "warn");
992 2 return 0;
993 }
994 4 Rex::Logger::info(" [ok] CDI specs written to /etc/cdi/nvidia.yaml");
995 4 return 1;
996 }
997 7 ▶ if ($device eq 'management.nvidia.com/gpu=all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($device eq "management.nvidia.com/gpu=all") 7 0 7
998 7 my $out = run "nvidia-ctk cdi generate --mode=management --vendor=management.nvidia.com "
999 . "--class=gpu --output=/etc/cdi/management.nvidia.yaml 2>&1", auto_die => 0;
1000 7 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 5 7
1001 2 ▶ $out //= '';
Condition50%1/2
$out //= ""
l!l
20
MC/DC0%0/1
$out //= ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
1002 2 $out =~ s/\s+\z//;
1003 2 ▶ Rex::Logger::info(" could not generate the CDI spec for $device (nvidia-ctk cdi generate "
Branch: 1 branch
BranchTrueFalseTotal
$out eq '' ? : 0 2 2
1004 . "--mode=management: " . ($out eq '' ? 'failed' : $out) . ")", "warn");
1005 2 return 0;
1006 }
1007 5 Rex::Logger::info(" [ok] CDI management spec written to /etc/cdi/management.nvidia.yaml");
1008 5 return 1;
1009 }
1010 0 Rex::Logger::info(" no CDI spec known for $device", "warn");
1011 0 return 0;
1012 }
1013  
1014 # Pure (k76): which of @wanted does not appear as a whole line in the output
1015 # of `nvidia-ctk cdi list` (one fully qualified device per line, the
1016 # "Found N CDI devices" count goes to stderr)? Order of @wanted is kept.
1017 27 ▶sub _cdi_missing_devices {
Subroutine _cdi_missing_devices: called CC=2 SCAR=6.9
1018 27 my ( $class, $list, @wanted ) = @_;
1019 27 ▶ my %have = map { s/^\s+|\s+\z//gr => 1 } split /\n/, ($list // '');
Condition100%2/2
$list // ""
l!l
261
MC/DC100%1/1
$list // ""
$list
Truth table100%2/2
A
A= $list
Aresult
11
00
1020 27 return grep { !$have{$_} } @wanted;
1021 }
1022  
1023 # Pure predicate for the generate_cdi_specs managed-source short-circuit: is a
1024 # MANAGED CDI source already present that owns /run/cdi/nvidia.yaml? Modern
1025 # nvidia-container-toolkit ships nvidia-cdi-refresh.path/.service, which
1026 # regenerate a CDI spec into the /run/cdi runtime scan dir and keep it fresh
1027 # across driver updates. When it is present, generate_cdi_specs must NOT also
1028 # write a static /etc/cdi/nvidia.yaml (both are default scan dirs → duplicate
1029 # nvidia.com/gpu + drift; karr #11).
1030 #
1031 # Signals (gathered impurely by generate_cdi_specs, matched here):
1032 # enabled_state => `systemctl is-enabled nvidia-cdi-refresh.path` output
1033 # active_state => `systemctl is-active nvidia-cdi-refresh.path` output
1034 # run_cdi => a /run/cdi/nvidia.yaml already exists (boolean)
1035 #
1036 # The systemd unit state is the PRIMARY signal, not the file. /run is tmpfs and
1037 # is wiped on every boot, so on a first deploy (right after the nouveau reboot)
1038 # the managed source may not have fired yet and /run/cdi/nvidia.yaml is absent
1039 # even though the refresh unit owns CDI from here on. The unit being installed
1040 # (enabled/enabled-runtime/static/indirect/alias) or armed (active/activating)
1041 # is durable across that reboot and answers the real question — "will this host
1042 # keep /run/cdi fresh?" — which a bare file check cannot. run_cdi is a
1043 # belt-and-suspenders confirmation: this code only ever writes /etc/cdi, so a
1044 # /run/cdi/nvidia.yaml can only have come from some OTHER producer — unambiguous
1045 # evidence of a second CDI source for the same kind (catches a producer under a
1046 # different unit name, or a host with no systemctl). disabled/masked/not-found
1047 # (opted out, or no such unit) do NOT count as managed.
1048 #
1049 # Pure (regex/string/boolean only, no run/systemctl/test) so it is unit-testable
1050 # offline, like _containerd_nvidia_action / Rex::GPU::NVIDIA::Setup->_driver_present.
1051 33 ▶sub _cdi_managed_source_present {
Subroutine _cdi_managed_source_present: called CC=6 SCAR=17.9
1052 33 my ( $class, %s ) = @_;
1053 33 ▶ my $enabled = $s{enabled_state} // '';
Condition100%2/2
$s{"enabled_state"} // ""
l!l
267
MC/DC100%1/1
$s{"enabled_state"} // ""
$s{"enabled_state"}
Truth table100%2/2
A
A= $s{"enabled_state"}
Aresult
11
00
1054 33 ▶ my $active = $s{active_state} // '';
Condition100%2/2
$s{"active_state"} // ""
l!l
2211
MC/DC100%1/1
$s{"active_state"} // ""
$s{"active_state"}
Truth table100%2/2
A
A= $s{"active_state"}
Aresult
11
00
1055 33 ▶ return 1 if $enabled =~ /^(?:enabled|enabled-runtime|static|indirect|alias)\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $enabled =~ /^(?:enabled|enabled-runtime|static|indirect|alias)\b/u 10 23 33
1056 23 ▶ return 1 if $active =~ /^(?:active|activating)\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $active =~ /^(?:active|activating)\b/u 2 21 23
1057 21 ▶ return 1 if $s{run_cdi};
Branch: 1 branch
BranchTrueFalseTotal
if $s{"run_cdi"} 3 18 21
1058 18 return 0;
1059 }
1060  
1061 # ============================================================
1062 # Reboot
1063 # ============================================================
1064  
1065 13 ▶sub _reboot_and_wait {
Subroutine _reboot_and_wait: called CC=9 SCAR=22.0
1066 13 my ( $class ) = @_;
1067 13 Rex::Logger::info("Rebooting host to activate NVIDIA driver (replacing nouveau)...");
1068  
1069 # The identity of the boot we are leaving (karr #78): an answer to `echo ok`
1070 # alone can come from the old system still shutting down. Reading it must
1071 # never stop the reboot: unreadable -> the old, answer-only behaviour.
1072 13 my $boot_id = _read_boot_id();
1073 13 ▶ Rex::Logger::info(" boot_id not readable before reboot, checking only that the host answers")
Branch: 1 branch
BranchTrueFalseTotal
unless length $boot_id 9 4 13
1074 unless length $boot_id;
1075  
1076 # Schedule reboot 2 s from now so the run() call can return cleanly
1077 13 run "nohup sh -c 'sleep 2 && shutdown -r now' >/dev/null 2>&1 &", auto_die => 0;
1078  
1079 # Wait long enough for the system to actually go down
1080 13 sleep 20;
1081  
1082 # Poll until SSH comes back (up to 5 minutes)
1083 13 my $conn = Rex::get_current_connection()->{conn};
1084 13 my $back = 0;
1085 13 for my $i (1..60) {
1086 315 eval { $conn->disconnect() };
1087 315 eval { $conn->reconnect() };
1088 315 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 252 63 315
1089 # Verify we can actually run a command: its output, not just that run
1090 # returned (karr #65); a PTY session answers "ok\r"
1091 252 my $test = eval { run "echo ok", auto_die => 0 };
1092 # same boot_id = still the old system going down: keep waiting
1093 252 ▶ if (defined $test && $test =~ /^ok\r?$/m
Branch: 1 branch
BranchTrueFalseTotal
if (defined $test and $test =~ /^ok\r?$/mu and !length($boot_id) || _new_boot_id($boot_id)) 8 244 252
Condition88%8/9
defined $test and $test =~ /^ok\r?$/mu and !length($boot_id) || _new_boot_id($boot_id)
!ll&&!rl&&r
1221228
defined $test and $test =~ /^ok\r?$/mu
!ll&&!rl&&r
0122130
not length $boot_id or _new_boot_id($boot_id)
l!l&&r!l&&!r
62122
MC/DC75%3/4
defined $test and $test =~ /^ok\r?$/mu and !length($boot_id) || _new_boot_id($boot_id)
defined $test $test =~ /^ok\r?$/mu not length $boot_id _new_boot_id($boot_id)
Truth table80%4/5
A and B and not(C) or D
A= defined $testB= $test =~ /^ok\r?$/muC= not length $boot_idD= _new_boot_id($boot_id)
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1094 && ( !length $boot_id || _new_boot_id( $boot_id ) )) {
1095 8 Rex::Logger::info(" Host is back online (after ~" . ($i * 5 + 20) . "s)");
1096 8 $back = 1;
1097 8 last;
1098 }
1099 }
1100 307 Rex::Logger::info(" Waiting for host to come back... ($i/60)");
1101 307 sleep 5;
1102 }
1103  
1104 13 ▶ die "Host did not come back after reboot\n" unless $back;
Branch: 1 branch
BranchTrueFalseTotal
unless $back 5 8 13
1105 }
1106  
1107 # True only for a readable boot_id that differs from the old one; an
1108 # unreadable one proves nothing, so the caller keeps waiting.
1109 124 ▶sub _new_boot_id {
Subroutine _new_boot_id: called CC=3 SCAR=11.0
1110 124 my ( $old ) = @_;
1111 124 my $now = _read_boot_id();
1112 124 ▶ return length $now && $now ne $old ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $now && $now ne $old ? : 2 122 124
Condition100%3/3
length $now and $now ne $old
!ll&&!rl&&r
60622
MC/DC100%2/2
length $now and $now ne $old
length $now $now ne $old
Truth table100%3/3
A and B
A= length $nowB= $now ne $old
ABresult
0X0
100
111
1113 }
1114  
1115 # The kernel's per-boot UUID, or '' when it cannot be read (never dies).
1116 # A plain function: _reboot_and_wait is called as function and as method.
1117 137 ▶sub _read_boot_id {
Subroutine _read_boot_id: called CC=3 SCAR=11.0
1118 137 my $out = eval { run "cat /proc/sys/kernel/random/boot_id 2>/dev/null", auto_die => 0 };
1119 137 ▶ return '' unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 137 137
1120 137 ▶ return $out =~ /^\s*([0-9a-fA-F-]{32,36})\s*$/m ? lc $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$out =~ /^\s*([0-9a-fA-F-]{32,36})\s*$/mu ? : 68 69 137
1121 }
1122  
1123 36 1;
1124  
1125 __END__
« blib/lib/Rex/GPU.pm ↑ Summary lib/Rex/GPU/NVIDIA/Setup/Debian.pm »