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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-15 14:42:06 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-04-05 07:35:27 +00:00
|
|
|
"sync"
|
2022-03-09 15:52:50 +00:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mounts is a generic discoverer for Mounts. It is customized by specifying the
|
|
|
|
// required entities as a list and a Locator that is used to find the target mounts
|
|
|
|
// based on the entry in the list.
|
|
|
|
type mounts struct {
|
|
|
|
None
|
2023-03-22 12:27:43 +00:00
|
|
|
logger logger.Interface
|
2022-03-09 15:52:50 +00:00
|
|
|
lookup lookup.Locator
|
2022-07-15 14:42:06 +00:00
|
|
|
root string
|
2022-03-09 15:52:50 +00:00
|
|
|
required []string
|
2022-04-05 07:35:27 +00:00
|
|
|
sync.Mutex
|
|
|
|
cache []Mount
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Discover = (*mounts)(nil)
|
|
|
|
|
2022-07-15 14:42:06 +00:00
|
|
|
// NewMounts creates a discoverer for the required mounts using the specified locator.
|
2023-03-22 12:27:43 +00:00
|
|
|
func NewMounts(logger logger.Interface, lookup lookup.Locator, root string, required []string) Discover {
|
2023-02-07 11:07:05 +00:00
|
|
|
return newMounts(logger, lookup, root, required)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newMounts creates a discoverer for the required mounts using the specified locator.
|
2023-03-22 12:27:43 +00:00
|
|
|
func newMounts(logger logger.Interface, lookup lookup.Locator, root string, required []string) *mounts {
|
2022-07-15 14:42:06 +00:00
|
|
|
return &mounts{
|
|
|
|
logger: logger,
|
|
|
|
lookup: lookup,
|
|
|
|
root: filepath.Join("/", root),
|
|
|
|
required: required,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:35:27 +00:00
|
|
|
func (d *mounts) Mounts() ([]Mount, error) {
|
2022-03-09 15:52:50 +00:00
|
|
|
if d.lookup == nil {
|
|
|
|
return nil, fmt.Errorf("no lookup defined")
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:35:27 +00:00
|
|
|
if d.cache != nil {
|
|
|
|
d.logger.Debugf("returning cached mounts")
|
|
|
|
return d.cache, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
2022-06-29 14:50:17 +00:00
|
|
|
uniqueMounts := make(map[string]Mount)
|
2022-03-09 15:52:50 +00:00
|
|
|
|
|
|
|
for _, candidate := range d.required {
|
|
|
|
d.logger.Debugf("Locating %v", candidate)
|
|
|
|
located, err := d.lookup.Locate(candidate)
|
|
|
|
if err != nil {
|
2023-06-06 19:46:38 +00:00
|
|
|
d.logger.Warningf("Could not locate %v: %v", candidate, err)
|
2022-03-09 15:52:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(located) == 0 {
|
2023-06-06 19:46:38 +00:00
|
|
|
d.logger.Warningf("Missing %v", candidate)
|
2022-03-09 15:52:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.logger.Debugf("Located %v as %v", candidate, located)
|
|
|
|
for _, p := range located {
|
2022-06-29 14:50:17 +00:00
|
|
|
if _, ok := uniqueMounts[p]; ok {
|
|
|
|
d.logger.Debugf("Skipping duplicate mount %v", p)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:42:06 +00:00
|
|
|
r := d.relativeTo(p)
|
2022-06-29 14:50:17 +00:00
|
|
|
if r == "" {
|
|
|
|
r = p
|
|
|
|
}
|
|
|
|
|
|
|
|
d.logger.Infof("Selecting %v as %v", p, r)
|
|
|
|
uniqueMounts[p] = Mount{
|
|
|
|
HostPath: p,
|
|
|
|
Path: r,
|
2023-02-07 11:10:32 +00:00
|
|
|
Options: []string{
|
|
|
|
"ro",
|
|
|
|
"nosuid",
|
|
|
|
"nodev",
|
|
|
|
"bind",
|
|
|
|
},
|
2022-06-29 14:50:17 +00:00
|
|
|
}
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var mounts []Mount
|
2022-06-29 14:50:17 +00:00
|
|
|
for _, m := range uniqueMounts {
|
|
|
|
mounts = append(mounts, m)
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 07:35:27 +00:00
|
|
|
d.cache = mounts
|
|
|
|
|
2022-06-29 14:50:17 +00:00
|
|
|
return d.cache, nil
|
2022-03-09 15:52:50 +00:00
|
|
|
}
|
2022-07-15 14:42:06 +00:00
|
|
|
|
|
|
|
// relativeTo returns the path relative to the root for the file locator
|
|
|
|
func (d *mounts) relativeTo(path string) string {
|
|
|
|
if d.root == "/" {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimPrefix(path, d.root)
|
|
|
|
}
|