Skip to content

Instantly share code, notes, and snippets.



Last active Mar 28, 2017
What would you like to do?
Set up LVM SSD cache via shell script
#!/bin/bash -u
# Script to automate the setup and conversion of cache pools in LVM.
# I changed all my busy LVs using it, worked well for me.
# Cache and/or thin pools are supported since RHEL 6.4-ish but it's
# more complex/powerful than i.e. bcache.
# They might be the best tool for you, or might as well not.
# this is as an example, you need to review the script.
# i've also only done basic tests of the error handlers below.
# they seem sound - but again: just an example.
# verify that the lv path we were told to handle is probably ok
test -b /dev/$vgname/$lvname &&
LV=$(lvdisplay /dev/$vgname/$lvname) &&
echo "$LV" | grep -q "LV Write Access read/write" &&
echo "$LV" | grep -q "LV Status available"
return $?
# verify that the pv path for the cache is part of the VG
test -b /dev/$pvname &&
PV=$(pvdisplay /dev/$pvname)
echo "$PV" | grep -q $vgname &&
echo "$PV" | grep -q "Allocatable yes"
return $?
# here you (and I likely will, but might take a year or two) could add a check that verifies the
# thin daemons and tools are in place.
# here you could add another check that verifies two things I didn't need
# 1) LE size for the meta lv - in my case one LE is 256M so I picked -l1
# 2) Verify free space on the cache SSD
# set up the cache
# -y is not passed automatically, in this case, right now, i think it's good.
# it really just slows you down for a few seconds and lets LVM warn you any way it deems good.
lvcreate -l 1 -n ${lvname}_meta \
/dev/$vgname /dev/$pvname
lvcreate -L4G -n ${lvname}_cache \
/dev/$vgname /dev/$pvname
lvconvert --type cache-pool --cachemode writeback --poolmetadata \
/dev/$vgname/${lvname}_meta /dev/$vgname/${lvname}_cache
lvconvert --type cache --cachepool \
/dev/$vgname/${lvname}_cache /dev/$vgname/$lvname
if lvpre && pvpre ; then
echo "there were some errors"

This comment has been minimized.

Copy link

@matya matya commented Mar 19, 2017

*were :p


This comment has been minimized.

Copy link
Owner Author

@FlorianHeigl FlorianHeigl commented Mar 28, 2017

@matya hehe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment