#!/bin/sh
#
# 
# LVM
#
# Description:	Manages an LVM volume as an HA resource
#
#
# Author:	Alan Robertson
# Support:	linux-ha@lists.linux-ha.org
# License:	GNU General Public License (GPL)
# Copyright:	(C) 2002 - 2005 International Business Machines, Inc.
#
#	This code significantly inspired by the LVM resource
#	in FailSafe by Lars Marowsky-Bree
#
#
# An example usage in /etc/ha.d/haresources: 
#       node1  10.0.0.170 ServeRAID::1::1 LVM::myvolname
#
# See usage() function below for more details...
#
#	  OCF parameters are as below:
#		OCF_RESKEY_volgrpname
#		
#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

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


usage() {
  methods=`LVM_methods`
  methods=`echo $methods | tr ' ' '|'`
  cat <<EOF
	usage: $0 $methods

	$0 manages an  Linux Volume Manager volume (LVM) as an HA resource

	The 'start' operation brings the given volume online
	The 'stop' operation takes the given volume offline
	The 'status' operation reports whether the volume is available
	The 'monitor' operation reports whether the volume seems present
	The 'validate-all' operation checks whether the OCF parameters are valid
	The 'meta-data' operation show meta data 
	The 'methods' operation reports on the methods $0 supports

EOF
}

meta_data() {
	cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="LVM">
<version>1.0</version>

<longdesc lang="en">
Resource script for LVM. It manages an  Linux Volume Manager volume (LVM) 
as an HA resource. 
</longdesc>
<shortdesc lang="en">Controls the availability of an LVM Volume Group</shortdesc>

<parameters>
<parameter name="volgrpname" unique="0" required="1">
<longdesc lang="en">
The name of volume group.
</longdesc>
<shortdesc lang="en">Volume group name</shortdesc>
<content type="string" default="" />
</parameter>
<parameter name="exclusive" unique="0" required="0">
<longdesc lang="en">
If set, the volume group will be activated exclusively.
</longdesc>
<shortdesc lang="en">Exclusive activation</shortdesc>
<content type="boolean" default="false" />
</parameter>

<parameter name="partial_activation" unique="0" required="0">
<longdesc lang="en">
If set, the volume group will be activated even only partial of the physical
volumes available. It helps to set to true, when you are using mirroring
logical volumes.
</longdesc>
<shortdesc lang="en">Activate VG even with partial PV only</shortdesc>
<content type="string" default="false" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="30" />
<action name="stop" timeout="30" />
<action name="status" timeout="30" />
<action name="monitor" depth="0" timeout="30" interval="10" />
<action name="methods" timeout="5" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="5" />
</actions>
</resource-agent>
EOF
}

#
# methods: What methods/operations do we support?
#
LVM_methods() {
  cat <<EOF
	start
	stop
	status
	monitor
	methods
	validate-all
	meta-data
	usage
EOF
}

#
#	Return LVM status (silently)
#
LVM_status() {
  local rc=1
  loglevel="debug"

  # Set the log level of the error message
  if [ "X${2}" = "X" ]; then
	loglevel="err"
	if ocf_is_probe; then
	  loglevel="warn"
	else 
	  if [ ${OP_METHOD} = "stop" ]; then
	    loglevel="info"
	  fi
	fi
  fi
  
  if [ -d /dev/$1 ]; then
	test "`cd /dev/$1 && ls`" != ""
	rc=$?
	if [ $rc -ne 0 ]; then
    	ocf_log err "VG $1 with no logical volumes is not supported by this RA!"
	fi
  fi

  if [ $rc -ne 0 ]; then
    	ocf_log $loglevel "LVM Volume $1 is not available (stopped)"
  fi

  if [ "X${2}" = "X" ]; then
	# status call return
  	return $rc
  fi

  # Report on LVM volume status to stdout...
  if [ $rc -eq 0 ]; then
	echo "Volume $1 is available (running)"
  else
	echo "Volume $1 is not available (stopped)"
  fi
  return $rc
}

#
#	Monitor the volume - does it really seem to be working?
#
#
LVM_monitor() {
  if
    LVM_status $1
  then
    : OK
  else
    ocf_log info "LVM Volume $1 is offline"
    return $OCF_NOT_RUNNING
  fi

  return $OCF_SUCCESS
}

