Virtualization With KVM On A CentOS 6.0 Server - Page 5

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2011-08-25 17:55. ::

8 Creating An LVM-Based Guest From The Command Line

CentOS 6.0 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...

vgdisplay

[root@server1 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_server1
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               465.27 GiB
  PE Size               4.00 MiB
  Total PE              119109
  Alloc PE / Size       24480 / 95.62 GiB
  Free  PE / Size       94629 / 369.64 GiB
  VG UUID               jk2N5a-pQdV-I49Y-UCR9-A0Js-jEe0-0y6Ipg

[root@server1 ~]#

... that contains the logical volumes /dev/vg_server1/lv_root with a size of approx. 50GB, /dev/vg_server1/lv_home with a size of approx. 40GB, and /dev/vg_server1/lv_swap (about 6GB) - the rest is not allocated and can be used for KVM guests:

lvdisplay

[root@server1 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg_server1/lv_root
  VG Name                vg_server1
  LV UUID                il99XH-Q7QV-R16x-Q51X-GOzp-8uq1-aMUb82
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/vg_server1/lv_home
  VG Name                vg_server1
  LV UUID                2OqWO9-4P91-UDzK-K6i3-zgJz-vJWN-9OMXpi
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                40.00 GiB
  Current LE             10240
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

  --- Logical volume ---
  LV Name                /dev/vg_server1/lv_swap
  VG Name                vg_server1
  LV UUID                f5e90C-DMt8-896t-MmNo-S2FM-7hSd-gtVGjy
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                5.62 GiB
  Current LE             1440
  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.2.1-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.0 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/vg_server1/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 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'/>
    </disk>
[...]

... 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'/>
    </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

 

10 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by holykim (registered user) on Sat, 2012-03-24 01:59.
Same problem here. Additionally I found some incompatible issues between MegaRaid and kernel 2.6.32-220. It just randomly locks up, nothing in the logs, nothing on the console. I installed KVM on CentOS 5.8 for downgrading the kernel but no luck as it locks up again. I moved to Xen and all went successful. Both windows and linux work fine on Xen. Just my experience, I would recommend to use Xen instead of KVM.
 
 

 

 

Submitted by Dmitriy (not registered) on Fri, 2012-03-23 09:39.
Thanks! This article was very helpfull!
Submitted by Matt (not registered) on Tue, 2012-03-06 22:57.

Hi,

First off, thanks for this tutorial, was very helpful!

I just have one problem.  I did everything written here in the same exact details and starting from a fresh install.  The guests have access to the internet but not the host...  Do you know what small tweak may be missing or what step did I miss (if any) to make the internet access work on the host?

Thanks again!