2021-10-11 14:31:02 +00:00
|
|
|
/**
|
2022-08-19 08:26:34 +00:00
|
|
|
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
|
2021-10-11 14:31:02 +00:00
|
|
|
#
|
|
|
|
# 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.
|
2022-08-19 08:26:34 +00:00
|
|
|
**/
|
|
|
|
|
2021-10-11 14:31:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-06-24 05:28:10 +00:00
|
|
|
"os/exec"
|
2021-10-11 14:31:02 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2021-09-06 09:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2022-06-24 05:28:10 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/crio"
|
|
|
|
"github.com/pelletier/go-toml"
|
2021-10-11 14:31:02 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
cli "github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-06-24 05:28:10 +00:00
|
|
|
restartModeSystemd = "systemd"
|
|
|
|
restartModeNone = "none"
|
|
|
|
|
|
|
|
defaultConfigMode = "hook"
|
|
|
|
|
|
|
|
// Hook-based settings
|
2021-10-11 14:31:02 +00:00
|
|
|
defaultHooksDir = "/usr/share/containers/oci/hooks.d"
|
|
|
|
defaultHookFilename = "oci-nvidia-hook.json"
|
2022-06-24 05:28:10 +00:00
|
|
|
|
|
|
|
// Config-based settings
|
|
|
|
defaultConfig = "/etc/crio/crio.conf"
|
|
|
|
defaultRuntimeClass = "nvidia"
|
|
|
|
defaultSetAsDefault = true
|
|
|
|
defaultRestartMode = restartModeSystemd
|
|
|
|
defaultHostRootMount = "/host"
|
2021-10-11 14:31:02 +00:00
|
|
|
)
|
|
|
|
|
2022-06-24 05:28:10 +00:00
|
|
|
// options stores the configuration from the command linek or environment variables
|
2022-06-24 05:03:04 +00:00
|
|
|
type options struct {
|
2022-06-24 05:28:10 +00:00
|
|
|
configMode string
|
|
|
|
|
2022-06-24 05:03:04 +00:00
|
|
|
hooksDir string
|
|
|
|
hookFilename string
|
|
|
|
runtimeDir string
|
2022-06-24 05:28:10 +00:00
|
|
|
|
|
|
|
config string
|
|
|
|
runtimeClass string
|
|
|
|
setAsDefault bool
|
|
|
|
restartMode string
|
|
|
|
hostRootMount string
|
2022-06-24 05:03:04 +00:00
|
|
|
}
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
func main() {
|
2022-06-24 05:03:04 +00:00
|
|
|
options := options{}
|
|
|
|
|
2021-10-11 14:31:02 +00:00
|
|
|
// Create the top-level CLI
|
|
|
|
c := cli.NewApp()
|
|
|
|
c.Name = "crio"
|
|
|
|
c.Usage = "Update cri-o hooks to include the NVIDIA runtime hook"
|
|
|
|
c.ArgsUsage = "<toolkit_dirname>"
|
|
|
|
c.Version = "0.1.0"
|
|
|
|
|
|
|
|
// Create the 'setup' subcommand
|
|
|
|
setup := cli.Command{}
|
|
|
|
setup.Name = "setup"
|
2022-06-24 05:28:10 +00:00
|
|
|
setup.Usage = "Configure cri-o for NVIDIA GPU containers"
|
2021-10-11 14:31:02 +00:00
|
|
|
setup.ArgsUsage = "<toolkit_dirname>"
|
2022-06-24 05:03:04 +00:00
|
|
|
setup.Action = func(c *cli.Context) error {
|
|
|
|
return Setup(c, &options)
|
|
|
|
}
|
|
|
|
setup.Before = func(c *cli.Context) error {
|
|
|
|
return ParseArgs(c, &options)
|
|
|
|
}
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
// Create the 'cleanup' subcommand
|
|
|
|
cleanup := cli.Command{}
|
|
|
|
cleanup.Name = "cleanup"
|
2022-06-24 05:28:10 +00:00
|
|
|
cleanup.Usage = "Remove the NVIDIA-specific cri-o configuration"
|
2022-06-24 05:03:04 +00:00
|
|
|
cleanup.Action = func(c *cli.Context) error {
|
|
|
|
return Cleanup(c, &options)
|
|
|
|
}
|
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: "hooks-dir",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "path to the cri-o hooks directory",
|
|
|
|
Value: defaultHooksDir,
|
2022-06-24 05:03:04 +00:00
|
|
|
Destination: &options.hooksDir,
|
2021-10-11 14:31:02 +00:00
|
|
|
EnvVars: []string{"CRIO_HOOKS_DIR"},
|
|
|
|
DefaultText: defaultHooksDir,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "hook-filename",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Usage: "filename of the cri-o hook that will be created / removed in the hooks directory",
|
|
|
|
Value: defaultHookFilename,
|
2022-06-24 05:03:04 +00:00
|
|
|
Destination: &options.hookFilename,
|
2021-10-11 14:31:02 +00:00
|
|
|
EnvVars: []string{"CRIO_HOOK_FILENAME"},
|
|
|
|
DefaultText: defaultHookFilename,
|
|
|
|
},
|
2022-06-24 05:28:10 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config-mode",
|
|
|
|
Usage: "the configuration mode to use. One of [hook | config]",
|
|
|
|
Value: defaultConfigMode,
|
|
|
|
Destination: &options.configMode,
|
|
|
|
EnvVars: []string{"CRIO_CONFIG_MODE"},
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "Path to the cri-o config file",
|
|
|
|
Value: defaultConfig,
|
|
|
|
Destination: &options.config,
|
|
|
|
EnvVars: []string{"CRIO_CONFIG"},
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "runtime-class",
|
|
|
|
Usage: "The name of the runtime class to set for the nvidia-container-runtime",
|
|
|
|
Value: defaultRuntimeClass,
|
|
|
|
Destination: &options.runtimeClass,
|
|
|
|
EnvVars: []string{"CRIO_RUNTIME_CLASS"},
|
|
|
|
},
|
|
|
|
// The flags below are only used by the 'setup' command.
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "set-as-default",
|
|
|
|
Usage: "Set nvidia-container-runtime as the default runtime",
|
|
|
|
Value: defaultSetAsDefault,
|
|
|
|
Destination: &options.setAsDefault,
|
|
|
|
EnvVars: []string{"CRIO_SET_AS_DEFAULT"},
|
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "restart-mode",
|
|
|
|
Usage: "Specify how cri-o should be restarted; If 'none' is selected, it will not be restarted [systemd | none]",
|
|
|
|
Value: defaultRestartMode,
|
|
|
|
Destination: &options.restartMode,
|
|
|
|
EnvVars: []string{"CRIO_RESTART_MODE"},
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "host-root",
|
|
|
|
Usage: "Specify the path to the host root to be used when restarting crio using systemd",
|
|
|
|
Value: defaultHostRootMount,
|
|
|
|
Destination: &options.hostRootMount,
|
|
|
|
EnvVars: []string{"HOST_ROOT_MOUNT"},
|
|
|
|
},
|
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 installs the prestart hook required to launch GPU-enabled containers
|
2022-06-24 05:03:04 +00:00
|
|
|
func Setup(c *cli.Context, o *options) error {
|
2021-10-11 14:31:02 +00:00
|
|
|
log.Infof("Starting 'setup' for %v", c.App.Name)
|
|
|
|
|
2022-06-24 05:28:10 +00:00
|
|
|
switch o.configMode {
|
|
|
|
case "hook":
|
|
|
|
return setupHook(o)
|
|
|
|
case "config":
|
|
|
|
return setupConfig(o)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid config-mode '%v'", o.configMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupHook installs the prestart hook required to launch GPU-enabled containers
|
|
|
|
func setupHook(o *options) error {
|
|
|
|
log.Infof("Installing prestart hook")
|
|
|
|
|
2022-06-24 05:03:04 +00:00
|
|
|
err := os.MkdirAll(o.hooksDir, 0755)
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
2022-06-24 05:03:04 +00:00
|
|
|
return fmt.Errorf("error creating hooks directory %v: %v", o.hooksDir, err)
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 05:03:04 +00:00
|
|
|
hookPath := getHookPath(o.hooksDir, o.hookFilename)
|
|
|
|
err = createHook(o.runtimeDir, hookPath)
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating hook: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-24 05:28:10 +00:00
|
|
|
// setupConfig updates the cri-o config for the NVIDIA container runtime
|
|
|
|
func setupConfig(o *options) error {
|
|
|
|
log.Infof("Updating config file")
|
|
|
|
|
|
|
|
cfg, err := crio.LoadConfig(o.config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = UpdateConfig(cfg, o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to update config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = crio.FlushConfig(o.config, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to flush config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RestartCrio(o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restart crio: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 14:31:02 +00:00
|
|
|
// Cleanup removes the specified prestart hook
|
2022-06-24 05:03:04 +00:00
|
|
|
func Cleanup(c *cli.Context, o *options) error {
|
2021-10-11 14:31:02 +00:00
|
|
|
log.Infof("Starting 'cleanup' for %v", c.App.Name)
|
|
|
|
|
2022-06-24 05:28:10 +00:00
|
|
|
switch o.configMode {
|
|
|
|
case "hook":
|
|
|
|
return cleanupHook(o)
|
|
|
|
case "config":
|
|
|
|
return cleanupConfig(o)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid config-mode '%v'", o.configMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupHook removes the prestart hook
|
|
|
|
func cleanupHook(o *options) error {
|
|
|
|
log.Infof("Removing prestart hook")
|
|
|
|
|
2022-06-24 05:03:04 +00:00
|
|
|
hookPath := getHookPath(o.hooksDir, o.hookFilename)
|
2021-10-11 14:31:02 +00:00
|
|
|
err := os.Remove(hookPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error removing hook '%v': %v", hookPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-24 05:28:10 +00:00
|
|
|
// cleanupConfig removes the NVIDIA container runtime from the cri-o config
|
|
|
|
func cleanupConfig(o *options) error {
|
|
|
|
log.Infof("Reverting config file modifications")
|
|
|
|
|
|
|
|
cfg, err := crio.LoadConfig(o.config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RevertConfig(cfg, o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to update config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = crio.FlushConfig(o.config, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to flush config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RestartCrio(o)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restart crio: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 14:31:02 +00:00
|
|
|
// ParseArgs parses the command line arguments to the CLI
|
2022-06-24 05:03:04 +00:00
|
|
|
func ParseArgs(c *cli.Context, o *options) error {
|
2021-10-11 14:31:02 +00:00
|
|
|
args := c.Args()
|
|
|
|
|
|
|
|
log.Infof("Parsing arguments: %v", args.Slice())
|
|
|
|
if c.NArg() != 1 {
|
|
|
|
return fmt.Errorf("incorrect number of arguments")
|
|
|
|
}
|
2022-06-24 05:03:04 +00:00
|
|
|
o.runtimeDir = args.Get(0)
|
2021-10-11 14:31:02 +00:00
|
|
|
log.Infof("Successfully parsed arguments")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createHook(toolkitDir string, hookPath string) error {
|
|
|
|
hook, err := os.Create(hookPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating hook file '%v': %v", hookPath, err)
|
|
|
|
}
|
|
|
|
defer hook.Close()
|
|
|
|
|
|
|
|
encoder := json.NewEncoder(hook)
|
2022-06-24 05:03:04 +00:00
|
|
|
err = encoder.Encode(generateOciHook(toolkitDir))
|
2021-10-11 14:31:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error writing hook file '%v': %v", hookPath, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHookPath(hooksDir string, hookFilename string) string {
|
|
|
|
return filepath.Join(hooksDir, hookFilename)
|
|
|
|
}
|
|
|
|
|
2022-08-19 08:26:34 +00:00
|
|
|
func generateOciHook(toolkitDir string) podmanHook {
|
2021-09-06 09:58:03 +00:00
|
|
|
hookPath := filepath.Join(toolkitDir, config.NVIDIAContainerRuntimeHookExecutable)
|
2021-10-11 14:31:02 +00:00
|
|
|
envPath := "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:" + toolkitDir
|
|
|
|
always := true
|
|
|
|
|
2022-08-19 08:26:34 +00:00
|
|
|
hook := podmanHook{
|
2021-10-11 14:31:02 +00:00
|
|
|
Version: "1.0.0",
|
|
|
|
Stages: []string{"prestart"},
|
2022-08-19 08:26:34 +00:00
|
|
|
Hook: specHook{
|
2021-10-11 14:31:02 +00:00
|
|
|
Path: hookPath,
|
2021-09-06 09:58:03 +00:00
|
|
|
Args: []string{filepath.Base(config.NVIDIAContainerRuntimeHookExecutable), "prestart"},
|
2021-10-11 14:31:02 +00:00
|
|
|
Env: []string{envPath},
|
|
|
|
},
|
2022-08-19 08:26:34 +00:00
|
|
|
When: When{
|
2021-10-11 14:31:02 +00:00
|
|
|
Always: &always,
|
|
|
|
Commands: []string{".*"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return hook
|
|
|
|
}
|
2022-06-24 05:28:10 +00:00
|
|
|
|
|
|
|
// UpdateConfig updates the cri-o config to include the NVIDIA Container Runtime
|
|
|
|
func UpdateConfig(config *toml.Tree, o *options) error {
|
|
|
|
runtimePath := filepath.Join(o.runtimeDir, "nvidia-container-runtime")
|
|
|
|
return crio.UpdateConfig(config, o.runtimeClass, runtimePath, o.setAsDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevertConfig reverts the cri-o config to remove the NVIDIA Container Runtime
|
|
|
|
func RevertConfig(config *toml.Tree, o *options) error {
|
|
|
|
return crio.RevertConfig(config, o.runtimeClass)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartCrio restarts crio depending on the value of restartModeFlag
|
|
|
|
func RestartCrio(o *options) error {
|
|
|
|
switch o.restartMode {
|
|
|
|
case restartModeNone:
|
|
|
|
log.Warnf("Skipping restart of crio due to --restart-mode=%v", o.restartMode)
|
|
|
|
return nil
|
|
|
|
case restartModeSystemd:
|
|
|
|
return RestartCrioSystemd(o.hostRootMount)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid restart mode specified: %v", o.restartMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartCrioSystemd restarts cri-o using systemctl
|
|
|
|
func RestartCrioSystemd(hostRootMount string) error {
|
|
|
|
log.Infof("Restarting cri-o using systemd and host root mounted at %v", hostRootMount)
|
|
|
|
|
|
|
|
command := "chroot"
|
|
|
|
args := []string{hostRootMount, "systemctl", "restart", "crio"}
|
|
|
|
|
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error restarting crio using systemd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|