brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: x-systemd.automount in fstab mounts a NAS share on first use

Systemd
~/tips/tip-disk-fstab-x-systemd-automount

192.168.1.x:/export/media /mnt/media nfs x-systemd.automount,x-systemd.idle-timeout=10min 0 0

Mounts a network share only when a program opens the folder, so boot never waits for it.

A network share in /etc/fstab has an annoying habit: the machine tries to mount it at boot, and if the NAS is slow or off, boot gets slow with it. I’d rather have the share show up the moment something actually opens the folder. systemd can do that with one extra word in the options column.

Add x-systemd.automount to the fstab line. systemd then creates an automount unit for that path, and the real mount only happens on first access. With x-systemd.idle-timeout it unmounts again after the share has been idle for a while:

192.168.1.x:/export/media  /mnt/media  nfs  x-systemd.automount,x-systemd.idle-timeout=10min  0  0

Then let systemd reread fstab and start the automount point. The unit is named after the path, so /mnt/media becomes mnt-media.automount:

sudo systemctl daemon-reload
sudo systemctl start mnt-media.automount
ls /mnt/media

That ls is what triggers the mount. Until then /mnt/media is only a placeholder, and nothing goes over the network. This is exactly what I’d want on a laptop or a VM that reads from the shares on my Unraid server: the share is there when I need it and doesn’t hold anything up when I don’t. According to systemd.mount, once you use x-systemd.automount the auto and noauto options no longer matter, because the automount unit is what gets pulled in at boot.

The caveat is the first access. If the NAS is down, the program that opens the folder sits and waits until the mount gives up, and that can look like a frozen terminal. x-systemd.mount-timeout= sets how long systemd waits for the mount command, so I’d add a short one for shares that are often offline. The idle timeout is off by default, so without it the share simply stays mounted after the first use.

Further reading: systemd.mount(5) manual page, systemd.automount(5) manual page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link