Skip to content

Commit fc8796e

Browse files
authored
Merge pull request #97 from cdesiniotis/nvpassthrough-make-load-opt-in
[nvpassthrough] do not load kernel modules unless user opts-in
2 parents f6b4227 + 6e479b9 commit fc8796e

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

pkg/nvpassthrough/nvpassthrough.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ type Interface interface {
4242
}
4343

4444
type nvpassthrough struct {
45-
logger basicLogger
46-
hostRoot string
47-
nvpciLib nvpci.Interface
45+
logger basicLogger
46+
hostRoot string
47+
nvpciLib nvpci.Interface
48+
loadKernelModules bool
4849
}
4950

5051
var _ Interface = (*nvpassthrough)(nil)
@@ -84,6 +85,8 @@ func WithLogger(logger basicLogger) Option {
8485
}
8586

8687
// WithHostRoot provides an Option to set the path to the host root filesystem.
88+
// The path is only used when the WithLoadKernelModules option is also enabled.
89+
// The path is assumed to be a chroot-able filesystem.
8790
func WithHostRoot(hostRoot string) Option {
8891
return func(w *nvpassthrough) {
8992
w.hostRoot = hostRoot
@@ -97,6 +100,15 @@ func WithNvpciLib(lib nvpci.Interface) Option {
97100
}
98101
}
99102

103+
// WithLoadKernelModules provides an Option for opting-in to loading
104+
// kernel modules before binding NVIDIA PCI devices to them. By default,
105+
// this behavior is disabled.
106+
func WithLoadKernelModules(loadKernelModules bool) Option {
107+
return func(w *nvpassthrough) {
108+
w.loadKernelModules = loadKernelModules
109+
}
110+
}
111+
100112
// FindBestVFIOVariant finds the "best" match of all vfio_pci aliases for
101113
// device in the host modules.alias file. This uses the algorithm of
102114
// finding every modules.alias line that begins with "alias vfio_pci:",
@@ -166,9 +178,11 @@ func (n *nvpassthrough) BindToVFIODriver(address string) error {
166178
return fmt.Errorf("failed to find best vfio variant driver: %w", err)
167179
}
168180

169-
km := newKernelModules(n.hostRoot)
170-
if err := km.load(vfioDriverName); err != nil {
171-
return fmt.Errorf("failed to load %q driver: %w", vfioDriverName, err)
181+
if n.loadKernelModules {
182+
km := newKernelModules(n.hostRoot)
183+
if err := km.load(vfioDriverName); err != nil {
184+
return fmt.Errorf("failed to load %q driver: %w", vfioDriverName, err)
185+
}
172186
}
173187

174188
// (cdesiniotis) Module names in the modules.alias file will only ever contain

0 commit comments

Comments
 (0)