Debian – Auto-Mounting CIFS Shares on Laptop

automountingcifsdebiansuspend

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:

  • autofs seems to be reliable, but doesn't appear to have any awareness of suspend/hibernate.
  • The _netdev option in mount and /etc/fstab looks interesting, but appears to apply only to NFS shares.
  • I have not yet been able to get gvfs to work, but then I haven't tried very hard.

Any suggestions on how to approach this?

ADDENDUM:

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…

Best Answer

If you use NetworkManager, you can write scripts that execute on a network interface coming up or going down.

NetworkManager will execute scripts in the /etc/NetworkManager/dispatcher.d directory in alphabetical order in response to network events. Each script should be (a) a regular file, (b) owned by root, (c) not writable by group or other, (d) not set-uid, (e) and executable by the owner. Each script receives two arguments, the first being the interface name of the device just activated, and second an action.

Detect your own network from for instance the search domain and then mount or umount when the interface comes online:

#!/bin/bash
#/etc/NetworkManager/dispatcher.d/mount-my-cifs    
case "$2" in
  up)
    # some logic to detect if we're at home
    grep "search myhome.local" /etc/resolv.conf
    if [ "$?" != "0" ] ; then 
       exit
    else 
       mount /mnt/cifs
    fi
    ;;
  down)
       umount /mnt/cifs
    ;;
esac
Related Question