Virtualization With KVM On Ubuntu 8.10 - Page 4
On this page
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/vg01 with a size of approx. 454GB...
vgdisplay
root@server1:~# vgdisplay
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 454.67 GB
PE Size 4.00 MB
Total PE 116396
Alloc PE / Size 75000 / 292.97 GB
Free PE / Size 41396 / 161.70 GB
VG UUID q3xIiX-LDlm-IbMu-2PK2-WVoc-zHb8-8ibb32
root@server1:~#
... that contains the logical volume /dev/vg01/root with a size of approx. 292GB - the rest is not allocated and can be used for VMs:
lvdisplay
root@server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/vg01/root
VG Name vg01
LV UUID f9W43z-RC1i-9JE8-CvOS-Qa89-0STq-q1M71e
LV Write Access read/write
LV Status available
# open 1
LV Size 292.97 GB
Current LE 75000
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0
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/vg01/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 ~/vm5/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/* ~/vm5/mytemplates/libvirt/
vi ~/vm5/mytemplates/libvirt/libvirtxml.tmpl
Make sure that you create all partitions in just one image file, so don't use --- in the vmbuilder.partition file:
vi ~/vm5/vmbuilder.partition
root 8000 swap 2000 /var 10000 |
vi ~/vm5/boot.sh
cd ~/vm5/
vmbuilder kvm ubuntu --suite=intrepid --flavour=virtual --arch=amd64 --mirror=http://192.168.0.100:9999/ubuntu -o --libvirt=qemu:///system --tmpfs=- --ip=192.168.0.105 --part=vmbuilder.partition --templates=mytemplates --user=administrator --name=Administrator --pass=howtoforge --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --firstboot=boot.sh --mem=256 --hostname=vm5
As you see from the vmbuilder.partition file, the VM will use a max. of 20GB, so we create a logical volume called /dev/vg01/vm5 with a size of 20GB now:
lvcreate -L20G -n vm5 vg01
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. The qemu-img command is part of the qemu package which we must install now:
apt-get install qemu
Then we go to the VM's ubuntu-kvm/ directory...
cd ~/vm5/ubuntu-kvm/
... and convert the image as follows:
qemu-img convert disk0.qcow2 -O raw /dev/vg01/vm5
Afterwards you can delete the disk image:
rm -f disk0.qcow2
Now we must open the VM's xml configuration file /etc/libvirt/qemu/vm5.xml...
vi /etc/libvirt/qemu/vm5.xml
... and change the following section...
[...] <disk type='file' device='disk'> <source file='/root/vm5/ubuntu-kvm/disk0.qcow2'/> <target dev='hda' bus='ide'/> </disk> [...] |
... so that it looks as follows:
[...] <disk type='file' device='disk'> <source file='/dev/vg01/vm5'/> <target dev='hda' bus='ide'/> </disk> [...] |
That's it! You can now use virsh to manage the VM.
7 Links
- KVM (Ubuntu Community Documentation): https://help.ubuntu.com/community/KVM
- vmbuilder: https://help.ubuntu.com/community/JeOSVMBuilder
- JeOS and vmbuilder: http://doc.ubuntu.com/ubuntu/serverguide/C/jeos-and-vmbuilder.html
- Ubuntu: http://www.ubuntu.com/