Virtualization With KVM On Ubuntu 12.04 LTS - 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.29 GiB
  PE Size               4.00 MiB
  Total PE              119115
  Alloc PE / Size       24079 / 94.06 GiB
  Free  PE / Size       95036 / 371.23 GiB
  VG UUID               PRenhH-0MvN-wXCL-nl4i-IfsQ-J6fc-2raYLD

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                dwnORf-yG3U-x1ZC-Bet1-TOoc-q1Dd-KZnbtw
  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           252:0

  --- Logical volume ---
  LV Name                /dev/vg0/swap_1
  VG Name                vg0
  LV UUID                ZdPKO6-sZrr-tIRb-PPcl-aWBj-QAUU-fnYUuP
  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           252:1

root@server1:~#

I will now create the virtual machine vm5 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/vm5) - I've tried this, and it gave back no errors, however, I was not able to boot the VM (start vm5 didn't show any errors either, but I've never been able to access the VM). Therefore, I will create vm5 as an image-based VM first and then convert it into an LVM-based VM.

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

root 8000
swap 2000
/var 10000

vi /var/lib/libvirt/images/vm5/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

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

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

lvcreate -L20G -n vm5 vg0

Don't create a file system in the new logical volume!

We will use the qemu-img command to convert the image to an LVM-based VM.

Now we go to the VM's ubuntu-kvm/ directory...

cd /var/lib/libvirt/images/vm5/ubuntu-kvm/

... and find out how our image is named:

ls -l

root@server1:/var/lib/libvirt/images/vm5/ubuntu-kvm# ls -l
total 592140
-rw-r--r-- 1 root root 606470144 May 21 14:06 tmpesHsUI.qcow2
root@server1:/var/lib/libvirt/images/vm5/ubuntu-kvm#

Now that we know the name of our image (tmpN27tbO.qcow2), we can convert it as follows:

qemu-img convert tmpesHsUI.qcow2 -O raw /dev/vg0/vm5

Afterwards you can delete the disk image:

rm -f tmpesHsUI.qcow2

Now we must modify the VM's configuration...

virsh edit vm5

... and change the following section...

[...]
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/vm5/ubuntu-kvm/tmpesHsUI.qcow2'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' unit='0'/>
    </disk>
[...]

... so that it looks as follows:

[...]
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/dev/vg0/vm5'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' unit='0'/>
    </disk>
[...]

You can now use virsh to manage the VM:

virsh --connect qemu:///system

Because we have modified the VM's XML file, we must run the define command first...

define /etc/libvirt/qemu/vm5.xml

... before we start the VM:

start vm5

 

7 Links

Share this page:

9 Comment(s)

Add comment

Comments

From: Anonymous at: 2012-06-06 02:34:42

'sudo -i' is the preferred way to gain permanent root via sudo.

From: rpjday at: 2012-06-11 23:33:49

It seems like installing libvirt-bin automatically adds the invoking userid to that group.  At least *something* seems to add you to that group.

 You do, however, still have to add yourself to the "kvm" group, so that part is unchanged.

From: Lisandro at: 2012-06-13 23:12:26

Its important to check that CPU virtualization is enabled with this command:

root@localhot: kvm-ok

 Or you will get this error:

'/var/run/libvirt/libvirt-sock': "No such file" 

From: Anonymous at: 2012-06-30 22:04:30

i used the brctl commands to add the bridge as the bridge_xxxx didn't work for me

further details @ https://help.ubuntu.com/community/NetworkConnectionBridge

 add these in /etc/network/interfaces after bridge address/subnet/dns data etc.

pre-up ip link set eth0 up
pre-up brctl addbr br0
pre-up brctl addif br0 eth0
pre-up ip addr flush dev eth0
post-down ip link set eth0 down
post-down ip link set br0 down
post-down brctl delif br0 eth0
post-down brctl delbr br0
then restart the network interface

 sudo /etc/init.d/networking restart


 thanks for the help -  i'm trying qemu-kvm as xen 4.1 doesn't seem to want to boot with 12.04

From: Pubudu Kodikara at: 2012-09-13 13:48:56

Still bridge networking doesn't work :( The guest machines can't connect to the network. Please help!

From: frhamard at: 2012-08-27 09:25:56

Thank you for this great and clear article. Do you think you can add a part on how to connect to the host with virt-manager? 

 

From: Anonymous at: 2014-05-29 14:41:24

how do I install centos as a virtual machine?

From: Anonymous at: 2012-12-14 02:54:54

It might be better to use the virsh edit domain command to edit the configuration file. It picks up the changes automatically.

From: Luis at: 2013-05-27 04:00:06

I would like to thank you for this walkthrough. It was of great help.

 Thanks!