diff --git a/doc/environment.md b/doc/environment.md index 31a21bcb2799..4d5d57c80c31 100644 --- a/doc/environment.md +++ b/doc/environment.md @@ -40,3 +40,4 @@ Name | Description `LXD_QEMU_FW_PATH` | Path (or `:` separated list of paths) to firmware (OVMF, SeaBIOS) to be used by QEMU `LXD_IDMAPPED_MOUNTS_DISABLE` | Disable idmapped mounts support (useful when testing traditional UID shifting) `LXD_DEVMONITOR_DIR` | Path to be monitored by the device monitor. This is primarily for testing. +`LXD_FSMONITOR_DRIVER` | Driver to be used for file system monitoring. This is primarily for testing. diff --git a/lxd/fsmonitor/drivers/driver_inotify.go b/lxd/fsmonitor/drivers/driver_inotify.go index 59553b4686e5..6df7b47bff14 100644 --- a/lxd/fsmonitor/drivers/driver_inotify.go +++ b/lxd/fsmonitor/drivers/driver_inotify.go @@ -36,6 +36,8 @@ var fsMonitorEventToINotifyEvent = map[fsmonitor.Event]uint32{ fsmonitor.EventRename: in.InMovedTo, } +var errIgnoreEvent = errors.New("Intentionally ignored event") + func (d *inotify) toFSMonitorEvent(mask uint32) (fsmonitor.Event, error) { for knownINotifyEvent, event := range inotifyEventToFSMonitorEvent { if mask&knownINotifyEvent != 0 { @@ -43,6 +45,10 @@ func (d *inotify) toFSMonitorEvent(mask uint32) (fsmonitor.Event, error) { } } + if mask&in.InIgnored != 0 || mask&(in.InUnmount|in.InIsdir) != 0 { + return -1, errIgnoreEvent + } + return -1, fmt.Errorf(`Unknown inotify event "%d"`, mask) } @@ -102,7 +108,10 @@ func (d *inotify) getEvents(ctx context.Context) { event.Name = filepath.Clean(event.Name) action, err := d.toFSMonitorEvent(event.Mask) if err != nil { - logger.Warn("Failed to match inotify event, skipping", logger.Ctx{"err": err}) + if !errors.Is(err, errIgnoreEvent) { + logger.Warn("Failed to match inotify event, skipping", logger.Ctx{"err": err}) + } + continue } diff --git a/lxd/fsmonitor/drivers/load.go b/lxd/fsmonitor/drivers/load.go index 7df77566a25d..143ffd60715e 100644 --- a/lxd/fsmonitor/drivers/load.go +++ b/lxd/fsmonitor/drivers/load.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os" "github.com/canonical/lxd/lxd/fsmonitor" "github.com/canonical/lxd/shared/logger" @@ -40,6 +41,11 @@ func Load(ctx context.Context, path string, events ...fsmonitor.Event) (fsmonito return d, nil } + driverName := os.Getenv("LXD_FSMONITOR_DRIVER") + if driverName != "" { + return startMonitor(driverName) + } + driver, err := startMonitor("fanotify") if err != nil { logger.Warn("Failed to initialize fanotify, falling back on inotify", logger.Ctx{"err": err}) diff --git a/test/suites/container_devices_unix.sh b/test/suites/container_devices_unix.sh index 33259be06253..ad2636fb0189 100644 --- a/test/suites/container_devices_unix.sh +++ b/test/suites/container_devices_unix.sh @@ -1,9 +1,37 @@ test_container_devices_unix_block() { + lxdFSMonitorDriver=${LXD_FSMONITOR_DRIVER:-} + + shutdown_lxd "${LXD_DIR}" + export LXD_FSMONITOR_DRIVER="fanotify" + respawn_lxd "${LXD_DIR}" true + _container_devices_unix "unix-block" + + shutdown_lxd "${LXD_DIR}" + export LXD_FSMONITOR_DRIVER="inotify" + respawn_lxd "${LXD_DIR}" true _container_devices_unix "unix-block" + + shutdown_lxd "${LXD_DIR}" + export LXD_FSMONITOR_DRIVER="${lxdFSMonitorDriver}" + respawn_lxd "${LXD_DIR}" true } test_container_devices_unix_char() { + lxdFSMonitorDriver=${LXD_FSMONITOR_DRIVER:-} + + shutdown_lxd "${LXD_DIR}" + export LXD_FSMONITOR_DRIVER="fanotify" + respawn_lxd "${LXD_DIR}" true + _container_devices_unix "unix-char" + + shutdown_lxd "${LXD_DIR}" + export LXD_FSMONITOR_DRIVER="inotify" + respawn_lxd "${LXD_DIR}" true _container_devices_unix "unix-char" + + shutdown_lxd "${LXD_DIR}" + export LXD_FSMONITOR_DRIVER="${lxdFSMonitorDriver}" + respawn_lxd "${LXD_DIR}" true } _container_devices_unix() {