-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbhyve-run.sh
310 lines (242 loc) · 5.29 KB
/
bhyve-run.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
#! /bin/sh
# "THE BEER-WARE LICENSE" (Revision 42):
# <[email protected]> wrote this file. As long as you retain this notice
# you can do whatever you want with this stuff. If we meet some day, and you
# think this stuff is worth it, you can buy me a beer in return.
# Tobias Rehbein
BHYVE="/usr/sbin/bhyve"
BHYVECTL="/usr/sbin/bhyvectl"
CAT="/bin/cat"
GREP="/usr/bin/grep"
GRUBBHYVE="/usr/local/sbin/grub-bhyve"
ID="/usr/bin/id"
IFCONFIG="/sbin/ifconfig"
KLDSTAT="/sbin/kldstat"
LESS="/usr/bin/less"
PRINTF="/usr/bin/printf"
SYSCTL="/sbin/sysctl"
TRUNCATE="/usr/bin/truncate"
CONFIG="bhyve-run.conf"
f_usage () {
${CAT} >&2 <<-eom
bhyve-run.sh - run, install and destroy your grub-bhyve based vms
bhyve-run.sh -i -- install vm
bhyve-run.sh [-r] -- run vm
bhyve-run.sh -d -- destroy vm
Options are defined in a local "${CONFIG}" file.
eom
}
f_load_config () {
if [ -r "${CONFIG}" ]; then
. "${CONFIG}"
return 0
fi
return 1
}
f_check_grubbhybe () {
[ -x "${GRUBBHYVE}" ]
}
f_user_is_root () {
[ $(${ID} -u) -eq 0 ]
}
f_vmm_loaded () {
${KLDSTAT} -v | ${GREP} -q "vmm"
}
f_if_exists () {
if ! [ $# -eq 1 ]; then
echo "missing parameter: f_if_exists <interface>"
exit 1
fi
${IFCONFIG} "$1" >/dev/null 2>&1
}
f_bridge_has_member () {
if ! [ $# -eq 1 ]; then
echo "missing parameter: f_bridge_has_member <interface>"
exit 1
fi
f_if_exists "$1" || return 1
${IFCONFIG} ${BRIDGE} | grep -q "member: $1"
}
f_bridge_has_other_members () {
${IFCONFIG} ${BRIDGE} | \
grep -v "member: ${IF}" | \
grep -q "member: "
}
f_if_create () {
if ! [ $# -eq 1 ]; then
echo "missing parameter: f_if_create <interface>"
exit 1
fi
f_if_exists "$1" && return 1
${IFCONFIG} "$1" create
}
f_if_destroy () {
if ! [ $# -eq 1 ]; then
echo "missing parameter: f_if_destroy <interface>"
exit 1
fi
f_if_exists "$1" || return 1
${IFCONFIG} "$1" destroy
}
f_setup_network () {
f_if_create "${BRIDGE}"
f_if_create "${TAP}"
f_bridge_has_member "${TAP}" || ${IFCONFIG} ${BRIDGE} addm ${TAP}
f_bridge_has_member "${IF}" || ${IFCONFIG} ${BRIDGE} addm ${IF}
${SYSCTL} net.link.tap.user_open=1 >/dev/null
${SYSCTL} net.link.tap.up_on_open=1 > /dev/null
${IFCONFIG} ${TAP} up
${IFCONFIG} ${BRIDGE} up
}
f_teardown_network () {
f_if_destroy "${TAP}"
if ! f_bridge_has_other_members; then
f_if_destroy "${BRIDGE}"
fi
cat >&2 <<-eom
You might want to reset the sysctls to 0:
sysctl net.link.tap.user_open=0
sysctl net.link.tap.up_on_open=0
eom
}
f_run_grubbhyve () {
if ! [ $# -eq 2 ]; then
echo "missing parameter: f_run_grubbhyve <root device> <param>"
exit 1
fi
if [ -z "$2" ]; then
${GRUBBHYVE} -m "${MAP}" -M "${MEM}" -r "$1" "${NAME}"
elif [ "$2" = "|less" ]; then
${GRUBBHYVE} -m "${MAP}" -M "${MEM}" -r "$1" "${NAME}" | ${LESS}
else
printf "c\n%s\n" "$2" | \
${GRUBBHYVE} -m "${MAP}" -M "${MEM}" -r "$1" "${NAME}"
fi
}
f_run_bhyve () {
${BHYVE} -c "${CPUS}" -m "${MEM}" -A -I -H \
-s 0,hostbridge -s 2,virtio-blk,"${IMG}" \
-s 3,virtio-net,"${TAP}" -s 4,ahci-cd,"${ISO}" \
-S 31,uart,stdio "${NAME}"
}
f_vm_running () {
[ -e "/dev/vmm/${NAME}" ]
}
f_if_active () {
if ! [ $# -eq 1 ]; then
echo "missing parameter: f_if_active <interface>"
exit 1
fi
f_if_exists "$1" || return 1
${IFCONFIG} "$1" | grep -q "status: active"
}
f_install_vm () {
cat >"${MAP}" <<-eof
(hd0) ${IMG}
(cd0) ${ISO}
eof
${TRUNCATE} -s "${IMGSIZE}" "${IMG}"
f_setup_network
f_run_grubbhyve "cd0" "${GRUB_INSTALL}"
f_run_bhyve
}
f_run_vm () {
if f_vm_running; then
echo "VM is already running." >&2
exit 1
fi
if f_if_active "${TAP}"; then
echo "Interface ${TAP} already in use." >&2
exit 1
fi
if [ ! \( -r "${MAP}" -a -r "${IMG}" \) ]; then
echo "VM seems not to be installed." >&2
exit 1
fi
f_setup_network
f_run_grubbhyve "${GRUB_RUN_ROOT}" "${GRUB_RUN}"
f_run_bhyve
}
f_destroy_vm () {
if f_vm_running; then
${BHYVECTL} --destroy --vm="${NAME}"
fi
f_teardown_network
}
if ! f_vmm_loaded; then
${CAT} >&2 <<-eom
"vmm.ko" has to be loaded. To do so, run:
kldload vmm
eom
exit 1
fi
if ! f_user_is_root; then
echo "You must be root to run this script." >&2
exit 1
fi
if ! f_check_grubbhybe; then
${CAT} >&2 <<-eom
grub-bhyve loader not. You can install it using something along the
lines of:
cd /usr/ports/sysutils/grub2-bhyve
make install clean
eom
exit 1
fi
if ! f_load_config; then
${CAT} >&2 <<-eom
Configuration file "${CONFIG}" not found.
Example:
cat >${CONFIG} <<eof
NAME="ubuntu"
TAP="tap0"
IF="lagg0"
BRIDGE="bridge0"
IMG="\${NAME}.img"
IMGSIZE="8G"
ISO=./ubuntu.iso
MAP=./device.map
CPUS=2
MEM=2048
GRUB_INSTALL="linux /install/vmlinuz file=/cdrom/preseed/ubuntu-server.seed
quiet --
initrd /install/initrd.gz
boot"
GRUB_RUN="insmod gzio
insmod part_msdos
insmod ext2
set root='(hd0,msdos1)'
search --no-floppy --fs-uuid --set=root 021b8c8a-fa59-4e30-b328-515fa86d3c49
linux /boot/vmlinuz-3.11.0-15-generic
root=UUID=021b8c8a-fa59-4e30-b328-515fa86d3c49 ro
initrd /boot/initrd.img-3.11.0-15-generic
boot"
eof
eom
exit 1
fi
while getopts "irdh" opt; do
case $opt in
i)
f_install_vm
exit 0
;;
r)
f_run_vm
exit 0
;;
d)
f_destroy_vm
exit 0
;;
h)
f_usage
exit 0
;;
*)
f_usage
exit 1
;;
esac
done
f_run_vm