Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have no problem mounting my shares (fstab or Gigolo). What I looking for is a way to automatically unmount the shares when I disconnect from the network and reconnect when I return. Gigolo seems close. It reconnects fine, but I still seem to have to manually disconnect from each share individually.

share|improve this question
add comment

2 Answers

You will have to write a script. This is a skeleton / template, call it net_mount and put it in /etc/NetworkManager/dispatcher.d

#!/bin/sh
# auto mount/umount network shares

IF=$1
STATUS=$2

# set your desired network here
NETMASK="192.168.0.0/24"

mount_shares (){
    # command(s) to mount shares here
    mount share1
    mount share2
    ....
}

umount_shares (){
    # command(s) to un-mount shares here
    umount share1
    umount share2
    ....
}

if [ -n "`/sbin/ip addr show $IF to $NETMASK`" ]; then
case "$STATUS" in
    up)
        mount_shares
        ;;
    pre-down)
        umount_shares
        ;;
    *)
        ;;
esac
else
    umount_shares
fi

Make it executable

sudo chmod a+x /etc/NetworkManager/dispatcher.d/net_mount
share|improve this answer
    
Thanks. So this script will be used in conjunction with fstab or should everything be done in the script? ie /etc/fstab : //192.168.0.100/example /mnt/example cifs username=user,password=1234 –  jasonwert Jan 12 '12 at 2:10
    
You have to fill in how to mount your shares. If they are in fstab, mount /mnt/example. Personally I would add them to fstab with the noauto option. –  bodhi.zazen Jan 12 '12 at 2:16
    
I seem to still have an issue. It mounts fine, but as an example if I pull the network cable nothing gets unmounted. My conky script hangs and if I try to go to /mnt it sits there spinning. As soon as I put the cable back in /mnt loads and conky starts working again. Am I missing something in my fstab? //192.168.0.150/videos /mnt/videos cifs noauto,username=user,dir_mask=0777,password=1234,rw 0 0 –  jasonwert Jan 12 '12 at 3:28
    
The script is for connecting and disconnecting with network manager. Why are you pulling your network cable ? Disconnect and re-connect with NM, pull the cable after you disconnect and before you re-connect. –  bodhi.zazen Jan 12 '12 at 4:18
    
Makes no difference. I tried "Disconnect" from the network manager and got the exact same result. Everything hangs –  jasonwert Jan 12 '12 at 4:29
show 2 more comments
up vote 1 down vote accepted

I found exactly what I was looking for in the Ubuntu Forums.

HOWTO: Automatically mount and unmount shares (alternative version)

It automatically mounts and unmounts shares as they come and go. I've used this method on Ubuntu 11.10, 12.04, and Debian Squeeze and it works flawlessly.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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