2022-03-29 10:01:12 +00:00
|
|
|
/**
|
|
|
|
# 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"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-05-26 08:29:52 +00:00
|
|
|
"path/filepath"
|
2022-03-29 10:01:12 +00:00
|
|
|
|
2023-05-26 08:29:52 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
2022-03-29 10:01:12 +00:00
|
|
|
"github.com/pelletier/go-toml"
|
2023-05-26 08:29:52 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-03-29 10:01:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configOverride = "XDG_CONFIG_HOME"
|
|
|
|
configFilePath = "nvidia-container-runtime/config.toml"
|
2023-05-26 08:29:52 +00:00
|
|
|
|
|
|
|
nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook"
|
|
|
|
nvidiaContainerRuntimeHookDefaultPath = "/usr/bin/nvidia-container-runtime-hook"
|
2022-03-29 10:01:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-04-08 05:18:49 +00:00
|
|
|
// DefaultExecutableDir specifies the default path to use for executables if they cannot be located in the path.
|
|
|
|
DefaultExecutableDir = "/usr/bin"
|
|
|
|
|
2022-04-08 05:24:39 +00:00
|
|
|
// NVIDIAContainerRuntimeHookExecutable is the executable name for the NVIDIA Container Runtime Hook
|
|
|
|
NVIDIAContainerRuntimeHookExecutable = "nvidia-container-runtime-hook"
|
|
|
|
// NVIDIAContainerToolkitExecutable is the executable name for the NVIDIA Container Toolkit (an alias for the NVIDIA Container Runtime Hook)
|
|
|
|
NVIDIAContainerToolkitExecutable = "nvidia-container-toolkit"
|
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
configDir = "/etc/"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the contents of the config.toml file for the NVIDIA Container Toolkit
|
|
|
|
// Note: This is currently duplicated by the HookConfig in cmd/nvidia-container-toolkit/hook_config.go
|
|
|
|
type Config struct {
|
2023-03-09 09:14:38 +00:00
|
|
|
AcceptEnvvarUnprivileged bool `toml:"accept-nvidia-visible-devices-envvar-when-unprivileged"`
|
|
|
|
|
2023-03-09 07:40:43 +00:00
|
|
|
NVIDIAContainerCLIConfig ContainerCLIConfig `toml:"nvidia-container-cli"`
|
|
|
|
NVIDIACTKConfig CTKConfig `toml:"nvidia-ctk"`
|
|
|
|
NVIDIAContainerRuntimeConfig RuntimeConfig `toml:"nvidia-container-runtime"`
|
|
|
|
NVIDIAContainerRuntimeHookConfig RuntimeHookConfig `toml:"nvidia-container-runtime-hook"`
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig sets up the config struct. Values are read from a toml file
|
|
|
|
// or set via the environment.
|
|
|
|
func GetConfig() (*Config, error) {
|
|
|
|
if XDGConfigDir := os.Getenv(configOverride); len(XDGConfigDir) != 0 {
|
|
|
|
configDir = XDGConfigDir
|
|
|
|
}
|
|
|
|
|
|
|
|
configFilePath := path.Join(configDir, configFilePath)
|
|
|
|
|
|
|
|
tomlFile, err := os.Open(configFilePath)
|
|
|
|
if err != nil {
|
2022-05-20 12:17:08 +00:00
|
|
|
return getDefaultConfig(), nil
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
defer tomlFile.Close()
|
|
|
|
|
|
|
|
cfg, err := loadConfigFrom(tomlFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read config values: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadRuntimeConfigFrom reads the config from the specified Reader
|
|
|
|
func loadConfigFrom(reader io.Reader) (*Config, error) {
|
|
|
|
toml, err := toml.LoadReader(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-09 11:56:26 +00:00
|
|
|
return getConfigFrom(toml)
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getConfigFrom reads the nvidia container runtime config from the specified toml Tree.
|
2022-05-09 11:56:26 +00:00
|
|
|
func getConfigFrom(toml *toml.Tree) (*Config, error) {
|
2022-03-29 10:01:12 +00:00
|
|
|
cfg := getDefaultConfig()
|
|
|
|
|
|
|
|
if toml == nil {
|
2022-05-09 11:56:26 +00:00
|
|
|
return cfg, nil
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 09:14:38 +00:00
|
|
|
cfg.AcceptEnvvarUnprivileged = toml.GetDefault("accept-nvidia-visible-devices-envvar-when-unprivileged", cfg.AcceptEnvvarUnprivileged).(bool)
|
|
|
|
|
2022-04-06 13:21:57 +00:00
|
|
|
cfg.NVIDIAContainerCLIConfig = *getContainerCLIConfigFrom(toml)
|
2022-03-29 12:02:40 +00:00
|
|
|
cfg.NVIDIACTKConfig = *getCTKConfigFrom(toml)
|
2022-05-09 11:56:26 +00:00
|
|
|
runtimeConfig, err := getRuntimeConfigFrom(toml)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load nvidia-container-runtime config: %v", err)
|
|
|
|
}
|
|
|
|
cfg.NVIDIAContainerRuntimeConfig = *runtimeConfig
|
2022-03-29 10:01:12 +00:00
|
|
|
|
2023-03-09 07:40:43 +00:00
|
|
|
runtimeHookConfig, err := getRuntimeHookConfigFrom(toml)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load nvidia-container-runtime-hook config: %v", err)
|
|
|
|
}
|
|
|
|
cfg.NVIDIAContainerRuntimeHookConfig = *runtimeHookConfig
|
|
|
|
|
2022-05-09 11:56:26 +00:00
|
|
|
return cfg, nil
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDefaultConfig defines the default values for the config
|
|
|
|
func getDefaultConfig() *Config {
|
|
|
|
c := Config{
|
2023-03-09 09:14:38 +00:00
|
|
|
AcceptEnvvarUnprivileged: true,
|
2022-04-06 13:21:57 +00:00
|
|
|
NVIDIAContainerCLIConfig: *getDefaultContainerCLIConfig(),
|
2022-03-29 12:02:40 +00:00
|
|
|
NVIDIACTKConfig: *getDefaultCTKConfig(),
|
2022-04-06 14:04:38 +00:00
|
|
|
NVIDIAContainerRuntimeConfig: *GetDefaultRuntimeConfig(),
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
2023-05-26 08:29:52 +00:00
|
|
|
|
|
|
|
// ResolveNVIDIAContainerRuntimeHookPath resolves the path the nvidia-container-runtime-hook binary.
|
|
|
|
func ResolveNVIDIAContainerRuntimeHookPath(logger *logrus.Logger, nvidiaContainerRuntimeHookPath string) string {
|
|
|
|
return resolveWithDefault(
|
|
|
|
logger,
|
|
|
|
"NVIDIA Container Runtime Hook",
|
|
|
|
nvidiaContainerRuntimeHookPath,
|
|
|
|
nvidiaContainerRuntimeHookDefaultPath,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveWithDefault resolves the path to the specified binary.
|
|
|
|
// If an absolute path is specified, it is used directly without searching for the binary.
|
|
|
|
// If the binary cannot be found in the path, the specified default is used instead.
|
|
|
|
func resolveWithDefault(logger *logrus.Logger, label string, path string, defaultPath string) string {
|
|
|
|
if filepath.IsAbs(path) {
|
|
|
|
logger.Debugf("Using specified %v path %v", label, path)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == "" {
|
|
|
|
path = filepath.Base(defaultPath)
|
|
|
|
}
|
|
|
|
logger.Debugf("Locating %v as %v", label, path)
|
|
|
|
lookup := lookup.NewExecutableLocator(logger, "")
|
|
|
|
|
|
|
|
resolvedPath := defaultPath
|
|
|
|
targets, err := lookup.Locate(path)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warnf("Failed to locate %v: %v", path, err)
|
|
|
|
} else {
|
|
|
|
logger.Debugf("Found %v candidates: %v", path, targets)
|
|
|
|
resolvedPath = targets[0]
|
|
|
|
}
|
|
|
|
logger.Debugf("Using %v path %v", label, path)
|
|
|
|
|
|
|
|
return resolvedPath
|
|
|
|
}
|