-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvirtualbox
More file actions
executable file
·207 lines (177 loc) · 4.11 KB
/
virtualbox
File metadata and controls
executable file
·207 lines (177 loc) · 4.11 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
set -euEo pipefail
trap 'echo "${BASH_SOURCE:-unknown}:${LINENO:-unknown}: $BASH_COMMAND";' ERR
readonly usage="
Usage: $(basename "$0") [<GENERAL OPTIONS>] <OPERATIONS> <OPTIONS>
GENERAL OPTIONS:
-h, --help Print this help
OPERATIONS:
create Create the VM
-n, --name <name> The VM name
-d, --directory <path> The VM base directory
-i, --iso <path> The VM ISO image
delete Delete the VM
-n, --name <name> The VM name
start Start the VM
-n, --name <name> The VM name
poweroff Poweroff the VM
-n, --name <name> The VM name
"
help_and_exit() {
echo "$usage"
exit 0
}
err_with_msg() {
echo >&2 "$1"
exit 1
}
err_with_help() {
echo >&2 "$1"
echo >&2 "$usage"
exit 1
}
# Exit if a operation or option not provided
if [[ "$#" -lt 1 ]]; then
err_with_help "error: you must provide a valid operation or option"
fi
# Print the program usage
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
help_and_exit
fi
# Get option and setup options
readonly operation="$1"
case "$operation" in
create)
readonly longoptions="name:,directory:,iso:"
readonly options="n:d:i:"
;;
delete | start | poweroff)
readonly longoptions="name:"
readonly options="n:"
;;
*)
err_with_help "error: unexpected operation on line ${LINENO}: $operation"
;;
esac
# Read options for operation
readonly arguments=("$@")
opts=$(
getopt \
--name "$(basename "$0") $operation" \
--longoptions "$longoptions" \
--options "$options" \
-- "${arguments[@]:1}"
)
eval set -- "$opts"
unset opts
while true; do
case "$1" in
-n | --name)
readonly name="$2"
shift 2
continue
;;
-d | --directory)
readonly directory="$2"
shift 2
continue
;;
-i | --iso)
readonly iso="$2"
shift 2
continue
;;
--)
shift
break
;;
*)
err_with_msg "error: unexpected option on line ${LINENO}: $1"
;;
esac
done
# Validate options for operations
case "$operation" in
create)
[[ -z ${name+x} ]] && err_with_help "error: name param is required"
[[ -z ${directory+x} ]] && err_with_help "error: directory param is required"
[[ -z ${iso+x} ]] && err_with_help "error: iso param is required"
;;
delete | start | poweroff)
[[ -z ${name+x} ]] && err_with_help "error: name param is required"
;;
*)
err_with_help "error: unexpected operation on line ${LINENO}: $operation"
;;
esac
operation_create() {
local -r vmname="$1"
local -r vmdirectory="$2"
local -r iso_path="$3"
VBoxManage createvm --name "${vmname}" \
--basefolder "${vmdirectory}" \
--ostype ArchLinux_64 \
--register \
;
VBoxManage modifyvm "${vmname}" \
--accelerate2dvideo on \
--accelerate3d on \
--acpi on \
--boot1 dvd \
--boot2 disk \
--cpus 2 \
--firmware efi \
--graphicscontroller vmsvga \
--memory 2048 \
--nic1 nat \
--vram 128 \
;
VBoxManage createmedium disk \
--filename "${vmdirectory}/${vmname}/disk.vdi" \
--format VDI \
--size 16000 \
;
VBoxManage storagectl "${vmname}" \
--add sata \
--bootable on \
--name SATA \
;
VBoxManage storageattach "${vmname}" \
--device 0 \
--medium "${vmdirectory}/${vmname}/disk.vdi" \
--nonrotational on \
--port 0 \
--storagectl SATA \
--type hdd \
;
VBoxManage storagectl "${vmname}" \
--add ide \
--bootable on \
--name IDE \
;
VBoxManage storageattach "${vmname}" \
--device 1 \
--medium "$iso_path" \
--port 1 \
--storagectl IDE \
--type dvddrive \
;
}
# Execute operation
case "$operation" in
create)
operation_create "$name" "$directory" "$iso"
;;
delete)
VBoxManage unregistervm "$name" --delete
;;
start)
VBoxManage startvm "$name" --type gui
;;
poweroff)
VBoxManage controlvm "$name" poweroff
;;
*)
err_with_help "error: unexpected operation on line ${LINENO}: $operation"
;;
esac