2022-03-14 12:16:46 +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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
|
|
|
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewLDCacheUpdateHook creates a discoverer that updates the ldcache for the specified mounts. A logger can also be specified
|
|
|
|
func NewLDCacheUpdateHook(logger *logrus.Logger, mounts Discover, cfg *Config) (Discover, error) {
|
|
|
|
d := ldconfig{
|
|
|
|
logger: logger,
|
|
|
|
mountsFrom: mounts,
|
|
|
|
lookup: lookup.NewExecutableLocator(logger, cfg.Root),
|
|
|
|
nvidiaCTKExecutablePath: cfg.NVIDIAContainerToolkitCLIExecutablePath,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
nvidiaCTKDefaultFilePath = "/usr/bin/nvidia-ctk"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ldconfig struct {
|
|
|
|
None
|
|
|
|
logger *logrus.Logger
|
|
|
|
mountsFrom Discover
|
|
|
|
lookup lookup.Locator
|
|
|
|
nvidiaCTKExecutablePath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hooks checks the required mounts for libraries and returns a hook to update the LDcache for the discovered paths.
|
|
|
|
func (d ldconfig) Hooks() ([]Hook, error) {
|
|
|
|
mounts, err := d.mountsFrom.Mounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to discover mounts for ldcache update: %v", err)
|
|
|
|
}
|
2022-09-29 09:48:08 +00:00
|
|
|
h := CreateLDCacheUpdateHook(
|
|
|
|
d.logger,
|
|
|
|
d.lookup,
|
|
|
|
d.nvidiaCTKExecutablePath,
|
|
|
|
nvidiaCTKDefaultFilePath,
|
|
|
|
getLibraryPaths(mounts),
|
|
|
|
)
|
|
|
|
return []Hook{h}, nil
|
|
|
|
}
|
2022-03-14 12:16:46 +00:00
|
|
|
|
2022-09-29 09:48:08 +00:00
|
|
|
// CreateLDCacheUpdateHook locates the NVIDIA Container Toolkit CLI and creates a hook for updating the LD Cache
|
|
|
|
func CreateLDCacheUpdateHook(logger *logrus.Logger, lookup lookup.Locator, execuable string, defaultPath string, libraries []string) Hook {
|
|
|
|
hookPath := defaultPath
|
|
|
|
targets, err := lookup.Locate(execuable)
|
2022-03-14 12:16:46 +00:00
|
|
|
if err != nil {
|
2022-09-29 09:48:08 +00:00
|
|
|
logger.Warnf("Failed to locate %v: %v", execuable, err)
|
2022-03-14 12:16:46 +00:00
|
|
|
} else if len(targets) == 0 {
|
2022-09-29 09:48:08 +00:00
|
|
|
logger.Warnf("%v not found", execuable)
|
2022-03-14 12:16:46 +00:00
|
|
|
} else {
|
2022-09-29 09:48:08 +00:00
|
|
|
logger.Debugf("Found %v candidates: %v", execuable, targets)
|
2022-03-14 12:16:46 +00:00
|
|
|
hookPath = targets[0]
|
|
|
|
}
|
2022-09-29 09:48:08 +00:00
|
|
|
logger.Debugf("Using NVIDIA Container Toolkit CLI path %v", hookPath)
|
2022-03-14 12:16:46 +00:00
|
|
|
|
2022-09-29 10:14:12 +00:00
|
|
|
args := []string{filepath.Base(hookPath), "hook", "update-ldcache"}
|
2022-09-29 09:48:08 +00:00
|
|
|
for _, f := range uniqueFolders(libraries) {
|
2022-05-09 14:53:05 +00:00
|
|
|
args = append(args, "--folder", f)
|
2022-03-14 12:16:46 +00:00
|
|
|
}
|
2022-09-29 09:48:08 +00:00
|
|
|
return Hook{
|
2022-03-14 12:16:46 +00:00
|
|
|
Lifecycle: cdi.CreateContainerHook,
|
|
|
|
Path: hookPath,
|
|
|
|
Args: args,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 09:48:08 +00:00
|
|
|
// getLibraryPaths extracts the library dirs from the specified mounts
|
|
|
|
func getLibraryPaths(mounts []Mount) []string {
|
2022-03-14 12:16:46 +00:00
|
|
|
var paths []string
|
|
|
|
for _, m := range mounts {
|
2022-09-29 09:48:08 +00:00
|
|
|
if !isLibName(m.Path) {
|
2022-03-14 12:16:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-09-29 09:48:08 +00:00
|
|
|
paths = append(paths, m.Path)
|
2022-03-14 12:16:46 +00:00
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
// isLibName checks if the specified filename is a library (i.e. ends in `.so*`)
|
|
|
|
func isLibName(filename string) bool {
|
|
|
|
|
2022-07-15 10:17:01 +00:00
|
|
|
base := filepath.Base(filename)
|
|
|
|
|
|
|
|
isLib, err := filepath.Match("lib?*.so*", base)
|
|
|
|
if !isLib || err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(base, ".so")
|
|
|
|
if len(parts) == 1 {
|
|
|
|
return true
|
2022-03-14 12:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 10:17:01 +00:00
|
|
|
return parts[len(parts)-1] == "" || strings.HasPrefix(parts[len(parts)-1], ".")
|
2022-03-14 12:16:46 +00:00
|
|
|
}
|
2022-09-29 09:48:08 +00:00
|
|
|
|
|
|
|
// uniqueFolders returns the unique set of folders for the specified files
|
|
|
|
func uniqueFolders(libraries []string) []string {
|
|
|
|
var paths []string
|
|
|
|
checked := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, l := range libraries {
|
|
|
|
dir := filepath.Dir(l)
|
|
|
|
if dir == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if checked[dir] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
checked[dir] = true
|
|
|
|
paths = append(paths, dir)
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|