16

I have a CIFS network share mounted on /etc/fstab at boot on a laptop connected to the wireless network. My fstab looks like this:

//192.168.0.100/MyShare  /mnt/MyShare  cifs  username=<username>,password=<password>,rw,uid=1000,gid=1000,nounix,iocharset=utf8,file_mode=0770,dir_mode=0770,vers=3.0  0  0

Now, that normally works fine, but being a laptop, i move a lot and reconnect to my home wireless network quite often. This means that, when i leave the network, the share mount also disconnects, but when i get back home, it won't reconnect automatically so i need to "mount -a" every time this happens.

I'm looking for an option to automatically mount it on network getting available, or at least mounting automatically upon access (that is, when Dolphin or other file manager accesses the mount point, it should get remounted as root).

Is there any easy way to do this? Any best practice, maybe? Thanks!

Later edit: In the meantime, I found something that i would call a workaround, not a solution, so i'll keep the question opened:

  • i created a script in /etc/network/if-up.d/<script> to be run upon connecting to my network:
#!/bin/bash
if iwconfig|grep -c <SSID>
then                                                         
        mount -a
fi

So now, when my connection becomes active, everything gets auto-mounted.

1 Answer 1

17

Your script seems like a viable option to me. I've seen far more convoluted workarounds to issues in Linux.

Another alternative is a systemd automount which fulfills the or at least mounting automatically upon access requirement. Simple enough to implement and revert back if it's not exactly what you want.

[1] Unmount the share if it is already mounted:

sudo umount /mnt/MyShare

[2] Add 2 more options to your fstab declaration: x-systemd.automount,x-systemd.idle-timeout=30

//192.168.0.100/MyShare  /mnt/MyShare  cifs  username=<username>,password=<password>,rw,uid=1000,gid=1000,nounix,iocharset=utf8,file_mode=0770,dir_mode=0770,vers=3.0,x-systemd.automount,x-systemd.idle-timeout=30  0  0

Then do some systemd stuff:

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

Now the share will only mount when accessed, rather than at boot (x-systemd.automount). It will automatically unmount if share access is idle for more than 30 seconds - user adjustable (x-systemd.idle-timeout=30).

For logs/status, find the unit name:

sudo systemctl list-automounts '*MyShare*'
WHAT                    WHERE        MOUNTED IDLE TIMEOUT UNIT
//192.168.0.100/MyShare /mnt/MyShare yes     0    30s     mnt-MyShare.automount

Automount status and triggering processes:

sudo systemctl status mnt-MyShare.automount
...
systemd[1]: mnt-MyShare.automount: Got automount request for /mnt/MyShare, triggered by 391 (ls)

Status of the mount itself:

sudo systemctl status mnt-MyShare.mount
1
  • What are reasonable values for idle-timeouts? Would it be feasible to keep shares mounted for 86400 seconds, say? (I don't know)
    – knb
    Commented Feb 21, 2023 at 15:59

You must log in to answer this question.