#!/bin/sh # # License: GNU General Public License (GPL) # Author: Martin Fick # Date: 04/19/07 # Origin: Hacked together from many other drbd and ocf scripts # # This script manages a drbd device # # It can make a drbd device primary or secondary # # Be sure to only allow this resource to run on the # two specific nodes where your drbd device is setup. # # usage: $0 {start|stop|status|monitor|meta-data} # # # OCF parameters are as below # OCF_RESKEY_drbd_resource # ####################################################################### # Initialization: # We use the STATUS_CODES from here . /usr/lib/heartbeat/ocf-shellfuncs USAGE="usage: $0 {start|stop|status|monitor|meta-data}"; ####################################################################### #HA_D=/etc/ha.d #. ${HA_D}/shellfuncs meta_data() { cat < 0.0 This script manages a drbd device It can make a drbd device primary or secondary OCF Resource Agent compliant drbd script. The drbd resource is a resource defined in /etc/drbd.conf drbd resource END exit $OCF_SUCCESS } ocf_logi() { # type msg # if [ "$1" != "err" ] ; then return ; fi shift echo "$@" >> /var/log/drbd.martin } drbd_reload() { drbd_stop || return drbd_start } drbd_stop() { # # Is the device already secondary? # # drbd_status # if [ $? = $OCF_NOT_RUNNING ]; then exit $OCF_NOT_RUNNING; fi stop="$($DRBDADM secondary $RES 2>&1)" drbd_status ; rc=$? if [ $rc = $OCF_NOT_RUNNING ]; then exit 0; fi ocf_logi err "$RES stop failed: ($rc)" ocf_logi err "$stop" return 1 } drbd_start() { drbd_status if [ $? = $OCF_SUCCESS ]; then return $OCF_SUCCESS; fi if is_drbd_enabled; then : OK else do_cmd modprobe -s drbd `$DRBDADM sh-mod-parms` || { ocf_logi err "Can not load the drbd module."$'\n'; return $OCF_ERR_GENERIC } ocf_logi debug "$RES start: Module loaded." fi if [ "$STATE" != "Secondary" ]; then $DRBDADM up $RES fi # try several times, in case heartbeat deadtime # was smaller than drbd ping time try=6 while true; do start="$($DRBDADM primary $RES 2>&1)" && break let "--try" || break sleep 1 done drbd_status ; rc=$? if [ $rc = $OCF_SUCCESS ]; then return $OCF_SUCCESS; fi ocf_logi err "$RES start failed: ($rc)" ocf_logi err "$start" return $rc } drbd_status() { ST=$( $DRBDADM state $RES 2>&1 ) STATE=${ST%/*} if [ "$STATE" = "Primary" ]; then echo "running" rc=$OCF_SUCCESS elif [ "$STATE" = "Secondary" ]; then echo "stopped" rc=$OCF_NOT_RUNNING else echo "$ST" rc=$OCF_NOT_RUNNING fi return $rc } drbd_monitor() { drbd_status } do_cmd() { local cmd="$*" ocf_logi Debug "$RES: Calling $cmd" local cmd_out=$($cmd 2>&1) ret=$? if [ $ret -ne 0 ]; then ocf_logi err "$RES: Called $cmd" ocf_logi err "$RES: Exit code $ret" ocf_logi err "$RES: Command output: $cmd_out" else ocf_logi debug "$RES: Exit code $ret" ocf_logi debug "$RES: Command output: $cmd_out" fi echo $cmd_out return $ret } is_drbd_enabled () { if [ -f /proc/drbd ]; then return 0 fi return 1 } usage() { echo $USAGE >&2 } # # Make a drbd device primary or secondary # DEFAULTFILE="/etc/default/drbd" DRBDADM="/sbin/drbdadm" if [ -f $DEFAULTFILE ]; then . $DEFAULTFILE fi if [ $# -ne 1 ] then usage exit $OCF_ERR_ARGS fi RES="$OCF_RESKEY_drbd_resource" case $1 in info) cat <<-!INFO Abstract=DRBD Device Manager Argument=DRBD Resource Name Description: A DRBD device is a network raid block device which can have a primary and a secondary backing device each on a separate machine. DRBD will keep them in sync. Please rerun with the meta-data command for a list of \\ valid arguments and their defaults. !INFO exit $OCF_SUCCESS;; esac case $1 in start|stop|status|monitor|reload) ocf_logi Debug "$RES: $1" cmd_out=$($cmd 2>&1) drbd_$1 ;; meta-data) meta_data;; usage) usage; exit $OCF_SUCCESS;; validate-all|notify|promote|demote) exit 0;; *) usage exit $OCF_ERR_ARGS ;; esac