#!/bin/sh
#
# Copyright (C) 2007-2009 Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# This file is part of LVM2.
# It is required for the proper handling of failures of LVM2 mirror
# devices that were created using the -m option of lvcreate.
#
#
# chkconfig: 12345 02 99
# description: Starts and stops dmeventd monitoring for lvm2
#
# For Red-Hat-based distributions such as Fedora, RHEL, CentOS.
#	       
### BEGIN INIT INFO
# Provides: lvm2-monitor
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 1 2 3 4 5
# Default-Stop: 0 6
# Short-Description: Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling
### END INIT INFO

. /etc/init.d/functions

DAEMON="lvm2-monitor"
DMEVENTD_DAEMON="dmeventd"

VGCHANGE="/sbin/vgchange"
VGS="/sbin/vgs"
LVS="/sbin/lvs"

LOCK_FILE="/var/lock/subsys/$DAEMON"
PID_FILE="/var/run/dmeventd.pid"

WARN=1
export LVM_SUPPRESS_LOCKING_FAILURE_MESSAGES=1

start()
{
	local config="'log{command_names=0 prefix=\"  \"}'"
	# TODO do we want to separate out already active groups only?
	VGSLIST=`eval $VGS --noheadings -o name --ignoreskippedcluster --config $config 2> /dev/null`
	for vg in $VGSLIST
	do
		run_cmd "Starting monitoring for LVM VG $vg" $VGCHANGE --monitor y --poll y --ignoreskippedcluster --config $config $vg
	done
}


stop()
{
	local config="'log{command_names=0 prefix=\"  \"}'"
	# TODO do we want to separate out already active groups only?
	if test "$WARN" = "1"; then
	   echo "Not stopping monitoring, this is a dangerous operation. Please use force-stop to override."
	   return 1
	fi
	VGSLIST=`eval $VGS --noheadings -o name --ignoreskippedcluster --config ${config} 2> /dev/null`
	for vg in $VGSLIST
	do
		run_cmd "Stopping monitoring for LVM VG $vg" $VGCHANGE --monitor n --ignoreskippedcluster --config $config $vg
	done
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	status "$DMEVENTD_DAEMON" 1>/dev/null 2>&1 && exit 0
	start
	RETVAL=$?
	[ "$RETVAL" = 0 ] && touch "$LOCK_FILE"
	;;

  force-stop)
	status "$DMEVENTD_DAEMON" 1>/dev/null 2>&1 || exit 0
	WARN=0
	stop
	RETVAL=$?
	[ "$RETVAL" = 0 ] && rm -f "$LOCK_FILE"
	;;

  stop)
	status "$DMEVENTD_DAEMON" 1>/dev/null 2>&1 || exit 0
	test "$runlevel" = "0" && WARN=0
	test "$runlevel" = "6" && WARN=0
	stop
	RETVAL=$?
	[ "$RETVAL" = 0 ] && rm -f "$LOCK_FILE"
	;;

  restart)
	WARN=0
	stop
	RETVAL=$?
	[ "$RETVAL" = 0 ] && rm -f "$LOCK_FILE"
	start
	RETVAL=$?
	[ "$RETVAL" = 0 ] && touch "$LOCK_FILE"
	;;

  status)
	status "$DMEVENTD_DAEMON"
	RETVAL=$?
	[ "$RETVAL" = 0 ] && $LVS -S 'seg_monitor=monitored' -o lv_full_name,seg_monitor
	;;

  *)
	msg_usage "$0 {start|stop|restart|status|force-stop}"
	;;
esac

exit $RETVAL
