#!/sbin/runscript
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License, v2 or later
# $Header: /var/cvsroot/gentoo-x86/sys-cluster/drbd/files/drbd-0.7.rc,v 1.1 2006/05/20 14:00:44 xmerlin Exp $

opts="${opts} reload"

DRBDADM="/sbin/drbdadm"
PROC_DRBD="/proc/drbd"
MODPROBE="modprobe"
RMMOD="rmmod"
UDEV_TIMEOUT=10
ADD_MOD_PARAM=""

depend() {
	use logger
	before heartbeat
	after sshd
	need net
}


load_module() {
	if [ ! -f ${PROC_DRBD} ]; then
		${MODPROBE} -s drbd `${DRBDADM} sh-mod-parms` ${ADD_MOD_PARAM} &> /dev/null
	fi
	
	# make sure udev has time to create the device files
	RESOURCE=`${DRBDADM} sh-resources` || exit 20
	RESOURCE=${RESOURCE%%\ *}
	DEVICE=`${DRBDADM} sh-dev ${RESOURCE}` || exit 20
	
	while [ ! -e ${DEVICE} ] && [ ${UDEV_TIMEOUT} -gt 0 ] ; do
		sleep 1
		UDEV_TIMEOUT=$(( ${UDEV_TIMEOUT}-1 ))
	done	
}

start() {
	ebegin "Starting DRBD"
	load_module
	${DRBDADM} up all
	eend $ret
}

stop() {
	ebegin "Stopping DRBD"

	# Check for mounted drbd devices	
	if ! grep -q '^/dev/drbd' /proc/mounts &>/dev/null; then
		if [ -f ${PROC_DRBD} ]; then
			${DRBDADM} down all
			${RMMOD} drbd
		fi
		eend $?
	else
		einfo "drbd devices mounted, please umount them before trying to stop drbd!!!"
		eend 1
	fi
}

status() {
	# NEEDS to be heartbeat friendly...
	# so: put some "OK" in the output.
	ebegin "Status of DRBD"
	if [ -e ${PROC_DRBD} ]; then
	    echo "drbd driver OK; device status:"
	    cat "${PROC_DRBD}"
	    eend 0
	else
	    echo >&2 "drbd not loaded"
	    eend 3
	fi
}

reload() {
	ebegin "Reloading DRBD"
	${DRBDADM} adjust all
	eend $?
}
