HowtoForge

Virtualization With KVM On An OpenSUSE 12.2 Server - Page 5

8 Creating An LVM-Based Guest

OpenSUSE 12.2 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/system with a size of approx. 465GB...

vgdisplay

server1:~ # vgdisplay
  --- Volume group ---
  VG Name               system
  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.61 GiB
  PE Size               4.00 MiB
  Total PE              119195
  Alloc PE / Size       27136 / 106.00 GiB
  Free  PE / Size       92059 / 359.61 GiB
  VG UUID               fKvgEc-efcR-yIfz-E35O-GSou-xdtT-etyJik

server1:~ #

... that contains the logical volume /dev/system/root with a size of approx. 100GB and the logical volume /dev/system/swap (about 6GB) - the rest is not allocated and can be used for KVM guests:

lvdisplay

server1:~ # lvdisplay
  --- Logical volume ---
  LV Name                /dev/system/root
  VG Name                system
  LV UUID                0yR2UP-FSrM-uu6p-Xy11-dRFb-omZV-5qZTX6
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                100.00 GiB
  Current LE             25600
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/system/swap
  VG Name                system
  LV UUID                XkdRHz-b5V0-Zyc8-nGYw-ljqF-WI49-dGoMxt
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                6.00 GiB
  Current LE             1536
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

server1:~ #

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

lvcreate -L20G -n vm12 system 

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

OpenSUSE 12.2 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/system/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 system 

Now convert the disk image:

qemu-img convert /var/lib/libvirt/images/vm10.img -O raw /dev/system/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='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
[...]

... so that it looks as follows:

[...]
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw'/>
      <source dev='/dev/system/vm10'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </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

 

Virtualization With KVM On An OpenSUSE 12.2 Server - Page 5