runqemu 14 KB

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