Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions internal/cdi/cdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cdi

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -87,7 +88,8 @@ func New(infolib info.Interface, nvmllib nvml.Interface, devicelib device.Interf
return &null{}, nil
}
hasNVML, _ := infolib.HasNvml()
if !hasNVML {
platform := infolib.ResolvePlatform()
if !hasNVML && platform != info.PlatformTegra {
klog.Warning("No valid resources detected, creating a null CDI handler")
return &null{}, nil
}
Expand Down Expand Up @@ -128,6 +130,13 @@ func New(infolib info.Interface, nvmllib nvml.Interface, devicelib device.Interf
nvcdi.WithVendor(c.vendor),
}

// On Tegra (CSV mode), the default CSV files live under the driver root.
// Inject driver-root-aware paths explicitly so the nvcdi library does not
// fall back to the hardcoded absolute paths returned by csv.DefaultFileList().
if platform == info.PlatformTegra {
commonOptions = append(commonOptions, nvcdi.WithCSVFiles(csvFilesForRoot(c.driverRoot)))
}

c.cdilibs = make(map[string]nvcdi.SpecGenerator)

c.cdilibs["gpu"], err = nvcdi.New(
Expand Down Expand Up @@ -177,16 +186,6 @@ func (cdi *cdiHandler) CreateSpecFile() error {
for class, cdilib := range cdi.cdilibs {
cdi.logger.Infof("Generating CDI spec for resource: %s/%s", cdi.vendor, class)

if class == "gpu" {
ret := cdi.nvmllib.Init()
if ret != nvml.SUCCESS {
return fmt.Errorf("failed to initialize NVML: %v", ret)
}
defer func() {
_ = cdi.nvmllib.Shutdown()
}()
}
Comment thread
cdesiniotis marked this conversation as resolved.
Resolved

spec, err := cdilib.GetSpec()
if err != nil {
return fmt.Errorf("failed to get CDI spec: %v", err)
Expand Down Expand Up @@ -268,3 +267,33 @@ func (cdi *cdiHandler) AdditionalDevices() []string {
}
return devices
}

// defaultCSVMountSpecPath mirrors the constant defined in the nvidia-container-toolkit's
// internal/platform-support/tegra/csv package.
const defaultCSVMountSpecPath = "/etc/nvidia-container-runtime/host-files-for-container.d"

var defaultCSVFileNames = []string{"devices.csv", "drivers.csv", "l4t.csv"}

// csvFilesForRoot returns the Tegra CSV file paths to use for CDI spec generation.
// It checks for the CSV directory at the driver root first, then falls back to the
// absolute path. Returns nil if the directory is not found at either location,
// allowing nvcdi to fall back to csv.DefaultFileList().
func csvFilesForRoot(driverRoot string) []string {
roots := []string{driverRoot}
if driverRoot != "/" {
roots = append(roots, "/")
}
for _, root := range roots {
csvDir := filepath.Join(root, defaultCSVMountSpecPath)
stat, err := os.Stat(csvDir)
if err != nil || !stat.IsDir() {
continue
}
paths := make([]string, len(defaultCSVFileNames))
for i, f := range defaultCSVFileNames {
paths[i] = filepath.Join(csvDir, f)
}
return paths
}
return nil
}
68 changes: 68 additions & 0 deletions internal/cdi/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
# Copyright (c) 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.
**/

package cdi

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestCSVFilesForRoot(t *testing.T) {
testCases := []struct {
description string
setup func(t *testing.T) string // returns driverRoot
expectedPaths func(driverRoot string) []string
}{
{
description: "directory exists at driver root",
setup: func(t *testing.T) string {
root := t.TempDir()
csvDir := filepath.Join(root, defaultCSVMountSpecPath)
require.NoError(t, os.MkdirAll(csvDir, 0755))
return root
},
expectedPaths: func(driverRoot string) []string {
csvDir := filepath.Join(driverRoot, defaultCSVMountSpecPath)
return []string{
filepath.Join(csvDir, "devices.csv"),
filepath.Join(csvDir, "drivers.csv"),
filepath.Join(csvDir, "l4t.csv"),
}
},
},
{
description: "directory does not exist at driver root or absolute path",
setup: func(t *testing.T) string {
return t.TempDir()
},
expectedPaths: func(_ string) []string {
return nil
},
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
driverRoot := tc.setup(t)
got := csvFilesForRoot(driverRoot)
require.Equal(t, tc.expectedPaths(driverRoot), got)
})
}
}