This is a short but intensive QEMU-KVM tutorial

Introduction to KVM

Kernel-based Virtual Machine (KVM) is a virtualization solution for the Linux kernel. KVM is a part of the Linux Kernel since version 2.6.20 (Feb. 2007). Also, KVM requires a processor with a hardware virtualization extension, which is meanwhile present on most desktop and server processors. Future Kernel versions will include KVM for ARM architecture too, making its ubiquitous available.

qemu-kvm

Talking about kvm is talking about virtualization technology or about kernel modules (kvm.ko, kvm-intel.ko or kvm-amd-ko). Sometimes kvm is mentioned as a virtual machine but this is not correct, because kvm does not provide virtualized hardware. This is where QEMU (generic machine emulator and virtualizer) comes to play.

Qemu-kvm is a stable fork of QEMU point releases with kvm features or extras on top of it. I’m a bit unsure about the state of the art, but there are plans to merge qemu-kvm fork completely to qemu (correct me please if you know more about it). However, current article is about qemu-kvm which is available in most common Linux distributions right now.

Setup qemu-kvm

The further description assumes you have root access to your Linux system. The following examples were tested on Linux Debian wheezy and Ubuntu 12.04 LTS.

1. Check Requirements

There is no sense to handle with kvm when underlying hardware doesn’t support Hardware-Virtualization, so check this first.

egrep -c '(vmx|svm)' /proc/cpuinfo
#0 bad
#1 or more is good

Even if supported, HV-Virtualization is often switched off in BIOS by default. Let’s check this also. If virtualization is on, kvm modules should be already loaded.

lsmod | grep kvm
#prints:
#kvm
#kvm_intel or kvm_amd

If modules are not loaded try to load them manually.

#eg. for intel
modprobe kvm
sudo modprobe kvm_intel

And if not successful, Check detailed messages.

dmesg | grep kvm

If BIOS is mentioned in the output, you know what to do - reboot and activate virtualization support.

2. Install qemu-kvm package

Now if you are ready install qemu-kvm

sudo apt-get install qemu-kvm

Now check some details of /dev/kvm

ls -la /dev/kvm
#crw-rw----+ 1 root kvm 10, 232 Feb 27 12:16 /dev/kvm

This is usually the case. Device /dev/kvm is writable by kvm group. So add a user to kvm group that should be able to start VM’s. That’s is. I also recommend adding the current user to kvm group

sudo adduser $USER kvm

3. Optional features

If you need to give your VM’s more RAM than physically exist on host system you have to use KSM (Read more about it if unsure). First, check if it is supported

grep KSM /boot/config-`uname -r`
#out: CONFIG_KSM=y
#indicates supported

And is it enabled?

cat /sys/kernel/mm/ksm/run
#0 inicates disabled
#1 indicates enabled
echo 1  > /sys/kernel/mm/ksm/run #enable
echo 2  > /sys/kernel/mm/ksm/run # disable

Image files

Virtual machines persist in containers or virtual drive images, therefore we should get familiar with them first.

Short overview of Image-Files formats

  • raw default format. Allows flexible converting. Takes only used side on host, but only if used ext4 and (ext3?). This is called spare file or spare image
  • qed enhanced disc format for faster access (since QEMU 0.14). It supports overlay and sparse images. Overlay means that on create you can assign allready existing image as base and only differences will be written to the overlay image (Beware: base image has to stay unchanged!). Its also faster than qcow2
  • qcow2 is most featured format in QEMU and is meant to replace qcow. This format support sparse images independent of underlying fs capabilities. It supports multiple VM-snapshots, encryption (AES) and compression.
  • qcowis old QEMU format. Images in qcow are sparse and like qcow2 independent of underlying file system capabilities.
  • vmdk is standard format for VMware Workstation. Overlay function is similar to qcow2.
  • vdi is standard format for Virtual Box.
  • parallels standard image tp of virtualization solutions of Paralles Inc.
  • vpc standard image format for Microsoft Virtual PC

Creating and managing qemu images

Creating of images is supported through qemu-img command. Here are few examples of how to create and manage image files

#Create 10G big raw image.
qemu-img create testimg.img 10G
#Use qed format
qemu-img create -f qed testimage.qed 1G
# out: Formatting 'testimage.qed', fmt=qed size=1073741824 cluster_size=65536 table_size=0

#Create overlay qcow2 image to base_image.img
qemu-img create -b base_image.img -f qcow2 base_overlay.ovl

We can retrieve some info on created files.

qemu-img info testimage.qed
#image: testimage.qed
#file format: qed
#virtual size: 1.0G (1073741824 bytes)
#disk size: 260K
#cluster_size: 65536

And cause the disk was not used the real size on the host is 260k at the moment. Beware of copy sparse files use --sparse=allways option in cp command (mostly it is chosen for you) otherwise the images could be copied hard and fill the maximum amount of their creation size.

Here some additional examples.

#making it 5GB bigger
qemu-img resize myimage.img +5GB
#converting ra image to qcow2
qemu-img convert -f raw -O qcow2 sourceimg.img target-dyn.img
#It make sense to convert qcow2 to qcow2. Not used sectors would be recognized and resulting image will be smaller.
qemu-img convert -f qcow2 -O qcow2 big.img resultingsmaller.img
#zipping image
qemu-img convert -c -O qcow2 source.img source_with_zipped_sectors.img
#encrypt
qemu-img convert -O qcow2 -o encryption source.img encrypted.img # ask for password (user large password to maximize security)

Starting first VM

The main utility to start VM is qemu-system-x86 or qemu-system-x86_64 dependent on your host architecture. Also, there is kvm command alias, but should be deprecated. Let’s start them!

#Starting debian installation from net inst CD image.
qemu-system-x86_64 -hda disk.img -cdrom debian-607-amd64-netinst.iso -boot d
#Starting debian installed debian from virual hdd: disk.img
qemu-system-x86_64 -hda disk.img

See more options on the manpage

Networking

The above examples were not networking capable so far, because no network device was assigned. The option -net nic assigns emulated network card.

#Starts an vm with networkcard
qemu-system-x86_64 -hda disk.img -net nic
#Assigns nic with specifig MAC
qemu-system-x86_64 -hda disk.img -net nic,macaddr=00:20:ac:1f:dd:01
#Ask for supportet nic models
qemu-system-x86_64 -net nic,model=?

The easiest way to network your VM is to use qemu user-mode networking

qemu-system-x86_64 disk.img -net nic -net user

It creates a private virtual network (10.0.2.0) with a firewall and DHCP server listening on 10.0.2.2 which is also a gateway, Simple DNS Server (10.0.2.3), and appropriate routing. Virtual guest will be 10.0.2.15. I didn’t test it extensively, but your guest’s systems would be visible to the network of the host system (correct me please if I’m wrong) because user mode does not NAT. That makes you dependent on network infrastructure outside of your host PC.

If you prefer a more elaborated solution with a private virtual network and NAT wait for my next article. ;)

Hope you gained new insights. Please feel free to give me your feedback.