Issues with my NFS systemd mount units

Hi All,

I have ongoing headaches with systemd. My mount, automount, and most recent error are below.

Summary: Two separate problems.
(1) Sometimes mounts fail on boot and I have to manually mount by doing

sudo systemctl enable --now mnt-Tower-Media.mount

(2) Sometimes the above manual command works, other times, it doesn’t and I get this error:

    /etc/systemd/system  sudo systemctl enable --now mnt-Tower-Media.mount                                                                                                        1 ✘ 
The unit files have no installation config (WantedBy=, RequiredBy=, UpheldBy=,
Also=, or Alias= settings in the [Install] section, and DefaultInstance= for
template units). This means they are not meant to be enabled or disabled using systemctl.
 
Possible reasons for having these kinds of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/, .requires/, or .upholds/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.

Here’s an example of my .automount

[Unit]
Description=Automount Media mount from unRaid tower
ConditionPathExists=/mnt/Tower/Media

[Automount]
Where=/mnt/Tower/Media
TimeoutIdleSec=20

[Install]
WantedBy=multi-user.target

Here’s the corresponding mount

  GNU nano 8.2                                                                       mnt-Tower-Media.mount                                                                                  
[Unit]
Description=Media folder mounted via NFS from Tower unRaid.
After=network.target

[Mount]
What=192.168.0.245:/mnt/user/Media
Where=/mnt/Tower/Media
Type=nfs
Options=_netdev,auto

The error is clearly described

Add to your mount unit

[Install]
WantedBy=multi-user.target

In any case - if you enable the mount unit - it will override the automount unit.

When you use automount only enable the automount unit.

See the following articles in Tutorials

[root tip] [How To] Use systemd to mount ANY device
[root tip] [How To] systemd mount unit samples

Why the inconsistent behavior? I’ll test your solution but I’m curious why it works about 90% of the time but then spits out errors otherwise. That to me, suggests a bug.

Not a bug - misconfiguration.

All units are run async

Possibly because you have tried to enabled the mount unit without a valid install section.

The whole idea behind mount and automount is to only mount on demand - as in when the mount point is accessed.

This is why an automount unit always fails if the corresponding mount unit is enabled.

The automount cannot mount because the mountpoint is already taken by the mount unit.

When a systemd unit fails - systemd is very informative - you will always be able to deduce why the error occurred from the journal.

This didn’t resolve the issue. Ongoing inconsistent behavior with it automounting about 90% of the time. The .mount isn’t enabled, the .automount is. Same problems.

That is strange - for years I have been

  • using mount units for my partitions
  • using mount/automount units for smb and nfs

I switched to mount/automount units because I experienced difficulties getting the systemd options in fstab to work - since then I have not issues with mounting my network shares.

If you cannot get it to work - there may be other underlying issues …

Work out what that is - now that is a completely different task.

Troubleshooting path

  • check your mount unit
    • test the mount unit manually
    • sudo systemctl start <mount-path.mount>
    • when manual start populates the mount path - stop the unit
    • sudo systemctl stop <mount-path.mount>
  • verify your automount unit
    • test the mount unit manually
    • sudo systemctl start <mount-path.automount>
    • list the content of the mount path
    • ls </mount/path>
    • when the mount path is populated you can enable the automount unit
    • sudo systemctl enable --now <mount-path.automount>

For illustrative purpose - proof-of-concept - here is how one of my share mounts is setup.

content of mount unit

 $ cat /etc/systemd/system/a-nfs-data.mount
[Unit]
Description=NAS NFS data share

[Mount]
What=nas.net.nix.dk:/volume1/data
Where=/a/nfs/data
Type=nfs
Options=_netdev,auto
TimeoutSec=30

[Install]
WantedBy=multi-user.target

content of automount

 $ cat /etc/systemd/system/a-nfs-data.automount
[Unit]
Description=NAS NFS data share
ConditionPathExists=/a/nfs/data

[Automount]
Where=/a/nfs/data
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

status for mount unit

 $ systemctl status a-nfs-data.mount
○ a-nfs-data.mount - NAS NFS data share
     Loaded: loaded (/etc/systemd/system/a-nfs-data.mount; disabled; preset: disabled)
     Active: inactive (dead) since Tue 2024-12-17 06:05:11 CET; 1min 40s ago
   Duration: 10.451s
 Invocation: 92b0311609b94aa99118d69b9671be6a
TriggeredBy: ● a-nfs-data.automount
      Where: /a/nfs/data
       What: nas.net.nix.dk:/volume1/data
   Mem peak: 2.4M
        CPU: 25ms

