forked from ohitz/libvirt-drbd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
libvirt-drbd | ||
============ | ||
|
||
libvirt hook to automatically promote/demote DRBD resources | ||
This script is a libvirt hook to automatically promote/demote DRBD | ||
resources. | ||
|
||
Place the "qemu" script in /etc/libvirt/hooks/ and it will | ||
automatically promote and demote DRBD resources configured in libvirt | ||
guests when the guests are started or shut down. | ||
|
||
In order for the DRBD resources to be recognized as such, they need to | ||
be configured with the /dev/drbd/by-res/<name> path. | ||
|
||
The script requires the xpath command from the libxml-xpath-perl | ||
package. | ||
|
||
The latest version of the script is at | ||
https://github.com/ohitz/libvirt-drbd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright (C) 2014 Oliver Hitz <[email protected]> | ||
# | ||
# This script is a libvirt hook to automatically promote/demote DRBD | ||
# resources. | ||
# | ||
# Place the "qemu" script in /etc/libvirt/hooks/ and it will | ||
# automatically promote and demote DRBD resources configured in libvirt | ||
# guests when the guests are started or shut down. | ||
# | ||
# In order for the DRBD resources to be recognized as such, they need to | ||
# be configured with the /dev/drbd/by-res/<name> path. | ||
# | ||
# The script requires the xpath command from the libxml-xpath-perl | ||
# package. | ||
# | ||
# The latest version of the script is at | ||
# https://github.com/ohitz/libvirt-drbd | ||
|
||
guest_name=$1 | ||
task=$2 | ||
|
||
# The entire XML configuration is given on stdin and is written to a temporary | ||
# file. | ||
guest_cfg=/tmp/libvirt.qemuhook.tmp.$guest_name.$$ | ||
cat - > $guest_cfg | ||
|
||
case "$task" in | ||
prepare) | ||
# "qemu <guest> prepare", means that libvirt is preparing to start the guest. | ||
for dev in `xpath -q -e "/domain/devices/disk/source/@dev" $guest_cfg | cut -d \" -f 2`; do | ||
drbd_resource=`echo $dev | sed 's|^/dev/drbd/by-res/||'` | ||
if [ "x$drbd_resource" != "x$dev" ]; then | ||
drbd_role="$(/sbin/drbdadm role $drbd_resource)" | ||
drbd_lrole="${drbd_role%%/*}" | ||
if [ "$drbd_lrole" != 'Primary' ]; then | ||
/sbin/drbdadm primary $drbd_resource || exit 1 | ||
fi | ||
fi | ||
done | ||
;; | ||
release) | ||
# "qemu <guest> release", means that the guest has been shutdown. | ||
for dev in `xpath -q -e "/domain/devices/disk/source/@dev" $guest_cfg | cut -d \" -f 2`; do | ||
drbd_resource=`echo $dev | sed 's|^/dev/drbd/by-res/||'` | ||
if [ "x$drbd_resource" != "x$dev" ]; then | ||
drbd_role="$(/sbin/drbdadm role $drbd_resource)" | ||
drbd_lrole="${drbd_role%%/*}" | ||
if [ "$drbd_lrole" = 'Primary' ]; then | ||
/sbin/drbdadm secondary $drbd_resource || exit 1 | ||
fi | ||
fi | ||
done | ||
;; | ||
esac | ||
|
||
# Remove temporary file. | ||
rm -f $guest_cfg | ||
|
||
exit 0 |