Mount NFS share upon first access

It seems like x-systemd.automount is exactly what I need to mount my NFS share via fstab but only when it is first accessed. My reading of various docs (like this one) indicates that this is the purpose of this option.

This is my line in fstab:
media:/mnt/Share/exports /mnt/nfs/media-server nfs defaults,noatime,x-systemd.automount 0 0

However, this doesn’t seem to work on my eOS system. I would expect ls /mnt/nfs/media-server (or viewing it via dolphin) to cause the share to be mounted (per this thread) but it does not. I still have to do mount -a after booting to mount the share. It then is available.

I see nothing in dmesg that would indicate an error. I do see the r8169 0000:06:00.0 eno1: Link is Down message right after mounting of the drives and that is expected given the network isn’t available at that point – right?

I’ve searched around a lot on this topic and found plenty of articles that specify using the x-systemd.automount option (here), using NetworkManager-wait-online or a separate script via cron/systemd timers (here), etc. None of these have worked.

Has anyone here successfully used x-systemd.auotmount (or any of the other methods) to mount an NFS share?

NOTE: I’ve been using LinuxMint (desktop and server) for several years and in the process of evaluating eOS with the expectation of making it my daily driver. With LM, I was relying upon autofs to accomplish this and it worked very well. I haven’t tried that option yet with eOS as I wanted to try to get the – what seems to be – simple option of x-systemd.automount working.

This is what I use for my nfs mounts: x-systemd.automount,x-systemd.device-timeout=10,timeo=14,x-systemd.idle-timeout=1min

And I just have this in my fstab, of course this is always mounted on startup.
pharazon:/raid /mnt/raid nfs rw 0 0

And using this, the behavior you see is that upon first access (e.g., ls <share_mount_point>) you see the list of contents in the NFS share – correct?

Could this be related to working over a WiFi connection? Doesn’t seem it should matter, but…

I tried the option string you provided:
media:/mnt/Share/exports /mnt/nfs/media-server nfs x-systemd.automount,x-systemd.device-timeout=10,timeo=14,x-systemd.idle-timeout=1min 0 0

But it didn’t mount it upon first access. I had to mount it manually and then it was available – see below. I tried this after rebooting and waiting a minute or so in case it needed some time. :wink:

~$ ls -al /mnt/nfs/media-server
total 8
drwxr-xr-x 2 root root 4096 Apr 29 14:25 .
drwxr-xr-x 3 root root 4096 Apr 29 14:25 ..
~$ sudo mount -a
~$ ls -al /mnt/nfs/media-server
total 20
drwxrwxrwx 5 rod  rod  4096 Jan 22 11:32 .
drwxr-xr-x 3 root root 4096 Apr 29 14:25 ..
drwxrwxrwx 8 rod  rod  4096 Apr 19 15:03 Documents
drwxrwxrwx 4 rod  rod  4096 Mar 28  2024 Software
drwxrwxr-x 2 rod  rod  4096 Mar 24 19:10 Xfer

I use Systemd units for this.

You need two files to make this work, but once set up, it will work as you want it to. One file will be a .mount file, the other an .automount file. Both should have the same filename where the “/path/to/mount/location” translates to “path-to-mount-location” For example, I have a mount at ~/Games/Emulation. The mount/automount files look like this

home-brent-Games-Emulation.mount
home-brent-Games-Emulation.automount

The contents of each file are as follows.

.mount

[Unit]
Description=Mount Emulation Folder (/home/brent/Games/Emulation)
Requires=network-online.target
After=network-online.target systemd-resolved.service
Wants=network-online.target systemd-resolved.service

[Mount]
What=192.168.1.100:/mnt/Emulation
Where=/home/brent/Games/Emulation
Type=nfs
Options=

[Install]
WantedBy=multi-user.target

The .automount file is a little simpler:

[Unit]
Description=Automount NFS share

[Automount]
Where=/home/brent/Games/Emulation

[Install]
WantedBy=multi-user.target

I use this setup for SMB, but I’ve modified it a little bit for NFS. Just add the NFS mount options you need by the Options= tag in the .mount file. Once complete, move the two files to /etc/systemd/system (You’ll need to do this as sudo) then enable the automount systemd unit with systemctl enable home-brent-Games-Emulation.automount --now Only one of the two files can be enabled at the same time. If you want the share to automount before being accessed, enable the .mount file. If you want it to automount when accessed, use the .automount file.

1 Like

Yes

I have no doubt that this is heavily redundant but "it works for me":trade_mark:.

noauto,x-systemd.automount,x-systemd.device-timeout=10,x-systemd.i
dle-timeout=0,_netdev,soft,rw,vers=4.1,tcp,bg,nofail

Thank you. Your solution works as I desired – enabling the .automount causes the share to mount upon first access. (NOTE: I did have to change the name of my mount point as the systemctl enable command did not like having a name with a dash in it; i.e., media-server. Probably confused it because the unit filename relies upon dashes in place of slashes.)

But, here’s my follow-up question: Based upon what I’ve read about x-systemd.automount, it relies upon units to do its magic, “dynamically” creating them as needed. I’m wondering why that option doesn’t work but creating these explicit units does work. Thoughts? Have you ever tried the x-systemd.automount option? Or did you just go and exlicitly create these units?

Of course, I’m glad to have a solution that works. But I’m always interested in understanding why an exception is required when there is supposed to be a normal option but it doesn’t work.

Thank you. I got the same non-result with this. Odd…

What is the process you are using for testing?

Good catch!
Try creating an fstab entry without dashes and see if it works. Smells like a bug in systemd fstab converter.

I did fstab years and years and years ago, before I went back to windows then came back to Linux much later. I’ve done manual units only for the last couple years.

1 Like

Yeah, thought of that and immediately gave it a try – nope. Still wouldn’t mount via fstab. :face_with_raised_eyebrow:

Below is my testing; you can see an example output here.

  1. Edit /etc/fstab entry for the share and save it.
  2. Reboot the system.
  3. After it settles, open a terminal and do ls <mount_point>.

If it worked, it would show the content present on the NFS share. If it doesn’t work, the ls returns just empty – but if I then do sudo mount -a, it works; an ls shows the content from the server.

As noted previously, my NFS share is media:/mnt/Share/exports and the local mount point is /mnt/nfs/server. (Previously the name was media-server but changed that because of the conflict over the dash.)

Based upon feedback from this thread, I had updated my fstab entry to be:
media:/mnt/Share/exports /mnt/nfs/server nfs defaults,noatime,x-systemd.automount,x-systemd.device-timeout=10,timeo=14,x-systemd.idle-timeout=1min 0 0

With the info for systemd units from @_TK , I created the unit files and enabled the .automount. I then followed the same testing steps and it works as expected.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.