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 (
|
2023-03-13 14:02:10 +00:00
|
|
|
"bufio"
|
2022-03-29 10:01:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-05-10 13:12:44 +00:00
|
|
|
"path/filepath"
|
2023-03-13 12:33:49 +00:00
|
|
|
"strings"
|
2022-03-29 10:01:12 +00:00
|
|
|
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-05-10 13:12:44 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
2023-03-13 12:33:49 +00:00
|
|
|
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
|
2022-03-29 10:01:12 +00:00
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configOverride = "XDG_CONFIG_HOME"
|
|
|
|
configFilePath = "nvidia-container-runtime/config.toml"
|
2023-05-10 13:12:44 +00:00
|
|
|
|
|
|
|
nvidiaCTKExecutable = "nvidia-ctk"
|
|
|
|
nvidiaCTKDefaultFilePath = "/usr/bin/nvidia-ctk"
|
2023-05-24 08:21:50 +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 {
|
2023-03-13 13:22:18 +00:00
|
|
|
return getDefaultConfig()
|
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) {
|
2023-03-13 13:22:18 +00:00
|
|
|
cfg, err := getDefaultConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-29 10:01:12 +00:00
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
if err := toml.Unmarshal(cfg); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal config: %v", err)
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
2023-03-09 09:14:38 +00:00
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
// getDefaultConfig defines the default values for the config
|
|
|
|
func getDefaultConfig() (*Config, error) {
|
|
|
|
tomlConfig, err := GetDefaultConfigToml()
|
2022-05-09 11:56:26 +00:00
|
|
|
if err != nil {
|
2023-03-13 13:22:18 +00:00
|
|
|
return nil, err
|
2022-05-09 11:56:26 +00:00
|
|
|
}
|
2022-03-29 10:01:12 +00:00
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
// tomlConfig above includes information about the default values and comments.
|
|
|
|
// we need to marshal it back to a string and then unmarshal it to strip the comments.
|
|
|
|
contents, err := tomlConfig.ToTomlString()
|
2023-03-09 07:40:43 +00:00
|
|
|
if err != nil {
|
2023-03-13 13:22:18 +00:00
|
|
|
return nil, err
|
2023-03-09 07:40:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
reloaded, err := toml.Load(contents)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-29 10:01:12 +00:00
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
d := Config{}
|
|
|
|
if err := reloaded.Unmarshal(&d); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal config: %v", err)
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
// The default value for the accept-nvidia-visible-devices-envvar-when-unprivileged is non-standard.
|
|
|
|
// As such we explicitly handle it being set here.
|
|
|
|
if reloaded.Get("accept-nvidia-visible-devices-envvar-when-unprivileged") == nil {
|
|
|
|
d.AcceptEnvvarUnprivileged = true
|
|
|
|
}
|
|
|
|
// The default value for the nvidia-container-runtime.debug is non-standard.
|
|
|
|
// As such we explicitly handle it being set here.
|
|
|
|
if reloaded.Get("nvidia-container-runtime.debug") == nil {
|
|
|
|
d.NVIDIAContainerRuntimeConfig.DebugFilePath = "/dev/null"
|
|
|
|
}
|
|
|
|
return &d, nil
|
2022-03-29 10:01:12 +00:00
|
|
|
}
|
2023-03-13 12:33:49 +00:00
|
|
|
|
|
|
|
// GetDefaultConfigToml returns the default config as a toml Tree.
|
|
|
|
func GetDefaultConfigToml() (*toml.Tree, error) {
|
|
|
|
tree, err := toml.TreeFromMap(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree.Set("disable-require", false)
|
|
|
|
tree.SetWithComment("swarm-resource", "", true, "DOCKER_RESOURCE_GPU")
|
|
|
|
tree.SetWithComment("accept-nvidia-visible-devices-envvar-when-unprivileged", "", true, true)
|
|
|
|
tree.SetWithComment("accept-nvidia-visible-devices-as-volume-mounts", "", true, false)
|
|
|
|
|
|
|
|
// nvidia-container-cli
|
|
|
|
tree.SetWithComment("nvidia-container-cli.root", "", true, "/run/nvidia/driver")
|
|
|
|
tree.SetWithComment("nvidia-container-cli.path", "", true, "/usr/bin/nvidia-container-cli")
|
|
|
|
tree.Set("nvidia-container-cli.environment", []string{})
|
|
|
|
tree.SetWithComment("nvidia-container-cli.debug", "", true, "/var/log/nvidia-container-toolkit.log")
|
|
|
|
tree.SetWithComment("nvidia-container-cli.ldcache", "", true, "/etc/ld.so.cache")
|
|
|
|
tree.Set("nvidia-container-cli.load-kmods", true)
|
|
|
|
tree.SetWithComment("nvidia-container-cli.no-cgroups", "", true, false)
|
2023-03-13 14:02:10 +00:00
|
|
|
|
|
|
|
tree.SetWithComment("nvidia-container-cli.user", "", getCommentedUserGroup(), getUserGroup())
|
2023-03-13 12:33:49 +00:00
|
|
|
tree.Set("nvidia-container-cli.ldconfig", getLdConfigPath())
|
|
|
|
|
|
|
|
// nvidia-container-runtime
|
|
|
|
tree.SetWithComment("nvidia-container-runtime.debug", "", true, "/var/log/nvidia-container-runtime.log")
|
|
|
|
tree.Set("nvidia-container-runtime.log-level", "info")
|
|
|
|
|
|
|
|
commentLines := []string{
|
|
|
|
"Specify the runtimes to consider. This list is processed in order and the PATH",
|
|
|
|
"searched for matching executables unless the entry is an absolute path.",
|
|
|
|
}
|
|
|
|
tree.SetWithComment("nvidia-container-runtime.runtimes", strings.Join(commentLines, "\n "), false, []string{"docker-runc", "runc"})
|
|
|
|
|
|
|
|
tree.Set("nvidia-container-runtime.mode", "auto")
|
|
|
|
|
|
|
|
tree.Set("nvidia-container-runtime.modes.csv.mount-spec-path", "/etc/nvidia-container-runtime/host-files-for-container.d")
|
|
|
|
tree.Set("nvidia-container-runtime.modes.cdi.default-kind", "nvidia.com/gpu")
|
|
|
|
tree.Set("nvidia-container-runtime.modes.cdi.annotation-prefixes", []string{cdi.AnnotationPrefix})
|
|
|
|
|
|
|
|
// nvidia-ctk
|
2023-05-10 13:12:44 +00:00
|
|
|
tree.Set("nvidia-ctk.path", nvidiaCTKExecutable)
|
2023-03-13 12:33:49 +00:00
|
|
|
|
2023-05-24 08:21:50 +00:00
|
|
|
// nvidia-container-runtime-hook
|
|
|
|
tree.Set("nvidia-container-runtime-hook.path", nvidiaContainerRuntimeHookExecutable)
|
|
|
|
|
2023-03-13 12:33:49 +00:00
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLdConfigPath() string {
|
2023-03-13 12:38:10 +00:00
|
|
|
if _, err := os.Stat("/sbin/ldconfig.real"); err == nil {
|
|
|
|
return "@/sbin/ldconfig.real"
|
|
|
|
}
|
|
|
|
return "@/sbin/ldconfig"
|
2023-03-13 12:33:49 +00:00
|
|
|
}
|
2023-03-13 14:02:10 +00:00
|
|
|
|
|
|
|
// getUserGroup returns the user and group to use for the nvidia-container-cli and whether the config option should be commented.
|
|
|
|
func getUserGroup() string {
|
|
|
|
return "root:video"
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCommentedUserGroup returns whether the nvidia-container-cli user and group config option should be commented.
|
|
|
|
func getCommentedUserGroup() bool {
|
|
|
|
uncommentIf := map[string]bool{
|
|
|
|
"suse": true,
|
|
|
|
"opensuse": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
idsLike := getDistIDLike()
|
|
|
|
for _, id := range idsLike {
|
|
|
|
if uncommentIf[id] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDistIDLike returns the ID_LIKE field from /etc/os-release.
|
|
|
|
func getDistIDLike() []string {
|
|
|
|
releaseFile, err := os.Open("/etc/os-release")
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer releaseFile.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(releaseFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "ID_LIKE=") {
|
|
|
|
value := strings.Trim(strings.TrimPrefix(line, "ID_LIKE="), "\"")
|
|
|
|
return strings.Split(value, " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-10 13:12:44 +00:00
|
|
|
|
|
|
|
// 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.
|
2023-03-22 12:27:43 +00:00
|
|
|
func ResolveNVIDIACTKPath(logger logger.Interface, nvidiaCTKPath string) string {
|
2023-05-24 08:21:50 +00:00
|
|
|
return resolveWithDefault(
|
|
|
|
logger,
|
|
|
|
"NVIDIA Container Toolkit CLI",
|
|
|
|
nvidiaCTKPath,
|
|
|
|
nvidiaCTKDefaultFilePath,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveNVIDIAContainerRuntimeHookPath resolves the path the nvidia-container-runtime-hook binary.
|
2023-03-22 12:27:43 +00:00
|
|
|
func ResolveNVIDIAContainerRuntimeHookPath(logger logger.Interface, nvidiaContainerRuntimeHookPath string) string {
|
2023-05-24 08:21:50 +00:00
|
|
|
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.
|
2023-03-22 12:27:43 +00:00
|
|
|
func resolveWithDefault(logger logger.Interface, label string, path string, defaultPath string) string {
|
2023-05-24 08:21:50 +00:00
|
|
|
if filepath.IsAbs(path) {
|
|
|
|
logger.Debugf("Using specified %v path %v", label, path)
|
|
|
|
return path
|
2023-05-10 13:12:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 08:21:50 +00:00
|
|
|
if path == "" {
|
|
|
|
path = filepath.Base(defaultPath)
|
2023-05-10 13:12:44 +00:00
|
|
|
}
|
2023-05-24 08:21:50 +00:00
|
|
|
logger.Debugf("Locating %v as %v", label, path)
|
2023-05-10 13:12:44 +00:00
|
|
|
lookup := lookup.NewExecutableLocator(logger, "")
|
2023-05-24 08:21:50 +00:00
|
|
|
|
|
|
|
resolvedPath := defaultPath
|
|
|
|
targets, err := lookup.Locate(path)
|
2023-05-10 13:12:44 +00:00
|
|
|
if err != nil {
|
2023-06-06 19:46:38 +00:00
|
|
|
logger.Warningf("Failed to locate %v: %v", path, err)
|
2023-05-10 13:12:44 +00:00
|
|
|
} else {
|
2023-05-24 08:21:50 +00:00
|
|
|
logger.Debugf("Found %v candidates: %v", path, targets)
|
|
|
|
resolvedPath = targets[0]
|
2023-05-10 13:12:44 +00:00
|
|
|
}
|
2023-05-24 08:21:50 +00:00
|
|
|
logger.Debugf("Using %v path %v", label, path)
|
2023-05-10 13:12:44 +00:00
|
|
|
|
2023-05-24 08:21:50 +00:00
|
|
|
return resolvedPath
|
2023-05-10 13:12:44 +00:00
|
|
|
}
|