Virtualization With KVM On A CentOS 6.3 Server - Page 5

8 Creating An LVM-Based Guest From The Command Line

CentOS 6.3 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/vg_server1 with a size of approx. 465GB...


[root@server1 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_server1
  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       26500 / 103.52 GiB
  Free  PE / Size       92612 / 361.77 GiB
  VG UUID               ZXWn5k-oVkA-ibuC-ip8x-edLx-3DMw-UrYMXg

[root@server1 ~]#

... that contains the logical volumes /dev/vg_server1/LogVol00 with a size of approx. 100GB and /dev/vg_server1/LogVol01 (about 6GB) - the rest is not allocated and can be used for KVM guests:


[root@server1 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_server1/LogVol01
  LV Name                LogVol01
  VG Name                vg_server1
  LV UUID                uUpXY3-yGfZ-X6bc-3D1u-gB4E-CfKE-vDcNfw
  LV Write Access        read/write
  LV Creation host, time, 2012-08-21 13:45:32 +0200
  LV Status              available
  # open                 1
  LV Size                5.86 GiB
  Current LE             1500
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/vg_server1/LogVol00
  LV Name                LogVol00
  VG Name                vg_server1
  LV UUID                FN1404-Aczo-9dfA-CnNI-IKn0-L2hW-Aix0rV
  LV Write Access        read/write
  LV Creation host, time, 2012-08-21 13:45:33 +0200
  LV Status              available
  # open                 1
  LV Size                97.66 GiB
  Current LE             25000
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

[root@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/vg_server1/vm12 with a size of 20GB:

lvcreate -L20G -n vm12 vg_server1

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/vg_server1/vm12 -c /var/lib/libvirt/images/debian-6.0.5-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/vg_server1/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

CentOS 6.3 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


Then create a logical volume (e.g. /dev/vg_server1/vm10) that has the same size as the image file. To find out the size of the image, type in ...

ls -l /var/lib/libvirt/images/

[root@server1 ~]# ls -l /var/lib/libvirt/images/
total 13819392
-rw-r--r-- 1 qemu qemu   177209344 May 12 22:41 debian-6.0.5-amd64-netinst.iso
-rw------- 1 root root 12884901888 Aug 21 15:37 vm10.img
-rw------- 1 qemu qemu 12884901888 Aug 21 15:51 vm11.img
[root@server1 ~]#

As you see, vm10.img has a size of exactly 12884901888 bytes. To create a logical volume of exactly the same size, we must specify -L 12884901888b (please don't forget the b at the end which tells lvcreate to use bytes - otherwise it would assume megabytes):

lvcreate -L 12884901888b -n vm10 vg_server1

Now we convert the image:

qemu-img convert /var/lib/libvirt/images/vm10.img -O raw /dev/vg_server1/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' cache='none'/>
      <source file='/var/lib/libvirt/images/vm10.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

... so that it looks as follows:

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

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:



10 Links

Share this page:

4 Comment(s)

Add comment


From: Dim at: 2012-08-30 19:00:45

You are missing DELAY=0 in the bridge ifcfg script. Without it an incoming migrating VM will lose several seconds of network connectivity

From: at: 2012-11-06 19:14:59

I had the following error occur and was able to fix it thanks to another site.
# virsh -c qemu:///system list
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
error: failed to connect to the hypervisor
yum -y install avahi
/etc/init.d/messagebus restart
/etc/init.d/avahi-daemon restart
/etc/init.d/libvirtd restart
/sbin/chkconfig messagebus on
/sbin/chkconfig avahi-daemon on

From: Anonymous at: 2012-11-20 05:38:08

Thanks for the great write-up. Makes for an impressive virtual lab for home or work use. 

From: Anonymous at: 2012-11-22 04:17:22

You don't have to worry about SELinux. Leave it enabled. I have centos 6.2 running with a similar sort of setup.

On the host, if you leave SELinux on, it is better. Each virtual machine will run under it's own svirt process meaning that the virtual machine is essentially block from reaching the host. This is important because it makes it much more difficult for a compromised virtual machine to act as an attack vector for the virtual host. It essentially seperates everything nicely.

See redhat admin docs, they are applicable to CentOS as well.

The documentation is not the most detailed I have ever seen, you won't get hand holding but they are good for nudging you in the right direction.