qemu.rst 19 KB

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