Virtualization With KVM On Ubuntu 10.10 - Page 3

6 Creating An LVM-Based VM

LVM-based VMs have some advantages over image-based VMs. They are not as heavy on hard disk IO, and they are easier to back up (using LVM snapshots).

To use LVM-based VMs, you need a volume group that has some free space that is not allocated to any logical volume. In this example, I use the volume group /dev/vg0 with a size of approx. 465GB...


root@server1:~# vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               465.29 GB
  PE Size               4.00 MB
  Total PE              119114
  Alloc PE / Size       24079 / 94.06 GB
  Free  PE / Size       95035 / 371.23 GB
  VG UUID               hUDyB2-hGR5-T7gI-wxt6-p4Om-PT6l-Bgbi85


... that contains the logical volumes /dev/vg0/root with a size of approx. 100GB and /dev/vg0/swap_1 with a size of 1GB - the rest is not allocated and can be used for VMs:


root@server1:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/root
  VG Name                vg0
  LV UUID                5PHWtQ-5XuQ-jgvu-uFrJ-f889-w46a-cIRFcb
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                93.13 GB
  Current LE             23841
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Name                /dev/vg0/swap_1
  VG Name                vg0
  LV UUID                N25s1p-AQWJ-X2WH-FAyA-xlS6-ettD-55ZHE8
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                952.00 MB
  Current LE             238
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1


I will now create the virtual machine vm3 as an LVM-based VM. We can use the vmbuilder command again. vmbuilder knows the --raw option which allows to write the VM to a block device (e.g. /dev/vg0/vm3).

mkdir -p /vm/vm3/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/* /vm/vm3/mytemplates/libvirt/

Make sure that you create all partitions in just one image file, so don't use --- in the vmbuilder.partition file:

vi /vm/vm3/vmbuilder.partition

root 8000
swap 2000
/var 10000

vi /vm/vm3/

# This script will run the first time the virtual machine boots
# It is ran as root.

# Expire the user account
passwd -e administrator

# Install openssh-server
apt-get update
apt-get install -qqy --force-yes openssh-server

As you see from the vmbuilder.partition file, the VM will use a max. of 20GB, so we create a logical volume called /dev/vg0/vm3 with a size of 20GB now:

lvcreate -L20G -n vm3 vg0

We can now create the new VM as follows (please note the --raw=/dev/vg0/vm3 switch!):

cd /vm/vm3/
vmbuilder kvm ubuntu --suite=lucid --flavour=virtual --arch=amd64 --mirror= -o --libvirt=qemu:///system --ip= --gw= --part=vmbuilder.partition --raw=/dev/vg0/vm3 --templates=mytemplates --user=administrator --name=Administrator --pass=howtoforge --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --firstboot=/vm/vm3/ --mem=256 --hostname=vm3 --bridge=br0

You can now use virsh to manage the VM:

virsh --connect qemu:///system

Run the define command first...

define /etc/libvirt/qemu/vm3.xml

... before you start the VM:

start vm3


7 Links

Share this page:

4 Comment(s)

Add comment


From: at: 2011-01-10 11:59:14

does this workin on 8.04 lts?

From: at: 2011-01-17 06:30:50

Hi, As usual best tutorial .

Can I use this tutorial to setup KVM based virtualization on Ubuntu 10.04 LTS version.

Which Virtulaization best for manage on Ubuntu 10.04 LTS Server ?

From: Anonymous at: 2010-12-24 17:01:34

It may be obvious for most linux users, but i stumbled in the vmbuilder.partition file. Make sure to use a space between mount point and size parameters. I've initially used 'tab', which gave me the following error which was not too descriptive, it took a while for me to figure it out :

Traceback (most recent call last):
  File "/usr/bin/vmbuilder", line 24, in <module>
  File "/usr/lib/python2.6/dist-packages/VMBuilder/contrib/", line 116, in main
  File "/usr/lib/python2.6/dist-packages/VMBuilder/contrib/", line 251, in set_disk_layout
    logging.debug("part: %s, size: %d" % (pair[0], int(pair[1])))
IndexError: list index out of range

From: Anonymous at: 2011-02-01 18:31:20

When installing guest to LV with --raw option=/dev/volume_group/logical_volume_name, vmbuilder still creates the vm as a qcow2 file in the current directory.  The file will be name something along tmpXXXXX.qcow2.

You can also see the process in real time when calling the command : Creating disk image: "/tmp/tmpsfBm_s

 There is a confirmed bug apparently there :