introduction.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. Introduction
  2. ============
  3. .. _Accelerators:
  4. Virtualisation Accelerators
  5. ---------------------------
  6. QEMU's system emulation provides a virtual model of a machine (CPU,
  7. memory and emulated devices) to run a guest OS. It supports a number
  8. of hypervisors (known as accelerators) as well as a JIT known as the
  9. Tiny Code Generator (TCG) capable of emulating many CPUs.
  10. .. list-table:: Supported Accelerators
  11. :header-rows: 1
  12. * - Accelerator
  13. - Host OS
  14. - Host Architectures
  15. * - KVM
  16. - Linux
  17. - Arm (64 bit only), MIPS, PPC, RISC-V, s390x, x86
  18. * - Xen
  19. - Linux (as dom0)
  20. - Arm, x86
  21. * - Hypervisor Framework (hvf)
  22. - MacOS
  23. - x86 (64 bit only), Arm (64 bit only)
  24. * - Windows Hypervisor Platform (whpx)
  25. - Windows
  26. - x86
  27. * - NetBSD Virtual Machine Monitor (nvmm)
  28. - NetBSD
  29. - x86
  30. * - Tiny Code Generator (tcg)
  31. - Linux, other POSIX, Windows, MacOS
  32. - Arm, x86, Loongarch64, MIPS, PPC, s390x, Sparc64
  33. Feature Overview
  34. ----------------
  35. System emulation provides a wide range of device models to emulate
  36. various hardware components you may want to add to your machine. This
  37. includes a wide number of VirtIO devices which are specifically tuned
  38. for efficient operation under virtualisation. Some of the device
  39. emulation can be offloaded from the main QEMU process using either
  40. vhost-user (for VirtIO) or :ref:`Multi-process QEMU`. If the platform
  41. supports it QEMU also supports directly passing devices through to
  42. guest VMs to eliminate the device emulation overhead. See
  43. :ref:`device-emulation` for more details.
  44. There is a full :ref:`featured block layer<Live Block Operations>`
  45. which allows for construction of complex storage topology which can be
  46. stacked across multiple layers supporting redirection, networking,
  47. snapshots and migration support.
  48. The flexible ``chardev`` system allows for handling IO from character
  49. like devices using stdio, files, unix sockets and TCP networking.
  50. QEMU provides a number of management interfaces including a line based
  51. :ref:`Human Monitor Protocol (HMP)<QEMU monitor>` that allows you to
  52. dynamically add and remove devices as well as introspect the system
  53. state. The :ref:`QEMU Monitor Protocol<QMP Ref>` (QMP) is a well
  54. defined, versioned, machine usable API that presents a rich interface
  55. to other tools to create, control and manage Virtual Machines. This is
  56. the interface used by higher level tools interfaces such as `Virt
  57. Manager <https://virt-manager.org/>`_ using the `libvirt framework
  58. <https://libvirt.org>`_.
  59. For the common accelerators QEMU, supported debugging with its
  60. :ref:`gdbstub<GDB usage>` which allows users to connect GDB and debug
  61. system software images.
  62. Running
  63. -------
  64. QEMU provides a rich and complex API which can be overwhelming to
  65. understand. While some architectures can boot something with just a
  66. disk image, those examples elide a lot of details with defaults that
  67. may not be optimal for modern systems.
  68. For a non-x86 system where we emulate a broad range of machine types,
  69. the command lines are generally more explicit in defining the machine
  70. and boot behaviour. You will find often find example command lines in
  71. the :ref:`system-targets-ref` section of the manual.
  72. While the project doesn't want to discourage users from using the
  73. command line to launch VMs, we do want to highlight that there are a
  74. number of projects dedicated to providing a more user friendly
  75. experience. Those built around the ``libvirt`` framework can make use
  76. of feature probing to build modern VM images tailored to run on the
  77. hardware you have.
  78. That said, the general form of a QEMU command line can be expressed
  79. as:
  80. .. parsed-literal::
  81. $ |qemu_system| [machine opts] \\
  82. [cpu opts] \\
  83. [accelerator opts] \\
  84. [device opts] \\
  85. [backend opts] \\
  86. [interface opts] \\
  87. [boot opts]
  88. Most options will generate some help information. So for example:
  89. .. parsed-literal::
  90. $ |qemu_system| -M help
  91. will list the machine types supported by that QEMU binary. ``help``
  92. can also be passed as an argument to another option. For example:
  93. .. parsed-literal::
  94. $ |qemu_system| -device scsi-hd,help
  95. will list the arguments and their default values of additional options
  96. that can control the behaviour of the ``scsi-hd`` device.
  97. .. list-table:: Options Overview
  98. :header-rows: 1
  99. :widths: 10, 90
  100. * - Options
  101. -
  102. * - Machine
  103. - Define the machine type, amount of memory etc
  104. * - CPU
  105. - Type and number/topology of vCPUs. Most accelerators offer
  106. a ``host`` cpu option which simply passes through your host CPU
  107. configuration without filtering out any features.
  108. * - Accelerator
  109. - This will depend on the hypervisor you run. Note that the
  110. default is TCG, which is purely emulated, so you must specify an
  111. accelerator type to take advantage of hardware virtualization.
  112. * - Devices
  113. - Additional devices that are not defined by default with the
  114. machine type.
  115. * - Backends
  116. - Backends are how QEMU deals with the guest's data, for example
  117. how a block device is stored, how network devices see the
  118. network or how a serial device is directed to the outside world.
  119. * - Interfaces
  120. - How the system is displayed, how it is managed and controlled or
  121. debugged.
  122. * - Boot
  123. - How the system boots, via firmware or direct kernel boot.
  124. In the following example we first define a ``virt`` machine which is a
  125. general purpose platform for running Aarch64 guests. We enable
  126. virtualisation so we can use KVM inside the emulated guest. As the
  127. ``virt`` machine comes with some built in pflash devices we give them
  128. names so we can override the defaults later.
  129. .. code::
  130. $ qemu-system-aarch64 \
  131. -machine type=virt,virtualization=on,pflash0=rom,pflash1=efivars \
  132. -m 4096 \
  133. We then define the 4 vCPUs using the ``max`` option which gives us all
  134. the Arm features QEMU is capable of emulating. We enable a more
  135. emulation friendly implementation of Arm's pointer authentication
  136. algorithm. We explicitly specify TCG acceleration even though QEMU
  137. would default to it anyway.
  138. .. code::
  139. -cpu max \
  140. -smp 4 \
  141. -accel tcg \
  142. As the ``virt`` platform doesn't have any default network or storage
  143. devices we need to define them. We give them ids so we can link them
  144. with the backend later on.
  145. .. code::
  146. -device virtio-net-pci,netdev=unet \
  147. -device virtio-scsi-pci \
  148. -device scsi-hd,drive=hd \
  149. We connect the user-mode networking to our network device. As
  150. user-mode networking isn't directly accessible from the outside world
  151. we forward localhost port 2222 to the ssh port on the guest.
  152. .. code::
  153. -netdev user,id=unet,hostfwd=tcp::2222-:22 \
  154. We connect the guest visible block device to an LVM partition we have
  155. set aside for our guest.
  156. .. code::
  157. -blockdev driver=raw,node-name=hd,file.driver=host_device,file.filename=/dev/lvm-disk/debian-bullseye-arm64 \
  158. We then tell QEMU to multiplex the :ref:`QEMU monitor` with the serial
  159. port output (we can switch between the two using :ref:`keys in the
  160. character backend multiplexer`). As there is no default graphical
  161. device we disable the display as we can work entirely in the terminal.
  162. .. code::
  163. -serial mon:stdio \
  164. -display none \
  165. Finally we override the default firmware to ensure we have some
  166. storage for EFI to persist its configuration. That firmware is
  167. responsible for finding the disk, booting grub and eventually running
  168. our system.
  169. .. code::
  170. -blockdev node-name=rom,driver=file,filename=(pwd)/pc-bios/edk2-aarch64-code.fd,read-only=true \
  171. -blockdev node-name=efivars,driver=file,filename=$HOME/images/qemu-arm64-efivars