Create Virtual Machine/OS on Ubuntu 12.04 with KVM

To manage multiple OS, you might have faced the need of Virtual Machines setup. If you are unaware of virtualization you can find more information here.

We will use kvm, vmbuilder and libvirt for making virtual image.

  • kvm – Full virtualization solution for Linux on x86 (64-bit included) hardware containing virtualization extensions (Intel VT or AMD-V).
  • vmbuilder – It is a Python-based software package for creating VM images of free software GNU/Linux-based operating systems
  • libvirt – It is toolkit to interact with the virtualization capabilities of recent versions of Linux

Build Virtual OS using KVM:

These instructions are followed on Ubuntu 12.04 LTS and debian based OS.
So, lets follow below instructions and commands step-by-step to create your Virtual Machines.

Step 1: Check Virtualization Support

First of all check for CPU supports hardware virtualization.

root@rtcamp1~# egrep '(vmx|svm)' --color=always /proc/cpuinfo

Output of this command should display something like this:

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushmmx fxsr sse sse2 ht syscall nx
mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good nopl extd_apicidpni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy
3dnowprefetch lbrv
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushmmx fxsr sse sse2 ht syscall nx mmxext
fxsr_opt rdtscp lm 3dnowext 3dnow rep_good nopl extd_apicidpni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy 3dnowprefetch
lbrv

If nothing comes , then you must stop from here as your hardware configuration do not support virtualization.
dd vmbuilder and kvm packages

Step 2: Install vmbuilder and kvm

Now we are going to install kvm and vmbuilder

root@rtcamp1~# apt-get update
root@rtcamp1~# apt-get install kvm ubuntu-virt-server libvirt-bin python-vm-builder kvm-ipxe

After that check if KVM has been successfully installed or not

root@rtcamp1~# virsh -c qemu:///system list

Output should display something like this:

root@rtcamp1:~# virsh -c qemu:///system list
Id Name State
----------------------------------

If it is showing any error then something is going wrong.
(check if, kvm is enabled from your BIOS or not )

root@rtcamp1~# kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

(Output should look like this otherwise you would need to enable virtual support from your BIOS settings)

Step 3: Create a Network Bridge

We need network bridge on our server .

root@rtcamp1~# apt-get install bridge-utils

Configure network interface for bridge.

root@rtcamp1~# vi /etc/network/interface

modify it as below example:

auto eth0
iface eth0 inet manual
auto br0
iface br0 inet static
address 192.168.0.1
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
dns-nameservers 8.8.8.8 8.8.4.4
bridge_ports eth0

Restart network service to activate bridge.
Check with ifconfig you should see br0 interface with ipaddress 192.168.0.1
( eth0 will not show you the ipaddress)

Reboot the system.

Step 4: Create Virtual Machine

We will create ubuntu 12.04 server (amd64 arch)

Example command.

root@rtcamp1~# mkdir virtualmachines
root@rtcamp1~# cd virtualmachines

Below is the vmbuilder command. Run vmbuilder e.g. as follows:

root@rtcamp1~# vmbuilder kvm ubuntu 
--suite=precise 
--flavour=server 
--mem 1024 
--cpus 1 
--rootsize '4096' 
--swapsize '1024' 
--kernel-flavour 'generic' 
--hostname 'ubuntu' 
--mirror 'http://archive.ubuntu.com/ubuntu' 
--components 'main,universe' 
--addpkg 'openssh-server' 
--name 'rtvm' 
--user 'rtvm1' 
--pass 'rtvm123' 
--ip '192.168.0.124' 
--mask '255.255.255.0' 
--net '192.168.0.0' 
--bcast '192.168.0.255' 
--gw '192.168.0.1' 
--dns '8.8.8.8' 
--bridge 'br0' 
--libvirt 'qemu:///system'

Important parameters and its description:

suite: Version of Ubuntu to install
flavour: The “flavour” of kernel to use in the VM. Either “virtual” or “server”.
mem: Size of random access memory in MB.
cpus: Number of CPUs to assign to VM.
rootsize: Size of root filesystem in MB of VM.
swapsize: Size of swap in MB
mirror: source url of ubuntu default archive.ubuntu.com
hostname: Hostname of VM.
ip: IP address of VM.
mask: Netmask of VM.
net: Network of VM.
bcast: Broadcast address of VM.
gw: Gateway of VM.
dns: DNS servers for VM.
addpkg: APT packages to install in the VM. I will suggest to add openssh.
user and pass: username and password ( don’t forget )
dest: Directory where VM image will create.
You can get more information about vmbuilder parameters here.

Note: The IP’s and gateways used above are of my own LAN, you shall change it as per your own configuration.

After Creating Virtual Server check the xml file

root@rtcamp1~# ls -l

You can find ubuntu-kvm folder

root@rtcamp1~# ls -l /etc/libvirt/qemu/

You can find ubuntu.xml created

Step 5: Manage Virtual Machines

Check with the virsh ( Virtual Shell)

root@rtcamp1~# virsh --connect qemu:///system

It will drop you in virsh prompt like this – virsh #

virsh # list

It will show you the running virtual machines list

virsh # list --all

It will show you all the virtual machines list

virsh # list --all
Id Name State
----------------------------------
- ubuntu shut off

Starting a Virtual Machine

Start virtual machine

virsh # start ubuntu

Shutdown virtual machine

 virsh # shutdown ubuntu

Deleting a Virtual Machine

To delete a virtual machine, first terminate it (if running), and then undefine it:

 virsh # destroy ubuntu
 virsh # undefine ubuntu

To get more help with virsh command line type help, it will print all the possible commands.

virsh # help

Hope you find this tutorial helpful. Do drop in your comments any questions or suggestions about the post.

Some helpful links :

2 Comments

sandeep September 12, 2012

Hi,
After following the steps you provided installation completed successfully.
While starting I got following error:
virsh # start ubuntu
error: Failed to start domain ubuntu
error: Cannot get interface MTU on ‘br0’: No such device

list –all is showing as you shown
virsh # list –all
Id Name State
———————————-
– ubuntu shut off

Can you please help me to resolve this problem.

manarius October 22, 2012

i had the same problem,

do a
ifconfig
in the console and look for the correct name of br0
in my case it was virbr0

now i am stuck at the point where windows needs cd/dvd drivers for my nonexistent cd/dvd drive 😀

have fun
manarius