Ensure existence of DRM devices nodes is checked

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-12-12 14:48:54 +01:00
parent 14e587d55f
commit 311e7a1feb
3 changed files with 37 additions and 90 deletions
cmd/nvidia-ctk/cdi/generate

View File

@ -1,57 +0,0 @@
/**
# 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 generate
import (
"path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/sirupsen/logrus"
)
// deviceDiscoverer defines a discoverer for device nodes
type deviceDiscoverer struct {
logger *logrus.Logger
root string
deviceNodePaths []string
}
var _ discover.Discover = (*deviceDiscoverer)(nil)
// Devices returns the device nodes for the full GPU.
func (d *deviceDiscoverer) Devices() ([]discover.Device, error) {
var deviceNodes []discover.Device
for _, dn := range d.deviceNodePaths {
deviceNode := discover.Device{
HostPath: filepath.Join(d.root, dn),
Path: dn,
}
deviceNodes = append(deviceNodes, deviceNode)
}
return deviceNodes, nil
}
// Hooks returns no hooks for a device discoverer
func (d *deviceDiscoverer) Hooks() ([]discover.Hook, error) {
return nil, nil
}
// Mounts returns no mounts for a device discoverer
func (d *deviceDiscoverer) Mounts() ([]discover.Mount, error) {
return nil, nil
}

View File

@ -29,14 +29,14 @@ import (
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
)
// fullGPUDiscoverer wraps a deviceDiscoverer and adds specifics required for discovering full GPUs
type fullGPUDiscoverer struct {
deviceDiscoverer
// byPathHookDiscoverer discovers the entities required for injecting by-path DRM device links
type byPathHookDiscoverer struct {
logger *logrus.Logger
root string
pciBusID string
}
var _ discover.Discover = (*fullGPUDiscoverer)(nil)
var _ discover.Discover = (*byPathHookDiscoverer)(nil)
// NewFullGPUDiscoverer creates a discoverer for the full GPU defined by the specified device.
func NewFullGPUDiscoverer(logger *logrus.Logger, root string, d device.Device) (discover.Discover, error) {
@ -61,22 +61,35 @@ func NewFullGPUDiscoverer(logger *logrus.Logger, root string, d device.Device) (
deviceNodePaths := append([]string{path}, drmDeviceNodes...)
device := fullGPUDiscoverer{
deviceDiscoverer: deviceDiscoverer{
logger: logger,
root: root,
deviceNodePaths: deviceNodePaths,
},
deviceNodes := discover.NewCharDeviceDiscoverer(
logger,
deviceNodePaths,
root,
)
byPathHooks := &byPathHookDiscoverer{
logger: logger,
root: root,
pciBusID: pciBusID,
}
return &device, nil
dd := discover.Merge(
deviceNodes,
byPathHooks,
)
return dd, nil
}
// Devices returns the empty list for the by-path hook discoverer
func (d *byPathHookDiscoverer) Devices() ([]discover.Device, error) {
return nil, nil
}
// Hooks returns the hooks for the GPU device.
// The following hooks are detected:
// 1. A hook to create /dev/dri/by-path symlinks
func (d *fullGPUDiscoverer) Hooks() ([]discover.Hook, error) {
func (d *byPathHookDiscoverer) Hooks() ([]discover.Hook, error) {
links, err := d.deviceNodeLinks()
if err != nil {
return nil, fmt.Errorf("failed to discover DRA device links: %v", err)
@ -103,11 +116,11 @@ func (d *fullGPUDiscoverer) Hooks() ([]discover.Hook, error) {
}
// Mounts returns an empty slice for a full GPU
func (d *fullGPUDiscoverer) Mounts() ([]discover.Mount, error) {
func (d *byPathHookDiscoverer) Mounts() ([]discover.Mount, error) {
return nil, nil
}
func (d *fullGPUDiscoverer) deviceNodeLinks() ([]string, error) {
func (d *byPathHookDiscoverer) deviceNodeLinks() ([]string, error) {
candidates := []string{
fmt.Sprintf("/dev/dri/by-path/pci-%s-card", d.pciBusID),
fmt.Sprintf("/dev/dri/by-path/pci-%s-render", d.pciBusID),

View File

@ -26,13 +26,6 @@ import (
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
)
// migDeviceDiscoverer wraps a deviceDiscoverer and adds specifics required for discovering MIG devices.
type migDeviceDiscoverer struct {
deviceDiscoverer
}
var _ discover.Discover = (*migDeviceDiscoverer)(nil)
// NewMigDeviceDiscoverer creates a discoverer for the specified mig device and its parent.
func NewMigDeviceDiscoverer(logger *logrus.Logger, root string, parent device.Device, d device.MigDevice) (discover.Discover, error) {
minor, ret := parent.GetMinorNumber()
@ -68,17 +61,15 @@ func NewMigDeviceDiscoverer(logger *logrus.Logger, root string, parent device.De
return nil, fmt.Errorf("failed to get CI cap device path: %v", err)
}
m := migDeviceDiscoverer{
deviceDiscoverer: deviceDiscoverer{
logger: logger,
root: root,
deviceNodePaths: []string{
parentPath,
giCapDevicePath,
ciCapDevicePath,
},
deviceNodes := discover.NewCharDeviceDiscoverer(
logger,
[]string{
parentPath,
giCapDevicePath,
ciCapDevicePath,
},
}
root,
)
return &m, nil
return deviceNodes, nil
}