Skip to content

Commit e71e6dc

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 7ea642e commit e71e6dc

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

@@ -155,14 +153,15 @@ func (m command) run(cfg *config) error {
155153

156154
type linkCreator struct {
157155
logger logger.Interface
158-
lister nodeLister
159156
driverRoot string
160157
devRoot string
161158
devCharPath string
162159
dryRun bool
163160
createAll bool
164161
createDeviceNodes bool
165162
loadKernelModules bool
163+
164+
devicesLib *nvdevices.Interface
166165
}
167166

168167
// Creator is an interface for creating symlinks to /dev/nv* devices in /dev/char.
@@ -174,6 +173,8 @@ type Creator interface {
174173
type Option func(*linkCreator)
175174

176175
// NewSymlinkCreator creates a new linkCreator.
176+
//
177+
// Deprecated: Use the `nvdevices` package instead.
177178
func NewSymlinkCreator(opts ...Option) (Creator, error) {
178179
c := linkCreator{}
179180
for _, opt := range opts {
@@ -192,52 +193,34 @@ func NewSymlinkCreator(opts ...Option) (Creator, error) {
192193
c.devCharPath = defaultDevCharPath
193194
}
194195

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

227-
if m.createDeviceNodes {
228-
devices, err := nvdevices.New(
229-
nvdevices.WithLogger(m.logger),
230-
nvdevices.WithDryRun(m.dryRun),
231-
nvdevices.WithDevRoot(m.devRoot),
232-
)
233-
if err != nil {
234-
return err
235-
}
207+
devices, err := nvdevices.New(
208+
nvdevices.WithLogger(c.logger),
209+
nvdevices.WithDryRun(c.dryRun),
210+
nvdevices.WithDevRoot(c.driverRoot),
211+
)
212+
if err != nil {
213+
return nil, err
214+
}
215+
c.devicesLib = devices
216+
217+
if c.createDeviceNodes {
236218
if err := devices.CreateNVIDIAControlDevices(); err != nil {
237-
return fmt.Errorf("failed to create NVIDIA device nodes: %v", err)
219+
return nil, fmt.Errorf("failed to create NVIDIA device nodes: %v", err)
238220
}
239221
}
240-
return nil
222+
223+
return c, nil
241224
}
242225

243226
// WithDriverRoot sets the driver root path.
@@ -299,42 +282,5 @@ func WithCreateDeviceNodes(createDeviceNodes bool) Option {
299282

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

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)