Systemd natively supports automount units, which only actually mounts e.g. a network drive when something on the system attempts to access the mount point in question.
For this to work reliably at boot-time for network drives, the trick seems to be to configure systemd to have an unlimited timeout for the units in question.
The following assumes that a share named //server/media_share
will be mounted on /mnt/server/media_share
.
Start by setting up a .mount
unit for the network drive (in this case, it's a samba/cifs share):
# contents of /etc/systemd/system/mnt-server-media_share.mount
# NB: The name of the .mount unit needs to match the mount location!
[Unit]
Description=Server media_stash share
StartLimitIntervalSec=0
[Mount]
What=//server/media_share
Where=/mnt/server/media_share
Type=cifs
Options=guest,sec=ntlmssp,_netdev
[Install]
WantedBy=multi-user.target
Then, set up an .automount
unit for the .mount
unit:
# contents of /etc/systemd/system/mnt-server-media_share.automount
# NB: The name of the .automount unit needs to match the mount location!
[Unit]
Description=Automount unit for //server/media_share
StartLimitIntervalSec=0
[Automount]
Where=/mnt/server/media_share
[Install]
WantedBy=multi-user.target
Finally, enable the .automount
unit with sudo systemctl enable --now mnt-server-media_share.automount
.
The end result should be that the share will be automounted the first time something tries to access it.
Running sudo mount
after enabling the .automount
unit should show a corresponding mount point line starting with systemd-1
:
systemd-1 on /mnt/server/media_share type autofs (... options ...)