Skip to content

Instantly share code, notes, and snippets.

@Low-power
Created November 27, 2017 18:54
Show Gist options
  • Save Low-power/749b97afea4c73b4459e56cf4365e2d8 to your computer and use it in GitHub Desktop.
Save Low-power/749b97afea4c73b4459e56cf4365e2d8 to your computer and use it in GitHub Desktop.
Simple mount helper program for mounting NFSv4 in GNU/Linux
#!/bin/sh
# Simple mount helper program for mounting NFSv4 in GNU/Linux
# Copyright 2015-2017 Rivoreo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
remote_share=
mount_point=
extended_options=
fs_type=nfs4
argv0="`basename \"$0\"`"
append_extended_options() {
[ -n "$extended_options" ] && extended_options="$extended_options,$1" || extended_options="$1"
}
print_usage() {
printf "Usage: %s [-o <nfs-options>] <remote-share> <mount-point>\\n" "$argv0" 1>&2
}
while [ $# -gt 0 ]; do
case "$1" in
-r)
append_extended_options ro
;;
-w)
append_extended_options rw
;;
-o)
if [ $# -lt 1 ]; then
echo "Option '-o' require an argument" 1>&2
exit 255
fi
shift
append_extended_options "$1"
;;
-h)
print_usage
exit 0
;;
-*)
printf "Unknown option '%s'\\n" "$1"
exit 255
;;
*)
if [ -z "$remote_share" ]; then
remote_share="$1"
elif [ -z "$mount_point" ]; then
mount_point="$1"
else
print_usage
exit 255
fi
;;
esac
shift
done
if [ -z "$remote_share" ] || [ -z "$mount_point" ]; then
print_usage
exit 255
fi
[ "$argv0" != mount.nfs4 ] && printf %s "$extended_options" | grep -Eq '(^|,)vers=[23]($|,)' && fs_type=nfs
if ! { printf %s "$extended_options" | grep -Eq "(^|,)addr="; }; then
server_addr="`printf %s \"$remote_share\" | sed -r 's/:.*//'`"
append_extended_options "addr=$server_addr"
fi
if ! { printf %s "$extended_options" | grep -Eq "(^|,)clientaddr="; }; then
client_addr="`hostname -i`" # XXX
append_extended_options "clientaddr=$client_addr"
fi
exec mount -i -t $fs_type -o "$extended_options" "$remote_share" "$mount_point"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment