2021-09-06 11:26:48 +00:00
|
|
|
/*
|
2022-02-22 14:09:45 +00:00
|
|
|
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
|
2021-09-06 11:26:48 +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.
|
|
|
|
*/
|
|
|
|
|
2023-02-07 20:09:53 +00:00
|
|
|
package runtime
|
2021-09-06 11:26:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-03-15 15:27:34 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2023-06-05 14:01:20 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
|
2022-05-09 13:42:59 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2022-06-15 12:12:17 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/modifier"
|
2021-09-06 11:30:15 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
2021-09-06 11:26:48 +00:00
|
|
|
)
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
// newNVIDIAContainerRuntime is a factory method that constructs a runtime based on the selected configuration and specified logger
|
2023-03-22 12:27:43 +00:00
|
|
|
func newNVIDIAContainerRuntime(logger logger.Interface, cfg *config.Config, argv []string) (oci.Runtime, error) {
|
2022-05-04 12:39:14 +00:00
|
|
|
lowLevelRuntime, err := oci.NewLowLevelRuntime(logger, cfg.NVIDIAContainerRuntimeConfig.Runtimes)
|
2021-09-06 11:26:48 +00:00
|
|
|
if err != nil {
|
2022-01-31 14:11:36 +00:00
|
|
|
return nil, fmt.Errorf("error constructing low-level runtime: %v", err)
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:06:28 +00:00
|
|
|
if !oci.HasCreateSubcommand(argv) {
|
|
|
|
logger.Debugf("Skipping modifier for non-create subcommand")
|
|
|
|
return lowLevelRuntime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ociSpec, err := oci.NewSpec(logger, argv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error constructing OCI specification: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-06-22 13:37:36 +00:00
|
|
|
specModifier, err := newSpecModifier(logger, cfg, ociSpec)
|
2022-04-06 13:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct OCI spec modifier: %v", err)
|
2022-03-24 11:45:03 +00:00
|
|
|
}
|
2022-02-22 14:09:45 +00:00
|
|
|
|
|
|
|
// Create the wrapping runtime with the specified modifier
|
2023-02-07 20:24:44 +00:00
|
|
|
r := oci.NewModifyingRuntimeWrapper(
|
2022-02-22 14:09:45 +00:00
|
|
|
logger,
|
|
|
|
lowLevelRuntime,
|
|
|
|
ociSpec,
|
|
|
|
specModifier,
|
|
|
|
)
|
2021-09-06 11:26:48 +00:00
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
2022-04-06 13:18:12 +00:00
|
|
|
|
|
|
|
// newSpecModifier is a factory method that creates constructs an OCI spec modifer based on the provided config.
|
2023-06-22 13:37:36 +00:00
|
|
|
func newSpecModifier(logger logger.Interface, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) {
|
2023-06-05 14:01:20 +00:00
|
|
|
rawSpec, err := ociSpec.Load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load OCI spec: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
image, err := image.NewCUDAImageFromSpec(rawSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-05 14:03:03 +00:00
|
|
|
mode := info.ResolveAutoMode(logger, cfg.NVIDIAContainerRuntimeConfig.Mode, image)
|
2023-06-22 13:36:43 +00:00
|
|
|
modeModifier, err := newModeModifier(logger, mode, cfg, ociSpec, image)
|
2022-06-02 13:41:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-05 13:01:58 +00:00
|
|
|
// For CDI mode we make no additional modifications.
|
|
|
|
if mode == "cdi" {
|
|
|
|
return modeModifier, nil
|
|
|
|
}
|
2022-06-02 13:41:04 +00:00
|
|
|
|
2023-06-05 14:01:20 +00:00
|
|
|
graphicsModifier, err := modifier.NewGraphicsModifier(logger, cfg, image)
|
2022-07-20 08:52:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:23:28 +00:00
|
|
|
featureModifier, err := modifier.NewFeatureGatedModifier(logger, cfg, image)
|
2022-06-02 13:41:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiers := modifier.Merge(
|
|
|
|
modeModifier,
|
2022-07-20 08:52:43 +00:00
|
|
|
graphicsModifier,
|
2023-11-22 13:23:28 +00:00
|
|
|
featureModifier,
|
2022-06-02 13:41:04 +00:00
|
|
|
)
|
|
|
|
return modifiers, nil
|
|
|
|
}
|
|
|
|
|
2023-06-22 13:36:43 +00:00
|
|
|
func newModeModifier(logger logger.Interface, mode string, cfg *config.Config, ociSpec oci.Spec, image image.CUDA) (oci.SpecModifier, error) {
|
2023-06-05 13:01:58 +00:00
|
|
|
switch mode {
|
2022-05-09 13:42:59 +00:00
|
|
|
case "legacy":
|
2023-05-24 08:21:50 +00:00
|
|
|
return modifier.NewStableRuntimeModifier(logger, cfg.NVIDIAContainerRuntimeHookConfig.Path), nil
|
2022-05-09 13:42:59 +00:00
|
|
|
case "csv":
|
2023-06-22 13:36:43 +00:00
|
|
|
return modifier.NewCSVModifier(logger, cfg, image)
|
2022-06-20 15:03:02 +00:00
|
|
|
case "cdi":
|
|
|
|
return modifier.NewCDIModifier(logger, cfg, ociSpec)
|
2022-04-06 13:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 13:42:59 +00:00
|
|
|
return nil, fmt.Errorf("invalid runtime mode: %v", cfg.NVIDIAContainerRuntimeConfig.Mode)
|
2022-04-06 13:18:12 +00:00
|
|
|
}
|