#!/bin/sh

# Usage: block-drbd [bind resource |unbind node]
#
# The resource argument to the bind command is the resource from the /etc/drbd.conf
# config file we are to make primary.
# Important: Because drbd does not tell us the device node, you have to setup the
# drbd config so that the resource name and the device nodes name match. We print
# the path to the drbd device node to stdout.
#
# Example:
# resource drbd0 {
#   on alf {
#     device /dev/drbd0 # /dev/drbd0 matches resource drbd0
#     disk /dev/hda
#   }
#   ...
# }
#
# The node argument to unbind is the name of the device node we are to
# unbind.
#
# This assumes you're running a correctly configured drbd!

case $1 in
        bind)
                if drbdadm primary $2; then
                        echo "/dev/$2"
                        exit 0
                fi
                exit 1
        ;;

        unbind)
                r=`echo $2 | sed -e 's/\/dev\///'`
                drbdadm secondary $r
                exit $?
        ;;

        *)
                echo 'Unknown command: ' $1
                echo 'Valid commands are: bind, unbind'
                exit 1
esac