Shell – inotifywait not alterting when device created

inotifylinuxshell

This works perfectly:

$ inotifywait --event create ~/foo
Setting up watches.
Watches established.
/home/ron/foo/ CREATE bar

However, this just sits there when directory tun0 is created under /sys/devices/virtual/net.

$ inotifywait --event create /sys/devices/virtual/net
Setting up watches.
Watches established.

Since those folders are world readable, I'd expect inotifywait to work.

So, what am I doing wrong?

Thanks

Best Answer

Although the inotify FAQ implies partial support:

Q: Can I watch sysfs (procfs, nfs...)?

Simply spoken: yes, but with some limitations. These limitations vary between kernel versions and tend to get smaller. Please read information about particular filesystems.

it does not actually say what might be supported (or in which kernel version, since that's mostly down to the inotify support in the filesystem itself rather than the library/utilities).

A simple explanation is that is doesn't really make sense to support inotify for everything in in /sys (or /proc) since they don't get modified in the conventional sense. Most of these files/directories represent a snapshot of kernel state at the time you view them.

Think of /proc/uptime as a simple example, it contains the uptime accurate to the centisecond. Should inotify notify you 100 times a second that it was "written" to? Apart from not being very useful, it would be both a performance issue and a tricky problem to solve since nothing is generating inotify events on behalf of these fictional "writes". Within the kernel inotify works at the filesystem API level.

The situation then is that some things in sysfs and procfs do generate inotify events, /proc/uptime for example will tell you when it has been accessed (access, open, close), but on my kernel /proc/mounts shows no events at all when file systems are mounted and unmounted.

Here's Greg Kroah-Hartman's take on it:

http://linux-fsdevel.vger.kernel.narkive.com/u0qmXPFK/inotify-sysfs and Linus:

http://www.spinics.net/lists/linux-fsdevel/msg73955.html

(both threads from 2014 however)

To solve your immediate problem you may be able to use dbus, e.g. dbus-monitor --monitor --system (no need to be root) will show trigger on tun devices being created and removed (though mine doesn't show the tun device name, only the HAL string with the PtP IP); udevadm monitor (no need to be root); or fall back to polling the directory (try: script to monitor for new files in a shared folder (windows host, linux guest)). (With udev you could also use inotifywait -m -r /dev/.udev and watch out for files starting with "n", but that's quite an ungly hack.)

Related Question