This repository was archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualbox.sh
More file actions
194 lines (178 loc) · 5.63 KB
/
virtualbox.sh
File metadata and controls
194 lines (178 loc) · 5.63 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
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
#----------------------------------------------------------------------
# virtualbox.sh
#
# Functions to simplify interaction with virtualbox. Basically some
# wrappers around the "VBoxManage" command-line.
#
# It is easiest to work with a virtual machine's uuid rather than the
# name (since the uuid doesn't have spaces). Therefore for all the
# functions other than vbox_get_uuid() a VM is always referenced by
# the uuid.
# ----------------------------------------------------------------------
#----------------------------------------------------------------------
# vbox_get_uuid <vm-name>
#
# Returns the uuid corresponding to the VM name. This is probably the
# first thing to use. From then on you can work with the uuid.
#
# Must be called using the $(...) pattern. Returns "" on any error.
#----------------------------------------------------------------------
vbox_get_uuid () {
local vmn="$1"
if [ "$vmn" == "" ]; then
echo ""
return 1
fi
local match=$(vboxmanage list vms | grep -m 1 "$vmn")
if [ "$match" == "" ]; then
echo ""
return 1
fi
local uuid=$(echo "$match" | sed -n 's/^[^{]*{\([^}]*\)}/\1/p')
echo "$uuid"
return 0
}
#----------------------------------------------------------------------
# vbox_get_vmname <uuid>
#
# Returns the vm name corresponding to the VM uuid. Must be called using the
# $(...) pattern. Returns "" if there is any error.
#----------------------------------------------------------------------
vbox_get_vmname () {
local uuid="$1"
if [ "$uuid" == "" ]; then
echo ""
return 1
fi
local match=$(vboxmanage list vms | grep -m 1 "$uuid")
if [ "$match" == "" ]; then
echo ""
return 1
fi
local vmname=$(echo "$match" | sed -n 's/^\"\([^\"]*\)\" [^{]*{[^}]*}/\1/p')
echo "$vmname"
return 0
}
#----------------------------------------------------------------------
# vbox_is_vm <uuid>
#
# Returns 0 (true) if the uuid corresponds to a valid registered VM.
# returns 1 (false) otherwise
#----------------------------------------------------------------------
vbox_is_vm () {
local uuid="$1"
if [ "$uuid" == "" ]; then
echo "$FUNCNAME: missing virtual machine: uuid"
return 1
fi
local match=$(vboxmanage list vms | grep -m 1 "$uuid")
if [ "$match" == "" ]; then
return 1
fi
local matcheduuid=$(echo "$match" | sed -n 's/^[^{]*{\([^}]*\)}/\1/p')
if [ "$uuid" == "$matcheduuid" ]; then
return 0
fi
return 1
}
#----------------------------------------------------------------------
# vbox_is_running <uuid>
#
# Returns 0 if the named virtual machine is running. Returns 1
# otherwise and if there is some error prints a warning.
#----------------------------------------------------------------------
vbox_is_running () {
local uuid="$1"
if ! vbox_is_vm "$uuid"; then
echo "$FUNCNAME: invalid virtual machine: $uuid"
return 1
fi
local running=$(vboxmanage list runningvms | grep -m 1 "$uuid")
if [ "$running" == "" ]; then
return 1
fi
return 0
}
#----------------------------------------------------------------------
# vbox_start_headless <uuid>
# vbox_start_gui <uuid>
#
# Starts the vm in different modes mode. Returns 0 on success otherwise
# prints error and returns 1.
#----------------------------------------------------------------------
vbox_start_headless () {
local uuid="$1"
vbox_start "$uuid" "headless"
}
vbox_start_gui () {
local uuid="$1"
vbox_start "$uuid" "gui"
}
#----------------------------------------------------------------------
# vbox_start <uuid> [gui|sdl|headless]
#
# Starts the vm in headless mode. Returns 0 on success otherwise
# prints error and returns 1.
#----------------------------------------------------------------------
vbox_start () {
local uuid="$1"
local type="$2"
if [ "$type" == "" ]; then
type="gui"
fi
if [ "$type" != "gui" ] && [ "$type" != "headless" ] &&
[ "$type" != "sdl" ]; then
echo "$FUNCNAME: invalid virtual machine start type: $type"
return 1
fi
if ! vbox_is_vm "$uuid"; then
echo "$FUNCNAME: invalid virtual machine: $uuid"
return 1
fi
if vbox_is_running "$uuid"; then
echo "$FUNCNAME: virtual machine already running: $uuid"
return 1
fi
vboxmanage startvm "$uuid" --type "$type"
return 0
}
#----------------------------------------------------------------------
# vbox_shutdown_savestate <uuid>
# vbox_shutdown_acpipowerdown <uuid>
#
# 1- Shutsdown a running VM by saving the state. On the next start the
# VM will be resumed from the saved state.
# 2 - Shutdown a running vm by issuing a ACPI power button call. This is
# the cleanest way to shutdown the VM.
#
#----------------------------------------------------------------------
vbox_shutdown_savestate () {
local uuid="$1"
vbox_shutdown "$uuid" "savestate"
}
vbox_shutdown_acpipowerbutton () {
local uuid="$1"
vbox_shutdown "$uuid" "acpipowerbutton"
}
#----------------------------------------------------------------------
# vbox_shutdown <uuid> [acpipowerbutton|savestate]
#
# Different ways of shutting down a VM. Currently supported are
# ACPI and savestate.
#
# Returns 0 on success otherwise prints error and returns 1.
#----------------------------------------------------------------------
vbox_shutdown () {
local uuid="$1"
local type="$2"
if [ "$type" != "acpipowerbutton" ] && [ "$type" != "savestate" ]; then
echo "$FUNCNAME: invalid virtual machine shutdown type: $type"
return 1
fi
if ! vbox_is_running "$uuid"; then
echo "$FUNCNAME: virtual machine is not running: $uuid"
return 1
fi
vboxmanage controlvm "$uuid" "$type"
return 0
}