I did practically everything described here (archive).
However, for automating the whole process with fstab on both client & server, this
provided important extra information, esp. about bind-mounting existing filesystems
into a dedicated NFS folder, and also about /etc/fstab
entries.
That's it really; it was much easier than I expected. I should have switched away from sshfs
earlier.
But I don't use unencrypted NFS (v3) for remote access, for that I still use sshfs
.
Automatic Mount Handling on a Wifi-connected Laptop
All this works perfectly with an ethernet connection on my desktop computer. But my laptop uses the same NFS set-up on a wireless connection, and whenever the home network is disconnected every application that accesses part of the NFS mount just hangs.
Again, the glorious ArchWiki has the solution. I opted for the NetworkManager dispatcher. Unfortunately, I had to adapt it quite a bit.
I...
- completely removed NFS mounts from /etc/fstab.
- rewrote the script to un/mount them directly.
- made it differentiate between pre-down
and down
events. I found out that manually disconnecting the Wifi triggers a down
event, and not a pre-down
event as one might expect (man NetworkManager
).
#!/bin/bash
# Find your connection UUID with "nmcli con show" in terminal.
# All NetworkManager connection types are supported: wireless, VPN, wired...
WANTED_CON_UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
if [[ "$CONNECTION_UUID" == "$WANTED_CON_UUID" ]]; then
# 2 array members make one mount, equivalent to the first 2 fields in an fstab entry
fs=("device1" "dir1" "device2" "dir2" .....) # mounts
# mount options (not umount)
opts=("-o" "option=value")
i=0
# Script parameter $1: NetworkManager connection name, not used
# Script parameter $2: dispatched event
case "$2" in
"up")
echo "$1 $2 - Regular Mount - ${fs[@]}"
while (( i<${#fs[@]} )); do mount -v -t nfs ${opts[@]} "${fs[$((i++))]}" "${fs[$((i++))]}"; done
;;
"pre-down"|"vpn-pre-down")
echo "$1 $2 - Regular Umount - ${fs[@]}"
while (( i<${#fs[@]} )); do umount -v "${fs[i]}"; ((i+=2)); done
;;
"down"|"vpn-down")
echo "$1 $2 - Forced Umount - ${fs[@]}"
while (( i<${#fs[@]} )); do umount -v -lf "${fs[i]}"; ((i+=2)); done
;;
esac
fi
Edit as required. Works immediately. You can test it with
journalctl -b -f -u NetworkManager-dispatcher.service
It's rather chatty - you can remove the -v
s from the mount/umount commands.