Linux LVM tips

Linux LVM Tips

Cache

Add a cache to a logical volume

RedHat has some good docs: LVM cache


A cache can be added and removed transparently to active Linux LVM logical volumes, even with mounted filesystems in use. This is so awesome and really adds a lot of performance to underlying RAID arrays of hard drives.

  • 2x HDD (sda, sdb), 1x SSD (sdc)
  • HDDs-> GPT partition table -> partition1 -> md raid1 -> LVM PV -> LVM VG (vgtest) -> LVM LV (lvtest1)
  • SSD-> GPT partition table -> partition1 -> LVM PV

pvcreate -v /dev/sdc1
vgextend vgtest /dev/sdc1
lvconvert -v --type cache --cachedevice /dev/sdc1 vgtest/lvtest1 --chunksize 1024 --cachesize 100G

Chunks are 64k by default. Adjust chunksize and cachesize to taste. The whole device (sdc1) will be added to the volume group (vgtest), regardless of how much is assigned to the cache logical volume. Add more partitions to the cache device if you also want to use it for other volume groups.

The write mode cache is set to writethrough by default, 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. Write safety can be traded for write speed by setting writeback:

lvchange --cachemode writeback vgtest/lvtest1

The cache can be removed as follows:

lvconvert --uncache vgtest/lvtest1
vgreduce vgtest /dev/sdc1
pvremove /dev/sdc1

Extend cached Logical Volume:  https://www.cowley.tech/blog/2017/05/03/extend-cached-logical-volume/

The process is:

  • Mark your cached LV as uncached
  • Remove the cache device from the volume group
  • Extended your LV
  • Add cache device to volume group
  • Recreate your cache

Monitor LVM

lvs -a -o name,vg_name,size,pool_lv,devices,cachemode,chunksize,segtype

lvs -a -o +devices,cache_total_blocks,cache_used_blocks,cache_dirty_blocks,cache_read_hits,cache_read_misses,cache_write_hits,cache_write_misses,segtype

echo "alias lvs-cache='lvs -a -o +devices,cache_total_blocks,cache_used_blocks,cache_dirty_blocks,cache_read_hits,cache_read_misses,cache_write_hits,cache_write_misses,segtype'" >> ~/.bashrc

lvmcache-statistics.sh

lvs --all --options +devices

lvs -o+chunksize

dmsetup status


Comments