Remove watch option from create-dev-char-symlinks

This removes the untested watch option from the
nvidia-ctk system create-dev-char-symlinks command.

This also removes the direct dependency on fsnotify.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2025-01-14 18:31:51 +01:00
parent c935779693
commit 8e6b57b38a
No known key found for this signature in database
2 changed files with 3 additions and 88 deletions

View File

@ -19,12 +19,8 @@ package devchar
import ( import (
"fmt" "fmt"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"strings"
"syscall"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
@ -44,7 +40,6 @@ type config struct {
devCharPath string devCharPath string
driverRoot string driverRoot string
dryRun bool dryRun bool
watch bool
createAll bool createAll bool
createDeviceNodes bool createDeviceNodes bool
loadKernelModules bool loadKernelModules bool
@ -89,13 +84,6 @@ func (m command) build() *cli.Command {
Destination: &cfg.driverRoot, Destination: &cfg.driverRoot,
EnvVars: []string{"NVIDIA_DRIVER_ROOT", "DRIVER_ROOT"}, EnvVars: []string{"NVIDIA_DRIVER_ROOT", "DRIVER_ROOT"},
}, },
&cli.BoolFlag{
Name: "watch",
Usage: "If set, the command will watch for changes to the driver root and recreate the symlinks when changes are detected.",
Value: false,
Destination: &cfg.watch,
EnvVars: []string{"WATCH"},
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "create-all", Name: "create-all",
Usage: "Create all possible /dev/char symlinks instead of limiting these to existing device nodes.", Usage: "Create all possible /dev/char symlinks instead of limiting these to existing device nodes.",
@ -127,7 +115,7 @@ func (m command) build() *cli.Command {
} }
func (m command) validateFlags(r *cli.Context, cfg *config) error { func (m command) validateFlags(r *cli.Context, cfg *config) error {
if cfg.createAll && cfg.watch { if cfg.createAll {
return fmt.Errorf("create-all and watch are mutually exclusive") return fmt.Errorf("create-all and watch are mutually exclusive")
} }
@ -145,19 +133,6 @@ func (m command) validateFlags(r *cli.Context, cfg *config) error {
} }
func (m command) run(c *cli.Context, cfg *config) error { func (m command) run(c *cli.Context, cfg *config) error {
var watcher *fsnotify.Watcher
var sigs chan os.Signal
if cfg.watch {
watcher, err := newFSWatcher(filepath.Join(cfg.driverRoot, "dev"))
if err != nil {
return fmt.Errorf("failed to create FS watcher: %v", err)
}
defer watcher.Close()
sigs = newOSWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
}
l, err := NewSymlinkCreator( l, err := NewSymlinkCreator(
WithLogger(m.logger), WithLogger(m.logger),
WithDevCharPath(cfg.devCharPath), WithDevCharPath(cfg.devCharPath),
@ -171,47 +146,11 @@ func (m command) run(c *cli.Context, cfg *config) error {
return fmt.Errorf("failed to create symlink creator: %v", err) return fmt.Errorf("failed to create symlink creator: %v", err)
} }
create:
err = l.CreateLinks() err = l.CreateLinks()
if err != nil { if err != nil {
return fmt.Errorf("failed to create links: %v", err) return fmt.Errorf("failed to create links: %v", err)
} }
if !cfg.watch { return nil
return nil
}
for {
select {
case event := <-watcher.Events:
deviceNode := filepath.Base(event.Name)
if !strings.HasPrefix(deviceNode, "nvidia") {
continue
}
if event.Has(fsnotify.Create) {
m.logger.Infof("%s created, restarting.", event.Name)
goto create
}
if event.Has(fsnotify.Remove) {
m.logger.Infof("%s removed. Ignoring", event.Name)
}
// Watch for any other fs errors and log them.
case err := <-watcher.Errors:
m.logger.Errorf("inotify: %s", err)
// React to signals
case s := <-sigs:
switch s {
case syscall.SIGHUP:
m.logger.Infof("Received SIGHUP, recreating symlinks.")
goto create
default:
m.logger.Infof("Received signal %q, shutting down.", s)
return nil
}
}
}
} }
type linkCreator struct { type linkCreator struct {
@ -399,27 +338,3 @@ type deviceNode struct {
func (d deviceNode) devCharName() string { func (d deviceNode) devCharName() string {
return fmt.Sprintf("%d:%d", d.major, d.minor) return fmt.Sprintf("%d:%d", d.major, d.minor)
} }
func newFSWatcher(files ...string) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
for _, f := range files {
err = watcher.Add(f)
if err != nil {
watcher.Close()
return nil, err
}
}
return watcher, nil
}
func newOSWatcher(sigs ...os.Signal) chan os.Signal {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, sigs...)
return sigChan
}

2
go.mod
View File

@ -5,7 +5,6 @@ go 1.20
require ( require (
github.com/NVIDIA/go-nvlib v0.6.1 github.com/NVIDIA/go-nvlib v0.6.1
github.com/NVIDIA/go-nvml v0.12.4-1 github.com/NVIDIA/go-nvml v0.12.4-1
github.com/fsnotify/fsnotify v1.7.0
github.com/moby/sys/symlink v0.3.0 github.com/moby/sys/symlink v0.3.0
github.com/opencontainers/runtime-spec v1.2.0 github.com/opencontainers/runtime-spec v1.2.0
github.com/pelletier/go-toml v1.9.5 github.com/pelletier/go-toml v1.9.5
@ -21,6 +20,7 @@ require (
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect github.com/kr/pretty v0.3.1 // indirect