Chapter 5. Advanced logical volume management
LVM includes advanced features such as:
- Snapshots, which are point-in-time copies of logical volumes (LVs)
- Caching, with which you can use faster storage as a cache for slower storage
- Creating custom thin pools
- Creating custom VDO LVs
5.1. Managing logical volume snapshots
A snapshot is a logical volume (LV) that mirrors the content of another LV at a specific point in time.
5.1.1. Understanding logical volume snapshots
When you create a snapshot, you are creating a new LV that serves as a point-in-time copy of another LV. Initially, the snapshot LV contains no actual data. Instead, it references the data blocks of the original LV at the moment of snapshot creation.
It is important to regularly monitor the snapshot’s storage usage. If a snapshot reaches 100% of its allocated space, it will become invalid.
It is essential to extend the snapshot before it gets completely filled. This can be done manually by using the lvextend
command or automatically via the /etc/lvm/lvm.conf
file.
- Thick LV snapshots
- When data on the original LV changes, the copy-on-write (CoW) system copies the original, unchanged data to the snapshot before the change is made. This way, the snapshot grows in size only as changes occur, storing the state of the original volume at the time of the snapshot’s creation. Thick snapshots are a type of LV that requires you to allocate some amount of storage space upfront. This amount can later be extended or reduced, however, you should consider what type of changes you intend to make to the original LV. This helps you to avoid either wasting resources by allocating too much space or needing to frequently increase the snapshot size if you allocate too little.
- Thin LV snapshots
Thin snapshots are a type of LV created from an existing thin provisioned LV. Thin snapshots do not require allocating extra space upfront. Initially, both the original LV and its snapshot share the same data blocks. When changes are made to the original LV, it writes new data to different blocks, while the snapshot continues to reference the original blocks, preserving a point-in-time view of the LV’s data at the snapshot creation.
Thin provisioning is a method of optimizing and managing storage efficiently by allocating disk space on an as-needed basis. This means that you can create multiple LVs without needing to allocate a large amount of storage upfront for each LV. The storage is shared among all LVs in a thin pool, making it a more efficient use of resources. A thin pool allocates space on-demand to its LVs.
- Choosing between thick and thin LV snapshots
- The choice between thick or thin LV snapshots is directly determined by the type of LV you are taking a snapshot of. If your original LV is a thick LV, your snapshots will be thick. If your original LV is thin, your snapshots will be thin.
5.1.2. Managing thick logical volume snapshots
When you create a thick LV snapshot, it is important to consider the storage requirements and the intended lifespan of your snapshot. You need to allocate enough storage for it based on the expected changes to the original volume. The snapshot must have a sufficient size to capture changes during its intended lifespan, but it cannot exceed the size of the original LV. If you expect a low rate of change, a smaller snapshot size of 10%-15% might be sufficient. For LVs with a high rate of change, you might need to allocate 30% or more.
It is essential to extend the snapshot before it gets completely filled. If a snapshot reaches 100% of its allocated space, it becomes invalid. You can monitor the snapshot capacity with the lvs -o lv_name,data_percent,origin
command.
5.1.2.1. Creating thick logical volume snapshots
You can create a thick LV snapshot with the lvcreate
command.
Prerequisites
- Administrative access.
- You have created a physical volume. For more information, see Creating LVM physical volume.
- You have created a volume group. For more information, see Creating LVM volume group.
- You have created a logical volume. For more information, see Creating logical volumes.
Procedure
Identify the LV of which you want to create a snapshot:
# lvs -o vg_name,lv_name,lv_size VG LV LSize VolumeGroupName LogicalVolumeName 10.00g
The size of the snapshot cannot exceed the size of the LV.
Create a thick LV snapshot:
# lvcreate --snapshot --size SnapshotSize --name SnapshotName VolumeGroupName/LogicalVolumeName
Replace SnapshotSize with the size you want to allocate for the snapshot (e.g. 10G). Replace SnapshotName with the name you want to give to the snapshot logical volume. Replace VolumeGroupName with the name of the volume group that contains the original logical volume. Replace LogicalVolumeName with the name of the logical volume that you want to create a snapshot of.
Verification
Verify that the snapshot is created:
# lvs -o lv_name,origin LV Origin LogicalVolumeName SnapshotName LogicalVolumeName
Additional resources
-
lvcreate(8)
andlvs(8)
man pages
5.1.2.2. Manually extending logical volume snapshots
If a snapshot reaches 100% of its allocated space, it becomes invalid. It is essential to extend the snapshot before it gets completely filled. This can be done manually by using the lvextend
command.
Prerequisites
- Administrative access.
Procedure
List the names of volume groups, logical volumes, source volumes for snapshots, their usage percentages, and sizes:
# lvs -o vg_name,lv_name,origin,data_percent,lv_size VG LV Origin Data% LSize VolumeGroupName LogicalVolumeName 10.00g VolumeGroupName SnapshotName LogicalVolumeName 82.00 5.00g
Extend the thick-provisioned snapshot:
# lvextend --size +AdditionalSize VolumeGroupName/SnapshotName
Replace AdditionalSize with how much space to add to the snapshot (for example, +1G). Replace VolumeGroupName with the name of the volume group. Replace SnapshotName with the name of the snapshot.
Verification
Verify that the LV is extended:
# lvs -o vg_name,lv_name,origin,data_percent,lv_size VG LV Origin Data% LSize VolumeGroupName LogicalVolumeName 10.00g VolumeGroupName SnapshotName LogicalVolumeName 68.33 6.00g
5.1.2.3. Automatically extending thick logical volume snapshots
If a snapshot reaches 100% of its allocated space, it becomes invalid. It is essential to extend the snapshot before it gets completely filled. This can be done automatically.
Prerequisites
- Administrative access.
Procedure
-
As the
root
user, open the/etc/lvm/lvm.conf
file in an editor of your choice. Uncomment the
snapshot_autoextend_threshold
andsnapshot_autoextend_percent
lines and set each parameter to a required value:snapshot_autoextend_threshold = 70 snapshot_autoextend_percent = 20
snapshot_autoextend_threshold
determines the percentage at which LVM starts to auto-extend the snapshot. For example, setting the parameter to 70 means that LVM will try to extend the snapshot when it reaches 70% capacity.snapshot_autoextend_percent
specifies by what percentage the snapshot should be extended when it reaches the threshold. For example, setting the parameter to 20 means the snapshot will be increased by 20% of its current size.- Save the changes and exit the editor.
Restart the
lvm2-monitor
:# systemctl restart lvm2-monitor
5.1.2.4. Merging thick logical volume snapshots
You can merge thick LV snapshot into the original logical volume from which the snapshot was created. The process of merging means that the original LV is reverted to the state it was in when the snapshot was created. Once the merge is complete, the snapshot is removed.
The merge between the original and snapshot LV is postponed if either is active. It only proceeds once the LVs are reactivated and not in use.
Prerequisites
- Administrative access.
Procedure
List the LVs, their volume groups, and their paths:
# lvs -o lv_name,vg_name,lv_path LV VG Path LogicalVolumeName VolumeGroupName /dev/VolumeGroupName/LogicalVolumeName SnapshotName VolumeGroupName /dev/VolumeGroupName/SnapshotName
Check where the LVs are mounted:
# findmnt -o SOURCE,TARGET /dev/VolumeGroupName/LogicalVolumeName # findmnt -o SOURCE,TARGET /dev/VolumeGroupName/SnapshotName
Replace /dev/VolumeGroupName/LogicalVolumeName with the path to your logical volume. Replace /dev/VolumeGroupName/SnapshotName with the path to your snapshot.
Unmount the LVs:
# umount /LogicalVolume/MountPoint # umount /Snapshot/MountPoint
Replace /LogicalVolume/MountPoint with the mounting point for your logical volume. Replace /Snapshot/MountPoint with the mounting point for your snapshot.
Deactivate the LVs:
# lvchange --activate n VolumeGroupName/LogicalVolumeName # lvchange --activate n VolumeGroupName/SnapshotName
Replace VolumeGroupName with the name of the volume group. Replace LogicalVolumeName with the name of the logical volume. Replace SnapshotName with the name of your snapshot.
Merge the thick LV snapshot into the origin:
# lvconvert --merge SnapshotName
Replace SnapshotName with the name of the snapshot.
Activate the LV:
# lvchange --activate y VolumeGroupName/LogicalVolumeName
Replace VolumeGroupName with the name of the volume group. Replace LogicalVolumeName with the name of the logical volume.
Mount the LV:
# umount /LogicalVolume/MountPoint
Replace /LogicalVolume/MountPoint with the mounting point for your logical volume.
Verification
Verify that the snapshot is removed:
# lvs -o lv_name
Additional resources
-
The
lvconvert(8)
,lvs(8)
man page
5.1.3. Managing thin logical volume snapshots
Thin provisioning is appropriate where storage efficiency is a priority. Storage space dynamic allocation reduces initial storage costs and maximizes the use of available storage resources. In environments with dynamic workloads or where storage grows over time, thin provisioning allows for flexibility. It enables the storage system to adapt to changing needs without requiring large upfront allocations of the storage space. With dynamic allocation, over-provisioning is possible, where the total size of all LVs can exceed the physical size of the thin pool, under the assumption that not all space will be utilized at the same time.
5.1.3.1. Creating thin logical volume snapshots
You can create a thin LV snapshot with the lvcreate
command. When creating a thin LV snapshot, avoid specifying the snapshot size. Including a size parameter results in the creation of a thick snapshot instead.
Prerequisites
- Administrative access.
- You have created a physical volume. For more information, see Creating LVM physical volume.
- You have created a volume group. For more information, see Creating LVM volume group.
- You have created a logical volume. For more information, see Creating logical volumes.
Procedure
Identify the LV of which you want to create a snapshot:
# lvs -o lv_name,vg_name,pool_lv,lv_size LV VG Pool LSize PoolName VolumeGroupName 152.00m ThinVolumeName VolumeGroupName PoolName 100.00m
Create a thin LV snapshot:
# lvcreate --snapshot --name SnapshotName VolumeGroupName/ThinVolumeName
Replace SnapshotName with the name you want to give to the snapshot logical volume. Replace VolumeGroupName with the name of the volume group that contains the original logical volume. Replace ThinVolumeName with the name of the thin logical volume that you want to create a snapshot of.
Verification
Verify that the snapshot is created:
# lvs -o lv_name,origin LV Origin PoolName SnapshotName ThinVolumeName ThinVolumeName
Additional resources
-
lvcreate(8)
andlvs(8)
man pages
5.1.3.2. Merging thin logical volume snapshots
You can merge thin LV snapshot into the original logical volume from which the snapshot was created. The process of merging means that the original LV is reverted to the state it was in when the snapshot was created. Once the merge is complete, the snapshot is removed.
Prerequisites
- Administrative access.
Procedure
List the LVs, their volume groups, and their paths:
# lvs -o lv_name,vg_name,lv_path LV VG Path ThinPoolName VolumeGroupName ThinSnapshotName VolumeGroupName /dev/VolumeGroupName/ThinSnapshotName ThinVolumeName VolumeGroupName /dev/VolumeGroupName/ThinVolumeName
Check where the original LV is mounted:
# findmnt -o SOURCE,TARGET /dev/VolumeGroupName/ThinVolumeName
Replace VolumeGroupName/ThinVolumeName with the path to your logical volume.
Unmount the LV:
# umount /ThinLogicalVolume/MountPoint
Replace /ThinLogicalVolume/MountPoint with the mounting point for your logical volume. Replace /ThinSnapshot/MountPoint with the mounting point for your snapshot.
Deactivate the LV:
# lvchange --activate n VolumeGroupName/ThinLogicalVolumeName
Replace VolumeGroupName with the name of the volume group. Replace ThinLogicalVolumeName with the name of the logical volume.
Merge the thin LV snapshot into the origin:
# lvconvert --mergethin VolumeGroupName/ThinSnapshotName
Replace VolumeGroupName with the name of the volume group. Replace ThinSnapshotName with the name of the snapshot.
Mount the LV:
# umount /ThinLogicalVolume/MountPoint
Replace /ThinLogicalVolume/MountPoint with the mounting point for your logical volume.
Verification
Verify that the original LV is merged:
# lvs -o lv_name
Additional resources
-
The
lvremove(8)
,lvs(8)
man page
5.2. Caching logical volumes
You can cache logical volumes by using the dm-cache
or dm-writecache
targets.
dm-cache
utilizes faster storage device (SSD) as cache for a slower storage device (HDD). It caches read and write data, optimizing access times for frequently used data. It is beneficial in mixed workload environments where enhancing read and write operations can lead to significant performance improvements.
dm-writecache
optimizes write operations by using a faster storage medium (SSD) to temporarily hold write data before it is committed to the primary storage device (HDD). It is beneficial for write-intensive applications where write performance can slow down the data transfer process.
5.2.1. Caching logical volumes with dm-cache
When caching LV with dm-cache
, a cache pool is created. A cache pool is a LV that combines both the cache data, which stores the actual cached content, and cache metadata, which tracks what content is stored in the cache. This pool is then associated with a specific LV to cache its data.
dm-cache
targets two types of blocks: frequently accessed (hot) blocks are moved to the cache, while less frequently accessed (cold) blocks remain on the slower device.
Prerequisites
- Administrative access.
Procedure
Display the LV you want to cache and its volume group:
# lvs -o lv_name,vg_name LV VG LogicalVolumeName VolumeGroupName
Create the cache pool:
# lvcreate --type cache-pool --name CachePoolName --size Size VolumeGroupName /FastDevicePath
Replace CachePoolName with the name of the cache pool. Replace Size with the size for your cache pool. Replace VolumeGroupName with the name of the volume group. Replace /FastDevicePath with the path to your fast device, for example SSD or NVME.
Attach the cache pool to the LV:
# lvconvert --type cache --cachepool VolumeGroupName/CachePoolName VolumeGroupName/LogicalVolumeName
Verification
Verify that the LV is now cached:
# lvs -o lv_name,pool_lv LV Pool LogicalVolumeName [CachePoolName_cpool]
Additional resources
-
lvcreate(8)
,lvconvert(8)
,lvs(8)
man pages
5.2.2. Caching logical volumes with dm-writecache
When caching LVs with dm-writecache
, a caching layer between the logical volume and the physical storage device is created. dm-writecache
operates by temporarily storing write operations in a faster storage medium, such as an SSD, before eventually writing them back to the primary storage device, optimizing write-intensive workloads.
Prerequisites
- Administrative access.
Procedure
Display the logical volume you want to cache and its volume group:
# lvs -o lv_name,vg_name LV VG LogicalVolumeName VolumeGroupName
Create a cache volume:
# lvcreate --name CacheVolumeName --size Size VolumeGroupName /FastDevicePath
Replace CacheVolumeName with the name of the cache volume. Replace Size with the size for your cache pool. Replace VolumeGroupName with the name of the volume group. Replace /FastDevicePath with the path to your fast device, for example SSD or NVME.
Attach the cache volume to the LV:
# lvconvert --type writecache --cachevol CacheVolumeName VolumeGroupName/LogicalVolumeName
Replace CacheVolumeName with the name of the cache volume. Replace VolumeGroupName with the name of the volume group. Replace LogicalVolumeName with the name of the logical volume.
Verification
Verify that the LV is now cached:
# lvs -o lv_name,pool_lv LV Pool LogicalVolumeName [CacheVolumeName_cvol]
Additional resources
-
lvcreate(8)
,lvconvert(8)
,lvs(8)
man pages
5.2.3. Uncaching a logical volume
Use two main ways to remove caching from a LV.
- Splitting
- You can detach the cache from the LV but preserve the cache volume itself. In this case the LV will no longer benefit from the caching mechanism but the cache volume and its data will remain intact. While the cache volume is preserved, the data within the cache cannot be reused and will be erased the next time it is used in a caching setup.
- Uncaching
- You can detaches the cache from the LV and remove the cache volume entirely. This action effectively destroys the cache, freeing up the space.
Prerequisites
- Administrative access.
Procedure
Display the cached LV:
# lvs -o lv_name,pool_lv,vg_name LV Pool VG LogicalVolumeName [CacheVolumeName_cvol] VolumeGroupName
Detach or remove the cached volume:
To detach the cached volume, use:
# lvconvert --splitcache VolumeGroupName/LogicalVolumeName
To detach and remove the cached volume, use:
# lvconvert --uncache VolumeGroupName/LogicalVolumeName
Replace VolumeGroupName with the name of the volume group. Replace LogicalVolumeName with the name of the logical volume.
Verification
Verify that the LV is not cached:
# lvs -o lv_name,pool_lv
Additional resources
-
lvconvert(8)
,lvs(8)
man pages
5.3. Creating a custom thin pool
You can create custom thin pools to have a better control over the storage.
Prerequisites
- Administrative access.
Procedure
Display available volume groups:
# vgs -o vg_name VG VolumeGroupName
List available devices:
# lsblk
Create a LV to hold the thin pool data:
# lvcreate --name ThinPoolDataName --size Size VolumeGroupName /DevicePath
Replace ThinPoolDataName with the name for your thin pool data LV. Replace Size with the size for your LV. Replace VolumeGroupName with the name of your volume group.
Create a LV to hold the thin pool metadata:
# lvcreate --name ThinPoolMetadataName --size Size VolumeGroupName /DevicePath
Combine the LVs into a thin pool:
# lvconvert --type thin-pool --poolmetadata ThinPoolMetadataName VolumeGroupName/ThinPoolDataName
Verification
Verify that the custom thin pool is created:
# lvs -o lv_name,seg_type LV Type ThinPoolDataName thin-pool
Additional resources
-
The
vgs(8)
lvs(8)
,lvcreate(8)
man pages
5.4. Creating a custom VDO logical volume
With Logical Volume Manager (LVM), you can create a custom LV that uses Virtual Data Optimizer (VDO) pool for data storage.
Prerequisites
- Administrative access.
Procedure
Display the VGs:
# vgs VG #PV #LV #SN Attr VSize VFree VolumeGroupName 1 0 0 wz--n- 28.87g 28.87g
Create a LV to be converted to a VDO pool:
# lvcreate --name VDOPoolName --size Size VolumeGroupName
Replace VDOPoolName with the name for your VDO pool. Replace Size with the size for your VDO pool. Replace VolumeGroupName with the name of the VG.
Convert this LV to a VDO pool. In this conversion, you are creating a new VDO LV that uses the VDO pool. Because
lvcreate
is creating a new VDO LV, you must specify parameters for the new VDO LV. Use--name|-n
to specify the name of the new VDO LV, and--virtualsize|-V
to specify the size of the new VDO LV.# lvconvert --type vdo-pool --name VDOVolumeName --virtualsize VDOVolumeSize VolumeGroupName/VDOPoolName
Replace VDOVolumeName with the name for your VDO volume. Replace VDOVolumeSize with the size for your VDO volume. Replace VolumeGroupName/VDOPoolName with the names for your VG and your VDO pool.
Verification
Verify that the LV is converted to the VDO pool:
*# lvs -o lv_name,vg_name,seg_type* LV VG Type VDOPoolName VolumeGroupName vdo-pool VDOVolumeName VolumeGroupName vdo
Additional resources
-
The
vgs(8)
,lvs(8)
,lvconvert(8)
man pages