#!/bin/bash
#
# gfs2 mount/unmount helper
#
# chkconfig: - 26 74
# description: mount/unmount gfs2 filesystems configured in /etc/fstab

### BEGIN INIT INFO
# Provides:		gfs2
# Required-Start:	$network cman
# Required-Stop:	$network cman
# Should-Start:		clvmd
# Should-Stop:		clvmd
# Default-Start:
# Default-Stop:
# Short-Description:	mount/unmount gfs2 filesystems configured in /etc/fstab
# Description:		mount/unmount gfs2 filesystems configured in /etc/fstab
### END INIT INFO

# set secure PATH
PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/sbin"

### generic wrapper functions

success()
{
	gprintf "[  OK  ]\r\n"
}

failure()
{
	gprintf "[FAILED]\r\n"
}

ok() {
	success
	echo
}

nok() {
	gprintf "%s\n" "$errmsg"
	failure
	echo
	exit 1
}

# rpm based distros
if [ -d /etc/sysconfig ]; then
	[ -f /etc/init.d/functions ] && . /etc/init.d/functions
	[ -f /etc/sysconfig/cluster ] && . /etc/sysconfig/cluster
	[ -f /etc/sysconfig/gfs2 ] && . /etc/sysconfig/gfs2
	[ -z "$LOCK_FILE" ] && LOCK_FILE="/var/lock/subsys/gfs2"
fi

# deb based distros
if [ -d /etc/default ]; then
	[ -f /etc/default/cluster ] && . /etc/default/cluster
	[ -f /etc/default/gfs2 ] && . /etc/default/gfs2
	[ -z "$LOCK_FILE" ] && LOCK_FILE="/var/lock/gfs2"
fi

# proc is required for both status and stop.
# start could live without, but better be consistent with the behavior
if [ ! -f /proc/mounts ]; then
	gprintf "GFS2: /proc is not available, unable to proceed\n"
	exit 1
fi

#
# This script's behavior is modeled closely after the netfs script.  
#
GFS2FSTAB=$(LC_ALL=C awk '!/^#/ && $3 == "gfs2" && $4 !~ /noauto/ { print $2 }' /etc/fstab)
GFS2MTAB=$(LC_ALL=C awk '!/^#/ && $3 == "gfs2" && $2 != "/" { print $2 }' /proc/mounts)

if [ -z "$GFS2FSTAB" ]; then
	gprintf "GFS2: no entries found in /etc/fstab\n"
	exit 6
fi

if [ "$EUID" != "0" ]; then
	gprintf "Only root can execute %s script\n" "$0"
	exit 4
fi

is_mounted()
{
	for i in $GFS2MTAB; do
		if [ "$1" = "$i" ]; then
			return 0
		fi
	done
	return 1
}

# See how we were called.
case "$1" in
start)
	mounted=0
	for fs in $GFS2FSTAB; do
		gprintf "Mounting GFS2 filesystem (%s): " "$fs"
		if is_mounted $fs; then
			gprintf "already mounted"
			ok
			mounted=$((mounted + 1))
			continue
		fi
		errmsg="$(mount $fs 2>&1)"
		if [ "$?" != 0 ]; then
			gprintf "%s\n" "$errmsg"
			failure
			echo
			continue
		fi
		ok
		mounted=$((mounted + 1))
	done
	[ $mounted -gt 0 ] && touch $LOCK_FILE
;;
stop)
	[ -z "$GFS2MTAB" ] && exit 0
	umount_failed=0
	for fs in $GFS2MTAB; do
		gprintf "Unmounting GFS2 filesystem (%s): " "$fs"
		errmsg="$(umount $fs 2>&1)"
		if [ "$?" != 0 ]; then
			gprintf "%s\n" "$errmsg"
			failure
			echo
			umount_failed=1
			continue
		fi
		ok
	done
	modprobe -r gfs2 > /dev/null 2>&1 || true
	[ $umount_failed = 0 ] && rm -f $LOCK_FILE
	;;

status)
	if [ -z "$GFS2MTAB" ] && [ -f $LOCK_FILE ]; then
		gprintf "GFS2: Found stale lock file %s\n" "$LOCK_FILE"
		exit 2
	fi

	if [ -n "$GFS2FSTAB" ] && [ -z "$GFS2MTAB" ]; then
		gprintf "GFS2: service is not running\n"
		exit 3
	fi

	gprintf "Configured GFS2 mountpoints: \n"
	for fs in $GFS2FSTAB; do
		echo $fs;
	done

	gprintf "Active GFS2 mountpoints: \n"
	for fs in $GFS2MTAB; do
		echo $fs;
	done
;;
condrestart|try-restart)
	$0 status >/dev/null 2>&1 || exit 0
	$0 restart
	exit $?
;;
restart|reload|force-reload)
	$0 stop && $0 start
	exit $?
;;
*)
	gprintf "Usage: %s {start|stop|restart|reload|force-reload|condrestart|try-restart|status}\n" "$0"
	exit 2
;;
esac

exit 0
