2021-09-06 11:26:48 +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.
|
|
|
|
*/
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
package modifier
|
2021-09-06 11:26:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-04-08 05:18:49 +00:00
|
|
|
"path/filepath"
|
2021-09-06 11:26:48 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-04-08 05:18:49 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2021-09-06 11:30:15 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
2021-09-06 11:26:48 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2022-02-22 14:09:45 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-06 11:26:48 +00:00
|
|
|
)
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
// NewStableRuntimeModifier creates an OCI spec modifier that inserts the NVIDIA Container Runtime Hook into an OCI
|
|
|
|
// spec. The specified logger is used to capture log output.
|
|
|
|
func NewStableRuntimeModifier(logger *logrus.Logger) oci.SpecModifier {
|
|
|
|
m := stableRuntimeModifier{logger: logger}
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
return &m
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
// stableRuntimeModifier modifies an OCI spec inplace, inserting the nvidia-container-runtime-hook as a
|
2022-01-31 14:11:36 +00:00
|
|
|
// prestart hook. If the hook is already present, no modification is made.
|
2022-02-22 14:09:45 +00:00
|
|
|
type stableRuntimeModifier struct {
|
|
|
|
logger *logrus.Logger
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
// Modify applies the required modification to the incoming OCI spec, inserting the nvidia-container-runtime-hook
|
|
|
|
// as a prestart hook.
|
2022-02-22 14:09:45 +00:00
|
|
|
func (m stableRuntimeModifier) Modify(spec *specs.Spec) error {
|
2022-04-08 05:24:39 +00:00
|
|
|
path, err := exec.LookPath(config.NVIDIAContainerRuntimeHookExecutable)
|
2021-09-06 11:26:48 +00:00
|
|
|
if err != nil {
|
2022-04-08 05:24:39 +00:00
|
|
|
path = filepath.Join(config.DefaultExecutableDir, config.NVIDIAContainerRuntimeHookExecutable)
|
2021-09-06 11:26:48 +00:00
|
|
|
_, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
m.logger.Infof("Using prestart hook path: %s", path)
|
2021-09-06 11:26:48 +00:00
|
|
|
|
|
|
|
args := []string{path}
|
|
|
|
if spec.Hooks == nil {
|
|
|
|
spec.Hooks = &specs.Hooks{}
|
|
|
|
} else if len(spec.Hooks.Prestart) != 0 {
|
|
|
|
for _, hook := range spec.Hooks.Prestart {
|
2022-04-08 05:24:39 +00:00
|
|
|
if strings.Contains(hook.Path, config.NVIDIAContainerRuntimeHookExecutable) {
|
2022-02-22 14:09:45 +00:00
|
|
|
m.logger.Infof("existing nvidia prestart hook found in OCI spec")
|
|
|
|
return nil
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spec.Hooks.Prestart = append(spec.Hooks.Prestart, specs.Hook{
|
|
|
|
Path: path,
|
|
|
|
Args: append(args, "prestart"),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|