forked from chazy/kvmperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-guest.sh
executable file
·242 lines (215 loc) · 4.96 KB
/
run-guest.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
#!/bin/bash
SMP=2
MEMSIZE=512
DTB=/root/a15x2.dtb
KERNEL=zImage
HUGETLBFS=0
HUGE=""
VIRTIO=1
NOVIRTIO=0
CONSOLE=1
CONSOLE_FILE=""
IO_PARAMS=""
NET=1
APPEND=""
GDB=0
usage() {
U=""
if [[ -n "$1" ]]; then
U="${U}$1\n\n"
fi
U="${U}Usage: $0 [options] <guest-name> \n\n"
U="${U}Options:\n"
U="$U -c | --CPU <nr>: Number of cores (default 2)\n"
U="$U -m | --mem <MB>: Memory size (default 512)\n"
U="$U -H | --hugetlbfs: Use hugetlbfs on /hugetlbfs\n"
U="$U -v | --virtio: Use virtio block devices and networking\n"
U="$U --novirtio: Don't use virtio (overrides --virtio)\n"
U="$U --no-net: Don't setup any guest network\n"
U="$U --gdb: Run QEMU in GDB\n"
U="$U --no-console: Don't output any console\n"
U="$U -c | --console <file>: Output console to <file>\n"
U="$U -h | --help: Show this output\n"
U="${U}\n"
U="${U}When specifying the guest-name, the system expects a file named\n"
U="${U}<guest-name>.img which is a raw file system image of some sort\n"
U="${U}which the guest kernel can read.\n"
echo -e "$U" >&2
}
while :
do
case "$1" in
-c | --cpu)
SMP="$2"
shift 2
;;
-m | --mem)
MEMSIZE="$2"
shift 2
;;
-H | --hugetlbfs)
HUGETLBFS=1
shift 1
;;
-v | --virtio)
if [[ $NOVIRTIO == 0 ]]; then
VIRTIO=1
else
echo "warning: Overriding virtio with --novirtio" >&2
fi
shift 1
;;
--no-net)
NET=0
shift 1
;;
--gdb)
GDB=1
shift 1
;;
--novirtio)
VIRTIO=0
NOVIRTIO=1
shift 1
;;
--no-console)
CONSOLE=0
shift 1
;;
-c | --console)
if [[ $CONSOLE == 0 ]]; then
usage "Paremeter conflict: $1 and --no-console"
exit 1
fi
CONSOLE_FILE="$2"
shift 2
;;
--append)
APPEND="$2"
shift 2
;;
-h | --help)
usage ""
exit 1
;;
--) # End of all options
shift
break
;;
-*) # Unknown option
echo "Error: Unknown option: $1" >&2
exit 1
;;
*)
GUEST="$1"
shift 1
break
;;
esac
done
if [[ -z "$GUEST" ]]; then
usage "error: guest not specified"
exit 1
elif [[ -n "$1" ]]; then
usage "error: unknown option: $1"
exit 1
fi
IMG="$GUEST.img"
CONFFILE="$GUEST.conf"
ROOT_PART=""
rm -f $CONFFILE
echo "# qemu config file" >> $CONFFILE
function write_config_section()
{
__SECTION="$1"
echo -en "\n[${__SECTION}" >> $CONFFILE
shift 1
if [[ "$1" == "--id" ]]; then
echo -n " \"${2}\"" >> $CONFFILE
shift 2
fi
echo "]" >> $CONFFILE
while [[ -n "$1" && -n "$2" ]]; do
echo " $1 = \"$2\"" >> $CONFFILE
shift 2
done
}
function write_id_section()
{
___SECTION="$1"
___ID="$2"
shift 2
write_config_section "$___SECTION" --id "$___ID" "$@"
}
function write_section()
{
write_config_section "$@"
}
MACFILE=".$GUEST.mac"
if [[ -f "$MACFILE" ]]; then
MAC=`cat $MACFILE`
else
MAC=`printf 'DE:AD:BE:EF:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))`
echo "$MAC" > "$MACFILE"
fi
if [[ $HUGETLBFS == 1 ]]; then
CUR=`cat /proc/sys/vm/nr_hugepages`
NRPAGES=$(((MEMSIZE + 128) / 2))
if [[ $CUR -lt $NRPAGES ]]; then
echo $NRPAGES > /proc/sys/vm/nr_hugepages
fi
HUGE="-mem-path /hugetlbfs"
fi
BOOT_CMD="console=ttyAMA0 mem=${MEMSIZE}M earlyprintk debug"
if [[ $VIRTIO == 1 ]]; then
#IO_PARAMS="$IO_PARAMS -drive if=none,file=${IMG},id=vda"
write_id_section drive vda if none file "$IMG" cache writethrough
#IO_PARAMS="$IO_PARAMS -device virtio-blk,transport=virtio-mmio.0,drive=vda"
write_section device driver virtio-blk transport virtio-mmio.0 drive vda
if [[ $NET == 1 ]]; then
#IO_PARAMS="$IO_PARAMS -netdev tap,id=tap0"
write_id_section netdev tap0 "type" tap
#IO_PARAMS="$IO_PARAMS -device virtio-net,transport=virtio-mmio.1,netdev=tap0,mac=$MAC"
write_section device driver virtio-net transport virtio-mmio.1 netdev tap0 mac $MAC
else
echo "warning: no network!" >&2
fi
BOOT_CMD="$BOOT_CMD virtio_mmio.device=1K@0x4e000000:74:0"
BOOT_CMD="$BOOT_CMD virtio_mmio.device=1K@0x4e100000:75:1"
BOOT_CMD="$BOOT_CMD virtio_mmio.device=1K@0x4e200000:76:2"
BOOT_CMD="$BOOT_CMD root=/dev/vda${ROOT_PART} rw rootfstype=ext4"
else
IO_PARAMS="$IO_PARAMS -sd $IMG"
if [[ $NET == 1 ]]; then
IO_PARAMS="$IO_PARAMS -net nic"
IO_PARAMS="$IO_PARAMS -net tap,vlan=0,ifname=tap0,downscript=no"
fi
if [[ -n "$ROOT_PART" ]]; then
ROOT_PART="p$ROOT_PART"
fi
BOOT_CMD="$BOOT_CMD root=/dev/mmcblk0${ROOT_PART} rw"
fi
BOOT_CMD="$BOOT_CMD init=/sbin/init --no-log noplymouth"
#(write_section machine dtb $DTB kernel $KERNEL accel kvm append "$BOOT_CMD" kernel_irqchip on)
(write_section machine dtb $DTB kernel $KERNEL accel kvm append "$BOOT_CMD")
if [[ $CONSOLE == 0 ]]; then
CONS="-serial null -display none"
elif [[ -n "$CONSOLE_FILE" ]]; then
CONS="-serial file:$CONSOLE_FILE -display none"
else
CONS="-nographic"
fi
#./qemu-system-arm \
ARGS="\
-smp $SMP \
$IO_PARAMS \
$HUGE \
-m $MEMSIZE -M vexpress-a15 -cpu cortex-a15 \
-readconfig $CONFFILE \
$CONS"
echo $ARGS
if [[ $GDB == 1 ]]; then
gdb --args ./qemu-system-arm $ARGS
else
./qemu-system-arm $ARGS
fi