-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdestroy_vm.sh
executable file
·35 lines (28 loc) · 941 Bytes
/
destroy_vm.sh
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
#!/bin/bash
set -o errexit
set -o errtrace
set -o pipefail
set -o nounset
function error_handler() {
echo "Error occurred in ${3} executing line ${1} with status code ${2}"
echo "The pipe status values were: ${4}"
}
# Please note basename... is intentionally at the end as it's a command that
# will effect the value of '$?'
trap 'error_handler ${LINENO} $? "$(basename ${BASH_SOURCE[0]})" "${PIPESTATUS[*]}"' ERR
# Log all commands before they're executed for debugging purposes
if [ -n "${DEBUG:-}" ]; then
set -o xtrace
fi
if [ ${EUID} != 0 ]; then
echo "This script is expecting to run as root."
exit 1
fi
EXISTING_VM="$(virsh list --all | awk '/gentoo-test/ { print $2 }' | head -n 1)"
if [ -n "${EXISTING_VM}" ]; then
# This first one will fail if it's not running
virsh destroy ${EXISTING_VM} || true
virsh undefine --remove-all-storage --nvram ${EXISTING_VM}
else
echo "Couldn't find a running Gentoo VM"
fi