Skip to content

Commit

Permalink
Remove fanotify warning on ubuntu pro fsmonitor initialisation (#14156)
Browse files Browse the repository at this point in the history
This change allows the caller to specify a driver when loading an
fsmonitor, or they can specify "any" to try to load fanotify but
fallback to inotify if it fails.
  • Loading branch information
tomponline committed Sep 26, 2024
2 parents d16275f + 6b9b18b commit 2aad04d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lxd/fsmonitor/drivers/driver_fanotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (d *fanotify) eventMask() (uint64, error) {

// DriverName returns the name of the driver.
func (d *fanotify) DriverName() string {
return "fanotify"
return fsmonitor.DriverNameFANotify
}

func (d *fanotify) load(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion lxd/fsmonitor/drivers/driver_inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (d *inotify) eventMask() (uint32, error) {

// DriverName returns the name of the driver.
func (d *inotify) DriverName() string {
return "inotify"
return fsmonitor.DriverNameINotify
}

func (d *inotify) load(ctx context.Context) error {
Expand Down
22 changes: 16 additions & 6 deletions lxd/fsmonitor/drivers/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"os"

"github.com/canonical/lxd/lxd/fsmonitor"
"github.com/canonical/lxd/lxd/storage/filesystem"
"github.com/canonical/lxd/shared/logger"
)

var drivers = map[string]func() driver{
"inotify": func() driver { return &inotify{} },
"fanotify": func() driver { return &fanotify{} },
fsmonitor.DriverNameINotify: func() driver { return &inotify{} },
fsmonitor.DriverNameFANotify: func() driver { return &fanotify{} },
}

// Load returns a new fsmonitor.FSMonitor with an applicable Driver.
Expand Down Expand Up @@ -46,10 +47,19 @@ func Load(ctx context.Context, path string, events ...fsmonitor.Event) (fsmonito
return startMonitor(driverName)
}

driver, err := startMonitor("fanotify")
if err != nil {
logger.Warn("Failed to initialize fanotify, falling back on inotify", logger.Ctx{"err": err})
driver, err = startMonitor("inotify")
var driver fsmonitor.FSMonitor
var err error
if filesystem.IsMountPoint(path) {
// If the file system is a mount point, try to use fanotify but fall back to inotify.
driver, err = startMonitor(fsmonitor.DriverNameFANotify)
if err != nil {
logger.Warn("Failed to initialize fanotify, falling back on inotify", logger.Ctx{"err": err, "path": path})
}
}

// If the file system is not a mount point or if setting up fanotify fails for another reason, use inotify.
if driver == nil {
driver, err = startMonitor(fsmonitor.DriverNameINotify)
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions lxd/fsmonitor/fsmonitor_interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package fsmonitor

const (
// DriverNameFANotify is the name of the FANotify driver.
//
// FANotify should be preferred over INotify because it is more performant and does not need to recursively watch
// subdirectories. However, it is not possible to use fanotify if the specified path is not a mountpoint because we
// need to use the unix.FAN_MARK_FILESYSTEM flag for this functionality.
DriverNameFANotify = "fanotify"

// DriverNameINotify is the name of the inotify driver.
DriverNameINotify = "inotify"
)

// FSMonitor represents a filesystem monitor.
type FSMonitor interface {
DriverName() string
Expand Down

0 comments on commit 2aad04d

Please sign in to comment.