mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
Split internal system package
This changes splits the functionality in the internal system package into two packages: one for dealing with devices and one for dealing with kernel modules. This removes ambiguity around the meaning of driver / device roots in each case. In each case, a root can be specified where device nodes are created or kernel modules loaded. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
49
internal/system/nvmodules/cmd.go
Normal file
49
internal/system/nvmodules/cmd.go
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
# 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 nvmodules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
||||
)
|
||||
|
||||
//go:generate moq -stub -out cmd_mock.go . cmder
|
||||
type cmder interface {
|
||||
// Run executes the command and returns the stdout, stderr, and an error if any
|
||||
Run(string, ...string) error
|
||||
}
|
||||
|
||||
type cmderLogger struct {
|
||||
logger.Interface
|
||||
}
|
||||
|
||||
func (c *cmderLogger) Run(cmd string, args ...string) error {
|
||||
c.Infof("Running: %v %v", cmd, strings.Join(args, " "))
|
||||
return nil
|
||||
}
|
||||
|
||||
type cmderExec struct{}
|
||||
|
||||
func (c *cmderExec) Run(cmd string, args ...string) error {
|
||||
if output, err := exec.Command(cmd, args...).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("%w; output=%v", err, string(output))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
83
internal/system/nvmodules/cmd_mock.go
Normal file
83
internal/system/nvmodules/cmd_mock.go
Normal file
@@ -0,0 +1,83 @@
|
||||
// Code generated by moq; DO NOT EDIT.
|
||||
// github.com/matryer/moq
|
||||
|
||||
package nvmodules
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Ensure, that cmderMock does implement cmder.
|
||||
// If this is not the case, regenerate this file with moq.
|
||||
var _ cmder = &cmderMock{}
|
||||
|
||||
// cmderMock is a mock implementation of cmder.
|
||||
//
|
||||
// func TestSomethingThatUsescmder(t *testing.T) {
|
||||
//
|
||||
// // make and configure a mocked cmder
|
||||
// mockedcmder := &cmderMock{
|
||||
// RunFunc: func(s string, strings ...string) error {
|
||||
// panic("mock out the Run method")
|
||||
// },
|
||||
// }
|
||||
//
|
||||
// // use mockedcmder in code that requires cmder
|
||||
// // and then make assertions.
|
||||
//
|
||||
// }
|
||||
type cmderMock struct {
|
||||
// RunFunc mocks the Run method.
|
||||
RunFunc func(s string, strings ...string) error
|
||||
|
||||
// calls tracks calls to the methods.
|
||||
calls struct {
|
||||
// Run holds details about calls to the Run method.
|
||||
Run []struct {
|
||||
// S is the s argument value.
|
||||
S string
|
||||
// Strings is the strings argument value.
|
||||
Strings []string
|
||||
}
|
||||
}
|
||||
lockRun sync.RWMutex
|
||||
}
|
||||
|
||||
// Run calls RunFunc.
|
||||
func (mock *cmderMock) Run(s string, strings ...string) error {
|
||||
callInfo := struct {
|
||||
S string
|
||||
Strings []string
|
||||
}{
|
||||
S: s,
|
||||
Strings: strings,
|
||||
}
|
||||
mock.lockRun.Lock()
|
||||
mock.calls.Run = append(mock.calls.Run, callInfo)
|
||||
mock.lockRun.Unlock()
|
||||
if mock.RunFunc == nil {
|
||||
var (
|
||||
errOut error
|
||||
)
|
||||
return errOut
|
||||
}
|
||||
return mock.RunFunc(s, strings...)
|
||||
}
|
||||
|
||||
// RunCalls gets all the calls that were made to Run.
|
||||
// Check the length with:
|
||||
//
|
||||
// len(mockedcmder.RunCalls())
|
||||
func (mock *cmderMock) RunCalls() []struct {
|
||||
S string
|
||||
Strings []string
|
||||
} {
|
||||
var calls []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}
|
||||
mock.lockRun.RLock()
|
||||
calls = mock.calls.Run
|
||||
mock.lockRun.RUnlock()
|
||||
return calls
|
||||
}
|
||||
93
internal/system/nvmodules/modules.go
Normal file
93
internal/system/nvmodules/modules.go
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
# 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 nvmodules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
||||
)
|
||||
|
||||
// Interface provides a set of utilities for interacting with NVIDIA modules on the system.
|
||||
type Interface struct {
|
||||
logger logger.Interface
|
||||
|
||||
dryRun bool
|
||||
root string
|
||||
|
||||
cmder
|
||||
}
|
||||
|
||||
// New constructs a new Interface struct with the specified options.
|
||||
func New(opts ...Option) *Interface {
|
||||
m := &Interface{}
|
||||
for _, opt := range opts {
|
||||
opt(m)
|
||||
}
|
||||
|
||||
if m.logger == nil {
|
||||
m.logger = logger.New()
|
||||
}
|
||||
if m.root == "" {
|
||||
m.root = "/"
|
||||
}
|
||||
|
||||
if m.dryRun {
|
||||
m.cmder = &cmderLogger{m.logger}
|
||||
} else {
|
||||
m.cmder = &cmderExec{}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// LoadAll loads all the NVIDIA kernel modules.
|
||||
func (m *Interface) LoadAll() error {
|
||||
modules := []string{"nvidia", "nvidia-uvm", "nvidia-modeset"}
|
||||
|
||||
for _, module := range modules {
|
||||
if err := m.Load(module); err != nil {
|
||||
return fmt.Errorf("failed to load module %s: %w", module, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var errInvalidModule = fmt.Errorf("invalid module")
|
||||
|
||||
// Load loads the specified NVIDIA kernel module.
|
||||
// If the root is specified we first chroot into this root.
|
||||
func (m *Interface) Load(module string) error {
|
||||
if !strings.HasPrefix(module, "nvidia") {
|
||||
return errInvalidModule
|
||||
}
|
||||
|
||||
var args []string
|
||||
if m.root != "/" {
|
||||
args = append(args, "chroot", m.root)
|
||||
}
|
||||
args = append(args, "/sbin/modprobe", module)
|
||||
|
||||
m.logger.Debugf("Loading kernel module %s: %v", module, args)
|
||||
err := m.Run(args[0], args[1:]...)
|
||||
if err != nil {
|
||||
m.logger.Debugf("Failed to load kernel module %s: %v", module, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
178
internal/system/nvmodules/modules_test.go
Normal file
178
internal/system/nvmodules/modules_test.go
Normal file
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
# 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 nvmodules
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
testlog "github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLoadAll(t *testing.T) {
|
||||
logger, _ := testlog.NewNullLogger()
|
||||
|
||||
runError := errors.New("run error")
|
||||
|
||||
testCases := []struct {
|
||||
description string
|
||||
root string
|
||||
runError error
|
||||
expectedError error
|
||||
expectedCalls []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}
|
||||
}{
|
||||
{
|
||||
description: "no root specified",
|
||||
root: "",
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}{
|
||||
{"/sbin/modprobe", []string{"nvidia"}},
|
||||
{"/sbin/modprobe", []string{"nvidia-uvm"}},
|
||||
{"/sbin/modprobe", []string{"nvidia-modeset"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "root causes chroot",
|
||||
root: "/some/root",
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}{
|
||||
{"chroot", []string{"/some/root", "/sbin/modprobe", "nvidia"}},
|
||||
{"chroot", []string{"/some/root", "/sbin/modprobe", "nvidia-uvm"}},
|
||||
{"chroot", []string{"/some/root", "/sbin/modprobe", "nvidia-modeset"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "run failure is returned",
|
||||
root: "",
|
||||
runError: runError,
|
||||
expectedError: runError,
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}{
|
||||
{"/sbin/modprobe", []string{"nvidia"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
cmder := &cmderMock{
|
||||
RunFunc: func(cmd string, args ...string) error {
|
||||
return tc.runError
|
||||
},
|
||||
}
|
||||
m := New(
|
||||
WithLogger(logger),
|
||||
WithRoot(tc.root),
|
||||
)
|
||||
m.cmder = cmder
|
||||
|
||||
err := m.LoadAll()
|
||||
require.ErrorIs(t, err, tc.expectedError)
|
||||
|
||||
require.EqualValues(t, tc.expectedCalls, cmder.RunCalls())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
logger, _ := testlog.NewNullLogger()
|
||||
|
||||
runError := errors.New("run error")
|
||||
|
||||
testCases := []struct {
|
||||
description string
|
||||
root string
|
||||
module string
|
||||
runError error
|
||||
expectedError error
|
||||
expectedCalls []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}
|
||||
}{
|
||||
{
|
||||
description: "no root specified",
|
||||
root: "",
|
||||
module: "nvidia",
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}{
|
||||
{"/sbin/modprobe", []string{"nvidia"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "root causes chroot",
|
||||
root: "/some/root",
|
||||
module: "nvidia",
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}{
|
||||
{"chroot", []string{"/some/root", "/sbin/modprobe", "nvidia"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "run failure is returned",
|
||||
root: "",
|
||||
module: "nvidia",
|
||||
runError: runError,
|
||||
expectedError: runError,
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
Strings []string
|
||||
}{
|
||||
{"/sbin/modprobe", []string{"nvidia"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "module prefis is checked",
|
||||
module: "not-nvidia",
|
||||
expectedError: errInvalidModule,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
cmder := &cmderMock{
|
||||
RunFunc: func(cmd string, args ...string) error {
|
||||
return tc.runError
|
||||
},
|
||||
}
|
||||
m := New(
|
||||
WithLogger(logger),
|
||||
WithRoot(tc.root),
|
||||
)
|
||||
m.cmder = cmder
|
||||
|
||||
err := m.Load(tc.module)
|
||||
require.ErrorIs(t, err, tc.expectedError)
|
||||
|
||||
require.EqualValues(t, tc.expectedCalls, cmder.RunCalls())
|
||||
})
|
||||
}
|
||||
}
|
||||
43
internal/system/nvmodules/options.go
Normal file
43
internal/system/nvmodules/options.go
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
# 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 nvmodules
|
||||
|
||||
import "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
||||
|
||||
// Option is a function that sets an option on the Interface struct.
|
||||
type Option func(*Interface)
|
||||
|
||||
// WithDryRun sets the dry run option for the Interface struct.
|
||||
func WithDryRun(dryRun bool) Option {
|
||||
return func(i *Interface) {
|
||||
i.dryRun = dryRun
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogger sets the logger for the Interface struct.
|
||||
func WithLogger(logger logger.Interface) Option {
|
||||
return func(i *Interface) {
|
||||
i.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
// WithRoot sets the root directory for the NVIDIA device nodes.
|
||||
func WithRoot(root string) Option {
|
||||
return func(i *Interface) {
|
||||
i.root = root
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user