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.


Share this page:

2 Comment(s)

Add comment


From: Seppe De Loore

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

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