Mount only if source is available

Hello Manjaro Friends,
My problem is actually not a specific Manjaro problem, but it seems to me that here is the most capable community, that’s why I’m writing here.

I use a lot of different Linux operating systems, but with all of them I have a problem that totally annoys me.
The freezing of terminals and file managers when the source of a SMB network share is not reachable.

I always have my NAS mounted on my systems, under Windows, under MAC and under Linux.
Especially with Linux everything freezes when I click on the network connection and the source is not online, even worse it is when the file manager wants to start with the last path and this was the network path, which is now not online, then the whole desktop is not accessible for a long time.

I’m sure you all know the problem too, long story short.
What I would like to achieve is that the network connection is only established when the source is available.
I could of course write a script for this, but I wonder if this is possibly also feasible with system functions, for example with systemd.
I mount my drives as follows:

//unimatrix/unimatrix-share /srv/mount/unimatrix-share	  cifs    noauto,user,x-systemd.automount,x-systemd.idle-timeout=60,username=****,password=*****,uid=1000,gid=1000,defaults  0   0

I’m not interested in the exact syntax inside the fstab (unless of course you find a logical error), but more about how to solve the problem, e.g. by changing the corresponding systemd units or so.

So again briefly, I want network drives to be mounted only ( manually or automatically ) if the source is present, otherwise nothing should happen.

Would be great if someone could help me with this.

Ah, the man of many words :slight_smile:
Thanks, but that describes only HOW I create systemd units to connect smb network drives.

But that doesn’t change or improve the problem that desktop and terminal freeze when the source is not online.
Or did I miss a special hint?

Only when necessary :slight_smile:

Indeed - an automount unit will solve your issue - don’t enable the mount unit - only the automount.

Then when you access the mount point the mount unit will be activated and it will not stall anything - only open an empty folder if not available.

I thought what I was doing IS an automount.
Whether I do it via fstab or create the units myself, it doesn’t matter, does it?
Did you read my entry in the fstab?

Anyway, I will read the instructions more carefully and see if that changes anything.
Thanks for now.

As you specifically mentioned you wasn’t interested in coments on that - I specifically did not.

And you specifically mention systemd - so I linked you to my topics on systemd mount units.

Theoretically it should not. I had the same issue as you - or close - using fstab - it was extremely difficult to create the correct combination that would not hang my system and at the same time be available on demand.

My mount units has been more complicated with relation to the dependencies but I eventually discovered that the _netdev option made most of them obsolete.

And they now works perfect.

You can see it in the modification history of the topics linked.

puh…linux…questions result in questions result in questions…
What is not quite clear to me when creating units where the share contains a minus sign.
1.
The command ‘systemd-escape -p --suffix=automount “/srv/mount/unimatrix-share”’ results in
srv-mount-unimatrix\x2dshare.automount’.

Does this mean I need to create a file that has the backslash in the filename?
So touch srv-mount-unimatrix\\x2dshare.automount (double backslash)?
2.
Why in the examples in the automount files, the information that is in the mount files is not mentioned, for example: Type=cifs etc.?

I think I need the same information in the automount file, don’t I?

Is it possible for you to show me your unit files so that I can adapt them?
I don’t feel much like doing a systemd study right now.

Thanks for your time, btw.

No - the automount unit activates the mount unit when

Sure no problem

➜  ~ cat /etc/systemd/system/data-nas-data.mount
[Unit]
Description=Data

[Mount]
What=//nas.net.nix.dk/data
Where=/data/nas/data
Type=cifs
Options=_netdev,iocharset=utf8,rw,file_mode=0666,dir_mode=0777,credentials=/etc/smb.cred

[Install]
WantedBy=multi-user.target
➜  ~ cat /etc/systemd/system/data-nas-data.automount
[Unit]
Description=Automount data share
ConditionPathExists=/data/nas/data

[Automount]
Where=/data/nas/data
TimeoutIdleSec=300

[Install]
WantedBy=multi-user.target

I just realized that it is not necessary to make share files executable - thus changing to file_mode=0666

