From 5885fead8fbccc771e05d309910f818f73411bf1 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 19 Sep 2022 14:54:44 +0200 Subject: [PATCH 1/3] Improve locating NVIDIA Container Runtime Hook This change ensures that a more concrete error is provided by the NVIDIA Container Runtime if the NVIDIA Container Runtime hook cannot be located. Signed-off-by: Evan Lezar --- internal/modifier/stable.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/internal/modifier/stable.go b/internal/modifier/stable.go index c927e702..1b4d8401 100644 --- a/internal/modifier/stable.go +++ b/internal/modifier/stable.go @@ -17,11 +17,10 @@ package modifier import ( - "os" - "os/exec" - "path/filepath" + "fmt" "github.com/NVIDIA/nvidia-container-toolkit/internal/config" + "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" @@ -44,21 +43,8 @@ type stableRuntimeModifier struct { // Modify applies the required modification to the incoming OCI spec, inserting the nvidia-container-runtime-hook // as a prestart hook. func (m stableRuntimeModifier) Modify(spec *specs.Spec) error { - path, err := exec.LookPath(config.NVIDIAContainerRuntimeHookExecutable) - if err != nil { - path = filepath.Join(config.DefaultExecutableDir, config.NVIDIAContainerRuntimeHookExecutable) - _, err = os.Stat(path) - if err != nil { - return err - } - } - - m.logger.Infof("Using prestart hook path: %s", path) - - args := []string{path} - if spec.Hooks == nil { - spec.Hooks = &specs.Hooks{} - } else if len(spec.Hooks.Prestart) != 0 { + // If an NVIDIA Container Runtime Hook already exists, we don't make any modifications to the spec. + if spec.Hooks != nil { for _, hook := range spec.Hooks.Prestart { if isNVIDIAContainerRuntimeHook(&hook) { m.logger.Infof("Existing nvidia prestart hook (%v) found in OCI spec", hook.Path) @@ -67,6 +53,21 @@ func (m stableRuntimeModifier) Modify(spec *specs.Spec) error { } } + // We create a locator and look for the NVIDIA Container Runtime Hook in the path. + candidates, err := lookup.NewExecutableLocator(m.logger, "").Locate(config.NVIDIAContainerRuntimeHookExecutable) + if err != nil { + return fmt.Errorf("failed to locate NVIDIA Container Runtime Hook: %v", err) + } + path := candidates[0] + if len(candidates) > 1 { + m.logger.Debugf("Using %v from multiple NVIDIA Container Runtime Hook candidates: %v", path, candidates) + } + + m.logger.Infof("Using prestart hook path: %v", path) + args := []string{path} + if spec.Hooks == nil { + spec.Hooks = &specs.Hooks{} + } spec.Hooks.Prestart = append(spec.Hooks.Prestart, specs.Hook{ Path: path, Args: append(args, "prestart"), From 2b08a79206fafd773614a4031f6ca5dcddfff750 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 19 Sep 2022 15:21:57 +0200 Subject: [PATCH 2/3] Ensure that errors are logged Signed-off-by: Evan Lezar --- cmd/nvidia-container-runtime/main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/nvidia-container-runtime/main.go b/cmd/nvidia-container-runtime/main.go index ea935aca..9859d910 100644 --- a/cmd/nvidia-container-runtime/main.go +++ b/cmd/nvidia-container-runtime/main.go @@ -48,7 +48,12 @@ func run(argv []string) (rerr error) { if err != nil { return fmt.Errorf("failed to set up logger: %v", err) } - defer logger.Reset() + defer func() { + if rerr != nil { + logger.Errorf("%v", rerr) + } + logger.Reset() + }() logger.Debugf("Command line arguments: %v", argv) runtime, err := newNVIDIAContainerRuntime(logger.Logger, cfg, argv) From 8beb7b4231d7e73cc89dbb3a832924c1d5585756 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 19 Sep 2022 15:31:10 +0200 Subject: [PATCH 3/3] Only remove nvidia-container-toolkit if it is a symlink Signed-off-by: Evan Lezar --- packaging/rpm/SPECS/nvidia-container-toolkit.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpm/SPECS/nvidia-container-toolkit.spec b/packaging/rpm/SPECS/nvidia-container-toolkit.spec index 24dd48fa..0c21b9bd 100644 --- a/packaging/rpm/SPECS/nvidia-container-toolkit.spec +++ b/packaging/rpm/SPECS/nvidia-container-toolkit.spec @@ -56,7 +56,7 @@ install -m 644 -t %{buildroot}/usr/share/containers/oci/hooks.d oci-nvidia-hook. ln -sf %{_bindir}/nvidia-container-runtime-hook %{_bindir}/nvidia-container-toolkit %postun -rm -f %{_bindir}/nvidia-container-toolkit +if [ -L %{_bindir}/nvidia-container-toolkit ] then; rm -f %{_bindir}/nvidia-container-toolkit; fi %files %license LICENSE