runqemu-internal 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #!/bin/bash -x
  2. # Handle running Poky images under qemu
  3. #
  4. # Copyright (C) 2006-2008 OpenedHand Ltd.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License version 2 as
  8. # published by the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. # Call setting:
  19. # QEMU_MEMORY (optional) - set the amount of memory in the emualted system.
  20. # SERIAL_LOGFILE (optional) - log the serial port output to a file
  21. # CROSSPATH - the path to any cross toolchain to use with distcc
  22. #
  23. # Image options:
  24. # MACHINE - the machine to run
  25. # FSTYPE - the image type to run
  26. # KERNEL - the kernel image file to use
  27. # ROOTFS - the disk image file to use
  28. #
  29. mem_size=-1
  30. #Get rid of <> and get the contents of extra qemu running params
  31. SCRIPT_QEMU_EXTRA_OPT=`echo $SCRIPT_QEMU_EXTRA_OPT | sed -e 's/<//' -e 's/>//'`
  32. #if user set qemu memory, eg: -m 256 in qemu extra params, we need to do some
  33. # validation check
  34. mem_set=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-m[[:space:]] *[0-9]*\)'`
  35. if [ ! -z "$mem_set" ] ; then
  36. #Get memory setting size from user input
  37. mem_size=`echo $mem_set | sed 's/-m[[:space:]] *//'`
  38. else
  39. case "$MACHINE" in
  40. "qemux86")
  41. mem_size=128
  42. ;;
  43. "qemux86-64")
  44. mem_size=128
  45. ;;
  46. "qemuarm")
  47. mem_size=128
  48. ;;
  49. "qemumips")
  50. mem_size=128
  51. ;;
  52. "qemuppc")
  53. mem_size=128
  54. ;;
  55. *)
  56. mem_size=64
  57. ;;
  58. esac
  59. fi
  60. # QEMU_MEMORY has 'M' appended to mem_size
  61. QEMU_MEMORY="$mem_size"M
  62. # Bug 433: qemuarm cannot use > 128 MB RAM
  63. if [ "$MACHINE" = "qemuarm" ]; then
  64. if [[ -z "$mem_size" || $mem_size -gt 128 ]]; then
  65. echo "WARNING: qemuarm does not support > 128M of RAM."
  66. echo "Changing QEMU_MEMORY to default of 128M."
  67. QEMU_MEMORY="128M"
  68. SCRIPT_QEMU_EXTRA_OPT=`echo $SCRIPT_QEMU_EXTRA_OPT | sed -e "s/$mem_set/-m 128/" `
  69. fi
  70. fi
  71. # We need to specify -m <mem_size> to overcome a bug in qemu 0.14.0
  72. # https://bugs.launchpad.net/ubuntu/+source/qemu-kvm/+bug/584480
  73. if [ -z "$mem_set" ] ; then
  74. SCRIPT_QEMU_EXTRA_OPT="$SCRIPT_QEMU_EXTRA_OPT -m $mem_size"
  75. fi
  76. # This file is created when runqemu-gen-tapdevs creates a bank of tap
  77. # devices, indicating that the user should not bring up new ones using
  78. # sudo.
  79. NOSUDO_FLAG="/etc/poky-nosudo"
  80. QEMUIFUP=`which runqemu-ifup`
  81. QEMUIFDOWN=`which runqemu-ifdown`
  82. NFSRUNNING="false"
  83. acquire_lock() {
  84. lockfile=$1
  85. if [ -z "$lockfile" ]; then
  86. echo "Error: missing lockfile arg passed to acquire_lock()"
  87. return 1
  88. fi
  89. if [ -e "$lockfile.lock" ]; then
  90. # Check that the lockfile is not stale
  91. ps=`ps -ewwo pid | grep $(cat $lockfile.lock)`
  92. if [ -z "$ps" ]; then
  93. echo "WARNING: Stale lock file detected, deleting $lockfile.lock."
  94. rm -f $lockfile.lock
  95. echo $$ > $lockfile.lock
  96. else
  97. return 1
  98. fi
  99. else
  100. echo $$ > $lockfile.lock
  101. fi
  102. return 0
  103. }
  104. release_lock() {
  105. lockfile=$1
  106. if [ -z "$lockfile" ]; then
  107. echo "Error: missing lockfile arg passed to release_lock()"
  108. return 1
  109. fi
  110. rm -f $lockfile.lock
  111. }
  112. LOCKDIR="/tmp/qemu-tap-locks"
  113. if [ ! -d "$LOCKDIR" ]; then
  114. mkdir $LOCKDIR
  115. chmod 777 $LOCKDIR
  116. fi
  117. IFCONFIG=`which ifconfig`
  118. if [ -z "$IFCONFIG" ]; then
  119. IFCONFIG=/sbin/ifconfig
  120. fi
  121. POSSIBLE=`$IFCONFIG -a | grep '^tap' | awk '{print $1}'`
  122. TAP=""
  123. LOCKFILE=""
  124. for tap in $POSSIBLE; do
  125. LOCKFILE="$LOCKDIR/$tap"
  126. echo "Acquiring lockfile for $tap..."
  127. acquire_lock $LOCKFILE
  128. if [ $? -eq 0 ]; then
  129. TAP=$tap
  130. break
  131. fi
  132. done
  133. if [ "$TAP" = "" ]; then
  134. if [ -e "$NOSUDO_FLAG" ]; then
  135. echo "Error: There are no available tap devices to use for networking,"
  136. echo "and I see $NOSUDO_FLAG exists, so I am not going to try creating"
  137. echo "a new one with sudo."
  138. exit 1
  139. fi
  140. GROUPID=`id -g`
  141. echo "Setting up tap interface under sudo"
  142. tap=`sudo $QEMUIFUP $GROUPID $OECORE_NATIVE_SYSROOT`
  143. if [ $? -ne 0 ]; then
  144. # Re-run standalone to see verbose errors
  145. sudo $QEMUIFUP $GROUPID $OECORE_NATIVE_SYSROOT
  146. return
  147. fi
  148. LOCKFILE="$LOCKDIR/$tap"
  149. echo "Acquiring lockfile for $tap..."
  150. acquire_lock $LOCKFILE
  151. if [ $? -eq 0 ]; then
  152. TAP=$tap
  153. fi
  154. else
  155. echo "Using preconfigured tap device '$TAP'"
  156. fi
  157. cleanup() {
  158. if [ ! -e "$NOSUDO_FLAG" ]; then
  159. sudo $QEMUIFDOWN $TAP $OECORE_NATIVE_SYSROOT
  160. fi
  161. echo "Releasing lockfile of preconfigured tap device '$TAP'"
  162. release_lock $LOCKFILE
  163. if [ "$NFSRUNNING" = "true" ]; then
  164. echo "Shutting down the userspace NFS server..."
  165. echo "runqemu-export-rootfs stop $ROOTFS"
  166. runqemu-export-rootfs stop $ROOTFS
  167. fi
  168. # If QEMU crashes or somehow tty properties are not restored
  169. # after qemu exits, we need to run stty sane
  170. stty sane
  171. }
  172. n1=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
  173. n2=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
  174. KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0"
  175. QEMU_TAP_CMD="-net tap,vlan=0,ifname=$TAP,script=no,downscript=no"
  176. QEMU_NETWORK_CMD="-net nic,vlan=0 $QEMU_TAP_CMD"
  177. KERNCMDLINE="mem=$QEMU_MEMORY"
  178. QEMU_UI_OPTIONS="-show-cursor -usb -usbdevice wacom-tablet"
  179. NFS_INSTANCE=`echo $TAP | sed 's/tap//'`
  180. export NFS_INSTANCE
  181. SERIALOPTS=""
  182. if [ "x$SERIAL_LOGFILE" != "x" ]; then
  183. SERIALOPTS="-serial file:$SERIAL_LOGFILE"
  184. fi
  185. case "$MACHINE" in
  186. "qemuarm") ;;
  187. "qemumips") ;;
  188. "qemuppc") ;;
  189. "qemuarmv6") ;;
  190. "qemuarmv7") ;;
  191. "qemux86") ;;
  192. "qemux86-64") ;;
  193. "akita") ;;
  194. "spitz") ;;
  195. *)
  196. echo "Error: Unsupported machine type $MACHINE"
  197. return
  198. ;;
  199. esac
  200. if [ ! -f "$KERNEL" ]; then
  201. echo "Error: Kernel image file $KERNEL doesn't exist"
  202. cleanup
  203. return
  204. fi
  205. if [ "$FSTYPE" != "nfs" -a ! -f "$ROOTFS" ]; then
  206. echo "Error: Image file $ROOTFS doesn't exist"
  207. cleanup
  208. return
  209. fi
  210. if [ "$FSTYPE" = "nfs" ]; then
  211. NFS_SERVER="192.168.7.1"
  212. NFS_DIR=`echo $ROOTFS | sed 's/^[^:]*:\(.*\)/\1/'`
  213. MOUNTD_PORT=$[ 21111 + $NFS_INSTANCE ]
  214. NFSD_PORT=$[ 11111 + $NFS_INSTANCE ]
  215. UNFS_OPTS="nfsvers=2,mountprog=$MOUNTD_PORT,nfsprog=$NFSD_PORT,udp"
  216. PSEUDO_LOCALSTATEDIR=~/.poky-sdk/pseudo
  217. export PSEUDO_LOCALSTATEDIR
  218. rpcbind_running=`ps ax | grep rpcbind | grep -v grep | wc -l`
  219. portmap_running=`ps ax | grep portmap | grep -v grep | wc -l`
  220. if [[ $rpcbind_running == 0 && $portmap_running == 0 ]]; then
  221. echo "You need to be running either rpcbind or portmap to continue"
  222. cleanup
  223. return
  224. fi
  225. # Start the userspace NFS server
  226. echo "runqemu-export-rootfs restart $ROOTFS"
  227. runqemu-export-rootfs restart $ROOTFS
  228. if [ $? != 0 ]; then
  229. cleanup
  230. return
  231. fi
  232. NFSRUNNING="true"
  233. fi
  234. if [ "$NFS_SERVER" = "" ]; then
  235. NFS_SERVER="192.168.7.1"
  236. NFS_DIR=$ROOTFS
  237. fi
  238. if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "qemuarmv6" -o "$MACHINE" = "qemuarmv7" ]; then
  239. QEMU=qemu-system-arm
  240. MACHINE_SUBTYPE=versatilepb
  241. QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS"
  242. # QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -force-pointer"
  243. if [ "$FSTYPE" = "ext3" ]; then
  244. KERNCMDLINE="root=/dev/sda rw console=ttyAMA0,115200 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY highres=off"
  245. QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb -hda $ROOTFS -no-reboot $QEMU_UI_OPTIONS"
  246. fi
  247. if [ "$FSTYPE" = "nfs" ]; then
  248. if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
  249. echo "Error: NFS mount point $ROOTFS doesn't exist"
  250. cleanup
  251. return
  252. fi
  253. KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  254. QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb --no-reboot $QEMU_UI_OPTIONS"
  255. fi
  256. if [ "$MACHINE" = "qemuarmv6" ]; then
  257. QEMUOPTIONS="$QEMUOPTIONS -cpu arm1136"
  258. fi
  259. if [ "$MACHINE" = "qemuarmv7" ]; then
  260. QEMUOPTIONS="$QEMUOPTIONS -cpu cortex-a8"
  261. fi
  262. fi
  263. if [ "$MACHINE" = "qemux86" ]; then
  264. QEMU=qemu
  265. QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware -enable-gl"
  266. if [ "$FSTYPE" = "ext3" ]; then
  267. KERNCMDLINE="vga=0 root=/dev/hda rw mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
  268. QEMUOPTIONS="$QEMU_NETWORK_CMD -hda $ROOTFS $QEMU_UI_OPTIONS"
  269. fi
  270. if [ "$FSTYPE" = "nfs" ]; then
  271. if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
  272. echo "Error: NFS mount point $ROOTFS doesn't exist."
  273. cleanup
  274. return
  275. fi
  276. KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  277. QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS"
  278. fi
  279. # Currently oprofile's event based interrupt mode doesn't work(Bug #828) in
  280. # qemux86 and qemux86-64. We can use timer interrupt mode for now.
  281. KERNCMDLINE="$KERNCMDLINE oprofile.timer=1"
  282. fi
  283. if [ "$MACHINE" = "qemux86-64" ]; then
  284. QEMU=qemu-system-x86_64
  285. QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware -enable-gl"
  286. if [ "$FSTYPE" = "ext3" ]; then
  287. KERNCMDLINE="vga=0 root=/dev/hda rw mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
  288. QEMUOPTIONS="$QEMU_NETWORK_CMD -hda $ROOTFS $QEMU_UI_OPTIONS"
  289. fi
  290. if [ "$FSTYPE" = "nfs" ]; then
  291. if [ "x$ROOTFS" = "x" ]; then
  292. ROOTFS=/srv/nfs/qemux86-64
  293. fi
  294. if [ ! -d "$ROOTFS" ]; then
  295. echo "Error: NFS mount point $ROOTFS doesn't exist."
  296. cleanup
  297. return
  298. fi
  299. KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  300. QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS"
  301. fi
  302. # Currently oprofile's event based interrupt mode doesn't work(Bug #828) in
  303. # qemux86 and qemux86-64. We can use timer interrupt mode for now.
  304. KERNCMDLINE="$KERNCMDLINE oprofile.timer=1"
  305. fi
  306. if [ "$MACHINE" = "spitz" ]; then
  307. QEMU=qemu-system-arm
  308. if [ "$FSTYPE" = "ext3" ]; then
  309. echo $ROOTFS
  310. ROOTFS=`readlink -f $ROOTFS`
  311. echo $ROOTFS
  312. if [ ! -e "$ROOTFS.qemudisk" ]; then
  313. echo "Adding a partition table to the ext3 image for use by QEMU, please wait..."
  314. runqemu-addptable2image $ROOTFS $ROOTFS.qemudisk
  315. fi
  316. QEMUOPTIONS="$QEMU_NETWORK_CMD -M spitz -hda $ROOTFS.qemudisk -portrait"
  317. fi
  318. fi
  319. if [ "$MACHINE" = "qemumips" ]; then
  320. QEMU=qemu-system-mips
  321. MACHINE_SUBTYPE=malta
  322. QEMU_UI_OPTIONS="-vga cirrus $QEMU_UI_OPTIONS"
  323. if [ "$FSTYPE" = "ext3" ]; then
  324. #KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  325. KERNCMDLINE="root=/dev/hda rw console=ttyS0 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  326. QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -hda $ROOTFS -no-reboot $QEMU_UI_OPTIONS"
  327. fi
  328. if [ "$FSTYPE" = "nfs" ]; then
  329. if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
  330. echo "Error: NFS mount point $ROOTFS doesn't exist"
  331. cleanup
  332. return
  333. fi
  334. KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  335. QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -no-reboot $QEMU_UI_OPTIONS"
  336. fi
  337. fi
  338. if [ "$MACHINE" = "qemuppc" ]; then
  339. QEMU=qemu-system-ppc
  340. MACHINE_SUBTYPE=prep
  341. CPU_SUBTYPE=603e
  342. BIOS=powerpc_rom.bin
  343. QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -nographic"
  344. if [ "$FSTYPE" = "ext3" ]; then
  345. KERNCMDLINE="root=/dev/hda rw console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  346. QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -M $MACHINE_SUBTYPE -bios $BIOS -hda $ROOTFS -no-reboot $QEMU_UI_OPTIONS"
  347. fi
  348. if [ "$FSTYPE" = "nfs" ]; then
  349. if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
  350. echo "Error: NFS mount point $ROOTFS doesn't exist"
  351. cleanup
  352. return
  353. fi
  354. KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty0 nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
  355. QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -M $MACHINE_SUBTYPE -bios $BIOS -no-reboot $QEMU_UI_OPTIONS"
  356. fi
  357. fi
  358. if [ "$MACHINE" = "akita" ]; then
  359. QEMU=qemu-system-arm
  360. if [ "$FSTYPE" = "jffs2" ]; then
  361. ROOTFS=`readlink -f $ROOTFS`
  362. if [ ! -e "$ROOTFS.qemuflash" ]; then
  363. echo "Converting raw image into flash image format for use by QEMU, please wait..."
  364. raw2flash.akita < $ROOTFS > $ROOTFS.qemuflash
  365. fi
  366. QEMUOPTIONS="$QEMU_NETWORK_CMD -M akita -mtdblock $ROOTFS.qemuflash -portrait"
  367. fi
  368. fi
  369. if [ "x$QEMUOPTIONS" = "x" ]; then
  370. echo "Error: Unable to support this combination of options"
  371. cleanup
  372. return
  373. fi
  374. PATH=$CROSSPATH:$OECORE_NATIVE_SYSROOT/usr/bin:$PATH
  375. QEMUBIN=`which $QEMU`
  376. if [ ! -x "$QEMUBIN" ]; then
  377. echo "Error: No QEMU binary '$QEMU' could be found."
  378. cleanup
  379. return
  380. fi
  381. function _quit() {
  382. if [ -n "$PIDFILE" ]; then
  383. #echo kill `cat $PIDFILE`
  384. kill `cat $PIDFILE`
  385. fi
  386. cleanup
  387. return
  388. }
  389. DISTCCD=`which distccd`
  390. PIDFILE=""
  391. trap _quit INT TERM QUIT
  392. if [ -x "$DISTCCD" ]; then
  393. echo "Starting distccd..."
  394. PIDFILE=`mktemp`
  395. $DISTCCD --allow 192.168.7.2 --daemon --pid-file $PIDFILE &
  396. else
  397. echo "WARNING: distccd not present, no distcc support loaded."
  398. fi
  399. # qemu got segfault if linked with nVidia's libgl
  400. if ldd $QEMUBIN | grep -i nvidia &> /dev/null
  401. then
  402. cat << EOM
  403. WARNING: nVidia proprietary OpenGL libraries detected.
  404. nVidia's OpenGL libraries are known to have compatibility issues with qemu,
  405. resulting in a segfault. Please uninstall these drivers or ensure the mesa libGL
  406. libraries precede nvidia's via LD_PRELOAD.
  407. EOM
  408. fi
  409. echo "Running $QEMU..."
  410. # -no-reboot is a mandatory option - see bug #100
  411. echo $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT --append '"'$KERNCMDLINE $SCRIPT_KERNEL_OPT'"'
  412. $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT --append "$KERNCMDLINE $SCRIPT_KERNEL_OPT"
  413. cleanup
  414. trap - INT TERM QUIT
  415. return