Mount NFS on linux using systemd to speed up boot

wait and see

Using fstab to mount nfs shares is a bit slow. It depends on the NetworkManager-wait-online.service, so it waits 6s to do 200ms operation.

m@r7:~$ systemd-analyze blame | head -n 20
6.259s NetworkManager-wait-online.service
4.299s qemu-kvm.service
4.209s plymouth-quit-wait.service
745ms dev-nvme0n1p2.device
627ms snapd.service
580ms fwupd.service
537ms systemd-logind.service
329ms postfix@-.service
316ms mnt-f2.mount
313ms systemd-resolved.service
300ms snap-intellij\x2didea\x2dcommunity-106.mount
274ms udisks2.service
254ms systemd-timesyncd.service
251ms snap-gtk\x2dcommon\x2dthemes-818.mount
219ms snap-intellij\x2didea\x2dcommunity-101.mount
212ms snap-intellij\x2didea\x2dcommunity-109.mount
205ms upower.service
198ms mnt-red.mount
183ms snap-gnome\x2d3\x2d26\x2d1604-74.mount
169ms snap-gnome\x2d3\x2d26\x2d1604-70.mount

Sometime this wait is necessary, but in this case – not. In order to avoid this wait and actually proceed with boot, then mount whenever network becomes available, move mount definition from fstab to systemd.unit.

Unit (a file in /etc/systemd/system) should be named as dir-dir-dir.mount. So, if we are going to mount nfs share to /mnt/red, when we would call the unit file mnt-red.mount.

In my case, unit file /etc/systemd/system/mnt-red.mount looks like this:

[Unit]
Description=Red
After=network-online.target

[Mount]
What=192.168.42.99:/mnt/red
Where=/mnt/red
Type=nfs
Options=auto,x-systemd.automount,x-systemd.device-timeout=10,timeo=14,vers=4.2

[Install]
WantedBy=multi-user.target

After that, you just need to enable it with

systemctl enable mnt-red.mount

Voila, 6s boot time shaved off.

Notes:

  • this wait of 6 seconds will still be there, mounting of nfs share is now not connected to fstab where root, home and other partitions are mounted from. it is done a bit later independently
  • it’s done on ubuntu 18.10