summaryrefslogtreecommitdiffstats
path: root/scripts/runqemu
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/runqemu')
-rwxr-xr-xscripts/runqemu507
1 files changed, 507 insertions, 0 deletions
diff --git a/scripts/runqemu b/scripts/runqemu
new file mode 100755
index 0000000000..ff64a1d4c2
--- /dev/null
+++ b/scripts/runqemu
@@ -0,0 +1,507 @@
1#!/bin/bash
2#
3# Handle running OE images standalone with QEMU
4#
5# Copyright (C) 2006-2011 Linux Foundation
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version 2 as
9# published by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20usage() {
21 MYNAME=`basename $0`
22 echo ""
23 echo "Usage: you can run this script with any valid combination"
24 echo "of the following options (in any order):"
25 echo " QEMUARCH - the qemu machine architecture to use"
26 echo " KERNEL - the kernel image file to use"
27 echo " ROOTFS - the rootfs image file or nfsroot directory to use"
28 echo " MACHINE - the machine name (optional, autodetected from KERNEL filename if unspecified)"
29 echo " RAMFS - boot a ramfs-based image"
30 echo " ISO - boot an ISO image"
31 echo " VM - boot a vmdk image"
32 echo " Simplified QEMU command-line options can be passed with:"
33 echo " nographic - disables video console"
34 echo " serial - enables a serial console on /dev/ttyS0"
35 echo " kvm - enables KVM when running qemux86/qemux86-64 (VT-capable CPU required)"
36 echo " publicvnc - enable a VNC server open to all hosts"
37 echo " qemuparams=\"xyz\" - specify custom parameters to QEMU"
38 echo " bootparams=\"xyz\" - specify custom kernel parameters during boot"
39 echo ""
40 echo "Examples:"
41 echo " $MYNAME qemuarm"
42 echo " $MYNAME qemux86-64 core-image-sato ext3"
43 echo " $MYNAME path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial"
44 echo " $MYNAME qemux86 ramfs"
45 echo " $MYNAME qemux86 iso"
46 echo " $MYNAME qemux86 qemuparams=\"-m 256\""
47 echo " $MYNAME qemux86 bootparams=\"psplash=false\""
48 echo " $MYNAME path/to/<image>-<machine>.vmdk"
49 exit 1
50}
51
52if [ "x$1" = "x" ]; then
53 usage
54fi
55
56error() {
57 echo "Error: "$*
58 usage
59}
60
61MACHINE=${MACHINE:=""}
62KERNEL=${KERNEL:=""}
63ROOTFS=${ROOTFS:=""}
64VM=${VM:=""}
65FSTYPE=""
66LAZY_ROOTFS=""
67SCRIPT_QEMU_OPT=""
68SCRIPT_QEMU_EXTRA_OPT=""
69SCRIPT_KERNEL_OPT=""
70SERIALSTDIO=""
71KVM_ENABLED="no"
72KVM_ACTIVE="no"
73
74# Determine whether the file is a kernel or QEMU image, and set the
75# appropriate variables
76process_filename() {
77 filename=$1
78
79 # Extract the filename extension
80 EXT=`echo $filename | awk -F . '{ print \$NF }'`
81 case /$EXT/ in
82 /bin/)
83 # A file ending in .bin is a kernel
84 [ -z "$KERNEL" ] && KERNEL=$filename || \
85 error "conflicting KERNEL args [$KERNEL] and [$filename]"
86 ;;
87 /ext[234]/|/jffs2/|/btrfs/)
88 # A file ending in a supportted fs type is a rootfs image
89 if [ -z "$FSTYPE" -o "$FSTYPE" = "$EXT" ]; then
90 FSTYPE=$EXT
91 ROOTFS=$filename
92 else
93 error "conflicting FSTYPE types [$FSTYPE] and [$EXT]"
94 fi
95 ;;
96 /vmdk/)
97 FSTYPE=$EXT
98 VM=$filename
99 ;;
100 *)
101 error "unknown file arg [$filename]"
102 ;;
103 esac
104}
105
106# Parse command line args without requiring specific ordering. It's a
107# bit more complex, but offers a great user experience.
108while true; do
109 arg=${1}
110 case "$arg" in
111 "qemux86" | "qemux86-64" | "qemuarm" | "qemumips" | "qemumipsel" | \
112 "qemumips64" | "qemush4" | "qemuppc" | "qemumicroblaze" | "qemuzynq")
113 [ -z "$MACHINE" ] && MACHINE=$arg || \
114 error "conflicting MACHINE types [$MACHINE] and [$arg]"
115 ;;
116 "ext2" | "ext3" | "ext4" | "jffs2" | "nfs" | "btrfs")
117 [ -z "$FSTYPE" -o "$FSTYPE" = "$arg" ] && FSTYPE=$arg || \
118 error "conflicting FSTYPE types [$FSTYPE] and [$arg]"
119 ;;
120 *-image*)
121 [ -z "$ROOTFS" ] || \
122 error "conflicting ROOTFS args [$ROOTFS] and [$arg]"
123 if [ -f "$arg" ]; then
124 process_filename $arg
125 elif [ -d "$arg" ]; then
126 # Handle the case where the nfsroot dir has -image-
127 # in the pathname
128 echo "Assuming $arg is an nfs rootfs"
129 FSTYPE=nfs
130 ROOTFS=$arg
131 else
132 ROOTFS=$arg
133 LAZY_ROOTFS="true"
134 fi
135 ;;
136 "ramfs")
137 FSTYPE=cpio.gz
138 RAMFS=true
139 ;;
140 "iso")
141 FSTYPE=iso
142 ISOFS=true
143 ;;
144 "nographic")
145 SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -nographic"
146 SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT console=ttyS0"
147 ;;
148 "serial")
149 SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -serial stdio"
150 SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT console=ttyS0"
151 SERIALSTDIO="1"
152 ;;
153 "biosdir="*)
154 CUSTOMBIOSDIR="${arg##biosdir=}"
155 ;;
156 "qemuparams="*)
157 SCRIPT_QEMU_EXTRA_OPT="${arg##qemuparams=}"
158
159 # Warn user if they try to specify serial or kvm options
160 # to use simplified options instead
161 serial_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-serial\)'`
162 kvm_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-enable-kvm\)'`
163 vga_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-vga\)'`
164 [ ! -z "$serial_option" -o ! -z "$kvm_option" ] && \
165 echo "Please use simplified serial or kvm options instead"
166 ;;
167 "bootparams="*)
168 SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT ${arg##bootparams=}"
169 ;;
170 "audio")
171 if [ "x$MACHINE" = "xqemux86" -o "x$MACHINE" = "xqemux86-64" ]; then
172 echo "Enabling audio in qemu."
173 echo "Please install snd_intel8x0 or snd_ens1370 driver in linux guest."
174 QEMU_AUDIO_DRV="alsa"
175 SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -soundhw ac97,es1370"
176 fi
177 ;;
178 "kvm")
179 KVM_ENABLED="yes"
180 KVM_CAPABLE=`grep -q 'vmx\|svm' /proc/cpuinfo && echo 1`
181 ;;
182 "slirp")
183 SLIRP_ENABLED="yes"
184 ;;
185 "publicvnc")
186 SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -vnc 0.0.0.0:0"
187 ;;
188 "") break ;;
189 *)
190 # A directory name is an nfs rootfs
191 if [ -d "$arg" ]; then
192 echo "Assuming $arg is an nfs rootfs"
193 if [ -z "$FSTYPE" -o "$FSTYPE" = "nfs" ]; then
194 FSTYPE=nfs
195 else
196 error "conflicting FSTYPE types [$arg] and nfs"
197 fi
198
199 if [ -z "$ROOTFS" ]; then
200 ROOTFS=$arg
201 else
202 error "conflicting ROOTFS args [$ROOTFS] and [$arg]"
203 fi
204 elif [ -f "$arg" ]; then
205 process_filename $arg
206 else
207 error "unable to classify arg [$arg]"
208 fi
209 ;;
210 esac
211 shift
212done
213
214if [ ! -c /dev/net/tun ] ; then
215 echo "TUN control device /dev/net/tun is unavailable; you may need to enable TUN (e.g. sudo modprobe tun)"
216 exit 1
217elif [ ! -w /dev/net/tun ] ; then
218 echo "TUN control device /dev/net/tun is not writable, please fix (e.g. sudo chmod 666 /dev/net/tun)"
219 exit 1
220fi
221
222# Report errors for missing combinations of options
223if [ -z "$MACHINE" -a -z "$KERNEL" -a -z "$VM" ]; then
224 error "you must specify at least a MACHINE, VM, or KERNEL argument"
225fi
226if [ "$FSTYPE" = "nfs" -a -z "$ROOTFS" ]; then
227 error "NFS booting without an explicit ROOTFS path is not yet supported"
228fi
229
230if [ -z "$MACHINE" ]; then
231 if [ "x$FSTYPE" = "xvmdk" ]; then
232 MACHINE=`basename $VM | sed -n 's/.*\(qemux86-64\|qemux86\|qemuarm\|qemumips64\|qemumips\|qemuppc\|qemush4\).*/\1/p'`
233 if [ -z "$MACHINE" ]; then
234 error "Unable to set MACHINE from vmdk filename [$VM]"
235 fi
236 echo "Set MACHINE to [$MACHINE] based on vmdk [$VM]"
237 else
238 MACHINE=`basename $KERNEL | sed -n 's/.*\(qemux86-64\|qemux86\|qemuarm\|qemumips64\|qemumips\|qemuppc\|qemush4\).*/\1/p'`
239 if [ -z "$MACHINE" ]; then
240 error "Unable to set MACHINE from kernel filename [$KERNEL]"
241 fi
242 echo "Set MACHINE to [$MACHINE] based on kernel [$KERNEL]"
243 fi
244fi
245
246YOCTO_KVM_WIKI="https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu"
247YOCTO_PARAVIRT_KVM_WIKI="https://wiki.yoctoproject.org/wiki/Running_an_x86_Yocto_Linux_image_under_QEMU_KVM"
248# Detect KVM configuration
249if [ "x$KVM_ENABLED" = "xyes" ]; then
250 if [ -z "$KVM_CAPABLE" ]; then
251 echo "You are trying to enable KVM on a cpu without VT support."
252 echo "Remove kvm from the command-line, or refer"
253 echo "$YOCTO_KVM_WIKI";
254 exit 1;
255 fi
256 if [ "x$MACHINE" != "xqemux86" -a "x$MACHINE" != "xqemux86-64" ]; then
257 echo "KVM only support x86 & x86-64. Remove kvm from the command-line";
258 exit 1;
259 fi
260 if [ ! -e /dev/kvm ]; then
261 echo "Missing KVM device. Have you inserted kvm modules?"
262 echo "For further help see:"
263 echo "$YOCTO_KVM_WIKI";
264 exit 1;
265 fi
266 if [ ! -e /dev/vhost-net ]; then
267 echo "Missing virtio net device. Have you inserted vhost-net module?"
268 echo "For further help see:"
269 echo "$YOCTO_PARAVIRT_KVM_WIKI";
270 exit 1;
271 fi
272 if [ -w /dev/kvm -a -r /dev/kvm ]; then
273 SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -enable-kvm"
274 KVM_ACTIVE="yes"
275 else
276 echo "You have no rights on /dev/kvm."
277 echo "Please change the ownership of this file as described at:"
278 echo "$YOCTO_KVM_WIKI";
279 exit 1;
280 fi
281 if [ ! -w /dev/vhost-net -o ! -r /dev/vhost-net ]; then
282 if [ "$SLIRP_ENABLED" != "yes" ] ; then
283 echo "You have no rights on /dev/vhost-net."
284 echo "Please change the ownership of this file as described at:"
285 echo "$YOCTO_PARAVIRT_KVM_WIKI";
286 exit 1;
287 fi
288 fi
289fi
290
291machine2=`echo $MACHINE | tr 'a-z' 'A-Z' | sed 's/-/_/'`
292# MACHINE is now set for all cases
293
294# Defaults used when these vars need to be inferred
295QEMUX86_DEFAULT_KERNEL=bzImage-qemux86.bin
296QEMUX86_DEFAULT_FSTYPE=ext3
297
298QEMUX86_64_DEFAULT_KERNEL=bzImage-qemux86-64.bin
299QEMUX86_64_DEFAULT_FSTYPE=ext3
300
301QEMUARM_DEFAULT_KERNEL=zImage-qemuarm.bin
302QEMUARM_DEFAULT_FSTYPE=ext3
303
304QEMUMIPS_DEFAULT_KERNEL=vmlinux-qemumips.bin
305QEMUMIPS_DEFAULT_FSTYPE=ext3
306
307QEMUMIPSEL_DEFAULT_KERNEL=vmlinux-qemumipsel.bin
308QEMUMIPSEL_DEFAULT_FSTYPE=ext3
309
310QEMUMIPS64_DEFAULT_KERNEL=vmlinux-qemumips64.bin
311QEMUMIPS64_DEFAULT_FSTYPE=ext3
312
313QEMUSH4_DEFAULT_KERNEL=vmlinux-qemumips.bin
314QEMUSH4_DEFAULT_FSTYPE=ext3
315
316QEMUPPC_DEFAULT_KERNEL=vmlinux-qemuppc.bin
317QEMUPPC_DEFAULT_FSTYPE=ext3
318
319QEMUMICROBLAZE_DEFAULT_KERNEL=linux.bin.ub
320QEMUMICROBLAZE_DEFAULT_FSTYPE=cpio
321
322QEMUZYNQ_DEFAULT_KERNEL=uImage
323QEMUZYNQ_DEFAULT_FSTYPE=cpio
324
325AKITA_DEFAULT_KERNEL=zImage-akita.bin
326AKITA_DEFAULT_FSTYPE=jffs2
327
328SPITZ_DEFAULT_KERNEL=zImage-spitz.bin
329SPITZ_DEFAULT_FSTYPE=ext3
330
331setup_path_vars() {
332 if [ -z "$OE_TMPDIR" ] ; then
333 PATHS_REQUIRED=true
334 elif [ "$1" = "1" -a -z "$DEPLOY_DIR_IMAGE" ] ; then
335 PATHS_REQUIRED=true
336 else
337 PATHS_REQUIRED=false
338 fi
339
340 if [ "$PATHS_REQUIRED" = "true" ]; then
341 # Try to get the variable values from bitbake
342 type -P bitbake &>/dev/null || {
343 echo "In order for this script to dynamically infer paths";
344 echo "to kernels or filesystem images, you either need";
345 echo "bitbake in your PATH or to source oe-init-build-env";
346 echo "before running this script" >&2;
347 exit 1; }
348
349 # We have bitbake in PATH, get the variable values from bitbake
350 BITBAKE_ENV_TMPFILE=`mktemp --tmpdir runqemu.XXXXXXXXXX`
351 if [ "$?" != "0" ] ; then
352 echo "Error: mktemp failed for bitbake environment output"
353 exit 1
354 fi
355
356 MACHINE=$MACHINE bitbake -e > $BITBAKE_ENV_TMPFILE
357 if [ -z "$OE_TMPDIR" ] ; then
358 OE_TMPDIR=`sed -n 's/^TMPDIR=\"\(.*\)\"/\1/p' $BITBAKE_ENV_TMPFILE`
359 fi
360 if [ -z "$DEPLOY_DIR_IMAGE" ] ; then
361 DEPLOY_DIR_IMAGE=`sed -n 's/^DEPLOY_DIR_IMAGE=\"\(.*\)\"/\1/p' $BITBAKE_ENV_TMPFILE`
362 fi
363 if [ -z "$OE_TMPDIR" ]; then
364 # Check for errors from bitbake that the user needs to know about
365 BITBAKE_OUTPUT=`cat $BITBAKE_ENV_TMPFILE | wc -l`
366 if [ "$BITBAKE_OUTPUT" -eq "0" ]; then
367 echo "Error: this script needs to be run from your build directory, or you need"
368 echo "to explicitly set OE_TMPDIR and DEPLOY_DIR_IMAGE in your environment"
369 else
370 echo "There was an error running bitbake to determine TMPDIR"
371 echo "Here is the output from 'bitbake -e':"
372 cat $BITBAKE_ENV_TMPFILE
373 fi
374 rm $BITBAKE_ENV_TMPFILE
375 exit 1
376 fi
377 rm $BITBAKE_ENV_TMPFILE
378 fi
379}
380
381setup_sysroot() {
382 # Toolchain installs set up $OECORE_NATIVE_SYSROOT in their
383 # environment script. If that variable isn't set, we're
384 # either in an in-tree build scenario or the environment
385 # script wasn't source'd.
386 if [ -z "$OECORE_NATIVE_SYSROOT" ]; then
387 setup_path_vars
388 BUILD_ARCH=`uname -m`
389 BUILD_OS=`uname | tr '[A-Z]' '[a-z]'`
390 BUILD_SYS="$BUILD_ARCH-$BUILD_OS"
391
392 OECORE_NATIVE_SYSROOT=$OE_TMPDIR/sysroots/$BUILD_SYS
393 fi
394}
395
396# Locate a rootfs image to boot which matches our expected
397# machine and fstype.
398findimage() {
399 where=$1
400 machine=$2
401 extension=$3
402
403 # Sort rootfs candidates by modification time - the most
404 # recently created one is the one we most likely want to boot.
405 filename=`ls -t1 $where/*-image*$machine.$extension 2>/dev/null | head -n1`
406 if [ "x$filename" != "x" ]; then
407 ROOTFS=$filename
408 return
409 fi
410
411 echo "Couldn't find a $machine rootfs image in $where."
412 exit 1
413}
414
415if [ -e "$ROOTFS" -a -z "$FSTYPE" ]; then
416 # Extract the filename extension
417 EXT=`echo $ROOTFS | awk -F . '{ print \$NF }'`
418 if [ "x$EXT" = "xext2" -o "x$EXT" = "xext3" -o \
419 "x$EXT" = "xjffs2" -o "x$EXT" = "xbtrfs" -o \
420 "x$EXT" = "xext4" ]; then
421 FSTYPE=$EXT
422 else
423 echo "Note: Unable to determine filesystem extension for $ROOTFS"
424 echo "We will use the default FSTYPE for $MACHINE"
425 # ...which is done further below...
426 fi
427fi
428
429if [ -z "$KERNEL" -a "x$FSTYPE" != "xvmdk" ]; then
430 setup_path_vars 1
431 eval kernel_file=\$${machine2}_DEFAULT_KERNEL
432 KERNEL=$DEPLOY_DIR_IMAGE/$kernel_file
433
434 if [ -z "$KERNEL" ]; then
435 error "Unable to determine default kernel for MACHINE [$MACHINE]"
436 fi
437fi
438# KERNEL is now set for all cases
439
440if [ -z "$FSTYPE" ]; then
441 eval FSTYPE=\$${machine2}_DEFAULT_FSTYPE
442
443 if [ -z "$FSTYPE" ]; then
444 error "Unable to determine default fstype for MACHINE [$MACHINE]"
445 fi
446fi
447
448# FSTYPE is now set for all cases
449
450# Handle cases where a ROOTFS type is given instead of a filename, e.g.
451# core-image-sato
452if [ "$LAZY_ROOTFS" = "true" ]; then
453 setup_path_vars 1
454 echo "Assuming $ROOTFS really means $DEPLOY_DIR_IMAGE/$ROOTFS-$MACHINE.$FSTYPE"
455 ROOTFS=$DEPLOY_DIR_IMAGE/$ROOTFS-$MACHINE.$FSTYPE
456fi
457
458if [ -z "$ROOTFS" -a "x$FSTYPE" != "xvmdk" ]; then
459 setup_path_vars 1
460 T=$DEPLOY_DIR_IMAGE
461 eval rootfs_list=\$${machine2}_DEFAULT_ROOTFS
462 findimage $T $MACHINE $FSTYPE
463
464 if [ -z "$ROOTFS" ]; then
465 error "Unable to determine default rootfs for MACHINE [$MACHINE]"
466 fi
467fi
468# ROOTFS is now set for all cases, now expand it to be an absolute path, it should exist at this point
469
470ROOTFS=`readlink -f $ROOTFS`
471
472echo ""
473echo "Continuing with the following parameters:"
474if [ "x$FSTYPE" != "xvmdk" ]; then
475 echo "KERNEL: [$KERNEL]"
476 echo "ROOTFS: [$ROOTFS]"
477else
478 echo "VMDK: [$VM]"
479fi
480echo "FSTYPE: [$FSTYPE]"
481
482setup_sysroot
483# OECORE_NATIVE_SYSROOT is now set for all cases
484
485INTERNAL_SCRIPT="$0-internal"
486if [ ! -f "$INTERNAL_SCRIPT" -o ! -r "$INTERNAL_SCRIPT" ]; then
487INTERNAL_SCRIPT=`which runqemu-internal`
488fi
489
490# Specify directory for BIOS, VGA BIOS and keymaps
491if [ ! -z "$CUSTOMBIOSDIR" ]; then
492 if [ -d "$OECORE_NATIVE_SYSROOT/$CUSTOMBIOSDIR" ]; then
493 echo "Assuming biosdir is $OECORE_NATIVE_SYSROOT/$CUSTOMBIOSDIR"
494 SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -L $OECORE_NATIVE_SYSROOT/$CUSTOMBIOSDIR"
495 else
496 if [ ! -d "$CUSTOMBIOSDIR" ]; then
497 echo "Custom BIOS directory not found. Tried: $CUSTOMBIOSDIR"
498 echo "and $OECORE_NATIVE_SYSROOT/$CUSTOMBIOSDIR"
499 exit 1;
500 fi
501 echo "Assuming biosdir is $CUSTOMBIOSDIR"
502 SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -L $CUSTOMBIOSDIR"
503 fi
504fi
505
506. $INTERNAL_SCRIPT
507exit $?