-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstart-qemu.sh.in
executable file
·521 lines (460 loc) · 11.7 KB
/
start-qemu.sh.in
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env bash
usage() {
local old_xtrace
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
{
echo "${script_name} - Run Linux in QEMU."
echo "Usage: ${script_name} [flags]"
echo "Option flags:"
echo " -a --arch - Target architecture. Default: '${target_arch}'."
echo " -c --kernel-cmd - Kernel command line options. Default: '${kernel_cmd}'."
echo " -e --ether-mac - QEMU Ethernet MAC. Default: '${ether_mac}'."
echo " -f --hostfwd-offset - QEMU hostfwd port offset. Default: '${hostfwd_offset}'."
echo " -i --initrd - Initrd image. Default: '${initrd}'."
echo " -k --kernel - Kernel image. Default: '${kernel}'."
# TODO echo " -m --modules - Kernel modules directory. To mount over existing modules directory. Default: '${modules}'."
echo " -o --out-file - stdout, stderr redirection file. Default: '${out_file}'."
echo " -s --systemd-debug - Run systemd with debug options. Default: '${systemd_debug}'."
echo " -t --qemu-tap - Use QEMU tap networking. Default: '${qemu_tap}'."
echo " --hda - QEMU IDE hard disk image hda. Default: '${hda}'."
echo " --hdb - QEMU IDE hard disk image hdb. Default: '${hdb}'."
echo " --hdc - QEMU IDE hard disk image hdc. Default: '${hdc}'."
echo " --cdrom - CDROM disk image. Default: '${cdrom}'."
echo " --efi-vars - QEMU EFI variables image. Default: '${efi_vars}'."
echo " --hda-boot - Boot from disk image hda. Default: '${hda_boot}'."
echo " --cdrom-boot - Boot from CDROM disk image. Default: '${cdrom_boot}'."
echo " --pid-file - PID file. Default: '${pid_file}'."
echo " --p9-share - Plan9 share directory. Default: '${p9_share}'."
echo " --nographic - Pass -nographic to QEMU. Default: '${nographic}'."
echo " -h --help - Show this help and exit."
echo " -v --verbose - Verbose execution."
echo " -g --debug - Extra verbose execution."
echo " -d --dry-run - Dry run, don't run qemu."
echo "Info:"
echo ' @PACKAGE_NAME@ v@PACKAGE_VERSION@'
echo ' @PACKAGE_URL@'
echo " Send bug reports to: Geoff Levand <geoff@infradead.org>."
} >&2
eval "${old_xtrace}"
}
process_opts() {
local short_opts="a:c:e:f:i:k:m:o:r:sthvgd"
local long_opts="arch:,kernel-cmd:,ether-mac:,hostfwd-offset:,\
initrd:,kernel:,modules:,out-file:,systemd-debug,qemu-tap,hda:,hdb:,hdc:,\
cdrom:,efi-vars:,hda-boot,cdrom-boot,pid-file:,p9-share:,nographic,\
help,verbose,debug,dry-run"
local opts
opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${script_name}" -- "$@")
eval set -- "${opts}"
while true ; do
case "${1}" in
-a | --arch)
target_arch="$(get_arch "${2}")"
shift 2
;;
-c | --kernel-cmd)
kernel_cmd="${2}"
shift 2
;;
-e | --ether-mac)
ether_mac="${2}"
shift 2
;;
-f | --hostfwd-offset)
hostfwd_offset="${2}"
shift 2
;;
-i | --initrd)
initrd="${2}"
shift 2
;;
-k | --kernel)
kernel="${2}"
shift 2
;;
-m | --modules)
modules="${2}"
shift 2
;;
-o | --out-file)
out_file="${2}"
shift 2
;;
-s | --systemd-debug)
systemd_debug=1
shift
;;
-t | --qemu-tap)
qemu_tap=1
shift
;;
--hda)
hda="${2}"
shift 2
;;
--hdb)
hdb="${2}"
shift 2
;;
--hdc)
hdc="${2}"
shift 2
;;
--cdrom)
cdrom="${2}"
shift 2
;;
--efi-vars)
efi_vars="${2}"
shift 2
;;
--hda-boot)
hda_boot=1
shift
;;
--cdrom-boot)
cdrom_boot=1
shift
;;
--pid-file)
pid_file="${2}"
shift 2
;;
--p9-share)
p9_share="${2}"
shift 2
;;
--nographic)
nographic=1
shift
;;
-h | --help)
usage=1
shift
;;
-v | --verbose)
verbose=1
shift
;;
-g | --debug)
verbose=1
debug=1
set -x
shift
;;
-d | --dry-run)
dry_run=1
shift
;;
--)
shift
extra_args="${*}"
break
;;
*)
echo "${script_name}: ERROR: Internal opts: '${*}'" >&2
exit 1
;;
esac
done
}
setup_efi() {
local efi_code_src
local efi_vars_src
local efi_full_src
case "${target_arch}" in
amd64)
efi_code_src="/usr/share/OVMF/OVMF_CODE.fd"
efi_vars_src="/usr/share/OVMF/OVMF_VARS.fd"
efi_full_src="/usr/share/ovmf/OVMF.fd"
;;
arm64)
efi_code_src="/usr/share/AAVMF/AAVMF_CODE.fd"
efi_vars_src="/usr/share/AAVMF/AAVMF_VARS.fd"
efi_full_src="/usr/share/qemu-efi-aarch64/QEMU_EFI.fd"
;;
esac
check_file "${efi_code_src}" 'EFI code' ''
check_file "${efi_vars_src}" 'EFI variables' ''
efi_code="${efi_code_src}"
if [[ ! ${distro_test} && ! -e ${efi_vars} ]]; then
copy_file "${efi_vars_src}" "${efi_vars}"
fi
}
on_exit() {
local result=${1}
if [[ -d "${tmp_dir:-}" ]]; then
if [[ ${keep_tmp_dir:-} ]]; then
echo "${script_name}: INFO: tmp dir preserved: '${tmp_dir}'" >&2
else
rm -rf "${tmp_dir:?}"
fi
fi
set +x
echo "${script_name}: Done: ${result}" >&2
}
on_err() {
local f_name=${1}
local line_no=${2}
local err_no=${3}
if [[ ${debug:-} ]]; then
echo '------------------------' >&2
set >&2
echo '------------------------' >&2
fi
echo "${script_name}: ERROR: (${err_no}) at ${f_name}:${line_no}." >&2
exit ${err_no}
}
#===============================================================================
export PS4='\[\e[0;33m\]+ ${BASH_SOURCE##*/}:${LINENO}:(${FUNCNAME[0]:-main}):\[\e[0m\] '
script_name="${0##*/}"
base_name="${script_name##*/%}"
base_name="${base_name%.sh*}"
real_source="$(realpath "${BASH_SOURCE}")"
SCRIPT_TOP="$(realpath "${SCRIPT_TOP:-${real_source%/*}}")"
start_time="$(date +%Y.%m.%d-%H.%M.%S)"
SECONDS=0
trap "on_exit 'Failed'" EXIT
trap 'on_err ${FUNCNAME[0]:-main} ${LINENO} ${?}' ERR
set -eE
set -o pipefail
set -o nounset
source "${SCRIPT_TOP}/tdd-lib/util.sh"
host_arch=$(get_arch "$(uname -m)")
target_arch="${host_arch}"
kernel_cmd=''
ether_mac='01:02:03:00:00:01'
hostfwd_offset='20000'
initrd=''
kernel=''
modules=''
out_file=''
systemd_debug=''
qemu_tap=''
hda=''
hdb=''
hdc=''
cdrom=''
efi_vars="$(pwd)/${target_arch}-EFI_VARS.fd"
hda_boot=''
cdrom_boot=''
pid_file=''
p9_share=''
nographic=''
usage=''
verbose=''
debug=''
dry_run=''
# TODO
use_virtio_net=''
disk_image=''
distro_test=''
process_opts "${@}"
TARGET_HOSTNAME=${TARGET_HOSTNAME:-"tdd-tester"}
MODULES_ID=${MODULES_ID:-"kernel_modules"}
P9_SHARE_ID=${P9_SHARE_ID:-"p9_share"}
qemu_append_args=''
if [[ ${systemd_debug} ]]; then
# FIXME: need to run set-systemd-debug.sh???
kernel_cmd+=" systemd.log_level=debug systemd.log_target=console systemd.journald.forward_to_console=1"
fi
if [[ -n "${usage}" ]]; then
usage
trap - EXIT
exit 0
fi
if [[ ${extra_args} ]]; then
echo "${script_name}: ERROR: Got extra args: '${extra_args}'" >&2
usage
exit 1
fi
case ${target_arch} in
arm64|ppc*)
;;
amd64)
;;
*)
echo "${script_name}: ERROR: Unsupported target arch '${target_arch}'." >&2
exit 1
;;
esac
if [[ "${hda_boot:+1}${cdrom_boot:+1}${kernel:+1}" != '1' ]]; then
echo "${script_name}: ERROR: Must provide only one of: --hda-boot, --cdrom-boot, or --kernel." >&2
usage
exit 1
fi
if [[ "${hda_boot:+1}${cdrom_boot:+1}${initrd:+1}" && "${hda_boot:+1}${cdrom_boot:+1}${initrd:+1}" != '1' ]]; then
echo "${script_name}: ERROR: Must provide only one of: --hda-boot, --cdrom-boot, or --initrd." >&2
usage
exit 1
fi
case "${host_arch}--${target_arch}" in
amd64--amd64)
have_efi=1
qemu_exe='qemu-system-x86_64'
qemu_args+=' -machine accel=kvm -cpu host -m 8192 -smp 2'
qemu_args+=' -device i6300esb,id=watchdog0 -watchdog-action reset'
;;
arm64--amd64)
have_efi=1
qemu_exe='qemu-system-x86_64'
qemu_args+=' -machine pc-q35-2.8 -cpu kvm64 -m 8192 -smp 2'
;;
amd64--arm64)
have_efi=1
qemu_exe='qemu-system-aarch64'
#qemu_mem="${qemu_mem:-5120}" # 5G
qemu_mem="${qemu_mem:-6144}" # 6G
#qemu_mem="${qemu_mem:-16384}" # 16G
qemu_args+=' -machine virt,gic-version=3 -cpu cortex-a57 -m ${qemu_mem} -smp 2'
;;
arm64--arm64)
have_efi=1
qemu_exe='qemu-system-aarch64'
qemu_args+=' -machine virt,gic-version=3,accel=kvm -cpu host -m 4096 -smp 2'
;;
amd64--ppc32)
have_efi=''
qemu_exe='qemu-system-ppc'
qemu_args+=' -machine g3beige -m 2047'
kernel_cmd+=' root=/dev/ram0 console=hvc0'
;;
amd64--ppc64)
have_efi=''
qemu_exe='qemu-system-ppc64'
#qemu_args+=' -machine cap-htm=off -m 8192'
#qemu_args+=' -machine pseries,cap-htm=off -m 8192'
qemu_args+=' -machine pseries,cap-htm=off -m 8192'
kernel_cmd+=' root=/dev/ram0 console=hvc0'
;;
*)
echo "${script_name}: ERROR: Unsupported host--target combo: '${host_arch}--${target_arch}'." >&2
exit 1
;;
esac
nic_model=${nic_model:-"virtio-net-pci"}
if [[ ${qemu_tap} ]]; then
qemu_args+=" \
-netdev tap,id=tap0,ifname=qemu0,br=br0 \
-device ${nic_model},netdev=tap0,mac=${ether_mac} \
"
else
ssh_fwd=$(( ${hostfwd_offset} + 22 ))
echo "${script_name}: SSH fwd = ${ssh_fwd}" >&2
# FIXME: When is -nic unsupported?
if [[ ${use_virtio_net} ]]; then
#virtio_net_type="virtio-net-device"
virtio_net_type="virtio-net-pci"
qemu_args+=" -netdev user,id=eth0,hostfwd=tcp::${ssh_fwd}-:22,hostname=${TARGET_HOSTNAME}"
qemu_args+=" -device ${virtio_net_type},netdev=eth0"
else
qemu_args+=" -nic user,model=${nic_model},hostfwd=tcp::${ssh_fwd}-:22,hostname=${TARGET_HOSTNAME}"
fi
fi
if [[ ${kernel} ]]; then
check_file "${kernel}" ' --kernel' '1'
qemu_args+=" -kernel ${kernel}"
if [[ ${hda_boot} ]]; then
qemu_append_args+=" root=/dev/vda rw"
fi
qemu_append_args+=" ${kernel_cmd}"
fi
if [[ ${initrd} ]]; then
check_file "${initrd}" ' --initrd' '1'
qemu_args+=" -initrd ${initrd}"
fi
if [[ ${hda} ]]; then
check_file "${hda}" ' --hda' '1'
qemu_args+=" -hda ${hda}"
fi
if [[ ${hdb} ]]; then
check_file "${hdb}" ' --hdb' '1'
qemu_args+=" -hdb ${hdb}"
fi
if [[ ${hdc} ]]; then
check_file "${hdc}" ' --hdc' '1'
qemu_args+=" -hdc ${hdc}"
fi
if [[ ${cdrom} ]]; then
check_file "${cdrom}" ' --cdrom' '1'
qemu_args+=" -cdrom ${cdrom}"
fi
if [[ ${hda_boot} ]]; then
if [[ ! ${hda} ]]; then
echo "${script_name}: ERROR: --hda-boot requires --hda'." >&2
usage
exit 1
fi
qemu_args+=" -boot c"
fi
if [[ ${cdrom_boot} ]]; then
if [[ ! ${cdrom} ]]; then
echo "${script_name}: ERROR: --cdrom-boot requires --cdrom'." >&2
usage
exit 1
fi
qemu_args+=" -boot d"
fi
if [[ ${p9_share} ]]; then
check_directory "${p9_share}" ' --p9-share' '1'
qemu_args+=" \
-virtfs local,id=${P9_SHARE_ID},path=${p9_share},security_model=none,mount_tag=${P9_SHARE_ID}"
echo "${script_name}: INFO: 'mount -t 9p -o trans=virtio ${P9_SHARE_ID} <mount-point> -oversion=9p2000.L'" >&2
fi
if [[ ${modules} ]]; then
check_directory "${modules}" ' --modules' '1'
qemu_args+=" \
-virtfs local,id=${MODULES_ID},path=${modules},security_model=none,mount_tag=${MODULES_ID}"
fi
if [[ ${disk_image} ]]; then # TODO
qemu_args+=" \
-drive if=none,id=blk,file=${disk_imag} \
-device virtio-blk-device,drive=blk \
"
fi
if [[ ${out_file} ]]; then
qemu_args+=" \
-display none \
-chardev file,id=char0,path=${out_file} \
-serial chardev:char0 \
"
fi
if [[ ${nographic} ]]; then
qemu_args+=" -nographic"
fi
if [[ ${pid_file} ]]; then
qemu_args+=" -pidfile ${pid_file}"
fi
if [[ ${have_efi} ]]; then
setup_efi
qemu_args+=" -drive if=pflash,file=${efi_code},format=raw,readonly=on"
qemu_args+=" -drive if=pflash,file=${efi_vars},format=raw"
fi
if [[ ${verbose} ]]; then
{
ls -l /dev/kvm || :
grep 'kvm' /etc/group || :
id
} >&2
fi
cmd="${qemu_exe} \
-name tdd-vm \
-object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0 \
${qemu_args} \
${qemu_append_args:+-append "'"${qemu_append_args}"'"} \
"
echo "${script_name}: INFO: Starting QEMU..." >&2
if [[ ${dry_run} || ${verbose} ]]; then
{
echo '-------------------------'
echo "${cmd}"
echo '-------------------------'
} >&1
fi
if [[ ! ${dry_run} ]]; then
eval exec "${cmd}"
fi
trap "on_exit 'Success.'" EXIT
exit 0