#!/bin/bash Author="Lars Ellenberg " Date="2014-09-18" Version="1.0" License="GPL v2+" # # Inspired by the rhcs_fence perl version # by Digimer (digimer@alteeve.ca) # Alteeve's Niche! - https://alteeve.ca/w/ # As found at https://github.com/digimer/rhcs_fence/releases/tag/0.2.8 # # This program ties Linbit's DRBD into Red Hat's RHCS's fence daemon via the # 'fence_node' shell call. # from environment # if tools do not yet export the plural, # use the (deprecated) singular. : ${DRBD_PEERS:=$DRBD_PEER} PROG=${0##*/} : ${DEBUG:=1} LOG_PRIO=daemon.warning LOG_TAG="$PROG[$$] $DRBD_PEERS: $DRBD_RESOURCE(minor $DRBD_MINOR)" ############################################################################## # helper functions ############################################################################## die() { echo "$*"; exit 1; } all_minors_up_to_date() { set -- $DRBD_MINOR local n_minors=$# [[ $n_minors != 0 ]] || die "Resource minor numbers unknown! Unable to proceed." # build a "grep extended regex" local _OLDIFS=$IFS IFS="|" local minor_regex="^ *($*): cs:" IFS=$_OLDIFS # grep -c -Ee '^ *(m|i|n|o|r|s): cs:.* ds:UpToDate' /proc/drbd local proc_drbd=$( >( exec 1>&- logger -s -p ${LOG_PRIO%.*}.debug -t "$LOG_TAG: DEBUG" ) exec 1> >( exec 1>&- logger -s -p $LOG_PRIO -t "$LOG_TAG" ) else [[ $DEBUG != 0 ]] && exec 3> >( exec 1>&- 2>&- logger -p ${LOG_PRIO%.*}.debug -t "$LOG_TAG: DEBUG" ) exec 1> >( exec 1>&- 2>&- logger -p $LOG_PRIO -t "$LOG_TAG" ) fi # and now point stderr to logger, too exec 2>&1 if [[ $DEBUG = 0 ]]; then debug() { :; } else debug() { echo >&3 "$*" ; } if [[ $DEBUG -gt 1 ]]; then BASH_XTRACEFD=3 set -x fi fi ############################################################################## # "main" ############################################################################## [[ $DRBD_PEERS ]] || die "No target list specified. You need to pass DRBD_PEERS via environment." all_minors_up_to_date || die "some minor device is NOT 'UpToDate', will not fence peer" for peer in $DRBD_PEERS; do for name in $peer ${peer%%.*}; do set -- $(cman_tool -F id,type nodes -n $name) id=$1 state=$2 [[ $id ]] && break done if [[ -z $id ]] || [[ $id = *[!0-9]* ]] ; then die "could not resolve cman node id of $peer, giving up" fi echo "resolved $peer as cman node $name, id $id, state $state" # record fence domain state now set -- $(fence_tool ls) fence_tool_ls="$* " debug "fence_tool ls: $fence_tool_ls" if [[ $state = M ]] ; then eject_target && continue # with next peer, if any else # maybe cman noticed before the handler triggered, # and fencing is already active anyways. if [[ $fence_tool_ls = *"victim now $id "* ]]; then wait_for_id_to_drop_out_of_membership && continue # with next peer, if any fi fi # apparently it was not in the member list. # or we timed out waiting for fenced debug "trying direct fence of $name" dash_v=-v [[ $DEBUG -gt 1 ]] && dash_v=-vv echo "fence_node $dash_v $name" if fence_node $dash_v $name ; then echo "successfully fenced $name" continue # with next peer, if any else die "fencing $name failed, giving up" fi done # if we fenced more than one peer, # add an other log line [[ $peer != $DRBD_PEERS ]] && echo "SUCCESSFULLY FENCED $DRBD_PEERS" exit 7