The ConditionPathExists works perfectly, but you need to adapt the path to your Where= statement.
For example:
Your want to mount something to /media/fritzbox , your add to the mount file Where=/media/fritzbox . You would need to add to the automount file Where=/media/fritzbox and in the Unit section ConditionPathExists=/media/fritzbox .
If you received an error, either your Where statement is wrong, or you named your mount unit not correctly.
Btw., /run/media should not be used for a permanent mount, with a mount unit or a fstab entry. It should only be used for temporary mounts, like a USB stick or if you want temporary mount a partition via a file manager.
I had it this way. Even copy-pasted the path everywhere, to make sure there’s no typo.
Unfortunately, it prevented the automount unit from working. And, the other tutorials do not mention it as well - so I am not sure if it is essential.
In order to use the correct name, I even copy-pasted the name provided by systemd-escape but to no avail.
Looks like systemd does not like the handling of file names with special chars in them.
In my example, with run-media-gerd-fritz\x2dnas.mount as provided filename, names show up as follows:
I assume that systemd does not get the names correctly back from that format (at least on my rig here). Hence, I had to change fritz-nas to fritz only, to get it working.
It is not essential. It is a check to determine if the mount point is available. Usually is not a problem if you decide to use a mount point on a non volatile filesystem (like ext4, xfs, btrfs). Unfortunately you chose a volatile filesystem (tmpfs), which means the mount point is simply not available at boot.
I also believe, this is the reason you have a problem with dashes. Looks like systemd will automatically create a mount point if it contains a dash.
But If I intend to use mount on demand / automount - will this work?
At least that’s where all the removable drives are mounted to, as well - so I assumed automount will work with /run/media/, too.
Please correct me if I am wrong.
Please read up on the naming convention - which must match the mount point - and you cannot use /run.
An automount unit does not stand alone. It complements the mount unit when that target is a removable device like a network share.
Starting a mount unit will create the path. The automount unit should only attempt to mount the device and path specified by the mount unit - if the path exist.
Don’t assume that - it is clearly stated that /run is a volatile structure which do no persist across restart - even login.
Because you have used /run - the path don’t exist because /run is recreated on system boot and when a user logs in.
It is also clearly stated - avoid using dashes (-) or spaces in your mountpoint.
EDIT:
Dashes in the mountpoint will work but the unit filename must use the escaped sequence - inside the unit file you use the file system name of the mountpoint not the escaped value.
In any case - avoid using special characters in your mountpoint - use only the ascii char table - this will save you a lot of pain
Thanks a lot to everybody for your help. Indeed, it is, as always, desirable to keep things as simple as possible.
So I just set up a mnt-fritz.mount unit like this
[Unit]
Description=Mount Fritz NAS (/mnt/fritz)
Requires=network-online.target
After=network-online.service
[Mount]
What=//192.168.178.1/FRITZ
Where=/mnt/fritz
Type=cifs
Options=vers=2.1,noserverino,credentials=/home/gerd/.fritzcredentials,uid=1000,gid=1000
#TimeoutSec=seconds
[Install]
WantedBy=multi-user.target
accompanied by a corresponding mnt-fritz.automount:
[Unit]
Description=Auto Mount Fritz NAS (/mnt/fritz)
Requires=network-online.target
After=network-online.service
[Automount]
Where=/mnt/fritz
TimeoutIdleSec=10
[Install]
WantedBy=multi-user.target
In short, -noserverino is supposed to avoid errors mounting a network share offered by a router common in Germany (“FritzBox”). The FritzBox can provide an attached USB stick or USB harddisk as a shared drive in the home network.
When having mounted that share via CIFS on a Linux system, some people experienced errors like
or
In dmesg, you could observe errors like
In general, mounting shares via CIFS, errors like
were reported.
That’s why I had included that option here as well. Maybe it is not necessary anymore with newer FitzBox firmware versions, but so far, I did not notice any negative impact.
Thanks a lot for pointing this out. Looks like at least some of the tutorials found on the net are incorrect at that point as they apparently simply have copied most of the contents of the .mount unit in the .automount unit.
That is interesting and important as well. Thanks a lot for making us aware - most of the SMB/CIFS tutorials appear to be wrong here, too.
So, as a result, it looks like the units shall look like below:
$ cat /etc/systemd/system/mnt-fritz.mount
[Unit]
Description=Mount Fritz NAS (/mnt/fritz)
Requires=network-online.target
After=network-online.service
[Mount]
What=//192.168.178.1/FRITZ
Where=/mnt/fritz
Type=cifs
Options=vers=2.1,noserverino,credentials=/home/gerd/.fritzcredentials,iocharset=utf8,rw,file_mode=0777,dir_mode=0777
#TimeoutSec=seconds
[Install]
WantedBy=multi-user.target
and
$ cat /etc/systemd/system/mnt-fritz.automount
[Unit]
Description=Auto Mount Fritz NAS (/mnt/fritz)
[Automount]
Where=/mnt/fritz
TimeoutIdleSec=10
[Install]
WantedBy=multi-user.target