Get nfs client to ignore server when server is offline

Have nfs share setup to automount in a laptop client. The server occasionally goes offline, and when it does, the laptop cannot reboot or shutdown (it seems to get stuck looking for the share). Are there mount options that tell the client to ignore missing share and proceed to reboot or shutdown cleanly, without having to manually umount the share.

The automount is performed by systemd mount and automount unit files. these are the relevant mount options currently used:

Options=vers=4,_netdev,rw,suid,users,async,nofail,
LazUnmount=true
ForceUnmount=true

@lak , welcome.

check your documentation, there's likely to be a 'timeout' option available ?

Amend to something like
Options=...,nofail,timeout=20

suggest you take a look at :
Ubuntu Manpage: systemd.mount - Mount unit configuration mentions x-systemd.mount-timeout

@munkeHoller, thankyou

neglected to include in opening post. TimeoutSec=1 and TimeoutIdleSec=60 is already set in their respective mount and automount unit files.

np.
still, recommend you read the documentation links posted regarding the systemd.mount-timeout and the section wrt FSTAB

thankyou. indeed i have gone over that page several times, that is where the current options came from.

can you supply the systemd unit mount file for the volume ?
is the mount in /etc/fstab ? if so, can you copy/paste the entry.
are there any (systemd) dependencies on the volume being mounted before continuing.

If you were asked the question - what details would you expect to be given ?

the more details you give the less questions need asking :smile: and better the chance of .. a solution / questions that need asking

anonymise any sensitive details prior to posting

sure. they are automount and mount units not fstab as stated in opening post. for
simplicity sake, the location of the export directory on the server is the same as the mount point of the client.

[Unit]
Description=Automount NFS share

[Automount]
Where=/mnt/Data
TimeoutIdleSec=60

[Install]
WantedBy=multi-user.target
[Unit]
Description=Mount NFS Share

[Mount]
What=server
Where=/mnt/Data
Type=nfs
Options=vers=4,_netdev,rw,suid,users,async,nofail,
TimeoutSec=1
LazyUnmount=true
ForceUnmount=true

[Install]
WantedBy=multi-user.target

hmm, nothing blazing obvious here, although ....

TimeoutIdleSec=
Configures an idle timeout. Once the mount has been idle for the specified time, systemd will attempt to unmount. Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass 0 to disable the timeout logic. The timeout is disabled by default.

perhaps removing that may have some influence on behaviour.

The time-outs are for the mount.
Once mounted the tcp layer dictates them.

The normal safe umount attempt is to first kill accessing processes and then umount without informing the server.

fuser -k /mnt/Data
fuser -k -9 /mnt/Data
umount -f /mnt/Data

This might work, depending on the kernel version and depending on the state of a current access. Some fuser implementations do a stat() and/or statfs() that gets hung itself...

Linux has got the "lazy" umount
umount -l /mnt/Data
that succeeds in nearly all access states, but leaves some dead resources in RAM. But it's perfect if run before a reboot/shutdown!

Last but not least, the mount options.
There is the classic soft mount option that after a time-out sets all accessing file handles to "I/O error". Linux has got some alternate "soft" options that you can try. See
man nfs
or
https://man7.org/linux/man-pages/man5/nfs.5.html

that is there, to my knowledge, so that i do not waste network bandwidth when not assessing the share.

thanks.

are you suggesting putting

fuser -k /mnt/Data
fuser -k -9 /mnt/Data
umount -f /mnt/Data

in a script that another systemd service unit can run prior to reboot/shutdown? i had been considering that earlier. was hoping that some mount option that i had overlooked could do the job without complicating the matter.

i will try out soft option, all my files are relatively small, they should not get lost in transit.

update: the soft option does not do it. next i will look at alternate soft options.

Put it in a script, followed by the
umount -l /mnt/Data
and run the script on the command line when the nfs server is not reachable.
In case the fuser are hung for a long time, disable them, and rerun the script with the remaining umount commands.
If even the umount -f is hung for a long time then disable that, too.

in the time i took to make the suggested script, i realized that using systemd automount/mount + a script to mimic what autofs does in other distros (broken/unmaintained in my laptop clearlinux) is sorely inadequate.

i asked bing chat ai to write a script that

  • mounts the share when i start laptop;
  • monitors my server;
  • mounts the share if server is online;
  • clean up the stale mount if server is offline (so nothing hangs when i shutdown or reboot laptop);
  • remount the share if server comes online again;
  • unmounts share after amount of time of no activity;
  • unmounts share prior to reboot/shutdown

i have systemd service to start script.

on the surface, the script seems to be working, but i have not test all scenerios.

