Mounting shares with cifs when at a specific network

Hi!
I’m sorry, but after browsing the internet for a whole day, I unfortunately need help.
I am lost!

I’ve installed Endeavour OS on my laptop and I’m using a Synology NAS at home. I’ve tried to mount some folders I’m sharing with user login permissions on this NAS to my filesystem on the laptop. I remember having done this once with an arch installation using the fstab on my stationary system. It was hard, because the system tried to mount the shares before the network connection was established, but somehow I figured it out.

But now, with a mobile device, I want to establish an auto mount depending on the user logging into the system. And I want this auto mount only to “trigger”, if the laptop is part of a specific network, because the NAS isn’t accessible from outside of the local network.

I’ve found this article:
https://wiki.archlinux.org/title/NetworkManager#Mounting_of_SMB_shares

This looks like the solutions for my problem, but I didn’t get it to work! It would just do nothing and I’m missing some explanations.

On other forums you are reading about using a systemd mount or a systemd user mount.

Here are some of the questions I can name right now:

  1. I need to add a link to the credentials-file somewhere.
  2. I need the mounted directories to have read and write access by the user without entering the sudo pw.
  3. Where should I mount the shares? I would like to mount them the same way Thunar does, if I access the network and enter the login manually. I would like to see them in the Network Tab with the eject button beside it.
  4. I want the connection to be disconnected, when I leave the network or shutdown the system.

I know these are a lot of questions, but some hints to some good instructions would help me a lot.

Hi

On my KDE Plasma based setup. I use smb4k it has a profiles feature that allows you to conveniently switch between a set of mounts.

Here is some documentation https://docs.kde.org/trunk5/en/smb4k/smb4k/smb4k.pdf

Thank you, I tested your solution.
You can do the mounts depending of the network environment and the mounted directories would be shown in file managers, but I don’t see a solution for doing it on boot-up or login automated.

The way I’m trying to set up the mounts is:

  1. On boot-up, or better on user login, check if the laptop is in a specific network environment or check if the NAS is reachable.
  2. mount some shared folder to the local filesystem with user read/write permissions.
  3. run a script to bind mount some sub-directories to the users local home directory.

With smb4k problem 1 can be solved, but only manually by the user?
Read/write permissions are working fine with smb4k.
Didn’t try scripts because I haven’t accomplished the auto mount.

Two ideas:

  1. You can add cifs/smb shares in fstab, I think it will only mount the share that’s available.
  2. You can add login scripts in kde or any other DE, but in every DE it’s different how you can add the script. You have to write some shell script for checking which network you’re on, then mounting.

Also don’t mark a question solved until it’s solved, or the thread will be locked.

I see you already tried with fstab, it’s weird it’s supposed to work.

Can you post what did you put in fstab? Did it mount with sudo mount -a?

I’ve used fstab with an older installation if arch. But there are two disadvantages for me with this solution.
First you have to add a timeout option, if you are not at your home network. This timeout will reduce boot up speed. Second if I add the local binds of the mounted shares to my home directory it will not work if the NAS isn’t accessible. Those are the reasons I’m looking for a different solution.

Yesterday I’ve tried a lot with systemd --user mounts, but didn’t figure it out.

I think using the NetworkManager Dispatcher would be the right way.
I’m trying to use this solution, because (if I get it right) the script will check the connected network using a UUID of the network. You could add your script for the blind mounts only of the connection is established.

Sorry it’s a bit hard to understand what you mean.

I did an smb mount like a year ago on linux mint, it worked at boot. But I don’t know what happens with two entries if one’s not accessible, shouldn’t be a problem.

Let’s go step-by-step. So please post what did you put in fstab and also check if it mounts with sudo mount -a no need to think about booting if it doesn’t mount with this command.

Sorry, if I get misunderstood. English is not my native language.

I don’t want to use fstab, because it is a mobile device. If I boot up my system and I’m not at home the NAS isn’t accessible and then I don’t want the system to try to connect.

Right now I’ve accomplished to mount the shares, if I’m connected to my home network.
What I’ve done:

I stored the following script at my home directory (mountMM.sh):

#!/bin/bash
mkdir /run/media/<user>
mkdir /run/media/<user>/Share1
mkdir /run/media/<user>/Share2
chown -R <user> /run/media/<user>
mount -t cifs //<ip>/Share1 /run/media/<user>/Share1 -o credentials=/home/<user>/login.cred,uid=<user>,gid=<user>
mount -t cifs //<ip>/Share2 /run/media/<user>/Share2 -o credentials=/home/<user>/login.cred,uid=<user>,gid=<user>

Then I wrote the NetworkManager dispatcher script:
/etc/NetworkManager/dispatcher.d/30-mountsmb.sh

#!/bin/sh
# Find the connection UUID with "nmcli connection show" in terminal.
# All NetworkManager connection types are supported: wireless, VPN, wired...
if [ "$2" = "up" ]; then
  if [ "$CONNECTION_UUID" = "<wifi-uuid>" ]; then
     /home/<user>/mountMM.sh
  fi
fi

I wrote the NetworkManager dispatcher script to unmount:
/etc/NetworkManager/dispatcher.d/pre-down.d/30-umountsmb.sh

#!/bin/sh
if [ "$CONNECTION_UUID" = "<wifi-uuid>" ]; then
   umount -a -l -t cifs
fi

As soon as I connect to my home wifi the dispatcher script runs and mount the shares to /run/media…
I have read and write access and can see the mounted shares in the file managers like USB-Sticks would be shown.

If I disconnect the network the shares become unmounted, but they are still shown as mounted in thunar. But if I look into the /run/media the created directories are gone.

Is there a different way to unmount them?

Maybe it’s an issue with thunar, if they’re really unmounted.

Check cat /proc/mounts

Looks like it is a problem with “mount” freezing and the dispatcher script called when disconnecting won’t finish because if this.
Sometimes only one of the two shares becomes unmounted, sometimes both.
But even, if both folders I mount the shares in, are empty “cat /proc/mounts” and “mount -lt cifs” still showing the shares as mounted.

So i guess mount is freezing for some reasons.
Something like this is written in the arch wiki, but I don’t understand what to do against it.

https://wiki.archlinux.org/title/NetworkManager#Mounting_of_SMB_shares

The previous “umount” scripts are still prone to leaving applications actually accessing the mount to ‘hang’.

Have you looked at https://wiki.archlinux.org/title/Autofs

Seems useful?

1 Like

This looks even more complex than the NetworkManager dispatcher.
It will take some time to get into it!

But this would be a solution instead of the NetworkManager?

My concerns about using fstab or perhaps autofs are the system permanently trying to connect to a network, which isn’t accessible and adding system and network load.

I don’t thin it will try repeatedly with fstab.

I have got smb4k to mount shares on start with the ‘remount recently used shares on program start’ option in its settings.

Smb4k notices if you disconnect the Wi-Fi network and it unmounts them cleanly - which is great.

Unfortunately it does not remount them if you re-connect to the network. You have to stop then restart smb4k. I am waiting for the developer to get back to me on this, he seems to think it is a bug.

https://sourceforge.net/p/smb4k/discussion/help/thread/e4b394c631/?limit=25#75e9