I've seen questions similar to this asked before in several variations, but I have yet to locate a satisfactory answer.
Here's what I have:
- A highly mobile Debian laptop that switches networks frequently.
- A FreeNAS server at home with a bunch of CIFS shares on it.
Here's what I want:
- If I access the CIFS share directory when I'm at home, auto-mount the CIFS share.
- If I access the CIFS share when I'm anywhere else, return an error immediately (i.e. don't contemplate the network for five minutes before figuring out the NAS isn't there).
- When I suspend or hibernate the machine, cleanly unmount the share so that the mount isn't pointing into thin air when the machine wakes back up again. If the share can't be cleanly unmounted (a process is using a directory), either:
- Abort the suspend/hibernate and tell me who's camping on the mount; or
- Force the unmount anyway and proceed with suspend/hibernate.
- Extra Credit: When the network connection drops (pull the plug or lose association with the WAP), attempt to cleanly unmount the share.
- Extra Credit: This magic should be independent of the network manager in use.
None of the various options I've read about seem to be appropriate:
autofsseems to be reliable, but doesn't appear to have any awareness of suspend/hibernate.
/etc/fstablooks interesting, but appears to apply only to NFS shares.
- I have not yet been able to get
gvfsto work, but then I haven't tried very hard.
Any suggestions on how to approach this?
Here's the short version of what I ended up doing so far. I installed
autofs, and wrote a small shell script to force-expire all auto-mounted volumes, which gets called whenever a network interface goes down.
I created an automounter map for my CIFS server, with the credentials stored in an external file (yuck). I then wrote the following shell script:
#!/bin/sh -e # Expire everything in the automounter. if [ -f /var/run/autofs.pid ]; then kill -USR1 "$(cat /var/run/autofs.pid)" fi
Sending the USR1 signal to the automount daemon causes it to force-expire all unused mounts. I then placed this script in
/etc/network/if-down.d/autofs, with a softlink in
/etc/network/if-post-down.d/autofs. NetworkManager invokes all the scripts in the latter directory when a connection drops, so any auto-mounted filesystems should be unmounted.
I'm not terribly happy with the arrangement, however. For a start,
autofs makes no distinction between auto-mounted volumes, and, upon receiving USR1, will unmount all inactive volumes, including non-network volumes, which is rather heavy-handed.
Further, the login password for the CIFS server has to be stored in a file. I would much prefer to be prompted for it.
Anyway, that's what I've kluged together so far. It works, but I'll be happy to use something better...