forked from ohitz/libvirt-drbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qemu
executable file
·108 lines (89 loc) · 2.96 KB
/
qemu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#
# Copyright (C) 2014 Oliver Hitz <[email protected]>
# Copyright (C) 2016 Marc Ponschab <[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 xmllint command from the libxml package.
#
# The latest version of the script is at
# https://github.com/ohitz/libvirt-drbd
# If qemu does't release the drbrd device immediately, make MAX_ATTEMPTS attemps after waiting SLEEP_BETWEEN_ATTEMPTS
MAX_ATTEMPTS=10;
SLEEP_BETWEEN_ATTEMPTS=1;
function cleanup() {
if [ -n "$guest_cfg" ]; then
rm -f $guest_cfg
fi
}
function selectDrbdDevices() {
xmllint --xpath "/domain/devices/disk/source[starts-with(@dev, '/dev/drbd/by-res/')]/@dev" $1;
}
function setRole() {
role=$1;
resourceName=$(echo $2 | sed -e 's|^dev="/dev/drbd/by-res/\+\([^/]\+\).*\?"|\1|');
for i in $(seq $MAX_ATTEMPTS); do
#echo "attempt $i: drbdadm $role $resourceName" >>/var/log/libvirt/hook-qemu.log
/usr/sbin/drbdadm $role $resourceName && return;
sleep $SLEEP_BETWEEN_ATTEMPTS;
done
echo "$MAX_ATTEMPTS attempts executing \"drbdadm $role $resourceName failed, giving up.\"" >&2
exit 1
}
function setDualPrimary() {
yesno=$1
resourceName=$(echo $2 | sed -e 's|^dev="/dev/drbd/by-res/\+\([^/]\+\).*\?"|\1|');
for i in $(seq $MAX_ATTEMPTS); do
#echo "attempt $i: drbdadm net-options --allow-two-primaries=$yesno $resourceName" >>/var/log/libvirt/hook-qemu.log
/usr/sbin/drbdadm net-options --allow-two-primaries=$yesno $resourceName && return;
sleep $SLEEP_BETWEEN_ATTEMPTS;
done
echo "$MAX_ATTEMPTS attempts executing \"drbdadm net-options --allow-two-primaries=$yesno $resourceName failed, giving up.\"" >&2
exit 1
}
trap cleanup EXIT
if [ "$#" -ne 4 ]; then
echo "$0: Illegal number of parameters." >&2
exit 1
fi
guest_name=$1
task=$2
# Create temporary file to hold the configuration.
guest_cfg=`mktemp`
case "$task" in
migrate)
# "qemu <guest> migrate", means that libvirt is preparing to migrate the guest to this host
# Read XML configuration on stdin.
cat - > $guest_cfg
for dev in $(selectDrbdDevices $guest_cfg); do
setDualPrimary "yes" $dev;
done
;;
prepare)
# "qemu <guest> prepare", means that libvirt is preparing to start the guest.
# Read XML configuration on stdin.
cat - > $guest_cfg
for dev in $(selectDrbdDevices $guest_cfg); do
setRole primary $dev;
done
;;
release)
# "qemu <guest> release", means that the guest has been shutdown.
# Read XML configuration on stdin.
cat - > $guest_cfg
for dev in $(selectDrbdDevices $guest_cfg); do
setRole secondary $dev;
setDualPrimary "no" $dev;
done
;;
esac
exit 0