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"
|
|
|
|
|
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
|
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
|
2024-09-26 12:37:05 +00:00
|
|
|
func (o tegraOptions) createCSVSymlinkHooks(targets []string) 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,
|
|
|
|
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) {
|
|
|
|
return discover.CreateCreateSymlinkHook(
|
2024-04-24 08:47:45 +00:00
|
|
|
d.nvidiaCDIHookPath,
|
2024-09-26 12:37:05 +00:00
|
|
|
d.getCSVFileSymlinks(),
|
2023-06-22 13:49:27 +00:00
|
|
|
).Hooks()
|
2022-03-17 14:11:40 +00:00
|
|
|
}
|
2022-05-11 14:03:13 +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
|
|
|
|
}
|