Skip to content

Instantly share code, notes, and snippets.

@gabrieljcs
Last active March 23, 2024 13:33
Show Gist options
  • Save gabrieljcs/805c183753046dcc6131 to your computer and use it in GitHub Desktop.
Save gabrieljcs/805c183753046dcc6131 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 100%FREE to use the remaining free space in the physical volume.

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 and @Yeshey for the 100%FREE option.

@gabrieljcs
Copy link
Author

gabrieljcs commented Jan 19, 2023

@vpieper ah, got it, thanks for the follow up!

@tuxmaster5000
Copy link

Wouldn't it be better adding the following command to use writeback mode to get better performance?

lvchange --cachemode writeback fedora_localhost-live/root

This will be dangerous, when the cache device will not be an RAID1. Its is the same risk when using caching on ZFS. You will risk your data, when the caching device will fails. So only an read cache are fail save without RAID1 on the cache device.

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