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 first_interface='enp6s0'
20 second_interface='enp7s0'
22 management_interface="${first_interface}.${management_vid}"
27 echo -e "\n\n${blue}This script is used to uninstall and clean the OPNFV Target System${reset}\n\n"
28 echo -e "\nUsage:\n$0 [arguments] \n"
29 echo -e "\n -no_parse : No variable parsing into config. Flag. \n"
30 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"
33 remove_interface_with_name_pattern() {
35 echo "${red}Cannot remove interface. No interface name pattern specified!${reset}"
38 local interface_name_pattern=$1
39 echo "${blue} Looking for interface with name pattern: ${interface_name_pattern}${reset}"
40 interface=$(ip link show | grep -oP ${interface_name_pattern})
41 if [ ! -z "${interface}" ]; then
42 echo "${blue}Interface ${interface} detected! Removing...${reset}"
43 ip link del ${interface}
44 if ip link show | grep -oP ${interface_name_pattern}; then
45 echo "${red}Could not remove interface ${interface} ${reset}"
48 echo "${blue}Interface ${interface} successfully removed${reset}"
51 echo "${blue}Interface with name pattern ${interface_name_pattern} does not exist, nothing to remove${reset}"
56 if [[ ( $1 == "--help") || $1 == "-h" ]]; then
61 echo -e "\n\n${blue}This script is used to uninstall and clean the OPNFV Target System${reset}\n\n"
62 echo "Use -h to display help"
65 while [ "`echo $1 | cut -c1`" = "-" ]
80 if [ ! -z "$base_config" ]; then
82 # Major version is pinned to force some consistency for Arno
83 if ! yum list installed | grep -i ipmitool; then
84 if ! yum -y install ipmitool-1*; then
85 echo "${red}Unable to install ipmitool!${reset}"
89 echo "${blue}Skipping ipmitool as it is already installed!${reset}"
92 ###find all the bmc IPs and number of nodes
94 output=`grep bmc_ip $base_config | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`
95 for line in ${output} ; do
96 bmc_ip[$node_counter]=$line
100 max_nodes=$((node_counter-1))
102 ###find bmc_users per node
104 output=`grep bmc_user $base_config | sed 's/\s*bmc_user:\s*//'`
105 for line in ${output} ; do
106 bmc_user[$node_counter]=$line
110 ###find bmc_pass per node
112 output=`grep bmc_pass $base_config | sed 's/\s*bmc_pass:\s*//'`
113 for line in ${output} ; do
114 bmc_pass[$node_counter]=$line
117 for mynode in `seq 0 $max_nodes`; do
118 echo "${blue}Node: ${bmc_ip[$mynode]} ${bmc_user[$mynode]} ${bmc_pass[$mynode]} ${reset}"
119 if ipmitool -I lanplus -P ${bmc_pass[$mynode]} -U ${bmc_user[$mynode]} -H ${bmc_ip[$mynode]} chassis power off; then
120 echo "${blue}Node: $mynode, ${bmc_ip[$mynode]} powered off!${reset}"
122 echo "${red}Error: Unable to power off $mynode, ${bmc_ip[$mynode]} ${reset}"
127 echo "${blue}Skipping Baremetal node poweroff as base_config was not provided${reset}"
129 ###check to see if vbox is installed
130 vboxpkg=`rpm -qa | grep VirtualBox`
131 if [ $? -eq 0 ]; then
137 ###legacy VM location check
139 if [ -d /tmp/bgs_vagrant ]; then
142 rm -rf /tmp/bgs_vagrant
146 if [ $skip_vagrant -eq 0 ]; then
147 if [ -d $vm_dir ]; then
149 for vm in $( ls $vm_dir ); do
151 if vagrant destroy -f; then
152 echo "${blue}Successfully destroyed $vm Vagrant VM ${reset}"
154 echo "${red}Unable to destroy $vm Vagrant VM! Attempting to killall vagrant if process is hung ${reset}"
156 echo "${blue}Checking if vagrant was already destroyed and no process is active...${reset}"
157 if ps axf | grep vagrant; then
158 echo "${red}Vagrant process still exists after kill...exiting ${reset}"
161 echo "${blue}Vagrant process doesn't exist. Moving on... ${reset}"
165 ##Vagrant boxes appear as VboxHeadless processes
166 ##try to gracefully destroy the VBox VM if it still exists
167 if vboxmanage list runningvms | grep $vm; then
168 echo "${red} $vm VBoxHeadless process still exists...Removing${reset}"
169 vbox_id=$(vboxmanage list runningvms | grep $vm | awk '{print $1}' | sed 's/"//g')
170 vboxmanage controlvm $vbox_id poweroff
171 if vboxmanage unregistervm --delete $vbox_id; then
172 echo "${blue}$vm VM is successfully deleted! ${reset}"
174 echo "${red} Unable to delete VM $vm ...Exiting ${reset}"
178 echo "${blue}$vm VM is successfully deleted! ${reset}"
182 echo "${blue}${vm_dir} doesn't exist, no VMs in OPNFV directory to destroy! ${reset}"
185 echo "${blue}Checking for any remaining virtual box processes...${reset}"
187 if ps axf | grep virtualbox; then
188 echo "${blue}virtualbox processes are still running. Killing any remaining VirtualBox processes...${reset}"
192 ###kill any leftover VMs (brute force)
193 if ps axf | grep VBoxHeadless; then
194 echo "${blue}VBoxHeadless processes are still running. Killing any remaining VBoxHeadless processes...${reset}"
199 echo "${blue}Removing VirtualBox... ${reset}"
200 yum -y remove $vboxpkg
203 echo "${blue}Skipping Vagrant destroy + VBox Removal as VirtualBox package is already removed ${reset}"
206 ###remove working vm directory
207 echo "${blue}Removing working VM directory: $vm_dir ${reset}"
210 ###check to see if libvirt is installed
211 echo "${blue}Checking if libvirt/KVM is installed"
212 if rpm -qa | grep -iE 'libvirt|kvm'; then
213 echo "${blue}Libvirt/KVM is installed${reset}"
214 echo "${blue}Checking for any QEMU/KVM VMs...${reset}"
216 while read -r line; do ((vm_count++)); done < <(virsh list --all | sed 1,2d | head -n -1)
217 if [ $vm_count -gt 0 ]; then
218 echo "${blue}VMs Found: $vm_count${reset}"
220 while read -r line; do ((vm_running++)); done < <(virsh list --all | sed 1,2d | head -n -1| grep -i running)
221 echo "${blue}Powering off $vm_running VM(s)${reset}"
223 if ! virsh destroy $vm; then
224 echo "${red}WARNING: Unable to power off VM ${vm}${reset}"
226 echo "${blue}VM $vm powered off!${reset}"
228 done < <(virsh list --all | sed 1,2d | head -n -1| grep -i running | sed 's/^[ \t]*//' | awk '{print $2}')
229 echo "${blue}Destroying libvirt VMs...${reset}"
231 if ! virsh undefine --remove-all-storage $vm; then
232 echo "${red}ERROR: Unable to remove the VM ${vm}${reset}"
235 echo "${blue}VM $vm removed!${reset}"
237 done < <(virsh list --all | sed 1,2d | head -n -1| awk '{print $2}')
239 echo "${blue}No VMs found for removal"
241 echo "${blue}Removing libvirt and kvm packages"
242 yum -y remove libvirt-*
245 echo "${blue}libvirt/KVM is not installed${reset}"
248 ###remove possible VMs (needed for 'rmmod kvm_intel')
249 if [ -n "$(ps -ef | grep qemu-kvm | grep -v grep)" ]; then
250 echo "${blue}Removing existing VMs ${reset}"
254 ###remove kernel modules
255 echo "${blue}Removing kernel modules ${reset}"
256 for kernel_mod in vboxnetadp vboxnetflt vboxpci vboxdrv kvm_intel kvm; do
257 if ! rmmod $kernel_mod; then
258 if rmmod $kernel_mod 2>&1 | grep -i 'not currently loaded'; then
259 echo "${blue} $kernel_mod is not currently loaded! ${reset}"
261 echo "${red}Error trying to remove Kernel Module: $kernel_mod ${reset}"
265 echo "${blue}Removed Kernel Module: $kernel_mod ${reset}"
270 echo "${blue}Checking whether PXE bridge ${pxe_bridge} exists${reset}"
271 if ! brctl show ${pxe_bridge} 2>&1 | grep -i 'No such device'; then
272 echo "${blue}PXE bridge ${pxe_bridge} detected! Removing...${reset}"
273 link_state=$(ip link show ${pxe_bridge} | grep -oP 'state \K[^ ]+')
274 if [[ ${link_state} != 'DOWN' ]]; then
275 ip link set dev ${pxe_bridge} down
277 link_state=$(ip link show ${pxe_bridge} | grep -oP 'state \K[^ ]+')
278 if [[ ${link_state} != 'DOWN' ]]; then
279 echo "${red}Could not bring DOWN bridge ${pxe_bridge} link state is ${link_state}${reset}"
283 brctl delbr ${pxe_bridge}
284 if ifconfig | grep ${pxe_bridge} || brctl show | grep ${pxe_bridge}; then
285 echo "${red}Error trying to remove ${pxe_bridge}${reset}"
288 echo "${blue}PXE bridge ${pxe_bridge} removed${reset}"
291 echo "${blue}PXE bridge ${pxe_bridge} does not exist${reset}"
294 ###remove PXE interface (VLAN 0)
295 echo "${blue}Checking whether PXE interface (VLAN 0) exists and remove it${reset}"
296 remove_interface_with_name_pattern "enp.+s.+\.0"
298 ###remove Openstack Management interface (VLAN 300)
299 echo "${blue}Checking whether Openstack Management interface (VLAN 300) exists and remove it${reset}"
300 remove_interface_with_name_pattern "enp.+s.+\.${management_vid}"
302 ###bounce interfaces to restore default IP config
303 echo "${blue}Bouncing interfaces to restore IP config${reset}"
304 for interface in $first_interface $second_interface; do
305 echo "${blue}Bouncing interface: ${interface}${reset}"
311 while [ $counter -lt $tries ]; do
312 if ip addr show $interface | grep -Eo "inet [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"; then
313 temp_ip=$(ip addr show $interface | grep -Eo "inet [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | awk '{print $2}')
314 echo "${blue}IP found on ${interface}. IP is ${temp_ip}${reset}"
322 if [ "$counter" -ge 5 ]; then
323 echo "${red}Error: Unable to get IP address on ${interface}${reset}"