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