Skip to content

Commit 194c1e6

Browse files
Add --disable-hook flag to cdi generate command
When running the nvidia-ctk cdi generate command, a user should be able to opt out of specific hooks. We propose to add a flag --disable-hook that will take a comma-separated list of hooks that will be skipped when creating the CDI spec. Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent adb5e67 commit 194c1e6

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

cmd/nvidia-ctk/cdi/generate/generate.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type options struct {
5757

5858
configSearchPaths cli.StringSlice
5959
librarySearchPaths cli.StringSlice
60+
disableHooks cli.StringSlice
6061

6162
csv struct {
6263
files cli.StringSlice
@@ -176,6 +177,12 @@ func (m command) build() *cli.Command {
176177
Usage: "Specify a pattern the CSV mount specifications.",
177178
Destination: &opts.csv.ignorePatterns,
178179
},
180+
&cli.StringSliceFlag{
181+
Name: "disable-hook",
182+
Usage: "Comma-separated list of hooks to skip when generating the CDI specification.",
183+
Value: cli.NewStringSlice(),
184+
Destination: &opts.disableHooks,
185+
},
179186
}
180187

181188
return &c
@@ -262,7 +269,7 @@ func (m command) generateSpec(opts *options) (spec.Interface, error) {
262269
deviceNamers = append(deviceNamers, deviceNamer)
263270
}
264271

265-
cdilib, err := nvcdi.New(
272+
initOpts := []nvcdi.Option{
266273
nvcdi.WithLogger(m.logger),
267274
nvcdi.WithDriverRoot(opts.driverRoot),
268275
nvcdi.WithDevRoot(opts.devRoot),
@@ -276,7 +283,15 @@ func (m command) generateSpec(opts *options) (spec.Interface, error) {
276283
nvcdi.WithCSVIgnorePatterns(opts.csv.ignorePatterns.Value()),
277284
// We set the following to allow for dependency injection:
278285
nvcdi.WithNvmlLib(opts.nvmllib),
279-
)
286+
}
287+
288+
if len(opts.disableHooks.Value()) > 0 {
289+
for _, hook := range opts.disableHooks.Value() {
290+
initOpts = append(initOpts, nvcdi.WithDisabledHook(nvcdi.HookName(hook)))
291+
}
292+
}
293+
294+
cdilib, err := nvcdi.New(initOpts...)
280295
if err != nil {
281296
return nil, fmt.Errorf("failed to create CDI library: %v", err)
282297
}

cmd/nvidia-ctk/cdi/generate/generate_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/NVIDIA/go-nvml/pkg/nvml/mock/dgxa100"
2727
testlog "github.com/sirupsen/logrus/hooks/test"
2828
"github.com/stretchr/testify/require"
29+
"github.com/urfave/cli/v2"
2930

3031
"github.com/NVIDIA/nvidia-container-toolkit/internal/test"
3132
)
@@ -36,6 +37,7 @@ func TestGenerateSpec(t *testing.T) {
3637
require.NoError(t, err)
3738

3839
driverRoot := filepath.Join(moduleRoot, "testdata", "lookup", "rootfs-1")
40+
disableHook := cli.NewStringSlice("enable-cuda-compat")
3941

4042
logger, _ := testlog.NewNullLogger()
4143
testCases := []struct {
@@ -112,6 +114,70 @@ containerEdits:
112114
- nosuid
113115
- nodev
114116
- bind
117+
`,
118+
},
119+
{
120+
description: "skipHook",
121+
options: options{
122+
format: "yaml",
123+
mode: "nvml",
124+
vendor: "example.com",
125+
class: "device",
126+
driverRoot: driverRoot,
127+
disableHooks: *disableHook,
128+
},
129+
expectedOptions: options{
130+
format: "yaml",
131+
mode: "nvml",
132+
vendor: "example.com",
133+
class: "device",
134+
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
135+
driverRoot: driverRoot,
136+
disableHooks: *disableHook,
137+
},
138+
expectedSpec: `---
139+
cdiVersion: 0.5.0
140+
kind: example.com/device
141+
devices:
142+
- name: "0"
143+
containerEdits:
144+
deviceNodes:
145+
- path: /dev/nvidia0
146+
hostPath: {{ .driverRoot }}/dev/nvidia0
147+
- name: all
148+
containerEdits:
149+
deviceNodes:
150+
- path: /dev/nvidia0
151+
hostPath: {{ .driverRoot }}/dev/nvidia0
152+
containerEdits:
153+
env:
154+
- NVIDIA_VISIBLE_DEVICES=void
155+
deviceNodes:
156+
- path: /dev/nvidiactl
157+
hostPath: {{ .driverRoot }}/dev/nvidiactl
158+
hooks:
159+
- hookName: createContainer
160+
path: /usr/bin/nvidia-cdi-hook
161+
args:
162+
- nvidia-cdi-hook
163+
- create-symlinks
164+
- --link
165+
- libcuda.so.1::/lib/x86_64-linux-gnu/libcuda.so
166+
- hookName: createContainer
167+
path: /usr/bin/nvidia-cdi-hook
168+
args:
169+
- nvidia-cdi-hook
170+
- update-ldcache
171+
- --folder
172+
- /lib/x86_64-linux-gnu
173+
mounts:
174+
- hostPath: {{ .driverRoot }}/lib/x86_64-linux-gnu/libcuda.so.999.88.77
175+
containerPath: /lib/x86_64-linux-gnu/libcuda.so.999.88.77
176+
options:
177+
- ro
178+
- nosuid
179+
- nodev
180+
- bind
115181
`,
116182
},
117183
}

0 commit comments

Comments
 (0)