#!/bin/bash -x # Handle running Poky images under qemu # # Copyright (C) 2006-2008 OpenedHand Ltd. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Call setting: # QEMU_MEMORY (optional) - set the amount of memory in the emualted system. # SERIAL_LOGFILE (optional) - log the serial port output to a file # CROSSPATH - the path to any cross toolchain to use with distcc # # Image options: # MACHINE - the machine to run # FSTYPE - the image type to run # KERNEL - the kernel image file to use # ROOTFS - the disk image file to use # if [ -z "$QEMU_MEMORY" ]; then case "$MACHINE" in "qemux86") QEMU_MEMORY="128M" ;; "qemux86-64") QEMU_MEMORY="128M" ;; "qemumips") QEMU_MEMORY="128M" ;; "qemuppc") QEMU_MEMORY="128M" ;; *) QEMU_MEMORY="64M" ;; esac fi QEMUIFUP=`which poky-qemu-ifup` QEMUIFDOWN=`which poky-qemu-ifdown` NFSRUNNING="false" LOCKUTIL=`which lockfile-create` if [ -z "$LOCKUTIL" ]; then echo "Error: Unable to find the lockfile-create utility" echo "On Ubuntu systems this is included in the lockfile-progs package" return fi LOCKDIR="/tmp/qemu-tap-locks" [ ! -d "$LOCKDIR" ] && mkdir $LOCKDIR POSSIBLE=`ifconfig -a | grep '^tap' | awk '{print $1}'` TAP="" LOCKFILE="" for tap in $POSSIBLE; do LOCKFILE="$LOCKDIR/$tap" echo "Acquiring lockfile for $tap..." if lockfile-create --use-pid -r 1 $LOCKFILE; then # the --use-pid option to lockfile-create will give use # the subshell's pid, so override it with the shell's pid: echo $$ > $LOCKFILE.lock TAP=$tap break fi done if [ "$TAP" = "" ]; then GROUPID=`id -g` echo 'Setting up tap interface under sudo' tap=`sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT` if [ $? -ne 0 ]; then # Re-run standalone to see verbose errors sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT return fi LOCKFILE="$LOCKDIR/$tap" echo "Acquiring lockfile for $tap..." if lockfile-create --use-pid -r 1 $LOCKFILE; then # the --use-pid option to lockfile-create will give us # the subshell's pid, so override it with the shell's pid: echo $$ > $LOCKFILE.lock TAP=$tap fi else echo "Using preconfigured tap device '$TAP'" fi release_lock() { if [ "$LOCKFILE" = "" ]; then $QEMUIFDOWN $TAP $POKY_NATIVE_SYSROOT else echo "Releasing lockfile of preconfigured tap device '$TAP'" lockfile-remove $LOCKFILE fi if [ "$NFSRUNNING" = "true" ]; then echo "Shutting down the userspace NFS server:" echo "poky-export-rootfs stop $ROOTFS" poky-export-rootfs stop $ROOTFS fi } n1=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ] n2=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ] KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0" QEMU_TAP_CMD="-net tap,vlan=0,ifname=$TAP,script=no,downscript=no" QEMU_NETWORK_CMD="-net nic,vlan=0 $QEMU_TAP_CMD" KERNCMDLINE="mem=$QEMU_MEMORY" QEMU_UI_OPTIONS="-show-cursor -usb -usbdevice wacom-tablet" SERIALOPTS="" if [ "x$SERIAL_LOGFILE" != "x" ]; then SERIALOPTS="-serial file:$SERIAL_LOGFILE" fi case "$MACHINE" in "qemuarm") ;; "qemumips") ;; "qemuppc") ;; "qemuarmv6") ;; "qemuarmv7") ;; "qemux86") ;; "qemux86-64") ;; "akita") ;; "spitz") ;; *) echo "Error: Unsupported machine type $MACHINE" return ;; esac if [ ! -f "$KERNEL" ]; then echo "Error: Kernel image file $KERNEL doesn't exist" release_lock return fi if [ "$FSTYPE" != "nfs" -a ! -f "$ROOTFS" ]; then echo "Error: Image file $ROOTFS doesn't exist" release_lock return fi if [ "$FSTYPE" = "nfs" ]; then NFS_SERVER="192.168.7.1" NFS_DIR=`echo $ROOTFS | sed 's/^[^:]*:\(.*\)/\1/'` UNFS_OPTS="nfsvers=2,mountprog=21111,nfsprog=11111,udp" PSEUDO_LOCALSTATEDIR=~/.poky-sdk/pseudo export PSEUDO_LOCALSTATEDIR rpcbind_running=`ps ax | grep rpcbind | grep -v grep | wc -l` portmap_running=`ps ax | grep portmap | grep -v grep | wc -l` if [[ $rpcbind_running == 0 && $portmap_running == 0 ]]; then echo "You need to be running either rpcbind or portmap to continue" release_lock return fi # Start the userspace NFS server echo "poky-export-rootfs restart $ROOTFS" poky-export-rootfs restart $ROOTFS if [ $? != 0 ]; then release_lock return fi NFSRUNNING="true" fi if [ "$NFS_SERVER" = "" ]; then NFS_SERVER="192.168.7.1" NFS_DIR=$ROOTFS fi if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "qemuarmv6" -o "$MACHINE" = "qemuarmv7" ]; then if [ -e /proc/sys/vm/mmap_min_addr ]; then if [ `cat /proc/sys/vm/mmap_min_addr` != "0" ]; then echo "Error, please set /proc/sys/vm/mmap_min_addr to 0 since otherwise it can cause problems with QEMU" return fi fi QEMU=qemu-system-arm MACHINE_SUBTYPE=versatilepb QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS" # QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -force-pointer" if [ "$FSTYPE" = "ext3" ]; then KERNCMDLINE="root=/dev/sda console=ttyAMA0,115200 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY highres=off" QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb -hda $ROOTFS -no-reboot $QEMU_UI_OPTIONS" fi if [ "$FSTYPE" = "nfs" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then echo "Error: NFS mount point $ROOTFS doesn't exist" release_lock return fi KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb --no-reboot $QEMU_UI_OPTIONS" fi if [ "$MACHINE" = "qemuarmv6" ]; then QEMUOPTIONS="$QEMUOPTIONS -cpu arm1136" fi if [ "$MACHINE" = "qemuarmv7" ]; then QEMUOPTIONS="$QEMUOPTIONS -cpu cortex-a8" fi fi if [ "$MACHINE" = "qemux86" ]; then QEMU=qemu QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware -enable-gl" if [ "$FSTYPE" = "ext3" ]; then KERNCMDLINE="vga=0 root=/dev/hda mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD" QEMUOPTIONS="$QEMU_NETWORK_CMD -hda $ROOTFS $QEMU_UI_OPTIONS" fi if [ "$FSTYPE" = "nfs" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then echo "Error: NFS mount point $ROOTFS doesn't exist." release_lock return fi KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS" fi fi if [ "$MACHINE" = "qemux86-64" ]; then QEMU=qemu-system-x86_64 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware -enable-gl" if [ "$FSTYPE" = "ext3" ]; then KERNCMDLINE="vga=0 root=/dev/hda mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD" QEMUOPTIONS="$QEMU_NETWORK_CMD -hda $ROOTFS $QEMU_UI_OPTIONS" fi if [ "$FSTYPE" = "nfs" ]; then if [ "x$ROOTFS" = "x" ]; then ROOTFS=/srv/nfs/qemux86-64 fi if [ ! -d "$ROOTFS" ]; then echo "Error: NFS mount point $ROOTFS doesn't exist." release_lock return fi KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS" fi fi if [ "$MACHINE" = "spitz" ]; then QEMU=qemu-system-arm if [ "$FSTYPE" = "ext3" ]; then echo $ROOTFS ROOTFS=`readlink -f $ROOTFS` echo $ROOTFS if [ ! -e "$ROOTFS.qemudisk" ]; then echo "Adding a partition table to the ext3 image for use by QEMU, please wait..." poky-addptable2image $ROOTFS $ROOTFS.qemudisk fi QEMUOPTIONS="$QEMU_NETWORK_CMD -M spitz -hda $ROOTFS.qemudisk -portrait" fi fi if [ "$MACHINE" = "qemumips" ]; then QEMU=qemu-system-mips MACHINE_SUBTYPE=malta QEMU_UI_OPTIONS="-vga cirrus" if [ "$TYPE" = "ext3" ]; then #KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -hda $ROOTFS -no-reboot $QEMU_UI_OPTIONS" fi if [ "$FSTYPE" = "nfs" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then echo "Error: NFS mount point $ROOTFS doesn't exist" release_lock return fi KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -no-reboot $QEMU_UI_OPTIONS" fi fi if [ "$MACHINE" = "qemuppc" ]; then QEMU=qemu-system-ppc MACHINE_SUBTYPE=prep CPU_SUBTYPE=603e BIOS=powerpc_rom.bin QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -nographic" if [ "$FSTYPE" = "ext3" ]; then KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -M $MACHINE_SUBTYPE -bios $BIOS -hda $ROOTFS -no-reboot $QEMU_UI_OPTIONS" fi if [ "$FSTYPE" = "nfs" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then echo "Error: NFS mount point $ROOTFS doesn't exist" release_lock return fi KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty0 nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -M $MACHINE_SUBTYPE -bios $BIOS -no-reboot $QEMU_UI_OPTIONS" fi fi if [ "$MACHINE" = "akita" ]; then QEMU=qemu-system-arm if [ "$FSTYPE" = "jffs2" ]; then ROOTFS=`readlink -f $ROOTFS` if [ ! -e "$ROOTFS.qemuflash" ]; then echo "Converting raw image into flash image format for use by QEMU, please wait..." raw2flash.akita < $ROOTFS > $ROOTFS.qemuflash fi QEMUOPTIONS="$QEMU_NETWORK_CMD -M akita -mtdblock $ROOTFS.qemuflash -portrait" fi fi if [ "x$QEMUOPTIONS" = "x" ]; then echo "Error: Unable to support this combination of options" release_lock return fi SDKDIR="/opt/poky/sysroots" if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "spitz" -o "$MACHINE" = "borzoi" -o "$MACHINE" = "akita" -o "$MACHINE" = "nokia800" ]; then SDKPATH="$SDKDIR/arm-poky-linux-gnueabi/bin" fi if [ "$MACHINE" = "qemux86" ]; then SDKPATH="$SDKDIR/i586-poky-linux/bin" fi if [ "$MACHINE" = "qemux86-64" ]; then SDKPATH="$SDKDIR/x86_64-poky-linux/bin" fi PATH=$CROSSPATH:$SDKPATH:$PATH QEMUBIN=`which $QEMU` if [ ! -x "$QEMUBIN" ]; then echo "Error: No QEMU binary '$QEMU' could be found." release_lock return fi function _quit() { if [ -n "$PIDFILE" ]; then #echo kill `cat $PIDFILE` kill `cat $PIDFILE` fi release_lock return } DISTCCD=`which distccd` PIDFILE="" trap _quit INT TERM QUIT if [ -x "$DISTCCD" ]; then echo "Starting distccd..." PIDFILE=`mktemp` $DISTCCD --allow 192.168.7.2 --daemon --pid-file $PIDFILE & else echo "Warning: distccd not present, no distcc support loaded." fi echo "Running $QEMU..." echo $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $* --append '"'$KERNCMDLINE'"' $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $* --append "$KERNCMDLINE" || /bin/true release_lock trap - INT TERM QUIT return