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-12-01 01:10:10 +00:00
"github.com/urfave/cli/v2"
cdi "tags.cncf.io/container-device-interface/pkg/parser"
2023-05-10 13:12:44 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
2023-03-22 12:27:43 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2023-06-22 13:49:27 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
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"
2023-02-27 14:53:04 +00:00
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
2022-06-20 11:53:16 +00:00
)
const (
2023-02-06 17:53:24 +00:00
allDeviceName = "all"
2022-06-20 11:53:16 +00:00
)
type command struct {
2023-03-22 12:27:43 +00:00
logger logger . Interface
2022-06-20 11:53:16 +00:00
}
2023-05-10 13:12:00 +00:00
type options struct {
2023-03-21 13:51:36 +00:00
output string
format string
deviceNameStrategies cli . StringSlice
driverRoot string
devRoot string
2024-04-24 08:47:45 +00:00
nvidiaCDIHookPath string
2023-03-21 13:51:36 +00:00
ldconfigPath string
mode string
vendor string
class string
2023-05-10 14:28:59 +00:00
2023-11-24 15:45:19 +00:00
configSearchPaths cli . StringSlice
2023-08-09 19:46:05 +00:00
librarySearchPaths cli . StringSlice
2023-05-10 14:28:59 +00:00
csv struct {
2023-09-22 14:30:20 +00:00
files cli . StringSlice
ignorePatterns cli . StringSlice
2023-05-10 14:28:59 +00:00
}
2024-09-16 09:27:57 +00:00
includePersistencedSocket bool
2022-06-20 11:53:16 +00:00
}
// NewCommand constructs a generate-cdi command with the specified logger
2023-03-22 12:27:43 +00:00
func NewCommand ( logger logger . Interface ) * cli . Command {
2022-06-20 11:53:16 +00:00
c := command {
logger : logger ,
}
return c . build ( )
}
// build creates the CLI command
func ( m command ) build ( ) * cli . Command {
2023-05-10 13:12:00 +00:00
opts := options { }
2022-06-20 11:53:16 +00:00
// 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 {
2023-05-10 13:12:00 +00:00
return m . validateFlags ( c , & opts )
2022-11-23 20:39:06 +00:00
} ,
2022-06-20 11:53:16 +00:00
Action : func ( c * cli . Context ) error {
2023-05-10 13:12:00 +00:00
return m . run ( c , & opts )
2022-06-20 11:53:16 +00:00
} ,
}
c . Flags = [ ] cli . Flag {
2023-11-24 15:45:19 +00:00
& cli . StringSliceFlag {
Name : "config-search-path" ,
Usage : "Specify the path to search for config files when discovering the entities that should be included in the CDI specification." ,
Destination : & opts . configSearchPaths ,
} ,
2022-06-20 11:53:16 +00:00
& 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" ,
2023-05-10 13:12:00 +00:00
Destination : & opts . output ,
2022-06-20 11:53:16 +00:00
} ,
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 ,
2023-05-10 13:12:00 +00:00
Destination : & opts . 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-05-10 13:12:00 +00:00
Destination : & opts . mode ,
2023-02-08 14:29:35 +00:00
} ,
2023-11-20 13:24:39 +00:00
& cli . StringFlag {
Name : "dev-root" ,
Usage : "Specify the root where `/dev` is located. If this is not specified, the driver-root is assumed." ,
Destination : & opts . devRoot ,
} ,
2023-03-21 13:51:36 +00:00
& cli . StringSliceFlag {
2023-01-20 14:58:04 +00:00
Name : "device-name-strategy" ,
2023-03-21 13:51:36 +00:00
Usage : "Specify the strategy for generating device names. If this is specified multiple times, the devices will be duplicated for each strategy. One of [index | uuid | type-index]" ,
2023-03-21 13:52:22 +00:00
Value : cli . NewStringSlice ( nvcdi . DeviceNameStrategyIndex , nvcdi . DeviceNameStrategyUUID ) ,
2023-03-21 13:51:36 +00:00
Destination : & opts . deviceNameStrategies ,
2023-01-20 14:58:04 +00:00
} ,
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." ,
2023-05-10 13:12:00 +00:00
Destination : & opts . driverRoot ,
2022-12-01 16:21:35 +00:00
} ,
2023-08-09 19:46:05 +00:00
& cli . StringSliceFlag {
Name : "library-search-path" ,
Usage : "Specify the path to search for libraries when discovering the entities that should be included in the CDI specification.\n\tNote: This option only applies to CSV mode." ,
Destination : & opts . librarySearchPaths ,
} ,
2022-12-12 14:26:21 +00:00
& cli . StringFlag {
2024-04-24 08:47:45 +00:00
Name : "nvidia-cdi-hook-path" ,
Aliases : [ ] string { "nvidia-ctk-path" } ,
Usage : "Specify the path to use for the nvidia-cdi-hook in the generated CDI specification. " +
"If not specified, the PATH will be searched for `nvidia-cdi-hook`. " +
"NOTE: That if this is specified as `nvidia-ctk`, the PATH will be searched for `nvidia-ctk` instead." ,
Destination : & opts . nvidiaCDIHookPath ,
2022-12-12 14:26:21 +00:00
} ,
2023-12-15 00:46:00 +00:00
& cli . StringFlag {
Name : "ldconfig-path" ,
Usage : "Specify the path to use for ldconfig in the generated CDI specification" ,
Destination : & opts . ldconfigPath ,
} ,
2023-03-28 05:22:36 +00:00
& cli . StringFlag {
2023-03-28 10:31:09 +00:00
Name : "vendor" ,
Aliases : [ ] string { "cdi-vendor" } ,
Usage : "the vendor string to use for the generated CDI specification." ,
Value : "nvidia.com" ,
2023-05-10 13:12:00 +00:00
Destination : & opts . vendor ,
2023-03-28 05:22:36 +00:00
} ,
2023-03-28 06:12:00 +00:00
& cli . StringFlag {
2023-03-28 10:31:09 +00:00
Name : "class" ,
Aliases : [ ] string { "cdi-class" } ,
Usage : "the class string to use for the generated CDI specification." ,
Value : "gpu" ,
2023-05-10 13:12:00 +00:00
Destination : & opts . class ,
2023-03-28 06:12:00 +00:00
} ,
2023-05-10 14:28:59 +00:00
& cli . StringSliceFlag {
Name : "csv.file" ,
2023-07-18 10:03:44 +00:00
Usage : "The path to the list of CSV files to use when generating the CDI specification in CSV mode." ,
2023-05-10 14:28:59 +00:00
Value : cli . NewStringSlice ( csv . DefaultFileList ( ) ... ) ,
Destination : & opts . csv . files ,
} ,
2023-09-22 14:30:20 +00:00
& cli . StringSliceFlag {
Name : "csv.ignore-pattern" ,
Usage : "Specify a pattern the CSV mount specifications." ,
Destination : & opts . csv . ignorePatterns ,
} ,
2024-09-16 09:27:57 +00:00
& cli . BoolFlag {
Name : "include-persistenced-socket" ,
Usage : "Include the nvidia-persistenced socket in the generated CDI specification." ,
Destination : & opts . includePersistencedSocket ,
} ,
2022-06-20 11:53:16 +00:00
}
return & c
}
2023-05-10 13:12:00 +00:00
func ( m command ) validateFlags ( c * cli . Context , opts * options ) error {
opts . format = strings . ToLower ( opts . format )
switch opts . format {
2023-02-22 14:33:30 +00:00
case spec . FormatJSON :
case spec . FormatYAML :
2022-11-23 20:39:06 +00:00
default :
2023-05-10 13:12:00 +00:00
return fmt . Errorf ( "invalid output format: %v" , opts . format )
2022-11-23 20:39:06 +00:00
}
2023-05-10 13:12:00 +00:00
opts . mode = strings . ToLower ( opts . mode )
switch opts . mode {
2023-02-20 14:27:34 +00:00
case nvcdi . ModeAuto :
2023-05-10 14:28:59 +00:00
case nvcdi . ModeCSV :
2023-02-20 14:27:34 +00:00
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-05-10 13:12:00 +00:00
return fmt . Errorf ( "invalid discovery mode: %v" , opts . mode )
2023-02-08 14:29:35 +00:00
}
2023-03-21 13:51:36 +00:00
for _ , strategy := range opts . deviceNameStrategies . Value ( ) {
_ , err := nvcdi . NewDeviceNamer ( strategy )
if err != nil {
return err
}
2023-01-20 14:58:04 +00:00
}
2024-04-24 08:47:45 +00:00
opts . nvidiaCDIHookPath = config . ResolveNVIDIACDIHookPath ( m . logger , opts . nvidiaCDIHookPath )
2023-02-17 09:11:18 +00:00
2023-05-10 13:12:00 +00:00
if outputFileFormat := formatFromFilename ( opts . output ) ; outputFileFormat != "" {
2022-11-23 20:39:06 +00:00
m . logger . Debugf ( "Inferred output format as %q from output file name" , outputFileFormat )
if ! c . IsSet ( "format" ) {
2023-05-10 13:12:00 +00:00
opts . format = outputFileFormat
} else if outputFileFormat != opts . format {
m . logger . Warningf ( "Requested output format %q does not match format implied by output file name: %q" , opts . format , outputFileFormat )
2022-11-23 20:39:06 +00:00
}
2022-06-20 11:53:16 +00:00
}
2023-05-10 13:12:00 +00:00
if err := cdi . ValidateVendorName ( opts . vendor ) ; err != nil {
2023-03-28 06:12:00 +00:00
return fmt . Errorf ( "invalid CDI vendor name: %v" , err )
}
2023-05-10 13:12:00 +00:00
if err := cdi . ValidateClassName ( opts . class ) ; err != nil {
2023-03-28 06:12:00 +00:00
return fmt . Errorf ( "invalid CDI class name: %v" , err )
}
2023-02-22 14:26:41 +00:00
return nil
}
2023-05-10 13:12:00 +00:00
func ( m command ) run ( c * cli . Context , opts * options ) error {
spec , err := m . generateSpec ( opts )
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-05-10 13:12:00 +00:00
if opts . output == "" {
2023-02-22 14:26:41 +00:00
_ , 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-05-10 13:12:00 +00:00
return spec . Save ( opts . 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-05-10 13:12:00 +00:00
func ( m command ) generateSpec ( opts * options ) ( spec . Interface , error ) {
2023-03-21 13:51:36 +00:00
var deviceNamers [ ] nvcdi . DeviceNamer
for _ , strategy := range opts . deviceNameStrategies . Value ( ) {
deviceNamer , err := nvcdi . NewDeviceNamer ( strategy )
if err != nil {
return nil , fmt . Errorf ( "failed to create device namer: %v" , err )
}
deviceNamers = append ( deviceNamers , deviceNamer )
2022-12-02 13:17:52 +00:00
}
2023-03-22 12:04:12 +00:00
cdilib , err := nvcdi . New (
2022-12-02 13:17:52 +00:00
nvcdi . WithLogger ( m . logger ) ,
2023-05-10 13:12:00 +00:00
nvcdi . WithDriverRoot ( opts . driverRoot ) ,
2023-11-20 13:24:39 +00:00
nvcdi . WithDevRoot ( opts . devRoot ) ,
2024-04-24 08:47:45 +00:00
nvcdi . WithNVIDIACDIHookPath ( opts . nvidiaCDIHookPath ) ,
2023-12-15 00:46:00 +00:00
nvcdi . WithLdconfigPath ( opts . ldconfigPath ) ,
2023-03-21 13:51:36 +00:00
nvcdi . WithDeviceNamers ( deviceNamers ... ) ,
2023-08-25 14:50:40 +00:00
nvcdi . WithMode ( opts . mode ) ,
2023-11-24 15:45:19 +00:00
nvcdi . WithConfigSearchPaths ( opts . configSearchPaths . Value ( ) ) ,
2023-08-09 19:46:05 +00:00
nvcdi . WithLibrarySearchPaths ( opts . librarySearchPaths . Value ( ) ) ,
2023-09-22 14:30:20 +00:00
nvcdi . WithCSVFiles ( opts . csv . files . Value ( ) ) ,
nvcdi . WithCSVIgnorePatterns ( opts . csv . ignorePatterns . Value ( ) ) ,
2024-09-16 09:27:57 +00:00
nvcdi . WithOptInFeature ( "include-persistenced-socket" , opts . includePersistencedSocket ) ,
2022-12-02 13:17:52 +00:00
)
2023-03-22 12:04:12 +00:00
if err != nil {
return nil , fmt . Errorf ( "failed to create CDI library: %v" , err )
}
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
}
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-03-17 09:47:31 +00:00
return spec . New (
2023-05-10 13:12:00 +00:00
spec . WithVendor ( opts . vendor ) ,
spec . WithClass ( opts . class ) ,
2023-02-22 14:26:41 +00:00
spec . WithDeviceSpecs ( deviceSpecs ) ,
spec . WithEdits ( * commonEdits . ContainerEdits ) ,
2023-05-10 13:12:00 +00:00
spec . WithFormat ( opts . format ) ,
2023-03-17 09:47:31 +00:00
spec . WithMergedDeviceOptions (
transform . WithName ( allDeviceName ) ,
transform . WithSkipIfExists ( true ) ,
) ,
2023-05-02 14:28:03 +00:00
spec . WithPermissions ( 0644 ) ,
2023-02-22 14:26:41 +00:00
)
2022-10-21 11:29:21 +00:00
}