#!/bin/sh
# $Id$
#
# Script:	S20ext2
#
# Description:	RSF-1 failover script for regular filesystems
#
# Platform:	Linux
#
# Author:	High-Availability.Com Ltd
#

. /opt/HAC/bin/rsf.sh

#
# EDIT
#
service=${RSF_SERVICE:-"unknown"}	# service name
disks="/FS1 /FS2"			# list of filesystems to mount

nfs_mounts=""	# local NFS (auto)mounts to be unmounted on shutdown

script="`basename $0`"
state="$1"
attempts=${2:-'1'}

# give up after three restarts
if	[ ${attempts} -gt 3 ]; then
	dated_echo "Too many retries, aborting!"
	exit ${COMPANY_ABORT}
fi

case "${state}" in
'start')
	dated_echo "Checking and mounting filesystems."

	for fs in ${disks}
	do
		# get device name
		device=`awk '!/^#/ { if ($2==fs) print $1 }' fs=${fs} /etc/fstab`
		fsckpass=`awk '!/^#/ { if ($2==fs) print $6 }' fs=${fs} /etc/fstab`
		# optional, to flush buffers (required for shared SCSI configurations)
		if blockdev --flushbufs "${device}" >/dev/null 2>&1
		then
		    :
		else
		    echo "blockdev: command not found - please install blockdev"
		fi

		dated_echo "Checking ${device} ..."

		if [ "${device}" = "" ]
		then
			${_echo} " failed!"
			dated_echo "\t  *** WARNING - cannot locate fstab entry for ${fs} ***"
			dated_echo "\t  *** Cannot check or mount it!                      ***"
			dated_echo "Aborting ${state}"
			exit ${COMPANY_ABORT}
		else
			if [ "${fsckpass}" -ne 0 -a "${fsckpass}" != "" ]; then
				fsck -p ${device} >/dev/null 2>&1
	
				if [ $? -gt 1 ]; then
					# failed
					${_echo} "failed!"
					dated_echo "*** ERROR - Unable to repair the ${fs} filesystem ***"
					dated_echo "Run fsck manually"
					dated_echo "Aborting ${state}"
					exit ${COMPANY_ABORT}
				else
					echo "done."
				fi
			fi

			dated_echo "Mounting ${fs} ... \c"
			mountout=`mount ${fs} 2>&1`
			case $? in
			0)
				${_echo} "done."
				;;
			1)
				${_echo} "problem mounting ${fs}."
				dated_echo "Mount message was '${mountout}'"
				exit ${COMPANY_ABORT}
				;;
			32)
				${_echo} "Problem mounting (already mounted?), proceeding."
				dated_echo "Mount message was '${mountout}'"
				;;
			*)
				${_echo} "failed!"
				dated_echo "Failed to mount ${fs}, restarting service"
				dated_echo "Mount message was '${mountout}'"
				exit ${COMPANY_RESTART}
				;;
			esac
		fi
	done

	dated_echo "Re-exporting filesystems."
	exportfs >/dev/null 2>&1

	exit ${COMPANY_OK}
	;;

'stop')
	dated_echo "Unmounting filesystems."
	for fs in ${nfs_mounts} ${disks}
	do
		device=`awk '!/^#/ { if ($2==fs) print $1 }' fs=${fs} /etc/fstab`
		if blockdev --flushbufs "${device}" >/dev/null 2>&1
		then
		    :
		else
		    echo "blockdev: command not found - please install blockdev"
		fi

		dated_echo "Unmounting ${fs} ... \c"
		umountout=`umount ${fs} 2>&1`
	
		if [ $? -eq 0 ]
		then
			${_echo} " done."
		else
			${_echo} " failed, attempting kill ... \c"
			fuser -s -m -k ${fs} >/dev/null 2>&1
			fuser -s -k ${fs} >/dev/null 2>&1
			sleep 3

			# -r will try remount read-only on failure
			umountout=`umount -r ${fs} 2>&1`
			if [ $? -eq 0 ]
			then
				# check for ro remount
				touch ${fs}/.check$$ >/dev/null 2>&1
				if [ $? -ne 0 ]; then
					echo " failed!"
					dated_echo "*** WARNING - ${fs} remounted read-only ***"
					dated_echo "*** Please check!                  ***"
					dated_echo "Aborting"
					exit ${COMPANY_ABORT}
				else
					rm -f ${fs}/.check$$
					${_echo} " done."
				fi
			else
				${_echo} " failed!"
				dated_echo "*** WARNING - cannot unmount ${fs} ***"
				dated_echo "Umount output '${umountout}'"
				dated_echo "*** Please check!                  ***"
				dated_echo "Aborting"
				exit ${COMPANY_ABORT}
			fi
		fi
	done

	dated_echo "Re-exporting filesystems."
	exportfs >/dev/null 2>&1

	exit ${COMPANY_OK}
	;;

*)	${_echo} "Usage: ${script} <start|stop>"
	exit ${COMPANY_WARN}
	;;

esac
