This is 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 hardware virtualization extension, which is meanwhile present on most desktop and server processors. Future Kernel versions will include KVM for ARM architecture too, making it's 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's 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). However below description is about qemu-kvm which is available in most common linux distributions.

Setup qemu-kvm

Further description assumes you have root access to your linux system. Following examples were tested on Linux Debian wheezey and Ubutuntu 12.04 LTS.

Check Requirenments

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

Installing qemu-kvm package

Now if your 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 usual the case. Device /dev/kvm is writable by kvm group. So add user to kvm group that should be able to start VM's.

That's is.

I also recommend to add the current user to kvm group

sudo adduser $USER kvm

Optionals

If you need to give your vm's more RAM that physically exist on hosts 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

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

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.

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. qcow is old QEMU format. Images in qcow are sparse and like qcow2 independent of underlying file system capabilities.

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

is standard format for VMware Workstation. Overlay function is similar to qcow2. vdi is standard format for Virtual Box.

is standard format for Virtual Box. parallels standard image tp of virtualization solutions of Paralles Inc.

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 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 disk was not used the real size on the host is 260k at the moment. Beware on copy sparse files use --sparse=allways option in cp command (mostly it is chosen for you) otherwise the images could be copy 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 to you host architecture. Also there is kvm command alias, but should be deprecated. Let's start them!

#Starting debian installtion 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

Above examples where not network capable, no network device was assigned. The option -net nic assigns emulatet 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=?

Te 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 private virtual network (10.0.2.0) with a firewall and DHCP server listening on 10.0.2.2 wich is also a gateway, Simple DNS Server (10.0.2.3) and apropriate routing. Virtual guest will be 10.0.2.15. I din't tested it extensive, but your guests systems would be visible to the network of the hostsystem (correct me please if i'm wrong) because user mode does not NAT. That makes you dependent to network infrastructure outside of your host PC.

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

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

Other

Native Linux KVM Tool kvm based virtulization tool, which has the idea to be very slim but also reduced to few features (e.g only linux guests).