Rawpixel - Fotolia
If you want to run several KVM virtual machines on a host, it makes sense to efficiently use your available storage. Logical Volume Manager thin provisioning can help improve efficiency and allow you to run more VMs. In this article, you'll read how to set up LVM thin provisioning.
To use LVM thin provisioning with KVM, you need to configure the VMs to use the LVM volume you've created as the storage back end and then begin setting up your LVM environment.
In a normal LVM environment, you configure a storage device as the physical volume. On top of the physical volume you create the volume group and from the VG, you can partition logical volumes (LVs). What makes LVM such a flexible approach to dealing with local storage is the fact that the LVs are allocated from the VG and are abstracted from the underlying physical disk. This allows administrators to add a new disk when they're running out of disk space on the VG or to create LVs that are larger than the size of individual disks in the VG.
With thin provisioning, the total disk space allocated to all LVs can exceed the total amount of available disk space on all disks. The assumption behind over-allocation is that the LVs will never grow to their maximum size. For example, if you want to deploy 10 VMs, each with a 10 GB disk, it is likely that actual disk use on each VM won't exceed 6 GB. If that is the case, it's a waste of disk space to reserve the full 10 GB of disk space for each VM.
While thin provisioning allows you to use disk space more efficiently, you also risk running out of physical disk space if the VMs need more space than anticipated. Therefore, it is essential to make the appropriate calculation before you decide on the maximum disk space you want to allocate.
The procedure below is based on a CentOS test system, where a 5 GB disk is used to create two 3 GB thin provisioned volumes. For this example, let's assume the disk has already been added and is named /dev/sdc.
Start by creating a VG on top of the /dev/sdc disk using the command vgcreate vgthin /dev/sdc. This will also automatically mark /dev/sdc as a PV.
To use thin provisioning, you need to create a pool on top of the volume. This pool is created as a special type of LVM logical volume, and the logical volumes will be created in the pool. Use the command lvcreate -l 100%FREE --thinpool tpthin vgthin. This creates a pool with the name tpthin, within the VG called vgtin. Notice that over-allocation doesn't happen at the level of the thin pool, but at the level of the LVs you'll create inside the thin pool. This is why you'll make this thin pool exactly as big as the VG.
At this point you can create the LVs. To do this, use the following command: lvcreate -V 3G --thin -n lvthin1 vgtin/tpthin. Repeat this command twice to create two volumes with a size of 3 GB each. Notice that while creating the thin provisioned volume, you'll use the option -V (virtual size) to specify the size of the volume, not the -L option, which specifies the regular size.
At this point you have created two thin provisioned logical volumes. You can now use them when creating VMs.
When using thin provisioned volumes, it is important to monitor the availability of physical disk space. To do this, type the lvs command to get an overview of the real use of storage on your thin provisioned volumes:
[root@server1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
root centos -wi-ao---- 8.51g
swap centos -wi-ao---- 1.00g
lvsan1 vgsan -wi-ao---- 200.00m
lvsan2 vgsan -wi-ao---- 200.00m
lvol0 vgthin -wi------- 8.00m
lvol1 vgthin -wi------- 8.00m
lvthin1 vgthin Vwi-aotz-- 3.00g tpthin 1.07
lvthin2 vgthin Vwi-a-tz-- 3.00g tpthin 0.00
tpthin vgthin twi-a-tz-- 4.96g 0.65
This command shows the amount of real disk space still available in the pool, as well as the amount of virtual disk space used in the thin provisioned volumes. Just make sure that you never get close to 100% usage in the thin provisioning pool and you'll be good. If you risk falling short of disk space, you can use the normal LVM commands vgextend and lvextend to grow the disk size available in the VG as well as the LVs.