2023-05-10 12:45:33 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package tegra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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-10 14:47:22 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
2023-09-22 13:23:12 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
|
2023-05-10 12:45:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tegraOptions struct {
|
2023-07-18 10:02:37 +00:00
|
|
|
logger logger.Interface
|
|
|
|
csvFiles []string
|
|
|
|
driverRoot string
|
2023-11-14 15:57:37 +00:00
|
|
|
devRoot string
|
2024-04-24 08:47:45 +00:00
|
|
|
nvidiaCDIHookPath string
|
2023-12-15 00:46:00 +00:00
|
|
|
ldconfigPath string
|
2023-07-18 10:02:37 +00:00
|
|
|
librarySearchPaths []string
|
2023-09-22 14:30:20 +00:00
|
|
|
ignorePatterns ignoreMountSpecPatterns
|
2023-09-22 13:23:12 +00:00
|
|
|
|
|
|
|
// The following can be overridden for testing
|
|
|
|
symlinkLocator lookup.Locator
|
|
|
|
symlinkChainLocator lookup.Locator
|
|
|
|
// TODO: This should be replaced by a regular mock
|
|
|
|
resolveSymlink func(string) (string, error)
|
2023-05-10 12:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option defines a functional option for configuring a Tegra discoverer.
|
|
|
|
type Option func(*tegraOptions)
|
|
|
|
|
|
|
|
// New creates a new tegra discoverer using the supplied options.
|
|
|
|
func New(opts ...Option) (discover.Discover, error) {
|
|
|
|
o := &tegraOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(o)
|
|
|
|
}
|
|
|
|
|
2023-11-14 15:57:37 +00:00
|
|
|
if o.devRoot == "" {
|
|
|
|
o.devRoot = o.driverRoot
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:23:12 +00:00
|
|
|
if o.symlinkLocator == nil {
|
|
|
|
o.symlinkLocator = lookup.NewSymlinkLocator(
|
|
|
|
lookup.WithLogger(o.logger),
|
|
|
|
lookup.WithRoot(o.driverRoot),
|
2023-08-25 15:38:36 +00:00
|
|
|
lookup.WithSearchPaths(append(o.librarySearchPaths, "/")...),
|
2023-09-22 13:23:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.symlinkChainLocator == nil {
|
|
|
|
o.symlinkChainLocator = lookup.NewSymlinkChainLocator(
|
|
|
|
lookup.WithLogger(o.logger),
|
|
|
|
lookup.WithRoot(o.driverRoot),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.resolveSymlink == nil {
|
|
|
|
o.resolveSymlink = symlinks.Resolve
|
|
|
|
}
|
|
|
|
|
|
|
|
csvDiscoverer, err := o.newDiscovererFromCSVFiles()
|
2023-05-10 12:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create CSV discoverer: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-24 08:47:45 +00:00
|
|
|
ldcacheUpdateHook, err := discover.NewLDCacheUpdateHook(o.logger, csvDiscoverer, o.nvidiaCDIHookPath, o.ldconfigPath)
|
2023-05-10 12:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create ldcach update hook discoverer: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:47:22 +00:00
|
|
|
tegraSystemMounts := discover.NewMounts(
|
|
|
|
o.logger,
|
|
|
|
lookup.NewFileLocator(lookup.WithLogger(o.logger)),
|
|
|
|
"",
|
|
|
|
[]string{
|
|
|
|
"/etc/nv_tegra_release",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-05-10 12:45:33 +00:00
|
|
|
d := discover.Merge(
|
|
|
|
csvDiscoverer,
|
|
|
|
// The ldcacheUpdateHook is added last to ensure that the created symlinks are included
|
|
|
|
ldcacheUpdateHook,
|
2023-05-10 14:47:22 +00:00
|
|
|
tegraSystemMounts,
|
2023-05-10 12:45:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger sets the logger for the discoverer.
|
2023-03-22 12:27:43 +00:00
|
|
|
func WithLogger(logger logger.Interface) Option {
|
2023-05-10 12:45:33 +00:00
|
|
|
return func(o *tegraOptions) {
|
|
|
|
o.logger = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDriverRoot sets the driver root for the discoverer.
|
|
|
|
func WithDriverRoot(driverRoot string) Option {
|
|
|
|
return func(o *tegraOptions) {
|
|
|
|
o.driverRoot = driverRoot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 15:57:37 +00:00
|
|
|
// WithDevRoot sets the /dev root.
|
|
|
|
// If this is unset, the driver root is assumed.
|
2023-12-14 03:56:02 +00:00
|
|
|
func WithDevRoot(devRoot string) Option {
|
2023-11-14 15:57:37 +00:00
|
|
|
return func(o *tegraOptions) {
|
2023-12-14 03:56:02 +00:00
|
|
|
o.devRoot = devRoot
|
2023-11-14 15:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 12:45:33 +00:00
|
|
|
// WithCSVFiles sets the CSV files for the discoverer.
|
|
|
|
func WithCSVFiles(csvFiles []string) Option {
|
|
|
|
return func(o *tegraOptions) {
|
|
|
|
o.csvFiles = csvFiles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 08:47:45 +00:00
|
|
|
// WithNVIDIACDIHookPath sets the path to the nvidia-cdi-hook binary.
|
|
|
|
func WithNVIDIACDIHookPath(nvidiaCDIHookPath string) Option {
|
2023-05-10 12:45:33 +00:00
|
|
|
return func(o *tegraOptions) {
|
2024-04-24 08:47:45 +00:00
|
|
|
o.nvidiaCDIHookPath = nvidiaCDIHookPath
|
2023-05-10 12:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-18 10:02:37 +00:00
|
|
|
|
2023-12-15 00:46:00 +00:00
|
|
|
// WithLdconfigPath sets the path to the ldconfig program
|
|
|
|
func WithLdconfigPath(ldconfigPath string) Option {
|
|
|
|
return func(o *tegraOptions) {
|
|
|
|
o.ldconfigPath = ldconfigPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 10:02:37 +00:00
|
|
|
// WithLibrarySearchPaths sets the library search paths for the discoverer.
|
|
|
|
func WithLibrarySearchPaths(librarySearchPaths ...string) Option {
|
|
|
|
return func(o *tegraOptions) {
|
|
|
|
o.librarySearchPaths = librarySearchPaths
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 14:30:20 +00:00
|
|
|
|
|
|
|
// WithIngorePatterns sets patterns to ignore in the CSV files
|
|
|
|
func WithIngorePatterns(ignorePatterns ...string) Option {
|
|
|
|
return func(o *tegraOptions) {
|
|
|
|
o.ignorePatterns = ignoreMountSpecPatterns(ignorePatterns)
|
|
|
|
}
|
|
|
|
}
|