2022-03-09 15:52:50 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2021, 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-09 15:52:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2022-03-09 15:52:50 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
2023-06-22 13:49:27 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
|
2022-03-09 15:52:50 +00:00
|
|
|
)
|
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
// newDiscovererFromCSVFiles creates a discoverer for the specified CSV files. A logger is also supplied.
|
2022-03-15 12:17:49 +00:00
|
|
|
// The constructed discoverer is comprised of a list, with each element in the list being associated with a
|
|
|
|
// single CSV files.
|
2023-07-18 10:02:37 +00:00
|
|
|
func newDiscovererFromCSVFiles(logger logger.Interface, files []string, driverRoot string, nvidiaCTKPath string, librarySearchPaths []string) (discover.Discover, error) {
|
2022-03-09 15:52:50 +00:00
|
|
|
if len(files) == 0 {
|
2023-06-06 19:46:38 +00:00
|
|
|
logger.Warningf("No CSV files specified")
|
2023-06-22 13:49:27 +00:00
|
|
|
return discover.None{}, nil
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:51:39 +00:00
|
|
|
targetsByType := getTargetsFromCSVFiles(logger, files)
|
|
|
|
|
|
|
|
devices := discover.NewDeviceDiscoverer(
|
|
|
|
logger,
|
|
|
|
lookup.NewCharDeviceLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)),
|
|
|
|
driverRoot,
|
|
|
|
targetsByType[csv.MountSpecDev],
|
|
|
|
)
|
|
|
|
|
|
|
|
directories := discover.NewMounts(
|
|
|
|
logger,
|
|
|
|
lookup.NewDirectoryLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)),
|
|
|
|
driverRoot,
|
|
|
|
targetsByType[csv.MountSpecDir],
|
|
|
|
)
|
|
|
|
|
|
|
|
// Libraries and symlinks use the same locator.
|
2023-07-18 10:02:37 +00:00
|
|
|
searchPaths := append(librarySearchPaths, "/")
|
|
|
|
symlinkLocator := lookup.NewSymlinkLocator(
|
|
|
|
lookup.WithLogger(logger),
|
|
|
|
lookup.WithRoot(driverRoot),
|
|
|
|
lookup.WithSearchPaths(searchPaths...),
|
|
|
|
)
|
2023-07-05 14:51:39 +00:00
|
|
|
libraries := discover.NewMounts(
|
|
|
|
logger,
|
|
|
|
symlinkLocator,
|
|
|
|
driverRoot,
|
|
|
|
targetsByType[csv.MountSpecLib],
|
|
|
|
)
|
|
|
|
|
|
|
|
nonLibSymlinks := ignoreFilenamePatterns{"*.so", "*.so.[0-9]"}.Apply(targetsByType[csv.MountSpecSym]...)
|
|
|
|
logger.Debugf("Non-lib symlinks: %v", nonLibSymlinks)
|
|
|
|
symlinks := discover.NewMounts(
|
|
|
|
logger,
|
|
|
|
symlinkLocator,
|
|
|
|
driverRoot,
|
|
|
|
nonLibSymlinks,
|
|
|
|
)
|
|
|
|
createSymlinks := createCSVSymlinkHooks(logger, nonLibSymlinks, libraries, nvidiaCTKPath)
|
|
|
|
|
|
|
|
d := discover.Merge(
|
|
|
|
devices,
|
|
|
|
directories,
|
|
|
|
libraries,
|
|
|
|
symlinks,
|
|
|
|
createSymlinks,
|
2023-07-18 09:57:26 +00:00
|
|
|
)
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2023-07-05 14:51:39 +00:00
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTargetsFromCSVFiles returns the list of mount specs from the specified CSV files.
|
|
|
|
// These are aggregated by mount spec type.
|
|
|
|
func getTargetsFromCSVFiles(logger logger.Interface, files []string) map[csv.MountSpecType][]string {
|
|
|
|
targetsByType := make(map[csv.MountSpecType][]string)
|
2022-03-10 13:00:24 +00:00
|
|
|
for _, filename := range files {
|
2022-06-16 12:17:53 +00:00
|
|
|
targets, err := loadCSVFile(logger, filename)
|
2022-03-09 15:52:50 +00:00
|
|
|
if err != nil {
|
2023-06-06 19:46:38 +00:00
|
|
|
logger.Warningf("Skipping CSV file %v: %v", filename, err)
|
2022-03-09 15:52:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-07-05 14:51:39 +00:00
|
|
|
for _, t := range targets {
|
|
|
|
targetsByType[t.Type] = append(targetsByType[t.Type], t.Path)
|
|
|
|
}
|
2022-03-10 13:00:24 +00:00
|
|
|
}
|
2023-07-05 14:51:39 +00:00
|
|
|
return targetsByType
|
2022-03-10 13:00:24 +00:00
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-07-06 09:37:14 +00:00
|
|
|
// loadCSVFile loads the specified CSV file and returns the list of mount specs
|
2023-03-22 12:27:43 +00:00
|
|
|
func loadCSVFile(logger logger.Interface, filename string) ([]*csv.MountSpec, error) {
|
2022-03-10 13:00:24 +00:00
|
|
|
// Create a discoverer for each file-kind combination
|
2022-04-08 06:11:10 +00:00
|
|
|
targets, err := csv.NewCSVFileParser(logger, filename).Parse()
|
2022-03-10 13:00:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse CSV file: %v", err)
|
|
|
|
}
|
|
|
|
if len(targets) == 0 {
|
|
|
|
return nil, fmt.Errorf("CSV file is empty")
|
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-06-16 12:17:53 +00:00
|
|
|
return targets, nil
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|