Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created July 17, 2012 15:33
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 dansimau/3130131 to your computer and use it in GitHub Desktop.
Save dansimau/3130131 to your computer and use it in GitHub Desktop.
Bash script to add psuedo-remount functionality to NFS mounts in Linux
#!/bin/bash
#
# Remount a filesystem with new options by unmounting it first (with umount)
# and mounting it again.
#
# Used for filesystems that do not support the -o remount option (eg. NFS).
#
# Compatible with Linux/bash only.
#
# 2012-07-17
# daniel.simmons@tobias.tv
#
# Get -o mount options
while getopts ":o:" opt; do
case $opt in
o)
mountoptions="$mountoptions $OPTARG"
;;
esac
done
shift $(($OPTIND-1))
mountpoint="$@"
# Show usage
if [ $# -lt 1 -o -z "$mountpoint" ]; then
echo "Unmounts and re-mounts a mount point with new options. Useful when the" >&2
echo "filesystem doesn't support remounting (such as NFS)." >&2
echo >&2
echo "Usage: $0 [-o options] <mount point>" >&2
echo >&2
echo "NOTE: Existing mount options are not preserved." >&2
exit 99
fi
# Iterate over current mount points to find the one that the user has specified
while read mount; do
# Parse mount arguments
set -- $mount
src=$1
target=$2
if [ "$target" == "$mountpoint" ]; then
# Unmount
umount "$target" || exit
# Mount again with the new options
mount -o $mountoptions "$src" "$target" || {
# A feeble attempt to re-mount successfully if the mount failed
# (in case the user specified invalid options)
mount "$src" "$target" &>/dev/null || echo "Unable to restore mount" >&2
exit 1
}
# Success!
exit 0
fi
done </proc/mounts
echo "Could not find $mountpoint in /proc/mounts." >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment