diff --git a/cmd/nvidia-container-runtime-hook/hook_config.go b/cmd/nvidia-container-runtime-hook/hook_config.go index 16922c29..7e6bc7a5 100644 --- a/cmd/nvidia-container-runtime-hook/hook_config.go +++ b/cmd/nvidia-container-runtime-hook/hook_config.go @@ -43,8 +43,9 @@ 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"` - NVIDIAContainerRuntime config.RuntimeConfig `toml:"nvidia-container-runtime"` + NvidiaContainerCLI CLIConfig `toml:"nvidia-container-cli"` + NVIDIAContainerRuntime config.RuntimeConfig `toml:"nvidia-container-runtime"` + NVIDIAContainerRuntimeHook config.RuntimeHookConfig `toml:"nvidia-container-runtime-hook"` } func getDefaultHookConfig() HookConfig { @@ -66,7 +67,8 @@ func getDefaultHookConfig() HookConfig { User: nil, Ldconfig: nil, }, - NVIDIAContainerRuntime: *config.GetDefaultRuntimeConfig(), + NVIDIAContainerRuntime: *config.GetDefaultRuntimeConfig(), + NVIDIAContainerRuntimeHook: *config.GetDefaultRuntimeHookConfig(), } } diff --git a/cmd/nvidia-container-runtime-hook/main.go b/cmd/nvidia-container-runtime-hook/main.go index b90fb662..95b02364 100644 --- a/cmd/nvidia-container-runtime-hook/main.go +++ b/cmd/nvidia-container-runtime-hook/main.go @@ -74,7 +74,7 @@ func doPrestart() { hook := getHookConfig() cli := hook.NvidiaContainerCLI - if info.ResolveAutoMode(&logInterceptor{}, hook.NVIDIAContainerRuntime.Mode) != "legacy" { + if !hook.NVIDIAContainerRuntimeHook.SkipModeDetection && info.ResolveAutoMode(&logInterceptor{}, hook.NVIDIAContainerRuntime.Mode) != "legacy" { 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 (e.g. specify the --runtime=nvidia flag) instead.") } diff --git a/internal/config/config.go b/internal/config/config.go index b3c6f890..b4f773a6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -47,9 +47,10 @@ var ( type Config struct { AcceptEnvvarUnprivileged bool `toml:"accept-nvidia-visible-devices-envvar-when-unprivileged"` - NVIDIAContainerCLIConfig ContainerCLIConfig `toml:"nvidia-container-cli"` - NVIDIACTKConfig CTKConfig `toml:"nvidia-ctk"` - NVIDIAContainerRuntimeConfig RuntimeConfig `toml:"nvidia-container-runtime"` + NVIDIAContainerCLIConfig ContainerCLIConfig `toml:"nvidia-container-cli"` + NVIDIACTKConfig CTKConfig `toml:"nvidia-ctk"` + NVIDIAContainerRuntimeConfig RuntimeConfig `toml:"nvidia-container-runtime"` + NVIDIAContainerRuntimeHookConfig RuntimeHookConfig `toml:"nvidia-container-runtime-hook"` } // GetConfig sets up the config struct. Values are read from a toml file @@ -103,6 +104,12 @@ func getConfigFrom(toml *toml.Tree) (*Config, error) { } cfg.NVIDIAContainerRuntimeConfig = *runtimeConfig + runtimeHookConfig, err := getRuntimeHookConfigFrom(toml) + if err != nil { + return nil, fmt.Errorf("failed to load nvidia-container-runtime-hook config: %v", err) + } + cfg.NVIDIAContainerRuntimeHookConfig = *runtimeHookConfig + return cfg, nil } diff --git a/internal/config/hook.go b/internal/config/hook.go new file mode 100644 index 00000000..4c35a1d6 --- /dev/null +++ b/internal/config/hook.go @@ -0,0 +1,62 @@ +/** +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package config + +import ( + "fmt" + + "github.com/pelletier/go-toml" +) + +// RuntimeHookConfig stores the config options for the NVIDIA Container Runtime +type RuntimeHookConfig struct { + // SkipModeDetection disables the mode check for the runtime hook. + SkipModeDetection bool `toml:"skip-mode-detection"` +} + +// dummyHookConfig allows us to unmarshal only a RuntimeHookConfig from a *toml.Tree +type dummyHookConfig struct { + RuntimeHook RuntimeHookConfig `toml:"nvidia-container-runtime-hook"` +} + +// getRuntimeHookConfigFrom reads the nvidia container runtime config from the specified toml Tree. +func getRuntimeHookConfigFrom(toml *toml.Tree) (*RuntimeHookConfig, error) { + cfg := GetDefaultRuntimeHookConfig() + + if toml == nil { + return cfg, nil + } + + d := dummyHookConfig{ + RuntimeHook: *cfg, + } + + if err := toml.Unmarshal(&d); err != nil { + return nil, fmt.Errorf("failed to unmarshal runtime config: %v", err) + } + + return &d.RuntimeHook, nil +} + +// GetDefaultRuntimeHookConfig defines the default values for the config +func GetDefaultRuntimeHookConfig() *RuntimeHookConfig { + c := RuntimeHookConfig{ + SkipModeDetection: false, + } + + return &c +}