2022-11-23 15:29:18 +00:00
|
|
|
/**
|
|
|
|
# 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 generate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-28 12:27:48 +00:00
|
|
|
"path/filepath"
|
2022-11-23 15:29:18 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/ldcache"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewDriverDiscoverer creates a discoverer for the libraries and binaries associated with a driver installation.
|
|
|
|
// The supplied NVML Library is used to query the expected driver version.
|
2023-02-02 14:42:01 +00:00
|
|
|
func NewDriverDiscoverer(logger *logrus.Logger, driverRoot string, nvidiaCTKPath string, nvmllib nvml.Interface) (discover.Discover, error) {
|
2022-11-28 12:27:48 +00:00
|
|
|
version, r := nvmllib.SystemGetDriverVersion()
|
|
|
|
if r != nvml.SUCCESS {
|
|
|
|
return nil, fmt.Errorf("failed to determine driver version: %v", r)
|
|
|
|
}
|
|
|
|
|
2023-02-02 14:42:01 +00:00
|
|
|
libraries, err := NewDriverLibraryDiscoverer(logger, driverRoot, nvidiaCTKPath, version)
|
2022-11-23 15:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create discoverer for driver libraries: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-02-02 14:42:01 +00:00
|
|
|
firmwares := NewDriverFirmwareDiscoverer(logger, driverRoot, version)
|
2022-11-28 12:27:48 +00:00
|
|
|
|
2023-02-02 14:42:01 +00:00
|
|
|
binaries := NewDriverBinariesDiscoverer(logger, driverRoot)
|
2022-11-23 15:29:18 +00:00
|
|
|
|
|
|
|
d := discover.Merge(
|
|
|
|
libraries,
|
2022-11-28 12:27:48 +00:00
|
|
|
firmwares,
|
2022-11-23 15:29:18 +00:00
|
|
|
binaries,
|
|
|
|
)
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDriverLibraryDiscoverer creates a discoverer for the libraries associated with the specified driver version.
|
2023-02-02 14:42:01 +00:00
|
|
|
func NewDriverLibraryDiscoverer(logger *logrus.Logger, driverRoot string, nvidiaCTKPath string, version string) (discover.Discover, error) {
|
|
|
|
libraryPaths, err := getVersionLibs(logger, driverRoot, version)
|
2022-11-23 15:29:18 +00:00
|
|
|
if err != nil {
|
2022-11-28 12:27:48 +00:00
|
|
|
return nil, fmt.Errorf("failed to get libraries for driver version: %v", err)
|
2022-11-23 15:29:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 16:39:22 +00:00
|
|
|
libraries := discover.NewMounts(
|
|
|
|
logger,
|
|
|
|
lookup.NewFileLocator(
|
|
|
|
lookup.WithLogger(logger),
|
2023-02-02 14:42:01 +00:00
|
|
|
lookup.WithRoot(driverRoot),
|
2022-12-05 16:39:22 +00:00
|
|
|
),
|
2023-02-02 14:42:01 +00:00
|
|
|
driverRoot,
|
2022-12-05 16:39:22 +00:00
|
|
|
libraryPaths,
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg := &discover.Config{
|
2023-02-02 14:57:15 +00:00
|
|
|
DriverRoot: driverRoot,
|
2022-12-05 16:39:22 +00:00
|
|
|
NvidiaCTKPath: nvidiaCTKPath,
|
2022-11-23 15:29:18 +00:00
|
|
|
}
|
2022-12-05 16:39:22 +00:00
|
|
|
hooks, _ := discover.NewLDCacheUpdateHook(logger, libraries, cfg)
|
|
|
|
|
|
|
|
d := discover.Merge(
|
|
|
|
libraries,
|
|
|
|
hooks,
|
|
|
|
)
|
2022-11-23 15:29:18 +00:00
|
|
|
|
2022-12-05 16:39:22 +00:00
|
|
|
return d, nil
|
2022-11-23 15:29:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 12:27:48 +00:00
|
|
|
// NewDriverFirmwareDiscoverer creates a discoverer for GSP firmware associated with the specified driver version.
|
2023-02-02 14:42:01 +00:00
|
|
|
func NewDriverFirmwareDiscoverer(logger *logrus.Logger, driverRoot string, version string) discover.Discover {
|
2022-11-28 12:27:48 +00:00
|
|
|
gspFirmwarePath := filepath.Join("/lib/firmware/nvidia", version, "gsp.bin")
|
|
|
|
return discover.NewMounts(
|
|
|
|
logger,
|
|
|
|
lookup.NewFileLocator(
|
|
|
|
lookup.WithLogger(logger),
|
2023-02-02 14:42:01 +00:00
|
|
|
lookup.WithRoot(driverRoot),
|
2022-11-28 12:27:48 +00:00
|
|
|
),
|
2023-02-02 14:42:01 +00:00
|
|
|
driverRoot,
|
2022-11-28 12:27:48 +00:00
|
|
|
[]string{gspFirmwarePath},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDriverBinariesDiscoverer creates a discoverer for GSP firmware associated with the GPU driver.
|
2023-02-02 14:42:01 +00:00
|
|
|
func NewDriverBinariesDiscoverer(logger *logrus.Logger, driverRoot string) discover.Discover {
|
2022-11-28 12:27:48 +00:00
|
|
|
return discover.NewMounts(
|
|
|
|
logger,
|
2023-02-02 14:42:01 +00:00
|
|
|
lookup.NewExecutableLocator(logger, driverRoot),
|
|
|
|
driverRoot,
|
2022-11-28 12:27:48 +00:00
|
|
|
[]string{
|
|
|
|
"nvidia-smi", /* System management interface */
|
|
|
|
"nvidia-debugdump", /* GPU coredump utility */
|
|
|
|
"nvidia-persistenced", /* Persistence mode utility */
|
|
|
|
"nvidia-cuda-mps-control", /* Multi process service CLI */
|
|
|
|
"nvidia-cuda-mps-server", /* Multi process service server */
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-05 16:39:22 +00:00
|
|
|
// getVersionLibs checks the LDCache for libraries ending in the specified driver version.
|
2023-02-02 14:42:01 +00:00
|
|
|
// Although the ldcache at the specified driverRoot is queried, the paths are returned relative to this driverRoot.
|
2022-12-05 16:39:22 +00:00
|
|
|
// This allows the standard mount location logic to be used for resolving the mounts.
|
2023-02-02 14:42:01 +00:00
|
|
|
func getVersionLibs(logger *logrus.Logger, driverRoot string, version string) ([]string, error) {
|
2022-11-23 15:29:18 +00:00
|
|
|
logger.Infof("Using driver version %v", version)
|
|
|
|
|
2023-02-02 14:42:01 +00:00
|
|
|
cache, err := ldcache.New(logger, driverRoot)
|
2022-11-23 15:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load ldcache: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
libs32, libs64 := cache.List()
|
|
|
|
|
|
|
|
var libs []string
|
|
|
|
for _, l := range libs64 {
|
|
|
|
if strings.HasSuffix(l, version) {
|
|
|
|
logger.Infof("found 64-bit driver lib: %v", l)
|
|
|
|
libs = append(libs, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range libs32 {
|
|
|
|
if strings.HasSuffix(l, version) {
|
|
|
|
logger.Infof("found 32-bit driver lib: %v", l)
|
|
|
|
libs = append(libs, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 14:42:01 +00:00
|
|
|
if driverRoot == "/" || driverRoot == "" {
|
2022-12-05 16:39:22 +00:00
|
|
|
return libs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var relative []string
|
|
|
|
for _, l := range libs {
|
2023-02-02 14:42:01 +00:00
|
|
|
relative = append(relative, strings.TrimPrefix(l, driverRoot))
|
2022-12-05 16:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return relative, nil
|
2022-11-23 15:29:18 +00:00
|
|
|
}
|