#!/bin/bash
#
# chkconfig: 345 70 08
# description: Loads and unloads the drbd module
#
# Copright 2001-2008 LINBIT Information Technologies
# Philipp Reisner, Lars Ellenberg
#
### BEGIN INIT INFO
# Provides: drbd
# Required-Start: $network $syslog sshd
# Required-Stop: $network $syslog sshd
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:    Control drbd resources.
### END INIT INFO

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

if [ -f $DEFAULTFILE ]; then
  . $DEFAULTFILE
fi

test -f $DRBDADM || exit 5

# we only use these two functions, define fallback versions of them ...
log_daemon_msg() { echo -n "${1:-}: ${2:-}"; }
log_end_msg() { echo "."; }
# ... and let the lsb override them, if it thinks it knows better.
if [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
fi

function assure_module_is_loaded
{
    [ -e "$PROC_DRBD" ] && return

    $MODPROBE -s drbd `$DRBDADM sh-mod-parms` $ADD_MOD_PARAM || {
	echo "Can not load the drbd module."$'\n'; exit 20
    }
    # tell klogd to reload module symbol information ...
    [ -e /var/run/klogd.pid ] && [ -x /sbin/klogd ] && /sbin/klogd -i
}

function adjust_with_progress
{
    IFS_O=$IFS
    NEWLINE='
'
    IFS=$NEWLINE
    local res

    COMMANDS=`$DRBDADM -d -n res adjust all` || exit 20
    echo -n "[ "

    for CMD in $COMMANDS; do
	case "$CMD" in
		res=*)		eval "$CMD";;
		*\ disk\ *)	echo -n "d($res) " ;;
		*\ syncer\ *)	echo -n "s($res) " ;;
		*\ net\ *)	echo -n "n($res) " ;;
		*)		echo ".. " ;;
	esac
	if ! eval "$CMD"; then
	    echo -e "\n[$res] cmd $CMD failed - continuing!\n "
	fi
    done
    echo -n "]"

    IFS=$IFS_O
}

drbd_pretty_status()
{
	local proc_drbd=$1
	# add resource names
	if ! type column &> /dev/null ||
	   ! type paste &> /dev/null ||
	   ! type join &> /dev/null ||
	   ! type sed &> /dev/null ||
	   ! type tr &> /dev/null
	then
		cat "$proc_drbd"
		return
	fi
	sed -e '2q' < "$proc_drbd"
	sed_script=$(
	   paste <(drbdadm sh-dev all | cat -n) \
		 <(drbdadm sh-resources| tr ' ' '\n')  |
	   sed -n -e 's/|/_/g' \
	       -e 's#^\( *[0-9]\+.\)/dev/drbd\([0-9]\+\).\(.*\)$#s|^ *\2:|\1\&\3|#p')

	p() {
		sed -e "1,2d" \
		      -e "$sed_script" \
		      -e '/^ *[0-9]\+: cs:Unconfigured/d;' \
		      -e 's/^\(.* cs:.*[^ ]\)   \([rs]...\)$/\1 - \2/g' \
		      -e 's/^\(.* \)cs:\([^ ]* \)st:\([^ ]* \)ds:\([^ ]*\)/\1\2\3\4/' \
		      -e 's/^\(.* \)cs:\([^ ]*\)$/\1\2/' \
		      -e 's/^ *[0-9]\+:/ x &??not-found??/;' \
		      -e '/^$/d;/ns:.*nr:.*dw:/d;/resync:/d;/act_log:/d;' \
		      -e 's/^\(.\[.*\)\(sync.ed:\)/... ... \2/;/^.finish:/d;' \
		      < "$proc_drbd" | tr -s '\t ' '  ' 
	}
	m() {
		join -1 2 -2 1 -o 1.1,2.2,2.3 \
			<(drbdadm sh-dev all | cat -n | sort -k2,2) \
			<(sort < /proc/mounts ) |
			sort -n | tr -s '\t ' '  ' | sed -e 's/^ *//'
	}
	# echo "=== p ==="
	# p
	# echo "=== m ==="
	# m
	# echo "========="
	# join -a1 <(p|sort) <(m|sort)
	# echo "========="
	(
	echo m:res cs st ds p mounted fstype
	join -a1 <(p|sort) <(m|sort) | cut -d' ' -f2-6,8- | sort -k1,1n -k2,2
	) | column -t
}

# Just in case drbdadm want to display any errors in the configuration
# file, or we need to ask the user about registering this installation
# at http://usage.drbd.org, we call drbdadm here without any IO
# redirection.
$DRBDADM sh-nop

case "$1" in
    start)
	log_daemon_msg "Starting DRBD resources"
	assure_module_is_loaded
	adjust_with_progress

	# make sure udev has time to create the device files
	for RESOURCE in `$DRBDADM sh-resources`; do
	    for DEVICE in `$DRBDADM sh-dev $RESOURCE`; do
		UDEV_TIMEOUT_LOCAL=$UDEV_TIMEOUT
		while [ ! -e $DEVICE ] && [ $UDEV_TIMEOUT_LOCAL -gt 0 ] ; do
		    sleep 1
		UDEV_TIMEOUT_LOCAL=$(( $UDEV_TIMEOUT_LOCAL-1 ))
		done
	    done
	done

	[ -d /var/lock/subsys ] && touch /var/lock/subsys/drbd	# for RedHat
	$DRBDADM wait-con-int # User interruptible version of wait-connect all

	$DRBDADM sh-b-pri all # Become primary if configured
	log_end_msg 0
	;;
    stop)
	log_daemon_msg "Stopping all DRBD resources"
	if [ -e $PROC_DRBD ] ; then
		$DRBDADM down all
		$RMMOD drbd
	fi
	[ -f /var/lock/subsys/drbd ] && rm /var/lock/subsys/drbd
	log_end_msg 0
	;;
    status)
	# NEEDS to be heartbeat friendly...
	# so: put some "OK" in the output.
	if [ -e $PROC_DRBD ]; then
	    echo "drbd driver loaded OK; device status:"
	    drbd_pretty_status $PROC_DRBD 2>/dev/null
	    exit 0
	else
	    echo >&2 "drbd not loaded"
	    exit 3
	fi
	;;
    reload)
	log_daemon_msg  "Reloading DRBD configuration"
	$DRBDADM adjust all
	log_end_msg 0
	;;
    restart|force-reload)
	log_daemon_msg "Restarting all DRBD resources"
	$DRBDADM down all
	$RMMOD drbd
	assure_module_is_loaded
	$DRBDADM up all
	log_end_msg 0
	;;
    *)
	echo "Usage: /etc/init.d/drbd {start|stop|status|reload|restart|force-reload}"
	exit 1
	;;
esac

exit 0
