-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvbman.sh
177 lines (160 loc) · 6.08 KB
/
vbman.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# Colors for fancy output
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 6)
RESET=$(tput sgr0)
# Function to list all VMs with their status (Running or Stopped)
list_vms() {
echo -e "${CYAN}Available Virtual Machines:${RESET}"
running_vms=$(VBoxManage list runningvms | awk -F\" '{print $2}')
i=1 # Counter for numbering
VBoxManage list vms | while read -r line; do
vm_name=$(echo "$line" | awk -F\" '{print $2}')
vm_uuid=$(echo "$line" | awk -F\" '{print $3}' | tr -d '{} ')
if echo "$running_vms" | grep -Fxq "$vm_name"; then
status="${GREEN}Running${RESET}"
else
status="${RED}Stopped${RESET}"
fi
printf "%d) %-40s UUID: %-38s [%s]\n" "$i" "$vm_name" "$vm_uuid" "$status"
((i++)) # Increment counter
done
echo ""
}
# Function to get VM UUIDs
get_vm_uuids() {
VBoxManage list vms | awk -F\" '{print $3}' | tr -d '{} '
}
# Function to get running VM UUIDs
get_running_vm_uuids() {
VBoxManage list runningvms | awk -F\" '{print $3}' | tr -d '{} '
}
# Function to start a VM with choice of head or headless
start_vm() {
local vm_uuid="$1"
echo -e "${YELLOW}Do you want to start the VM in:${RESET}"
echo -e " ${GREEN}1) Headed (GUI) Mode${RESET}"
echo -e " ${GREEN}2) Headless (Background) Mode${RESET}"
echo -n "Enter your choice: "
read -r mode
if [[ $mode == "1" ]]; then
echo -e "${GREEN}Starting VM with GUI: $vm_uuid...${RESET}"
VBoxManage startvm "$vm_uuid"
elif [[ $mode == "2" ]]; then
echo -e "${GREEN}Starting VM in Headless mode: $vm_uuid...${RESET}"
VBoxManage startvm "$vm_uuid" --type headless
else
echo -e "${RED}Invalid choice. VM not started.${RESET}"
return
fi
echo -e "${YELLOW}VM is now running.${RESET}"
}
# Function to shut down a VM gracefully with force shutdown fallback
shutdown_vm() {
local vm_uuid="$1"
echo -e "${YELLOW}Attempting to gracefully shut down VM: $vm_uuid...${RESET}"
VBoxManage controlvm "$vm_uuid" acpipowerbutton
sleep 5 # Wait a few seconds for ACPI to take effect
# Check if VM is still running after ACPI shutdown attempt
if VBoxManage list runningvms | grep -q "$vm_uuid"; then
echo -e "${RED}ACPI shutdown failed! Forcing power off...${RESET}"
VBoxManage controlvm "$vm_uuid" poweroff
echo -e "${GREEN}VM $vm_uuid has been forcefully shut down.${RESET}"
else
echo -e "${GREEN}VM $vm_uuid has been shut down successfully.${RESET}"
fi
}
# Function to forcefully shut down a VM
force_shutdown_vm() {
local vm_uuid="$1"
echo -e "${RED}Force shutting down VM with UUID: $vm_uuid...${RESET}"
VBoxManage controlvm "$vm_uuid" poweroff
echo -e "${GREEN}Forced shutdown executed.${RESET}"
}
# Fancy menu function
menu() {
while true; do
clear
echo -e "${CYAN}======================================${RESET}"
echo -e "${YELLOW} VirtualBox VM Management Script ${RESET}"
echo -e "${CYAN}======================================${RESET}"
echo -e "${GREEN}1)${RESET} List all VMs (Show Status)"
echo -e "${GREEN}2)${RESET} Start a VM"
echo -e "${GREEN}3)${RESET} Shut down a VM"
echo -e "${GREEN}4)${RESET} Force shut down a VM"
echo -e "${GREEN}5)${RESET} Shut down ALL running VMs"
echo -e "${GREEN}6)${RESET} Force shut down ALL running VMs"
echo -e "${RED}7) Exit${RESET}"
echo -e "${CYAN}======================================${RESET}"
echo -n "Enter your choice: "
read -r choice
case $choice in
1)
list_vms
read -p "Press Enter to return to the menu..."
;;
2)
list_vms
echo -n "Enter the number of the VM to start: "
read -r vm_num
vm_uuid=$(get_vm_uuids | sed -n "${vm_num}p" | xargs)
if [ -n "$vm_uuid" ]; then
start_vm "$vm_uuid"
else
echo -e "${RED}Invalid selection.${RESET}"
fi
read -p "Press Enter to return to the menu..."
;;
3)
list_vms
echo -n "Enter the number of the VM to shut down: "
read -r vm_num
vm_uuid=$(get_vm_uuids | sed -n "${vm_num}p" | xargs)
if [ -n "$vm_uuid" ]; then
shutdown_vm "$vm_uuid"
else
echo -e "${RED}Invalid selection.${RESET}"
fi
read -p "Press Enter to return to the menu..."
;;
4)
list_vms
echo -n "Enter the number of the VM to FORCE shut down: "
read -r vm_num
vm_uuid=$(get_vm_uuids | sed -n "${vm_num}p" | xargs)
if [ -n "$vm_uuid" ]; then
force_shutdown_vm "$vm_uuid"
else
echo -e "${RED}Invalid selection.${RESET}"
fi
read -p "Press Enter to return to the menu..."
;;
5)
echo -e "${YELLOW}Shutting down all running VMs...${RESET}"
for vm_uuid in $(get_running_vm_uuids); do
shutdown_vm "$vm_uuid"
done
read -p "Press Enter to return to the menu..."
;;
6)
echo -e "${RED}Force shutting down all running VMs...${RESET}"
for vm_uuid in $(get_running_vm_uuids); do
force_shutdown_vm "$vm_uuid"
done
read -p "Press Enter to return to the menu..."
;;
7)
echo -e "${GREEN}Exiting...${RESET}"
exit 0
;;
*)
echo -e "${RED}Invalid choice, please try again.${RESET}"
read -p "Press Enter to return to the menu..."
;;
esac
done
}
# Run the menu
menu