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-06-22 13:49:27 +00:00
|
|
|
func newDiscovererFromCSVFiles(logger logger.Interface, files []string, driverRoot 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-18 09:57:26 +00:00
|
|
|
symlinkLocator := lookup.NewSymlinkLocator(
|
|
|
|
lookup.WithLogger(logger),
|
|
|
|
lookup.WithRoot(driverRoot),
|
|
|
|
)
|
2022-04-07 09:12:41 +00:00
|
|
|
locators := map[csv.MountSpecType]lookup.Locator{
|
2023-02-02 14:57:15 +00:00
|
|
|
csv.MountSpecDev: lookup.NewCharDeviceLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)),
|
2023-07-18 13:36:03 +00:00
|
|
|
csv.MountSpecDir: lookup.NewDirectoryLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)),
|
2022-04-07 09:12:41 +00:00
|
|
|
// Libraries and symlinks are handled in the same way
|
|
|
|
csv.MountSpecLib: symlinkLocator,
|
|
|
|
csv.MountSpecSym: symlinkLocator,
|
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-06-16 12:17:53 +00:00
|
|
|
var mountSpecs []*csv.MountSpec
|
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
|
|
|
|
}
|
2022-06-16 12:17:53 +00:00
|
|
|
mountSpecs = append(mountSpecs, targets...)
|
2022-03-10 13:00:24 +00:00
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2023-02-02 14:57:15 +00:00
|
|
|
return newFromMountSpecs(logger, locators, driverRoot, mountSpecs)
|
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
|
|
|
}
|
|
|
|
|
2022-03-10 13:00:24 +00:00
|
|
|
// newFromMountSpecs creates a discoverer for the CSV file. A logger is also supplied.
|
2022-03-15 12:17:49 +00:00
|
|
|
// A list of csvDiscoverers is returned, with each being associated with a single MountSpecType.
|
2023-06-22 13:49:27 +00:00
|
|
|
func newFromMountSpecs(logger logger.Interface, locators map[csv.MountSpecType]lookup.Locator, driverRoot string, targets []*csv.MountSpec) (discover.Discover, error) {
|
2022-04-08 06:52:07 +00:00
|
|
|
if len(targets) == 0 {
|
2023-06-22 13:49:27 +00:00
|
|
|
return &discover.None{}, nil
|
2022-04-08 06:52:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
var discoverers []discover.Discover
|
2022-04-08 09:59:26 +00:00
|
|
|
var mountSpecTypes []csv.MountSpecType
|
2022-03-10 13:00:24 +00:00
|
|
|
candidatesByType := make(map[csv.MountSpecType][]string)
|
|
|
|
for _, t := range targets {
|
2022-04-08 09:59:26 +00:00
|
|
|
if _, exists := candidatesByType[t.Type]; !exists {
|
|
|
|
mountSpecTypes = append(mountSpecTypes, t.Type)
|
|
|
|
}
|
2022-03-10 13:00:24 +00:00
|
|
|
candidatesByType[t.Type] = append(candidatesByType[t.Type], t.Path)
|
|
|
|
}
|
|
|
|
|
2022-04-08 09:59:26 +00:00
|
|
|
for _, t := range mountSpecTypes {
|
2022-03-10 13:00:24 +00:00
|
|
|
locator, exists := locators[t]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("no locator defined for '%v'", t)
|
|
|
|
}
|
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
var m discover.Discover
|
2022-04-08 06:52:07 +00:00
|
|
|
switch t {
|
|
|
|
case csv.MountSpecDev:
|
2023-06-22 13:49:27 +00:00
|
|
|
m = discover.NewDeviceDiscoverer(logger, locator, driverRoot, candidatesByType[t])
|
2022-04-08 06:52:07 +00:00
|
|
|
default:
|
2023-06-22 13:49:27 +00:00
|
|
|
m = discover.NewMounts(logger, locator, driverRoot, candidatesByType[t])
|
2022-04-08 06:52:07 +00:00
|
|
|
}
|
2022-07-06 09:37:14 +00:00
|
|
|
discoverers = append(discoverers, m)
|
2022-03-09 15:52:50 +00:00
|
|
|
|
2022-03-10 13:00:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:49:27 +00:00
|
|
|
return discover.Merge(discoverers...), nil
|
2022-03-10 13:00:24 +00:00
|
|
|
}
|