5

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

2
  • Not every type of filesystem supports these events, what's your kernel version? Commented Oct 15, 2017 at 8:19
  • 4.4.0-96-generic from Xubuntu 16.04.
    – RonJohn
    Commented Oct 15, 2017 at 8:58

2 Answers 2

8

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.)

2

/sys and /proc are not filesystems. They are kernel interfaces which act like a filesystem.

When a network device is created then there is no filesystem folder created. Normal filesystems change when data is changed even when noone is looking. But the content of /sys and /proc is created the moment someone looks at it.

What you need is e.g. udev events (which run a script).

1
  • udev script is a good idea udevadm test /sys/class/net/tun0 Commented Oct 15, 2017 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .