runqemu 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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=xyz - the machine name (optional, autodetected from KERNEL filename if unspecified)"
  28. echo " Additional QEMU command-line options can be passed with:"
  29. echo " nographic - disables video console"
  30. echo " serial - enables a serial console on /dev/ttyS0"
  31. echo " kvm - enables KVM when running qemux86/qemux86-64, VT capable CPU required"
  32. echo " \"<extra-qemu-options>\" - enables extra qemu options, excluding serial and kvm"
  33. echo ""
  34. echo "Examples:"
  35. echo " $MYNAME qemuarm"
  36. echo " $MYNAME qemux86-64 core-image-sato ext3"
  37. echo " $MYNAME path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial"
  38. echo " $MYNAME qemux86 \"<-m 256>\""
  39. exit 1
  40. }
  41. if [ "x$1" = "x" ]; then
  42. usage
  43. fi
  44. MACHINE=${MACHINE:=""}
  45. KERNEL=""
  46. FSTYPE=""
  47. ROOTFS=""
  48. LAZY_ROOTFS=""
  49. SCRIPT_QEMU_OPT=""
  50. SCRIPT_QEMU_EXTRA_OPT=""
  51. SCRIPT_KERNEL_OPT=""
  52. TMPDIR=""
  53. # Determine whether the file is a kernel or QEMU image, and set the
  54. # appropriate variables
  55. process_filename() {
  56. filename=$1
  57. # Extract the filename extension
  58. EXT=`echo $filename | awk -F . '{ print \$NF }'`
  59. # A file ending in .bin is a kernel
  60. if [ "x$EXT" = "xbin" ]; then
  61. if [ -z "$KERNEL" ]; then
  62. KERNEL=$filename
  63. else
  64. echo "Error: conflicting KERNEL args [$KERNEL] and [$filename]"
  65. usage
  66. fi
  67. elif [[ "x$EXT" == "xext2" || "x$EXT" == "xext3" ||
  68. "x$EXT" == "xjffs2" ]]; then
  69. # A file ending in a supportted fs type is a rootfs image
  70. if [[ -z "$FSTYPE" || "$FSTYPE" == "$EXT" ]]; then
  71. FSTYPE=$EXT
  72. ROOTFS=$filename
  73. else
  74. echo "Error: conflicting FSTYPE types [$FSTYPE] and [$EXT]"
  75. usage
  76. fi
  77. else
  78. echo "Error: unknown file arg [$filename]"
  79. usage
  80. fi
  81. }
  82. # Parse command line args without requiring specific ordering. It's a
  83. # bit more complex, but offers a great user experience.
  84. KVM_ENABLED="no"
  85. i=1
  86. while [ $i -le $# ]; do
  87. arg=${!i}
  88. case $arg in
  89. "qemux86" | "qemux86-64" | "qemuarm" | "qemumips" | "qemuppc")
  90. if [ -z "$MACHINE" ]; then
  91. MACHINE=$arg
  92. else
  93. echo "Error: conflicting MACHINE types [$MACHINE] and [$arg]"
  94. usage
  95. fi
  96. ;;
  97. "ext2" | "ext3" | "jffs2" | "nfs")
  98. if [[ -z "$FSTYPE" || "$FSTYPE" == "$arg" ]]; then
  99. FSTYPE=$arg
  100. else
  101. echo "Error: conflicting FSTYPE types [$FSTYPE] and [$arg]"
  102. usage
  103. fi
  104. ;;
  105. *-image-*)
  106. if [ -z "$ROOTFS" ]; then
  107. if [ -f "$arg" ]; then
  108. process_filename $arg
  109. elif [ -d "$arg" ]; then
  110. # Handle the case where the nfsroot dir has -image-
  111. # in the pathname
  112. echo "Assuming $arg is an nfs rootfs"
  113. FSTYPE=nfs
  114. ROOTFS=$arg
  115. else
  116. ROOTFS=$arg
  117. LAZY_ROOTFS="true"
  118. fi
  119. else
  120. echo "Error: conflicting ROOTFS args [$ROOTFS] and [$arg]"
  121. usage
  122. fi
  123. ;;
  124. "nographic")
  125. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -nographic"
  126. ;;
  127. "serial")
  128. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -serial stdio"
  129. SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT console=ttyS0"
  130. ;;
  131. "audio")
  132. if [[ "x$MACHINE" == "xqemux86" || "x$MACHINE" == "xqemux86-64" ]]; then
  133. echo "Enable audio on qemu. Pls. install snd_intel8x0 or snd_ens1370 driver in linux guest.";
  134. QEMU_AUDIO_DRV="alsa"
  135. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -soundhw ac97,es1370"
  136. fi
  137. ;;
  138. "kvm")
  139. KVM_ENABLED="yes"
  140. KVM_CAPABLE=`grep 'vmx\|smx' /proc/cpuinfo`
  141. ;;
  142. \<*\>)
  143. SCRIPT_QEMU_EXTRA_OPT=$arg
  144. serial_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-serial\)'`
  145. kvm_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-enable-kvm\)'`
  146. echo "$kvm_option"
  147. if [[ ! -z "$serial_option" || ! -z "$kvm_option" ]]; then
  148. echo "Error: Please use serial or kvm params instead!"
  149. usage
  150. fi
  151. ;;
  152. *)
  153. # A directory name is an nfs rootfs
  154. if [ -d "$arg" ]; then
  155. echo "Assuming $arg is an nfs rootfs"
  156. if [[ -z "$FSTYPE" || "$FSTYPE" == "nfs" ]]; then
  157. FSTYPE=nfs
  158. else
  159. echo "Error: conflicting FSTYPE types [$arg] and nfs"
  160. usage
  161. fi
  162. if [ -z "$ROOTFS" ]; then
  163. ROOTFS=$arg
  164. else
  165. echo "Error: conflicting ROOTFS args [$ROOTFS] and [$arg]"
  166. usage
  167. fi
  168. elif [ -f "$arg" ]; then
  169. process_filename $arg
  170. else
  171. echo "Error: unable to classify arg [$arg]"
  172. usage
  173. fi
  174. ;;
  175. esac
  176. i=$((i + 1))
  177. done
  178. YOCTO_KVM_WIKI="https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu"
  179. # Detect KVM configuration
  180. if [[ "x$KVM_ENABLED" == "xyes" ]]; then
  181. if [[ -z "$KVM_CAPABLE" ]]; then
  182. echo "You are tring to enable KVM on cpu without VT support. Remove kvm from the command-line, or refer";
  183. echo "$YOCTO_KVM_WIKI";
  184. exit 1;
  185. fi
  186. if [[ "x$MACHINE" != "xqemux86" && "x$MACHINE" != "xqemux86-64" ]]; then
  187. echo "KVM only support x86 & x86-64. Remove kvm from the command-line";
  188. exit 1;
  189. fi
  190. if [ ! -e /dev/kvm ]; then
  191. echo "Missing KVM device. Have you inserted kvm modules? Pls. refer";
  192. echo "$YOCTO_KVM_WIKI";
  193. exit 1;
  194. fi
  195. if 9<>/dev/kvm ; then
  196. SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -enable-kvm"
  197. else
  198. echo "You have no rights on /dev/kvm. Pls. change the owndership as described at";
  199. echo "$YOCTO_KVM_WIKI";
  200. exit 1;
  201. fi
  202. fi
  203. # Report errors for missing combinations of options
  204. if [[ -z "$MACHINE" && -z "$KERNEL" ]]; then
  205. echo "Error: you must specify at least a MACHINE or KERNEL argument"
  206. usage
  207. fi
  208. if [[ "$FSTYPE" == "nfs" && -z "$ROOTFS" ]]; then
  209. echo "Error: NFS booting without an explicit ROOTFS path is not yet supported"
  210. usage
  211. fi
  212. if [ -z "$MACHINE" ]; then
  213. MACHINE=`basename $KERNEL | sed 's/.*-\(qemux86-64\|qemux86\|qemuarm\|qemumips\|qemuppc\).*/\1/'`
  214. if [ -z "$MACHINE" ]; then
  215. echo "Error: Unable to set MACHINE from kernel filename [$KERNEL]"
  216. usage
  217. fi
  218. echo "Set MACHINE to [$MACHINE] based on kernel [$KERNEL]"
  219. fi
  220. machine2=`echo $MACHINE | tr 'a-z' 'A-Z' | sed 's/-/_/'`
  221. # MACHINE is now set for all cases
  222. # Defaults used when these vars need to be inferred
  223. QEMUX86_DEFAULT_KERNEL=bzImage-qemux86.bin
  224. QEMUX86_DEFAULT_FSTYPE=ext3
  225. QEMUX86_DEFAULT_ROOTFS="core-image-sato-sdk core-image-sato core-image-lsb core-image-basic core-image-minimal"
  226. QEMUX86_64_DEFAULT_KERNEL=bzImage-qemux86-64.bin
  227. QEMUX86_64_DEFAULT_FSTYPE=ext3
  228. QEMUX86_64_DEFAULT_ROOTFS="core-image-sato-sdk core-image-sato core-image-lsb core-image-basic core-image-minimal"
  229. QEMUARM_DEFAULT_KERNEL=zImage-qemuarm.bin
  230. QEMUARM_DEFAULT_FSTYPE=ext3
  231. QEMUARM_DEFAULT_ROOTFS="core-image-sato-sdk core-image-sato core-image-lsb core-image-basic core-image-minimal"
  232. QEMUMIPS_DEFAULT_KERNEL=vmlinux-qemumips.bin
  233. QEMUMIPS_DEFAULT_FSTYPE=ext3
  234. QEMUMIPS_DEFAULT_ROOTFS="core-image-sato-sdk core-image-sato core-image-lsb core-image-basic core-image-minimal"
  235. QEMUPPC_DEFAULT_KERNEL=zImage-qemuppc.bin
  236. QEMUPPC_DEFAULT_FSTYPE=ext3
  237. QEMUPPC_DEFAULT_ROOTFS="core-image-sato-sdk core-image-sato core-image-lsb core-image-basic core-image-minimal"
  238. AKITA_DEFAULT_KERNEL=zImage-akita.bin
  239. AKITA_DEFAULT_FSTYPE=jffs2
  240. AKITA_DEFAULT_ROOTFS="core-image-sato"
  241. SPITZ_DEFAULT_KERNEL=zImage-spitz.bin
  242. SPITZ_DEFAULT_FSTYPE=ext3
  243. SPITZ_DEFAULT_ROOTFS="core-image-sato"
  244. setup_tmpdir() {
  245. if [ -z "$TMPDIR" ]; then
  246. if [ "x$BUILDDIR" = "x" -o ! -d "$BUILDDIR/tmp" ]; then
  247. # BUILDDIR unset, try and get TMPDIR from bitbake
  248. type -P bitbake &>/dev/null || {
  249. echo "In order for this script to dynamically infer paths";
  250. echo "to kernels or filesystem images, you either need";
  251. echo "bitbake in your PATH or to source oe-init-build-env";
  252. echo "before running this script" >&2;
  253. exit 1; }
  254. # We have bitbake in PATH, get TMPDIR from bitbake
  255. TMPDIR=`bitbake -e | grep TMPDIR=\" | cut -d '=' -f2 | cut -d '"' -f2`
  256. else
  257. TMPDIR=$BUILDDIR/tmp
  258. fi
  259. fi
  260. }
  261. setup_sysroot() {
  262. # Toolchain installs set up $OECORE_NATIVE_SYSROOT in their
  263. # environment script. If that variable isn't set, we're
  264. # either in an in-tree build scenario or the environment
  265. # script wasn't source'd.
  266. if [ -z "$OECORE_NATIVE_SYSROOT" ]; then
  267. setup_tmpdir
  268. BUILD_ARCH=`uname -m`
  269. BUILD_OS=`uname | tr '[A-Z]' '[a-z]'`
  270. BUILD_SYS="$BUILD_ARCH-$BUILD_OS"
  271. OECORE_NATIVE_SYSROOT=$TMPDIR/sysroots/$BUILD_SYS
  272. fi
  273. }
  274. # Locate a rootfs image based on defaults defined above
  275. findimage() {
  276. where=$1
  277. machine=$2
  278. extension=$3
  279. names=$4
  280. for name in $names; do
  281. fullname=$where/$name-$machine.$extension
  282. if [ -e "$fullname" ]; then
  283. ROOTFS=$fullname
  284. return
  285. fi
  286. done
  287. echo "Couldn't find image in $where. Attempted image names were:"
  288. for name in $names; do
  289. echo $name-$machine.$extension
  290. done
  291. exit 1
  292. }
  293. if [[ -e "$ROOTFS" && -z "$FSTYPE" ]]; then
  294. # Extract the filename extension
  295. EXT=`echo $ROOTFS | awk -F . '{ print \$NF }'`
  296. if [[ "x$EXT" == "xext2" || "x$EXT" == "xext3" ||
  297. "x$EXT" == "xjffs2" ]]; then
  298. FSTYPE=$EXT
  299. else
  300. echo "Note: Unable to determine filesystem extension for $ROOTFS"
  301. echo "We will use the default FSTYPE for $MACHINE"
  302. # ...which is done further below...
  303. fi
  304. fi
  305. if [ -z "$KERNEL" ]; then
  306. setup_tmpdir
  307. eval kernel_file=\$${machine2}_DEFAULT_KERNEL
  308. KERNEL=$TMPDIR/deploy/images/$kernel_file
  309. if [ -z "$KERNEL" ]; then
  310. echo "Error: Unable to determine default kernel for MACHINE [$MACHINE]"
  311. usage
  312. fi
  313. fi
  314. # KERNEL is now set for all cases
  315. if [ -z "$FSTYPE" ]; then
  316. eval FSTYPE=\$${machine2}_DEFAULT_FSTYPE
  317. if [ -z "$FSTYPE" ]; then
  318. echo "Error: Unable to determine default fstype for MACHINE [$MACHINE]"
  319. usage
  320. fi
  321. fi
  322. # FSTYPE is now set for all cases
  323. # Handle cases where a ROOTFS type is given instead of a filename, e.g.
  324. # core-image-sato
  325. if [ "$LAZY_ROOTFS" = "true" ]; then
  326. setup_tmpdir
  327. echo "Assuming $ROOTFS really means $TMPDIR/deploy/images/$ROOTFS-$MACHINE.$FSTYPE"
  328. ROOTFS=$TMPDIR/deploy/images/$ROOTFS-$MACHINE.$FSTYPE
  329. fi
  330. if [ -z "$ROOTFS" ]; then
  331. setup_tmpdir
  332. T=$TMPDIR/deploy/images
  333. eval rootfs_list=\$${machine2}_DEFAULT_ROOTFS
  334. findimage $T $MACHINE $FSTYPE "$rootfs_list"
  335. if [ -z "$ROOTFS" ]; then
  336. echo "Error: Unable to determine default rootfs for MACHINE [$MACHINE]"
  337. usage
  338. fi
  339. fi
  340. # ROOTFS is now set for all cases
  341. echo ""
  342. echo "Continuing with the following parameters:"
  343. echo "KERNEL: [$KERNEL]"
  344. echo "ROOTFS: [$ROOTFS]"
  345. echo "FSTYPE: [$FSTYPE]"
  346. setup_sysroot
  347. # OECORE_NATIVE_SYSROOT is now set for all cases
  348. # We can't run without a libGL.so
  349. libgl='no'
  350. test -e /usr/lib/libGL.so -a -e /usr/lib/libGLU.so && libgl='yes'
  351. test -e /usr/lib64/libGL.so -a -e /usr/lib64/libGLU.so && libgl='yes'
  352. if [ "$libgl" != 'yes' ]; then
  353. echo "You need libGL.so and libGLU.so to exist in your library path to run the QEMU emulator.
  354. Ubuntu package names are: libgl1-mesa-dev and libglu1-mesa-dev."
  355. exit 1;
  356. fi
  357. INTERNAL_SCRIPT=`which runqemu-internal`
  358. . $INTERNAL_SCRIPT