2023-02-06 17:53:24 +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 nvcdi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/dxcore"
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-02-06 17:53:24 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
|
|
|
)
|
|
|
|
|
|
|
|
var requiredDriverStoreFiles = []string{
|
|
|
|
"libcuda.so.1.1", /* Core library for cuda support */
|
|
|
|
"libcuda_loader.so", /* Core library for cuda support on WSL */
|
|
|
|
"libnvidia-ptxjitcompiler.so.1", /* Core library for PTX Jit support */
|
|
|
|
"libnvidia-ml.so.1", /* Core library for nvml */
|
|
|
|
"libnvidia-ml_loader.so", /* Core library for nvml on WSL */
|
|
|
|
"libdxcore.so", /* Core library for dxcore support */
|
2024-01-03 16:26:57 +00:00
|
|
|
"libnvdxgdmal.so.1", /* dxgdmal library for cuda */
|
2023-02-06 17:53:24 +00:00
|
|
|
"nvcubins.bin", /* Binary containing GPU code for cuda */
|
|
|
|
"nvidia-smi", /* nvidia-smi binary*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// newWSLDriverDiscoverer returns a Discoverer for WSL2 drivers.
|
2024-04-24 08:47:45 +00:00
|
|
|
func newWSLDriverDiscoverer(logger logger.Interface, driverRoot string, nvidiaCDIHookPath, ldconfigPath string) (discover.Discover, error) {
|
2023-02-06 17:53:24 +00:00
|
|
|
err := dxcore.Init()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize dxcore: %v", err)
|
|
|
|
}
|
2023-08-25 14:48:11 +00:00
|
|
|
defer func() {
|
|
|
|
if err := dxcore.Shutdown(); err != nil {
|
|
|
|
logger.Warningf("failed to shutdown dxcore: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-02-06 17:53:24 +00:00
|
|
|
|
|
|
|
driverStorePaths := dxcore.GetDriverStorePaths()
|
|
|
|
if len(driverStorePaths) == 0 {
|
|
|
|
return nil, fmt.Errorf("no driver store paths found")
|
|
|
|
}
|
|
|
|
logger.Infof("Using WSL driver store paths: %v", driverStorePaths)
|
|
|
|
|
2024-04-24 08:47:45 +00:00
|
|
|
return newWSLDriverStoreDiscoverer(logger, driverRoot, nvidiaCDIHookPath, ldconfigPath, driverStorePaths)
|
2023-02-06 17:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newWSLDriverStoreDiscoverer returns a Discoverer for WSL2 drivers in the driver store associated with a dxcore adapter.
|
2024-04-24 08:47:45 +00:00
|
|
|
func newWSLDriverStoreDiscoverer(logger logger.Interface, driverRoot string, nvidiaCDIHookPath string, ldconfigPath string, driverStorePaths []string) (discover.Discover, error) {
|
2023-02-06 17:53:24 +00:00
|
|
|
var searchPaths []string
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
for _, path := range driverStorePaths {
|
|
|
|
if seen[path] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
searchPaths = append(searchPaths, path)
|
|
|
|
}
|
|
|
|
if len(searchPaths) > 1 {
|
2023-06-06 19:46:38 +00:00
|
|
|
logger.Warningf("Found multiple driver store paths: %v", searchPaths)
|
2023-02-06 17:53:24 +00:00
|
|
|
}
|
|
|
|
searchPaths = append(searchPaths, "/usr/lib/wsl/lib")
|
|
|
|
|
|
|
|
libraries := discover.NewMounts(
|
|
|
|
logger,
|
|
|
|
lookup.NewFileLocator(
|
|
|
|
lookup.WithLogger(logger),
|
|
|
|
lookup.WithSearchPaths(
|
|
|
|
searchPaths...,
|
|
|
|
),
|
|
|
|
lookup.WithCount(1),
|
|
|
|
),
|
|
|
|
driverRoot,
|
|
|
|
requiredDriverStoreFiles,
|
|
|
|
)
|
|
|
|
|
2023-06-26 18:34:53 +00:00
|
|
|
symlinkHook := nvidiaSMISimlinkHook{
|
2024-04-24 08:47:45 +00:00
|
|
|
logger: logger,
|
|
|
|
mountsFrom: libraries,
|
|
|
|
nvidiaCDIHookPath: nvidiaCDIHookPath,
|
2023-06-26 18:34:53 +00:00
|
|
|
}
|
2023-02-06 17:53:24 +00:00
|
|
|
|
2024-04-24 08:47:45 +00:00
|
|
|
ldcacheHook, _ := discover.NewLDCacheUpdateHook(logger, libraries, nvidiaCDIHookPath, ldconfigPath)
|
2023-02-06 17:53:24 +00:00
|
|
|
|
|
|
|
d := discover.Merge(
|
|
|
|
libraries,
|
|
|
|
symlinkHook,
|
|
|
|
ldcacheHook,
|
|
|
|
)
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
2023-06-26 18:34:53 +00:00
|
|
|
|
|
|
|
type nvidiaSMISimlinkHook struct {
|
|
|
|
discover.None
|
2024-04-24 08:47:45 +00:00
|
|
|
logger logger.Interface
|
|
|
|
mountsFrom discover.Discover
|
|
|
|
nvidiaCDIHookPath string
|
2023-06-26 18:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hooks returns a hook that creates a symlink to nvidia-smi in the driver store.
|
|
|
|
// On WSL2 the driver store location is used unchanged, for this reason we need
|
|
|
|
// to create a symlink from /usr/bin/nvidia-smi to the nvidia-smi binary in the
|
|
|
|
// driver store.
|
|
|
|
func (m nvidiaSMISimlinkHook) Hooks() ([]discover.Hook, error) {
|
|
|
|
mounts, err := m.mountsFrom.Mounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to discover mounts: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var target string
|
|
|
|
for _, mount := range mounts {
|
|
|
|
if filepath.Base(mount.Path) == "nvidia-smi" {
|
|
|
|
target = mount.Path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if target == "" {
|
|
|
|
m.logger.Warningf("Failed to find nvidia-smi in mounts: %v", mounts)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
link := "/usr/bin/nvidia-smi"
|
|
|
|
links := []string{fmt.Sprintf("%s::%s", target, link)}
|
2024-04-24 08:47:45 +00:00
|
|
|
symlinkHook := discover.CreateCreateSymlinkHook(m.nvidiaCDIHookPath, links)
|
2023-06-26 18:34:53 +00:00
|
|
|
|
|
|
|
return symlinkHook.Hooks()
|
|
|
|
}
|