The reason you need \x2d in the filename is because you have a dash (-) and in the mountpoint (unimatrix-share) . systemd uses dashes in the filename for the tree structure. Thus if you have a dash you need to use \x2d as this unicode for -.

So you are correct a double backslash - personally I avoid backslash in the mountpoint but it may be necessary if you have scripts relying on it.

To create the file using the output you could do

cd /etc/systemd/system
sudo touch $(systemd-escape -p --suffix=automunt "/srv/mount/unimatrix-share")
sudo touch $(systemd-escape -p --suffix=mount "/srv/mount/unimatrix-share") 

Then edit the files - below is modified for your system

➜  ~ cat '/etc/systemd/system/unimatrix\x2dshare.mount'
[Unit]
Description=Data

[Mount]
What=//unimatrix/unimatrix-share
Where=/srv/mount/unimatrix-share
Type=cifs
Options=_netdev,iocharset=utf8,rw,file_mode=0666,dir_mode=0777,credentials=/etc/smb.cred

[Install]
WantedBy=multi-user.target
➜  ~ cat '/etc/systemd/system/unimatrix\x2dshare.automount'
[Unit]
Description=Automount data share
ConditionPathExists=/srv/mount/unimatrix-share

[Automount]
Where=/srv/mount/unimatrix-share
TimeoutIdleSec=300

[Install]
WantedBy=multi-user.target

listing the files

➜  system ls *.*mount   
 data-iso.mount            data-private.mount
 data-nas-data.automount   data-projects.mount
 data-nas-data.mount       data-virtualbox.mount
 data-nas-web.automount   'srv-mount-unimatrix\x2dshare.automount'
 data-nas-web.mount       'srv-mount-unimatrix\x2dshare.mount'

Edit your fstab to exclude the mount and unmount it before enabling the automount

sudo umount /srv/mount/unimatrix-share

Then

sudo systemctl enable --now 'srv-mount-unimatrix\x2dshare.automount'

Then browse the mountpoint and watch the magic

1 Like

You are the best!
I´ ll check this and come back.

Ok, first of all thank you very much, your help was much appreciated.
The mounting of the drives works flawlessly.

But, ( isn’t there always a ‘but’? :slight_smile: ), that worked great before too.
I admit, the version without ‘fstab’ is more flexible and I learned a little bit, but the problem is still the same.

  1. is the drive mounted and I remove the source,
    either by turning off the NAS, or by taking the network away from the virtual machine, my file manager freezes. And that doesn’t change until I restart the machine.

  2. what now additionally is, if I start the VM for example WITHOUT network and activate it only after the start, I can also not mount the drive by clicking on the share, that worked in the ‘fstab - version’ always.

So the old problem remains, if the source is gone, my file manager freezes.

Is it possible to set the timeout of the freeze?

You can always create scenarios which the system cannot instantly recover from. This is the nature of networked connections - somewhere it will be a tradeoff.

If you disconnect the network with the folder open in a file manager - this will of course hang your filemanager or your teminal for a period of time - that is unavoidable.

In the automount unit - you can set a low threshold of timeout - which then will ensure a clean unmount after the specified period.

TimeoutIdleSec=1

Likewise you can set in the mount unit which is the number of seconds the unit will wait for the resource to come online

TimeoutSec=10

If you disconnect the device in the middle of an operation there is no clean exit

Any changes requires a reload of the system daemon

sudo systemctl daemon-reload

systemd is a beast with a lot of possibilities

Take this for what it is - a deep look in the systemd man page.

You can control the garbage collection of any given systemd unit using the CollectMode=. This tweak the algorithm used for the unit. The setting defaults to inactive but can be changed.

My guess is that setting CollectMode is forcing it to be unloaded if network is gone.

CollectMode=inactive-or-failed
1 Like

No, I think I may not have expressed myself correctly.

I think it is because systemd - regardless of the timeout - always tries to mount the drive. And that’s where the freeze occurs when the source is not accessible.
Of course I can force this.

So I just tried it again exactly.

I have set the timeout to 5 seconds.
Drive was mapped at startup.
I closed the file manager, wait about 2 minutes, the timeout should make the drive unavailable.
I close everything and remove the network on the VM.

