SSHFS Inotify – How to Use Inotify or Named Pipes Over SSHFS

fifoinotifysshfs

Thanks sshfs magic, I can mount my home dir from a remote server with

sshfs user@server:/home/user ~/remote

Optimistically, I thought I'd set a local inotify-hook on ~/remote/logFile (in the sshfs mount) so a local program can react to remote log changes.

cd ~/remote
touch logFile                                # create remote file
inotifywait logFile &                        # set up local inotify-hook
ssh user@server -x touch /home/user/logFile  # touch file from remote

Nothing happens. inotifywait is silent unless I touch the file locally. Writing to a named pipe fails similarly.

Why is this?
How can I bridge this gap?

I could run inotifywait on the remote, hack up a file system change serialisation strategy and maintain a connection to the local, but then I'm basically reimplementing SSHFS. And it completely kills the abstraction.

Best Answer

THe SSHFS filesystem is built on top of the SFTP protocol. SFTP provides only facilities to manipulate files in “classical” ways; the client makes a request to the server (list a directory, upload a file, etc.), and the server responds. There is no facility in this protocol for the server to spontaneously notify the client that something has happened.

This makes it impossible to provide a facility such as inotify inside SSHFS. It would be possible to extend SSHFS with proprietary extensions, or to supplement it with a full-fledged SSH connection; but I don't know of any such extension to SSHFS.

Named pipes can't be implemented on top of SSHFS for the same reason. NFS, the classical networked filesystem, doesn't have any facility to support cross-machines named pipes either. On a networked filesystem, a named pipe creates an independent point of communication on each of the machines where it is mounted (in addition to the server).

FAM (the inotify analogue in SGI IRIX, which has been ported to Linux) provides a daemon which allows notifications to be sent over the network. Linux has rather deprecated FAM since inotify came onto the scene, so I don't know if getting FAM to run would be easier than rolling your own application-specific notification system. You'd need to set up some port forwarding over SSH or establish a VPN in order to secure the network link for FAM and NFS.

If you elect to roll your own, assuming that you're ok with giving the clients shell access, it's fairly easy to run an inotify monitor on behalf of a client: have the client open an SSH connection, and run the inotifywait command on the server, parsing its output on the client. You can set up a master connection to make it faster to open many connections from the same client to the same server.

Related Question