2021-10-11 14:31:02 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2021, 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-02-24 09:06:59 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/operator"
|
2021-10-11 14:31:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-02-24 09:06:59 +00:00
|
|
|
nvidiaContainerRuntimeSource = "/usr/bin/nvidia-container-runtime"
|
2021-10-11 14:31:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// installContainerRuntimes sets up the NVIDIA container runtimes, copying the executables
|
|
|
|
// and implementing the required wrapper
|
|
|
|
func installContainerRuntimes(toolkitDir string, driverRoot string) error {
|
2023-02-24 09:06:59 +00:00
|
|
|
runtimes := operator.GetRuntimes()
|
|
|
|
for _, runtime := range runtimes {
|
|
|
|
r := newNvidiaContainerRuntimeInstaller(runtime.Path)
|
2021-10-11 14:31:02 +00:00
|
|
|
|
2023-02-24 09:06:59 +00:00
|
|
|
_, err := r.install(toolkitDir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error installing NVIDIA container runtime: %v", err)
|
|
|
|
}
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-24 09:06:59 +00:00
|
|
|
// newNVidiaContainerRuntimeInstaller returns a new executable installer for the NVIDIA container runtime.
|
2023-08-25 14:14:06 +00:00
|
|
|
// This installer will copy the specified source executable to the toolkit directory.
|
2023-02-24 09:06:59 +00:00
|
|
|
// 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 {
|
|
|
|
wrapperName := filepath.Base(source)
|
|
|
|
dotfileName := wrapperName + ".real"
|
2021-10-11 14:31:02 +00:00
|
|
|
target := executableTarget{
|
2023-02-24 09:06:59 +00:00
|
|
|
dotfileName: dotfileName,
|
|
|
|
wrapperName: wrapperName,
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
2023-02-24 09:06:59 +00:00
|
|
|
return newRuntimeInstaller(source, target, nil)
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRuntimeInstaller(source string, target executableTarget, env map[string]string) *executable {
|
|
|
|
preLines := []string{
|
|
|
|
"",
|
|
|
|
"cat /proc/modules | grep -e \"^nvidia \" >/dev/null 2>&1",
|
|
|
|
"if [ \"${?}\" != \"0\" ]; then",
|
|
|
|
" echo \"nvidia driver modules are not yet loaded, invoking runc directly\"",
|
|
|
|
" exec runc \"$@\"",
|
|
|
|
"fi",
|
|
|
|
"",
|
|
|
|
}
|
|
|
|
|
|
|
|
runtimeEnv := make(map[string]string)
|
|
|
|
runtimeEnv["XDG_CONFIG_HOME"] = filepath.Join(destDirPattern, ".config")
|
|
|
|
for k, v := range env {
|
|
|
|
runtimeEnv[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
r := executable{
|
|
|
|
source: source,
|
|
|
|
target: target,
|
|
|
|
env: runtimeEnv,
|
|
|
|
preLines: preLines,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &r
|
|
|
|
}
|