Skip to content

Instantly share code, notes, and snippets.

@FlorianHeigl
Last active January 23, 2022 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianHeigl/caf2ea6b66d05cd264509493db3b7869 to your computer and use it in GitHub Desktop.
Save FlorianHeigl/caf2ea6b66d05cd264509493db3b7869 to your computer and use it in GitHub Desktop.
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.
pvname=sdc
vgname=vgxen
lvname=$1
# verify that the lv path we were told to handle is probably ok
lvpre()
{
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
pvpre()
{
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.
cache_lv()
{
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
cache_lv
else
echo "there were some errors"
fi
@matya
Copy link

matya commented Mar 19, 2017

*were :p

@FlorianHeigl
Copy link
Author

@matya hehe

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