#!/bin/sh
#
# $Id: ICP.in,v 1.2 2002/04/16 14:02:32 lars Exp $
# 
# ICP
#
# Description:	Manages an ICP Vortex clustered host drive as an HA resource
#
#
# Author:	Lars Marowsky-Bree <lmb@suse.de>
# Support:	linux-ha-dev@lists.tummy.com
# License:	GNU General Public License (GPL)
# Copyright:	(C) 2002 SuSE Linux AG
#
#
# An example usage in /etc/ha.d/haresources: 
#       node1  10.0.0.170 LinuxSCSI::0:0 ICP::c0h1::/dev/sdb1 LVM::myvolname
#
# Notice that you will need to get the utility "icpclucon" from the ICP
# support to use this.
#
# See usage() function below for more details...
#

prefix=/usr
exec_prefix=/usr
#. /etc/ha.d/shellfuncs
. /etc/ha.d/shellfuncs

#
ICPCLUCON=/usr/sbin/icpclucon
#
BLOCKDEV=/sbin/blockdev
#
unset LC_ALL; export LC_ALL
unset LANGUAGE; export LANGUAGE

usage() {
  methods=`ICP_methods | grep -v methods`
  methods=`echo $methods | tr ' ' '|'`
  cat <<-! >&1
	usage: $0 <ICP cluster drive ID> <Device> ($methods)
	usage: $0 methods

	$0 manages an ICP Vortex clustered host drive.

	The 'start' operation reserves the given host drive.
	The 'stop' operation releses the given host drive.
	The 'status' operation reports whether the host drive is reserved.
	The 'monitor' operation reports whether the host drive is reserved.
	The 'methods' operation reports on the methods $0 supports

	$Id: ICP.in,v 1.2 2002/04/16 14:02:32 lars Exp $
	!
  exit 1

}

#
#	run:  run a script, and log its output.
#
run() {
  output=`"$@" 2>&1`
  rc=$?
  output=`echo $output`
  if
    [ $rc -eq 0 ]
  then 
    if
      [ ! -z "$output" ]
    then
      ha_log "info: $output"
    fi
    return 0
  else
    if
      [ ! -z "$output" ]
    then
      ha_log "ERROR: $output"
    else
      ha_log "ERROR: command failed: $*"
    fi
    return $rc
  fi
}



#
# methods: What methods/operations do we support?
#
ICP_methods() {
  cat <<-!
	start
	stop
	status
	monitor
	methods
	!
}

ICP_status() {
        local icp_out
        
        icp_out=$($ICPCLUCON -v -status $1)
        if [ $? -ne 0 ]; then
                ha_log "Hostdrive not reserved by us."
                return 1
        fi

        if expr match "$icp_out" \
                '.*Drive is reserved by this host.*' >/dev/null 2>&1 ; then
                ha_log "Volume $1 is reserved by us."
                return 0
        elif expr match "$icp_out" \
                '.*Drive is not reserved by any host.*' >/dev/null 2>&1 ; then
                ha_log "Volume $1 not reserved by any host."
                return 1
        else
                ha_log "Unknown output from icpclucon. Assuming we do not have a reservation:"
		ha_log "$icp_out"
                return 1
        fi
}

ICP_report_status() {
  if ICP_status $1 ; then
	echo "$1: running"
	return 0
  else
	echo "$1: not running"
	return 1
  fi
}


#
#	Monitor the host drive - does it really seem to be working?
#
#
ICP_monitor() {

  if
    ICP_status $1
  then
    : OK
  else
    ha_log "ERROR: ICP host drive $1 is offline"
    return 1
  fi

}

Clear_bufs() {
    $BLOCKDEV --flushbufs $2
}

#
#	Enable ICP host drive
#
ICP_start() {

  ha_log "Activating host drive $1"
  run $ICPCLUCON -v -reserve $1
  if [ $? -ne 0 ]; then
  	ha_log "Forcing reservation of $1"
	run $ICPCLUCON -v -force $1 || return 1
  fi

  if
    ICP_status $1
  then
    : OK
    # A reservation isn't as prompt as it should be
    sleep 3
    return 0
  else
    ha_log "ERROR: ICP: $1 was not reserved correctly"
    return 1
  fi
}

#
#	Release the ICP host drive
#
ICP_stop() {

  ha_log "Releasing ICP host drive $1"
  run $ICPCLUCON -v -release $1 || return 1

  ha_log "Verifying reservation"
  if ICP_status $1 ; then
    ha_log "ERROR: ICP: $1 was not released correctly"
    return 1
  fi
  return 0
}


#
#	'main' starts here...
#

if
  [ $# -eq 1 -a "methods" = "$1" ]
then
  ICP_methods
  exit $?
fi

# What kind of method was invoked?
case "$3" in

  start)	ICP_start $1
		Clear_bufs $2
		exit $?;;

  stop)		ICP_stop $1
		Clear_bufs $2
		exit $?;;

  status)	ICP_report_status $1
		exit $?;;

  monitor)	ICP_monitor $1
		exit $?;;

  methods)	ICP_methods
		exit $?;;
esac

usage
