-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqemu
executable file
·59 lines (46 loc) · 1.24 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
#!/bin/bash
# Copy this file to /etc/libvirt/hooks, make sure it's called "qemu".
# After this file is installed, restart libvirt.
# From now on, you can easily add per-guest qemu hooks.
# Add your hooks in /etc/libvirt/hooks/qemu.d/vm_name/hook_name/state_name.
# For a list of available hooks, please refer to https://www.libvirt.org/hooks.html
set -eu
set -o pipefail
shopt -s nullglob
if [[ -e /.vmdebug ]]; then
exit 0
fi
GUEST_NAME="$1"
HOOK_NAME="$2"
STATE_NAME="$3"
MISC="${@:4}"
BASEDIR="$(dirname $0)"
HOOKPATH="$BASEDIR/qemu.d/$GUEST_NAME/$HOOK_NAME/$STATE_NAME"
LOGPATH="/var/log/libvirt/qemu/${GUEST_NAME}_${HOOK_NAME}_${STATE_NAME}.log"
log() {
echo "$@" | tee "$LOGPATH"
}
clear_log() {
echo "" > "$LOGPATH"
}
run_hook() {
HOOK="$1"
if [[ -f "$HOOK" ]] && [[ -x "$HOOK" ]]; then
log "Info: Executing hook: $HOOK"
"$HOOK" 2>&1 | tee "$LOGPATH"
else
log "Warning: found non-executable hook: $HOOK. Ignoring."
fi
}
# Run hook(s)
if [[ -f "$HOOKPATH" ]]; then
clear_log
run_hook "$HOOKPATH"
elif [[ -d "$HOOKPATH" ]]; then
clear_log
for hook in "$HOOKPATH"/*; do
run_hook "$hook"
done
else
log "Warning: $HOOKPATH is neither a file nor a directory. Ignoring."
fi