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 am trying setup permanent samba share mounts. That's not too hard using these instructions. But, I want them to

  1. Automatically remount whenever I join the network where these shares are available.
  2. Automatically unmount (or make access requests fail immediately instead of hanging) whenever I leave the network, i.e. avoid this automatically.

Googling suggests that AutoFS might be helpful. I gather it takes care of the 1. above but I am not sure about the 2.

The other questions about automated Samba mounts, i.e. How to mount a samba share permanently?, do not seem to address automatic remounts/unmounts, so I think this is not a duplicate.

Thanks.

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted
+100

Mounting Samba shares with CIFS:

Edit your /etc/fstab file to automatically mount your Samba shares:

//<server>/<share> <mount point> cifs rw,_netdev,user=<username>,password=<password>,uid=<uid>,gid=<gid> 0 0

Notice the _netdev option:

CIFS doesn't know about this option and will throw a warning about it, but you should leave it in.

This option ensures that the drives won't get mounted if you have no network or if you are on the wrong network.

It also makes sure your shares get mounted/unmounted when you switch networks. That's because Debian's if* scripts are triggered when your network configuration changes, and they watch all the filesystems with the _netdev option and do the right thing automagically.

Source

you can check man mount for more information about _netdev

To insatll CIFS tools

sudo apt-get install cifs-util
share|improve this answer
    
Thanks, I will try this out. If this works as advertised, this is exactly what I was looking for. –  Laurynas Biveinis Oct 8 '12 at 9:42
    
You are welcome try and inform us by the result please –  user61928 Oct 8 '12 at 9:56
    
So what happens with you?! –  user61928 Oct 9 '12 at 8:31
    
I have planned to check this later today. –  Laurynas Biveinis Oct 9 '12 at 11:22
    
I added "//ip/share /mnt/mnt-point cifs rw,_netdev,nodev,noexec,user,username=foo,password=bar,uid=foo,gid=foo,file_mode‌​=0600,dir_mode=0700,nounix 0 0" to /etc/fstab, mounted the share, started a file op, killed the WiFi by HW switch, and it hanged. –  Laurynas Biveinis Oct 9 '12 at 14:21
show 2 more comments

The mount will always try to re-connect when you access the resource. If you are network hopping then it will depend if you have a route to the host or not. But, AutoFS will keep trying. There are parameters you can configure (timeouts, retries, etc).

share|improve this answer
add comment

Write the scripts to mount and to unmount the share.

Place them in "/etc/network/if-up.d" and in "/etc/network/if-down.d" respectively. These are the locations for scripts that are run when the network connection is established or closed.

However, if you are looking for a particular server to come up or down online, the only solution I see is to make a cron job that checks the server connection periodically, keeps the state and runs the scripts accordingly.

share|improve this answer
    
For robustness (i.e. server going down unexpectedly) I think the latter solution is a better one. I will wait for more answers, if any, in the case this particular wheel has been invented already. Paldies. –  Laurynas Biveinis Oct 2 '12 at 15:06
add comment

AutoFS will work fine. It will auto mount when you access the share, remount if dropped and unmount when idle. When you log off and your processes are all terminated (due to log off) then the mount will fall after being idle for some time. You can configure the idle time.

See this how-to: http://www.howtoforge.com/accessing_windows_or_samba_shares_using_autofs

share|improve this answer
    
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. –  hexafraction Oct 3 '12 at 20:41
    
Thanks. Will it hang trying to mount if I try to access the mount point outside the network? Likewise if it is mounted and I suspend while on the network, then resume outside of it with some request keeping the mount busy? –  Laurynas Biveinis Oct 4 '12 at 4:24
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.