diff --git a/cmd/nvidia-container-runtime/modifier/experimental_test.go b/cmd/nvidia-container-runtime/modifier/experimental_test.go index 9e09873a..34f83bf4 100644 --- a/cmd/nvidia-container-runtime/modifier/experimental_test.go +++ b/cmd/nvidia-container-runtime/modifier/experimental_test.go @@ -231,7 +231,7 @@ func TestExperimentalModifier(t *testing.T) { }, }, { - description: "modification fails for existing nvidia-container-runtime-hook", + description: "modification removes existing nvidia-container-runtime-hook", spec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ @@ -254,20 +254,19 @@ func TestExperimentalModifier(t *testing.T) { return hooks, nil }, }, - expectedError: fmt.Errorf("nvidia-container-runtime-hook already exists"), expectedSpec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ { - Path: "/path/to/nvidia-container-runtime-hook", - Args: []string{"/path/to/nvidia-container-runtime-hook", "prestart"}, + Path: "/hook/b", + Args: []string{"/hook/b", "argb"}, }, }, }, }, }, { - description: "modification fails for existing nvidia-container-toolkit", + description: "modification removes existing nvidia-container-toolkit", spec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ @@ -290,13 +289,12 @@ func TestExperimentalModifier(t *testing.T) { return hooks, nil }, }, - expectedError: fmt.Errorf("nvidia-container-toolkit already exists"), expectedSpec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ { - Path: "/path/to/nvidia-container-toolkit", - Args: []string{"/path/to/nvidia-container-toolkit", "prestart"}, + Path: "/hook/b", + Args: []string{"/hook/b", "argb"}, }, }, }, diff --git a/cmd/nvidia-container-runtime/modifier/hook_remover.go b/cmd/nvidia-container-runtime/modifier/hook_remover.go index 797de81f..c737bdcc 100644 --- a/cmd/nvidia-container-runtime/modifier/hook_remover.go +++ b/cmd/nvidia-container-runtime/modifier/hook_remover.go @@ -17,7 +17,6 @@ package modifier import ( - "fmt" "path/filepath" "github.com/NVIDIA/nvidia-container-toolkit/internal/config" @@ -47,10 +46,19 @@ func (m nvidiaContainerRuntimeHookRemover) Modify(spec *specs.Spec) error { return nil } + var newPrestart []specs.Hook + for _, hook := range spec.Hooks.Prestart { if isNVIDIAContainerRuntimeHook(&hook) { - return fmt.Errorf("spec already contains required 'prestart' hook") + m.logger.Debugf("Removing hook %v", hook) + continue } + newPrestart = append(newPrestart, hook) + } + + if len(newPrestart) != len(spec.Hooks.Prestart) { + m.logger.Debugf("Updating 'prestart' hooks to %v", newPrestart) + spec.Hooks.Prestart = newPrestart } return nil diff --git a/cmd/nvidia-container-toolkit/hook_config.go b/cmd/nvidia-container-toolkit/hook_config.go index b4cb0133..6a6d18b3 100644 --- a/cmd/nvidia-container-toolkit/hook_config.go +++ b/cmd/nvidia-container-toolkit/hook_config.go @@ -7,6 +7,7 @@ import ( "reflect" "github.com/BurntSushi/toml" + "github.com/NVIDIA/nvidia-container-toolkit/internal/config" ) const ( @@ -41,10 +42,11 @@ type HookConfig struct { AcceptDeviceListAsVolumeMounts bool `toml:"accept-nvidia-visible-devices-as-volume-mounts"` SupportedDriverCapabilities DriverCapabilities `toml:"supported-driver-capabilities"` - NvidiaContainerCLI CLIConfig `toml:"nvidia-container-cli"` + NvidiaContainerCLI CLIConfig `toml:"nvidia-container-cli"` + NVIDIAContainerRuntime config.RuntimeConfig `toml:"nvidia-container-runtime"` } -func getDefaultHookConfig() (config HookConfig) { +func getDefaultHookConfig() HookConfig { return HookConfig{ DisableRequire: false, SwarmResource: nil, @@ -63,6 +65,7 @@ func getDefaultHookConfig() (config HookConfig) { User: nil, Ldconfig: nil, }, + NVIDIAContainerRuntime: *config.GetDefaultRuntimeConfig(), } } diff --git a/cmd/nvidia-container-toolkit/main.go b/cmd/nvidia-container-toolkit/main.go index 13f8197c..81c86572 100644 --- a/cmd/nvidia-container-toolkit/main.go +++ b/cmd/nvidia-container-toolkit/main.go @@ -17,6 +17,7 @@ import ( var ( debugflag = flag.Bool("debug", false, "enable debug output") + forceflag = flag.Bool("force", false, "force execution of prestart hook in experimental mode") configflag = flag.String("config", "", "configuration file") defaultPATH = []string{"/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"} @@ -85,6 +86,10 @@ func doPrestart() { hook := getHookConfig() cli := hook.NvidiaContainerCLI + if hook.NVIDIAContainerRuntime.Experimental && !*forceflag { + log.Panicln("invoking the NVIDIA Container Runtime Hook directly (e.g. specifying the docker --gpus flag) is not supported. Please use the NVIDIA Container Runtime instead.") + } + container := getContainerConfig(hook) nvidia := container.Nvidia if nvidia == nil { diff --git a/internal/config/config.go b/internal/config/config.go index 19f41386..c5b985a9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -103,7 +103,7 @@ func getDefaultConfig() *Config { c := Config{ NVIDIAContainerCLIConfig: *getDefaultContainerCLIConfig(), NVIDIACTKConfig: *getDefaultCTKConfig(), - NVIDIAContainerRuntimeConfig: *getDefaultRuntimeConfig(), + NVIDIAContainerRuntimeConfig: *GetDefaultRuntimeConfig(), } return &c diff --git a/internal/config/runtime.go b/internal/config/runtime.go index 9ae8de48..7486e9e9 100644 --- a/internal/config/runtime.go +++ b/internal/config/runtime.go @@ -29,7 +29,7 @@ type RuntimeConfig struct { // getRuntimeConfigFrom reads the nvidia container runtime config from the specified toml Tree. func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig { - cfg := getDefaultRuntimeConfig() + cfg := GetDefaultRuntimeConfig() if toml == nil { return cfg @@ -42,8 +42,8 @@ func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig { return cfg } -// getDefaultRuntimeConfig defines the default values for the config -func getDefaultRuntimeConfig() *RuntimeConfig { +// GetDefaultRuntimeConfig defines the default values for the config +func GetDefaultRuntimeConfig() *RuntimeConfig { c := RuntimeConfig{ DebugFilePath: "/dev/null", Experimental: false, diff --git a/internal/discover/legacy.go b/internal/discover/legacy.go index 4a2d8270..b8a5068a 100644 --- a/internal/discover/legacy.go +++ b/internal/discover/legacy.go @@ -59,7 +59,7 @@ func (d legacy) Hooks() ([]Hook, error) { } d.logger.Debugf("Using NVIDIA Container Runtime Hook path %v", hookPath) - args := []string{hookPath, "prestart"} + args := []string{hookPath, "--force", "prestart"} h := Hook{ Lifecycle: cdi.PrestartHook, Path: hookPath,