Skip to content

Commit

Permalink
Remove watch option from create-dev-char-symlinks
Browse files Browse the repository at this point in the history
This also removes the direct dependency on fsnotify.

Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Jan 14, 2025
1 parent c935779 commit 64ad1c5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ package devchar
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"

"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
Expand All @@ -44,7 +40,6 @@ type config struct {
devCharPath string
driverRoot string
dryRun bool
watch bool
createAll bool
createDeviceNodes bool
loadKernelModules bool
Expand Down Expand Up @@ -89,13 +84,6 @@ func (m command) build() *cli.Command {
Destination: &cfg.driverRoot,
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{
Name: "create-all",
Usage: "Create all possible /dev/char symlinks instead of limiting these to existing device nodes.",
Expand Down Expand Up @@ -127,7 +115,7 @@ func (m command) build() *cli.Command {
}

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")
}

Expand All @@ -145,19 +133,6 @@ func (m command) validateFlags(r *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(
WithLogger(m.logger),
WithDevCharPath(cfg.devCharPath),
Expand All @@ -171,47 +146,11 @@ func (m command) run(c *cli.Context, cfg *config) error {
return fmt.Errorf("failed to create symlink creator: %v", err)
}

create:
err = l.CreateLinks()
if err != nil {
return fmt.Errorf("failed to create links: %v", err)
}
if !cfg.watch {
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
}
}
}
return nil
}

type linkCreator struct {
Expand Down Expand Up @@ -399,27 +338,3 @@ type deviceNode struct {
func (d deviceNode) devCharName() string {
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 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.20
require (
github.com/NVIDIA/go-nvlib v0.6.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/opencontainers/runtime-spec v1.2.0
github.com/pelletier/go-toml v1.9.5
Expand All @@ -21,6 +20,7 @@ require (
require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // 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/hashicorp/errwrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down

0 comments on commit 64ad1c5

Please sign in to comment.