In this article, you'll learn how easy it is to create a Linux Kernel-based virtual machine, or KVM, virtualisation host. KVM can provide a good foundation for Linux-based virtualisation. You'll also learn how to install Windows and Ubuntu as virtualised operating systems in the KVM-virtualised environment.
I use Ubuntu Server in this tip. Other Linux distributions will work; however, the names of configuration files used may be different.
Networking with KVM virtualisation
On a server where virtualisation is used, you can have more virtual machines than you have network boards. Therefore, a solution needs to be implemented for the virtual machines to share network boards in your server. To make this possible, you need to create a virtual network bridge. To do this, you need to redefine the contents of the /etc/network/interface file as in the follow example (figure 1). This code is meant to replace all contents that you currently have in this file, but you must modify it to reflect the number of network cards in your server:
Figure 1: To provide network access for all virtual machines, you need a network bridge:
auto lo iface lo inet loopback auto br0 iface br0 inet static address 192.168.1.99 network 192.168.1.0 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.254 bridge_ports eth0 bridge_fd 0 bridge_hello 2 bridge_maxage 12 bridge_stop off
The /etc/network/interface file makes sure that when you reboot your server or restart your network, a device with the name br0 is created to replace the eth0 device. This device is meant to use the eth0 as its physical backend, as specified by the line bridge ports eth0. After creating the configuration file in this way, use sudo /etc/init.d/networking restart to restart our network. Your network is now ready to handle KVM virtual machines.
Configuring KVM on Ubuntu Server
Perform the following steps to set up your server for virtualisation (the procedure described here is supported on Ubuntu Server 8.04 and later):
- Install all software necessary (the KVM and QEMU packages) for KVM virtualisation. As root, use the command
apt-get install kvm qemu libvirt-bin.
- After installing these software packages, make sure that the kvm kernel module is loaded. Use lsmod to determine if this is the case (
lsmod | grep kvm) and if the module is not loaded, install it using
Next, you have to do some additional preparation. This preparation involves preparing the libvirt tools which you will use to create virtual machines. First, you need to add the user account you want to use for KVM management purposes to the libvirtd group. You can do this using the sudo adduser libvirtd command. Replace with the name of the user whose account you want to use.
And that's it! Your Ubuntu Server is now ready for the installation and operation of guest operating systems. The next section describes how to install Windows as a guest operating system.
Installing Windows as a guest operating system on KVM
Before installing Windows as your first guest operating system, you should ask yourself exactly what you want to do with the virtual machines. Is your server running in a data center and are you accomplishing all tasks (including installation of the virtual machines) remotely? If so, you can run it without a graphical user interface. But if you want to be able to manage the virtual machine(s) from the physical server itself, it's a good idea to install a GUI on the server. The procedure described below assumes that you do have some graphical interface that can be used to display the Windows installation interface. You can also use an SSH session with graphical support from a workstation to do this.
- To install Windows as a virtualised operating system, you first need to set up storage. The simplest way of trying out virtualisation is by using a disk image file. You can create this by using dd or qemu-img as in the following command that creates an 8GB disk image file with the name windows.img in the directory /var/lib/virt (make sure to create this directory before creating the image file):
dd if=/dev/zero of=/var/lib/virt/windows.img bs=1M count=8192
Now that you've created the disk image file, you can use the kvm command to install Windows. Make sure that the Windows installation CD is in the drive (or use an ISO file) and run the following command to start the installation, creating a Windows virtual machine with a total of 512 MB of RAM. This command uses the windows.img disk file that you've just created. Want to use an ISO file instead of a physical CD-ROM? Just replace /dev/cdrom by a complete path to the ISO file. The -no-acpi option used in this example isn't really required, but it may be useful if you are experiencing problems using ACPI:
kvm -m 512 -cdrom /dev/cdrom -boot d windows.img
Is the kvm command complaining about the lack of support for virtualisation on your CPU? You probably haven't switched virtualisation support on in your system BIOS yet. Restart your machine, enter the system BIOS and make sure that virtualisation support is on. Typically, you'll find this in the advanced section of your BIOS configuration and the option that you are looking for has a name like vm, vt or just virtualisation.
- A QEMU window opens in which you'll see the Windows installer loading. Complete the Windows installation from this interface.
- Once the installation of virtualised Windows is finished, you can run it in the same way you installed it. Use the kvm command again, but omit the option -boot d which ensures that you're booting from CD-ROM first. So, the following command runs an installed instance of Windows that is on the windows.img file:
kvm -m 512 -cdrom /dev/cdrom windows.img
You now have your virtualised Windows machine. That was easy, wasn't it? Next, we'll have a look at how to install Ubuntu as a guest on top of your Ubuntu Server virtualisation host.
Installing Ubuntu Server as a guest operating system on KVM
After reading the previous section about installing Windows as a guest operating system in KVM, you probably can already guess how to install an instance of virtualised Ubuntu. Fundamentally, there are no differences between installing Windows or Ubuntu: You create a virtual disk and install Ubuntu Server on that. Assuming that the installation CD is in an ISO image with the name ubuntu.iso, you can use the following procedure:
- Create the disk file:
dd if=/dev/zero of=/var/lib/virt/ubuntu.img bs=1M count=4096
Use the kvm command to start the installation from the Ubuntu ISO file:
kvm -m 256 -cdrom /isos/ubuntu.iso -boot d /var/lib/virt/ubuntu.img
If you are having problems installing Ubuntu or another Linux distribution as a guest operating system, the graphical menu that most boot loaders display nowadays before starting the installation could be the reason. Try a nongraphical installation program such as the Ubuntu netboot mini.iso file instead. This will help you install any Linux distribution without problems.
- Install Ubuntu Server as if it was a "normal" server.
- Boot the virtual Ubuntu Server you've just installed with the following command, and you're done:
kvm -m 256 ubuntu.img
In this article, you have learned how to set up Ubuntu Server as a KVM host and install some virtual machines in it. This helps you evaluating the next generation virtualisation solution that runs purely from a Linux kernel, but allows you to virtualise almost any operating system.