Now there should be no more problems.
But now I open a console and want to look at the directories where the mountpoints are, for example, freeze.
If I open the file manager with any path again ( after minutes ) , freeze.
If I look at my ‘mounts and file system consumption’ with df -hT, freeze.
A real work is now simply no longer possible.

And I don’t want to start any operating system comparisons here, but this is definitely a Linux thing and across any distribution.
With Windows, the file manager freezes briefly, there’s an error message, and that’s it.
It’s the same with Mac.
I just want to have it exactly the same here, an error message, but then all shall be well :slight_smile:

When done correct the device is not mapped at startup - it is only mapped when you access the mountpoint.

YES, sorry, that’s what I meant. It is available from startup when I click on it.

It would be so simple, if systemd-mount would check at every mount-attempt, first ( simple ping ) if the source would be reachable.

Ok - so what you really want is something completely different from your OT

Mount only if source is available

That is exactly what you did with your fstab and what I suggest using mount units.

What you really want has moved the goal post significantly :slight_smile:

Then we need another quote :slight_smile:

The Linux philosophy is ‘Laugh in the face of danger’. Oops. Wrong One. ‘Do it yourself’. Yes, that’s it. - Linux Torvalds

Honestly, I don’t understand what has changed.

I just don’t want my system to freeze when my NAS is not online or when I restart it.
That’s all really.
I personally don’t care what technique I do it with, or if it’s smb, nfs, sshfs, or if it’s systemd or fstab pure.
I just want to be able to continue working when my NAS is no longer online. :slight_smile:

btw.: how can I quote answers?

When is a NAS ever offline?

If a networked device - your Manjaro system is connected to a website on the other side of the world and that server goes offline - then you understand the delay in your browser.

But if you shutdown your NAS (Network Attach Storage) you want your Manjaro to continue if nothing has happened?

Just like your browser will continue for a short while to load the link you just clicked - this is the same with your NAS - you will have to wait until the system realizes the device is not reachable.

To achieve that you need to configure timeouts - but you cannot hide the fact that your Manjaro system knows the NAS was there seconds ago and that it takes a short while before the system acknowledge that fact.

As I said - you can force create issues the system cannot quickly recover from - and that is especially true when it comes to network no matter the system in question.

I don’t want to complicate this, but please listen to me carefully, you don’t quite understand what I mean.
Perhaps I am not expressing myself correctly.
Even if I reduce the timeout to, say, 3 seconds, I still get a file manager freeze after 10 minutes because systemd or whatever mechanism keeps trying to mount the system regardless of whether the source is there or not. Get it? Even after 10 minutes , still…
Of course I understand that my browser can’t find the page when the server is not online, but that’s why the rest of my system still works.

But my file manager stays for ALWAYS in the state that when I try to show me folders, or my console when I do a simple ‘ls’.
And this has nothing to do with my lack of understanding of ‘things’, it’s a Linux thing.
I work with Windows and Mac at home and there is no problem like that. That’s all.
And please, let’s not get into pointless discussions about how I use my NAS. I don’t think it’s that strange. Why should it always be on at home?
It’s got my document storage and a few movies on it, I don’t need that online all the time.


The last sentence of yours is especially important and possibly describes my problem better.
I think my system is just NOT able to notice that the source is no longer online, at least that’s how it looks to me,
because no matter what time passes, I always get timeouts when I open the file manager or use the console.

I don’t think I can describe the behavior any other way. Hope you understand me now.
Maybe this is not fixable and I have to live with it.

Nevertheless, thank you for all the time you have invested, which is not self-evident.

I take it all back and claim the opposite. :slight_smile:

If you set the timeout correctly, especially in the right file, then it works.

I have set the both ‘timeout’ in the mount file, but especially, TimeoutIdleSec in the automount file down ( 10 seconds ), now it is exactly as I expect it.
The file manager stops responding, but 10 seconds later the system has adjusted and there are no more freezes, not even in the console.
I’ll keep watching this, but it seems to be the solution.

So you were right about the timeouts.
Many, many thanks for that.

1 Like