lv-convert2cache v0.1.3 [12 Nov 2024] by Dominic
Description
TL;DR: lv-convert2cache.sh supercharges a Logical Volume.
lv-convert2cache.sh is a bash wrapper script for LVM functions which convert an existing LVM logical volume (LV) into a read/write 'cache LV'. A typical scenario is that an existing LV (in an existing volume group (VG)) is currently stored on one (or more) physical volume(s) (PV(s)) which are on one (or more) slower devices (e.g. spinning disks), and we want to make this LV faster for read and write by (optionally creating and) using a new PV on a smaller but faster device (e.g. NVMe SSD) as a 'cache pool'. For more info see 'man 7 lvmcache'.
After some sanity tests, lv-convert2cache.sh asks you (once) to confirm before any action is taken.
When lv-convert2cache.sh has completed, the new cache LV will have the same name as, and be usable as, the original LV. There will also be two hidden LVs - the data LV on the original slower drive (named as the original LV with '_corig' suffixed) and a cache pool LV on the faster device (named as the original LV with '_CacheVol_cvol' suffixed). Internally the cache pool LV will hold both the cached data and the metadata for managing the cache. All LVs can be seen with: lvs --all --options +devices
lv-convert2cache.sh has been tested under LVM 2.03.11(2) (2021-01-08).
Usage
lv-convert2cache.sh [options] existing_VG/existing_LV PV_for_cache ["further_options_for_lvconvert_(at_cache_creation)"]
Example
lv-convert2cache.sh timedicer/home /dev/sda1 "--chunksize 512"
Options
-b use writeback cachemode setting (faster writes, but risk of data loss)
-h see this help
-l see changelog
-n do not show program title
-v merely show existing cache LVs, if any
Notes
The device (typically a drive partition) to be used for the new PV (PV_for_cache i.e. the cache pool) must exist, but need not already be a PV. It should be much faster than the PV(s) which hold the existing_LV (or the conversion is pointless), and its contents (if any) must be expendable - they will be lost.
I have not seen any guidance on the recommended size ratio between the cache pool device (PV_for_cache) and the underlying data LV (existing_LV). Most examples use small cache pools (e.g. 10GB), maybe because they were written when larger SSDs were expensive. I have used a 200GB cache pool (for a 2TB source LV) without problems, except having to specify "--chunksize 512" when creating it.
- Of 2 possible cache methods, lv-convert2cache.sh uses 'dm-cache' (alternative 'dm-writecache' only speeds up writing not reading)
- Of 2 possible cache options, lv-convert2cache.sh uses 'cachevol' (the cached copies of data blocks and the metadata for managing the cache are both stored in the same (hidden) LV on the same (fast) device; the more complex alternative option 'cachepool' allows having cache data & metadata on different (LVs and) devices [which could make the cached LV operate marginally faster])
- Unsurprisingly, to make changes lv-convert2cache.sh must be run by superuser
- I made this utility to help me and you, but you use it at your own risk
Normally lv-convert2cache.sh uses the default and recommended 'writethrough' cachemode setting, but this means that caching gives no speed gain when writing, and may even be slower. Option '-b' (untested at time of writing [22 Dec 2023]) forces use of cachemode 'writeback' - this should bring write speed gains but at the risk of data loss if the cache pool LV (cachevol) is corrupted or lost.
Note for versions of LVM earlier than 2.03.12 it is not possible to resize logical volumes of cache type, including those created by lv-convert2cache.sh. To workaround this limitation, split and then rejoin the data and cache LVs (but you will lose the cache data which will have to be rebuilt over time):
# example: VG 'timedicer', cache LV 'home', non-standard chunksize '512'
lvconvert --splitcache timedicer/home # split cache LV
lvs -a -o +cache_mode,cache_settings,cache_policy # show LVs
lvextend -L+100G -r timedicer/home # extend LV and underlying FS
# restore cache LV
lvconvert --type cache --cachevol timedicer/home_CacheVol timedicer/home --chunksize 512
lvs -a -o +cache_mode,cache_settings,cache_policy,chunk_size # show LVs
With the above lvconvert command it is possible to specify --cachemode writeback (instead of the default --cachemode writethrough) for faster writes, however as of LVM tools 2.03.11(2) this is regarded as unsafe and you will have to ignore/override warnings saying 'repairing a damaged cachevol is not yet possible' and 'cache mode writethrough is suggested for safe operation'. lv-convert2cache.sh (except with option '-b') sticks with the default setting.
Another approach which works equally well but may be a little slower (because --uncache is slower than --splitcache) involves removing the cache pool LV, something like this:
lvextend -L+100G -r timedicer/home # extend LV and the underlying FS
lv-convert2cache.sh timedicer/home /dev/sda1 "--chunksize 512"
If you lose the device on which your cache pool is stored, you will have problems, especially because you will also have lost the metadata for the whole LV (so you will not be able to access the underlying LV even though all your data should still be there). See the first listed page at Kudos below; if this is insufficient, recovery should be possible by using a backup of LVM metadata to recreate the PV, VG and cache pool LV - for guidance try: https://www.golinuxcloud.com/recover-lvm2-partition-restore-vg-pv-metadata/#How_to_manually_delete_LVM_metadata_in_Linux or, in case it has disappeared, then as pdf at: https://www.timedicer.co.uk/programs/helpinfo/Recover_LVM2_partition_PV_VG_LVM_metadata.pdf
Kudos
The following pages were helpful in devising this utility and writing the help text -
https://blog.delouw.ch/2020/01/29/using-lvm-cache-for-storage-tiering/
https://manpages.ubuntu.com/manpages/focal/man7/lvmcache.7.html
https://docs.redhat.com/fr/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/enabling-caching-to-improve-logical-volume-performance_configuring-and-managing-logical-volumes
https://www.stderr.nl/Blog/Software/Linux/LvmResizeCachedLv.html
License
Copyright © 2024 Dominic Raferd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Changelog
0.1.3 [12 Nov 2024] - Updated link in kudos section
0.1.2 [20 Dec 2023] - Add -b option (enable writeback cachemode) (untested)
0.1.1 [19 Dec 2023] - Help text changes: add info re backup pdf page for data recovery + re splitting cached LV
0.1.0 [14 Dec 2022] - Help text changes, and add hints on what to do if cache pool device goes missing
0.0.6 [16 Nov 2022] - Help text clarifications, bugfix -v option, add -n option
0.0.5 [08 Apr 2022] - Help text clarifications
0.0.4 [06 Apr 2022] - Add help about how to resize/extend a cache type LV, add -v option
0.0.3 [01 Apr 2022] - Help text clarifications
0.0.2 [11 Feb 2022] - Help text clarifications
0.0.1 [02 Feb 2022] - Help text changes, bug fix, check if existing LV is already cached
0.0.0 [11 Jan 2022] - Initial version
Download lv-convert2cache.sh
Donation
I have provided this software free gratis and for nothing. If you would like to thank me with a contribution, please let me know and I will send you a link. Thank you!
My Other Sites
- TimeDicer - Onsite/offsite data backup for Windows (uses rdiff-backup)
- Finding a 4D Backup Solution
- Web Scraping How To - extracting data from web sites
My Programs
Here is a selection of some (other) programs I have written, most of which run under GNU/Linux from the command line (CLI), are freely available and can be obtained by clicking on the links. Dependencies are shown and while in most cases written and tested on an x86-based Linux server, they should run on a Raspberry Pi, and many can run under Windows using Windows Subsystem for Linux (WSL) or Cygwin. Email me if you have problems or questions, or if you think I could help with a programming requirement.
Backup Utilities
- TimeDicer - Onsite/offsite data backup for Windows (uses rdiff-backup) [ GNU/Linux & MS Windows©: 2008-20 ]
- rdiff-backup-regress - GNU/Linux script to regress an rdiff-backup archive. [ GNU/Linux: 2012-24 ]
Debian/Ubuntu kernel and LVM Utilities
- kernel-remove - GNU/Linux script to list the installed GNU/Linux kernels in a Debian-based distro (e.g. Ubuntu), and can be used to remove an unwanted kernel and related packages, updating grub appropriately. [ GNU/Linux-Debian/Ubuntu: 2010-24 ]
- lvm-usage - GNU/Linux script to show available disk space and how it is used; run as cron job to warn if usage is above a set percentage. Provides additional information if LVM is in use. [ GNU/Linux: 2012-24 ]
- lvm-delete-snapshot - GNU/Linux script to remove LVM snapshot that has been left over by another process. [ GNU/Linux: 2012-21 ]
- netnames - GNU/Linux script shows current name, biosdevname and 'predictable name' of network device - helps with network device name scheme migration. [ GNU/Linux-Debian/Ubuntu: 2020-20 ]
Miscellaneous Programs
- sleepwalker - Windows© program which can be run from a remote machine to 'wake up' a Windows© machine behind a router, wait for it to start and then initiate Remote Desktop session. [MS Windows©: 2008-22]
- numliststat - GNU/Linux program giving statistical value(s) for a piped-in list of numbers. [ GNU/Linux: 2022-24 ]
- relay-enforcer - GNU/Linux program enabling a postfix-based mail server relaying to Gmail to act on reports from Gmail about blocked emails. [ GNU/Linux: 2016-24 ]
- pdf-compress - GNU/Linux program to create smaller b/w pdf file from an original large pdf file, especially when original resulted from scanning. [ GNU/Linux: 2016-23 ]
- tiny-device-monitor - GNU/Linux program to test webpages (including password-protected) or machines to check they are live; use as a cron job for your own websites, for hardware presenting a webpage, or for any machines with a presence on your local LAN or on the internet. [ GNU/Linux: 2009-24 ]
- form-extractor - GNU/Linux program to extract form tags from a web page or downloaded file. [ GNU/Linux: 2012-24 ]
- mythic-dns-sync - GNU/Linux program to update DNS record at mythic-beasts.com to match local external ip. [ GNU/Linux: 2016-23 ]
- saynoto0870 - For UK, a GNU/Linux script which performs automated lookup of the www.saynoto0870.com database, finding cheap or free geographic number replacements for expensive non-geographic (087* or 084*) numbers. [ GNU/Linux: 2012-12 ]
- bind9-resolved-switch - GNU/Linux program for switching permanently between using bind9 or systemd-resolved as the system DNS resolver. [ GNU/Linux: 2016-24 ]
- unlock - GNU/Linux remote program for easy entering of decrypt passphrase on a remote machine which has root dm-crypt+LUKS. [ GNU/Linux: 2017-18 ]
- wifi-updown - GNU/Linux program to take down wifi interface if there is a working wired interface (or restore wifi if not). [ GNU/Linux: 2018-23 ]
- routefix - GNU/Linux program to restore a default ip traffic route if there is none such (e.g. after running wifi-updown). [ GNU/Linux: 2018-23 ]
- dutree - GNU/Linux program to show a tree-style list of files and directories at the specified location which are greater than the specified size (default 1GB). [ GNU/Linux: 2012-24 ]
- Accounts - Multi-business multi-currency accounting software, uses Access [MS Windows©: 1996-2024]
- Rents Program - Residential lettings/landlord front office program, with many special features for UK market [MS Windows©: 1991-2024]
This section is closed. If you have a question, please submit it by email, thank you.