#!/bin/sh
#
# $Id: ServeRAID.in,v 1.8.2.1 2003/03/12 18:24:50 lars Exp $
# 
# ServeRAID
#
# Description:	Enables/Disables shared ServeRAID merge groups
#
# Author:	Alan Robertson, Renzo Alejandro Granados
#
# Support:	linux-ha-dev@lists.tummy.com
#
# License:	GNU Lesser General Public License (LGPL)
#
# Copyright:	(C) 2002 International Business Machines
#		(C) 2002 Renzo Alejandro Granados
#
# usage: ./ServeRAID <adapter> <MergeGroup#>  (start|stop|status)
#
#<adapter>:	Adapter number of the ServeRAID adapter 
#<MergeGroup#>:	MergeGroup # of the logical drive under consideration.
#
#
# The ServeRAID clustering model is a bit odd, and its terminology needs
# a little explanation
#
#	Logical Volume - a particular SCSI id {target id and LUN} on
#		a particular controller.
#
#	Merge Group - when active on one side or the other of the ServeRAID
#		configuration it corresponds with a logical drive.
#		Merge group numbers are permanently assigned to a particular
#		chunk of storage.  Shared merge groups are in the
#		range of 1 to 8, and are largely arbitrary.
#		Unshared merge groups start at 200.
#		We can only deal with shared merge groups.  When a merge
#		group is activated on one of the controllers, it becomes
#		a logical volume on that system.  NOTE:  The order in
#		which the Merge Groups are activated determines which
#		SCSI Ids they become.  This makes for extra headaches
#		for this script to deal with.  It also means that if
#		you have more than one shared ServeRAID merge group on
#		a particular controller, that the SCSI IDs will not
#		be constant.  This requires mounting by uuid or label.
#
#	One of the ServerRAID controllers has to be configured with
#	SCSI initiator ID 6, and the other with SCSI id 7.
#
#	At this time, the ServeRAID clustering solution only works with
#	RAID 1 setups.  It does NOT support RAID 5.
#
#
# An example usage in /etc/ha.d/haresources: 
#       node1  10.0.0.170 ServeRAID::1::1
#
unset LC_ALL; export LC_ALL
unset LANGUAGE; export LANGUAGE

#	Older ServeRAID utility returns 1 when it succeeds (weird)
#	BUT - the newly released version is more normal...

srsuccess=0

usage() {
  cat <<-! >&1
	usage: $0 <serveraid#> <sharedmergegroup#> (start|stop|status)

	The serveraid# is the number of the ServeRAID adapter
	ServeRAID adapters are numbered starting from 1.

	The shared merge group number is a number between 1 and 8 inclusive.
	It indicates to the controller which logical disk to fail over.

                node1  10.0.0.170 ServeRAID::1::1

	PREREQUISITES:
	You must configure your ServeRAID adapters for clustering for this
	to work.

	To do this, you must use the bootable "ServeRAID Support CD" and right
	click your controller and pick "configure for clustering".  The Linux
	version of the ServeRAID manager does not have the "configure for
	clustering" option.

	You will need at least version 5.10 (~July 2002 release) of the ipssend
	command for this script to work.

	$Id: ServeRAID.in,v 1.8.2.1 2003/03/12 18:24:50 lars Exp $
	!
  exit 1
}

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

ServeRAID_methods() {
  cat <<-!
	start
	stop
	status
	methods
	!
}

ServeRAIDSCSI="/proc/scsi/ips"

 
IPS=ipssend
proc_scsi=/proc/scsi/scsi


parseinst() {
  adapter=error
  mergegroup=error
  scsinumber=error
  if
    [ $# -ne 2 ]
  then
    echo "Invalid ServeRAID instance: $*" >&2
    return 1
  fi
  logicaldrivenumber=$1
  scsinumber=`expr $1 - 1`
  if
    [ ! -f $ServeRAIDSCSI/$scsinumber ]
  then
    echo "No such ServeRAID adapter: $1" >&2
    scsinumber=error
    return 2
  fi
    
  case $2 in
    [1-8]);;
    *)	echo "Invalid Shared Merge Group Number: $2" >&2
        scsinumber=error
	return 3;;
  esac
  adapter=$1
  mergegroup=$2
  return 0
}

