Virtualization With KVM On A Debian Squeeze Server - Page 5

8 Creating An LVM-Based Guest From The Command Line

Debian Squeeze KVM Host:

LVM-based guests have some advantages over image-based guests. They are not as heavy on hard disk IO, and they are easier to back up (using LVM snapshots).

To use LVM-based guests, 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

[email protected]:~# 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               UfMJrb-eu18-NlYw-PGx1-wM5f-4d7S-x10hpw

[email protected]:~#

... that contains the logical volume /dev/vg0/root with a size of approx. 93GB and the logical volume /dev/vg0/swap_1 (about 1GB) - the rest is not allocated and can be used for KVM guests:

lvdisplay

[email protected]:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/root
  VG Name                vg0
  LV UUID                ydV63n-e57f-T3vu-xcBD-OZ7u-gXPf-QoGxVk
  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           254:0

  --- Logical volume ---
  LV Name                /dev/vg0/swap_1
  VG Name                vg0
  LV UUID                0H0BAx-VeBU-pH4T-CpnD-7oFI-6ulP-krl66i
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                952.00 MiB
  Current LE             238
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

[email protected]:~#

I will now create the virtual machine vm12 as an LVM-based guest. I want vm12 to have 20GB of disk space, so I create the logical volume /dev/vg0/vm12 with a size of 20GB:

lvcreate -L20G -n vm12 vg0

Afterwards, we use the virt-install command again to create the guest:

virt-install --connect qemu:///system -n vm12 -r 512 --vcpus=2 --disk path=/dev/vg0/vm12 -c /var/lib/libvirt/images/debian-6.0.0-amd64-netinst.iso --vnc --noautoconsole --os-type linux --os-variant debiansqueeze --accelerate --network=bridge:br0 --hvm

Please note that instead of --disk path=/var/lib/libvirt/images/vm12.img,size=20 I use --disk path=/dev/vg0/vm12, and I don't need to define the disk space anymore because the disk space is defined by the size of the logical volume vm12 (20GB).

Now follow chapter 5 to install that guest.

 

9 Converting Image-Based Guests To LVM-Based Guests

Debian Squeeze KVM Host:

No let's assume we want to convert our image-based guest vm10 into an LVM-based guest. This is how we do it:

First make sure the guest is stopped:

virsh --connect qemu:///system

shutdown vm10

quit

Then create a logical volume (e.g. /dev/vg0/vm10) that has the same size as the image file - the image has 12GB, so the logical volume must have 12GB of size as well:

lvcreate -L12G -n vm10 vg0

Now there are two ways of converting the image:

  1. The first one is as follows:

    qemu-img convert /var/lib/libvirt/images/vm10.img -O host_device /dev/vg0/vm10

  2. OR you do it like this:

    qemu-img convert /var/lib/libvirt/images/vm10.img -O raw /var/lib/libvirt/images/vm10.raw
    dd if=/var/lib/libvirt/images/vm10.raw of=/dev/vg0/vm10
    rm -f /var/lib/libvirt/images/vm10.raw

(The command

qemu-img convert /var/lib/libvirt/images/vm10.img -O raw /dev/vg0/vm10

does not work; you will get the following error message:

qemu-img: Error while formatting '/dev/vg0/vm10'

)

Afterwards you can delete the disk image:

rm -f /var/lib/libvirt/images/vm10.img

Now we must open the guest's xml configuration file /etc/libvirt/qemu/vm10.xml...

vi /etc/libvirt/qemu/vm10.xml

... and change the following section...

[...]
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/vm10.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
[...]

... so that it looks as follows:

[...]
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw'/>
      <source dev='/dev/vg0/vm10'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
[...]

Afterwards we must redefine the guest:

virsh --connect qemu:///system

define /etc/libvirt/qemu/vm10.xml

Still on the virsh shell, we can start the guest...

start vm10

... and leave the virsh shell:

quit

 

Falko Timme

About Falko Timme

Falko Timme is an experienced Linux administrator and founder of Timme Hosting, a leading nginx business hosting company in Germany. He is one of the most active authors on HowtoForge since 2005 and one of the core developers of ISPConfig since 2000. He has also contributed to the O'Reilly book "Linux System Administration".

Share this page:

Suggested articles

0 Comment(s)

Add comment

Comments

By: Falko Timme