2022-06-20 11:53:16 +00:00
/ * *
# Copyright ( c ) 2022 , 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-11-01 19:05:23 +00:00
package generate
2022-06-20 11:53:16 +00:00
import (
"fmt"
"os"
"path/filepath"
"strings"
2023-02-17 09:11:18 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
2022-11-23 15:29:18 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
2022-12-02 13:17:52 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi"
2023-02-22 14:26:41 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
2022-10-21 11:29:21 +00:00
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
2022-06-20 11:53:16 +00:00
specs "github.com/container-orchestrated-devices/container-device-interface/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/device"
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
)
const (
2023-02-06 17:53:24 +00:00
allDeviceName = "all"
2022-06-20 11:53:16 +00:00
)
type command struct {
logger * logrus . Logger
}
type config struct {
2023-01-20 14:58:04 +00:00
output string
format string
deviceNameStrategy string
2023-02-02 14:42:01 +00:00
driverRoot string
2023-01-20 14:58:04 +00:00
nvidiaCTKPath string
2023-03-06 09:00:12 +00:00
mode string
2022-06-20 11:53:16 +00:00
}
// NewCommand constructs a generate-cdi command with the specified logger
func NewCommand ( logger * logrus . Logger ) * cli . Command {
c := command {
logger : logger ,
}
return c . build ( )
}
// build creates the CLI command
func ( m command ) build ( ) * cli . Command {
cfg := config { }
// Create the 'generate-cdi' command
c := cli . Command {
2022-11-01 19:05:23 +00:00
Name : "generate" ,
2022-06-20 11:53:16 +00:00
Usage : "Generate CDI specifications for use with CDI-enabled runtimes" ,
2022-11-23 20:39:06 +00:00
Before : func ( c * cli . Context ) error {
return m . validateFlags ( c , & cfg )
} ,
2022-06-20 11:53:16 +00:00
Action : func ( c * cli . Context ) error {
return m . run ( c , & cfg )
} ,
}
c . Flags = [ ] cli . Flag {
& cli . StringFlag {
Name : "output" ,
2022-11-23 20:39:06 +00:00
Usage : "Specify the file to output the generated CDI specification to. If this is '' the specification is output to STDOUT" ,
2022-06-20 11:53:16 +00:00
Destination : & cfg . output ,
} ,
2022-11-23 20:39:06 +00:00
& cli . StringFlag {
Name : "format" ,
Usage : "The output format for the generated spec [json | yaml]. This overrides the format defined by the output file extension (if specified)." ,
2023-02-22 14:33:30 +00:00
Value : spec . FormatYAML ,
2022-11-23 20:39:06 +00:00
Destination : & cfg . format ,
2022-06-20 11:53:16 +00:00
} ,
2023-02-08 14:29:35 +00:00
& cli . StringFlag {
2023-03-06 09:00:12 +00:00
Name : "mode" ,
Aliases : [ ] string { "discovery-mode" } ,
2023-02-20 14:27:34 +00:00
Usage : "The mode to use when discovering the available entities. One of [auto | nvml | wsl]. If mode is set to 'auto' the mode will be determined based on the system configuration." ,
Value : nvcdi . ModeAuto ,
2023-03-06 09:00:12 +00:00
Destination : & cfg . mode ,
2023-02-08 14:29:35 +00:00
} ,
2023-01-20 14:58:04 +00:00
& cli . StringFlag {
Name : "device-name-strategy" ,
2023-01-30 12:36:11 +00:00
Usage : "Specify the strategy for generating device names. One of [index | uuid | type-index]" ,
2022-12-02 13:17:52 +00:00
Value : nvcdi . DeviceNameStrategyIndex ,
2023-01-20 14:58:04 +00:00
Destination : & cfg . deviceNameStrategy ,
} ,
2022-12-01 16:21:35 +00:00
& cli . StringFlag {
2023-02-02 14:42:01 +00:00
Name : "driver-root" ,
Usage : "Specify the NVIDIA GPU driver root to use when discovering the entities that should be included in the CDI specification." ,
Destination : & cfg . driverRoot ,
2022-12-01 16:21:35 +00:00
} ,
2022-12-12 14:26:21 +00:00
& cli . StringFlag {
Name : "nvidia-ctk-path" ,
Usage : "Specify the path to use for the nvidia-ctk in the generated CDI specification. If this is left empty, the path will be searched." ,
Destination : & cfg . nvidiaCTKPath ,
} ,
2022-06-20 11:53:16 +00:00
}
return & c
}
2023-02-22 14:26:41 +00:00
func ( m command ) validateFlags ( c * cli . Context , cfg * config ) error {
2022-11-23 20:39:06 +00:00
cfg . format = strings . ToLower ( cfg . format )
switch cfg . format {
2023-02-22 14:33:30 +00:00
case spec . FormatJSON :
case spec . FormatYAML :
2022-11-23 20:39:06 +00:00
default :
return fmt . Errorf ( "invalid output format: %v" , cfg . format )
}
2023-03-06 09:00:12 +00:00
cfg . mode = strings . ToLower ( cfg . mode )
switch cfg . mode {
2023-02-20 14:27:34 +00:00
case nvcdi . ModeAuto :
case nvcdi . ModeNvml :
case nvcdi . ModeWsl :
2023-03-01 11:22:57 +00:00
case nvcdi . ModeManagement :
2023-02-08 14:29:35 +00:00
default :
2023-03-06 09:00:12 +00:00
return fmt . Errorf ( "invalid discovery mode: %v" , cfg . mode )
2023-02-08 14:29:35 +00:00
}
2022-12-02 13:17:52 +00:00
_ , err := nvcdi . NewDeviceNamer ( cfg . deviceNameStrategy )
2023-01-20 14:58:04 +00:00
if err != nil {
return err
}
2023-02-17 09:11:18 +00:00
cfg . nvidiaCTKPath = discover . FindNvidiaCTK ( m . logger , cfg . nvidiaCTKPath )
2022-11-23 20:39:06 +00:00
if outputFileFormat := formatFromFilename ( cfg . output ) ; outputFileFormat != "" {
m . logger . Debugf ( "Inferred output format as %q from output file name" , outputFileFormat )
if ! c . IsSet ( "format" ) {
cfg . format = outputFileFormat
} else if outputFileFormat != cfg . format {
m . logger . Warningf ( "Requested output format %q does not match format implied by output file name: %q" , cfg . format , outputFileFormat )
}
2022-06-20 11:53:16 +00:00
}
2023-02-22 14:26:41 +00:00
return nil
}
func ( m command ) run ( c * cli . Context , cfg * config ) error {
spec , err := m . generateSpec ( cfg )
2022-06-20 11:53:16 +00:00
if err != nil {
2023-02-22 14:26:41 +00:00
return fmt . Errorf ( "failed to generate CDI spec: %v" , err )
2022-06-20 11:53:16 +00:00
}
2023-02-22 14:33:30 +00:00
m . logger . Infof ( "Generated CDI spec with version %v" , spec . Raw ( ) . Version )
2022-06-20 11:53:16 +00:00
2023-02-22 14:26:41 +00:00
if cfg . output == "" {
_ , err := spec . WriteTo ( os . Stdout )
2022-06-20 11:53:16 +00:00
if err != nil {
2023-02-22 14:26:41 +00:00
return fmt . Errorf ( "failed to write CDI spec to STDOUT: %v" , err )
2022-06-20 11:53:16 +00:00
}
2023-02-22 14:19:22 +00:00
return nil
2022-06-20 11:53:16 +00:00
}
2023-03-01 05:50:46 +00:00
return spec . Save ( cfg . output )
2022-06-20 11:53:16 +00:00
}
2022-11-23 20:39:06 +00:00
func formatFromFilename ( filename string ) string {
ext := filepath . Ext ( filename )
switch strings . ToLower ( ext ) {
case ".json" :
2023-02-22 14:33:30 +00:00
return spec . FormatJSON
case ".yaml" , ".yml" :
return spec . FormatYAML
2022-11-23 20:39:06 +00:00
}
return ""
}
2023-02-22 14:26:41 +00:00
func ( m command ) generateSpec ( cfg * config ) ( spec . Interface , error ) {
2022-12-02 13:17:52 +00:00
deviceNamer , err := nvcdi . NewDeviceNamer ( cfg . deviceNameStrategy )
if err != nil {
return nil , fmt . Errorf ( "failed to create device namer: %v" , err )
}
2022-06-20 11:53:16 +00:00
nvmllib := nvml . New ( )
if r := nvmllib . Init ( ) ; r != nvml . SUCCESS {
return nil , r
}
defer nvmllib . Shutdown ( )
devicelib := device . New ( device . WithNvml ( nvmllib ) )
2022-12-02 13:17:52 +00:00
cdilib := nvcdi . New (
nvcdi . WithLogger ( m . logger ) ,
nvcdi . WithDriverRoot ( cfg . driverRoot ) ,
nvcdi . WithNVIDIACTKPath ( cfg . nvidiaCTKPath ) ,
nvcdi . WithDeviceNamer ( deviceNamer ) ,
nvcdi . WithDeviceLib ( devicelib ) ,
nvcdi . WithNvmlLib ( nvmllib ) ,
2023-03-06 09:00:12 +00:00
nvcdi . WithMode ( string ( cfg . mode ) ) ,
2022-12-02 13:17:52 +00:00
)
deviceSpecs , err := cdilib . GetAllDeviceSpecs ( )
2022-06-20 11:53:16 +00:00
if err != nil {
2022-11-23 15:29:18 +00:00
return nil , fmt . Errorf ( "failed to create device CDI specs: %v" , err )
2022-06-20 11:53:16 +00:00
}
2023-02-06 17:53:24 +00:00
var hasAll bool
for _ , deviceSpec := range deviceSpecs {
if deviceSpec . Name == allDeviceName {
hasAll = true
break
}
}
if ! hasAll {
allDevice , err := MergeDeviceSpecs ( deviceSpecs , allDeviceName )
if err != nil {
return nil , fmt . Errorf ( "failed to create CDI specification for %q device: %v" , allDeviceName , err )
}
deviceSpecs = append ( deviceSpecs , allDevice )
}
2022-10-10 08:19:08 +00:00
2022-12-02 13:17:52 +00:00
commonEdits , err := cdilib . GetCommonEdits ( )
2022-11-23 15:29:18 +00:00
if err != nil {
2022-12-02 13:17:52 +00:00
return nil , fmt . Errorf ( "failed to create edits common for entities: %v" , err )
2022-10-21 13:28:45 +00:00
}
2022-10-07 14:23:18 +00:00
2023-02-22 14:26:41 +00:00
return spec . New (
spec . WithVendor ( "nvidia.com" ) ,
spec . WithClass ( "gpu" ) ,
spec . WithDeviceSpecs ( deviceSpecs ) ,
spec . WithEdits ( * commonEdits . ContainerEdits ) ,
spec . WithFormat ( cfg . format ) ,
)
2022-06-20 11:53:16 +00:00
}
2023-02-06 17:53:24 +00:00
// MergeDeviceSpecs creates a device with the specified name which combines the edits from the previous devices.
// If a device of the specified name already exists, an error is returned.
func MergeDeviceSpecs ( deviceSpecs [ ] specs . Device , mergedDeviceName string ) ( specs . Device , error ) {
if err := cdi . ValidateDeviceName ( mergedDeviceName ) ; err != nil {
return specs . Device { } , fmt . Errorf ( "invalid device name %q: %v" , mergedDeviceName , err )
}
for _ , d := range deviceSpecs {
if d . Name == mergedDeviceName {
return specs . Device { } , fmt . Errorf ( "device %q already exists" , mergedDeviceName )
}
}
mergedEdits := edits . NewContainerEdits ( )
2022-10-21 11:29:21 +00:00
2022-11-23 15:29:18 +00:00
for _ , d := range deviceSpecs {
edit := cdi . ContainerEdits {
ContainerEdits : & d . ContainerEdits ,
2022-10-21 11:29:21 +00:00
}
2023-02-06 17:53:24 +00:00
mergedEdits . Append ( & edit )
2022-10-21 11:29:21 +00:00
}
2023-02-06 17:53:24 +00:00
merged := specs . Device {
Name : mergedDeviceName ,
ContainerEdits : * mergedEdits . ContainerEdits ,
2022-10-21 11:29:21 +00:00
}
2023-02-06 17:53:24 +00:00
return merged , nil
2022-10-21 11:29:21 +00:00
}