Skip to content

Commit 2e90474

Browse files
committed
Add CreateDevCharSymlinks to nvdevices package
This change adds a CreateDevCharSymlinks function to the API of the nvdevices packages. The implementation in nvidia-ctk system create-dev-char-symlinks is updated to use this API. This allows the GPU Operator to move to this API instead of importing the cmd/system/create-dev-char-symlinks package. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent f15296b commit 2e90474

File tree

7 files changed

+112
-226
lines changed

7 files changed

+112
-226
lines changed

cmd/nvidia-ctk/system/create-dev-char-symlinks/create-dev-char-symlinks.go

Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package devchar
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
23-
"path/filepath"
2422

2523
"github.com/urfave/cli/v3"
2624

@@ -151,14 +149,15 @@ func (m command) run(cfg *config) error {
151149

152150
type linkCreator struct {
153151
logger logger.Interface
154-
lister nodeLister
155152
driverRoot string
156153
devRoot string
157154
devCharPath string
158155
dryRun bool
159156
createAll bool
160157
createDeviceNodes bool
161158
loadKernelModules bool
159+
160+
devicesLib *nvdevices.Interface
162161
}
163162

164163
// Creator is an interface for creating symlinks to /dev/nv* devices in /dev/char.
@@ -170,6 +169,8 @@ type Creator interface {
170169
type Option func(*linkCreator)
171170

172171
// NewSymlinkCreator creates a new linkCreator.
172+
//
173+
// Deprecated: Use the `nvdevices` package instead.
173174
func NewSymlinkCreator(opts ...Option) (Creator, error) {
174175
c := linkCreator{}
175176
for _, opt := range opts {
@@ -188,52 +189,34 @@ func NewSymlinkCreator(opts ...Option) (Creator, error) {
188189
c.devCharPath = defaultDevCharPath
189190
}
190191

191-
if err := c.setup(); err != nil {
192-
return nil, err
193-
}
194-
195-
if c.createAll {
196-
lister, err := newAllPossible(c.logger, c.devRoot)
197-
if err != nil {
198-
return nil, fmt.Errorf("failed to create all possible device lister: %v", err)
199-
}
200-
c.lister = lister
201-
} else {
202-
c.lister = existing{c.logger, c.devRoot}
203-
}
204-
return c, nil
205-
}
206-
207-
func (m linkCreator) setup() error {
208-
if !m.loadKernelModules && !m.createDeviceNodes {
209-
return nil
210-
}
211-
212-
if m.loadKernelModules {
192+
if c.loadKernelModules {
213193
modules := nvmodules.New(
214-
nvmodules.WithLogger(m.logger),
215-
nvmodules.WithDryRun(m.dryRun),
216-
nvmodules.WithRoot(m.driverRoot),
194+
nvmodules.WithLogger(c.logger),
195+
nvmodules.WithDryRun(c.dryRun),
196+
nvmodules.WithRoot(c.driverRoot),
217197
)
218198
if err := modules.LoadAll(); err != nil {
219-
return fmt.Errorf("failed to load NVIDIA kernel modules: %v", err)
199+
return nil, fmt.Errorf("failed to load NVIDIA kernel modules: %v", err)
220200
}
221201
}
222202

223-
if m.createDeviceNodes {
224-
devices, err := nvdevices.New(
225-
nvdevices.WithLogger(m.logger),
226-
nvdevices.WithDryRun(m.dryRun),
227-
nvdevices.WithDevRoot(m.devRoot),
228-
)
229-
if err != nil {
230-
return err
231-
}
203+
devices, err := nvdevices.New(
204+
nvdevices.WithLogger(c.logger),
205+
nvdevices.WithDryRun(c.dryRun),
206+
nvdevices.WithDevRoot(c.driverRoot),
207+
)
208+
if err != nil {
209+
return nil, err
210+
}
211+
c.devicesLib = devices
212+
213+
if c.createDeviceNodes {
232214
if err := devices.CreateNVIDIAControlDevices(); err != nil {
233-
return fmt.Errorf("failed to create NVIDIA device nodes: %v", err)
215+
return nil, fmt.Errorf("failed to create NVIDIA device nodes: %v", err)
234216
}
235217
}
236-
return nil
218+
219+
return c, nil
237220
}
238221

239222
// WithDriverRoot sets the driver root path.
@@ -295,42 +278,5 @@ func WithCreateDeviceNodes(createDeviceNodes bool) Option {
295278

296279
// CreateLinks creates symlinks for all NVIDIA device nodes found in the driver root.
297280
func (m linkCreator) CreateLinks() error {
298-
deviceNodes, err := m.lister.DeviceNodes()
299-
if err != nil {
300-
return fmt.Errorf("failed to get device nodes: %v", err)
301-
}
302-
303-
if len(deviceNodes) != 0 && !m.dryRun {
304-
err := os.MkdirAll(m.devCharPath, 0755)
305-
if err != nil {
306-
return fmt.Errorf("failed to create directory %s: %v", m.devCharPath, err)
307-
}
308-
}
309-
310-
for _, deviceNode := range deviceNodes {
311-
target := deviceNode.path
312-
linkPath := filepath.Join(m.devCharPath, deviceNode.devCharName())
313-
314-
m.logger.Infof("Creating link %s => %s", linkPath, target)
315-
if m.dryRun {
316-
continue
317-
}
318-
319-
err = os.Symlink(target, linkPath)
320-
if err != nil {
321-
m.logger.Warningf("Could not create symlink: %v", err)
322-
}
323-
}
324-
325-
return nil
326-
}
327-
328-
type deviceNode struct {
329-
path string
330-
major uint32
331-
minor uint32
332-
}
333-
334-
func (d deviceNode) devCharName() string {
335-
return fmt.Sprintf("%d:%d", d.major, d.minor)
281+
return m.devicesLib.CreateDevCharSymlinks(m.devCharPath, !m.createAll)
336282
}

cmd/nvidia-ctk/system/create-dev-char-symlinks/existing.go

Lines changed: 0 additions & 89 deletions
This file was deleted.

cmd/nvidia-ctk/system/create-dev-char-symlinks/existing_linux.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

cmd/nvidia-ctk/system/create-dev-char-symlinks/existing_other.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

internal/lookup/locator.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@ type Locator interface {
2727

2828
// ErrNotFound indicates that a specified pattern or file could not be found.
2929
var ErrNotFound = errors.New("not found")
30+
31+
type always string
32+
33+
const Always = always("always")
34+
35+
var _ Locator = (*always)(nil)
36+
37+
func (l always) Locate(s string) ([]string, error) {
38+
return []string{s}, nil
39+
}

cmd/nvidia-ctk/system/create-dev-char-symlinks/all.go renamed to pkg/system/nvdevices/all.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
**/
1616

17-
package devchar
17+
package nvdevices
1818

1919
import (
2020
"fmt"
@@ -34,6 +34,20 @@ type allPossible struct {
3434
migCaps nvcaps.MigCaps
3535
}
3636

37+
type nodeLister interface {
38+
DeviceNodes() ([]deviceNode, error)
39+
}
40+
41+
type deviceNode struct {
42+
path string
43+
major uint32
44+
minor uint32
45+
}
46+
47+
func (d deviceNode) devCharName() string {
48+
return fmt.Sprintf("%d:%d", d.major, d.minor)
49+
}
50+
3751
// newAllPossible returns a new allPossible device node lister.
3852
// This lister lists all possible device nodes for NVIDIA GPUs, control devices, and capability devices.
3953
func newAllPossible(logger logger.Interface, devRoot string) (nodeLister, error) {

0 commit comments

Comments
 (0)