QEMU

From LinuxReviews
Jump to navigationJump to search
QEMU
QEMU 5.2.0 booting Gentoo Linux.
QEMU 5.2.0 booting Gentoo Linux.
Original author(s)Fabrice Bellard
Repositorygit.qemu.org/git/qemu.git
Written inC
Operating systemLinux, macOS, Microsoft Windows
License GNU GPL v2
Websitewww.qemu.org
Cpu-icon2.png

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.

Architectures[edit]

QEMU can emulate the following CPU architectures:

  • IA-32 (x86) and x86-64 PCs
  • PowerPC (PReP & Power Macintosh)
  • RISC-V
  • MISP64 v6 and earlier variants
  • Sun SPARC (sun4m and sun4u)
  • ETRAX CRIS
  • ARM development boards (Versatile/PB & Integrator/CP)
  • MicroBlaze

Disk Formats[edit]

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 (.qed, .qcow or .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 dd)
  • macOS Universal Disk Image Format (.dmg). Read-only.
  • 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)

Typical Usage[edit]

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.

Qemu-5.2.0-running-xfce-desktop-on-gentoo.jpg
QEMU 5.2.0 running Gentoo Linux with a Xfce desktop.

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

-m How much memory the virtual machine should have
-vga cirrus|vmware|qxl|xenfb|tcx|cg3|virtio|none])
-soundhw Sound card hardware. hda is typically the best option.
-nic/code> Virtual network interface.

Accelerated CPU Using KVM[edit]

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 qemu-system-x86_64.

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[edit]

The -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 media-libs/mesa.

The OpenGL performance you get with the virgl driver is dismal, to put it mildly.

glmark2, terrain, 1920x1050

virgl is faster than running software rendering inside the virtual machine, so there is an advantage to it.

glmark2, terrain, 1920x1050

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[edit]

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.

Arch Linux /usr/share/edk2-ovmf/x64/OVMF_CODE.fd
Debian/Ubuntu /usr/share/OVMF/OVMF_CODE.fd
Fedora /usr/share/edk2/ovmf/OVMF_CODE.fd
Gentoo /usr/share/edk2/ovmf/OVMF_CODE.fd

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[edit]

ISO images can be added to virtual machines with -cdrom filename.iso. It is also possible to add them with a series of -drive options. -cdrom is faster.

Common Problems[edit]

  • 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.

Links[edit]

The QEMU website is at https://www.qemu.org/ and the git repository is at https://git.qemu.org/git/qemu.git


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.