#!/bin/sh
# Copyright (c) 2005 Oracle
# All rights reserved.
#
# chkconfig:	2345 25 19
# description:	Mount OCFS2 volumes at boot.
#

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/ocfs2 ] && . /etc/sysconfig/ocfs2

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down OCFS2
	exit 1
	fi
else
	exit 0
fi

success_status()
{
    success
    echo
}

failure_status()
{
    failure $1
    echo
}

ocfs2mounts()
{
    LC_ALL=C awk '$3 == "ocfs2"  { print $2 }' /proc/mounts
}

ocfs2fstab()
{
    LC_ALL=C awk '!/^#/ && $3 == "ocfs2" && $4 !~ /noauto/ { print $2 }' /etc/fstab
}

RETVAL=0

case "$1" in
    start|reload)
    	if [ ! -f /var/lock/subsys/ocfs2 ]; then
	        if [ -n "`ocfs2fstab`" ] ; then
		    run_cmd "Mounting OCFS2 filesystems" mount -a -t ocfs2
		    touch /var/lock/subsys/ocfs2
	fi
	else
		msg_already_running "OCFS2 filesystems"
	fi
        ;;
    stop)
        show "Oracle Cluster File System (OCFS2) "
	busy
        remaining="`ocfs2mounts`"
        sig=
        retry=3
        while [ -n "$remaining" -a "$retry" -gt 0 ]
        do
            if [ "$retry" -lt 3 ]; then
                echo -n "Retry stopping Oracle Cluster File System (OCFS2) "
            fi
            umount -a -t ocfs2 > /dev/null 2>&1
            sleep 1

            remaining="`ocfs2mounts`"
            [ -z "$remaining" ] && break
            failure_status "Unable to unmount OCFS2 filesystems"

            /sbin/fuser -k -m $sig $remaining
            sleep 5
            retry=$(($retry - 1))
            sig=-9
        done
        [ -z "$remaining" ] && ok
	rm -f /var/lock/subsys/ocfs2
        ;;
    restart|force-reload)
        $0 stop
        $0 start
        ;;
    status)
        if [ -f /proc/mounts ] ; then
            [ -n "`ocfs2fstab`" ] && {
                echo "Configured OCFS2 mountpoints: " `ocfs2fstab`
            }

            [ -n "`ocfs2mounts`" ] && {
                echo "Active OCFS2 mountpoints: " `ocfs2mounts`
            }
        else
            echo -n "Checking OCFS2 mountpoints: "
            failure_status
        fi
        ;;
    try-restart|condrestart)
        $0 status
        if test $? = 0; then
            $0 restart
        fi
        ;;
    *)
        msg_usage "$0 {start|stop|status|reload|force-reload|restart|try-restart}"
        exit 3
esac

exit $RETVAL
