Skip to content

Commit

Permalink
[no-relnote] Use NVCT_CONFIG_FILE_PATH in toolkit install
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Oct 30, 2024
1 parent abf6b8a commit 91aadb7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 44 deletions.
5 changes: 0 additions & 5 deletions tools/container/toolkit/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type executable struct {
target executableTarget
env map[string]string
preLines []string
argLines []string
}

// install installs an executable component of the NVIDIA container toolkit. The source executable
Expand Down Expand Up @@ -128,10 +127,6 @@ func (e executable) writeWrapperTo(wrapper io.Writer, destFolder string, dotfile
// Add the call to the target executable
fmt.Fprintf(wrapper, "%s \\\n", dotfileName)

// Insert additional lines in the `arg` list
for _, line := range e.argLines {
fmt.Fprintf(wrapper, "\t%s \\\n", r.apply(line))
}
// Add the script arguments "$@"
fmt.Fprintln(wrapper, "\t\"$@\"")

Expand Down
17 changes: 0 additions & 17 deletions tools/container/toolkit/executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,6 @@ func TestWrapper(t *testing.T) {
"",
},
},
{
e: executable{
argLines: []string{
"argline1",
"argline2",
},
},
expectedLines: []string{
shebang,
"PATH=/dest/folder:$PATH \\",
"source.real \\",
"\targline1 \\",
"\targline2 \\",
"\t\"$@\"",
"",
},
},
}

for i, tc := range testCases {
Expand Down
13 changes: 7 additions & 6 deletions tools/container/toolkit/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"path/filepath"

"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/operator"
)

