Add csv mode to nvidia-ctk cdi generate command

This chagne allows the csv mode option to specified in the
nvidia-ctk cdi generate command and adds a --csv.file option
that can be repeated to specify the CSV files to be processed.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-05-10 16:28:59 +02:00
parent 418e4014e6
commit e30fd0f4ad
3 changed files with 31 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import (
"strings" "strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
@ -48,6 +49,10 @@ type options struct {
mode string mode string
vendor string vendor string
class string class string
csv struct {
files cli.StringSlice
}
} }
// NewCommand constructs a generate-cdi command with the specified logger // NewCommand constructs a generate-cdi command with the specified logger
@ -123,13 +128,18 @@ func (m command) build() *cli.Command {
Value: "gpu", Value: "gpu",
Destination: &opts.class, Destination: &opts.class,
}, },
&cli.StringSliceFlag{
Name: "csv.file",
Usage: "The path to the list of CSV files to use when generating the CDI specification in CDI mode.",
Value: cli.NewStringSlice(csv.DefaultFileList()...),
Destination: &opts.csv.files,
},
} }
return &c return &c
} }
func (m command) validateFlags(c *cli.Context, opts *options) error { func (m command) validateFlags(c *cli.Context, opts *options) error {
opts.format = strings.ToLower(opts.format) opts.format = strings.ToLower(opts.format)
switch opts.format { switch opts.format {
case spec.FormatJSON: case spec.FormatJSON:
@ -141,6 +151,7 @@ func (m command) validateFlags(c *cli.Context, opts *options) error {
opts.mode = strings.ToLower(opts.mode) opts.mode = strings.ToLower(opts.mode)
switch opts.mode { switch opts.mode {
case nvcdi.ModeAuto: case nvcdi.ModeAuto:
case nvcdi.ModeCSV:
case nvcdi.ModeNvml: case nvcdi.ModeNvml:
case nvcdi.ModeWsl: case nvcdi.ModeWsl:
case nvcdi.ModeManagement: case nvcdi.ModeManagement:
@ -215,6 +226,7 @@ func (m command) generateSpec(opts *options) (spec.Interface, error) {
nvcdi.WithNVIDIACTKPath(opts.nvidiaCTKPath), nvcdi.WithNVIDIACTKPath(opts.nvidiaCTKPath),
nvcdi.WithDeviceNamer(deviceNamer), nvcdi.WithDeviceNamer(deviceNamer),
nvcdi.WithMode(string(opts.mode)), nvcdi.WithMode(string(opts.mode)),
nvcdi.WithCSVFiles(opts.csv.files.Value()),
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create CDI library: %v", err) return nil, fmt.Errorf("failed to create CDI library: %v", err)

View File

@ -33,6 +33,22 @@ const (
DefaultMountSpecPath = "/etc/nvidia-container-runtime/host-files-for-container.d" DefaultMountSpecPath = "/etc/nvidia-container-runtime/host-files-for-container.d"
) )
// DefaultFileList returns the list of CSV files that are used by default.
func DefaultFileList() []string {
files := []string{
"devices.csv",
"drivers.csv",
"l4t.csv",
}
var paths []string
for _, file := range files {
paths = append(paths, filepath.Join(DefaultMountSpecPath, file))
}
return paths
}
// GetFileList returns the (non-recursive) list of CSV files in the specified // GetFileList returns the (non-recursive) list of CSV files in the specified
// folder // folder
func GetFileList(root string) ([]string, error) { func GetFileList(root string) ([]string, error) {

View File

@ -19,6 +19,7 @@ package nvcdi
import ( import (
"fmt" "fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -84,11 +85,7 @@ func New(opts ...Option) (Interface, error) {
switch l.resolveMode() { switch l.resolveMode() {
case ModeCSV: case ModeCSV:
if len(l.csvFiles) == 0 { if len(l.csvFiles) == 0 {
l.csvFiles = []string{ l.csvFiles = csv.DefaultFileList()
"/etc/nvidia-container-runtime/host-files-for-container.d/l4t.csv",
"/etc/nvidia-container-runtime/host-files-for-container.d/drivers.csv",
"/etc/nvidia-container-runtime/host-files-for-container.d/devices.csv",
}
} }
lib = (*csvlib)(l) lib = (*csvlib)(l)
case ModeManagement: case ModeManagement: