2015-07-01 11:32:35 +03:00
#!/bin/sh
# This is an example CTDB NFS callout script for Ganesha. It is based
# on the last version of 60.ganesha shipped with CTDB. As such, it
# does not try to monitor RPC services that were not monitored by
# 60.ganesha - this might be a useful improvement. It has also not
# been properly tested.
# You should check your version of NFS Ganesha to see if it ships with
# a newer callout.
# To use this:
#
2017-02-22 12:37:14 +03:00
# * Set CTDB_NFS_CALLOUT in your CTDB configuration to point to (a
# copy of) this script, making sure it is executable.
2015-07-01 11:32:35 +03:00
#
2017-02-22 12:37:14 +03:00
# * Create a new directory alongside the nfs-checks.d directory, for
# example nfs-checks-ganesha.d. Install 20.nfs-ganesha.check in
# this directory. Symlink to any other check files from
# nfs-checks.d that should still be used, such as
# 00.portmapper.check. Set CTDB_NFS_CHECKS_DIR to point to this new
# directory of check files.
2016-05-04 21:03:29 +03:00
#
# * It is recommended, but not required, to install the grace_period
# script (usually shipped in a utils package for NFS-Ganesha) to
# /usr/bin/grace_period
2015-07-01 11:32:35 +03:00
# I (Martin Schwenke) hereby relicense all of my contributions to this
# callout (and, previously, to 60.ganesha) to a license compatible
# with NFS Ganesha (right now this is LGPLv3, but I'm flexible).
# There may be other contributions to be considered for relicensing,
# particularly those in commit 28cbe527d47822f870e8252495ab2a1c8fddd12f.
######################################################################
# Exit on 1st error
set -e
2016-04-29 05:18:05 +03:00
# Filesystem type and mount point for the (typically clustered)
# volume that will contain the NFS-Ganesha state.
state_fs="${CTDB_NFS_STATE_FS_TYPE:-gpfs}"
state_dir="${CTDB_NFS_STATE_MNT}" # No sane default.
2016-05-10 17:50:10 +03:00
# To change the following, edit the default values below. Do not set
# these - they aren't configuration variables, just hooks for testing.
nfs_exports_file="${CTDB_NFS_EXPORTS_FILE:-/etc/ganesha/ganesha.conf}"
nfs_service="${CTDB_NFS_SERVICE:-nfs-ganesha}"
ganesha_rec_subdir=${CTDB_GANESHA_REC_SUBDIR:-.ganesha}
procfs=${PROCFS_PATH:-/proc}
2017-02-22 06:38:45 +03:00
case "$state_fs" in
2017-02-22 06:25:55 +03:00
gpfs)
GANRECDIR="/var/lib/nfs/ganesha"
;;
glusterfs)
if [ -z "${state_dir}" ]; then
echo "CTDB_NFS_STATE_MNT not defined for GlusterFS"
exit 1
fi
2017-02-22 06:38:45 +03:00
host=$(hostname)
2017-02-22 06:25:55 +03:00
NODESTATEDIR="$state_dir/nfs-ganesha/$host"
GANSTATEDIR="$state_dir/nfs-ganesha/.noderefs"
NODESTATELN="$GANSTATEDIR/$host"
;;
2016-05-03 09:53:07 +03:00
esac
2015-07-01 11:32:35 +03:00
##################################################
usage ()
{
2017-02-22 06:38:45 +03:00
_c=$(basename "$0")
2017-02-22 06:25:55 +03:00
cat <<EOF
2015-07-01 11:32:35 +03:00
usage: $_c { shutdown | startup }
$_c { stop | start | check } nfs
$_c { releaseip | takeip }
$_c { monitor-list-shares }
EOF
exit 1
}
##################################################
# Basic service stop and start
basic_stop ()
{
2017-02-22 06:25:55 +03:00
case "$1" in
2015-07-01 11:32:35 +03:00
nfs)
2017-02-22 06:25:55 +03:00
service "$nfs_service" stop
;;
2015-07-01 11:32:35 +03:00
*)
2017-02-22 06:25:55 +03:00
usage
esac
2015-07-01 11:32:35 +03:00
}
basic_start ()
{
2017-02-22 06:25:55 +03:00
case "$1" in
2015-07-01 11:32:35 +03:00
nfs)
2017-02-22 06:25:55 +03:00
service "$nfs_service" start
;;
2015-07-01 11:32:35 +03:00
*)
2017-02-22 06:25:55 +03:00
usage
esac
2015-07-01 11:32:35 +03:00
}
##################################################
# "stop" and "start" options for restarting
service_stop ()
{
case "$1" in
nfs)
basic_stop "nfs"
;;
nlockmgr)
# Do nothing - used by statd-callout
:
;;
*)
usage
esac
}
service_start ()
{
2017-02-22 06:25:55 +03:00
case "$1" in
2015-07-01 11:32:35 +03:00
nfs)
2017-02-22 06:25:55 +03:00
basic_start "nfs"
;;
2015-07-01 11:32:35 +03:00
nlockmgr)
2017-02-22 06:25:55 +03:00
# Do nothing - used by statd-callout
:
;;
2015-07-01 11:32:35 +03:00
*)
2017-02-22 06:25:55 +03:00
usage
esac
2015-07-01 11:32:35 +03:00
}
##################################################
# Nitty gritty - monitoring and IP handling
2016-05-04 22:16:27 +03:00
# Check that a symlink exists, create it otherwise.
# Usage: check_ln <TARGET> <LINK>
check_ln ()
{
2017-02-22 06:25:55 +03:00
if [ ! -L "${2}" ] ; then
rm -vrf "${2}"
else
_t=$(readlink "${2}")
if [ "$_t" != "${1}" ] ; then
rm -v "${2}"
fi
fi
# This is not an "else". It also re-creates the link if it was
# removed above!
if [ ! -e "${2}" ]; then
ln -sfv "${1}" "${2}"
fi
2016-05-04 22:16:27 +03:00
}
2016-04-29 04:58:30 +03:00
# Return 'active' if the shared filesystem is accessible.
2015-07-01 11:32:35 +03:00
get_cluster_fs_state ()
{
2017-02-22 06:25:55 +03:00
case $state_fs in
gpfs)
/usr/lpp/mmfs/bin/mmgetstate | awk 'NR == 4 { print $3 }'
;;
glusterfs)
# Since we're past create_ganesha_recdirs(), we're active.
echo "active"
;;
*)
echo "File system $state_fs not supported"
exit 1
;;
esac
2015-07-01 11:32:35 +03:00
}
create_ganesha_recdirs ()
{
2017-02-22 06:38:45 +03:00
if ! _mounts=$(mount | grep "$state_fs"); then
2017-02-22 06:25:55 +03:00
echo "Failed to find mounts of type $state_fs"
exit 1
fi
if [ -z "$_mounts" ]; then
echo "startup $state_fs not ready"
exit 0
fi
case $state_fs in
gpfs)
_mntpt=$(echo "$_mounts" | sort | awk 'NR == 1 {print $3}')
_link_dst="${_mntpt}/${ganesha_rec_subdir}"
mkdir -vp "$_link_dst"
check_ln "$_link_dst" "$GANRECDIR"
;;
glusterfs)
[ -d /var/lib/nfs.backup ] || \
mv /var/lib/nfs /var/lib/nfs.backup
2017-02-22 06:38:45 +03:00
check_ln "$NODESTATEDIR" /var/lib/nfs
2017-02-22 06:25:55 +03:00
2017-02-22 06:38:45 +03:00
mkdir -p "${NODESTATEDIR}/ganesha/v4recov"
mkdir -p "${NODESTATEDIR}/ganesha/v4old"
mkdir -p "${NODESTATEDIR}/statd/sm"
mkdir -p "${NODESTATEDIR}/statd/sm.bak"
touch "${NODESTATEDIR}/state"
touch "${NODESTATEDIR}/statd/state"
2017-02-22 06:25:55 +03:00
2017-02-22 06:38:45 +03:00
mkdir -p "$GANSTATEDIR"
check_ln "$NODESTATEDIR" "$NODESTATELN"
2017-02-22 06:44:09 +03:00
for _dir in "${GANSTATEDIR}/"* ; do
# Handle no directories case
if [ ! -d "$_dir" ] ; then
break
fi
_node="${_dir##*/}" # basename
if [ "${_node}" != "${host}" ]; then
check_ln "${GANSTATEDIR}/${_node}/ganesha" \
"${NODESTATEDIR}/ganesha/${_node}"
check_ln "${GANSTATEDIR}/${_node}/statd" \
"${NODESTATEDIR}/statd/${_node}"
2017-02-22 06:25:55 +03:00
fi
done
;;
esac
2015-07-01 11:32:35 +03:00
}
service_check ()
{
2017-02-22 06:25:55 +03:00
create_ganesha_recdirs
# Always succeed if cluster filesystem is not active
_cluster_fs_state=$(get_cluster_fs_state)
2017-02-22 06:38:45 +03:00
if [ "$_cluster_fs_state" != "active" ] ; then
2017-02-22 06:25:55 +03:00
return 0
fi
# Check that NFS Ganesha is running, according to PID file
_pidfile="/var/run/ganesha.pid"
_ganesha="/usr/bin/ganesha.nfsd"
2017-02-22 06:38:45 +03:00
if ! { read -r _pid < "$_pidfile" && \
2017-02-22 06:25:55 +03:00
grep "$_ganesha" "${procfs}/${_pid}/cmdline" ; } >/dev/null 2>&1
then
echo "ERROR: NFS Ganesha not running according to PID file"
return 1
fi
2015-07-01 11:32:35 +03:00
2016-04-29 04:58:30 +03:00
return 0
2015-07-01 11:32:35 +03:00
}
#-------------------------------------------------
nfs_releaseip ()
{
2017-02-22 06:25:55 +03:00
if [ -x "/usr/bin/grace_period" ]; then
/usr/bin/grace_period "2:${2}"
else
dbus-send --print-reply --system --dest=org.ganesha.nfsd \
/org/ganesha/nfsd/admin org.ganesha.nfsd.admin.grace \
string:"2:${2}"
fi
2015-07-01 11:32:35 +03:00
}
nfs_takeip ()
{
2017-02-22 06:25:55 +03:00
case $state_fs in
glusterfs)
2017-02-22 06:38:45 +03:00
check_ln "$NODESTATEDIR" "${GANSTATEDIR}/${2}"
2017-02-22 06:25:55 +03:00
;;
esac
if [ -x "/usr/bin/grace_period" ]; then
/usr/bin/grace_period "5:${2}"
else
dbus-send --print-reply --system --dest=org.ganesha.nfsd \
/org/ganesha/nfsd/admin org.ganesha.nfsd.admin.grace \
string:"5:${2}"
fi
2015-07-01 11:32:35 +03:00
}
##################################################
# service init startup and final shutdown
nfs_shutdown ()
{
2017-02-22 06:25:55 +03:00
basic_stop "nfs"
2015-07-01 11:32:35 +03:00
}
nfs_startup ()
{
2017-02-22 06:25:55 +03:00
basic_stop "nfs" || true
2016-04-29 04:58:30 +03:00
2017-02-22 06:25:55 +03:00
create_ganesha_recdirs
2015-07-01 11:32:35 +03:00
2017-02-22 06:25:55 +03:00
basic_start "nfs"
_f="${procfs}/sys/net/ipv4/tcp_tw_recycle"
2020-04-21 14:47:01 +03:00
if [ -f "$_f" ] ; then
2017-02-22 06:25:55 +03:00
echo 1 >"$_f"
fi
2015-07-01 11:32:35 +03:00
}
##################################################
# list share directories
nfs_monitor_list_shares ()
{
2017-02-22 06:38:45 +03:00
grep Path "$nfs_exports_file" |
2017-02-22 06:25:55 +03:00
cut -f2 -d\" |
sort -u
2015-07-01 11:32:35 +03:00
}
##################################################
2016-04-29 05:12:33 +03:00
nfs_register ()
{
2017-02-22 06:25:55 +03:00
cat <<EOF
2016-04-29 05:12:33 +03:00
shutdown
startup
stop
start
2018-01-31 09:07:46 +03:00
check
2016-04-29 05:12:33 +03:00
releaseip
takeip
monitor-list-shares
EOF
}
##################################################
2015-07-01 11:32:35 +03:00
action="$1"
shift
case "$action" in
2017-02-22 06:25:55 +03:00
shutdown) nfs_shutdown ;;
startup) nfs_startup ;;
stop) service_stop "$1" ;;
start) service_start "$1" ;;
2018-01-31 09:07:46 +03:00
check) service_check "$1" ;;
2017-02-22 06:25:55 +03:00
releaseip) nfs_releaseip "$@" ;;
takeip) nfs_takeip "$@" ;;
monitor-list-shares) nfs_monitor_list_shares ;;
register) nfs_register ;;
2018-01-31 09:07:46 +03:00
monitor-pre|monitor-post|releaseip-pre|takeip-pre)
2015-07-01 11:32:35 +03:00
# Not required/implemented
:
;;
2017-02-22 06:25:55 +03:00
*)
2015-07-01 11:32:35 +03:00
usage
esac