#! /bin/sh
# This file is "/etc/ha.d/resources.d/enbd-primary". It should be on
# both secondary and primary in the failover pair.

MD=/dev/md0
MNT=/mnt
MNTOPTIONS=""
#MNTOPTIONS="-o loop=/dev/loop0"
MNTTYPE="ext3"

SECONDARY="strauss"
SECONDARYPARTITION=/users/image

PRIMARY="mahler"
PRIMARYPARTITION=/dev/sda12

NBDID="one"


PATH=/sbin:/bin:/usr/sbin:/usr/bin;/usr/local/sbin:/usr/local/bin

# enbd primary - this version is for running on the enbd primary
# this is because when the enbd primary comes up after a failure, this is run
# as enbd-primary start and so must sync the array to the nbd disk

primaryside_client_start () {
        echo -n "Starting enbd-primary. Will sync to nbd disc"
           umount $MNT
           raidstop $MD # shouldn't be running anyway...

           # possibly reinsert module
           /etc/init.d/nbd start module

           # error out pending requests
           echo -n "0" > /proc/nbdinfo
           # wait for device to activate itself again
           sleep 10
           
           /etc/init.d/nbd restart client $NBDID

           sleep 2
           mkraid --really-force --dangerous-no-resync $MD
           raidsetfaulty $MD $PRIMARYPARTITION
           sleep 1
           raidstop $MD
           sleep 1
           raidstart $MD
           sleep 1
           raidhotadd $MD $PRIMARYPARTITION
           mount -t $MNTTYPE $MD $MNT $MNTOPTIONS
        echo "."
}

primaryside_client_stop () {
        echo -n "Stopping enbd-primary and umounting"
            umount $MNT
            raidstop $MD

            # kill primary
            /etc/init.d/nbd stop client $NBDID
            
            # error out pending requests on device
            echo -n "0" > /proc/nbdinfo
            sleep 10
            echo -n "0" > /proc/nbdinfo
        echo "."
}

# enbd primary - this version is for running on the actual enbd secondary 
# because it is run when the enbd primary comes up (as enbd-primary stop)

secondaryside_client_start () {
        echo -n "Stopping enbd-secondary, mounting locally"
           /etc/init.d/nbd stop server $NBDID
        
           mount $TYPE $SECONDARYPARTITION $MNT $MNTOPTIONS
           sleep 8
           
           # we're actually going to start nbd-secondary here to make
           # certain that it's running when the real primary comes back
           # up.
           
           /etc/init.d/nbd start server $NBDID

        echo "."
}

secondaryside_client_stop () {
        echo -n "Starting enbd-secondary, umount locally"
            umount $SECONDARYPARTITION

            /etc/init.d/nbd restart server $NBDID
        echo "."
}

case "$1" in
    start)
        case `uname -n` in
        $PRIMARY) primaryside_client_start ;;
        $SECONDARY) secondaryside_client_start ;;
        esac
    ;;

    stop)
        case `uname -n` in
        $PRIMARY) primaryside_client_stop ;;
        $SECONDARY) secondaryside_client_stop ;;
        esac
    ;;

    *)
        echo "Usage: $0 {start|stop}" >&2
        exit 1
    ;;
esac

true