#
#	Run:  Run a script, and log its output.
#	Note that we know the stupid ipssend command has its return
#	codes reversed - it returns 1 for success, and 0 for failure...
#	We account for that too...
#
run() {
  output=`"$@" 2>&1`
  rc=$?
  output=`echo $output`
  if
    [ $rc -eq $srsuccess ]
  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 1
  fi
}

MergeGroupToSCSI_ID() {

  PerlScript="while (<>) {
	/logical drive number *([0-9]+)/i && (\$ld=\$1);
	/part of merge group *: *$mergegroup /i && print \$ld - 1, \"\n\";
  }"

  ID=`$IPS getconfig $adapter ld | perl -e "$PerlScript"`
  case $ID in
    [0-9]*)	echo "$ID"; return 0;;
    *)		return 1;;
  esac
}

MergeGroupRaidLevel() {

  PerlScript="while (<>) {
	/RAID level *: *([0-9]+[A-Z]*)/i && (\$ld=\$1);
	/part of merge group *: *$mergegroup /i && print \$ld, \"\n\";
  }"

  Level=`$IPS getconfig $adapter ld | perl -e "$PerlScript"`
  case $Level in
    ?*)	echo "$Level"; return 0;;
    *)		return 1;;
  esac
}

ReleaseSCSI() {
  SID=`MergeGroupToSCSI_ID` &&
  echo "scsi-remove-single-device `expr $adapter - 1` 0 $SID 0" > $proc_scsi
}

AddSCSI() {
  SID=`MergeGroupToSCSI_ID` &&
  echo "scsi-add-single-device `expr $adapter - 1` 0 $SID 0" > $proc_scsi
}

#
# start: Enable the given ServeRAID device
#
ServeRAID_start() {
  parseinst "$@" || return $?
  if
    run $IPS UNMERGE $adapter $mergegroup
  then
    if
      #
      # Normally we do a MERGE PARTNER, but if we still own the drive for
      # some reason, then we'll need to do a MERGE OWN instead...
      #
      out=`$IPS MERGE $adapter $mergegroup PARTNER 2>&1`
      if
        [ $? -eq $srsuccess ]
      then
	echo "info: $out"
      else
        run $IPS MERGE $adapter $mergegroup OWN
      fi
    then
      : OK All is well!
      run $IPS SYNCH $adapter DRIVE $logicaldrivenumber &
      AddSCSI
    else
      return $?
    fi
  else
    return $?
  fi
  if
    ServeRAID_status "$@"
  then
    return 0
  else
    echo "ERROR: ServeRAID device $1 not active!"
    return 1
  fi
}


#
# stop: Disable the given ServeRAID device
#
ServeRAID_stop() {
  parseinst "$@" || return $?
  ReleaseSCSI
  if
    run $IPS UNMERGE $adapter $mergegroup
  then
    : UNMERGE $adapter $mergegroup worked
  fi
  if
    ServeRAID_status "$@"
  then
    echo "ERROR: ServeRAID device $@ is still active!"
    return 1
  else
    return 0
  fi
}


#
# status: is the given device now available?
#
ServeRAID_status() {
  parseinst "$@" || return $?
  #
  # The output we're looking for
  #	Part of merge group            : 2   
  #
  $IPS getconfig $adapter ld \
  |	grep -i "part of merge group[ 	]*: *$mergegroup *$" >/dev/null
}

if
  [ $# -eq 1 -a "X$1" = "Xmethods" ]
then
  ServeRAID_methods
  exit $?
fi

if
  [ $# -ne 3 ]
then
  usage
  exit 1
fi

: Right Number of arguments..

# Look for the start, stop, status, or methods calls...
case "$3" in
  stop)
	ServeRAID_stop $1 $2
	exit $?;;
  start)
	ServeRAID_start $1 $2
	exit $?;;
  status) 
	if
	  ServeRAID_status $1 $2
	then
	  echo ServeRAID merge group $1 $2 is running
	  exit 0
	else
	  echo ServeRAID merge group $1 $2 is stopped.
	  exit 1
	 fi
	exit $?;;

#
# methods: What methods do we support?
#
  methods) 
	ServeRAID_methods
	exit $?;;

*)
    usage
    exit 1;;

esac

exit 1
