2022-03-17 14:11:40 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2022, 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.
|
|
|
|
**/
|
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
package tegra
|
2022-03-17 14:11:40 +00:00
|
|
|
|
|
|
|
import (
|
2022-05-11 14:03:13 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-05-11 11:22:47 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
2022-03-17 14:11:40 +00:00
|
|
|
)
|
|
|
|
|
2023-05-11 11:22:47 +00:00
|
|
|
type symlinkHook struct {
|
2023-06-22 13:49:27 +00:00
|
|
|
discover.None
|
2024-04-24 08:47:45 +00:00
|
|
|
logger logger.Interface
|
|
|
|
nvidiaCDIHookPath string
|
|
|
|
targets []string
|
|
|
|
mountsFrom discover.Discover
|
2023-09-22 13:23:12 +00:00
|
|
|
|
|
|
|
// The following can be overridden for testing
|
|
|
|
symlinkChainLocator lookup.Locator
|
|
|
|
resolveSymlink func(string) (string, error)
|
2022-03-17 14:11:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
// createCSVSymlinkHooks creates a discoverer for a hook that creates required symlinks in the container
|
2023-09-22 13:23:12 +00:00
|
|
|
func (o tegraOptions) createCSVSymlinkHooks(targets []string, mounts discover.Discover) discover.Discover {
|
2023-07-05 14:51:39 +00:00
|
|
|
return symlinkHook{
|
2023-09-22 13:23:12 +00:00
|
|
|
logger: o.logger,
|
2024-04-24 08:47:45 +00:00
|
|
|
nvidiaCDIHookPath: o.nvidiaCDIHookPath,
|
2023-09-22 13:23:12 +00:00
|
|
|
targets: targets,
|
|
|
|
mountsFrom: mounts,
|
|
|
|
symlinkChainLocator: o.symlinkChainLocator,
|
|
|
|
resolveSymlink: o.resolveSymlink,
|
2022-03-17 14:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hooks returns a hook to create the symlinks from the required CSV files
|
2023-06-22 13:49:27 +00:00
|
|
|
func (d symlinkHook) Hooks() ([]discover.Hook, error) {
|
2023-05-11 11:22:47 +00:00
|
|
|
specificLinks, err := d.getSpecificLinks()
|
2022-05-11 14:03:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to determine specific links: %v", err)
|
|
|
|
}
|
2023-05-11 11:22:47 +00:00
|
|
|
|
|
|
|
csvSymlinks := d.getCSVFileSymlinks()
|
2022-05-11 14:03:13 +00:00
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
return discover.CreateCreateSymlinkHook(
|
2024-04-24 08:47:45 +00:00
|
|
|
d.nvidiaCDIHookPath,
|
2023-06-22 13:49:27 +00:00
|
|
|
append(csvSymlinks, specificLinks...),
|
|
|
|
).Hooks()
|
2022-03-17 14:11:40 +00:00
|
|
|
}
|
2022-05-11 14:03:13 +00:00
|
|
|
|
2023-05-11 11:22:47 +00:00
|
|
|
// getSpecificLinks returns the required specic links that need to be created
|
|
|
|
func (d symlinkHook) getSpecificLinks() ([]string, error) {
|
2022-05-11 14:03:13 +00:00
|
|
|
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)
|
|
|
|
|
2023-08-29 08:12:45 +00:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(lib, "libcuda.so"):
|
2022-05-11 14:03:13 +00:00
|
|
|
// XXX Many applications wrongly assume that libcuda.so exists (e.g. with dlopen).
|
|
|
|
target = "libcuda.so.1"
|
|
|
|
link = "libcuda.so"
|
2023-08-29 08:12:45 +00:00
|
|
|
case strings.HasPrefix(lib, "libGLX_nvidia.so"):
|
2022-05-11 14:03:13 +00:00
|
|
|
// XXX GLVND requires this symlink for indirect GLX support.
|
|
|
|
target = lib
|
|
|
|
link = "libGLX_indirect.so.0"
|
2023-08-29 08:12:45 +00:00
|
|
|
case strings.HasPrefix(lib, "libnvidia-opticalflow.so"):
|
2022-05-11 14:03:13 +00:00
|
|
|
// XXX Fix missing symlink for libnvidia-opticalflow.so.
|
|
|
|
target = "libnvidia-opticalflow.so.1"
|
|
|
|
link = "libnvidia-opticalflow.so"
|
2023-08-29 08:12:45 +00:00
|
|
|
default:
|
2022-05-11 14:03:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if linkProcessed[link] {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-11 11:22:47 +00:00
|
|
|
linkProcessed[link] = true
|
2022-05-11 14:03:13 +00:00
|
|
|
|
|
|
|
linkPath := filepath.Join(filepath.Dir(m.Path), link)
|
2023-05-11 11:22:47 +00:00
|
|
|
links = append(links, fmt.Sprintf("%v::%v", target, linkPath))
|
2022-05-11 14:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return links, nil
|
|
|
|
}
|
2023-05-11 11:22:47 +00:00
|
|
|
|
2023-07-05 14:51:39 +00:00
|
|
|
// getSymlinkCandidates returns a list of symlinks that are candidates for being created.
|
|
|
|
func (d symlinkHook) getSymlinkCandidates() []string {
|
2023-05-11 11:22:47 +00:00
|
|
|
var candidates []string
|
2023-07-05 14:51:39 +00:00
|
|
|
for _, target := range d.targets {
|
2023-09-22 13:23:12 +00:00
|
|
|
reslovedSymlinkChain, err := d.symlinkChainLocator.Locate(target)
|
2023-05-11 11:22:47 +00:00
|
|
|
if err != nil {
|
2023-07-05 14:51:39 +00:00
|
|
|
d.logger.Warningf("Failed to locate symlink %v", target)
|
2023-05-11 11:22:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-07-05 14:51:39 +00:00
|
|
|
candidates = append(candidates, reslovedSymlinkChain...)
|
2023-05-11 11:22:47 +00:00
|
|
|
}
|
2023-07-05 14:51:39 +00:00
|
|
|
return candidates
|
|
|
|
}
|
2023-05-11 11:22:47 +00:00
|
|
|
|
2023-07-05 14:51:39 +00:00
|
|
|
func (d symlinkHook) getCSVFileSymlinks() []string {
|
2023-05-11 11:22:47 +00:00
|
|
|
var links []string
|
|
|
|
created := make(map[string]bool)
|
|
|
|
// candidates is a list of absolute paths to symlinks in a chain, or the final target of the chain.
|
2023-07-05 14:51:39 +00:00
|
|
|
for _, candidate := range d.getSymlinkCandidates() {
|
2023-09-22 13:23:12 +00:00
|
|
|
target, err := d.resolveSymlink(candidate)
|
2023-05-11 11:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
d.logger.Debugf("Skipping invalid link: %v", err)
|
|
|
|
continue
|
|
|
|
} else if target == candidate {
|
|
|
|
d.logger.Debugf("%v is not a symlink", candidate)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
link := fmt.Sprintf("%v::%v", target, candidate)
|
|
|
|
if created[link] {
|
|
|
|
d.logger.Debugf("skipping duplicate link: %v", link)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
created[link] = true
|
|
|
|
|
|
|
|
links = append(links, link)
|
|
|
|
}
|
|
|
|
|
|
|
|
return links
|
|
|
|
}
|