Semi-Quick KVM Virtualization using libvirt

A “Semi-Quick” procedure to have a working KVM environment and a fresh VM Guest working on it.

I call it “Semi” because the entire procedure takes a bit of time, but still less than a life!


Time to check if the Hardware supports Virtualization:

To run KVM, we need a processor that supports hardware virtualization.
To see if the processor supports one of these, we can review the output from this command:

egrep -c '(vmx|svm)' /proc/cpuinfo

0 means that the CPU doesn’t support hardware virtualization.
1 (or more) means that the CPU support hardware virtualization (but we still need to make sure that virtualization is enabled in the BIOS).

Running a 64 bit kernel on the host operating system is recommended but not required.
To serve more than 2GB of RAM for VMs, we must use a 64-bit kernel. On a 32-bit kernel install, we’ll be limited to 2GB RAM at maximum for a given VM.
Also, a 64-bit system can host both 32-bit and 64-bit guests. A 32-bit system can only host 32-bit guests.

To see if the processor is 64-bit, you can run this command:

egrep -c ' lm ' /proc/cpuinfo

If 0 is printed, it means that the CPU is not 64-bit.
If 1 or higher, it is. Note: lm stands for Long Mode which equates to a 64-bit CPU.


Time to install the Virtual Machine Host:

To setup my dedicated Ubuntu KVM Hosts, I always perform a fresh Ubuntu installation with package selection set to “Virtual Machine Host” because this option installs everything you need to have a fully functional “VM Host System”.
To have remote administration facility via SSH, I also always select “OpenSSH Server” in the menu.

I also install the “virtinst” package, a collection of programs useful to create and clone Virtual Machines. To do that, I simply use apt:

apt-get install virtinst


Time to configure the network.

In this sample environment, I have only one phisical GigabitEthernet interface available on the “Virtual Machine Host”, so I decided to create a “Network Bridge” to connect all “Virtual Interfaces” dynamically created during the boot process of “Virtual Machine Guests” inside the “Virtual Machine Host”.
In a production environment I use separate phisical interfaces for Host and Guest machines.

First of all the package “bridge-utils” is needed in this kind of setup, so (if not present in the system) I need to install it:

apt-get install bridge-utils

Now I need to configure networking, bridging my eth0 interface to br0, the one I will use to bridge “Virtual Interfaces” to.
To perform these tasks I edit the /etc/network/interfaces file, adding the following configuration directives and commenting-out all configuration related to eth0:

auto br0
 iface br0 inet static
 bridge_ports eth0
 bridge_fd 9
 bridge_stp on
 bridge_hello 2
 bridge_maxage 12

NOTE: IPv4 addresses need to be referred to network scenario, IPv6 can (must) be also configured, this is not covered on this article, bridge_* options has to be referred to network scenario.

Since I am using “UFW” (Uncomplicated Firewall, in Ubuntu) to protect the “Virtual Machine Host” and to filter VNC Console IP Access, I need to exclude packet processing trough the bridge interface. To do that I simply insert this code to the end of /etc/ufw/sysctl.conf file:


Restart networking or (better) perform a machine reboot to ensure all configuration details are properly loaded.

shutdown -r now

If everything is ok, all network services will bring-up on the “new” br0 interfaces.


Time to install the first “Guest Virtual Machine”.

For convenience, since I assume to install other Guest Machines of the same type, I download the ISO of the OS Installation locally.
(In this example I will use Ubuntu 11.04 Server (amd64), the same OS used for the VM Host)

I create a directory named “VMs” and I download the ISO file from an Ubuntu mirror, in this directory I will also store the disk-image files of my VM Guests.

mkdir /VMs
wget http://[choose_ubuntu_mirror]/ubuntu-11.04-server-amd64.iso

When the download finishes, I immediately create the first VM Guest, by using the “virt-install” command:

virt-install \
  -n VM01 \
  -r 512 \
  -f /VMs/VM01.img \
  -s 5 \
  -c /VMs/ubuntu-11.04-server-amd64.iso \
  --network=bridge:br0 \
  --accelerate \
  --os-type=linux \
  --vnc \
  --vnclisten= \
  --vncport=5901 \

This is a sample working option-set, allocating 512Mbytes of RAM and 5Gbytes HDD.
More options are available and need to be evaluated by reading virt-install man page.

I will be able (after the script confirm it) to connect to the Virtual Machine Console via VNC using the address of the “Virtual Machine Host” in port 5901 (as I specified in the command options above).

Now I only need to complete a new OS Installation, at the end of the configuration I will have a “new” machine available in the network, my first “Virtual Machine”.

Comments are closed.