Skip to content

Instantly share code, notes, and snippets.

@dj-mcculloch
Last active April 18, 2024 22:18
Show Gist options
  • Save dj-mcculloch/9e097535ea35df8e2ec1e6e32f7f73ac to your computer and use it in GitHub Desktop.
Save dj-mcculloch/9e097535ea35df8e2ec1e6e32f7f73ac to your computer and use it in GitHub Desktop.
autofs NFS Mount Executable Map with Wake-On-LAN and Path Validation
#!/bin/bash
# Author: dj-mcculloch
# Documentation: https://dj-does.medium.com/nfs-mounts-and-wake-on-lan-25c0c1d55c90
# Adaptation of rufflove's comment solution (https://www.blogger.com/profile/01096649615527099308) via tech-bodges'
# blog post from 2010 http://tech-bodges.blogspot.com/2010/06/linux-autofs-and-wake-on-lan-bodge.html as well as
# bryanchicken's post (https://forum.kodi.tv/showthread.php?tid=118164)
# {NFS_IP} is where you would put in your NFS' IPv4 address, e.g. the IP
# address of your NAS, 192.168.1.100
nfs_ipv4="{NFS_IP}"
# {NFS_MAC} is where you would put in your NFS' MAC address, e.g. the
# MAC address of your NAS, 00:11:aa:bb:cc:55
nfs_mac_address="{NFS_MAC}"
# {MOUNT_DIR} is the subdirectory created under the directory specified in auto.master where the NFS volume
# will be mounted
mount_subdirectory="{MOUNT_DIR}"
# {REMOTE_VOLUME_PATH} is the directory on the remote NFS server that you're mounting
remote_nfs_volume_path="{REMOTE_VOLUME_PATH}"
# Ping the NFS IP one time, with a timeout of one second, and send the
# output of the ping to /dev/null because we don't need it
ping -c 1 -w 1 -q $nfs_ipv4 > /dev/null
# If the result of ping, $?, is not equal to zero, then there was an error and
# we assume the error is because the target NFS is asleep; let's send a WOL
# packet
if [ $? -ne 0 ]; then
# Send the WOL Magic Packet
sudo /usr/sbin/etherwake $nfs_mac_address
# Try to reach the NFS volume for 120 seconds
start_time="$(date "+%s")"
until [ $(($(date "+%s")-$start_time)) -gt 120 ]; do
# Check the NFS port, 2049, to see if it is up
nc -zw 1 $nfs_ipv4 2049
# If the result of nc, $?, is not equal to zero, sleep for a second and then try again, otherwise break out of
# the loop
if [ $? -eq 0 ]; then break; else sleep 1; fi
done
fi
# Finally, echo out the parameters for mounting the NFS volume that
# auto.master expects
# The if-statement prevents you from creating and mounting to unintended sub-directories
if [ "$1" = "$mount_subdirectory" ]; then
echo -n -e "-fstype=nfs4,retry=0,timeo=50,hard,intr,tcp $nfs_ipv4:$remote_nfs_volume_path"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment