Skip to content

Commit d757f6e

Browse files
committed
[no-relnote] Move control device nodes to separate file
Signed-off-by: Evan Lezar <[email protected]>
1 parent 5d5166c commit d757f6e

File tree

2 files changed

+99
-69
lines changed

2 files changed

+99
-69
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
**/
16+
17+
package nvdevices
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
"strings"
23+
24+
"github.com/NVIDIA/nvidia-container-toolkit/internal/info/proc/devices"
25+
)
26+
27+
// A controlDeviceNode represents an NVIDIA devices node for control or meta devices.
28+
// Such device nodes are typically required regardless of which GPU is being accessed.
29+
type controlDeviceNode string
30+
31+
func (c controlDeviceNode) path() string {
32+
return filepath.Join("dev", string(c))
33+
}
34+
35+
// CreateNVIDIAControlDevices creates the NVIDIA control device nodes at the configured devRoot.
36+
func (m *Interface) CreateNVIDIAControlDevices() error {
37+
controlNodes := []controlDeviceNode{"nvidiactl", "nvidia-modeset", "nvidia-uvm", "nvidia-uvm-tools"}
38+
for _, node := range controlNodes {
39+
if err := m.createControlDeviceNode(node); err != nil {
40+
return fmt.Errorf("failed to create device node %s: %w", node, err)
41+
}
42+
}
43+
return nil
44+
}
45+
46+
// createControlDeviceNode creates the specified NVIDIA device node at the configured devRoot.
47+
func (m *Interface) createControlDeviceNode(node controlDeviceNode) error {
48+
if !strings.HasPrefix(string(node), "nvidia") {
49+
return fmt.Errorf("invalid device node %q: %w", node, errInvalidDeviceNode)
50+
}
51+
52+
major, err := m.controlDeviceNodeMajor(node)
53+
if err != nil {
54+
return fmt.Errorf("failed to determine major: %w", err)
55+
}
56+
57+
minor, err := m.controlDeviceNodeMinor(node)
58+
if err != nil {
59+
return fmt.Errorf("failed to determine minor: %w", err)
60+
}
61+
62+
return m.createDeviceNode(node.path(), int(major), int(minor))
63+
}
64+
65+
// controlDeviceNodeMajor returns the major number for the specified NVIDIA control device node.
66+
// If the device node is not supported, an error is returned.
67+
func (m *Interface) controlDeviceNodeMajor(node controlDeviceNode) (int64, error) {
68+
var valid bool
69+
var major devices.Major
70+
switch node {
71+
case "nvidia-uvm", "nvidia-uvm-tools":
72+
major, valid = m.Get(devices.NVIDIAUVM)
73+
case "nvidia-modeset", "nvidiactl":
74+
major, valid = m.Get(devices.NVIDIAGPU)
75+
}
76+
77+
if valid {
78+
return int64(major), nil
79+
}
80+
81+
return 0, errInvalidDeviceNode
82+
}
83+
84+
// controlDeviceNodeMinor returns the minor number for the specified NVIDIA control device node.
85+
// If the device node is not supported, an error is returned.
86+
func (m *Interface) controlDeviceNodeMinor(node controlDeviceNode) (int64, error) {
87+
switch node {
88+
case "nvidia-modeset":
89+
return devices.NVIDIAModesetMinor, nil
90+
case "nvidia-uvm-tools":
91+
return devices.NVIDIAUVMToolsMinor, nil
92+
case "nvidia-uvm":
93+
return devices.NVIDIAUVMMinor, nil
94+
case "nvidiactl":
95+
return devices.NVIDIACTLMinor, nil
96+
}
97+
98+
return 0, errInvalidDeviceNode
99+
}

internal/system/nvdevices/devices.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"path/filepath"
23-
"strings"
2423

2524
"github.com/NVIDIA/nvidia-container-toolkit/internal/info/proc/devices"
2625
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
@@ -70,77 +69,9 @@ func New(opts ...Option) (*Interface, error) {
7069
return i, nil
7170
}
7271

73-
// CreateNVIDIAControlDevices creates the NVIDIA control device nodes at the configured devRoot.
74-
func (m *Interface) CreateNVIDIAControlDevices() error {
75-
controlNodes := []string{"nvidiactl", "nvidia-modeset", "nvidia-uvm", "nvidia-uvm-tools"}
76-
for _, node := range controlNodes {
77-
err := m.CreateNVIDIADevice(node)
78-
if err != nil {
79-
return fmt.Errorf("failed to create device node %s: %w", node, err)
80-
}
81-
}
82-
return nil
83-
}
84-
85-
// CreateNVIDIADevice creates the specified NVIDIA device node at the configured devRoot.
86-
func (m *Interface) CreateNVIDIADevice(node string) error {
87-
node = filepath.Base(node)
88-
if !strings.HasPrefix(node, "nvidia") {
89-
return fmt.Errorf("invalid device node %q: %w", node, errInvalidDeviceNode)
90-
}
91-
92-
major, err := m.Major(node)
93-
if err != nil {
94-
return fmt.Errorf("failed to determine major: %w", err)
95-
}
96-
97-
minor, err := m.Minor(node)
98-
if err != nil {
99-
return fmt.Errorf("failed to determine minor: %w", err)
100-
}
101-
102-
return m.createDeviceNode(filepath.Join("dev", node), int(major), int(minor))
103-
}
104-
10572
// createDeviceNode creates the specified device node with the require major and minor numbers.
10673
// If a devRoot is configured, this is prepended to the path.
10774
func (m *Interface) createDeviceNode(path string, major int, minor int) error {
10875
path = filepath.Join(m.devRoot, path)
10976
return m.Mknode(path, major, minor)
11077
}
111-
112-
// Major returns the major number for the specified NVIDIA device node.
113-
// If the device node is not supported, an error is returned.
114-
func (m *Interface) Major(node string) (int64, error) {
115-
var valid bool
116-
var major devices.Major
117-
switch node {
118-
case "nvidia-uvm", "nvidia-uvm-tools":
119-
major, valid = m.Get(devices.NVIDIAUVM)
120-
case "nvidia-modeset", "nvidiactl":
121-
major, valid = m.Get(devices.NVIDIAGPU)
122-
}
123-
124-
if valid {
125-
return int64(major), nil
126-
}
127-
128-
return 0, errInvalidDeviceNode
129-
}
130-
131-
// Minor returns the minor number for the specified NVIDIA device node.
132-
// If the device node is not supported, an error is returned.
133-
func (m *Interface) Minor(node string) (int64, error) {
134-
switch node {
135-
case "nvidia-modeset":
136-
return devices.NVIDIAModesetMinor, nil
137-
case "nvidia-uvm-tools":
138-
return devices.NVIDIAUVMToolsMinor, nil
139-
case "nvidia-uvm":
140-
return devices.NVIDIAUVMMinor, nil
141-
case "nvidiactl":
142-
return devices.NVIDIACTLMinor, nil
143-
}
144-
145-
return 0, errInvalidDeviceNode
146-
}

0 commit comments

Comments
 (0)