runqemu 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. usage() {
  20. MYNAME=`basename $0`
  21. echo ""
  22. echo "Usage: you can run this script with any valid combination"
  23. echo "of the following options (in any order):"
  24. echo " QEMUARCH - the qemu machine architecture to use"
  25. echo " KERNEL - the kernel image file to use"
  26. echo " ROOTFS - the rootfs image file or nfsroot directory to use"
  27. echo " MACHINE - the machine name (optional, autodetected from KERNEL filename if unspecified)"
  28. echo " RAMFS - boot a ramfs-based image"
  29. echo " ISO - boot an ISO image"
  30. echo " VM - boot a vmdk image"
  31. echo " Simplified QEMU command-line options can be passed with:"
  32. echo " nographic - disables video console"
  33. echo " serial - enables a serial console on /dev/ttyS0"
  34. echo " kvm - enables KVM when running qemux86/qemux86-64 (VT-capable CPU required)"
  35. echo " publicvnc - enable a VNC server open to all hosts"
  36. echo " qemuparams=\"xyz\" - specify custom parameters to QEMU"
  37. echo " bootparams=\"xyz\" - specify custom kernel parameters during boot"
  38. echo ""
  39. echo "Examples:"
  40. echo " $MYNAME qemuarm"
  41. echo " $MYNAME qemux86-64 core-image-sato ext3"
  42. echo " $MYNAME path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial"
  43. echo " $MYNAME qemux86 ramfs"
  44. echo " $MYNAME qemux86 iso"
  45. echo " $MYNAME qemux86 qemuparams=\"-m 256\""
  46. echo " $MYNAME qemux86 bootparams=\"psplash=false\""
  47. echo " $MYNAME path/to/<image>-<machine>.vmdk"
  48. exit 1
  49. }
  50. if [ "x$1" = "x" ]; then
  51. usage
  52. fi
  53. error() {
  54. echo "Error: "$*
  55. usage
  56. }
  57. MACHINE=${MACHINE:=""}
  58. KERNEL=${KERNEL:=""}
  59. ROOTFS=${ROOTFS:=""}
  60. VM=${VM:=""}
  61. FSTYPE=""
  62. LAZY_ROOTFS=""
  63. SCRIPT_QEMU_OPT=""
  64. SCRIPT_QEMU_EXTRA_OPT=""
  65. SCRIPT_KERNEL_OPT=""
  66. SERIALSTDIO=""
  67. # Determine whether the file is a kernel or QEMU image, and set the
  68. # appropriate variables
  69. process_filename() {
  70. filename=$1
  71. # Extract the filename extension
  72. EXT=`echo $filename | awk -F . '{ print \$NF }'`
  73. case /$EXT/ in
  74. /bin/)
  75. # A file ending in .bin is a kernel
  76. [ -z "$KERNEL" ] && KERNEL=$filename || \
  77. error "conflicting KERNEL args [$KERNEL] and [$filename]"
  78. ;;
  79. /ext[234]/|/jffs2/|/btrfs/)
  80. # A file ending in a supportted fs type is a rootfs image
  81. if [ -z "$FSTYPE" -o "$FSTYPE" = "$EXT" ]; then
  82. FSTYPE=$EXT
  83. ROOTFS=$filename
  84. else
  85. error "conflicting FSTYPE types [$FSTYPE] and [$EXT]"
  86. fi
  87. ;;
  88. /vmdk/)
  89. FSTYPE=$EXT
  90. VM=$filename
  91. ;;
  92. *)
  93. error "unknown file arg [$filename]"
  94. ;;
  95. esac
  96. }
  97. # Parse command line args without requiring specific ordering. It's a
  98. # bit more complex, but offers a great user experience.
  99. KVM_ENABLED="no"
  100. while true; do
  101. arg=${1}
  102. case "$arg" in
  103. "qemux86" | "qemux86-64" | "qemuarm" | "qemumips" | "qemumipsel" | "qemumips64" | "qemush4" | "qemuppc")
  104. [ -z "$MACHINE" ] && MACHINE=$arg || \
  105. error "conflicting MACHINE types [$MACHINE] and [$arg]"
  106. ;;
  107. "ext2" | "ext3" | "ext4" | "jffs2" | "nfs" | "btrfs")
  108. [ -z "$FSTYPE" -o "$FSTYPE" = "$arg" ] && FSTYPE=$arg || \
  109. error "conflicting FSTYPE types [$FSTYPE] and [$arg]"
  110. ;;
  111. *-image*)
  112. [ -z "$ROOTFS" ] || \
  113. error "conflicting ROOTFS args [$ROOTFS] and [$arg]"
  114. if [ -f "$arg" ]; then
  115. process_filename $arg
  116. elif [ -d "$arg" ]; then
  117. # Handle the case where the nfsroot dir has -image-
  118. # in the pathname
  119. echo "Assuming $arg is an nfs rootfs"
  120. FSTYPE=nfs
  121. ROOTFS=$arg
  122. else
  123. ROOTFS=$arg
  124. LAZY_ROOTFS="true"
  125. fi
  126. ;;
  127. "ramfs")
  128. FSTYPE=cpio.gz
  129. RAMFS=true
  130. ;;
  131. "iso")
  132. FSTYPE=iso
  133. ISOFS=true
  134. ;;
  135. "nographic")
  136. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -nographic"
  137. SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT console=ttyS0"
  138. ;;
  139. "serial")
  140. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -serial stdio"
  141. SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT console=ttyS0"
  142. SERIALSTDIO="1"
  143. ;;
  144. "qemuparams="*)
  145. SCRIPT_QEMU_EXTRA_OPT="${arg##qemuparams=}"
  146. # Warn user if they try to specify serial or kvm options
  147. # to use simplified options instead
  148. serial_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-serial\)'`
  149. kvm_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-enable-kvm\)'`
  150. [ ! -z "$serial_option" -o ! -z "$kvm_option" ] && \
  151. error "Please use simplified serial or kvm options instead"
  152. ;;
  153. "bootparams="*)
  154. SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT ${arg##bootparams=}"
  155. ;;
  156. "audio")
  157. if [ "x$MACHINE" = "xqemux86" -o "x$MACHINE" = "xqemux86-64" ]; then
  158. echo "Enabling audio in qemu."
  159. echo "Please install snd_intel8x0 or snd_ens1370 driver in linux guest."
  160. QEMU_AUDIO_DRV="alsa"
  161. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -soundhw ac97,es1370"
  162. fi
  163. ;;
  164. "kvm")
  165. KVM_ENABLED="yes"
  166. KVM_CAPABLE=`grep -q 'vmx\|svm' /proc/cpuinfo && echo 1`
  167. ;;
  168. "publicvnc")
  169. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -vnc 0.0.0.0:0"
  170. ;;
  171. "") break ;;
  172. *)
  173. # A directory name is an nfs rootfs
  174. if [ -d "$arg" ]; then
  175. echo "Assuming $arg is an nfs rootfs"
  176. if [ -z "$FSTYPE" -o "$FSTYPE" = "nfs" ]; then
  177. FSTYPE=nfs
  178. else
  179. error "conflicting FSTYPE types [$arg] and nfs"
  180. fi
  181. if [ -z "$ROOTFS" ]; then
  182. ROOTFS=$arg
  183. else
  184. error "conflicting ROOTFS args [$ROOTFS] and [$arg]"
  185. fi
  186. elif [ -f "$arg" ]; then
  187. process_filename $arg
  188. else
  189. error "unable to classify arg [$arg]"
  190. fi
  191. ;;
  192. esac
  193. shift
  194. done
  195. if [ ! -c /dev/net/tun ] ; then
  196. echo "TUN control device /dev/net/tun is unavailable; you may need to enable TUN (e.g. sudo modprobe tun)"
  197. exit 1
  198. elif [ ! -w /dev/net/tun ] ; then
  199. echo "TUN control device /dev/net/tun is not writable, please fix (e.g. sudo chmod 666 /dev/net/tun)"
  200. exit 1
  201. fi
  202. # Report errors for missing combinations of options
  203. if [ -z "$MACHINE" -a -z "$KERNEL" -a -z "$VM" ]; then
  204. error "you must specify at least a MACHINE, VM, or KERNEL argument"
  205. fi
  206. if [ "$FSTYPE" = "nfs" -a -z "$ROOTFS" ]; then
  207. error "NFS booting without an explicit ROOTFS path is not yet supported"
  208. fi
  209. if [ -z "$MACHINE" ]; then
  210. if [ "x$FSTYPE" = "xvmdk" ]; then
  211. MACHINE=`basename $VM | sed 's/.*\(qemux86-64\|qemux86\|qemuarm\|qemumips64\|qemumips\|qemuppc\|qemush4\).*/\1/'`
  212. if [ -z "$MACHINE" ]; then
  213. error "Unable to set MACHINE from vmdk filename [$VM]"
  214. fi
  215. echo "Set MACHINE to [$MACHINE] based on vmdk [$VM]"
  216. else
  217. MACHINE=`basename $KERNEL | sed 's/.*\(qemux86-64\|qemux86\|qemuarm\|qemumips64\|qemumips\|qemuppc\|qemush4\).*/\1/'`
  218. if [ -z "$MACHINE" ]; then
  219. error "Unable to set MACHINE from kernel filename [$KERNEL]"
  220. fi
  221. echo "Set MACHINE to [$MACHINE] based on kernel [$KERNEL]"
  222. fi
  223. fi
  224. YOCTO_KVM_WIKI="https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu"
  225. YOCTO_PARAVIRT_KVM_WIKI="https://wiki.yoctoproject.org/wiki/Running_an_x86_Yocto_Linux_image_under_QEMU_KVM"
  226. # Detect KVM configuration
  227. if [ "x$KVM_ENABLED" = "xyes" ]; then
  228. if [ -z "$KVM_CAPABLE" ]; then
  229. echo "You are trying to enable KVM on a cpu without VT support."
  230. echo "Remove kvm from the command-line, or refer"
  231. echo "$YOCTO_KVM_WIKI";
  232. exit 1;
  233. fi
  234. if [ "x$MACHINE" != "xqemux86" -a "x$MACHINE" != "xqemux86-64" ]; then
  235. echo "KVM only support x86 & x86-64. Remove kvm from the command-line";
  236. exit 1;
  237. fi
  238. if [ ! -e /dev/kvm ]; then
  239. echo "Missing KVM device. Have you inserted kvm modules?"
  240. echo "For further help see:"
  241. echo "$YOCTO_KVM_WIKI";
  242. exit 1;
  243. fi
  244. if [ ! -e /dev/vhost-net ]; then
  245. echo "Missing virtio net device. Have you inserted vhost-net module?"
  246. echo "For further help see:"
  247. echo "$YOCTO_PARAVIRT_KVM_WIKI";
  248. exit 1;
  249. fi
  250. if [ -w /dev/kvm -a -r /dev/kvm ]; then
  251. if [ "x$MACHINE" = "xqemux86" ]; then
  252. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -enable-kvm -cpu kvm32"
  253. elif [ "x$MACHINE" = "xqemux86-64" ]; then
  254. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -enable-kvm -cpu kvm64"
  255. fi
  256. KVM_ACTIVE="yes"
  257. else
  258. echo "You have no rights on /dev/kvm."
  259. echo "Please change the ownership of this file as described at:"
  260. echo "$YOCTO_KVM_WIKI";
  261. exit 1;
  262. fi
  263. if [ ! -w /dev/vhost-net -o ! -r /dev/vhost-net ]; then
  264. echo "You have no rights on /dev/vhost-net."
  265. echo "Please change the ownership of this file as described at:"
  266. echo "$YOCTO_PARAVIRT_KVM_WIKI";
  267. exit 1;
  268. fi
  269. fi
  270. machine2=`echo $MACHINE | tr 'a-z' 'A-Z' | sed 's/-/_/'`
  271. # MACHINE is now set for all cases
  272. # Defaults used when these vars need to be inferred
  273. QEMUX86_DEFAULT_KERNEL=bzImage-qemux86.bin
  274. QEMUX86_DEFAULT_FSTYPE=ext3
  275. QEMUX86_64_DEFAULT_KERNEL=bzImage-qemux86-64.bin
  276. QEMUX86_64_DEFAULT_FSTYPE=ext3
  277. QEMUARM_DEFAULT_KERNEL=zImage-qemuarm.bin
  278. QEMUARM_DEFAULT_FSTYPE=ext3
  279. QEMUMIPS_DEFAULT_KERNEL=vmlinux-qemumips.bin
  280. QEMUMIPS_DEFAULT_FSTYPE=ext3
  281. QEMUMIPSEL_DEFAULT_KERNEL=vmlinux-qemumipsel.bin
  282. QEMUMIPSEL_DEFAULT_FSTYPE=ext3
  283. QEMUMIPS64_DEFAULT_KERNEL=vmlinux-qemumips64.bin
  284. QEMUMIPS64_DEFAULT_FSTYPE=ext3
  285. QEMUSH4_DEFAULT_KERNEL=vmlinux-qemumips.bin
  286. QEMUSH4_DEFAULT_FSTYPE=ext3
  287. QEMUPPC_DEFAULT_KERNEL=vmlinux-qemuppc.bin
  288. QEMUPPC_DEFAULT_FSTYPE=ext3
  289. AKITA_DEFAULT_KERNEL=zImage-akita.bin
  290. AKITA_DEFAULT_FSTYPE=jffs2
  291. SPITZ_DEFAULT_KERNEL=zImage-spitz.bin
  292. SPITZ_DEFAULT_FSTYPE=ext3
  293. setup_tmpdir() {
  294. if [ -z "$OE_TMPDIR" ]; then
  295. # Try to get OE_TMPDIR from bitbake
  296. type -P bitbake &>/dev/null || {
  297. echo "In order for this script to dynamically infer paths";
  298. echo "to kernels or filesystem images, you either need";
  299. echo "bitbake in your PATH or to source oe-init-build-env";
  300. echo "before running this script" >&2;
  301. exit 1; }
  302. # We have bitbake in PATH, get OE_TMPDIR from bitbake
  303. OE_TMPDIR=`MACHINE=$MACHINE bitbake -e | grep ^TMPDIR=\" | cut -d '=' -f2 | cut -d '"' -f2`
  304. if [ -z "$OE_TMPDIR" ]; then
  305. # Check for errors from bitbake that the user needs to know about
  306. BITBAKE_OUTPUT=`bitbake -e | wc -l`
  307. if [ "$BITBAKE_OUTPUT" -eq "0" ]; then
  308. echo "Error: this script needs to be run from your build directory,"
  309. echo "or you need to explicitly set OE_TMPDIR in your environment"
  310. else
  311. echo "There was an error running bitbake to determine TMPDIR"
  312. echo "Here is the output from 'bitbake -e':"
  313. bitbake -e
  314. fi
  315. exit 1
  316. fi
  317. fi
  318. }
  319. setup_sysroot() {
  320. # Toolchain installs set up $OECORE_NATIVE_SYSROOT in their
  321. # environment script. If that variable isn't set, we're
  322. # either in an in-tree build scenario or the environment
  323. # script wasn't source'd.
  324. if [ -z "$OECORE_NATIVE_SYSROOT" ]; then
  325. setup_tmpdir
  326. BUILD_ARCH=`uname -m`
  327. BUILD_OS=`uname | tr '[A-Z]' '[a-z]'`
  328. BUILD_SYS="$BUILD_ARCH-$BUILD_OS"
  329. OECORE_NATIVE_SYSROOT=$OE_TMPDIR/sysroots/$BUILD_SYS
  330. fi
  331. }
  332. # Locate a rootfs image to boot which matches our expected
  333. # machine and fstype.
  334. findimage() {
  335. where=$1
  336. machine=$2
  337. extension=$3
  338. # Sort rootfs candidates by modification time - the most
  339. # recently created one is the one we most likely want to boot.
  340. filenames=`ls -t $where/*-image*$machine.$extension 2>/dev/null | xargs`
  341. for name in $filenames; do
  342. case $name in
  343. *core-image-sato* | \
  344. *core-image-lsb* | \
  345. *core-image-basic* | \
  346. *core-image-minimal* )
  347. ROOTFS=$name
  348. return
  349. ;;
  350. esac
  351. done
  352. echo "Couldn't find a $machine rootfs image in $where."
  353. exit 1
  354. }
  355. if [ -e "$ROOTFS" -a -z "$FSTYPE" ]; then
  356. # Extract the filename extension
  357. EXT=`echo $ROOTFS | awk -F . '{ print \$NF }'`
  358. if [ "x$EXT" = "xext2" -o "x$EXT" = "xext3" -o \
  359. "x$EXT" = "xjffs2" -o "x$EXT" = "xbtrfs" ]; then
  360. FSTYPE=$EXT
  361. else
  362. echo "Note: Unable to determine filesystem extension for $ROOTFS"
  363. echo "We will use the default FSTYPE for $MACHINE"
  364. # ...which is done further below...
  365. fi
  366. fi
  367. if [ -z "$KERNEL" -a "x$FSTYPE" != "xvmdk" ]; then
  368. setup_tmpdir
  369. eval kernel_file=\$${machine2}_DEFAULT_KERNEL
  370. KERNEL=$OE_TMPDIR/deploy/images/$kernel_file
  371. if [ -z "$KERNEL" ]; then
  372. error "Unable to determine default kernel for MACHINE [$MACHINE]"
  373. fi
  374. fi
  375. # KERNEL is now set for all cases
  376. if [ -z "$FSTYPE" ]; then
  377. eval FSTYPE=\$${machine2}_DEFAULT_FSTYPE
  378. if [ -z "$FSTYPE" ]; then
  379. error "Unable to determine default fstype for MACHINE [$MACHINE]"
  380. fi
  381. fi
  382. # FSTYPE is now set for all cases
  383. # Handle cases where a ROOTFS type is given instead of a filename, e.g.
  384. # core-image-sato
  385. if [ "$LAZY_ROOTFS" = "true" ]; then
  386. setup_tmpdir
  387. echo "Assuming $ROOTFS really means $OE_TMPDIR/deploy/images/$ROOTFS-$MACHINE.$FSTYPE"
  388. ROOTFS=$OE_TMPDIR/deploy/images/$ROOTFS-$MACHINE.$FSTYPE
  389. fi
  390. if [ -z "$ROOTFS" -a "x$FSTYPE" != "xvmdk" ]; then
  391. setup_tmpdir
  392. T=$OE_TMPDIR/deploy/images
  393. eval rootfs_list=\$${machine2}_DEFAULT_ROOTFS
  394. findimage $T $MACHINE $FSTYPE
  395. if [ -z "$ROOTFS" ]; then
  396. error "Unable to determine default rootfs for MACHINE [$MACHINE]"
  397. fi
  398. fi
  399. # ROOTFS is now set for all cases
  400. echo ""
  401. echo "Continuing with the following parameters:"
  402. if [ "x$FSTYPE" != "xvmdk" ]; then
  403. echo "KERNEL: [$KERNEL]"
  404. echo "ROOTFS: [$ROOTFS]"
  405. else
  406. echo "VMDK: [$VM]"
  407. fi
  408. echo "FSTYPE: [$FSTYPE]"
  409. setup_sysroot
  410. # OECORE_NATIVE_SYSROOT is now set for all cases
  411. INTERNAL_SCRIPT="$0-internal"
  412. if [ ! -f "$INTERNAL_SCRIPT" -o ! -r "$INTERNAL_SCRIPT" ]; then
  413. INTERNAL_SCRIPT=`which runqemu-internal`
  414. fi
  415. . $INTERNAL_SCRIPT
  416. exit $?