There is a new version of this tutorial available for Ubuntu 12.10 (Quantal Quetzal).

Virtualization With KVM On Ubuntu 9.04 - Page 4

This tutorial exists for these OS versions

On this page

  1. 6 Creating An LVM-Based VM
  2. 7 Links

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.28 GB
  PE Size               4.00 MB
  Total PE              119112
  Alloc PE / Size       25856 / 101.00 GB
  Free  PE / Size       93256 / 364.28 GB
  VG UUID               K4p7q1-yXEr-bxKk-KD0c-MRfH-tMwk-i37oKx


... 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                KJsFce-MuLU-k60P-vK5q-eMaQ-T6Ga-DEC5Jz
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                100.00 GB
  Current LE             25600
  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                g6ejlz-RPbP-Qfnt-lqDp-HlNR-5lUF-uAFmPL
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                1.00 GB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1


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/vg0/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/

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 --bridge=br0

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/vm5 with a size of 20GB now:

lvcreate -L20G -n vm5 vg0

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:

aptitude 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/vg0/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/vg0/vm5'/>
      <target dev='hda' bus='ide'/>

You can now use virsh to manage the VM:

virsh --connect qemu:///system

Because we have modified the VM's XML file, we must run the define command first...

define /etc/libvirt/qemu/vm5.xml

... before we start the VM:

start vm5


Share this page:

1 Comment(s)