Skip to content

Instantly share code, notes, and snippets.

@ilikenwf
Created November 30, 2020 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilikenwf/b4792750b4eaf01dce5ef78519b373cf to your computer and use it in GitHub Desktop.
Save ilikenwf/b4792750b4eaf01dce5ef78519b373cf to your computer and use it in GitHub Desktop.
auto_share from archwiki - modified to support both nfs and smb
#!/bin/bash
function net_umount {
umount -l -f $1 &>/dev/null
}
function net_mount {
mountpoint -q $1 || mount $1
}
function do_mount {
printf %s "$1" | while IFS= read -r line
do
SERVER=$(echo $line | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}")
MOUNT_POINT=$(echo $line | cut -f2 -d" ")
# Check if server already tested
if [[ "${server_ok[@]}" =~ "${SERVER}" ]]; then
# The server is up, make sure the share are mounted
net_mount $MOUNT_POINT
elif [[ "${server_notok[@]}" =~ "${SERVER}" ]]; then
# The server could not be reached, unmount the share
net_umount $MOUNT_POINT
else
# Check if the server is reachable
ping -c 1 "${SERVER}" &>/dev/null
if [ $? -ne 0 ]; then
server_notok[${#server_notok[@]}]=$SERVER
# The server could not be reached, unmount the share
net_umount $MOUNT_POINT
else
server_ok[${#server_ok[@]}]=$SERVER
# The server is up, make sure the share are mounted
net_mount $MOUNT_POINT
fi
fi
done
}
NFS_MOUNTS=$(sed -e '/^.*#/d' -e '/^.*:/!d' -e 's/\t/ /g' /etc/fstab | tr -s " ")$'\n'b
do_mount "$NFS_MOUNTS"
SMB_MOUNTS=$(sed -e '/^.*#/d' -e '/^.*\/\//!d' -e 's/\t/ /g' /etc/fstab | tr -s " ")$'\n'b
do_mount "$SMB_MOUNTS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment