-
Notifications
You must be signed in to change notification settings - Fork 5
/
vm
executable file
·136 lines (116 loc) · 2.51 KB
/
vm
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
#!/bin/bash
#
# Copyright (c) 2015 SUSE Linux GmbH
#
# This file is part of the kernel-ci project and licensed under the GNU GPLv2
#
set -o nounset
SRC_ROOT=$(dirname ${BASH_SOURCE[0]})
source ${SRC_ROOT}/lib.sh
QEMU="qemu-system-"
ARCH="x86_64"
KERNEL=""
INITRD=""
ROOTFS=""
ROOT="/dev/vda3"
TIMEOUT=""
QEMUARGS=""
# TODO Add networking
usage()
{
prog=$(basename $0)
echo "$prog [-a arch] [-d] [-r rootfs] [-R root] -k <kernel> -i <initrd> [-q qemuargs]" >&2
echo " -a arch Architecture for QEMU." >&2
echo " -d Enable debug output." >&2
echo " -r rootfs Rootfs image to use." >&2
echo " -R root root= kernel cmdline option" >&2
echo " -k kernel kernel image to use." >&2
echo " -i initrd initrd image to use." >&2
echo " -q qemuargs Extra qemu arguments." >&2
}
while getopts "da:k:i:r:hR:t:q:" opt; do
case ${opt} in
a)
ARCH=$OPTARG
;;
d)
DEBUG="-d"
;;
k)
KERNEL=$OPTARG
;;
i)
INITRD=$OPTARG
;;
r)
ROOTFS=$OPTARG
;;
R)
ROOT=$OPTARG
;;
t)
TIMEOUT=$OPTARG
;;
q)
QEMUARGS=$OPTARG
;;
h)
usage
exit 1
;;
esac
done
if [ x"$KERNEL" == "x" ]; then
pr_err "No kernel option given"
usage
exit 1
fi
QEMU_ROOTFS=""
QEMU_DEVICE=""
if [ x"$ROOTFS" != "x" ]; then
QEMU_ROOTFS="-drive file="${ROOTFS}",if=none,id=drive-virtio-disk0 -snapshot"
if [ "$ARCH" == "aarch64" ]; then
QEMU_DEVICE="-device virtio-blk-pci,scsi=off,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1"
else
QEMU_DEVICE="-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1"
fi
fi
if [ x"$INITRD" == "x" ]; then
pr_err "No initrd option given"
usage
exit 1
fi
QUIET=quiet
if [ "$DEBUG" == "-d" ]; then
QUIET=debug
fi
if [ "$ARCH" == "aarch64" ]; then
QEMU_EXTRA="-machine virt -cpu host -bios /usr/share/qemu/qemu-uefi-aarch64.bin -enable-kvm"
CONSOLE="ttyAMA0"
else
QEMU_EXTRA="$QEMUARGS"
CONSOLE="ttyS0"
fi
QEMU_CMD=(
"$QEMU$ARCH"
$QEMU_EXTRA
# Disk
$QEMU_DEVICE
$QEMU_ROOTFS
# Serial output
-serial stdio -monitor none -nographic
# CPU & Memory
-smp 2 -m 1024
# Kernel & initrd
-append 'root='${ROOT}' ro console='${CONSOLE}' '${QUIET}' rdinit=/bin/init crashkernel=64M'
-kernel "$KERNEL"
-initrd "$INITRD"
)
pr_debug "Linux kernel to load: $KERNEL"
pr_debug "Initramfs to load: $INITRD"
pr_debug "${QEMU_CMD[@]}"
if test -z "$TIMEOUT" || test "$TIMEOUT" -eq 0; then
"${QEMU_CMD[@]}"
else
timeout --foreground --kill-after=10 "$TIMEOUT" "${QEMU_CMD[@]}"
fi