#
#	Enable LVM volume
#
LVM_start() {
  local vgchange_options
  local active_mode

  # TODO: This MUST run vgimport as well

  ocf_log info "Activating volume group $1"

  if [ "$LVM_MAJOR" -eq "1" ]; then
	ocf_run vgscan $1
  else
	ocf_run vgscan
  fi

  active_mode="ly"
  if ocf_is_true "$OCF_RESKEY_exclusive" ; then
  	active_mode="ey"
  fi
  vgchange_options="-a $active_mode"

  if ocf_is_true "$OCF_RESKEY_partial_activation" ; then
	vgchange_options="$vgchange_options --partial"
  fi

  # for clones (clustered volume groups), we'll also have to force
  # monitoring, even if disabled in lvm.conf.
  if ocf_is_clone; then
	vgchange_options="$vgchange_options --monitor y"
  fi

  ocf_run vgchange $vgchange_options $1 || return $OCF_ERR_GENERIC

  if LVM_status $1; then
    : OK Volume $1 activated just fine!
    return $OCF_SUCCESS 
  else
    ocf_log err "LVM: $1 did not activate correctly"
    return $OCF_NOT_RUNNING
  fi
}

#
#	Disable the LVM volume
#
LVM_stop() {

  vgdisplay "$1" 2>&1 | grep 'Volume group .* not found' >/dev/null && {
    ocf_log info "Volume group $1 not found"
    return $OCF_SUCCESS
  }
  ocf_log info "Deactivating volume group $1"
  ocf_run vgchange -a ln $1 || return $OCF_ERR_GENERIC

  if
    LVM_status $1
  then
    ocf_log err "LVM: $1 did not stop correctly"
    return $OCF_ERR_GENERIC 
  fi

  # TODO: This MUST run vgexport as well

  return $OCF_SUCCESS
}

#
#	Check whether the OCF instance parameters are valid
#
LVM_validate_all() {
  check_binary $AWK

#	Off-the-shelf tests...  
  VGOUT=`vgck ${VOLUME} 2>&1`
  
  if [ $? -ne 0 ]; then
	ocf_log err "Volume group [$VOLUME] does not exist or contains error! ${VGOUT}"
	exit $OCF_ERR_GENERIC
  fi

#	Double-check
  if 
    [ "$LVM_MAJOR" -eq "1" ]
  then
	VGOUT=`vgdisplay ${VOLUME} 2>&1`
  else
	VGOUT=`vgdisplay -v ${VOLUME} 2>&1`
  fi

  if [ $? -ne 0 ]; then
	ocf_log err "Volume group [$VOLUME] does not exist or contains error! ${VGOUT}"
	exit $OCF_ERR_GENERIC
  fi

  return $OCF_SUCCESS
}
#
#	'main' starts here...
#

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

case $1 in
  meta-data)	meta_data
		exit $OCF_SUCCESS;;

  methods)	LVM_methods
		exit $?;;

  usage)	usage
		exit $OCF_SUCCESS;;
  *)		;;
esac

if 
  [ -z "$OCF_RESKEY_volgrpname" ]
then
  ocf_log err "You must identify the volume group name!"
  exit $OCF_ERR_CONFIGURED 
fi

# Get the LVM version number, for this to work we assume(thanks to panjiam):
# 
# LVM1 outputs like this
#
#	# vgchange --version
#	vgchange: Logical Volume Manager 1.0.3
#	Heinz Mauelshagen, Sistina Software  19/02/2002 (IOP 10)
#
# LVM2 and higher versions output in this format
#
#	# vgchange --version
#	LVM version:     2.00.15 (2004-04-19)
#	Library version: 1.00.09-ioctl (2004-03-31)
#	Driver version:  4.1.0

LVM_VERSION=`vgchange --version 2>&1 | \
	$AWK '/Logical Volume Manager/ {print $5"\n"; exit; }
	     /LVM version:/ {printf $3"\n"; exit;}'`
rc=$?

if
  ( [ $rc -ne 0 ] || [ -z "$LVM_VERSION" ] )
then
  ocf_log err "LVM: $1 could not determine LVM version. Try 'vgchange --version' manually and modify $0 ?"
  exit $OCF_ERR_INSTALLED
fi
LVM_MAJOR="${LVM_VERSION%%.*}"

VOLUME=$OCF_RESKEY_volgrpname
OP_METHOD=$1
# What kind of method was invoked?
case "$1" in

  start)	LVM_start $VOLUME
		exit $?;;

  stop)		LVM_stop $VOLUME
		exit $?;;

  status)	LVM_status $VOLUME $1
		exit $?;;

  monitor)	LVM_monitor $VOLUME
		exit $?;;

  validate-all)	LVM_validate_all
		;;

  *)		usage
		exit $OCF_ERR_UNIMPLEMENTED;;
esac