Expand All @@ -29,10 +30,10 @@ const (

// installContainerRuntimes sets up the NVIDIA container runtimes, copying the executables
// and implementing the required wrapper
func installContainerRuntimes(toolkitDir string, driverRoot string) error {
func installContainerRuntimes(toolkitDir string, configFilePath string) error {
runtimes := operator.GetRuntimes()
for _, runtime := range runtimes {
r := newNvidiaContainerRuntimeInstaller(runtime.Path)
r := newNvidiaContainerRuntimeInstaller(runtime.Path, configFilePath)

_, err := r.install(toolkitDir)
if err != nil {
Expand All @@ -46,17 +47,17 @@ func installContainerRuntimes(toolkitDir string, driverRoot string) error {
// This installer will copy the specified source executable to the toolkit directory.
// The executable is copied to a file with the same name as the source, but with a ".real" suffix and a wrapper is
// created to allow for the configuration of the runtime environment.
func newNvidiaContainerRuntimeInstaller(source string) *executable {
func newNvidiaContainerRuntimeInstaller(source string, configFilePath string) *executable {
wrapperName := filepath.Base(source)
dotfileName := wrapperName + ".real"
target := executableTarget{
dotfileName: dotfileName,
wrapperName: wrapperName,
}
return newRuntimeInstaller(source, target, nil)
return newRuntimeInstaller(source, target, configFilePath, nil)
}

func newRuntimeInstaller(source string, target executableTarget, env map[string]string) *executable {
func newRuntimeInstaller(source string, target executableTarget, configFilePath string, env map[string]string) *executable {
preLines := []string{
"",
"cat /proc/modules | grep -e \"^nvidia \" >/dev/null 2>&1",
Expand All @@ -68,7 +69,7 @@ func newRuntimeInstaller(source string, target executableTarget, env map[string]
}

runtimeEnv := make(map[string]string)
runtimeEnv["XDG_CONFIG_HOME"] = filepath.Join(destDirPattern, ".config")
runtimeEnv[config.FilePathOverrideEnvVar] = configFilePath
for k, v := range env {
runtimeEnv[k] = v
}
Expand Down
4 changes: 2 additions & 2 deletions tools/container/toolkit/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func TestNvidiaContainerRuntimeInstallerWrapper(t *testing.T) {
r := newNvidiaContainerRuntimeInstaller(nvidiaContainerRuntimeSource)
r := newNvidiaContainerRuntimeInstaller(nvidiaContainerRuntimeSource, "/config/file/path/config.toml")

const shebang = "#! /bin/sh"
const destFolder = "/dest/folder"
Expand All @@ -45,8 +45,8 @@ func TestNvidiaContainerRuntimeInstallerWrapper(t *testing.T) {
" exec runc \"$@\"",
"fi",
"",
"NVCTK_CONFIG_FILE_PATH=/config/file/path/config.toml \\",
"PATH=/dest/folder:$PATH \\",
"XDG_CONFIG_HOME=/dest/folder/.config \\",
"source.real \\",
"\t\"$@\"",
"",
Expand Down
25 changes: 11 additions & 14 deletions tools/container/toolkit/toolkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,9 @@ func Install(cli *cli.Context, opts *Options, toolkitRoot string) error {
log.Errorf("Ignoring error: %v", fmt.Errorf("error removing toolkit directory: %v", err))
}

toolkitConfigDir := filepath.Join(toolkitRoot, ".config", "nvidia-container-runtime")
toolkitConfigPath := filepath.Join(toolkitConfigDir, configFilename)
toolkitConfigFilePath := filepath.Join(toolkitRoot, ".config", config.RelativeFilePath)

err = createDirectories(toolkitRoot, toolkitConfigDir)
err = createDirectories(toolkitRoot, filepath.Dir(toolkitConfigFilePath))
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("could not create required directories: %v", err)
} else if err != nil {
Expand All @@ -314,7 +313,7 @@ func Install(cli *cli.Context, opts *Options, toolkitRoot string) error {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA container library: %v", err))
}

err = installContainerRuntimes(toolkitRoot, opts.DriverRoot)
err = installContainerRuntimes(toolkitRoot, toolkitConfigFilePath)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container runtime: %v", err)
} else if err != nil {
Expand All @@ -328,7 +327,7 @@ func Install(cli *cli.Context, opts *Options, toolkitRoot string) error {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA container CLI: %v", err))
}

nvidiaContainerRuntimeHookPath, err := installRuntimeHook(toolkitRoot, toolkitConfigPath)
nvidiaContainerRuntimeHookPath, err := installRuntimeHook(toolkitRoot, toolkitConfigFilePath)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container runtime hook: %v", err)
} else if err != nil {
Expand All @@ -349,7 +348,7 @@ func Install(cli *cli.Context, opts *Options, toolkitRoot string) error {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA Container CDI Hook CLI: %v", err))
}

err = installToolkitConfig(cli, toolkitConfigPath, nvidiaContainerCliExecutable, nvidiaCTKPath, nvidiaContainerRuntimeHookPath, opts)
err = installToolkitConfig(cli, toolkitConfigFilePath, nvidiaContainerCliExecutable, nvidiaCTKPath, nvidiaContainerRuntimeHookPath, opts)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container toolkit config: %v", err)
} else if err != nil {
Expand Down Expand Up @@ -423,8 +422,8 @@ func installLibrary(libName string, toolkitRoot string) error {

// installToolkitConfig installs the config file for the NVIDIA container toolkit ensuring
// that the settings are updated to match the desired install and nvidia driver directories.
func installToolkitConfig(c *cli.Context, toolkitConfigPath string, nvidiaContainerCliExecutablePath string, nvidiaCTKPath string, nvidaContainerRuntimeHookPath string, opts *Options) error {
log.Infof("Installing NVIDIA container toolkit config '%v'", toolkitConfigPath)
func installToolkitConfig(c *cli.Context, toolkitConfigFilePath string, nvidiaContainerCliExecutablePath string, nvidiaCTKPath string, nvidaContainerRuntimeHookPath string, opts *Options) error {
log.Infof("Installing NVIDIA container toolkit config '%v'", toolkitConfigFilePath)

cfg, err := config.New(
config.WithConfigFile(nvidiaContainerToolkitConfigSource),
Expand All @@ -433,7 +432,7 @@ func installToolkitConfig(c *cli.Context, toolkitConfigPath string, nvidiaContai
return fmt.Errorf("could not open source config file: %v", err)
}

targetConfig, err := os.Create(toolkitConfigPath)
targetConfig, err := os.Create(toolkitConfigFilePath)
if err != nil {
return fmt.Errorf("could not create target config file: %v", err)
}
Expand Down Expand Up @@ -579,17 +578,15 @@ func installContainerCLI(toolkitRoot string) (string, error) {
func installRuntimeHook(toolkitRoot string, configFilePath string) (string, error) {
log.Infof("Installing NVIDIA container runtime hook from '%v'", nvidiaContainerRuntimeHookSource)

argLines := []string{
fmt.Sprintf("-config \"%s\"", configFilePath),
}

e := executable{
source: nvidiaContainerRuntimeHookSource,
target: executableTarget{
dotfileName: "nvidia-container-runtime-hook.real",
wrapperName: "nvidia-container-runtime-hook",
},
argLines: argLines,
env: map[string]string{
config.FilePathOverrideEnvVar: configFilePath,
},
}

installedPath, err := e.install(toolkitRoot)
Expand Down

0 comments on commit 91aadb7

Please sign in to comment.