qemu.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. *******************************
  3. Using the Quick EMUlator (QEMU)
  4. *******************************
  5. The Yocto Project uses an implementation of the Quick EMUlator (QEMU)
  6. Open Source project as part of the Yocto Project development "tool set".
  7. This chapter provides both procedures that show you how to use the Quick
  8. EMUlator (QEMU) and other QEMU information helpful for development
  9. purposes.
  10. Overview
  11. ========
  12. Within the context of the Yocto Project, QEMU is an emulator and
  13. virtualization machine that allows you to run a complete image you have
  14. built using the Yocto Project as just another task on your build system.
  15. QEMU is useful for running and testing images and applications on
  16. supported Yocto Project architectures without having actual hardware.
  17. Among other things, the Yocto Project uses QEMU to run automated Quality
  18. Assurance (QA) tests on final images shipped with each release.
  19. .. note::
  20. This implementation is not the same as QEMU in general.
  21. This section provides a brief reference for the Yocto Project
  22. implementation of QEMU.
  23. For official information and documentation on QEMU in general, see the
  24. following references:
  25. - `QEMU Website <https://wiki.qemu.org/Main_Page>`__\ *:* The official
  26. website for the QEMU Open Source project.
  27. - `Documentation <https://wiki.qemu.org/Manual>`__\ *:* The QEMU user
  28. manual.
  29. Running QEMU
  30. ============
  31. To use QEMU, you need to have QEMU installed and initialized as well as
  32. have the proper artifacts (i.e. image files and root filesystems)
  33. available. Follow these general steps to run QEMU:
  34. #. *Install QEMU:* QEMU is made available with the Yocto Project a
  35. number of ways. One method is to install a Software Development Kit
  36. (SDK). See ":ref:`sdk-manual/intro:the qemu emulator`" section in the
  37. Yocto Project Application Development and the Extensible Software
  38. Development Kit (eSDK) manual for information on how to install QEMU.
  39. #. *Setting Up the Environment:* How you set up the QEMU environment
  40. depends on how you installed QEMU:
  41. - If you cloned the ``poky`` repository or you downloaded and
  42. unpacked a Yocto Project release tarball, you can source the build
  43. environment script (i.e. :ref:`structure-core-script`)::
  44. $ cd poky
  45. $ source oe-init-build-env
  46. - If you installed a cross-toolchain, you can run the script that
  47. initializes the toolchain. For example, the following commands run
  48. the initialization script from the default ``poky_sdk`` directory::
  49. . poky_sdk/environment-setup-core2-64-poky-linux
  50. #. *Ensure the Artifacts are in Place:* You need to be sure you have a
  51. pre-built kernel that will boot in QEMU. You also need the target
  52. root filesystem for your target machine's architecture:
  53. - If you have previously built an image for QEMU (e.g. ``qemux86``,
  54. ``qemuarm``, and so forth), then the artifacts are in place in
  55. your :term:`Build Directory`.
  56. - If you have not built an image, you can go to the
  57. :yocto_dl:`machines/qemu </releases/yocto/&DISTRO_REL_LATEST_TAG;/machines/qemu/>` area and download a
  58. pre-built image that matches your architecture and can be run on
  59. QEMU.
  60. See the ":ref:`sdk-manual/appendix-obtain:extracting the root filesystem`"
  61. section in the Yocto Project Application Development and the
  62. Extensible Software Development Kit (eSDK) manual for information on
  63. how to extract a root filesystem.
  64. #. *Run QEMU:* The basic ``runqemu`` command syntax is as follows::
  65. $ runqemu [option ] [...]
  66. Based on what you provide on the command
  67. line, ``runqemu`` does a good job of figuring out what you are trying
  68. to do. For example, by default, QEMU looks for the most recently
  69. built image according to the timestamp when it needs to look for an
  70. image. Minimally, through the use of options, you must provide either
  71. a machine name, a virtual machine image (``*wic.vmdk``), or a kernel
  72. image (``*.bin``).
  73. Here are some additional examples to help illustrate further QEMU:
  74. - This example starts QEMU with MACHINE set to "qemux86-64".
  75. Assuming a standard :term:`Build Directory`, ``runqemu``
  76. automatically finds the ``bzImage-qemux86-64.bin`` image file and
  77. the ``core-image-minimal-qemux86-64-20200218002850.rootfs.ext4``
  78. (assuming the current build created a ``core-image-minimal``
  79. image)::
  80. $ runqemu qemux86-64
  81. .. note::
  82. When more than one image with the same name exists, QEMU finds
  83. and uses the most recently built image according to the
  84. timestamp.
  85. - This example produces the exact same results as the previous
  86. example. This command, however, specifically provides the image
  87. and root filesystem type::
  88. $ runqemu qemux86-64 core-image-minimal ext4
  89. - This example specifies to boot an :term:`Initramfs` image and to
  90. enable audio in QEMU. For this case, ``runqemu`` sets the internal
  91. variable ``FSTYPE`` to ``cpio.gz``. Also, for audio to be enabled,
  92. an appropriate driver must be installed (see the ``audio`` option
  93. in :ref:`dev-manual/qemu:\`\`runqemu\`\` command-line options`
  94. for more information)::
  95. $ runqemu qemux86-64 ramfs audio
  96. - This example does not provide enough information for QEMU to
  97. launch. While the command does provide a root filesystem type, it
  98. must also minimally provide a `MACHINE`, `KERNEL`, or `VM` option::
  99. $ runqemu ext4
  100. - This example specifies to boot a virtual machine image
  101. (``.wic.vmdk`` file). From the ``.wic.vmdk``, ``runqemu``
  102. determines the QEMU architecture (`MACHINE`) to be "qemux86-64" and
  103. the root filesystem type to be "vmdk"::
  104. $ runqemu /home/scott-lenovo/vm/core-image-minimal-qemux86-64.wic.vmdk
  105. Switching Between Consoles
  106. ==========================
  107. When booting or running QEMU, you can switch between supported consoles
  108. by using Ctrl+Alt+number. For example, Ctrl+Alt+3 switches you to the
  109. serial console as long as that console is enabled. Being able to switch
  110. consoles is helpful, for example, if the main QEMU console breaks for
  111. some reason.
  112. .. note::
  113. Usually, "2" gets you to the main console and "3" gets you to the
  114. serial console.
  115. Removing the Splash Screen
  116. ==========================
  117. You can remove the splash screen when QEMU is booting by using Alt+left.
  118. Removing the splash screen allows you to see what is happening in the
  119. background.
  120. Disabling the Cursor Grab
  121. =========================
  122. The default QEMU integration captures the cursor within the main window.
  123. It does this since standard mouse devices only provide relative input
  124. and not absolute coordinates. You then have to break out of the grab
  125. using the "Ctrl+Alt" key combination. However, the Yocto Project's
  126. integration of QEMU enables the wacom USB touch pad driver by default to
  127. allow input of absolute coordinates. This default means that the mouse
  128. can enter and leave the main window without the grab taking effect
  129. leading to a better user experience.
  130. Running Under a Network File System (NFS) Server
  131. ================================================
  132. One method for running QEMU is to run it on an NFS server. This is
  133. useful when you need to access the same file system from both the build
  134. and the emulated system at the same time. It is also worth noting that
  135. the system does not need root privileges to run. It uses a user space
  136. NFS server to avoid that. Follow these steps to set up for running QEMU
  137. using an NFS server.
  138. #. *Extract a Root Filesystem:* Once you are able to run QEMU in your
  139. environment, you can use the ``runqemu-extract-sdk`` script, which is
  140. located in the ``scripts`` directory along with the ``runqemu``
  141. script.
  142. The ``runqemu-extract-sdk`` takes a root filesystem tarball and
  143. extracts it into a location that you specify. Here is an example that
  144. takes a file system and extracts it to a directory named
  145. ``test-nfs``:
  146. .. code-block:: none
  147. runqemu-extract-sdk ./tmp/deploy/images/qemux86-64/core-image-sato-qemux86-64.tar.bz2 test-nfs
  148. #. *Start QEMU:* Once you have extracted the file system, you can run
  149. ``runqemu`` normally with the additional location of the file system.
  150. You can then also make changes to the files within ``./test-nfs`` and
  151. see those changes appear in the image in real time. Here is an
  152. example using the ``qemux86`` image:
  153. .. code-block:: none
  154. runqemu qemux86-64 ./test-nfs
  155. .. note::
  156. Should you need to start, stop, or restart the NFS share, you can use
  157. the following commands:
  158. - To start the NFS share::
  159. runqemu-export-rootfs start file-system-location
  160. - To stop the NFS share::
  161. runqemu-export-rootfs stop file-system-location
  162. - To restart the NFS share::
  163. runqemu-export-rootfs restart file-system-location
  164. QEMU CPU Compatibility Under KVM
  165. ================================
  166. By default, the QEMU build compiles for and targets 64-bit and x86 Intel
  167. Core2 Duo processors and 32-bit x86 Intel Pentium II processors. QEMU
  168. builds for and targets these CPU types because they display a broad
  169. range of CPU feature compatibility with many commonly used CPUs.
  170. Despite this broad range of compatibility, the CPUs could support a
  171. feature that your host CPU does not support. Although this situation is
  172. not a problem when QEMU uses software emulation of the feature, it can
  173. be a problem when QEMU is running with KVM enabled. Specifically,
  174. software compiled with a certain CPU feature crashes when run on a CPU
  175. under KVM that does not support that feature. To work around this
  176. problem, you can override QEMU's runtime CPU setting by changing the
  177. ``QB_CPU_KVM`` variable in ``qemuboot.conf`` in the :term:`Build Directory`
  178. ``deploy/image`` directory. This setting specifies a ``-cpu`` option passed
  179. into QEMU in the ``runqemu`` script. Running ``qemu -cpu help`` returns a
  180. list of available supported CPU types.
  181. QEMU Performance
  182. ================
  183. Using QEMU to emulate your hardware can result in speed issues depending
  184. on the target and host architecture mix. For example, using the
  185. ``qemux86`` image in the emulator on an Intel-based 32-bit (x86) host
  186. machine is fast because the target and host architectures match. On the
  187. other hand, using the ``qemuarm`` image on the same Intel-based host can
  188. be slower. But, you still achieve faithful emulation of ARM-specific
  189. issues.
  190. To speed things up, the QEMU images support using ``distcc`` to call a
  191. cross-compiler outside the emulated system. If you used ``runqemu`` to
  192. start QEMU, and the ``distccd`` application is present on the host
  193. system, any BitBake cross-compiling toolchain available from the build
  194. system is automatically used from within QEMU simply by calling
  195. ``distcc``. You can accomplish this by defining the cross-compiler
  196. variable (e.g. ``export CC="distcc"``). Alternatively, if you are using
  197. a suitable SDK image or the appropriate stand-alone toolchain is
  198. present, the toolchain is also automatically used.
  199. .. note::
  200. There are several mechanisms to connect to the system running
  201. on the QEMU emulator:
  202. - QEMU provides a framebuffer interface that makes standard consoles
  203. available.
  204. - Generally, headless embedded devices have a serial port. If so,
  205. you can configure the operating system of the running image to use
  206. that port to run a console. The connection uses standard IP
  207. networking.
  208. - SSH servers are available in some QEMU images. The ``core-image-sato``
  209. QEMU image has a Dropbear secure shell (SSH) server that runs with the
  210. root password disabled. The ``core-image-full-cmdline`` QEMU image has
  211. OpenSSH instead of Dropbear. Including these SSH servers allow you to use
  212. standard ``ssh`` and ``scp`` commands. The ``core-image-minimal`` QEMU
  213. image, however, contains no SSH server.
  214. - You can use a provided, user-space NFS server to boot the QEMU
  215. session using a local copy of the root filesystem on the host. In
  216. order to make this connection, you must extract a root filesystem
  217. tarball by using the ``runqemu-extract-sdk`` command. After
  218. running the command, you must then point the ``runqemu`` script to
  219. the extracted directory instead of a root filesystem image file.
  220. See the
  221. ":ref:`dev-manual/qemu:running under a network file system (nfs) server`"
  222. section for more information.
  223. QEMU Command-Line Syntax
  224. ========================
  225. The basic ``runqemu`` command syntax is as follows::
  226. $ runqemu [option ] [...]
  227. Based on what you provide on the command line, ``runqemu`` does a
  228. good job of figuring out what you are trying to do. For example, by
  229. default, QEMU looks for the most recently built image according to the
  230. timestamp when it needs to look for an image. Minimally, through the use
  231. of options, you must provide either a machine name, a virtual machine
  232. image (``*wic.vmdk``), or a kernel image (``*.bin``).
  233. Here is the command-line help output for the ``runqemu`` command::
  234. $ runqemu --help
  235. Usage: you can run this script with any valid combination
  236. of the following environment variables (in any order):
  237. KERNEL - the kernel image file to use
  238. ROOTFS - the rootfs image file or nfsroot directory to use
  239. MACHINE - the machine name (optional, autodetected from KERNEL filename if unspecified)
  240. Simplified QEMU command-line options can be passed with:
  241. nographic - disable video console
  242. serial - enable a serial console on /dev/ttyS0
  243. slirp - enable user networking, no root privileges required
  244. kvm - enable KVM when running x86/x86_64 (VT-capable CPU required)
  245. kvm-vhost - enable KVM with vhost when running x86/x86_64 (VT-capable CPU required)
  246. publicvnc - enable a VNC server open to all hosts
  247. audio - enable audio
  248. [*/]ovmf* - OVMF firmware file or base name for booting with UEFI
  249. tcpserial=<port> - specify tcp serial port number
  250. biosdir=<dir> - specify custom bios dir
  251. biosfilename=<filename> - specify bios filename
  252. qemuparams=<xyz> - specify custom parameters to QEMU
  253. bootparams=<xyz> - specify custom kernel parameters during boot
  254. help, -h, --help: print this text
  255. Examples:
  256. runqemu
  257. runqemu qemuarm
  258. runqemu tmp/deploy/images/qemuarm
  259. runqemu tmp/deploy/images/qemux86/<qemuboot.conf>
  260. runqemu qemux86-64 core-image-sato ext4
  261. runqemu qemux86-64 wic-image-minimal wic
  262. runqemu path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial
  263. runqemu qemux86 iso/hddimg/wic.vmdk/wic.qcow2/wic.vdi/ramfs/cpio.gz...
  264. runqemu qemux86 qemuparams="-m 256"
  265. runqemu qemux86 bootparams="psplash=false"
  266. runqemu path/to/<image>-<machine>.wic
  267. runqemu path/to/<image>-<machine>.wic.vmdk
  268. ``runqemu`` Command-Line Options
  269. ================================
  270. Here is a description of ``runqemu`` options you can provide on the
  271. command line:
  272. .. note::
  273. If you do provide some "illegal" option combination or perhaps you do
  274. not provide enough in the way of options, ``runqemu``
  275. provides appropriate error messaging to help you correct the problem.
  276. - `QEMUARCH`: The QEMU machine architecture, which must be "qemuarm",
  277. "qemuarm64", "qemumips", "qemumips64", "qemuppc", "qemux86", or
  278. "qemux86-64".
  279. - `VM`: The virtual machine image, which must be a ``.wic.vmdk``
  280. file. Use this option when you want to boot a ``.wic.vmdk`` image.
  281. - `ROOTFS`: A root filesystem that has one of the following filetype
  282. extensions: "ext2", "ext3", "ext4", "jffs2", "nfs", or "btrfs". If
  283. the filename you provide for this option uses "nfs", it must provide
  284. an explicit root filesystem path.
  285. - `KERNEL`: A kernel image, which is a ``.bin`` file. When you provide a
  286. ``.bin`` file, ``runqemu`` detects it and assumes the file is a
  287. kernel image.
  288. - `MACHINE`: The architecture of the QEMU machine, which must be one of
  289. the following: "qemux86", "qemux86-64", "qemuarm", "qemuarm64",
  290. "qemumips", "qemumips64", or "qemuppc". The MACHINE and QEMUARCH
  291. options are basically identical. If you do not provide a MACHINE
  292. option, ``runqemu`` tries to determine it based on other options.
  293. - ``ramfs``: Indicates you are booting an :term:`Initramfs`
  294. image, which means the ``FSTYPE`` is ``cpio.gz``.
  295. - ``iso``: Indicates you are booting an ISO image, which means the
  296. ``FSTYPE`` is ``.iso``.
  297. - ``nographic``: Disables the video console, which sets the console to
  298. "ttys0". This option is useful when you have logged into a server and
  299. you do not want to disable forwarding from the X Window System (X11)
  300. to your workstation or laptop.
  301. - ``serial``: Enables a serial console on ``/dev/ttyS0``.
  302. - ``biosdir``: Establishes a custom directory for BIOS, VGA BIOS and
  303. keymaps.
  304. - ``biosfilename``: Establishes a custom BIOS name.
  305. - ``qemuparams=\"xyz\"``: Specifies custom QEMU parameters. Use this
  306. option to pass options other than the simple "kvm" and "serial"
  307. options.
  308. - ``bootparams=\"xyz\"``: Specifies custom boot parameters for the
  309. kernel.
  310. - ``audio``: Enables audio in QEMU. The MACHINE option must be either
  311. "qemux86" or "qemux86-64" in order for audio to be enabled.
  312. Additionally, the ``snd_intel8x0`` or ``snd_ens1370`` driver must be
  313. installed in linux guest.
  314. - ``slirp``: Enables "slirp" networking, which is a different way of
  315. networking that does not need root access but also is not as easy to
  316. use or comprehensive as the default.
  317. Using ``slirp`` by default will forward the guest machine's
  318. 22 and 23 TCP ports to host machine's 2222 and 2323 ports
  319. (or the next free ports). Specific forwarding rules can be configured
  320. by setting ``QB_SLIRP_OPT`` as environment variable or in ``qemuboot.conf``
  321. in the :term:`Build Directory` ``deploy/image`` directory.
  322. Examples::
  323. QB_SLIRP_OPT="-netdev user,id=net0,hostfwd=tcp::8080-:80"
  324. QB_SLIRP_OPT="-netdev user,id=net0,hostfwd=tcp::8080-:80,hostfwd=tcp::2222-:22"
  325. The first example forwards TCP port 80 from the emulated system to
  326. port 8080 (or the next free port) on the host system,
  327. allowing access to an http server running in QEMU from
  328. ``http://<host ip>:8080/``.
  329. The second example does the same, but also forwards TCP port 22 on the
  330. guest system to 2222 (or the next free port) on the host system,
  331. allowing ssh access to the emulated system using
  332. ``ssh -P 2222 <user>@<host ip>``.
  333. Keep in mind that proper configuration of firewall software is required.
  334. - ``kvm``: Enables KVM when running "qemux86" or "qemux86-64" QEMU
  335. architectures. For KVM to work, all the following conditions must be
  336. met:
  337. - Your MACHINE must be either qemux86" or "qemux86-64".
  338. - Your build host has to have the KVM modules installed, which are
  339. ``/dev/kvm``.
  340. - The build host ``/dev/kvm`` directory has to be both writable and
  341. readable.
  342. - ``kvm-vhost``: Enables KVM with VHOST support when running "qemux86"
  343. or "qemux86-64" QEMU architectures. For KVM with VHOST to work, the
  344. following conditions must be met:
  345. - ``kvm`` option conditions defined above must be met.
  346. - Your build host has to have virtio net device, which are
  347. ``/dev/vhost-net``.
  348. - The build host ``/dev/vhost-net`` directory has to be either
  349. readable or writable and "slirp-enabled".
  350. - ``publicvnc``: Enables a VNC server open to all hosts.