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-09-22 13:23:12 +00:00
|
|
|
func (o tegraOptions) newDiscovererFromCSVFiles() (discover.Discover, error) {
|
|
|
|
if len(o.csvFiles) == 0 {
|
|
|
|
o.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-09-22 13:23:12 +00:00
|
|
|
targetsByType := getTargetsFromCSVFiles(o.logger, o.csvFiles)
|
2023-07-05 14:51:39 +00:00
|
|
|
|
|
|
|
devices := discover.NewDeviceDiscoverer(
|
2023-09-22 13:23:12 +00:00
|
|
|
o.logger,
|
|
|
|
lookup.NewCharDeviceLocator(lookup.WithLogger(o.logger), lookup.WithRoot(o.driverRoot)),
|
|
|
|
o.driverRoot,
|
2023-07-05 14:51:39 +00:00
|
|
|
targetsByType[csv.MountSpecDev],
|
|
|
|
)
|
|
|
|
|
|
|
|
directories := discover.NewMounts(
|
2023-09-22 13:23:12 +00:00
|
|
|
o.logger,
|
|
|
|
lookup.NewDirectoryLocator(lookup.WithLogger(o.logger), lookup.WithRoot(o.driverRoot)),
|
|
|
|
o.driverRoot,
|
2023-07-05 14:51:39 +00:00
|
|
|
targetsByType[csv.MountSpecDir],
|
|
|
|
)
|
|
|
|
|
|
|
|
// Libraries and symlinks use the same locator.
|
|
|
|
libraries := discover.NewMounts(
|
2023-09-22 13:23:12 +00:00
|
|
|
o.logger,
|
|
|
|
o.symlinkLocator,
|
|
|
|
o.driverRoot,
|
2023-07-05 14:51:39 +00:00
|
|
|
targetsByType[csv.MountSpecLib],
|
|
|
|
)
|
|
|
|
|
2023-09-22 14:30:20 +00:00
|
|
|
symlinkTargets := o.ignorePatterns.Apply(targetsByType[csv.MountSpecSym]...)
|
|
|
|
o.logger.Debugf("Filtered symlink targets: %v", symlinkTargets)
|
2023-07-05 14:51:39 +00:00
|
|
|
symlinks := discover.NewMounts(
|
2023-09-22 13:23:12 +00:00
|
|
|
o.logger,
|
|
|
|
o.symlinkLocator,
|
|
|
|
o.driverRoot,
|
2023-09-22 19:13:57 +00:00
|
|
|
symlinkTargets,
|
2023-07-05 14:51:39 +00:00
|
|
|
)
|
2023-09-22 19:13:57 +00:00
|
|
|
createSymlinks := o.createCSVSymlinkHooks(symlinkTargets, libraries)
|
2023-07-05 14:51:39 +00:00
|
|
|
|
|
|
|
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.
|
2023-09-22 13:23:12 +00:00
|
|
|
// TODO: We use a function variable here to allow this to be overridden for testing.
|
|
|
|
// This should be properly mocked.
|
|
|
|
var getTargetsFromCSVFiles = func(logger logger.Interface, files []string) map[csv.MountSpecType][]string {
|
2023-07-05 14:51:39 +00:00
|
|
|
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
|
|
|
}
|