Skip to content

Instantly share code, notes, and snippets.

@nickadam
Created January 27, 2016 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickadam/cc754442cf82ff19b071 to your computer and use it in GitHub Desktop.
Save nickadam/cc754442cf82ff19b071 to your computer and use it in GitHub Desktop.
DRBD Autodemote
#!/usr/bin/env bash
if [ -z "$1" ]
then
echo "Please specify a resource"
echo " drbd-autodemote r0"
exit 1
fi
resource="$1"
# verify that resource is connected
if ! /sbin/drbdadm cstate "$resource" 2>/dev/null | grep "^Connected$" > /dev/null
then
echo "Could not demote $resource, resource is not connected"
exit 1
fi
# verify that resource is uptodate
if ! /sbin/drbdadm dstate "$resource" 2>/dev/null | grep "^UpToDate/UpToDate$" > /dev/null
then
echo "Could not demote $resource, resource is not up to date"
exit 1
fi
# verify that resource is primary
if ! /sbin/drbdadm role "$resource" 2>/dev/null | grep "^Primary/Secondary$" > /dev/null
then
echo "Could not demote $resource, resource is not primary"
exit 1
fi
# if this resource is mounted, stop docker, kill other processes associated, and unmount
mountpoint=$(drbd-overview | grep "[0-9]:$resource\/[0-9]\s\+Connected\s\+Primary/Secondary\s\+UpToDate/UpToDate" | awk '{print $7}')
if [ ! -z "$mountpoint" ]
then
service docker stop
volume_in_use=1
while [ ! -z $volume_in_use ]
do
pids=$(lsof -n | grep "$mountpoint" | awk '{print $2}' | uniq)
if [ ! -z "$pids" ]
then
echo "Killing $pids"
kill $pids
sleep 1
else
volume_in_use=
fi
done
umount "$mountpoint"
fi
#finally set the resource to secondary
/sbin/drbdadm secondary "$resource" 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment