forked from suse-edge/misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_vm.sh
executable file
·73 lines (66 loc) · 1.64 KB
/
delete_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
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
#!/usr/bin/env bash
set -euo pipefail
BASEDIR="$(dirname "$0")"
die(){
echo ${1} 1>&2
exit ${2}
}
usage(){
cat <<-EOF
Usage: ${0} [-f <path/to/variables/file>] [-n <vmname>]
EOF
}
while getopts 'f:n:h' OPTION; do
case "${OPTION}" in
f)
[ -f "${OPTARG}" ] && ENVFILE="${OPTARG}" || die "Parameters file ${OPTARG} not found" 2
;;
n)
NAMEOPTION="${OPTARG}"
;;
h)
usage && exit 0
;;
?)
usage && exit 2
;;
esac
done
set -a
# Get the env file
source ${ENVFILE:-${BASEDIR}/.env}
# Some defaults just in case
VMNAME="${NAMEOPTION:-${VMNAME:-slemicro}}"
EXTRADISKS="${EXTRADISKS:-false}"
set +a
if [ $(uname -o) == "Darwin" ]; then
# Stop and delete the VM using UTM
OUTPUT=$(
osascript <<-END
tell application "UTM"
if exists virtual machine named "${VMNAME}" then
set vm to virtual machine named "${VMNAME}"
if status of vm is started then stop vm
repeat
if status of vm is stopped then exit repeat
end repeat
delete virtual machine named "${VMNAME}"
end if
end tell
END
)
elif [ $(uname -o) == "GNU/Linux" ]; then
if virsh list --all --name | grep -q ${VMNAME} ; then
[ "$(virsh domstate ${VMNAME} 2>/dev/null)" == "running" ] && virsh destroy ${VMNAME}
virsh undefine ${VMNAME}
else
die "${VMNAME} not found" 2
fi
else
die "Unsupported operating system" 2
fi
[ -f ${VMFOLDER}/${VMNAME}.raw ] && rm -f ${VMFOLDER}/${VMNAME}.raw
[ -f ${VMFOLDER}/${VMNAME}.qcow2 ] && rm -f ${VMFOLDER}/${VMNAME}.qcow2
[ -f ${VMFOLDER}/ignition-and-combustion-${VMNAME}.iso ] && rm -f ${VMFOLDER}/ignition-and-combustion-${VMNAME}.iso
[ "${EXTRADISKS}" != false ] && rm -f ${VMFOLDER}/${VMNAME}-extra-disk-*.raw
exit 0