one problem i noticed so far is when i have the share open in nautilus file manager, it goes offline after a certain time - due to inactivity? i want it to unmount only if i am not accessing the share ie: i do not have the share folder open.

i am not very computer literate, hoping extra sets of eyes can help:

#!/bin/bash

# Define the mount point and the remote server details
MOUNT_POINT="/mnt/data"
REMOTE_SERVER="server"
REMOTE_PATH="/data"
INACTIVE_TIME=300

# Function to check if the remote server is online
function is_server_online {
    ping -c 1 $REMOTE_SERVER > /dev/null 2>&1
    return $?
}

# Function to clean up stale mount point
function cleanup_stale_mount {
    # Check if the mount point is already mounted
    if mountpoint -q $MOUNT_POINT; then
        # Unmount the stale mount point
        sudo umount -f -l $MOUNT_POINT

        # Check if the unmount was successful
        if [ $? -eq 0 ]; then
            echo "Successfully unmounted stale mount point $MOUNT_POINT"
        else
            echo "Failed to unmount stale mount point $MOUNT_POINT"
        fi
    fi
}

# Function to unmount the share when not in use
function unmount_share {
    # Check if the mount point is already mounted
    if mountpoint -q $MOUNT_POINT; then
        # Check if any processes are accessing the mount point
        if fuser -m $MOUNT_POINT > /dev/null 2>&1; then
            echo "$MOUNT_POINT is in use, not unmounting."
            sleep $INACTIVE_TIME && unmount_share &
        else
            # Unmount the share
            sudo umount $MOUNT_POINT

            # Check if the unmount was successful
            if [ $? -eq 0 ]; then
                echo "Successfully unmounted $MOUNT_POINT"
            else
                echo "Failed to unmount $MOUNT_POINT"
            fi
        fi
    fi
}

# Function to mount the share when server is online and share is not mounted.
function mount_share {
    # Check if the remote server is online and mount point is not already mounted.
    if is_server_online && ! mountpoint -q $MOUNT_POINT; then

        # Create the mount point if it doesn't exist.
        if [ ! -d $MOUNT_POINT ]; then
            mkdir -p $MOUNT_POINT
        fi

        # Mount the remote path to the mount point.
        sudo mount -t nfs $REMOTE_SERVER:$REMOTE_PATH $MOUNT_POINT

        # Check if the mount was successful.
        if [ $? -eq 0 ]; then
            echo "Successfully mounted $REMOTE_SERVER:$REMOTE_PATH to $MOUNT_POINT"
            sleep $INACTIVE_TIME && unmount_share &
        else
            echo "Failed to mount $REMOTE_SERVER:$REMOTE_PATH to $MOUNT_POINT"
        fi

    elif ! is_server_online; then 
      cleanup_stale_mount 
    fi
    
    sleep 10 && mount_share &
}

# Start monitoring for mounting and unmounting.
mount_share &

# Function to handle shutdown and reboot signals.
function shutdown {
  cleanup_stale_mount 
  exit 0 
}

trap shutdown SIGINT SIGTERM SIGKILL

# Keep script running until shutdown or reboot signal is received.
while true; do sleep 1; done;

Hmm, it is all plausible.
I suspect the background things, because you never know when they are finished.
One is a umount attempt after 300 seconds, but what is this:
sleep 10 && mount_share &
This is within the mount_share function - does it call itself after 10 seconds??

Regarding the combined umount -f -l I have no experience. I have always used umount then umount -f (can leave resources behind on the NFS server) then umount -l (can leave local resources behind).

this is how ai explains the issue with inactivity even though the share is mounted to nautilus:

If you have a Nautilus window open on the mount point directory but are not actively working with any of the files, the fuser command may or may not detect this as activity, depending on how Nautilus interacts with the file system. If Nautilus keeps an open file handle on the directory or any of its files, the fuser command will detect this as activity and the script will not unmount the share. If Nautilus does not keep any open file handles, the fuser command will not detect any activity and the script will unmount the share after the specified period of inactivity.

regarding sleep 10, it is suppose to ping the server every 10sec.

i am not quite there by a long shot, there is alot to mull over. also, i cannot edit the title anymore to better reflect what i need, which is basically autofs mimicry.

The "call myself after X seconds" only works for a long time if the calling process terminates before - otherwise shell processes will pile up.

Did you try the soft option?
And consider the sync option that reduces speed but also the risk of data corruption (not fully written).

sudo mount -t nfs -o soft,sync ...

not sure exactly how to monitor shell processes, but if this helps, htop looks normal even after a few hours of clicking in and out of the share.

tried the soft option previously with the mount unit file, the system (laptop) was still hanging at reboot and shutdown when the server had gone offline.

i will start new post to better reflect what i want nfs client to do.