How to prevent SSHFS mount freeze after changing connection after suspend?

I am using SSHFS with reconnect attribute. It works fine, when I suspend notebook and start it again. But problem is when I take notebook somewhere - meaning different IP and different connection (might be Wifi or Ethernet).

Mounted SSHFS folder just freezes and also freezes all the applications which were using it. And the only solution to it I have so far is to use

sudo umount -f /mnt/sshfs_folder

Which is nowhere near to beeing good solution, but so far the only which works. Is there a way how to make SSHFS seemlessly take care of this problem so changing network connection after suspend will not cause any problems ?

Oh and I have Ubuntu 14.04, but I suppose that this is SSHFS specific.


More thorough answer here: SSHFS - auto reconnect.

In short, use:

# create local folder to mount into
mkdir -p ~/mnt/my_server

# now mount your SSH File System
sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 \
username@server_hostname:/path/on/server/to/mount ~/mnt/my_server

To unmount the file system:

sudo umount ~/mnt/my_server

I wrote a shell script called sshfs-reconnect with the following:

while true
do
    sshfs -f -o ConnectTimeout=3,ConnectionAttempts=1,ServerAliveInterval=5,ServerAliveCountMax=3 $@
    echo "disconnected from $@"
    sleep 3
    echo "retry $@ ..."
done

If I leave this running in the background, it has most of the desired behaviors:

  • If the server disappears briefly, processes accessing the mount will hang until the server comes back
  • If the server disappears for too long (about 15 seconds in this case) then waiting processes will get an I/O error
  • If we try to access the mount again while the server is gone, we either get a "transport endpoint is not connected" error or file-not-found (I would prefer to get only the transport error, but haven't figured out how to achieve that)
  • Once the server comes back, we can access the mount again with no intervention

Details about this solution:

  • I do not use -o reconnect here because I do not want sshfs to attempt to reconnect indefinitely, since this causes any process waiting on the mounted filesystem to hang forever. Instead, I let sshfs disconnect after a certain timeout and let those waiting processes fail gracefully.
  • The options ServerAliveInterval=5,ServerAliveCountMax=3 say that ssh checks every 5 seconds to see whether the remote server is still responding. If it does not receive a response after 3 of these checks, then it disconnects automatically. Thus any process waiting on this mount should hang about 15 seconds max.
  • The options ConnectTimeout=3,ConnectionAttempts=1 are necessary because otherwise ssh will wait a very long time when attempting to reestablish connections, which again causes other processes to hang if they try to access the mount during this time.