mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-04-07 05:54:25 +00:00
Support container runtime other than runc
Signed-off-by: R0CKSTAR <yeahdongcn@gmail.com>
This commit is contained in:
parent
37a00041c4
commit
e7f84c06cf
@ -17,7 +17,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
||||
)
|
||||
@ -26,8 +31,19 @@ const (
|
||||
ociSpecFileName = "config.json"
|
||||
dockerRuncExecutableName = "docker-runc"
|
||||
runcExecutableName = "runc"
|
||||
nvidiaRuntimeName = "nvidia"
|
||||
runcRuntimeName = "runc"
|
||||
dockerDefaultConfig = "/etc/docker/daemon.json"
|
||||
)
|
||||
|
||||
type dockerDaemon struct {
|
||||
Runtimes map[string]dockerRuntime `json:"runtimes,omitempty"`
|
||||
}
|
||||
|
||||
type dockerRuntime struct {
|
||||
Path *string `json:"path,omitempty"`
|
||||
}
|
||||
|
||||
// newRuntime is a factory method that constructs a runtime based on the selected configuration.
|
||||
func newRuntime(argv []string) (oci.Runtime, error) {
|
||||
ociSpec, err := newOCISpec(argv)
|
||||
@ -35,12 +51,18 @@ func newRuntime(argv []string) (oci.Runtime, error) {
|
||||
return nil, fmt.Errorf("error constructing OCI specification: %v", err)
|
||||
}
|
||||
|
||||
runc, err := newRuncRuntime()
|
||||
runtime, err := newDefaultRuntime()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error constructing runc runtime: %v", err)
|
||||
logger.Errorf("Error constructing default runtime: %v", err)
|
||||
|
||||
runc, err := newRuncRuntime()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error constructing runc runtime: %v", err)
|
||||
}
|
||||
runtime = runc
|
||||
}
|
||||
|
||||
r, err := newNvidiaContainerRuntimeWithLogger(logger.Logger, runc, ociSpec)
|
||||
r, err := newNvidiaContainerRuntimeWithLogger(logger.Logger, runtime, ociSpec)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error constructing NVIDIA Container Runtime: %v", err)
|
||||
}
|
||||
@ -72,3 +94,47 @@ func newRuncRuntime() (oci.Runtime, error) {
|
||||
runcExecutableName,
|
||||
)
|
||||
}
|
||||
|
||||
func dockerRuntimeExecutablePath(name string) (string, error) {
|
||||
file, err := os.Open(dockerDefaultConfig)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
bytes, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
daemon := dockerDaemon{}
|
||||
if err := json.Unmarshal(bytes, &daemon); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return *daemon.Runtimes[name].Path, nil
|
||||
}
|
||||
|
||||
// newDefaultRuntime locates the default runtime binary and wraps it in a SyscallExecRuntime
|
||||
func newDefaultRuntime() (oci.Runtime, error) {
|
||||
cmd := exec.Command("docker", "info", "--format", "{{.DefaultRuntime}}")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting docker default runtime: %v", err)
|
||||
}
|
||||
defaultRuntimeName := strings.TrimSpace(string(output))
|
||||
if defaultRuntimeName == nvidiaRuntimeName || defaultRuntimeName == runcRuntimeName {
|
||||
return nil, fmt.Errorf("docker default runtime is %v, bail out: %v", defaultRuntimeName, err)
|
||||
}
|
||||
candidates := []string{}
|
||||
defaultRuntimeExecutablePath, err := dockerRuntimeExecutablePath(defaultRuntimeName)
|
||||
if err != nil {
|
||||
logger.Errorf("Error getting docker default runtime (%v)'s executable path: %v", defaultRuntimeName, err)
|
||||
candidates = append(candidates, defaultRuntimeName)
|
||||
} else {
|
||||
candidates = append(candidates, defaultRuntimeExecutablePath)
|
||||
}
|
||||
return oci.NewLowLevelRuntimeWithLogger(
|
||||
logger.Logger,
|
||||
candidates...,
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user