Instead of spending money to purchase expensive storage in some scenarios it may be more cost-effective to use aftermarket hardware as a slow yet extendable storage whilst backing the storage with a fast cache. Fortunately, the Linux Logical Volume Manager (LVM) supports backing logical volumes with a cache placed on a different physical medium. At the same time, logical volumes are the fundamental storage technology for virtualization via libvirt such that virtual machines can gain a lot from speeding up the physical storage. This guide will demonstrate creating a cache device for a logical volume.
Assume two physical volumes (pvs
):
/dev/nvme0n1
/dev/sda1
The slow storage device may be a RAID drive backed by the hardware.
Issuing pvs
lists the following physical volumes:
PV VG Fmt Attr PSize PFree /dev/nvme0n1 vms lvm2 a-- <476.94g 384.81g /dev/sda1 vms lvm2 a-- 801.80g 301.80g
that were created using the LVM commands pvcreate
:
pvcreate /dev/nvme0n1 pvcreate /dev/sda1
and then placed inside the same volume group (required for caching) named vms
:
vgcreate vms /dev/nvme0n1 /dev/sda1
Inside the vms
volume group, a logical volume is created via the lvcreate
command:
lvcreate -L 20G -n slow vms /dev/sda1
where:
20G
specifies a logical volume of 20GiB
,slow
is the name of the volume,vms
is the volume group created previously,/dev/sda1
corresponds to the slow storage device where the logical volume will be placed.The next step is to create the cache and metadata logical volumes. Ulteriorly, both the cache and metadata volumes can be created in one single command but it is neater to be able to create both the cache and metadata volumes separately and then convert them to a cache volume.
The cache and metadata volumes are created as if they were logical volumes; issue:
lvcreate -L 5G -n slow_cache vms /dev/nvme0n1
where:
5G
is the cache volume size ,slow_cache
is the name of the cache logical volume,vms
is the same volume group consisting of the slow and fast storage,/dev/nvme0n1
is the fast storage device
The choice for a cache volume is up to the user however it is customary to use a cache size that is approximately 3
times less than the size of the slow volume.
Next, the metadata volume can be created - the metadata volume does not have to be large and the default minimal size of 20MiB
is mostly sufficient for all use cases:
lvcreate -L 20M -n slow_meta vms /dev/nvme0n1
where:
20M
is the size of the metadata volume,slow_meta
is the name of the metadata volume,vms
is the name of the volume group,/dev/nvme0n1
is the fast storage device.
Now that both the cache and metadata volumes have been created, both volumes will be fused together to create a cache pool via the lvconvert
command:
lvconvert --type cache-pool --cachemode writethrough --poolmetadata vms/slow_meta vms/slow_cache
Finally, the slow
logical volume will be backed by the newly created cache volume:
lvconvert --type cache --cachepool vms/slow_cache vms/slow
If successful, lvconvert
will indicate that the slow
logical volume is now cached.
Unfortunately, a logical volume backed by cache cannot be resized and requires the cache to be first detached from the logical volume. Similarly, when preparing for data migration, the cache may have to be removed.
To remove the cache for the slow
logical volume, issue:
lvconvert --uncache vms/slow
and the cache will be removed.
The previous steps can then be repeated in order to re-enable the cache.