Skip to content

Instantly share code, notes, and snippets.

@hluaces
Forked from gabrieljcs/lvm-cache-fedora.md
Created September 30, 2020 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hluaces/7be44c270e351d4825ae7f6ff09be676 to your computer and use it in GitHub Desktop.
Save hluaces/7be44c270e351d4825ae7f6ff09be676 to your computer and use it in GitHub Desktop.
Instructions to create an LVM cache for root in Fedora

LVM cache in Fedora

From the man-pages: "The cache logical volume type uses a small and fast LV to improve the performance of a large and slow LV. It does this by storing the frequently used blocks on the faster LV. LVM refers to the small fast LV as a cache pool LV. The large slow LV is called the origin LV. Due to requirements from dm-cache (the kernel driver), LVM further splits the cache pool LV into two devices - the cache data LV and cache metadata LV. The cache data LV is where copies of data blocks are kept from the origin LV to increase speed. The cache metadata LV holds the accounting information that specifies where data blocks are stored (e.g. on the origin LV or on the cache data LV). Users should be familiar with these LVs if they wish to create the best and most robust cached logical volumes. All of these associated LVs must be in the same VG."

Assuming LVM is already setup in HDD (e.g. from anaconda) and SSD is untouched.

Create a physical volume for the SSD

# pvcreate /dev/sdX

e.g. pvcreate /dev/sdb, sdb being the SSD.

Extend your existing volume group to include the SSD

# vgextend VOLUME_GROUP /dev/sdX

anaconda names the volume group as fedora_HOSTNAME.

Create a cache metadata LV

# lvcreate -n meta -L YMB VOLUME_GROUP /dev/sdX

Where Y is 1000 times smaller than the cache data LV, with a minimum of 8MB. e.g. if you have a 32 GB SSD, Y = 32MB.

Create a cache data LV

# lvcreate -n cache -l Z VOLUME_GROUP /dev/sdX

Where Z is the size of your cache. Use 99%PVS to use 99% of the available physical volume size, i.e. mostly all of the SSD.

Create the cache pool LV

# lvconvert --type cache-pool --cachemode writeback --poolmetadata VOLUME_GROUP/meta VOLUME_GROUP/cache

This combines the cache data and metadata into a cache pool that uses writeback mode. Ommitting defaults to writethrough, which stores data in the cache and on the origin LV.

Create the cache

# lvconvert --type cache --cachepool VOLUME_GROUP/cache VOLUME_GROUP/root

The pool is then converted into a cache for the root partition.

Rebuild initramfs

# dracut -v -f

This will rebuild the initramfs to include the cache LV.

Sources and further info:

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Logical_Volume_Manager_Administration/lvm_cache_volume_creation.html

http://man7.org/linux/man-pages/man7/lvmcache.7.html

Thanks to ntn888 for the cachemode suggestion.

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