Skip to content

Commit

Permalink
fsmonitor: Test unix devices with both drivers (canonical#14111)
Browse files Browse the repository at this point in the history
The `fsmonitor` package has two drivers, but only fanotify is tested for
unix devices (see
canonical#14110 (comment)).
This is the default driver but it may not be available in all
kernels/architectures.

This PR adds an environment variable to force a specific driver to be
used. The unix char and block device tests are then run with both
drivers.

The `inotify` package was encountering some unknown events. As it turned
out, mapping these events to `fsmonitor` events broke the test suite, so
I've added exceptions for them to reduce log verbosity.
  • Loading branch information
tomponline committed Sep 16, 2024
2 parents cc00bf4 + bf567a5 commit 217bacb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
11 changes: 10 additions & 1 deletion lxd/fsmonitor/drivers/driver_inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ 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 {
return event, nil
}
}

if mask&in.InIgnored != 0 || mask&(in.InUnmount|in.InIsdir) != 0 {
return -1, errIgnoreEvent
}

return -1, fmt.Errorf(`Unknown inotify event "%d"`, mask)
}

Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions lxd/fsmonitor/drivers/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"

"github.com/canonical/lxd/lxd/fsmonitor"
"github.com/canonical/lxd/shared/logger"
Expand Down Expand Up @@ -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})
Expand Down
28 changes: 28 additions & 0 deletions test/suites/container_devices_unix.sh
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit 217bacb

Please sign in to comment.