dev-manual-qemu.rst 19 KB

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