3 #Common clean script to uninstall provisioning server
4 #author: Tim Rozet (trozet@redhat.com)
6 #Removes Libvirt, KVM, Vagrant, VirtualBox
8 #Destroys Vagrant VMs running in $vm_dir/
9 #Shuts down all nodes found in Khaleesi settings
10 #Removes hypervisor kernel modules (VirtualBox & KVM/Libvirt)
19 private_interface='enp6s0'
21 management_interface="${private_interface}.${management_vid}"
26 echo -e "\n\n${blue}This script is used to uninstall and clean the OPNFV Target System${reset}\n\n"
27 echo -e "\nUsage:\n$0 [arguments] \n"
28 echo -e "\n -no_parse : No variable parsing into config. Flag. \n"
29 echo -e "\n -base_config : Full path of ksgen settings file to parse. Required. Will provide BMC info to shutdown hosts. Example: -base_config /opt/myinventory.yml \n"
32 remove_interface_with_name_pattern() {
34 echo "${red}Cannot remove interface. No interface name pattern specified!${reset}"
37 local interface_name_pattern=$1
38 echo "${blue} Looking for interface with name pattern: ${interface_name_pattern}${reset}"
39 interface=$(ip link show | grep -oP ${interface_name_pattern})
40 if [ ! -z "${interface}" ]; then
41 echo "${blue}Interface ${interface} detected! Removing...${reset}"
42 ip link del ${interface}
43 if ip link show | grep -oP ${interface_name_pattern}; then
44 echo "${red}Could not remove interface ${interface} ${reset}"
47 echo "${blue}Interface ${interface} successfully removed${reset}"
50 echo "${blue}Interface with name pattern ${interface_name_pattern} does not exist, nothing to remove${reset}"
55 if [[ ( $1 == "--help") || $1 == "-h" ]]; then
60 echo -e "\n\n${blue}This script is used to uninstall and clean the OPNFV Target System${reset}\n\n"
61 echo "Use -h to display help"
64 while [ "`echo $1 | cut -c1`" = "-" ]
79 if [ ! -z "$base_config" ]; then
81 # Major version is pinned to force some consistency for Arno
82 if ! yum list installed | grep -i ipmitool; then
83 if ! yum -y install ipmitool-1*; then
84 echo "${red}Unable to install ipmitool!${reset}"
88 echo "${blue}Skipping ipmitool as it is already installed!${reset}"
91 ###find all the bmc IPs and number of nodes
93 output=`grep bmc_ip $base_config | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`
94 for line in ${output} ; do
95 bmc_ip[$node_counter]=$line
99 max_nodes=$((node_counter-1))
101 ###find bmc_users per node
103 output=`grep bmc_user $base_config | sed 's/\s*bmc_user:\s*//'`
104 for line in ${output} ; do
105 bmc_user[$node_counter]=$line
109 ###find bmc_pass per node
111 output=`grep bmc_pass $base_config | sed 's/\s*bmc_pass:\s*//'`
112 for line in ${output} ; do
113 bmc_pass[$node_counter]=$line
116 for mynode in `seq 0 $max_nodes`; do
117 echo "${blue}Node: ${bmc_ip[$mynode]} ${bmc_user[$mynode]} ${bmc_pass[$mynode]} ${reset}"
118 if ipmitool -I lanplus -P ${bmc_pass[$mynode]} -U ${bmc_user[$mynode]} -H ${bmc_ip[$mynode]} chassis power off; then
119 echo "${blue}Node: $mynode, ${bmc_ip[$mynode]} powered off!${reset}"
121 echo "${red}Error: Unable to power off $mynode, ${bmc_ip[$mynode]} ${reset}"
126 echo "${blue}Skipping Baremetal node poweroff as base_config was not provided${reset}"
128 ###check to see if vbox is installed
129 vboxpkg=`rpm -qa | grep VirtualBox`
130 if [ $? -eq 0 ]; then
136 ###legacy VM location check
138 if [ -d /tmp/bgs_vagrant ]; then
141 rm -rf /tmp/bgs_vagrant
145 if [ $skip_vagrant -eq 0 ]; then
146 if [ -d $vm_dir ]; then
148 for vm in $( ls $vm_dir ); do
150 if vagrant destroy -f; then
151 echo "${blue}Successfully destroyed $vm Vagrant VM ${reset}"
153 echo "${red}Unable to destroy $vm Vagrant VM! Attempting to killall vagrant if process is hung ${reset}"
155 echo "${blue}Checking if vagrant was already destroyed and no process is active...${reset}"
156 if ps axf | grep vagrant; then
157 echo "${red}Vagrant process still exists after kill...exiting ${reset}"
160 echo "${blue}Vagrant process doesn't exist. Moving on... ${reset}"
164 ##Vagrant boxes appear as VboxHeadless processes
165 ##try to gracefully destroy the VBox VM if it still exists
166 if vboxmanage list runningvms | grep $vm; then
167 echo "${red} $vm VBoxHeadless process still exists...Removing${reset}"
168 vbox_id=$(vboxmanage list runningvms | grep $vm | awk '{print $1}' | sed 's/"//g')
169 vboxmanage controlvm $vbox_id poweroff
170 if vboxmanage unregistervm --delete $vbox_id; then
171 echo "${blue}$vm VM is successfully deleted! ${reset}"
173 echo "${red} Unable to delete VM $vm ...Exiting ${reset}"
177 echo "${blue}$vm VM is successfully deleted! ${reset}"
181 echo "${blue}${vm_dir} doesn't exist, no VMs in OPNFV directory to destroy! ${reset}"
184 echo "${blue}Checking for any remaining virtual box processes...${reset}"
186 if ps axf | grep virtualbox; then
187 echo "${blue}virtualbox processes are still running. Killing any remaining VirtualBox processes...${reset}"
191 ###kill any leftover VMs (brute force)
192 if ps axf | grep VBoxHeadless; then
193 echo "${blue}VBoxHeadless processes are still running. Killing any remaining VBoxHeadless processes...${reset}"
198 echo "${blue}Removing VirtualBox... ${reset}"
199 yum -y remove $vboxpkg
202 echo "${blue}Skipping Vagrant destroy + VBox Removal as VirtualBox package is already removed ${reset}"
205 ###remove working vm directory
206 echo "${blue}Removing working VM directory: $vm_dir ${reset}"
209 ###check to see if libvirt is installed
210 echo "${blue}Checking if libvirt/KVM is installed"
211 if rpm -qa | grep -iE 'libvirt|kvm'; then
212 echo "${blue}Libvirt/KVM is installed${reset}"
213 echo "${blue}Checking for any QEMU/KVM VMs...${reset}"
215 while read -r line; do ((vm_count++)); done < <(virsh list --all | sed 1,2d | head -n -1)
216 if [ $vm_count -gt 0 ]; then
217 echo "${blue}VMs Found: $vm_count${reset}"
219 while read -r line; do ((vm_running++)); done < <(virsh list --all | sed 1,2d | head -n -1| grep -i running)
220 echo "${blue}Powering off $vm_running VM(s)${reset}"
222 if ! virsh destroy $vm; then
223 echo "${red}WARNING: Unable to power off VM ${vm}${reset}"
225 echo "${blue}VM $vm powered off!${reset}"
227 done < <(virsh list --all | sed 1,2d | head -n -1| grep -i running | sed 's/^[ \t]*//' | awk '{print $2}')
228 echo "${blue}Destroying libvirt VMs...${reset}"
230 if ! virsh undefine --remove-all-storage $vm; then
231 echo "${red}ERROR: Unable to remove the VM ${vm}${reset}"
234 echo "${blue}VM $vm removed!${reset}"
236 done < <(virsh list --all | sed 1,2d | head -n -1| awk '{print $2}')
238 echo "${blue}No VMs found for removal"
240 echo "${blue}Removing libvirt and kvm packages"
241 yum -y remove libvirt-*
244 echo "${blue}libvirt/KVM is not installed${reset}"
247 ###remove possible VMs (needed for 'rmmod kvm_intel')
248 if [ -n "$(ps -ef | grep qemu-kvm | grep -v grep)" ]; then
249 echo "${blue}Removing existing VMs ${reset}"
253 ###remove kernel modules
254 echo "${blue}Removing kernel modules ${reset}"
255 for kernel_mod in vboxnetadp vboxnetflt vboxpci vboxdrv kvm_intel kvm; do
256 if ! rmmod $kernel_mod; then
257 if rmmod $kernel_mod 2>&1 | grep -i 'not currently loaded'; then
258 echo "${blue} $kernel_mod is not currently loaded! ${reset}"
260 echo "${red}Error trying to remove Kernel Module: $kernel_mod ${reset}"
264 echo "${blue}Removed Kernel Module: $kernel_mod ${reset}"
269 echo "${blue}Checking whether PXE bridge ${pxe_bridge} exists${reset}"
270 if ! brctl show ${pxe_bridge} 2>&1 | grep -i 'No such device'; then
271 echo "${blue}PXE bridge ${pxe_bridge} detected! Removing...${reset}"
272 link_state=$(ip link show ${pxe_bridge} | grep -oP 'state \K[^ ]+')
273 if [[ ${link_state} != 'DOWN' ]]; then
274 ip link set dev ${pxe_bridge} down
276 link_state=$(ip link show ${pxe_bridge} | grep -oP 'state \K[^ ]+')
277 if [[ ${link_state} != 'DOWN' ]]; then
278 echo "${red}Could not bring DOWN bridge ${pxe_bridge} link state is ${link_state}${reset}"
282 brctl delbr ${pxe_bridge}
283 if ifconfig | grep ${pxe_bridge} || brctl show | grep ${pxe_bridge}; then
284 echo "${red}Error trying to remove ${pxe_bridge}${reset}"
287 echo "${blue}PXE bridge ${pxe_bridge} removed${reset}"
290 echo "${blue}PXE bridge ${pxe_bridge} does not exist${reset}"
293 ###remove PXE interface (VLAN 0)
294 echo "${blue}Checking whether PXE interface (VLAN 0) exists and remove it${reset}"
295 remove_interface_with_name_pattern "enp.+s.+\.0"
297 ###moving IP Address from Openstack Management interface back to base interface
298 echo "${blue}Moving IP addresses from VLAN ${management_vid} interface ${management_interface} back to interface ${private_interface}${reset}"
299 management_interface_ip_addr_list=$(ip addr show ${management_interface} | grep -oP 'inet \K[^ ]+')
300 if [[ ! -z ${management_interface_ip_addr_list} ]]; then
301 echo -e "${blue}Found IP addresses on VLAN ${management_vid} interface ${management_interface}:\n${management_interface_ip_addr_list}${reset}"
302 for management_interface_ip_addr in ${management_interface_ip_addr_list}
304 echo "${blue}Removing IP address ${management_interface_ip_addr} from VLAN ${management_vid} interface ${management_interface}${reset}"
305 ip addr del ${management_interface_ip_addr} dev ${management_interface}
306 if ip addr show ${management_interface} | grep ${management_interface_ip_addr}; then
307 echo "${red}Could not remove IP address ${management_interface_ip_addr} from VLAN ${management_vid} interface ${management_interface}${reset}"
310 if ! ip addr show ${private_interface} | grep ${management_interface_ip_addr}; then
311 echo "${blue}Adding IP address ${management_interface_ip_addr} to interface ${private_interface}${reset}"
312 ip addr add ${management_interface_ip_addr} dev ${private_interface}
313 if ! ip addr show ${private_interface} | grep ${management_interface_ip_addr}; then
314 echo "${red}Could not set IP address ${management_interface_ip_addr} to interface ${private_interface}${reset}"
318 echo "${blue}Interface ${private_interface} already has assigned to itself this IP address ${management_interface_ip_addr}${reset}"
322 echo "${red}No IP Address is assigned to VLAN ${management_vid} interface ${management_interface}, there isn't any IP address to move to interface ${private_interface}${reset}"
325 ###remove Openstack Management interface (VLAN 300)
326 echo "${blue}Checking whether Openstack Management interface (VLAN 300) exists and remove it${reset}"
327 remove_interface_with_name_pattern "enp.+s.+\.${management_vid}"