diff --git a/cmd/nvidia-ctk/cdi/generate/generate.go b/cmd/nvidia-ctk/cdi/generate/generate.go index 71fd0d01..b88c9234 100644 --- a/cmd/nvidia-ctk/cdi/generate/generate.go +++ b/cmd/nvidia-ctk/cdi/generate/generate.go @@ -22,7 +22,7 @@ import ( "path/filepath" "strings" - "github.com/NVIDIA/nvidia-container-toolkit/internal/discover" + "github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/edits" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" @@ -154,7 +154,7 @@ func (m command) validateFlags(c *cli.Context, opts *options) error { return err } - cfg.nvidiaCTKPath = discover.FindNvidiaCTK(m.logger, cfg.nvidiaCTKPath) + opts.nvidiaCTKPath = config.ResolveNVIDIACTKPath(m.logger, opts.nvidiaCTKPath) if outputFileFormat := formatFromFilename(opts.output); outputFileFormat != "" { m.logger.Debugf("Inferred output format as %q from output file name", outputFileFormat) diff --git a/internal/config/config.go b/internal/config/config.go index 2ecfa120..7601d0fd 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -22,15 +22,21 @@ import ( "io" "os" "path" + "path/filepath" "strings" + "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" "github.com/pelletier/go-toml" + "github.com/sirupsen/logrus" ) const ( configOverride = "XDG_CONFIG_HOME" configFilePath = "nvidia-container-runtime/config.toml" + + nvidiaCTKExecutable = "nvidia-ctk" + nvidiaCTKDefaultFilePath = "/usr/bin/nvidia-ctk" ) var ( @@ -181,7 +187,7 @@ func GetDefaultConfigToml() (*toml.Tree, error) { tree.Set("nvidia-container-runtime.modes.cdi.annotation-prefixes", []string{cdi.AnnotationPrefix}) // nvidia-ctk - tree.Set("nvidia-ctk.path", "nvidia-ctk") + tree.Set("nvidia-ctk.path", nvidiaCTKExecutable) return tree, nil } @@ -232,3 +238,33 @@ func getDistIDLike() []string { } return nil } + +// ResolveNVIDIACTKPath resolves the path to the nvidia-ctk binary. +// This executable is used in hooks and needs to be an absolute path. +// If the path is specified as an absolute path, it is used directly +// without checking for existence of an executable at that path. +func ResolveNVIDIACTKPath(logger *logrus.Logger, nvidiaCTKPath string) string { + if filepath.IsAbs(nvidiaCTKPath) { + logger.Debugf("Using specified NVIDIA Container Toolkit CLI path %v", nvidiaCTKPath) + return nvidiaCTKPath + } + + if nvidiaCTKPath == "" { + nvidiaCTKPath = nvidiaCTKExecutable + } + logger.Debugf("Locating NVIDIA Container Toolkit CLI as %v", nvidiaCTKPath) + lookup := lookup.NewExecutableLocator(logger, "") + hookPath := nvidiaCTKDefaultFilePath + targets, err := lookup.Locate(nvidiaCTKPath) + if err != nil { + logger.Warnf("Failed to locate %v: %v", nvidiaCTKPath, err) + } else if len(targets) == 0 { + logger.Warnf("%v not found", nvidiaCTKPath) + } else { + logger.Debugf("Found %v candidates: %v", nvidiaCTKPath, targets) + hookPath = targets[0] + } + logger.Debugf("Using NVIDIA Container Toolkit CLI path %v", hookPath) + + return hookPath +} diff --git a/internal/discover/hooks.go b/internal/discover/hooks.go index 87202e7c..3135ea06 100644 --- a/internal/discover/hooks.go +++ b/internal/discover/hooks.go @@ -19,14 +19,7 @@ package discover import ( "path/filepath" - "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" - "github.com/sirupsen/logrus" -) - -const ( - nvidiaCTKExecutable = "nvidia-ctk" - nvidiaCTKDefaultFilePath = "/usr/bin/nvidia-ctk" ) var _ Discover = (*Hook)(nil) @@ -72,32 +65,3 @@ func CreateNvidiaCTKHook(nvidiaCTKPath string, hookName string, additionalArgs . Args: append([]string{filepath.Base(nvidiaCTKPath), "hook", hookName}, additionalArgs...), } } - -// FindNvidiaCTK locates the nvidia-ctk executable to be used in hooks. -// If an nvidia-ctk path is specified as an absolute path, it is used directly -// without checking for existence of an executable at that path. -func FindNvidiaCTK(logger *logrus.Logger, nvidiaCTKPath string) string { - if filepath.IsAbs(nvidiaCTKPath) { - logger.Debugf("Using specified NVIDIA Container Toolkit CLI path %v", nvidiaCTKPath) - return nvidiaCTKPath - } - - if nvidiaCTKPath == "" { - nvidiaCTKPath = nvidiaCTKExecutable - } - logger.Debugf("Locating NVIDIA Container Toolkit CLI as %v", nvidiaCTKPath) - lookup := lookup.NewExecutableLocator(logger, "") - hookPath := nvidiaCTKDefaultFilePath - targets, err := lookup.Locate(nvidiaCTKPath) - if err != nil { - logger.Warnf("Failed to locate %v: %v", nvidiaCTKPath, err) - } else if len(targets) == 0 { - logger.Warnf("%v not found", nvidiaCTKPath) - } else { - logger.Debugf("Found %v candidates: %v", nvidiaCTKPath, targets) - hookPath = targets[0] - } - logger.Debugf("Using NVIDIA Container Toolkit CLI path %v", hookPath) - - return hookPath -} diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index 4e30f8b9..47a80e29 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -22,7 +22,6 @@ import ( "strings" "github.com/NVIDIA/nvidia-container-toolkit/internal/config" - "github.com/NVIDIA/nvidia-container-toolkit/internal/discover" "github.com/NVIDIA/nvidia-container-toolkit/internal/info" "github.com/opencontainers/runtime-spec/specs-go" ) @@ -65,7 +64,7 @@ func (r rt) Run(argv []string) (rerr error) { if r.modeOverride != "" { cfg.NVIDIAContainerRuntimeConfig.Mode = r.modeOverride } - cfg.NVIDIACTKConfig.Path = discover.FindNvidiaCTK(r.logger.Logger, cfg.NVIDIACTKConfig.Path) + cfg.NVIDIACTKConfig.Path = config.ResolveNVIDIACTKPath(r.logger.Logger, cfg.NVIDIACTKConfig.Path) // Print the config to the output. configJSON, err := json.MarshalIndent(cfg, "", " ")