Search code examples
clinuxkernelinotify

How could I detect when a directory is mounted with inotify?


I'm using Linux Inotify to detect FS events on my program.

How could I be notified when a device is mounted on a monitored directory?


Solution

  • I don't think you can do it with inotify. Here is the method though:

    1. Read uevents from kernel via a Netlink socket and filter out those where "ACTION" is not "mount".
    2. Read and parse "/proc/mounts" when you get an event with a "mount" action.
    3. Find a record for a mount point with device that was just mounted and filter it out if it's not the directory you are watching.