Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1756  / 2 Years ago, sun, january 23, 2022, 8:05:09

The problem: I have a laptop (running ubuntu 20.04) which is highly mobile and works from multiple locations. While at location A: I have a CIFS share which I need to work. While not at location A, I don't need this particular CIFS share. If I mount the CIFS share manually using everything works, but I would like to automate the process because I change locations at least 2 - 3 times a day. When I connect to the network at location A, I don't want to manually have to mount the cifs file share, I'd rather just like it to be running. Even more importantly, if I disconnect from location A, I don't want my system to crash because it thinks there should be a file system which doesn't exist anymore. Manually completing these tasks is beginning to be tedious very quickly. How do I get the share to mount automatically while connected to a particular network and then unmount when not connected to that same network?


NOTE:This is not a question about fstab, unless of course you know of a way to get the fstab file to behave in this fashion of constantly switching location and networks and only mounting the file system only in the appropriate location/network. I have an fstab entry for the cifs share which makes mounting and unmounting easier, but doesn't actually solve my problem.


More From » samba

 Answers
6

One possibility is a systemd automount.


[1] Create a mount point.


It cannot be under your home directory or /media. So for example create one at /mnt/SrvAshare.


[2] Then add - as an example - the following in /etc/fstab:


//serverA/sharename /mnt/SrvAshare cifs defaults,uid=1000,noauto,x-systemd.automount,x-systemd.idle-timeout=30,x-systemd.mount-timeout=10 0 0

[3] Then make systemd happy:


sudo systemctl daemon-reload
sudo systemctl restart remote-fs.target

It works by not mounting automatically on boot ( noauto ) but as required ( x-systemd.automount ) when you or some application or some process accesses the /mnt/SrvAshare mount point. It's failry seamless. If for example you just do an ls -l /mnt/SrvAshare it will mount the share.


x-systemd.idle-timeout=30 will unmount the share if it has not been used for 30 seconds ( user specified ).


x-systemd.mount-timeout=10 will try to mount the share for 10 seconds ( user specified ) then stop if unreachable. Useful if you inadvertently select the mount point when you are in the wrong location.


[#1048] Tuesday, January 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
assionortly

Total Points: 423
Total Questions: 121
Total Answers: 115

Location: Chad
Member since Wed, Sep 30, 2020
4 Years ago
assionortly questions
;