Virtualization With KVM On Ubuntu 9.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 ~/vm3/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/* ~/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 ~/vm3/vmbuilder.partition

root 8000
swap 2000
/var 10000

vi ~/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 ~/vm3/
vmbuilder kvm ubuntu --suite=karmic --flavour=virtual --arch=amd64 --mirror= -o --libvirt=qemu:///system --tmpfs=- --ip= --part=vmbuilder.partition --raw=/dev/vg0/vm3 --templates=mytemplates --user=administrator --name=Administrator --pass=howtoforge --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --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:

7 Comment(s)

Add comment


From: phubert at: 2009-12-30 07:25:23

The dual-use of acronyms is widespread, especially considering the different disciplines that make use of them. But, to have the same acronym found within the same discipline shouldn't be too surprising, either, considering that technology continues to advance, hence the potential application for new acronyms continues to increase.

 In this case, both acronyms (one hardware one software) both simply make sense. So we understand by the context! Languages are even worse, by the way!

From: at: 2009-12-23 11:45:05

KVM is short for Keyboard, Video and Mouse and is hardware device.

From: nutznboltz at: 2009-12-24 16:07:26

Yes, regrettably, the selection of "KVM" to mean "Kernel Virtual Machine" was done without regard to the prevalence of KVM's use as an acronym of "Keyboard, Video, Mouse".


From: Rudolf Leitgeb at: 2010-05-11 09:51:07

Under Ubuntu Lucid Lynx the vmbuilder command became a little bit more finicky with its command line arguments. It no longer supports the --tmpfs option and the --firstboot option only works without the = sign.


The command line must now look like this to work:

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


Note the absent = sign between --firstboot and and the absent option --tmpfs

From: Anonymous at: 2010-04-15 03:33:04

Thanks for the great tutorial, this was pretty freakin' easy to do. And the effect is soo much better than using headless virtualbox, like I used to do. :D

From: Anonymous at: 2009-12-18 20:36:02

Great writeup although I would be very interested in seeing this same write up utilizing Virt-manager instead.  I have been struggling to learn how to use virt-manager and would be interested in a more experienced users methods.  Thanks for a great write up though.

From: Marc P at: 2010-01-04 03:11:00

Great article, but I've a couple of corrections below:

  1. it's not necessary to define the VMs as per your  final steps (define /etc/libvirt/qemu/vm3.xml) - this is done automatically by vmbuilder. However you are correct in noting that if the xml is changed manually define must be done. 
  2. the --raw option does not work properly.  While it creates the VM correctly, that VM cannot boot.  (As verified in my own testing - reference also bug 260682 (this has  been around since at least 8.x and remains uncorrected.
  3. if you are using --raw anyway, there is currently a hard-coded max in the vmbuilder tool, such that any attempt to write to a volume greater than 5GB will fail. (See bug 303882)   I believe this is fixed in 8.10 but has not been merged into 9.10.  The final comment in this bug points out the file you need to modify to increase this limit.