#!/bin/bash
#
# chkconfig: 345 25 75
# description: start/stop persistent reservation service for lvm

. /etc/init.d/functions

[ -f /etc/sysconfig/scsi_reserve ] && . /etc/sysconfig/scsi_reserve

# check for sg_persist command provided by sg3_utils package
#
if ! sg_persist -V &> /dev/null ; then
   echo "error: sg_persist not found"
   exit 2
fi

# check for gethostip command provided by syslinux package
#
if ! gethostip -h &> /dev/null ; then
   echo "error: gethostip not found"
   exit 3
fi

# get scsi devices that are part of clustered volumes
#
scsi_devices=$( lvs -o vg_attr,devices --noheadings \
		| awk --posix ' $1 ~ /[-a-z]{5}c/ { print $2 } ' \
		| sed -e 's/([0-9]*)//' | sort | uniq )

# if no scsi devices were found we can exit now
#
[ -z "$scsi_devices" ] && exit 0

# get the node name and node addr from cman
#
node_name=$( cman_tool status | grep "Node name" | awk -F": " '{ print $2 }' )
node_addr=$( cman_tool status | grep "Node addr" | awk -F": " '{ print $2 }' )

# create unique key for this host
#
key=$( gethostip -x $node_name )

###############################################################################

case $1 in

start)

rval=0

echo "$key" > /var/lock/subsys/scsi_reserve

cat /dev/null > /var/run/scsi_reserve

# register each device using our key
#
for dev in $scsi_devices
do

  echo -n "Registering device: $dev"

  for error in 1
  do
    sg_persist -d $dev -o -G -S $key &>/dev/null || break
    error=0
  done

  if [ $error -eq 0 ]; then
      echo $dev >> /var/run/scsi_reserve
      success "register of device $dev"
  else
      # perhaps we are already resgistered
      #
      if sg_persist -d $dev -i -k 2>/dev/null | grep -qiE "${key#0}" ; then
	  echo $dev >> /var/run/scsi_reserve
          success "register device $dev"
      else
          failure
	  rval=1
      fi
  fi

  echo

  # create a reservation
  #
  sg_persist -d $dev -o -R -K $key -T 5 &>/dev/null

done

# start the watchdog if configured and registration was successful
#
if [ "$rval" == 0 ] && [ "${WATCHDOG}" == yes ] ; then
    echo -n "Starting watchdog: "
    if watchdog -c /etc/cluster/scsi_watchdog.conf ; then
	success "watchdog start"
    else
	failure "watchdog start"
    fi
    echo
fi
;;

stop)

rval=0

# stop the watchdog before we unregister
#
if [ "${WATCHDOG}" == yes ]; then
    echo -n "Stopping watchdog: "

    if killproc watchdog ; then
	success "watchdog stop"
    else
	failure "watchdog stop"
    fi
    echo
fi

# unregister each device for this node
#
for dev in $scsi_devices
do
  echo -n "Unregistering device: $dev"

  # get list of keys registered for this device
  #
  reg_keys=$( sg_persist -d $dev -i -k | grep '^[[:space:]]*0x' )

  # check if this node/key is the node/key holding the reservation
  #
  if sg_persist -d $dev -i -r 2>/dev/null | grep -qiE "${key#0}" ; then
      if echo "$reg_keys" | grep -qivE "${key#0}" ; then
	  error=1
      else
	  for error in 1
	  do
	    sg_persist -d $dev -o -G -K $key -S 0 &>/dev/null || break
	    error=0
	  done
      fi
  else
      for error in 1
      do
	sg_persist -d $dev -o -G -K $key -S 0 &>/dev/null || break
	error=0
      done
  fi

  if [ $error -eq 0 ]; then
      success "unregister device $dev"
  else
      failure
      rval=1
  fi

  echo
done

rm -f /var/lock/subsys/scsi_reserve

;;

status)

rval=0

# find devices that are registered with our key
#
for dev in $scsi_devices
do
  if sg_persist -d $dev -i -k 2>/dev/null | grep -qiE "${key#0}" ; then
      devices[${#devices[@]}]=$dev
  fi
done

if [ -z "$devices" ]; then
    echo "No devices resgistered."
else
    echo "Found ${#devices[@]} registered device(s):"

    for i in "${devices[@]}" ; do
	echo $i
    done
fi
;;

esac

exit $rval

