qemu.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. 1. *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. 2. *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. 3. *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/yocto-&DISTRO;/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. 4. *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. .. note::
  81. When more than one image with the same name exists, QEMU finds
  82. and uses the most recently built image according to the
  83. timestamp.
  84. ::
  85. $ runqemu qemux86-64
  86. - This example produces the exact same results as the previous
  87. example. This command, however, specifically provides the image
  88. and root filesystem type.
  89. ::
  90. $ runqemu qemux86-64 core-image-minimal ext4
  91. - This example specifies to boot an :term:`Initramfs` image and to
  92. enable audio in QEMU. For this case, ``runqemu`` sets the internal
  93. variable ``FSTYPE`` to ``cpio.gz``. Also, for audio to be enabled,
  94. an appropriate driver must be installed (see the previous
  95. description for the ``audio`` option for more information).
  96. ::
  97. $ runqemu qemux86-64 ramfs audio
  98. - This example does not provide enough information for QEMU to
  99. launch. While the command does provide a root filesystem type, it
  100. must also minimally provide a `MACHINE`, `KERNEL`, or `VM` option.
  101. ::
  102. $ runqemu ext4
  103. - This example specifies to boot a virtual machine image
  104. (``.wic.vmdk`` file). From the ``.wic.vmdk``, ``runqemu``
  105. determines the QEMU architecture (`MACHINE`) to be "qemux86-64" and
  106. the root filesystem type to be "vmdk".
  107. ::
  108. $ runqemu /home/scott-lenovo/vm/core-image-minimal-qemux86-64.wic.vmdk
  109. Switching Between Consoles
  110. ==========================
  111. When booting or running QEMU, you can switch between supported consoles
  112. by using Ctrl+Alt+number. For example, Ctrl+Alt+3 switches you to the
  113. serial console as long as that console is enabled. Being able to switch
  114. consoles is helpful, for example, if the main QEMU console breaks for
  115. some reason.
  116. .. note::
  117. Usually, "2" gets you to the main console and "3" gets you to the
  118. serial console.
  119. Removing the Splash Screen
  120. ==========================
  121. You can remove the splash screen when QEMU is booting by using Alt+left.
  122. Removing the splash screen allows you to see what is happening in the
  123. background.
  124. Disabling the Cursor Grab
  125. =========================
  126. The default QEMU integration captures the cursor within the main window.
  127. It does this since standard mouse devices only provide relative input
  128. and not absolute coordinates. You then have to break out of the grab
  129. using the "Ctrl+Alt" key combination. However, the Yocto Project's
  130. integration of QEMU enables the wacom USB touch pad driver by default to
  131. allow input of absolute coordinates. This default means that the mouse
  132. can enter and leave the main window without the grab taking effect
  133. leading to a better user experience.
  134. Running Under a Network File System (NFS) Server
  135. ================================================
  136. One method for running QEMU is to run it on an NFS server. This is
  137. useful when you need to access the same file system from both the build
  138. and the emulated system at the same time. It is also worth noting that
  139. the system does not need root privileges to run. It uses a user space
  140. NFS server to avoid that. Follow these steps to set up for running QEMU
  141. using an NFS server.
  142. 1. *Extract a Root Filesystem:* Once you are able to run QEMU in your
  143. environment, you can use the ``runqemu-extract-sdk`` script, which is
  144. located in the ``scripts`` directory along with the ``runqemu``
  145. script.
  146. The ``runqemu-extract-sdk`` takes a root filesystem tarball and
  147. extracts it into a location that you specify. Here is an example that
  148. takes a file system and extracts it to a directory named
  149. ``test-nfs``:
  150. .. code-block:: none
  151. runqemu-extract-sdk ./tmp/deploy/images/qemux86-64/core-image-sato-qemux86-64.tar.bz2 test-nfs
  152. 2. *Start QEMU:* Once you have extracted the file system, you can run
  153. ``runqemu`` normally with the additional location of the file system.
  154. You can then also make changes to the files within ``./test-nfs`` and
  155. see those changes appear in the image in real time. Here is an
  156. example using the ``qemux86`` image:
  157. .. code-block:: none
  158. runqemu qemux86-64 ./test-nfs
  159. .. note::
  160. Should you need to start, stop, or restart the NFS share, you can use
  161. the following commands:
  162. - To start the NFS share::
  163. runqemu-export-rootfs start file-system-location
  164. - To stop the NFS share::
  165. runqemu-export-rootfs stop file-system-location
  166. - To restart the NFS share::
  167. runqemu-export-rootfs restart file-system-location
  168. QEMU CPU Compatibility Under KVM
  169. ================================
  170. By default, the QEMU build compiles for and targets 64-bit and x86 Intel
  171. Core2 Duo processors and 32-bit x86 Intel Pentium II processors. QEMU
  172. builds for and targets these CPU types because they display a broad
  173. range of CPU feature compatibility with many commonly used CPUs.
  174. Despite this broad range of compatibility, the CPUs could support a
  175. feature that your host CPU does not support. Although this situation is
  176. not a problem when QEMU uses software emulation of the feature, it can
  177. be a problem when QEMU is running with KVM enabled. Specifically,
  178. software compiled with a certain CPU feature crashes when run on a CPU
  179. under KVM that does not support that feature. To work around this
  180. problem, you can override QEMU's runtime CPU setting by changing the
  181. ``QB_CPU_KVM`` variable in ``qemuboot.conf`` in the :term:`Build Directory`
  182. ``deploy/image`` directory. This setting specifies a ``-cpu`` option passed
  183. into QEMU in the ``runqemu`` script. Running ``qemu -cpu help`` returns a
  184. list of available supported CPU types.
  185. QEMU Performance
  186. ================
  187. Using QEMU to emulate your hardware can result in speed issues depending
  188. on the target and host architecture mix. For example, using the
  189. ``qemux86`` image in the emulator on an Intel-based 32-bit (x86) host
  190. machine is fast because the target and host architectures match. On the
  191. other hand, using the ``qemuarm`` image on the same Intel-based host can
  192. be slower. But, you still achieve faithful emulation of ARM-specific
  193. issues.
  194. To speed things up, the QEMU images support using ``distcc`` to call a
  195. cross-compiler outside the emulated system. If you used ``runqemu`` to
  196. start QEMU, and the ``distccd`` application is present on the host
  197. system, any BitBake cross-compiling toolchain available from the build
  198. system is automatically used from within QEMU simply by calling
  199. ``distcc``. You can accomplish this by defining the cross-compiler
  200. variable (e.g. ``export CC="distcc"``). Alternatively, if you are using
  201. a suitable SDK image or the appropriate stand-alone toolchain is
  202. present, the toolchain is also automatically used.
  203. .. note::
  204. There are several mechanisms to connect to the system running
  205. on the QEMU emulator:
  206. - QEMU provides a framebuffer interface that makes standard consoles
  207. available.
  208. - Generally, headless embedded devices have a serial port. If so,
  209. you can configure the operating system of the running image to use
  210. that port to run a console. The connection uses standard IP
  211. networking.
  212. - SSH servers are available in some QEMU images. The ``core-image-sato``
  213. QEMU image has a Dropbear secure shell (SSH) server that runs with
  214. the root password disabled. The ``core-image-full-cmdline`` and
  215. ``core-image-lsb`` QEMU images have OpenSSH instead of Dropbear.
  216. Including these SSH servers allow you to use standard ``ssh`` and
  217. ``scp`` commands. The ``core-image-minimal`` QEMU image, however,
  218. contains no SSH server.
  219. - You can use a provided, user-space NFS server to boot the QEMU
  220. session using a local copy of the root filesystem on the host. In
  221. order to make this connection, you must extract a root filesystem
  222. tarball by using the ``runqemu-extract-sdk`` command. After
  223. running the command, you must then point the ``runqemu`` script to
  224. the extracted directory instead of a root filesystem image file.
  225. See the
  226. ":ref:`dev-manual/qemu:running under a network file system (nfs) server`"
  227. section for more information.
  228. QEMU Command-Line Syntax
  229. ========================
  230. The basic ``runqemu`` command syntax is as follows::
  231. $ runqemu [option ] [...]
  232. Based on what you provide on the command line, ``runqemu`` does a
  233. good job of figuring out what you are trying to do. For example, by
  234. default, QEMU looks for the most recently built image according to the
  235. timestamp when it needs to look for an image. Minimally, through the use
  236. of options, you must provide either a machine name, a virtual machine
  237. image (``*wic.vmdk``), or a kernel image (``*.bin``).
  238. Following is the command-line help output for the ``runqemu`` command::
  239. $ runqemu --help
  240. Usage: you can run this script with any valid combination
  241. of the following environment variables (in any order):
  242. KERNEL - the kernel image file to use
  243. ROOTFS - the rootfs image file or nfsroot directory to use
  244. MACHINE - the machine name (optional, autodetected from KERNEL filename if unspecified)
  245. Simplified QEMU command-line options can be passed with:
  246. nographic - disable video console
  247. serial - enable a serial console on /dev/ttyS0
  248. slirp - enable user networking, no root privileges is required
  249. kvm - enable KVM when running x86/x86_64 (VT-capable CPU required)
  250. kvm-vhost - enable KVM with vhost when running x86/x86_64 (VT-capable CPU required)
  251. publicvnc - enable a VNC server open to all hosts
  252. audio - enable audio
  253. [*/]ovmf* - OVMF firmware file or base name for booting with UEFI
  254. tcpserial=<port> - specify tcp serial port number
  255. biosdir=<dir> - specify custom bios dir
  256. biosfilename=<filename> - specify bios filename
  257. qemuparams=<xyz> - specify custom parameters to QEMU
  258. bootparams=<xyz> - specify custom kernel parameters during boot
  259. help, -h, --help: print this text
  260. Examples:
  261. runqemu
  262. runqemu qemuarm
  263. runqemu tmp/deploy/images/qemuarm
  264. runqemu tmp/deploy/images/qemux86/<qemuboot.conf>
  265. runqemu qemux86-64 core-image-sato ext4
  266. runqemu qemux86-64 wic-image-minimal wic
  267. runqemu path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial
  268. runqemu qemux86 iso/hddimg/wic.vmdk/wic.qcow2/wic.vdi/ramfs/cpio.gz...
  269. runqemu qemux86 qemuparams="-m 256"
  270. runqemu qemux86 bootparams="psplash=false"
  271. runqemu path/to/<image>-<machine>.wic
  272. runqemu path/to/<image>-<machine>.wic.vmdk
  273. ``runqemu`` Command-Line Options
  274. ================================
  275. Following is a description of ``runqemu`` options you can provide on the
  276. command line:
  277. .. note::
  278. If you do provide some "illegal" option combination or perhaps you do
  279. not provide enough in the way of options, ``runqemu``
  280. provides appropriate error messaging to help you correct the problem.
  281. - `QEMUARCH`: The QEMU machine architecture, which must be "qemuarm",
  282. "qemuarm64", "qemumips", "qemumips64", "qemuppc", "qemux86", or
  283. "qemux86-64".
  284. - `VM`: The virtual machine image, which must be a ``.wic.vmdk``
  285. file. Use this option when you want to boot a ``.wic.vmdk`` image.
  286. The image filename you provide must contain one of the following
  287. strings: "qemux86-64", "qemux86", "qemuarm", "qemumips64",
  288. "qemumips", "qemuppc", or "qemush4".
  289. - `ROOTFS`: A root filesystem that has one of the following filetype
  290. extensions: "ext2", "ext3", "ext4", "jffs2", "nfs", or "btrfs". If
  291. the filename you provide for this option uses "nfs", it must provide
  292. an explicit root filesystem path.
  293. - `KERNEL`: A kernel image, which is a ``.bin`` file. When you provide a
  294. ``.bin`` file, ``runqemu`` detects it and assumes the file is a
  295. kernel image.
  296. - `MACHINE`: The architecture of the QEMU machine, which must be one of
  297. the following: "qemux86", "qemux86-64", "qemuarm", "qemuarm64",
  298. "qemumips", "qemumips64", or "qemuppc". The MACHINE and QEMUARCH
  299. options are basically identical. If you do not provide a MACHINE
  300. option, ``runqemu`` tries to determine it based on other options.
  301. - ``ramfs``: Indicates you are booting an :term:`Initramfs`
  302. image, which means the ``FSTYPE`` is ``cpio.gz``.
  303. - ``iso``: Indicates you are booting an ISO image, which means the
  304. ``FSTYPE`` is ``.iso``.
  305. - ``nographic``: Disables the video console, which sets the console to
  306. "ttys0". This option is useful when you have logged into a server and
  307. you do not want to disable forwarding from the X Window System (X11)
  308. to your workstation or laptop.
  309. - ``serial``: Enables a serial console on ``/dev/ttyS0``.
  310. - ``biosdir``: Establishes a custom directory for BIOS, VGA BIOS and
  311. keymaps.
  312. - ``biosfilename``: Establishes a custom BIOS name.
  313. - ``qemuparams=\"xyz\"``: Specifies custom QEMU parameters. Use this
  314. option to pass options other than the simple "kvm" and "serial"
  315. options.
  316. - ``bootparams=\"xyz\"``: Specifies custom boot parameters for the
  317. kernel.
  318. - ``audio``: Enables audio in QEMU. The MACHINE option must be either
  319. "qemux86" or "qemux86-64" in order for audio to be enabled.
  320. Additionally, the ``snd_intel8x0`` or ``snd_ens1370`` driver must be
  321. installed in linux guest.
  322. - ``slirp``: Enables "slirp" networking, which is a different way of
  323. networking that does not need root access but also is not as easy to
  324. use or comprehensive as the default.
  325. - ``kvm``: Enables KVM when running "qemux86" or "qemux86-64" QEMU
  326. architectures. For KVM to work, all the following conditions must be
  327. met:
  328. - Your MACHINE must be either qemux86" or "qemux86-64".
  329. - Your build host has to have the KVM modules installed, which are
  330. ``/dev/kvm``.
  331. - The build host ``/dev/kvm`` directory has to be both writable and
  332. readable.
  333. - ``kvm-vhost``: Enables KVM with VHOST support when running "qemux86"
  334. or "qemux86-64" QEMU architectures. For KVM with VHOST to work, the
  335. following conditions must be met:
  336. - ``kvm`` option conditions defined above must be met.
  337. - Your build host has to have virtio net device, which are
  338. ``/dev/vhost-net``.
  339. - The build host ``/dev/vhost-net`` directory has to be either
  340. readable or writable and "slirp-enabled".
  341. - ``publicvnc``: Enables a VNC server open to all hosts.