Is there a way to mount/unmount network drive as needed [SOLVED]

I've 2 network drives on one subnet (nfs and samba). I would like to access them only if computer is connected to particular ssid (subnet).

I'm using gnome primarily. And files stops responding if mount points can't be accessed. There is no real way to recover from this apart from connecting to network, unmount and then change network.

I would like those drives to be accessed by system only if they are reachable.

[EDIT][UPDATE]: Following suggestions from comments. I've tried systemd and autofs methods both of them don't work the way I've setup access to mounts. If mount point is in home OR bookmarks in files app, it would hang up as soon as network is disconnected. It works well when mount points are not visible by default (for e.g. /smb/share1). Currently using autofs <- easy and short to setup with mount points under /smb and /nfs

29 points · 21 comments · view on lemmy.world

21 Comments

IanTwenty@piefed.social · 20 pts · 275d (5 replies)

Maybe look into autofs which will mount only when you choose to access the drives and then unmount on idle. Could be simpler then trying to react to network status.

https://wiki.archlinux.org/title/Autofs

rando@lemmy.ml · 4 pts · 275d

Thanks will have a look at autofs

rando@lemmy.ml · 2 pts · 267d (2 replies)

updated OP

IanTwenty@piefed.social · 1 pts · 267d (1 reply)

How frustrating about the hangs, looks like this has been a problem for years in various desktops and file managers and you've already found the best mitigation (keeping the mounts out of home)

rando@lemmy.ml · 1 pts · 267d

Bookmarking this location in file manager also causes problem.

thatradomguy@lemmy.world · 2 pts · 275d

I second this. autofs is was I'd recommend.

thenextguy@lemmy.world · 6 pts · 275d (5 replies)

Systemd will do this. Iirc you just need to put the mount info in fstab.

https://unix.stackexchange.com/questions/765065/how-to-auto-mount-cifs-share-if-the-server-is-only-ready-later

rando@lemmy.ml · 1 pts · 270d

I currently have systemd solution but it only manages to mount when needed.

corsicanguppy@lemmy.ca · -14 pts · 275d (3 replies)

Let's avoid systemd solutions. They'll just lock you deeper into lennart's cancer.

prex@aussie.zone · 4 pts · 275d (1 reply)

If you have systemd installed then using it is fine.
Genuine question: what init do you use & how would you do this? Of course it doesn't need to involve init.

caseyweederman@lemmy.ca · 6 pts · 275d

They just reach into their computer case and tickle the pins on the CPU when they want to initialize PID 1.

exu@feditown.com · 1 pts · 275d

Imagine using a well designed set of tools instead of parts stuck in the 90s

d00phy@lemmy.world · 5 pts · 275d (3 replies)

I know autofs will work with nfs. Never used it with SMB. I’ve used it on a share of /home to specifically mount /home/user as needed (e.g., at login).

corsicanguppy@lemmy.ca · 3 pts · 275d

I've used smb with autofs. Works a treat.

Be sure you have nothing running from or accessing the mount constantly, of course -- I forgot with a homedir -- or it'll never I mount.

rando@lemmy.ml · 1 pts · 275d

Thanks will have a look at autofs

rando@lemmy.ml · 1 pts · 267d

updated OP

just_another_person@lemmy.world · 2 pts · 275d (3 replies)

This should sidestep that and timeout pretty quickly: https://jshtab.github.io/guide/gvfs-autmount/

rando@lemmy.ml · 1 pts · 267d (2 replies)

I'll have a look at this, updated OP for now

just_another_person@lemmy.world · 1 pts · 267d (1 reply)

You never mentioned you were trying to mount live files or your home directory...that's an entirely different thing.

Yes, it does matter.

rando@lemmy.ml · 1 pts · 267d

I'm not mounting home directory itself but my samba mount point directory was inside home directory. For e.g. /home/user/share1

flx@lemmy.blahaj.zone · 1 pts · 269d

A shot in the dark, but try setting them up to connect to a hostname instead of an IP - maybe if the name doesn't resolve then it won't get stuck?