Table of Contents

About

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.

Layout

Assume two physical volumes (pvs):

The slow storage device may be a RAID drive backed by the hardware.

Creating Physical Volumes and a Volume Group

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:

Caching the Slow Logical Volume

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:

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:

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.

Removing the Cache Volume

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.