Skip to content

Commit

Permalink
Detect device root from driver root
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Nov 21, 2023
1 parent 14d518c commit a4076dd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/nvidia-dra-plugin/cdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type CDIHandler struct {
nvcdi nvcdi.Interface
registry cdiapi.Registry
driverRoot string
devRoot string
targetDriverRoot string
nvidiaCTKPath string

Expand Down Expand Up @@ -84,6 +85,7 @@ func NewCDIHandler(opts ...cdiOption) (*CDIHandler, error) {
nvcdilib, err := nvcdi.New(
nvcdi.WithDeviceLib(h.nvdevice),
nvcdi.WithDriverRoot(h.driverRoot),
nvcdi.WithDevRoot(h.devRoot),
nvcdi.WithLogger(h.logger),
nvcdi.WithNvmlLib(h.nvml),
nvcdi.WithMode("nvml"),
Expand Down
11 changes: 8 additions & 3 deletions cmd/nvidia-dra-plugin/device_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sync"

"github.com/NVIDIA/go-nvlib/pkg/nvml"
"k8s.io/klog/v2"

nascrd "github.com/NVIDIA/k8s-dra-driver/api/nvidia.com/resource/gpu/nas/v1alpha1"
)
Expand Down Expand Up @@ -126,7 +127,8 @@ type DeviceState struct {
}

func NewDeviceState(ctx context.Context, config *Config) (*DeviceState, error) {
nvdevlib, err := newDeviceLib(root(config.flags.containerDriverRoot))
containerDriverRoot := root(config.flags.containerDriverRoot)
nvdevlib, err := newDeviceLib(containerDriverRoot)
if err != nil {
return nil, fmt.Errorf("failed to create device library: %w", err)
}
Expand All @@ -136,12 +138,15 @@ func NewDeviceState(ctx context.Context, config *Config) (*DeviceState, error) {
return nil, fmt.Errorf("error enumerating all possible devices: %w", err)
}

devRoot := containerDriverRoot.getDevRoot()
klog.Infof("using devRoot=%v", devRoot)

hostDriverRoot := config.flags.hostDriverRoot
containerDriverRoot := config.flags.containerDriverRoot
cdi, err := NewCDIHandler(
WithNvml(nvdevlib.nvmllib),
WithDeviceLib(nvdevlib),
WithDriverRoot(containerDriverRoot),
WithDriverRoot(string(containerDriverRoot)),
WithDevRoot(devRoot),
WithTargetDriverRoot(hostDriverRoot),
WithNvidiaCTKPath(config.flags.nvidiaCTKPath),
WithCDIRoot(config.flags.cdiRoot),
Expand Down
20 changes: 20 additions & 0 deletions cmd/nvidia-dra-plugin/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"fmt"
"os"
"path/filepath"
)

Expand Down Expand Up @@ -60,6 +61,25 @@ func (r root) getNvidiaSMIPath() (string, error) {
return binaryPath, nil
}

// isDevRoot checks whether the specified root is a dev root.
// A dev root is defined as a root containing a /dev folder.
func (r root) isDevRoot() bool {
stat, err := os.Stat(filepath.Join(string(r), "dev"))
if err != nil {
return false
}
return stat.IsDir()
}

// getDevRoot returns the dev root associated with the root.
// If the root is not a dev root, this defaults to "/".
func (r root) getDevRoot() string {
if r.isDevRoot() {
return string(r)
}
return "/"
}

// findFile searches the root for a specified file.
// A number of folders can be specified to search in addition to the root itself.
// If the file represents a symlink, this is resolved and the final path is returned.
Expand Down
7 changes: 7 additions & 0 deletions cmd/nvidia-dra-plugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func WithDriverRoot(root string) cdiOption {
}
}

// WithDevRoot provides a cdiOption to set the device root used by the 'cdi' interface.
func WithDevRoot(root string) cdiOption {
return func(c *CDIHandler) {
c.devRoot = root
}
}

// WithTargetDriverRoot provides an cdiOption to set the target driver root used by the 'cdi' interface.
func WithTargetDriverRoot(root string) cdiOption {
return func(c *CDIHandler) {
Expand Down

0 comments on commit a4076dd

Please sign in to comment.