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"
|
2022-05-09 13:42:59 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
|
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"
|
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
|
|
|
// newNVIDIAContainerRuntime is a factory method that constructs a runtime based on the selected configuration and specified logger
|
2022-03-29 10:01:12 +00:00
|
|
|
func newNVIDIAContainerRuntime(logger *logrus.Logger, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
specModifier, err := newSpecModifier(logger, cfg, ociSpec, argv)
|
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.
|
2022-05-04 12:06:28 +00:00
|
|
|
func newSpecModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec, argv []string) (oci.SpecModifier, error) {
|
2022-06-02 13:41:04 +00:00
|
|
|
modeModifier, err := newModeModifier(logger, cfg, ociSpec, argv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:52:43 +00:00
|
|
|
graphicsModifier, err := modifier.NewGraphicsModifier(logger, cfg, ociSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-02 13:41:04 +00:00
|
|
|
gdsModifier, err := modifier.NewGDSModifier(logger, cfg, ociSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mofedModifier, err := modifier.NewMOFEDModifier(logger, cfg, ociSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-08 13:42:31 +00:00
|
|
|
tegraModifier, err := modifier.NewTegraPlatformFiles(logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-02 13:41:04 +00:00
|
|
|
modifiers := modifier.Merge(
|
|
|
|
modeModifier,
|
2022-07-20 08:52:43 +00:00
|
|
|
graphicsModifier,
|
2022-06-02 13:41:04 +00:00
|
|
|
gdsModifier,
|
|
|
|
mofedModifier,
|
2022-08-08 13:42:31 +00:00
|
|
|
tegraModifier,
|
2022-06-02 13:41:04 +00:00
|
|
|
)
|
|
|
|
return modifiers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newModeModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec, argv []string) (oci.SpecModifier, error) {
|
2022-05-09 13:42:59 +00:00
|
|
|
switch info.ResolveAutoMode(logger, cfg.NVIDIAContainerRuntimeConfig.Mode) {
|
|
|
|
case "legacy":
|
2022-04-06 13:18:12 +00:00
|
|
|
return modifier.NewStableRuntimeModifier(logger), nil
|
2022-05-09 13:42:59 +00:00
|
|
|
case "csv":
|
|
|
|
return modifier.NewCSVModifier(logger, cfg, ociSpec)
|
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
|
|
|
}
|