2019-10-22 21:36:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-13 13:22:18 +00:00
|
|
|
"fmt"
|
2019-10-22 21:36:22 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-01-22 14:33:23 +00:00
|
|
|
"reflect"
|
2022-09-30 03:05:56 +00:00
|
|
|
"strings"
|
2019-10-22 21:36:22 +00:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2022-04-06 14:05:26 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2019-10-22 21:36:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configPath = "/etc/nvidia-container-runtime/config.toml"
|
|
|
|
driverPath = "/run/nvidia/driver"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaultPaths = [...]string{
|
|
|
|
path.Join(driverPath, configPath),
|
|
|
|
configPath,
|
|
|
|
}
|
|
|
|
|
2020-07-24 11:41:38 +00:00
|
|
|
// CLIConfig : options for nvidia-container-cli.
|
2019-10-22 21:36:22 +00:00
|
|
|
type CLIConfig struct {
|
2020-07-20 14:13:49 +00:00
|
|
|
Root *string `toml:"root"`
|
|
|
|
Path *string `toml:"path"`
|
|
|
|
Environment []string `toml:"environment"`
|
|
|
|
Debug *string `toml:"debug"`
|
|
|
|
Ldcache *string `toml:"ldcache"`
|
|
|
|
LoadKmods bool `toml:"load-kmods"`
|
|
|
|
NoPivot bool `toml:"no-pivot"`
|
|
|
|
NoCgroups bool `toml:"no-cgroups"`
|
|
|
|
User *string `toml:"user"`
|
|
|
|
Ldconfig *string `toml:"ldconfig"`
|
2019-10-22 21:36:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 09:52:23 +00:00
|
|
|
// HookConfig : options for the nvidia-container-runtime-hook.
|
2019-10-22 21:36:22 +00:00
|
|
|
type HookConfig struct {
|
2021-11-09 19:19:56 +00:00
|
|
|
DisableRequire bool `toml:"disable-require"`
|
|
|
|
SwarmResource *string `toml:"swarm-resource"`
|
|
|
|
AcceptEnvvarUnprivileged bool `toml:"accept-nvidia-visible-devices-envvar-when-unprivileged"`
|
|
|
|
AcceptDeviceListAsVolumeMounts bool `toml:"accept-nvidia-visible-devices-as-volume-mounts"`
|
|
|
|
SupportedDriverCapabilities DriverCapabilities `toml:"supported-driver-capabilities"`
|
2019-10-22 21:36:22 +00:00
|
|
|
|
2023-03-09 07:40:43 +00:00
|
|
|
NvidiaContainerCLI CLIConfig `toml:"nvidia-container-cli"`
|
|
|
|
NVIDIAContainerRuntime config.RuntimeConfig `toml:"nvidia-container-runtime"`
|
|
|
|
NVIDIAContainerRuntimeHook config.RuntimeHookConfig `toml:"nvidia-container-runtime-hook"`
|
2019-10-22 21:36:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
func getDefaultHookConfig() (HookConfig, error) {
|
|
|
|
rtConfig, err := config.GetDefaultRuntimeConfig()
|
|
|
|
if err != nil {
|
|
|
|
return HookConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rtHookConfig, err := config.GetDefaultRuntimeHookConfig()
|
|
|
|
if err != nil {
|
|
|
|
return HookConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := HookConfig{
|
2020-08-07 14:24:32 +00:00
|
|
|
DisableRequire: false,
|
|
|
|
SwarmResource: nil,
|
|
|
|
AcceptEnvvarUnprivileged: true,
|
|
|
|
AcceptDeviceListAsVolumeMounts: false,
|
2021-11-09 19:19:56 +00:00
|
|
|
SupportedDriverCapabilities: allDriverCapabilities,
|
2019-10-22 21:36:22 +00:00
|
|
|
NvidiaContainerCLI: CLIConfig{
|
2020-07-20 14:13:49 +00:00
|
|
|
Root: nil,
|
|
|
|
Path: nil,
|
|
|
|
Environment: []string{},
|
|
|
|
Debug: nil,
|
|
|
|
Ldcache: nil,
|
|
|
|
LoadKmods: true,
|
|
|
|
NoPivot: false,
|
|
|
|
NoCgroups: false,
|
|
|
|
User: nil,
|
|
|
|
Ldconfig: nil,
|
2019-10-22 21:36:22 +00:00
|
|
|
},
|
2023-03-13 13:22:18 +00:00
|
|
|
NVIDIAContainerRuntime: *rtConfig,
|
|
|
|
NVIDIAContainerRuntimeHook: *rtHookConfig,
|
2019-10-22 21:36:22 +00:00
|
|
|
}
|
2023-03-13 13:22:18 +00:00
|
|
|
|
|
|
|
return c, nil
|
2019-10-22 21:36:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
func getHookConfig() (*HookConfig, error) {
|
2019-10-22 21:36:22 +00:00
|
|
|
var err error
|
2023-03-13 13:22:18 +00:00
|
|
|
var config HookConfig
|
2019-10-22 21:36:22 +00:00
|
|
|
|
|
|
|
if len(*configflag) > 0 {
|
2023-03-13 13:22:18 +00:00
|
|
|
config, err = getDefaultHookConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get default configuration: %v", err)
|
|
|
|
}
|
2019-10-22 21:36:22 +00:00
|
|
|
_, err = toml.DecodeFile(*configflag, &config)
|
|
|
|
if err != nil {
|
2023-03-13 13:22:18 +00:00
|
|
|
return nil, fmt.Errorf("couldn't open configuration file: %v", err)
|
2019-10-22 21:36:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, p := range defaultPaths {
|
2023-03-13 13:22:18 +00:00
|
|
|
config, err = getDefaultHookConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get default configuration: %v", err)
|
|
|
|
}
|
2019-10-22 21:36:22 +00:00
|
|
|
_, err = toml.DecodeFile(p, &config)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
} else if !os.IsNotExist(err) {
|
2023-03-13 13:22:18 +00:00
|
|
|
return nil, fmt.Errorf("couldn't open default configuration file: %v", err)
|
2019-10-22 21:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 19:19:56 +00:00
|
|
|
if config.SupportedDriverCapabilities == all {
|
|
|
|
config.SupportedDriverCapabilities = allDriverCapabilities
|
|
|
|
}
|
|
|
|
// We ensure that the supported-driver-capabilites option is a subset of allDriverCapabilities
|
|
|
|
if intersection := allDriverCapabilities.Intersection(config.SupportedDriverCapabilities); intersection != config.SupportedDriverCapabilities {
|
|
|
|
configName := config.getConfigOption("SupportedDriverCapabilities")
|
|
|
|
log.Panicf("Invalid value for config option '%v'; %v (supported: %v)\n", configName, config.SupportedDriverCapabilities, allDriverCapabilities)
|
|
|
|
}
|
|
|
|
|
2023-03-13 13:22:18 +00:00
|
|
|
return &config, nil
|
2019-10-22 21:36:22 +00:00
|
|
|
}
|
2021-01-22 14:33:23 +00:00
|
|
|
|
|
|
|
// getConfigOption returns the toml config option associated with the
|
|
|
|
// specified struct field.
|
|
|
|
func (c HookConfig) getConfigOption(fieldName string) string {
|
|
|
|
t := reflect.TypeOf(c)
|
|
|
|
f, ok := t.FieldByName(fieldName)
|
|
|
|
if !ok {
|
|
|
|
return fieldName
|
|
|
|
}
|
|
|
|
v, ok := f.Tag.Lookup("toml")
|
|
|
|
if !ok {
|
|
|
|
return fieldName
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2022-09-30 03:05:56 +00:00
|
|
|
|
|
|
|
// getSwarmResourceEnvvars returns the swarm resource envvars for the config.
|
|
|
|
func (c *HookConfig) getSwarmResourceEnvvars() []string {
|
|
|
|
if c.SwarmResource == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates := strings.Split(*c.SwarmResource, ",")
|
|
|
|
|
|
|
|
var envvars []string
|
|
|
|
for _, c := range candidates {
|
|
|
|
trimmed := strings.TrimSpace(c)
|
|
|
|
if len(trimmed) > 0 {
|
|
|
|
envvars = append(envvars, trimmed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return envvars
|
|
|
|
}
|