After RHEL6.7, LVM provides full support for LVM cache logical volumes. Using dm-cache, a LVM cache logical volume uses a small logical volume consisting of fast block devices (such as SSD drives) to improve the performance of a larger and slower logical volume by storing the frequently used blocks on the smaller, faster logical volume.

Here is an quick example shows how to create a LVM cache LV.

Basic idea

 The basic idea is to create one cache lv and one cache meta lv on SSD, using them to crate a cache pool logical volume, then attatch the cache pool to a big regular slow Logical Volume.

Some components that different from regular ones.

 

  • Cache data logical volume — the logical volume containing the data blocks for the cache pool logical volume
  • Cache metadata logical volume — the logical volume containing the metadata for the cache pool logical volume, which holds the accounting information that specifies where data blocks are stored (for example, on the origin logical volume or the cache data logical volume).
  • Cache logical volume — the logical volume containing the origin logical volume and the cache pool logical volume. This is the resultant usable device which encapsulates the various cache volume components.

 

Step by step

The following procedure creates an LVM cache logical volume.

Create a regular volume group

 Create a regular VG that contains a slow physical volume and a fast physical volume. 

# pvcreate /dev/sdb
# pvcreate /dev/ssd    # not real name as you can tell, easy to remember later
# vgcreate VG /dev/sdb /dev/ssd

Create a regular volume on big slow PV. 

# lvcreate -L 1T -n lv VG /dev/sdb

Create the cache data logical volume on SSD PV. 

# lvcreate -L 200G -n lv_cache VG /dev/ssd

Create the cache metadata logical volume on SSD PV.

This logical volume will hold cache pool metadata.

# lvcreate -L 100M -n lv_cache_meta VG /dev/ssd

Create the cache pool logical volume

by combining the cache data and the cache metadata logical volumes into a logical volume of type cache-pool

# lvconvert --type cache-pool --cachemode writethrough --poolmetadata VG/lv_cache_meta VG/lv_cache
  WARNING: Converting logical volume VG/lv_cache and VG/lv_cache_meta to pool's data and metadata volumes.
  THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)
  Converted VG/lv_cache to cache pool.

In this example the pcachemodep argument is set to writethrough , which indicates that a write is considered complete only when it has been stored in both the cache pool logical volume and on the origin logical volume.

When you execute this command, the cache data logical volume is renamed with _cdata appended to the original name of the cache data logical volume, and the cache metadata logical volume is renamed with _cmeta appended to the original name of the cache data logical volume; both of these volumes become hidden.

# lvs -a -o +devices
  LV               VG  Attr       LSize   Pool Origin Data%  Meta% Cpy%Sync Devices
  lv               VG  -wi-a-----   1000.00g                                /dev/sdb
  lv_cache         VG  Cwi---C---   200.00g                                 lv_cache_cdata(0)
  [lv_cache_cdata] VG  Cwi-------   200.00g                                 /dev/ssd
  [lv_cache_cmeta] VG  ewi-------   100.00m                                 /dev/ssd

Create the cache logical volume by combining the cache pool logical volume with the origin logical volume. The user-accessible cache logical volume takes the name of the origin logical volume. The origin logical volume becomes a hidden logical volume with _corig appended to the original name. You can execute this command when the origin logical volume is in use.

# lvconvert --type cache --cachepool VG/lv_cache VG/lv
  Logical volume VG/lv is now cached.
# lvs -a -o +devices
  LV                VG  Attr       LSize   Pool       Origin            Data%  Meta% Devices
  lv                VG  Cwi-a-C--- 1000.00g [lv_cache] [lv_corig] 0.02   2.31  0.00  lv_corig(0)
  [lv_corig]        VG  owi-aoC--- 1000.00g                                          /dev/sdb
  [lv_cache]        VG  Cwi---C---  200.00g                              0.02   2.31 lv_cache_cdata(0)
  [lv_cache_cdata]  VG  Cwi-ao----  200.00g                                          /dev/ssd
  [lv_cache_cmeta]  VG  ewi-ao----  100.00m                                          /dev/ssd
  [lvol0_pmspare]   VG  ewi-------  100.00m                                          /dev/ssd

 

More options

Spare metadata LV

See lvmthin(7) for a description of the "pool metadata spare" LV.The same concept is used for cache pools.

Automatic pool metadata LV

A cache data LV can be converted to cache pool LV without specifying
       a cache pool metadata LV.  LVM will automatically create a metadata
       LV from the same VG.

       lvcreate -n CacheDataLV -L CacheSize VG
       lvconvert --type cache-pool VG/CacheDataLV

Create a new cache LV without an existing origin LV

A cache LV can be created using an existing cache pool without an
       existing origin LV.  A new origin LV is created and linked to the
       cache pool in a single step.

       lvcreate --type cache -L LargeSize -n CacheLV
              --cachepool VG/CachePoolLV VG SlowPVs

Single step cache pool LV creation

A cache pool LV can be created with a single lvcreate command, rather
       than using lvconvert on existing LVs.  This one command creates a
       cache data LV, a cache metadata LV, and combines the two into a cache
       pool LV.

       lvcreate --type cache-pool -L CacheSize -n CachePoolLV VG FastPVs

Convert existing LVs to cache types

When an existing origin LV is converted to a cache LV, the specified
       cache pool may be a normal LV, rather than a cache pool LV.  In this
       case, lvm will first convert the normal LV to a cache pool LV.  A
       pool metadata LV may optionally be specified.

       lvcreate -n OriginLV -L LargeSize VG
       lvcreate -n CacheDataLV -L CacheSize VG
       lvconvert --type cache --cachepool VG/CataDataLV VG/OriginLV

       This is equivalent to:

       lvcreate -n OriginLV -L LargeSize VG
       lvcreate -n CacheDataLV -L CacheSize VG
       lvconvert --type cache-pool VG/CacheDataLV
       lvconvert --type cache --cachepool VG/CachePoolLV VG/OriginLV