QEMU 5.2.0 booting Gentoo Linux.
|Original author(s)||Fabrice Bellard|
|Operating system||Linux, macOS, Microsoft Windows|
|License||GNU GPL v2|
QEMU is a very versatile and powerful multi-platform computer emulator capable of both full emulation and hardware virtualization. It can be used to run operating systems for a variety of hardware and CPU architectures without hardware acceleration or Linux operating systems for x86-64 on x86-64 hardware at near-native speeds using virtualization hardware extensions on Intel and AMD processor hardware.
QEMU is not some kind of API re-implementation (like WINE). It can emulate a complete computer with virtual graphics adapters, virtual network equipment, virtual sound cards and so on. This makes it possible to run operating systems for x86, PowerPC, ARM, SPARC, MicroBlace, OpenRISC and a few more on any of the platforms and operating systems capable of running the QEMU emulator.
QEMU can also be used by user-level processes to run software compiled for one architecture on another.
QEMU can emulate the following CPU architectures:
- IA-32 (x86) and x86-64 PCs
- PowerPC (PReP & Power Macintosh)
- MISP64 v6 and earlier variants
- Sun SPARC (sun4m and sun4u)
- ETRAX CRIS
- ARM development boards (Versatile/PB & Integrator/CP)
QEMU has its own native disk container format called QCOW (QEMU copy-on-write). These can be created by the
qemu-img utility with a
create argument followed by
-f qcow2 (or other format), a filename and the desired size of the image. A container can be created with:
qemu-img create -f qcow2 arch-linux.qcow2 12G
.qcow2 is the current native QEMU disk format. Older QCOW images can have other extensions (
.cow). The newest QCOW3 disk format is only a backwards-compatible extension of QCOW2 (see Features/Qcow3 at the QEMU Wiki) so QCOW3 images are in all practical applications referred to as
qcow2 (you can not
qemu-img create -f qcow3).
QEMU can, in addition to QCOW images, work with:
- ISO images (historically used for CD/DVD images, typically used by Linux distribution install images)
- Raw images (.img) that contain sector-by-sector contents of a disk (can be created using
- macOS Universal Disk Image Format (
- Bochs . Read-only.
- Linux cloop. Read-only.
- Parallels disk image (.hdd, .hds). Read-only.
- VirtualBox Virtual Disk Image (.vdi)
- Virtual PC Virtual Hard Disk (.vhd)
- Virtual VFAT
- VMware Virtual Machine Disk (.vmdk)
QEMU is very powerful, but it is not very user-friendly. Getting a virtual machine with something like a bootable ISO image up and running is not as easy as running
qemu linuxdistro.iso. A rather long list of various command-line arguments is required. The list of command-line arguments will depend on what kind of virtual machine image you want to run, what actual hardware you are running it on, what kind of network configuration you want, what virtual storage devices should be made available and so on.
Launching a virtual machine with Gentoo Linux could be done like this:
qemu-kvm -m 12288 -vga virtio -display gtk,gl=on \ -cpu host -smp 4 \ -usbdevice tablet -soundhw hda \ -drive file=/home/yuri/QEMU/gentoo.qcow,index=0,media=disk,aio=threads,cache.no-flush=on,cache.writeback=on,cache.direct=off \ -drive file=/home/yuri/QEMU/qemu-swap.qcow,index=1,media=disk,aio=threads,cache.no-flush=on,cache.writeback=on,cache.direct=off -drive file=/home/yuri/QEMU/tempdrive.qcow,index=3,media=disk,aio=threads,cache.no-flush=on,cache.writeback=on,cache.direct=off
The last 3 lines are all adding native file system files as virtual hard drives. You won't need those, but you will likely want to configure
||How much memory the virtual machine should have|
||Sound card hardware. |
||Virtual network interface.|
Accelerated CPU Using KVM
Most modern processors support some kind of virtualization. QEMU can take advantage of that; the only thing you need to do is to make sure you run
qemu-kvm instead of
KVM can not be used with other CPU architectures (you can't run
qemu-system-riscv64 with KVM), it is only for x86-64.
Accelerated Video Output
-vga virtio -display gtk,gl=on combination is typically a good choice for running newer GNU/Linux distributions in a virtual machine. This allows the virtual machine to use OpenGL capabilities on the host machine. Performance is dismal, but it is faster than software rendering. Vulkan may or may not be supported in the future (see QEMU Support For Rendering Vulkan Software On The Hosts GPU In Virtual Machines Using Virgil 3D Is Underway).
There are some requirements for using the
virtio QEMU video output with virtual machines running Linux. You will need to have a kernel compiled with
CONFIG_DRM_VIRTIO_GPU inside the virtual machine, and you will need to have Mesa compiled with the
virgl driver. Most Linux distributions have this driver available as a part of the standard Mesa driver package (
mesa-dri-drivers). Gentoo users need to make sure they have set
VIDEO_CARDS=virgl when compiling
The OpenGL performance you get with the virgl driver is dismal, to put it mildly.
virgl is faster than running software rendering inside the virtual machine, so there is an advantage to it.
The difference between 4 and 60 fps in the glmark2 benchmark is pretty huge. Do note that that particular benchmark produces 516 fps natively. That's a lot more than 60..
Booting With EFI BIOS
There's a free EFI BIOS called "OVMF" (Open Virtual Machine Firmware) available on most distributions (usually in a package called
edk2-ovmf). QEMU can use this EFI BIOS if you point it to a OVMF BIOS file with
-bios. The path where it is installed will depend on your distribution.
Example (memtest86 5.x+ requires a EFI BIOS, so it is a good way to test if it works):
wget https://www.memtest86.com/downloads/memtest86-usb.zip unzip memtest86-usb.zip qemu-system-x86_64 \ -enable-kvm -m 2G \ -bios /usr/share/edk2-ovmf/OVMF_CODE.fd \ -drive file=memtest86-usb.img,format=raw
ISO images can be added to virtual machines with
-cdrom filename.iso. It is also possible to add them with a series of
-cdrom is faster.
- Linux Kernel version 5.1 does NOT play nice with
-vga virtio -display gtk,gl=on. This was a problem specific to Linux 5.1 and the early 5.2rc series; it is not a general problem with Linux 5.x.