Virtualization With KVM On Ubuntu 11.04 - 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...

vgdisplay

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 GiB
  PE Size               4.00 MiB
  Total PE              119112
  Alloc PE / Size       24079 / 94.06 GiB
  Free  PE / Size       95033 / 371.22 GiB
  VG UUID               xGcqq6-kH4t-C3et-n31G-xb2S-1XJp-H07Apl

root@server1:~#

... 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:

lvdisplay

root@server1:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/root
  VG Name                vg0
  LV UUID                LZrfMp-DbL5-rZEl-4m1l-tyxL-53bm-gCeiTg
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                93.13 GiB
  Current LE             23841
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           251:0

  --- Logical volume ---
  LV Name                /dev/vg0/swap_1
  VG Name                vg0
  LV UUID                Vlwyhq-cfeh-RthB-sM6A-Tt07-Ar5G-eFGjrV
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                952.00 MiB
  Current LE             238
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           251:1

root@server1:~#

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 /var/lib/libvirt/images/vm3/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/* /var/lib/libvirt/images/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 /var/lib/libvirt/images/vm3/vmbuilder.partition

root 8000
swap 2000
/var 10000

vi /var/lib/libvirt/images/vm3/boot.sh

# 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:

cd /var/lib/libvirt/images/vm3/
vmbuilder kvm ubuntu --suite=natty --flavour=virtual --arch=amd64 --mirror=http://de.archive.ubuntu.com/ubuntu -o --libvirt=qemu:///system --ip=192.168.0.103 --gw=192.168.0.1 --part=vmbuilder.partition --raw=/dev/mapper/vg0-vm3 --templates=mytemplates --user=administrator --name=Administrator --pass=howtoforge --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --firstboot=/var/lib/libvirt/images/vm3/boot.sh --mem=256 --hostname=vm3 --bridge=br0

Please note that I use --raw=/dev/mapper/vg0-vm3 instead of --raw=/dev/vg0/vm3 switch - both /dev/vg0/vm3 and /dev/mapper/vg0-vm3 are symlinks that point to the same logical volume - /dev/dm-2 in my case - but when I used --raw=/dev/vg0/vm3 the vmbuilder process failed with the following error message...

2011-05-05 15:53:24,056 INFO    : Partition at beginning of disk - reserving first cylinder
2011-05-05 15:53:24,764 INFO    : device-mapper: deps ioctl failed: No such device or address
2011-05-05 15:53:24,765 INFO    : Cleaning up
2011-05-05 15:53:24,765 ERROR   : Process (['parted', '--script', '--', '/dev/vg0/vm3', 'mkpart', 'primary', 'ext2', '63s', '7999']) returned -11. stdout: , stderr: device-mapper: deps ioctl failed: No such device or address

Traceback (most recent call last):
  File "/usr/bin/vmbuilder", line 24, in <module>
    cli.main()
  File "/usr/lib/python2.7/dist-packages/VMBuilder/contrib/cli.py", line 223, in main
    hypervisor.install_os()
  File "/usr/lib/python2.7/dist-packages/VMBuilder/hypervisor.py", line 65, in install_os
    self.call_hooks('mount_partitions', self.chroot_dir)
  File "/usr/lib/python2.7/dist-packages/VMBuilder/distro.py", line 67, in call_hooks
    call_hooks(self, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/VMBuilder/util.py", line 165, in call_hooks
    getattr(context, func, log_no_such_method)(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/VMBuilder/hypervisor.py", line 88, in mount_partitions
    disk.partition()
  File "/usr/lib/python2.7/dist-packages/VMBuilder/disk.py", line 117, in partition
    part.create(self)
  File "/usr/lib/python2.7/dist-packages/VMBuilder/disk.py", line 295, in create
    run_cmd('parted', '--script', '--', disk.filename, 'mkpart', 'primary', self.parted_fstype(), partition_start, self.end)
  File "/usr/lib/python2.7/dist-packages/VMBuilder/util.py", line 120, in run_cmd
    raise VMBuilderException, "Process (%s) returned %d. stdout: %s, stderr: %s" % (args.__repr__(), status, mystdout.buf, mystderr.buf)
VMBuilder.exception.VMBuilderException: Process (['parted', '--script', '--', '/dev/vg0/vm3', 'mkpart', 'primary', 'ext2', '63s', '7999']) returned -11. stdout: , stderr: device-mapper: deps ioctl failed: No such device or address

root@server1:/var/lib/libvirt/images/vm3#

... while it succeeded with --raw=/dev/mapper/vg0-vm3 (I tested this multiple times). This looks like a bug in vmbuilder or parted to me.

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

 

Share this page:

1 Comment(s)

Add comment

Comments

From: Anonymous at: 2011-11-02 12:42:38

Great tutorial; just one question. I don't get any errors when running the vmbuilder command, but when attempting to "define" a vm the error says there is no file or directory. It's all there in the /var/... directories, but the .xml profile is not being created in the /etc/.. directory. (I've searched the entire file system). Do you have any suggestions on where else to troubleshoot?  I'm using Ubuntu 11.04 on an 64-bit AMD dual core with KVM. The same hardware supported VMs fine under Ubuntu 9.04.