Add creation of select driver symlinks to CDI spec

This change aligns the creation of symlinks under CDI with
the implementation in libnvidia-container. If the driver libraries
are present, the following symlinks are created:

* {{ .LibRoot }}/libcuda.so -> libcuda.so.1
* {{ .LibRoot }}/libnvidia-opticalflow.so -> libnvidia-opticalflow.so.1
* {{ .LibRoot }}/libGLX_indirect.so.0 -> libGLX_nvidia.so.{{ .Version }}

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar
2024-09-26 14:37:05 +02:00
parent 006aebf31e
commit 82ae2e615a
7 changed files with 464 additions and 70 deletions

View File

@@ -139,7 +139,7 @@ func TestNewNvmlMIGDiscoverer(t *testing.T) {
},
expectedDevices: nil,
expectedMounts: nil,
expectedHooks: []discover.Hook{},
expectedHooks: nil,
},
}
for _, tc := range testCases {

View File

@@ -49,14 +49,20 @@ func (o tegraOptions) newDiscovererFromCSVFiles() (discover.Discover, error) {
targetsByType[csv.MountSpecDir],
)
// Libraries and symlinks use the same locator.
libraries := discover.NewMounts(
o.logger,
o.symlinkLocator,
o.driverRoot,
targetsByType[csv.MountSpecLib],
// We create a discoverer for mounted libraries and add additional .so
// symlinks for the driver.
libraries := discover.WithDriverDotSoSymlinks(
discover.NewMounts(
o.logger,
o.symlinkLocator,
o.driverRoot,
targetsByType[csv.MountSpecLib],
),
"",
o.nvidiaCDIHookPath,
)
// We process the expliclitlty requested symlinks.
symlinkTargets := o.ignorePatterns.Apply(targetsByType[csv.MountSpecSym]...)
o.logger.Debugf("Filtered symlink targets: %v", symlinkTargets)
symlinks := discover.NewMounts(
@@ -65,7 +71,7 @@ func (o tegraOptions) newDiscovererFromCSVFiles() (discover.Discover, error) {
o.driverRoot,
symlinkTargets,
)
createSymlinks := o.createCSVSymlinkHooks(symlinkTargets, libraries)
createSymlinks := o.createCSVSymlinkHooks(symlinkTargets)
d := discover.Merge(
devices,

View File

@@ -18,8 +18,6 @@ package tegra
import (
"fmt"
"path/filepath"
"strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
@@ -31,7 +29,6 @@ type symlinkHook struct {
logger logger.Interface
nvidiaCDIHookPath string
targets []string
mountsFrom discover.Discover
// The following can be overridden for testing
symlinkChainLocator lookup.Locator
@@ -39,12 +36,11 @@ type symlinkHook struct {
}
// createCSVSymlinkHooks creates a discoverer for a hook that creates required symlinks in the container
func (o tegraOptions) createCSVSymlinkHooks(targets []string, mounts discover.Discover) discover.Discover {
func (o tegraOptions) createCSVSymlinkHooks(targets []string) discover.Discover {
return symlinkHook{
logger: o.logger,
nvidiaCDIHookPath: o.nvidiaCDIHookPath,
targets: targets,
mountsFrom: mounts,
symlinkChainLocator: o.symlinkChainLocator,
resolveSymlink: o.resolveSymlink,
}
@@ -52,62 +48,12 @@ func (o tegraOptions) createCSVSymlinkHooks(targets []string, mounts discover.Di
// Hooks returns a hook to create the symlinks from the required CSV files
func (d symlinkHook) Hooks() ([]discover.Hook, error) {
specificLinks, err := d.getSpecificLinks()
if err != nil {
return nil, fmt.Errorf("failed to determine specific links: %v", err)
}
csvSymlinks := d.getCSVFileSymlinks()
return discover.CreateCreateSymlinkHook(
d.nvidiaCDIHookPath,
append(csvSymlinks, specificLinks...),
d.getCSVFileSymlinks(),
).Hooks()
}
// getSpecificLinks returns the required specic links that need to be created
func (d symlinkHook) getSpecificLinks() ([]string, error) {
mounts, err := d.mountsFrom.Mounts()
if err != nil {
return nil, fmt.Errorf("failed to discover mounts for ldcache update: %v", err)
}
linkProcessed := make(map[string]bool)
var links []string
for _, m := range mounts {
var target string
var link string
lib := filepath.Base(m.Path)
switch {
case strings.HasPrefix(lib, "libcuda.so"):
// XXX Many applications wrongly assume that libcuda.so exists (e.g. with dlopen).
target = "libcuda.so.1"
link = "libcuda.so"
case strings.HasPrefix(lib, "libGLX_nvidia.so"):
// XXX GLVND requires this symlink for indirect GLX support.
target = lib
link = "libGLX_indirect.so.0"
case strings.HasPrefix(lib, "libnvidia-opticalflow.so"):
// XXX Fix missing symlink for libnvidia-opticalflow.so.
target = "libnvidia-opticalflow.so.1"
link = "libnvidia-opticalflow.so"
default:
continue
}
if linkProcessed[link] {
continue
}
linkProcessed[link] = true
linkPath := filepath.Join(filepath.Dir(m.Path), link)
links = append(links, fmt.Sprintf("%v::%v", target, linkPath))
}
return links, nil
}
// getSymlinkCandidates returns a list of symlinks that are candidates for being created.
func (d symlinkHook) getSymlinkCandidates() []string {
var candidates []string