-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-vm.sh
More file actions
executable file
·152 lines (136 loc) · 4.09 KB
/
delete-vm.sh
File metadata and controls
executable file
·152 lines (136 loc) · 4.09 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
#
# This deletes a VM from the whole system. Use carefully
#
# gnd @ gnd.sk, 2017 - 2019
#
#######################################################################
usage() {
printf "\n"
printf "This deletes a VM completely from the system & disk \n"
printf "Usage: \n"
printf "$0 <name VM_NAME> \n\n"
}
# Check if LIMA_ROOT set
if [ -z $LIMA_ROOT ]; then
echo "Cant find LIMA. Please check if the install finished correctly."
echo "Exiting. Reason: LIMA_ROOT not set."
exit
fi
# Define globals
source $LIMA_ROOT/vms/settings
DATUM=`/bin/date +%D|sed 's/\//_/g'`
### VM specified
case "$1" in
'name')
VM_NAME=$2
LINS=`cat $VM_LIST | awk {'print $2;'}|grep "^$VM_NAME$"|wc -l`
if [[ $LINS -lt 1 ]]; then
echo "No such name $VM_NAME found"
exit
fi
if [[ $LINS -gt 1 ]]; then
echo "More names found, please be specific:"
cat $VM_LIST | awk {'print $2;'}|grep "^$VM_NAME$"
exit
fi
VM_PORT=`cat $VM_LIST | awk {'print $2" "$4;'}|grep "^$VM_NAME "|awk {'print $2;'}`
VM_TYPE=`cat $VM_LIST | awk {'print $2" "$5;'}|grep "^$VM_NAME "|awk {'print $2;'}`
VM_IP=`cat $VM_LIST | awk {'print $2" "$3;'}|grep "^$VM_NAME "|awk {'print $2;'}`
PROXY=`cat $VM_LIST | awk {'print $2" "$6;'}|grep "^$VM_NAME "|awk {'print $2;'}`
;;
*)
usage
exit
;;
esac
### Delete VM from all files
read -p "This will delete all the data for the VM $VM_NAME. Do you wish to proceed ? [y/n]: " ANS
if [[ $ANS == "y" ]]; then
# build arguments for tar
ARGS=""
if [ -f $VM_DIR/vmlist ]; then
ARGS=$VM_DIR/vmlist
fi
if [ -f $VM_DIR/static.allowed ]; then
ARGS="$ARGS $VM_DIR/static.allowed"
fi
if [ -f $VM_DIR/dynamic.banned ]; then
ARGS="$ARGS $VM_DIR/dynamic.banned"
fi
if [ -f $VM_DIR/proxies.conf ]; then
ARGS="$ARGS $VM_DIR/proxies.conf"
fi
if [ -f $VM_DIR/ssh-forwards ]; then
ARGS="$ARGS $VM_DIR/ssh-forwards"
fi
# create conf file backup
tar -cf $BUP_DIR"/temp/conf_"$DATUM".tar" $ARGS
# destroy from libvirtd
virsh destroy $VM_NAME
# remove from pool
if [[ $VM_TYPE == "dyn" ]]; then
echo "Deleting dynamic VM $VM_NAME from disk"
mv $VM_DIR/dynamic/$VM_NAME $BUP_DIR"/temp/vms/dynamic/deleted_"$VM_NAME"_"$DATUM
fi
if [[ $VM_TYPE == "sta" ]]; then
echo "Deleting static VM $VM_NAME from disk"
mv $VM_DIR/static/$VM_NAME $BUP_DIR"/temp/vms/static/deleted_"$VM_NAME"_"$DATUM
fi
# remove from firewalls
echo "Removing VM $VM_NAME from firewalls"
if [[ $VM_TYPE == "dyn" ]]; then
$SCRIPT_DIR/enable-nat.sh name $VM_NAME
fi
if [[ $VM_TYPE == "sta" ]]; then
$SCRIPT_DIR/disable-nat.sh name $VM_NAME
fi
# remove from SSH forwards
if [[ -f $VM_DIR/ssh-forwards ]]; then
echo "Removing SSH forwards for VM $VM_NAME with IP $VM_IP"
sed -i "/$VM_IP/d" $VM_DIR/ssh-forwards
# Reload the system firewall
if [ ! -z $OSFW ]; then
$OSFW
fi
# Reload the lima firewall
$IPFW
fi
# remove from proxies
RED='\033[0;31m'
NC='\033[0m'
if [[ $PROXY == "folder" ]]; then
echo "Removing Apache proxy for VM $VM_NAME from proxies.conf"
sed -i "/$VM_IP/d" $VM_DIR/proxies.conf
fi
if [[ $PROXY == "vhost" ]]; then
FILE=`grep -l $VM_IP $APACHE_VHOST_DIR/*|tail -1`
if [[ ! -z $FILE ]]; then
printf "${RED}This VM has a separate apache vhost: $FILE . Please remove manually.${NC}\n"
else
printf "${RED}This VM might have a separate apache vhost. Please check & remove manually.${NC}\n"
fi
fi
if [[ $PROXY == "both" ]]; then
echo "Removing Apache proxy for VM $VM_NAME from proxies.conf"
sed -i "/$VM_IP/d" $VM_DIR/proxies.conf
FILE=`grep -l $VM_IP $APACHE_VHOST_DIR/*|tail -1`
if [[ ! -z $FILE ]]; then
printf "${RED}This VM has a separate apache vhost: $FILE. Please remove manually.${NC}\n"
else
printf "${RED}This VM might have a separate apache vhost. Please check & remove manually.${NC}\n"
fi
fi
echo "Removing Apache proxy for VM $VM_NAME"
sed -i "/$VM_IP/d" $VM_DIR/proxies.conf
apachectl restart
# remove from the vmlist
LINE="$VM_NAME $VM_IP $VM_PORT $VM_TYPE"
echo "Removing VM $VM_NAME from vmlist"
sed -i "/$LINE/d" $VM_LIST
# Done
echo "VM $VM_NAME deleted"
else
echo "Exiting .."
exit
fi