more stable nfs mount

This commit is contained in:
lelo 2025-12-22 13:33:06 +00:00
parent 4f9cac6f08
commit b8713fcc7e

View File

@ -24,6 +24,15 @@ if ! command -v jq >/dev/null 2>&1; then
exit 1
fi
# Ensure an NFS mount helper is available before doing any work.
if ! command -v mount.nfs >/dev/null 2>&1 && \
! command -v mount.nfs4 >/dev/null 2>&1 && \
[ ! -x /sbin/mount.nfs ] && [ ! -x /usr/sbin/mount.nfs ]; then
echo "[ERROR] NFS client utilities are missing (mount.nfs/mount.nfs4 not found)."
echo "[ERROR] Install the 'nfs-common' package on this host and re-run the script."
exit 1
fi
# extract the server names at the top level:
SERVERS=($(jq -r 'keys[]' "$CONFIG_FILE"))
@ -39,7 +48,9 @@ is_tunnel_active() {
# Check if the given mount point is currently mounted
is_nfs_mounted() {
local mount_point=$1
mount | grep -q "${mount_point}"
local fstype
fstype=$(findmnt -rn -T "${mount_point}" -o FSTYPE 2>/dev/null)
[[ "${fstype}" == nfs* ]]
}
# Check if the mount point directory is accessible (i.e. can be listed)
@ -48,6 +59,33 @@ is_mount_accessible() {
ls -1 "${mount_point}" >/dev/null 2>&1
}
# Run the NFS mount command and verify it really succeeded.
mount_nfs_share() {
local mount_point=$1
local nfs_share=$2
local local_port=$3
local mount_opts="ro,port=${local_port},nolock,soft,timeo=5,retrans=3"
local mount_output
if ! mount_output=$(sudo mount -t nfs -o "${mount_opts}" 127.0.0.1:"${nfs_share}" "${mount_point}" 2>&1); then
echo "[ERROR] Failed to mount ${nfs_share} at ${mount_point}: ${mount_output}"
return 1
fi
if ! is_nfs_mounted "${mount_point}"; then
echo "[ERROR] Mount command returned success but ${mount_point} is not an active NFS mount."
echo "[DEBUG] Mount output: ${mount_output}"
return 1
fi
if ! is_mount_accessible "${mount_point}"; then
echo "[ERROR] ${mount_point} is mounted but not accessible (check tunnel/NFS server)."
return 1
fi
echo "[SUCCESS] NFS share mounted successfully at ${mount_point}."
}
###############################################################################
# Main Loop: Process Each Server and Its Mount Points
###############################################################################
@ -104,25 +142,22 @@ for server in "${SERVERS[@]}"; do
if is_nfs_mounted "${MOUNT_POINT}"; then
if ! is_mount_accessible "${MOUNT_POINT}"; then
echo "[WARNING] Mount point ${MOUNT_POINT} is not accessible. Attempting to remount..."
sudo umount "${MOUNT_POINT}"
sleep 2
sudo mount -t nfs -o ro,port="${LOCAL_PORT}",nolock,soft,timeo=5,retrans=3 127.0.0.1:"${NFS_SHARE}" "${MOUNT_POINT}"
if is_mount_accessible "${MOUNT_POINT}"; then
echo "[SUCCESS] Remounted successfully and folder is now accessible."
if sudo umount "${MOUNT_POINT}"; then
sleep 2
if mount_nfs_share "${MOUNT_POINT}" "${NFS_SHARE}" "${LOCAL_PORT}"; then
echo "[SUCCESS] Remounted successfully and folder is now accessible."
else
echo "[ERROR] Remount failed, folder still not accessible."
fi
else
echo "[ERROR] Remount failed, folder still not accessible."
echo "[ERROR] Failed to unmount ${MOUNT_POINT} during remount attempt."
fi
else
echo "[INFO] NFS share is mounted and accessible at ${MOUNT_POINT}."
fi
else
echo "[INFO] NFS share is not mounted at ${MOUNT_POINT}. Attempting to mount..."
sudo mount -t nfs -o ro,port="${LOCAL_PORT}",nolock,soft,timeo=5,retrans=3 127.0.0.1:"${NFS_SHARE}" "${MOUNT_POINT}"
if is_mount_accessible "${MOUNT_POINT}"; then
echo "[SUCCESS] NFS share mounted successfully at ${MOUNT_POINT}."
else
echo "[ERROR] Failed to mount NFS share ${NFS_SHARE} at ${MOUNT_POINT} or folder not accessible!"
fi
mount_nfs_share "${MOUNT_POINT}" "${NFS_SHARE}" "${LOCAL_PORT}"
fi
done