Virtualization with KVM and Debian Lenny
Some weeks ago I’ve started to work with virtualized machines, and as an old Linux user, I’ve choosen KVM as virtualization infrastructure. KVM is part of the Linux kernel since the 2.6.20 version and Debian Lenny 5.0 was released with the 2.6.26 kernel version. But in order to work with KVM we need a CPU processor with virtualization support, so the first step is to check if our CPU has it. You have to execute:
egrep '(vmx|svm)' --color=always /proc/cpuinfo
Now you should see a colored flag for each processor core. If you see it, you can go ahead with the following steps.
Although we don’t need any extra repository, I’ve decide to use the backports.org semi-official repository to take advantage of the more recent software releases. To enable it, type the following commands:
echo "deb http://www.backports.org/debian lenny-backports main contrib non-free" \ > /etc/apt/sources.list.d/backports.list && \ wget -O - http://backports.org/debian/archive.key | apt-key add - && \ aptitude update
Now, we have to install the required packages:
aptitude install -t lenny-backports kvm virtinst libvirt-bin
Finally we must prepare the network to allow the virtual machine to connect with the world. The Debian libvirt-bin package comes with a preconfigured private network (192.168.122.0/24) with a DHCP server managed with dnsmasq. This network is called default and if we need some virtual machine to start automatically after the host reboot, we have to start this network automatically too. We can do it with virsh, a Swiss knife command to manage our virtual machines with the help of libvirt.
virsh net-start default && virsh net-autostart default
Ok, we’re ready to install our new virtual machine with the help of the virt-inst command. We will install a virtual machine named vmguest, with 512Mb or ram, running linux (Debian Lenny) using kvm as virtualization infrastructure. The disk will be allocated onto the host filesystem in a raw disk image (/opt/vmguest-root.disk) with a size of 20G and using virtio as the block device driver. The virtual machine will be connected to the default network with a virtio network card. The virtual machine will start a VNC server to allow you to connect to the screen and will disable the connection to the virtual machine serial console. Finally the default keymap will be es and the installer will be downloaded from an external location instead of using a cdrom image.
virt-install --name=vmguest \ --ram 512 \ --os-type linux --os-variant debianlenny \ --virt-type kvm \ --disk path=/opt/vmguest-root.disk,bus=virtio,size=20 \ -w network=default,model=virtio \ --vnc \ --noautoconsole \ --keymap es \ --location http://ftp.debian.org/debian/dists/lenny/main/installer-amd64
Now we have to connect to the virtual machine session in order to complete the installation. To do it I usually use virt-viewer through an ssh tunnel to connect to the host machine (remember to install it if you need it). To do it type:
virt-viewer -c qemu+ssh://firstname.lastname@example.org/system vmguest
If you are directly connected to the host machine, you have to type as root:
virt-viewer -c qemu:///system vmguest
Ok, that’s all. You ready to install your new Debian Lenny onto the virtual machine. Once installed, the virtual machines will be managed with the virsh command… but it will be on another host.
Edited: For this first post about KVM I think it’s a better idea to save the disk onto the host filesystem, although I prefer LVM, because the file based disk image does not need any previous setup.
Edited 2: This post was written when the libvirt backport version was 0.6.5-3. I you have any problem with the latest backport packages you can download the old ones from http://snapshot.debian.org/package/libvirt/0.6.5-3~bpo50%2B1/