This commit is contained in:
Evan Lezar 2025-02-20 21:15:55 +00:00 committed by GitHub
commit b91cfbba9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 303 additions and 140 deletions

View File

@ -49,6 +49,7 @@ TRANSLATOR:
const: const:
- {action: accept, from: "^NVSANDBOXUTILS_"} - {action: accept, from: "^NVSANDBOXUTILS_"}
- {action: accept, from: "^nvSandboxUtils"} - {action: accept, from: "^nvSandboxUtils"}
- {action: replace, from: "^NVSANDBOXUTILS_255_MASK_", to: "MASK255_" }
- {action: replace, from: "^NVSANDBOXUTILS_"} - {action: replace, from: "^NVSANDBOXUTILS_"}
- {action: replace, from: "^nvSandboxUtils"} - {action: replace, from: "^nvSandboxUtils"}
- {action: accept, from: "^NV"} - {action: accept, from: "^NV"}

View File

@ -14,7 +14,7 @@
# limitations under the License. # limitations under the License.
**/ **/
package nvcdi package dgpu
import ( import (
"fmt" "fmt"
@ -22,7 +22,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/NVIDIA/go-nvml/pkg/nvml"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
@ -32,47 +31,22 @@ import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
) )
// NewDriverDiscoverer creates a discoverer for the libraries and binaries associated with a driver installation. // newNvmlDriverDiscoverer constructs a discoverer from the specified NVML library.
// The supplied NVML Library is used to query the expected driver version. func (o *options) newNvmlDriverDiscoverer() (discover.Discover, error) {
func NewDriverDiscoverer(logger logger.Interface, driver *root.Driver, nvidiaCDIHookPath string, ldconfigPath string, nvmllib nvml.Interface) (discover.Discover, error) { libraries, err := o.newNvmlDriverLibraryDiscoverer()
if r := nvmllib.Init(); r != nvml.SUCCESS {
return nil, fmt.Errorf("failed to initialize NVML: %v", r)
}
defer func() {
if r := nvmllib.Shutdown(); r != nvml.SUCCESS {
logger.Warningf("failed to shutdown NVML: %v", r)
}
}()
version, r := nvmllib.SystemGetDriverVersion()
if r != nvml.SUCCESS {
return nil, fmt.Errorf("failed to determine driver version: %v", r)
}
return newDriverVersionDiscoverer(logger, driver, nvidiaCDIHookPath, ldconfigPath, version)
}
func newDriverVersionDiscoverer(logger logger.Interface, driver *root.Driver, nvidiaCDIHookPath, ldconfigPath, version string) (discover.Discover, error) {
libraries, err := NewDriverLibraryDiscoverer(logger, driver, nvidiaCDIHookPath, ldconfigPath, version)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create discoverer for driver libraries: %v", err) return nil, fmt.Errorf("failed to create discoverer for driver libraries: %v", err)
} }
ipcs, err := discover.NewIPCDiscoverer(logger, driver.Root) firmwares, err := o.newNvmlDriverFirmwareDiscoverer()
if err != nil {
return nil, fmt.Errorf("failed to create discoverer for IPC sockets: %v", err)
}
firmwares, err := NewDriverFirmwareDiscoverer(logger, driver.Root, version)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create discoverer for GSP firmware: %v", err) return nil, fmt.Errorf("failed to create discoverer for GSP firmware: %v", err)
} }
binaries := NewDriverBinariesDiscoverer(logger, driver.Root) binaries := o.newNvmlDriverBinariesDiscoverer()
d := discover.Merge( d := discover.Merge(
libraries, libraries,
ipcs,
firmwares, firmwares,
binaries, binaries,
) )
@ -80,32 +54,27 @@ func newDriverVersionDiscoverer(logger logger.Interface, driver *root.Driver, nv
return d, nil return d, nil
} }
// NewDriverLibraryDiscoverer creates a discoverer for the libraries associated with the specified driver version. // newNvmlDriverLibraryDiscoverer creates a discoverer for the libraries associated with the specified driver version.
func NewDriverLibraryDiscoverer(logger logger.Interface, driver *root.Driver, nvidiaCDIHookPath, ldconfigPath, version string) (discover.Discover, error) { func (o *options) newNvmlDriverLibraryDiscoverer() (discover.Discover, error) {
libraryPaths, err := getVersionLibs(logger, driver, version) libraryPaths, err := getVersionLibs(o.logger, o.driver, o.version)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get libraries for driver version: %v", err) return nil, fmt.Errorf("failed to get libraries for driver version: %v", err)
} }
libraries := discover.NewMounts( libraries := discover.NewMounts(
logger, o.logger,
lookup.NewFileLocator( lookup.NewFileLocator(
lookup.WithLogger(logger), lookup.WithLogger(o.logger),
lookup.WithRoot(driver.Root), lookup.WithRoot(o.driver.Root),
), ),
driver.Root, o.driver.Root,
libraryPaths, libraryPaths,
) )
updateLDCache, _ := discover.NewLDCacheUpdateHook(logger, libraries, nvidiaCDIHookPath, ldconfigPath) d := discover.WithDriverDotSoSymlinks(
libraries,
d := discover.Merge( o.version,
discover.WithDriverDotSoSymlinks( o.nvidiaCDIHookPath,
libraries,
version,
nvidiaCDIHookPath,
),
updateLDCache,
) )
return d, nil return d, nil
@ -153,31 +122,31 @@ func getCustomFirmwareClassPath(logger logger.Interface) string {
return strings.TrimSpace(string(customFirmwareClassPath)) return strings.TrimSpace(string(customFirmwareClassPath))
} }
// NewDriverFirmwareDiscoverer creates a discoverer for GSP firmware associated with the specified driver version. // newNvmlDriverFirmwareDiscoverer creates a discoverer for GSP firmware associated with the specified driver version.
func NewDriverFirmwareDiscoverer(logger logger.Interface, driverRoot string, version string) (discover.Discover, error) { func (o *options) newNvmlDriverFirmwareDiscoverer() (discover.Discover, error) {
gspFirmwareSearchPaths, err := getFirmwareSearchPaths(logger) gspFirmwareSearchPaths, err := getFirmwareSearchPaths(o.logger)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get firmware search paths: %v", err) return nil, fmt.Errorf("failed to get firmware search paths: %v", err)
} }
gspFirmwarePaths := filepath.Join("nvidia", version, "gsp*.bin") gspFirmwarePaths := filepath.Join("nvidia", o.version, "gsp*.bin")
return discover.NewMounts( return discover.NewMounts(
logger, o.logger,
lookup.NewFileLocator( lookup.NewFileLocator(
lookup.WithLogger(logger), lookup.WithLogger(o.logger),
lookup.WithRoot(driverRoot), lookup.WithRoot(o.driver.Root),
lookup.WithSearchPaths(gspFirmwareSearchPaths...), lookup.WithSearchPaths(gspFirmwareSearchPaths...),
), ),
driverRoot, o.driver.Root,
[]string{gspFirmwarePaths}, []string{gspFirmwarePaths},
), nil ), nil
} }
// NewDriverBinariesDiscoverer creates a discoverer for GSP firmware associated with the GPU driver. // newNvmlDriverBinariesDiscoverer creates a discoverer for binaries associated with the specified driver version.
func NewDriverBinariesDiscoverer(logger logger.Interface, driverRoot string) discover.Discover { func (o *options) newNvmlDriverBinariesDiscoverer() discover.Discover {
return discover.NewMounts( return discover.NewMounts(
logger, o.logger,
lookup.NewExecutableLocator(logger, driverRoot), lookup.NewExecutableLocator(o.logger, o.driver.Root),
driverRoot, o.driver.Root,
[]string{ []string{
"nvidia-smi", /* System management interface */ "nvidia-smi", /* System management interface */
"nvidia-debugdump", /* GPU coredump utility */ "nvidia-debugdump", /* GPU coredump utility */

View File

@ -0,0 +1,31 @@
/**
# 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 dgpu
import (
"fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
)
// newNvsandboxutilsDriverDiscoverer constructs a discoverer from the specified nvsandboxutils library.
func (o *options) newNvsandboxutilsDriverDiscoverer() (discover.Discover, error) {
if o.nvsandboxutilslib == nil {
return nil, nil
}
return nil, fmt.Errorf("nvsandboxutils driver discovery is not implemented")
}

View File

@ -0,0 +1,74 @@
/**
# 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 dgpu
import (
"errors"
"fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
)
// NewDriverDiscoverer creates a discoverer for the libraries and binaries associated with a driver installation.
func NewDriverDiscoverer(opts ...Option) (discover.Discover, error) {
o := new(opts...)
if o.version == "" {
return nil, fmt.Errorf("a version must be specified")
}
var discoverers []discover.Discover
var errs error
nvsandboxutilsDiscoverer, err := o.newNvsandboxutilsDriverDiscoverer()
if err != nil {
// TODO: Log a warning
errs = errors.Join(errs, err)
} else if nvsandboxutilsDiscoverer != nil {
discoverers = append(discoverers, nvsandboxutilsDiscoverer)
}
nvmlDiscoverer, err := o.newNvmlDriverDiscoverer()
if err != nil {
// TODO: Log a warning
errs = errors.Join(errs, err)
} else if nvmlDiscoverer != nil {
discoverers = append(discoverers, nvmlDiscoverer)
}
if len(discoverers) == 0 {
return nil, errs
}
cached := discover.WithCache(
discover.FirstValid(
discoverers...,
),
)
updateLDCache, _ := discover.NewLDCacheUpdateHook(o.logger, cached, o.nvidiaCDIHookPath, o.ldconfigPath)
ipcs, err := discover.NewIPCDiscoverer(o.logger, o.driver.Root)
if err != nil {
return nil, fmt.Errorf("failed to create discoverer for IPC sockets: %v", err)
}
return discover.Merge(
cached,
updateLDCache,
ipcs,
), nil
}

View File

@ -18,13 +18,16 @@ package dgpu
import ( import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvcaps" "github.com/NVIDIA/nvidia-container-toolkit/internal/nvcaps"
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils" "github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils"
) )
type options struct { type options struct {
logger logger.Interface logger logger.Interface
driver *root.Driver
devRoot string devRoot string
ldconfigPath string
nvidiaCDIHookPath string nvidiaCDIHookPath string
isMigDevice bool isMigDevice bool
@ -33,6 +36,9 @@ type options struct {
migCaps nvcaps.MigCaps migCaps nvcaps.MigCaps
migCapsError error migCapsError error
// version stores the driver version.
version string
nvsandboxutilslib nvsandboxutils.Interface nvsandboxutilslib nvsandboxutils.Interface
} }
@ -45,6 +51,19 @@ func WithDevRoot(root string) Option {
} }
} }
func WithDriver(driver *root.Driver) Option {
return func(l *options) {
l.driver = driver
}
}
// WithLdconfigPath sets the path to the ldconfig program
func WithLdconfigPath(path string) Option {
return func(l *options) {
l.ldconfigPath = path
}
}
// WithLogger sets the logger for the library // WithLogger sets the logger for the library
func WithLogger(logger logger.Interface) Option { func WithLogger(logger logger.Interface) Option {
return func(l *options) { return func(l *options) {
@ -72,3 +91,9 @@ func WithNvsandboxuitilsLib(nvsandboxutilslib nvsandboxutils.Interface) Option {
l.nvsandboxutilslib = nvsandboxutilslib l.nvsandboxutilslib = nvsandboxutilslib
} }
} }
func WithVersion(version string) Option {
return func(l *options) {
l.version = version
}
}

View File

@ -20,11 +20,12 @@ import (
"fmt" "fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/dgpu"
) )
// newCommonNVMLDiscoverer returns a discoverer for entities that are not associated with a specific CDI device. // newCommonNVMLDiscoverer returns a discoverer for entities that are not associated with a specific CDI device.
// This includes driver libraries and meta devices, for example. // This includes driver libraries and meta devices, for example.
func (l *nvmllib) newCommonNVMLDiscoverer() (discover.Discover, error) { func (l *nvmllib) newCommonNVMLDiscoverer(version string) (discover.Discover, error) {
metaDevices := discover.NewCharDeviceDiscoverer( metaDevices := discover.NewCharDeviceDiscoverer(
l.logger, l.logger,
l.devRoot, l.devRoot,
@ -41,7 +42,15 @@ func (l *nvmllib) newCommonNVMLDiscoverer() (discover.Discover, error) {
l.logger.Warningf("failed to create discoverer for graphics mounts: %v", err) l.logger.Warningf("failed to create discoverer for graphics mounts: %v", err)
} }
driverFiles, err := NewDriverDiscoverer(l.logger, l.driver, l.nvidiaCDIHookPath, l.ldconfigPath, l.nvmllib) driverFiles, err := dgpu.NewDriverDiscoverer(
dgpu.WithDevRoot(l.devRoot),
dgpu.WithDriver(l.driver),
dgpu.WithLdconfigPath(l.ldconfigPath),
dgpu.WithLogger(l.logger),
dgpu.WithNVIDIACDIHookPath(l.nvidiaCDIHookPath),
dgpu.WithNvsandboxuitilsLib(l.nvsandboxutilslib),
dgpu.WithVersion(version),
)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create discoverer for driver files: %v", err) return nil, fmt.Errorf("failed to create discoverer for driver files: %v", err)
} }

View File

@ -83,7 +83,25 @@ func (l *nvmllib) GetAllDeviceSpecs() ([]specs.Device, error) {
// GetCommonEdits generates a CDI specification that can be used for ANY devices // GetCommonEdits generates a CDI specification that can be used for ANY devices
func (l *nvmllib) GetCommonEdits() (*cdi.ContainerEdits, error) { func (l *nvmllib) GetCommonEdits() (*cdi.ContainerEdits, error) {
common, err := l.newCommonNVMLDiscoverer() if l.nvsandboxutilslib != nil {
if r := l.nvsandboxutilslib.Init(l.driverRoot); r != nvsandboxutils.SUCCESS {
l.logger.Warningf("Failed to init nvsandboxutils: %v; ignoring", r)
l.nvsandboxutilslib = nil
}
defer func() {
if l.nvsandboxutilslib == nil {
return
}
_ = l.nvsandboxutilslib.Shutdown()
}()
}
version, err := (*nvcdilib)(l).getDriverVersion()
if err != nil {
return nil, fmt.Errorf("failed to get driver version: %v", err)
}
common, err := l.newCommonNVMLDiscoverer(version)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create discoverer for common entities: %v", err) return nil, fmt.Errorf("failed to create discoverer for common entities: %v", err)
} }

View File

@ -18,30 +18,21 @@ package nvcdi
import ( import (
"fmt" "fmt"
"path/filepath"
"strings"
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device" "github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info" "github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
"github.com/NVIDIA/go-nvml/pkg/nvml" "github.com/NVIDIA/go-nvml/pkg/nvml"
"tags.cncf.io/container-device-interface/pkg/cdi"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/cuda"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils" "github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv" "github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
"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"
) )
type wrapper struct {
Interface
vendor string
class string
mergedDeviceOptions []transform.MergedDeviceOption
}
type nvcdilib struct { type nvcdilib struct {
logger logger.Interface logger logger.Interface
nvmllib nvml.Interface nvmllib nvml.Interface
@ -180,50 +171,36 @@ func New(opts ...Option) (Interface, error) {
return &w, nil return &w, nil
} }
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface. // getDriverVersion returns the driver version of the current system.
func (l *wrapper) GetSpec() (spec.Interface, error) { func (l *nvcdilib) getDriverVersion() (string, error) {
deviceSpecs, err := l.GetAllDeviceSpecs() if version, err := l.getDriverVersionNvsandboxutils(); err == nil && version != "" {
if err != nil {
return nil, err
}
edits, err := l.GetCommonEdits()
if err != nil {
return nil, err
}
return spec.New(
spec.WithDeviceSpecs(deviceSpecs),
spec.WithEdits(*edits.ContainerEdits),
spec.WithVendor(l.vendor),
spec.WithClass(l.class),
spec.WithMergedDeviceOptions(l.mergedDeviceOptions...),
)
}
// GetCommonEdits returns the wrapped edits and adds additional edits on top.
func (m *wrapper) GetCommonEdits() (*cdi.ContainerEdits, error) {
edits, err := m.Interface.GetCommonEdits()
if err != nil {
return nil, err
}
edits.Env = append(edits.Env, image.EnvVarNvidiaVisibleDevices+"=void")
return edits, nil
}
// getCudaVersion returns the CUDA version of the current system.
func (l *nvcdilib) getCudaVersion() (string, error) {
version, err := l.getCudaVersionNvsandboxutils()
if err == nil {
return version, err return version, err
} }
// Fallback to NVML // Fallback to NVML
return l.getCudaVersionNvml() if version, err := l.getDriverVersionNvml(); err == nil && version != "" {
return version, err
}
// Fallback to getting the version from the libcuda.so suffix.
return l.getDriverVersionLibcudaSo()
} }
func (l *nvcdilib) getCudaVersionNvml() (string, error) { func (l *nvcdilib) getDriverVersionLibcudaSo() (string, error) {
libCudaPaths, err := cuda.New(
l.driver.Libraries(),
).Locate(".*.*")
if err != nil {
return "", fmt.Errorf("failed to locate libcuda.so: %v", err)
}
libCudaPath := libCudaPaths[0]
version := strings.TrimPrefix(filepath.Base(libCudaPath), "libcuda.so.")
return version, nil
}
func (l *nvcdilib) getDriverVersionNvml() (string, error) {
if hasNVML, reason := l.infolib.HasNvml(); !hasNVML { if hasNVML, reason := l.infolib.HasNvml(); !hasNVML {
return "", fmt.Errorf("nvml not detected: %v", reason) return "", fmt.Errorf("nvml not detected: %v", reason)
} }
@ -247,7 +224,7 @@ func (l *nvcdilib) getCudaVersionNvml() (string, error) {
return version, nil return version, nil
} }
func (l *nvcdilib) getCudaVersionNvsandboxutils() (string, error) { func (l *nvcdilib) getDriverVersionNvsandboxutils() (string, error) {
if l.nvsandboxutilslib == nil { if l.nvsandboxutilslib == nil {
return "", fmt.Errorf("libnvsandboxutils is not available") return "", fmt.Errorf("libnvsandboxutils is not available")
} }

View File

@ -27,8 +27,8 @@ import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits" "github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/cuda"
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils" "github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/dgpu"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
) )
@ -75,12 +75,20 @@ func (m *managementlib) GetCommonEdits() (*cdi.ContainerEdits, error) {
}() }()
} }
version, err := m.getCudaVersion() version, err := (*nvcdilib)(m).getDriverVersion()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get CUDA version: %v", err) return nil, fmt.Errorf("failed to get driver version: %v", err)
} }
driver, err := newDriverVersionDiscoverer(m.logger, m.driver, m.nvidiaCDIHookPath, m.ldconfigPath, version) driver, err := dgpu.NewDriverDiscoverer(
dgpu.WithDevRoot(m.devRoot),
dgpu.WithDriver(m.driver),
dgpu.WithLdconfigPath(m.ldconfigPath),
dgpu.WithLogger(m.logger),
dgpu.WithNVIDIACDIHookPath(m.nvidiaCDIHookPath),
dgpu.WithNvsandboxuitilsLib(m.nvsandboxutilslib),
dgpu.WithVersion(version),
)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create driver library discoverer: %v", err) return nil, fmt.Errorf("failed to create driver library discoverer: %v", err)
} }
@ -93,27 +101,6 @@ func (m *managementlib) GetCommonEdits() (*cdi.ContainerEdits, error) {
return edits, nil return edits, nil
} }
// getCudaVersion returns the CUDA version for use in managementlib containers.
func (m *managementlib) getCudaVersion() (string, error) {
version, err := (*nvcdilib)(m).getCudaVersion()
if err == nil {
return version, nil
}
libCudaPaths, err := cuda.New(
m.driver.Libraries(),
).Locate(".*.*")
if err != nil {
return "", fmt.Errorf("failed to locate libcuda.so: %v", err)
}
libCudaPath := libCudaPaths[0]
version = strings.TrimPrefix(filepath.Base(libCudaPath), "libcuda.so.")
return version, nil
}
type managementDiscoverer struct { type managementDiscoverer struct {
discover.Discover discover.Discover
} }

72
pkg/nvcdi/wrapper.go Normal file
View File

@ -0,0 +1,72 @@
/**
# Copyright 2024 NVIDIA CORPORATION
#
# 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 nvcdi
import (
"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
)
type wrapper struct {
Interface
vendor string
class string
mergedDeviceOptions []transform.MergedDeviceOption
}
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface.
func (l *wrapper) GetSpec() (spec.Interface, error) {
deviceSpecs, err := l.GetAllDeviceSpecs()
if err != nil {
return nil, err
}
edits, err := l.GetCommonEdits()
if err != nil {
return nil, err
}
return spec.New(
spec.WithDeviceSpecs(deviceSpecs),
spec.WithEdits(*edits.ContainerEdits),
spec.WithVendor(l.vendor),
spec.WithClass(l.class),
spec.WithMergedDeviceOptions(l.mergedDeviceOptions...),
)
}
// GetAllDeviceSpecs returns the device specs for all available devices.
func (l *wrapper) GetAllDeviceSpecs() ([]specs.Device, error) {
return l.Interface.GetAllDeviceSpecs()
}
// GetCommonEdits returns the wrapped edits and adds additional edits on top.
func (m *wrapper) GetCommonEdits() (*cdi.ContainerEdits, error) {
edits, err := m.Interface.GetCommonEdits()
if err != nil {
return nil, err
}
edits.Env = append(edits.Env, image.EnvVarNvidiaVisibleDevices+"=void")
return edits, nil
}