2021-10-11 14:31:02 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2020-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"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2023-04-24 11:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
|
2023-06-09 15:17:54 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
|
2021-10-11 14:31:02 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
cli "github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
restartModeSignal = "signal"
|
|
|
|
restartModeSystemd = "systemd"
|
2022-02-18 14:38:11 +00:00
|
|
|
restartModeNone = "none"
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
nvidiaRuntimeName = "nvidia"
|
|
|
|
nvidiaRuntimeBinary = "nvidia-container-runtime"
|
|
|
|
nvidiaExperimentalRuntimeName = "nvidia-experimental"
|
2023-02-24 15:16:27 +00:00
|
|
|
nvidiaExperimentalRuntimeBinary = "nvidia-container-runtime.experimental"
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
defaultConfig = "/etc/containerd/config.toml"
|
|
|
|
defaultSocket = "/run/containerd/containerd.sock"
|
|
|
|
defaultRuntimeClass = "nvidia"
|
2021-11-22 10:12:13 +00:00
|
|
|
defaultRuntmeType = "io.containerd.runc.v2"
|
2021-10-11 14:31:02 +00:00
|
|
|
defaultSetAsDefault = true
|
|
|
|
defaultRestartMode = restartModeSignal
|
|
|
|
defaultHostRootMount = "/host"
|
|
|
|
|
|
|
|
reloadBackoff = 5 * time.Second
|
|
|
|
maxReloadAttempts = 6
|
|
|
|
|
|
|
|
socketMessageToGetPID = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
// nvidiaRuntimeBinaries defines a map of runtime names to binary names
|
|
|
|
var nvidiaRuntimeBinaries = map[string]string{
|
|
|
|
nvidiaRuntimeName: nvidiaRuntimeBinary,
|
|
|
|
nvidiaExperimentalRuntimeName: nvidiaExperimentalRuntimeBinary,
|
|
|
|
}
|
|
|
|
|
|
|
|
// options stores the configuration from the command line or environment variables
|
|
|
|
type options struct {
|
2023-06-09 15:17:54 +00:00
|
|
|
container.Options
|
2023-06-09 14:50:51 +00:00
|
|
|
|
|
|
|
// containerd-specific options
|
2021-10-11 14:31:02 +00:00
|
|
|
useLegacyConfig bool
|
2023-06-09 14:50:51 +00:00
|
|
|
runtimeType string
|
2023-03-23 19:12:23 +00:00
|
|
|
|
|
|
|
ContainerRuntimeModesCDIAnnotationPrefixes cli.StringSlice
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
options := options{}
|
|
|
|
|
|
|
|
// Create the top-level CLI
|
|
|
|
c := cli.NewApp()
|
|
|
|
c.Name = "containerd"
|
|
|
|
c.Usage = "Update a containerd config with the nvidia-container-runtime"
|
|
|
|
c.Version = "0.1.0"
|
|
|
|
|
|
|
|
// Create the 'setup' subcommand
|
|
|
|
setup := cli.Command{}
|
|
|
|
setup.Name = "setup"
|
|
|
|
setup.Usage = "Trigger a containerd config to be updated"
|
|
|
|
setup.ArgsUsage = "<runtime_dirname>"
|
|
|
|
setup.Action = func(c *cli.Context) error {
|
|
|
|
return Setup(c, &options)
|
|
|
|
}
|
2023-06-09 14:50:51 +00:00
|
|
|
setup.Before = func(c *cli.Context) error {
|
2023-06-09 15:17:54 +00:00
|
|
|
return container.ParseArgs(c, &options.Options)
|
2023-06-09 14:50:51 +00:00
|
|
|
}
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
// Create the 'cleanup' subcommand
|
|
|
|
cleanup := cli.Command{}
|
|
|
|
cleanup.Name = "cleanup"
|
|
|
|
cleanup.Usage = "Trigger any updates made to a containerd config to be undone"
|
|
|
|
cleanup.ArgsUsage = "<runtime_dirname>"
|
|
|
|
cleanup.Action = func(c *cli.Context) error {
|
|
|
|
return Cleanup(c, &options)
|
|
|
|
}
|
2023-06-09 14:50:51 +00:00
|
|
|
cleanup.Before = func(c *cli.Context) error {
|
2023-06-09 15:17:54 +00:00
|
|
|
return container.ParseArgs(c, &options.Options)
|
2023-06-09 14:50:51 +00:00
|
|
|
}
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
// Register the subcommands with the top-level CLI
|
|
|
|
c.Commands = []*cli.Command{
|
|
|
|
&setup,
|
|
|
|
&cleanup,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup common flags across both subcommands. All subcommands get the same
|
|
|
|
// set of flags even if they don't use some of them. This is so that we
|
|
|
|
// only require the user to specify one set of flags for both 'startup'
|
|
|
|
// and 'cleanup' to simplify things.
|
|
|
|
commonFlags := []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "Path to the containerd config file",
|
|
|
|
Value: defaultConfig,
|
2023-06-09 15:17:54 +00:00
|
|
|
Destination: &options.Config,
|
2023-03-24 09:54:58 +00:00
|
|
|
EnvVars: []string{"CONTAINERD_CONFIG", "RUNTIME_CONFIG"},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "socket",
|
|
|
|
Usage: "Path to the containerd socket file",
|
|
|
|
Value: defaultSocket,
|
2023-06-09 15:17:54 +00:00
|
|
|
Destination: &options.Socket,
|
2023-03-24 09:54:58 +00:00
|
|
|
EnvVars: []string{"CONTAINERD_SOCKET", "RUNTIME_SOCKET"},
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "restart-mode",
|
|
|
|
Usage: "Specify how containerd should be restarted; If 'none' is selected, it will not be restarted [signal | systemd | none]",
|
|
|
|
Value: defaultRestartMode,
|
2023-06-09 15:17:54 +00:00
|
|
|
Destination: &options.RestartMode,
|
2023-03-24 09:54:58 +00:00
|
|
|
EnvVars: []string{"CONTAINERD_RESTART_MODE", "RUNTIME_RESTART_MODE"},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "runtime-class",
|
2023-03-24 09:54:58 +00:00
|
|
|
Aliases: []string{"runtime-name", "nvidia-runtime-name"},
|
2021-10-11 14:31:02 +00:00
|
|
|
Usage: "The name of the runtime class to set for the nvidia-container-runtime",
|
|
|
|
Value: defaultRuntimeClass,
|
2023-06-09 15:17:54 +00:00
|
|
|
Destination: &options.RuntimeName,
|
2023-03-24 09:54:58 +00:00
|
|
|
EnvVars: []string{"CONTAINERD_RUNTIME_CLASS", "NVIDIA_RUNTIME_NAME"},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
2023-06-09 14:50:51 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "nvidia-runtime-dir",
|
|
|
|
Aliases: []string{"runtime-dir"},
|
|
|
|
Usage: "The path where the nvidia-container-runtime binaries are located. If this is not specified, the first argument will be used instead",
|
2023-06-09 15:17:54 +00:00
|
|
|
Destination: &options.RuntimeDir,
|
2023-06-09 14:50:51 +00:00
|
|
|
EnvVars: []string{"NVIDIA_RUNTIME_DIR"},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "set-as-default",
|
|
|
|
Usage: "Set nvidia-container-runtime as the default runtime",
|
|
|
|
Value: defaultSetAsDefault,
|
2023-06-09 15:17:54 +00:00
|
|
|
Destination: &options.SetAsDefault,
|
2023-03-24 09:54:58 +00:00
|
|
|
EnvVars: []string{"CONTAINERD_SET_AS_DEFAULT", "NVIDIA_RUNTIME_SET_AS_DEFAULT"},
|
2021-10-11 14:31:02 +00:00
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "host-root",
|
|
|
|
Usage: "Specify the path to the host root to be used when restarting containerd using systemd",
|
|
|
|
Value: defaultHostRootMount,
|
2023-06-09 15:17:54 +00:00
|
|
|
Destination: &options.HostRootMount,
|
2021-10-11 14:31:02 +00:00
|
|
|
EnvVars: []string{"HOST_ROOT_MOUNT"},
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "use-legacy-config",
|
|
|
|
Usage: "Specify whether a legacy (pre v1.3) config should be used",
|
|
|
|
Destination: &options.useLegacyConfig,
|
|
|
|
EnvVars: []string{"CONTAINERD_USE_LEGACY_CONFIG"},
|
|
|
|
},
|
2023-03-24 09:54:58 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "runtime-type",
|
|
|
|
Usage: "The runtime_type to use for the configured runtime classes",
|
|
|
|
Value: defaultRuntmeType,
|
|
|
|
Destination: &options.runtimeType,
|
|
|
|
EnvVars: []string{"CONTAINERD_RUNTIME_TYPE"},
|
|
|
|
},
|
2023-03-23 19:12:23 +00:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "nvidia-container-runtime-modes.cdi.annotation-prefixes",
|
|
|
|
Destination: &options.ContainerRuntimeModesCDIAnnotationPrefixes,
|
|
|
|
EnvVars: []string{"NVIDIA_CONTAINER_RUNTIME_MODES_CDI_ANNOTATION_PREFIXES"},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the subcommand flags with the common subcommand flags
|
|
|
|
setup.Flags = append([]cli.Flag{}, commonFlags...)
|
|
|
|
cleanup.Flags = append([]cli.Flag{}, commonFlags...)
|
|
|
|
|
|
|
|
// Run the top-level CLI
|
|
|
|
if err := c.Run(os.Args); err != nil {
|
|
|
|
log.Fatal(fmt.Errorf("Error: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup updates a containerd configuration to include the nvidia-containerd-runtime and reloads it
|
|
|
|
func Setup(c *cli.Context, o *options) error {
|
|
|
|
log.Infof("Starting 'setup' for %v", c.App.Name)
|
|
|
|
|
2023-02-23 20:01:24 +00:00
|
|
|
cfg, err := containerd.New(
|
2023-06-09 15:17:54 +00:00
|
|
|
containerd.WithPath(o.Config),
|
2023-02-23 20:01:24 +00:00
|
|
|
containerd.WithRuntimeType(o.runtimeType),
|
|
|
|
containerd.WithUseLegacyConfig(o.useLegacyConfig),
|
2023-03-23 19:12:23 +00:00
|
|
|
containerd.WithContainerAnnotations(o.containerAnnotationsFromCDIPrefixes()...),
|
2023-02-23 20:01:24 +00:00
|
|
|
)
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-06-09 15:17:54 +00:00
|
|
|
err = o.Configure(cfg)
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
2023-06-09 15:17:54 +00:00
|
|
|
return fmt.Errorf("unable to configure containerd: %v", err)
|
2023-02-23 20:01:24 +00:00
|
|
|
}
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
err = RestartContainerd(o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restart containerd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Completed 'setup' for %v", c.App.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup reverts a containerd configuration to remove the nvidia-containerd-runtime and reloads it
|
|
|
|
func Cleanup(c *cli.Context, o *options) error {
|
|
|
|
log.Infof("Starting 'cleanup' for %v", c.App.Name)
|
|
|
|
|
2023-02-23 20:01:24 +00:00
|
|
|
cfg, err := containerd.New(
|
2023-06-09 15:17:54 +00:00
|
|
|
containerd.WithPath(o.Config),
|
2023-02-23 20:01:24 +00:00
|
|
|
containerd.WithRuntimeType(o.runtimeType),
|
|
|
|
containerd.WithUseLegacyConfig(o.useLegacyConfig),
|
2023-03-23 19:12:23 +00:00
|
|
|
containerd.WithContainerAnnotations(o.containerAnnotationsFromCDIPrefixes()...),
|
2023-02-23 20:01:24 +00:00
|
|
|
)
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-06-09 15:17:54 +00:00
|
|
|
err = o.Unconfigure(cfg)
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
2023-06-09 15:17:54 +00:00
|
|
|
return fmt.Errorf("unable to unconfigure containerd: %v", err)
|
2023-02-23 20:01:24 +00:00
|
|
|
}
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
err = RestartContainerd(o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restart containerd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Completed 'cleanup' for %v", c.App.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartContainerd restarts containerd depending on the value of restartModeFlag
|
|
|
|
func RestartContainerd(o *options) error {
|
2023-06-09 15:17:54 +00:00
|
|
|
switch o.RestartMode {
|
2021-10-11 14:31:02 +00:00
|
|
|
case restartModeNone:
|
2023-06-09 15:17:54 +00:00
|
|
|
log.Warnf("Skipping sending signal to containerd due to --restart-mode=%v", o.RestartMode)
|
2021-10-11 14:31:02 +00:00
|
|
|
return nil
|
|
|
|
case restartModeSignal:
|
|
|
|
err := SignalContainerd(o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to signal containerd: %v", err)
|
|
|
|
}
|
|
|
|
case restartModeSystemd:
|
2023-06-09 15:17:54 +00:00
|
|
|
return RestartContainerdSystemd(o.HostRootMount)
|
2021-10-11 14:31:02 +00:00
|
|
|
default:
|
2023-06-09 15:17:54 +00:00
|
|
|
return fmt.Errorf("Invalid restart mode specified: %v", o.RestartMode)
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignalContainerd sends a SIGHUP signal to the containerd daemon
|
|
|
|
func SignalContainerd(o *options) error {
|
|
|
|
log.Infof("Sending SIGHUP signal to containerd")
|
|
|
|
|
|
|
|
// Wrap the logic to perform the SIGHUP in a function so we can retry it on failure
|
|
|
|
retriable := func() error {
|
2023-06-09 15:17:54 +00:00
|
|
|
conn, err := net.Dial("unix", o.Socket)
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to dial: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
sconn, err := conn.(*net.UnixConn).SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get syscall connection: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err1 := sconn.Control(func(fd uintptr) {
|
|
|
|
err = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_PASSCRED, 1)
|
|
|
|
})
|
|
|
|
if err1 != nil {
|
|
|
|
return fmt.Errorf("unable to issue call on socket fd: %v", err1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to SetsockoptInt on socket fd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = conn.(*net.UnixConn).WriteMsgUnix([]byte(socketMessageToGetPID), nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to WriteMsgUnix on socket fd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oob := make([]byte, 1024)
|
|
|
|
_, oobn, _, _, err := conn.(*net.UnixConn).ReadMsgUnix(nil, oob)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to ReadMsgUnix on socket fd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oob = oob[:oobn]
|
|
|
|
scm, err := syscall.ParseSocketControlMessage(oob)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to ParseSocketControlMessage from message received on socket fd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ucred, err := syscall.ParseUnixCredentials(&scm[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to ParseUnixCredentials from message received on socket fd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = syscall.Kill(int(ucred.Pid), syscall.SIGHUP)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to send SIGHUP to 'containerd' process: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to send a SIGHUP up to maxReloadAttempts times
|
|
|
|
var err error
|
|
|
|
for i := 0; i < maxReloadAttempts; i++ {
|
|
|
|
err = retriable()
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == maxReloadAttempts-1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Warnf("Error signaling containerd, attempt %v/%v: %v", i+1, maxReloadAttempts, err)
|
|
|
|
time.Sleep(reloadBackoff)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Max retries reached %v/%v, aborting", maxReloadAttempts, maxReloadAttempts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Successfully signaled containerd")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartContainerdSystemd restarts containerd using systemctl
|
|
|
|
func RestartContainerdSystemd(hostRootMount string) error {
|
|
|
|
log.Infof("Restarting containerd using systemd and host root mounted at %v", hostRootMount)
|
|
|
|
|
|
|
|
command := "chroot"
|
|
|
|
args := []string{hostRootMount, "systemctl", "restart", "containerd"}
|
|
|
|
|
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error restarting containerd using systemd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-23 19:12:23 +00:00
|
|
|
|
|
|
|
// containerAnnotationsFromCDIPrefixes returns the container annotations to set for the given CDI prefixes.
|
|
|
|
func (o *options) containerAnnotationsFromCDIPrefixes() []string {
|
|
|
|
var annotations []string
|
|
|
|
for _, prefix := range o.ContainerRuntimeModesCDIAnnotationPrefixes.Value() {
|
|
|
|
annotations = append(annotations, prefix+"*")
|
|
|
|
}
|
|
|
|
|
|
|
|
return annotations
|
|
|
|
}
|