Virtualization With KVM On Ubuntu 8.10 - Page 4

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/vg01 with a size of approx. 454GB...


root@server1:~# vgdisplay
  --- Volume group ---
  VG Name               vg01
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               454.67 GB
  PE Size               4.00 MB
  Total PE              116396
  Alloc PE / Size       75000 / 292.97 GB
  Free  PE / Size       41396 / 161.70 GB
  VG UUID               q3xIiX-LDlm-IbMu-2PK2-WVoc-zHb8-8ibb32


... that contains the logical volume /dev/vg01/root with a size of approx. 292GB - the rest is not allocated and can be used for VMs:


root@server1:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg01/root
  VG Name                vg01
  LV UUID                f9W43z-RC1i-9JE8-CvOS-Qa89-0STq-q1M71e
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                292.97 GB
  Current LE             75000
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0


I will now create the virtual machine vm5 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/vg01/vm5) - I've tried this, and it gave back no errors, however, I was not able to boot the VM (start vm5 didn't show any errors either, but I've never been able to access the VM). Therefore, I will create vm5 as an image-based VM first and then convert it into an LVM-based VM.

mkdir -p ~/vm5/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/* ~/vm5/mytemplates/libvirt/

vi ~/vm5/mytemplates/libvirt/libvirtxml.tmpl

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

vi ~/vm5/vmbuilder.partition

root 8000
swap 2000
/var 10000

vi ~/vm5/

cd ~/vm5/
vmbuilder kvm ubuntu --suite=intrepid --flavour=virtual --arch=amd64 --mirror= -o --libvirt=qemu:///system --tmpfs=- --ip= --part=vmbuilder.partition --templates=mytemplates --user=administrator --name=Administrator --pass=howtoforge --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --mem=256 --hostname=vm5

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

lvcreate -L20G -n vm5 vg01

Don't create a file system in the new logical volume!

We will use the qemu-img command to convert the image to an LVM-based VM. The qemu-img command is part of the qemu package which we must install now:

apt-get install qemu

Then we go to the VM's ubuntu-kvm/ directory...

cd ~/vm5/ubuntu-kvm/

... and convert the image as follows:

qemu-img convert disk0.qcow2 -O raw /dev/vg01/vm5

Afterwards you can delete the disk image:

rm -f disk0.qcow2

Now we must open the VM's xml configuration file /etc/libvirt/qemu/vm5.xml...

vi /etc/libvirt/qemu/vm5.xml

... and change the following section...

    <disk type='file' device='disk'>
      <source file='/root/vm5/ubuntu-kvm/disk0.qcow2'/>
      <target dev='hda' bus='ide'/>

... so that it looks as follows:

    <disk type='file' device='disk'>
      <source file='/dev/vg01/vm5'/>
      <target dev='hda' bus='ide'/>

That's it! You can now use virsh to manage the VM.


7 Links

Share this page:

9 Comment(s)

Add comment


From: Jim Dennis at: 2009-05-16 21:45:47


The first problem I encountered while going through this HOWTO was in trying to follow the example for the configuration of br0 in the /etc/network/interfaces examples.

Like many readers my existing configuration was being managed by NetworkManager ... and is using DHCP.  Naturally my attempt to follow the example past this point (thinking I was creating a new virtual network and chosing an suitable RFC1918 block of static addresses to assign to that) left my system disconnected from the 'net.

It appears that all I needed to do was:

  1. Add an entry for eth0 identifying it as "inet manual" (presumably meaning: "managed by the bridge-utils").
  2. Replace all the static addressing directives in the example with  an "inet dhcp" entry for br0

The results look like:

auto br0
iface   br0     inet    dhcp
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off

iface   eth0    inet manual
        # managed by bridge-utils?

Perhaps adding an alternative for those on DHCP and a link to the best info we can find on bridge-utils would help.

 The best info I was able to find so far is at: LinuxFoundation: Net:Bridge. But I didn't have to dive far into it before I figured out what I was supposed to be doing by taking a closer look at the before and "after" examples.

 I hope the maintainer of this HOWTO will read this.



From: at: 2009-02-16 21:12:27

If virtualization does not show up after

egrep '(vmx|svm)' --color=always /proc/cpuinfo

 You may have to enable virtualization in the bios, I have found that this function is disabled by default in Dell servers and may also be in others.

From: Matt Mossholder at: 2009-03-06 02:40:35

Canonical recommends that you not use the following:

  • sudo su
  • sudo /bin/<insert shell here>
  • ...etc.

The recommended way of getting a root shell is:

sudo -i




From: Tom at: 2011-10-09 16:29:07

Nice article. Very good explanations.

There are a lot management tools will make KVM deployment much easier:

Since IBM and Redhat start to support KVM, many companies are deploying KVM cloud farms now.

From: Rainbof at: 2009-03-20 06:47:35

when you get

during creating the VM, following error appeared:

in file ~/vm1/mytemplates/libvirt/libvirtxml.tmpl must be this

<interface type='bridge'>
<source bridge='br0'/>

The error from vmbuilder really should have been "No <source> 'bridge' attribute[...]".

From: Anonymous at: 2009-03-06 13:12:35

If you get an error like this:

  /proc/misc: No entry for device-mapper found

while running vmbuilder, you need to install the device mapper kernel module:

  modprobe dm_mod


From: Anonymous at: 2009-03-07 11:20:31

Great site ! and good tips.

Please include what the user has to type if the chipset is a i386.


From: Seppe De Loore at: 2009-02-25 08:34:32

This is a great tutorial. Just had to iron out a small "bridge" error:

there seems to be an error in the vmbuiler documentation and the template ( mytemplates/libvirt/libvirtxml.tmpl) should be adapted to read:

<interface type='bridge'>
<source bridge='br0'/>

 The interface type should also be changed to 'bridge' instead of network.

Further installing a more complete PERL (I added perl-debug) will remove the PERL error messages the vmbuilder script spawns.

Anyway, I got my KVM server running. Thank you.

From: Anonymous at: 2010-02-10 11:01:12

You can omit the config file entirely and instead use the command line option --bridge=br0