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

### BEGIN INIT INFO
# Provides:		gfs
# Required-Start:	$network cman
# Required-Stop:	$null
# Default-Start:	2 3 5
# Default-Stop:
# Short-Description:	mount/unmount gfs filesystems configured in /etc/fstab
# Description:		mount/unmount gfs filesystems configured in /etc/fstab
### END INIT INFO

. /etc/rc.status

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

[ -f /etc/sysconfig/cluster ] && . /etc/sysconfig/cluster

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

# See how we were called.
case "$1" in
  start)
        if [ -n "$GFSFSTAB" ] 
	then
		action $"Mounting GFS filesystems: " mount -a -t gfs
		rc_status
	fi
	touch /var/lock/subsys/gfs
	;;

  stop)
  	if [ -n "$GFSMTAB" ] 
	then
		sig=
		retry=6
		remaining=`LC_ALL=C awk '!/^#/ && $3 == "gfs" && $2 != "/" {print $2}' /proc/mounts`
		while [ -n "$remaining" -a "$retry" -gt 0 ]
		do
			action $"Unmounting GFS filesystems: " umount -a -t gfs
			
			if [ $retry -eq 0 ] 
			then
				action $"Unmounting GFS filesystems (lazy): " umount -l -a -t gfs
				break
			fi

			sleep 2
			remaining=`LC_ALL=C awk '!/^#/ && $3 == "gfs" && $2 != "/" {print $2}' /proc/mounts`
			[ -z "$remaining" ] && break
			/sbin/fuser -k -m $sig $remaining >/dev/null
			sleep 10
			retry=$(($retry - 1))
			sig=-9
		done
	fi

	modprobe -r gfs
	rc_status
	rm -f /var/lock/subsys/gfs
	;;

  status)
	if [ -f /proc/mounts ]
	then
	        [ -n "$GFSFSTAB" ] && {
		     echo $"Configured GFS mountpoints: "
		     for fs in $GFSFSTAB; do echo $fs ; done
		}
		[ -n "$GFSMTAB" ] && {
                      echo $"Active GFS mountpoints: "
		      for fs in $GFSMTAB; do echo $fs ; done
		}
	else
		echo "/proc filesystem unavailable"
		rc_failed
	fi
	rc_status
	;;

  restart)
	$0 stop
	$0 start
	;;

  reload)
        $0 start
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|status}"
	exit 1
esac

rc_exit
