8

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...

  • You might want to take a look at the tool Gigolo: uvena.de/gigolo/index.html. I use it to manage the reestablishing of SMB mounts b/w home and work on my laptop. – slm Dec 10 '13 at 15:38
  • @slm: Gigolo looks interesting, but there doesn't appear to be a way to alter the mount options. As a consequence, the share comes up in what I call "dumb FAT" mode, where all the files and directories have permissions 0700. By contrast, mounting the same share using sudo mount -t cifs ... yields preferable results. I'll fiddle with it a bit more, but I don't think Gigolo is quite what I'm looking for. – ewhac Dec 12 '13 at 9:06
2

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
  • The above assumes that the cifs mount is defined in /etc/fstab with the noauto option – HBruijn Dec 10 '13 at 14:20
  • Please add that assumption into the answer. – Bleeding Fingers Feb 14 '14 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.