#!/bin/sh

### BEGIN INIT INFO
# Provides:          global filesystem version 2
# Required-Start:    cman
# Required-Stop:     cman
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mount and unmount GFS v2 shares
### END INIT INFO

#
# 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)

# See how we were called.
case "$1" in
  start)
        if [ -n "$GFS2FSTAB" ] 
	then
		echo -n "Mounting GFS2 filesystems: "
		mount -a -t gfs2
		echo "done."
	fi
	;;

  stop)
  	if [ -n "$GFS2MTAB" ] 
	then
		sig=
		retry=6
		remaining=`LC_ALL=C awk '!/^#/ && $3 == "gfs2" && $2 != "/" {print $2}' /proc/mounts`
		while [ -n "$remaining" -a "$retry" -gt 0 ]
		do
			echo -n "Unmounting GFS2 filesystems: "
			umount -a -t gfs2

			if [ $retry -eq 0 ] 
			then
				echo "failed"
				echo -n "Unmounting GFS2 filesystems (lazy): "
				umount -l -a -t gfs2
				break
			fi

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

	modprobe -r lock_nolock 2>&1 || true
	modprobe -r lock_dlm 2>&1 || true
	modprobe -r gfs2 2>&1 || true
	;;

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

  restart)
	$0 stop
	$0 start
	;;

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

exit 0
