-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvb.sh
executable file
·418 lines (402 loc) · 7.96 KB
/
vb.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/bin/bash
# Headers
main_header() {
echo "VirtualBox TUI"
separator_header
}
vm_header() {
echo "VM: $vm_selected"
vboxmanage showvminfo "$vm_selected_uuid" | grep -e '^State:'
separator_header
}
separator_header() {
echo "--------------"
}
# Functions
list_vms() { # Lists VMs for selection
clear
main_header
vm_list=$(vboxmanage list vms --sorted)
if [ -z "$vm_list" ]; then
echo "No VMs found"
separator_header
vm_list_selected=$(gum choose \
"Refresh" \
"Exit")
case $vm_list_selected in
"Refresh")
list_vms
;;
"Exit")
exit 0
;;
esac
else
echo "Select a VM:"
separator_header
vm_selected=$(gum choose --limit=1 <<<"$vm_list")
vm_selected_uuid=$(echo "$vm_selected" |
awk '{print $(NF)}' |
sed -e 's/^{//' -e 's/}$//')
stat_vm
fi
}
stat_vm() { # Single VM view
clear
main_header
vm_header
echo "VM Actions:"
separator_header
list_actions
}
list_actions() { # Actions applicable to single VM
action_selected=$(gum choose \
"Start" \
"Pause" \
"Resume" \
"Reboot" \
"Stop" \
"Manage" \
"Refresh" \
"Home" \
"Exit")
case $action_selected in
"Start")
list_start_options
;;
"Pause")
gum confirm \
--default="no" \
"Pause VM?" &&
gum spin \
--title="Pausing VM..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" pause
list_vms
;;
"Resume")
gum spin \
--title="Resuming VM..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" resume
list_vms
;;
"Reboot")
list_reboot_options
;;
"Stop")
list_stop_options
;;
"Manage")
list_manage_options
;;
"Refresh")
stat_vm
;;
"Home")
list_vms
;;
"Exit")
exit 0
;;
esac
}
list_start_options() { # VM start options
clear
main_header
vm_header
echo "VM Start Options:"
separator_header
start_option_selected=$(gum choose \
"Normal" \
"Headless" \
"Detachable")
case $start_option_selected in
"Normal")
gum spin \
--title="Starting VM normally..." \
--show-output \
-- vboxmanage startvm "$vm_selected_uuid" --type=gui
list_vms
;;
"Headless")
gum spin \
--title="Starting VM headless..." \
--show-output \
-- vboxmanage startvm "$vm_selected_uuid" --type=headless
list_vms
;;
"Detachable")
gum spin \
--title="Starting VM detachable..." \
--show-output \
-- vboxmanage startvm "$vm_selected_uuid" --type=separate
list_vms
;;
esac
}
list_reboot_options() { # VM reboot options
clear
main_header
vm_header
echo "VM Reboot Options:"
separator_header
reboot_option_selected=$(gum choose \
"Safe (Requires Guest Additions)" \
"Force")
case $reboot_option_selected in
"Safe (Requires Guest Additions)")
gum confirm \
--default="no" \
"Reboot VM safely?" &&
gum spin \
--title="Rebooting VM safely..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" reboot
list_vms
;;
"Force")
gum confirm \
--default="no" \
"Force-reboot VM?" &&
gum spin \
--title="Force-rebooting VM..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" reset
list_vms
;;
esac
}
list_stop_options() { # VM stop options
clear
main_header
vm_header
echo "VM Stop Options:"
separator_header
stop_option_selected=$(gum choose \
"Safe (Requires Guest Additions)" \
"ACPI" \
"Force" \
"Save State")
case $stop_option_selected in
"Safe (Requires Guest Additions)")
gum confirm \
--default="no" \
"Stop VM safely?" &&
gum spin \
--title="Stopping VM safely..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" shutdown
list_vms
;;
"ACPI")
gum confirm \
--default="no" \
"Press ACPI power button?" &&
gum spin \
--title="Stopping VM with ACPI button press..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" acpipowerbutton
list_vms
;;
"Force")
gum confirm \
--default="no" \
"Force-stop VM?" &&
gum spin \
--title="Force-stopping VM..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" poweroff
list_vms
;;
"Save State")
gum confirm \
--default="no" \
"Save VM state and stop?" &&
gum spin \
--title="Saving VM state and stopping..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" savestate
list_vms
;;
esac
}
list_manage_options() { # VM manage options
manage_option_selected=$(gum choose \
"Snapshots" \
"Discard State" \
"Autostart")
case $manage_option_selected in
"Snapshots")
list_snapshot_options
;;
"Discard State")
gum confirm \
--default="no" \
"Discard VM State?" &&
gum spin \
--title="Discarding VM state..." \
--show-output \
-- vboxmanage discardstate "$vm_selected_uuid"
list_vms
;;
"Autostart")
list_autostart_options
;;
esac
}
list_autostart_options() { # VM autostart options
clear
main_header
vm_header
echo "VM Autostart Options:"
separator_header
autostart_option_selected=$(gum choose \
"Enable" \
"Disable" \
"Set Delay")
case $autostart_option_selected in
"Enable")
gum spin \
--title="Enabling VM autostart..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" autostart-enabled on
list_vms
;;
"Disable")
gum spin \
--title="Disabling VM autostart..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" autostart-disabled off
list_vms
;;
"Set Delay")
echo "Enter VM Autostart Delay in Seconds:"
gum spin \
--title="Setting VM autostart delay..." \
--show-output \
-- vboxmanage controlvm "$vm_selected_uuid" autostart-delay "$(gum input --placeholder="Autostart delay in seconds")"
list_vms
;;
esac
}
list_snapshot_options() {
snapshot_option_selected=$(gum choose \
"Take" \
"Restore" \
"Edit" \
"Delete")
case $snapshot_option_selected in
"Take")
take_snapshot
;;
"Restore")
restore_snapshot
;;
"Edit")
edit_snapshot
;;
"Delete")
delete_snapshot
;;
esac
}
list_snapshots() {
snapshot_list=$(vboxmanage snapshot "$vm_selected_uuid" list)
clear
main_header
vm_header
if [ "$snapshot_list" = "This machine does not have any snapshots" ]; then
echo "No Snapshots Found"
separator_header
snapshot_list_selected=$(gum choose \
"Refresh" \
"Back to VM" \
"VM List" \
"Exit")
case $snapshot_list_selected in
"Refresh")
list_snapshots
;;
"Back to VM")
stat_vm
;;
"VM List")
vm_list
;;
"Exit")
exit 0
;;
esac
else
echo "Select Snapshot:"
separator_header
snapshot_selected=$(gum choose --limit=1 <<<"$snapshot_list")
snapshot_selected_uuid=$(echo "$snapshot_selected" |
awk '{
for(i=1; i<=NF; i++) {
tmp=match($i, /[A-Za-z0-9]{8}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{12}/)
if(tmp) {
print $i
}
}
}' | sed -e 's/)$//')
fi
}
take_snapshot() {
clear
main_header
vm_header
echo "Enter Snapshot Name:"
separator_header
gum spin \
--title="Taking Snapshot..." \
--show-output \
-- vboxmanage snapshot "$vm_selected_uuid" take \
"$(gum input --placeholder='My Snapshot')" --live
list_vms
}
restore_snapshot() {
clear
main_header
vm_header
list_snapshots
gum confirm \
--default="no" \
"Restore Snapshot?" &&
gum spin \
--title="Restoring Snapshot..." \
--show-output \
-- vboxmanage snapshot "$vm_selected_uuid" restore "$snapshot_selected_uuid"
list_vms
}
edit_snapshot() {
clear
main_header
vm_header
list_snapshots
echo "Enter New Snapshot Name:"
separator_header
gum spin \
--title="Editing Snapshot..." \
--show-output \
-- vboxmanage snapshot "$vm_selected_uuid" edit "$snapshot_selected_uuid" \
--name="$(gum input --placeholder='My Snapshot')"
list_vms
}
delete_snapshot() {
clear
main_header
vm_header
list_snapshots
gum confirm \
--default="no" \
"Delete Snapshot?" &&
gum spin \
--title="Deleting Snapshot..." \
--show-output \
-- vboxmanage snapshot "$vm_selected_uuid" delete "$snapshot_selected_uuid"
list_vms
}
# Init
list_vms