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