dec 17 06:05:00 tiger systemd[1]: Mounting NAS NFS data share...
dec 17 06:05:00 tiger systemd[1]: Mounted NAS NFS data share.
dec 17 06:05:11 tiger systemd[1]: Unmounting NAS NFS data share...
dec 17 06:05:11 tiger systemd[1]: a-nfs-data.mount: Deactivated successfully.
dec 17 06:05:11 tiger systemd[1]: Unmounted NAS NFS data share.

status for automount unit

 $ systemctl status a-nfs-data.automount --no-pager
● a-nfs-data.automount - NAS NFS data share
     Loaded: loaded (/etc/systemd/system/a-nfs-data.automount; enabled; preset: disabled)
     Active: active (waiting) since Tue 2024-12-17 06:04:52 CET; 2min 24s ago
 Invocation: 0de80f5642e043dda1a5c10cb958ee19
   Triggers: ● a-nfs-data.mount
      Where: /a/nfs/data

dec 17 06:04:52 tiger systemd[1]: Set up automount NAS NFS data share.
dec 17 06:05:00 tiger systemd[1]: a-nfs-data.automount: Got automount request for /a/nfs/data, trigger…75 (ls)
Hint: Some lines were ellipsized, use -l to show in full.

listing the share

The content of /a/nfs/data is listed using mount on access via the automount unit.

 $ ls /a/nfs/data
 codeformatting
 conky
 diskstation-rsync-scripts
 download
 Downloads.tar.gz
 @eaDir
 fh-gpg.tar.gz
 frede
 Huawei
 images-project
 inno
 iso-files
 iso-profiles
 iso-upload
 learning-projects
 log-center
 lxde-project
 lxqt-project
 lxqt-scratchpad
 mabox-live-user.7z
 macOS
 MailScanner-Settings-backup
 manjaro-iso-archive
 manjaro-openbox-0.8.13.1-i686-FINAL.iso
 manjaro-openbox-0.8.13.1-x86_64-FINAL.iso
 manjaro-project
 misc-skel
 music-backup
 nordic.jpg
 NorseIconTheme.tar.gz
 NorseSolidTheme.tar.gz
 ns-uex-lan-settings
 openbox-project
 other-projects
 pacbang-project
 pacbang-webserver-archive.tar.gz
 pacbang-webserver-configuration.tar.gz
 pacbang-webserver-homes.tar.gz
 pacbang-webserver-logs.tar.gz
 pacbang-webserver-root-home.tar.gz
 screenshot.png
 scripts
 seagate-backup-disk
 space-on-premises
 ssh-keys.tar.gz
 SteamCity
 test-vm
 thinkpad-backup
 vb6-xp-vdisks-backup
 virtual-box
 w7rollups.zip
 wallpapers
 wallpapers-project
 watchtower
 web.net.nix.dk
 win7-post-sp1-rollup

I’ll work through these but it appears like manually starting the automount is working. The real issue is that on reboot, it automounts only about . . . 50% of the time which makes a few automated tasks harder to run (I have to check to see if mount exists before running certain jobs).

As I have noted before - DO NOT enable the mount unit when your intent is to use an automount unit.

Which is what you have done …

Disable the mount unit - automount will fail if the mount unit is enabled.

Then remember this

  • disable mount unit
  • enable automount unit
sudo systemctl disable --now mnt-Tower-Media.mount
sudo systemctl enable --now mnt-Tower-Media.automount

The option _netdev tells systemd to only automount when network is available.

I am thinking your automation tasks need to check for the availability of the share before executing.

One method is to use nmap to check if the host can be reached - use nmap to scan for nfs servers

You will need nmap synced

sudo pacman -Syu nmap

The following is an example of such check

#!/usr/bin/bash

# variables
#SMB:   445
#NFS : 2049
#SSH :   22
PORT=2049
HOST=ip.x.y.z

RESULT=$(nmap $HOST -Pn -p $PORT | grep -e 'open')
# use nmap to check the host for service on port
if [[ -z  "${RESULT}" ]] ; then
    echo "host ${HOST}:${PORT} is down"
    # exit with error code
    exit 1
fi

echo
echo "Host: $HOST has ${RESULT}"
echo
echo "do important stuff"
echo ""
echo "browsing $HOST:/volume1/data"
echo "mounted using '/etc/systemd/system/a-nfs-data.automount' unit"
echo "-= content of share =-"
echo ls /a/nfs/data | head -n 4
# shellcheck disable=SC2012
ls /a/nfs/data | head -n 4

The result from the script

09:11:33 ○ [fh@tiger] ~/temp
 $ bash test.sh

2049/tcp open  nfs

do important stuff

browsing nas.net.nix.dk:/volume1/data
mounted using '/etc/systemd/system/a-nfs-data.automount' unit
ls /a/nfs/data
-= content of share =-
 1.mp4
 2.mp4
 3.mp4
 4.mp4