-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnuke.sh
executable file
·76 lines (64 loc) · 1.55 KB
/
nuke.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
74
75
76
#!/bin/bash
#set -x
VM=$1
VIRSH=/usr/bin/virsh
CWD="/var/lib/libvirt/images"
POOL="default"
check_original () {
echo $VM > /tmp/name
if [ `cat /tmp/name | grep -E 'original|golden'` ]; then
echo "WARNING, you are asking me to nuke an original or golden image"
read -rep $'Are you sure you want to continue? y/n \n'
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted"
exit 1
fi
fi
}
check_existing () {
$VIRSH list --all --name | grep -w $VM > /dev/null 2>&1
}
check_running () {
$VIRSH list --name | grep -w $VM > /dev/null 2>&1
}
check_original
check_existing
if [ $? -ne "0" ]; then
echo "No domain found named $VM found. Bailing out."
exit 1
fi
### adding a confirmation step anyway
echo "WARNING, this will destroy irreparably your machine $VM"
read -rep $'Are you sure you want to continue? y/n \n'
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted"
exit 1
fi
check_running
if [ $? -eq "0" ]; then
echo "Destroying $VM"
$VIRSH destroy $VM
fi
check_running
if [ $? -eq "1" ]; then
if [ -f /etc/libvirt/qemu/$VM.xml ]; then
echo "Removing kvm configuration file..."
$VIRSH undefine $VM
else
echo "No configuration file for $VM"
fi
if [ -f $CWD/$VM.img ]; then
echo "Nuking $VM.img..."
$VIRSH pool-refresh --pool=$POOL
$VIRSH vol-delete $VM.img --pool=$POOL
fi
if [ -f $CWD/$VM.qcow2 ]; then
echo "Nuking $VM.qcow2..."
$VIRSH pool-refresh --pool=$POOL
$VIRSH vol-delete $VM.qcow2 --pool=$POOL
fi
else
echo "can't determine if $VM is running, got $? exit"
exit 1
fi
exit 0