First Impressions of KVM/QEMU

Sunday, November 13, 2016 🌐中文

Preface

I’ve been using Linux seriously for 11 months now. From Debian to Arch, Linux has become part of my daily life.

People around me use QQ and WeChat. I still need them to send files and pictures, but I don’t want to work in Windows, so I installed VMware Workstation to run a virtual machine.

At first, running Windows 10 on Debian was smooth. After switching to Arch, it often stuttered because ntfs3g consumed too much CPU. Later I switched the host-side partition filesystem for the VM to Ext4, which helped a bit. After removing the built-in apps from Windows 10, things became much better.

KVM is short for Kernel-based Virtual Machine. It’s an open-source system virtualization module and a full virtualization solution for x86 hardware platforms on Linux.

QEMU is a powerful open-source emulator: it can emulate systems and a wide range of hardware.

guide_fig

With KVM acceleration, QEMU can achieve much better performance. I had never used QEMU before, so I spent a lot of time getting started and digging into details. I learned a lot in the process and gained a deeper understanding of virtualization.

IBM DeveloperWorks (CN) is full of high-quality content, along with official docs and wikis for many products. The built-in man pages are also very detailed.

It’s thanks to developers’ great contributions that I can work and have fun on Linux so conveniently. I’m also a bit ashamed that I’m not yet in a position to contribute back.

Installation

To make learning easier, I decided not to use a GUI at first. (Later I realized I was too naive: in a real environment there’s far more configuration than I expected, and it’s much more complex—CPU, keyboard, USB, audio, etc. are all tricky to get right.)

sudo pacman -S qemu

But installing a GUI frontend is still a good idea. I tried qtemu and qemu-launcher; aqemu feels more usable.

yaourt aqemu

Adding a Disk

Creating a disk image

QEMU supports many image formats, including VMDK, VDI, VHD (vpc), VHDX, qcow1, and QED. You can manage them with qemu-img.

In practice, raw and qcow2 are most commonly used.

raw is a raw disk image format. It’s the fastest, but it allocates the disk space immediately.

qcow2 is a bit slower, but it doesn’t need to allocate space upfront and supports many features such as snapshots, rollback, encryption, and compression.

For example, create a raw disk image:

qemu-img create -f raw Kali_Linux.img 20G

Or create a qcow2 image with 2MB cluster size and full preallocation:

qemu-img create -f qcow2 -o cluster_size=2M,preallocation=full Kali_Linux.qcow2 4G

Format conversion

qemu-img also supports converting disk image formats. In some cases, you need to migrate disk images.

For example, I wanted to compare Kali Linux performance under KVM vs VMware, so I needed to convert a VMDK into a disk image format that’s more suitable for QEMU.

Convert vmdk to raw

qemu-img convert -f vmdk -O raw Kali\ 2016\ 64-bit.vmdk Kali_Linux.img

Convert vmdk to qcow2

qemu-img convert -f vmdk -O qcow2 Kali\ 2016\ 64-bit.vmdk Kali_Linux.qcow2

Enabling hardware-assisted virtualization

Make sure VT is enabled in the BIOS. My machine is Intel-based, so I added intel_iommu=on to the kernel parameters. For AMD, use amd_iommu=on.

IOMMU essentially allows mapping hardware devices directly into a VM, addressing peripheral device support and improving hardware utilization.

Edit /boot/grub/grub.cfg:

linux   /boot/vmlinuz-linux root=UUID=6bb8eda5-d588-4e6a-8f5e-bbc856cc9f96 rw quiet intel_iommu=on

After rebooting, verify that IOMMU is enabled:

gorgias@3vil ~> dmesg | grep -e DMAR -e IOMMU
[    0.000000] DMAR: IOMMU enabled

Install a graphics driver—here I’ll try VMware’s first. After installing, the mouse-cursor issue is fixed, but when entering the desktop you may see a few seconds of screen corruption.

sudo pacman -S xf86-video-vmware xf86-input-vmmouse

Try launching the VM with these parameters: use the Kali_Linux.qcow2 disk image, 2GB memory, VMware VGA, emulated Q35 chipset, KVM acceleration, and enable IOMMU.

qemu-system-x86_64 /home/gorgias/media/Lancer/KVM/Kali\ Linux/Kali_Linux.qcow2 -enable-kvm -m 2048 -vga vmware -machine q35,accel=kvm,type=q35 -device intel-iommu

It feels quite smooth.

demo

QEMU monitor

QEMU provides a monitor for inspecting a VM’s runtime state.

In the QEMU window, press Ctrl + Alt + 2 (not on the numpad) to switch to the QEMU monitor. If it doesn’t work, check whether the shortcut conflicts with something else.

You can also add -monitor dev when launching the VM. For example, -monitor stdio allows standard input as the monitor command source.

In the monitor, run info kvm to check whether the KVM module is enabled:

(qemu) info kvm
kvm support: enabled

QEMU_monitor

AQEMU

There’s no way I’m configuring everything by hand. No way. Not in this lifetime.

QEMU is extremely powerful and worth deep study—but for most of us, casually playing with it is enough.

AQEMU

The UI looks a bit like VirtualBox and it’s feature-complete. Configure it like you would in VMware and the VM will boot.

In terms of both performance and usability, VMware is still much stronger.

Reference

Arch Linux Wiki - QEMU

WIKIBOOKS - QEMU/Monitor

linux-kvm.org

Toolsarchkvmqemu

Building OpenWrt Firmware from Source

Cracking a Milk Membership Card with an ACR122U on Arch Linux