60

I've recently created a dropbox system using inotify, watching for files created in a particular directory. The directory I'm watching is mounted from an NFS server, and inotify is behaving differently than I'd expect. Consider the following scenario in which an inotify script is run on machine A, watching /some/nfs/dir/also/visible/to/B.

-Using machine A to create a file in /some/nfs/dir/also/visible/to/B, the script behaves as expected. Using machine B to carry out the same action, the script is not notified about a new file dropped in the directory.
-When the script is run on the NFS server, it gets notified when files are created from both machine A and machine B.

Is this a bug in the bug in the package I'm using to access inotofy, or is this expected behaviour?

BenMorel
  • 30,280
  • 40
  • 163
  • 285
ajwood
  • 15,375
  • 15
  • 54
  • 90
  • 2
    So you're saying that only the script on the server that owns the filesystem or the computer that made the change gets notified? That is the behavior I would expect. – Gabe Nov 20 '10 at 04:48
  • 1
    I reported a feature request [here](https://bugzilla.kernel.org/show_bug.cgi?id=53161). – HRJ Jan 29 '13 at 09:26
  • I think this question could be moved (migrated) to https://serverfault.com/ – F. Hauri Aug 20 '19 at 11:07

5 Answers5

74

inotify requires support from the kernel to work. When an application tracks a directory, it asks the kernel to inform it when those changes occur. When the change occurs, in addition to writing those changes to disk, the kernel also notifies the watching process.

On a remote NFS machine, the change is not visible to the kernel; it happens entirely remotely. NFS predates inotify and there is no network level support for it in NFS, or anything equivalent.

If you want to get around this, You can run a service on the storage server (since that kernel will always see changes to the filesystem) that brokers inotify requests for remote machines, and forward the data to the remote clients.

Edit: It seems odd to me that NFS should be blamed for its lack of support for inotify.

Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems in 1984, wikipedia article

However:

Inotify (inode notify) is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem. [...] It has been included in the mainline Linux kernel from release 2.6.13 (June 18, 2005 ) [...]. wikipedia article

It's hard to expect a portable network protocol/application to support a specific kernel feature developed for a different operating system and that appeared more than twenty years later. Even if it did include extensions for it, they would not be available or useful on other operating systems.

*emphasis mine in all cases


Another problem with this; Lets suppose we are not using a network at all, but rather, a local filesystem with good inotify support: ext3 (suppose its mounted at /mnt/foo). But instead of a real disk, the filesystem is mounted from a loopback device ; and the underlying file is in turn accessible at a different location in the vfs (say, /var/images/foo.img).

Now, you're not supposed to modify mounted ext3 filesystems, But it's still reasonably safe to do so if the change is to file contents instead of metadata.

So suppose a clever user modifies the file system image (/var/images/foo.img) in a hex editor, replacing a file's contents with some other data, while at the same time an inotify watch is observing the same file on the mounted filesystem.

There's no reasonable way one can arrange for inotify to always inform the watching process of this sort of change. Although there are probably some gyrations that could be take to make ext3 notice and honor the change, none of that would apply to, say, the xfs drtiver, which is otherwise quite similar.

Nor should it. You're cheating!. inotify can only inform you of changes that occured through the vfs at the actual mountpoint being watched. If the changes occured outside that VFS, because of a change to the underlying data, inotify can't help you and isn't designed to solve that problem.

Have you considered using a message queue for network notification?

SingleNegationElimination
  • 137,315
  • 28
  • 247
  • 284
  • 16
    This just seems to a bug. Yes the change is made elsewhere, but the fact is NFS clients do eventually discover newly added files. Even if the event were deferred, it should be possible for a NFS client to notify inotify listeners when it discovers newly added / removed resources. – James Blackburn Feb 25 '11 at 14:32
  • 3
    @JamesBlackburn : this is likely for performance reasons. NFS clients probably only discover changes lazily, upon request. – static_rtti Oct 04 '12 at 09:11
  • 6
    Of course, NFS can not be blamed for not supporting inotify. But, you example is totally flawed. An NFS implementation could support something like inotify and it could map its (NFS internal protocol) stuff to inotify stuff. It does not. So it is – Frunsi Nov 16 '12 at 03:20
  • 1
    I need to implement something like this, but I'm confused on how I would go about using message queues to solve the problem. Do you mind elaborating? I have about 13-14 instances of different (VMs) that each access a mounted NAS, and since inotify is per kernel/VM, I run into this dilemma. – meder omuraliev Feb 19 '14 at 21:46
  • 2
    @meder: that sounds an awful lot like a [question!](http://stackoverflow.com/questions/ask) – SingleNegationElimination Feb 19 '14 at 22:33
  • The suggestion of proxying inotify events is very sensible and there are implementations out there: https://github.com/sillypog/inotify-proxy – deed02392 Jul 04 '17 at 11:35
  • Can this work on NAS Storage if it is mounted like a directory on a server? – Jaskaran Singh Puri Jul 25 '19 at 13:39
  • Your last part comparing nfs to loopback is not really relavent or an accurate anology. NFS shares are designed to be modified by multiple systems. The protocol is written with that in mind. Loopback devices do not support writing to the underlying file directly. There is zero code designed to check for stale pages of cache having been updated on disk. – Philip Couling Jan 09 '21 at 16:35
6

I found an SGI FAM using an supervisor daemon to monitor file modification. It supports NFS and you can see some description on wiki

cmchao
  • 86
  • 1
  • 3
  • 2
    It seems that FAM requires a FAM server running on the remote FS in order to work. Given that FAM is even older than inotify, I wonder why the later doesn't support remote FS. – Wernight May 08 '14 at 16:19
6

To anyone who has come across this question in the search for an answer of why bind mounting on Docker will not detect file changes from host directory (for hot reloading of an app), it's because the propagation of file changes between host and container is not communicated to the container kernel.

Only changes from the container itself is communicated to the kernel. Solution for this is to have your live reload utility turn on "polling mode" instead of using fsnotify.

Derian Tungka
  • 61
  • 3
  • 6
  • 1
    Yes and if u came from java world here is an example how to do it easily with apache-commons -> https://github.com/eugenp/tutorials/blob/master/core-java-io/src/main/java/com/baeldung/dirmonitoring/DirectoryMonitoringExample.java – Dzmitry Hubin Oct 10 '18 at 14:00
  • Can this work on NAS Storage if it is mounted like a directory on a server? – Jaskaran Singh Puri Jul 25 '19 at 13:39
  • Actually I just tried this on a CentOS 7 host and container guest with Docker 18, and it worked, even on an NFS host volume. – Doctor J Aug 28 '19 at 19:45
  • 1
    In Docker there is no such thing as a container kernel: There is only one kernel which is shared between the host and the container. Thus inotify works inside the container if used on bind mounts from host local filesystems. But it does not work if the bind mount is of a NFS filesystem where the docker host is the NFS client and the filesystem is changed by some other NFS client or directly on the NFS server. – Juergen Sep 11 '20 at 16:44
0

I agree with SingleNegationElimination's explanation, and would like to add that iSCSI targets will work, since they alert the kernel.

So things on "real" file systems (relative to the system, that is) will trigger Inotify to alert. Like Rsync'ing, net-catting something into a mounted partition.

If you have to get notifications via inotify (or have to use inotify) you can make a cron to rsync -avz over to the file system. Drawbacks of course are that you are using real system hdd space.

0

I second @SingleNegationElimination.

Also, you can try notify-forwarder.

  • Machine A watches for local inotify events, then forwards them to Machine B (via UDP).
  • Machine B doesn't (can't?) replay the events, but fires an ATTRIB event for the changed file.

If you use vagrant, use vagrant-notify-forwarder.

jchook
  • 5,317
  • 2
  • 31
  • 37
  • Can this work on NAS Storage if it is mounted like a directory on a server? – Jaskaran Singh Puri Jul 25 '19 at 13:39
  • @JaskaranSinghPuri inotify works at the VFS layer of the kernel, so I think you will see file operations performed on the machine running `inotifywait`, but not operations performed on a different remote client. – jchook Jul 26 '19 at 02:13