-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmkill.sh
More file actions
115 lines (100 loc) · 2.52 KB
/
Copy pathvmkill.sh
File metadata and controls
115 lines (100 loc) · 2.52 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
#!/bin/bash
# Title: VMKill v1.6
# Purpose: Shutdown all VMs, then shutdown/reboot host if desired.
# Author: Chris Johnson cc452@icloud.com
# Explanation: Looks for active virtual machines using virsh, sends each a shutdown signal, then polls virsh until all VMs are verified offline.
# It then shuts down or reboots the host, depending on chosen parameters.
# Example: ./vmkill -s
# Optional Parameters: -s shutdown host, -r reboot host, -h list optional parameters
# Script header
script_header () {
printf "\nVMKill - v1.6\n------------------------------\n"
}
# Checks for root user
check_root () {
if [ "$UID" != "0" ]; then
printf "Must be root to run.\n\n"
exit 3
fi
}
# Getopts definition
script_options () {
while getopts "srh" opt; do
case $opt in
s) shutdown_host
;;
r) reboot_host
;;
h) file_help
;;
esac
done
}
# Saves list of active virtual machines to an array
map_vmArray () {
mapfile -t vmshutdown < <(virsh list --name)
}
# Checks for active VMs. If there are none, exits.
check_active () {
if [ "${vmshutdown[0]}" == '' ]; then
printf "There are no active VMs.\n\n"
exit 2
fi
}
# Cycles through the array, shutting down each virtual machine referenced, ignoring any empty fields in the array
shutdown_VMs () {
for (( i=0; i<${#vmshutdown[@]}; i++ ));
do
if [ "${vmshutdown[i]}" != '' ]; then
printf "Sending shutdown signal to ${vmshutdown[i]}.\n"
virsh shutdown ${vmshutdown[i]}
fi
done
}
VM_status () {
# Initialize function variables
VIRSHSTATUS=0
SEC="A"
#Calls for list of active VMs, until the list returns nothing
printf "Waiting on VM shutdown to complete.\n"
until [ "$VIRSHSTATUS" = '' ]; do
VIRSHSTATUS=$(virsh list --name)
# Displays a * char once per second during the VIRSHSTATUS run, to show the script hasn’t frozen
if [ "$SEC" != $(date +%S) ]; then
SEC=$(date +%S)
printf "*"
fi
done
# Displays completion once virsh returns zero active VMs
printf "\nVM shutdown complete.\n\n"
}
shutdown_host () {
printf "Shutting down host $(hostnamectl --static).\n\n"
map_vmArray
check_active
shutdown_VMs
VM_status
shutdown now
exit
}
reboot_host () {
printf "Rebooting host $(hostnamectl --static).\n\n"
map_vmArray
check_active
shutdown_VMs
VM_status
reboot
exit
}
file_help () {
printf "Script accepts -s for shutting down host, -r for rebooting host, and -h for help.\n\n"
exit
}
# Function calls (main program sequence)
script_header
check_root
script_options
map_vmArray
check_active
shutdown_VMs
VM_status