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

NBD=/dev/nd/a
MD=/dev/md0

MNT=/mnt
MNTOPTIONS=""
FSTYPE="ext3"

PRIMARY="mahler"
SECONDARY="strauss"

NBDID="one"
SECONDARYPARTITION=/users/image

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

# enbd secondary - this version is for running on the enbd primary
# (yes, really)
# this is because when the enbd secondary goes down, the enbd primary has to cope
# and it is this script that heartbeat will run

primaryside_server_start () {
        echo -n "Starting enbd-secondary takeover"
            # remove NBD component from raid mirror
            raidsetfaulty $MD $NBD/0
            # shut down NBD primary daemon
            /etc/init.d/nbd stop client $NBDID
            # kill any lingering requests on the device
            echo -n "0" > /proc/nbdinfo
            sleep 10
            echo -n "0" > /proc/nbdinfo
        echo "."
}

primaryside_server_stop () {
        echo -n "Stopping enbd-secondary takeover"
           # kill lingering requests to the device
           echo -n "0" > /proc/nbdinfo
           sleep 10
           echo -n "0" > /proc/nbdinfo
           # remove the raid device from its mountpoint
           fuser -muk $MNT
           umount $MNT
           # stop the whole raid device
           raidstop $MD
           sleep 1
           # we need to make sure that the nbd-secondary is up
           # wait the 5s for the NBD device to reenable itself after
           # the echo 0.
           # for the moment just sleep for a while
           sleep 15
           /etc/init.d/nbd start client $NBDID
           sleep 2

           # restart the raid device, but without resync. We will
           # then mark the nbd device fault and reintegrate it, 
           # causing a resync over the net
           mkraid --really-force --dangerous-no-resync $MD
           raidsetfaulty $MD $NBD/0
           sleep 1
           raidstop $MD
           sleep 1
           raidstart $MD
           sleep 1
           raidhotadd $MD $NBD/0

           mount -t $FSTYPE $MD $MNT $MNTOPTIONS
        echo "."
}

# enbd secondary - this version is for running on the actual enbd secondary 
# it is run when the enbd secondary comes up.

secondaryside_server_start () {
        echo -n "Starting enbd-secondary"
            # we didn't have the partition. I don't see why we should
            # umount it!
            umount $SECONDARYPARTITION 
            /etc/init.d/nbd start module
            /etc/init.d/nbd restart server $NBDID
                  
        echo "."
}

secondaryside_server_stop () {
        echo -n "Stopping enbd-secondary"
           /etc/init.d/nbd stop server $NBDID
        echo "."
}

case "$1" in
    start)
        case  `uname -n` in
        $SECONDARY) secondaryside_server_start ;;
        $PRIMARY) primaryside_server_start ;;
        esac
    ;;

    stop)
        case  `uname -n` in
        $SECONDARY) secondaryside_server_stop ;;
        $PRIMARY) primaryside_server_stop ;;
        esac
    ;;

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

true


