Chapter 4. Basic logical volume management With LVM, you can do the following tasks: Create new logical volumes to extend storage capabilities of your system Extend existing volumes and thin pools to accommodate growing data Rename volumes for better organization Reduce volumes to free up unused space Safely remove volumes when they are no longer needed Activate or deactivate volumes to control the system's access to its data 4.1. Overview of logical volume features With the Logical Volume Manager (LVM), you can manage disk storage in a flexible and efficient way that traditional partitioning schemes cannot offer. Below is a summary of key LVM features that are used for storage management and optimization. Concatenation Concatenation involves combining space from one or more physical volumes into a singular logical volume, effectively merging the physical storage. Striping Striping optimizes data I/O efficiency by distributing data across multiple physical volumes. This method enhances performance for sequential reads and writes by allowing parallel I/O operations. RAID LVM supports RAID levels 0, 1, 4, 5, 6, and 10. When you create a RAID logical volume, LVM creates a metadata subvolume that is one extent in size for every data or parity subvolume in the array. Thin provisioning Thin provisioning enables the creation of logical volumes that are larger than the available physical storage. With thin provisioning, the system dynamically allocates storage based on actual usage instead of allocating a predetermined amount upfront. Snapshots With LVM snapshots, you can create point-in-time copies of logical volumes. A snapshot starts empty. As changes occur on the original logical volume, the snapshot captures the pre-change states through copy-on-write (CoW), growing only with changes to preserve the state of the original logical volume. Caching LVM supports the use of fast block devices, such as SSD drives as write-back or write-through caches for larger slower block devices. Users can create cache logical volumes to improve the performance of their existing logical volumes or create new cache logical volumes composed of a small and fast device coupled with a large and slow device. 4.2. Creating logical volumes LVM provides a flexible approach to handling disk storage by abstracting the physical layer into logical volumes that can be created and adjusted based on your needs. 4.2.1. Creating a linear (thick) logical volume With linear logical volumes (LVs), you can merge multiple physical storage units into one virtual storage space. You can easily expand or reduce linear LVs to accommodate the data requirements. Prerequisites Administrative access. The lvm2 package is installed. The volume group is created. For more information, see Creating LVM volume group . Procedure List the names of volume groups and their size: Create a linear LV: Replace LogicalVolumeName with the name of the LV. Replace VolumeSize with the size for the LV. If no size suffix is provided the command defaults to MB. Replace VolumeGroupName with the name of the volume group. Verification Verify that the linear LV is created: Additional resources The vgs(8) , lvs(8) , lvcreate(8) man pages 4.2.2. Creating or resizing a logical volume by using the storage RHEL system role Use the storage role to perform the following tasks: To create an LVM logical volume in a volume group consisting of many disks To resize an existing file system on LVM To express an LVM volume size in percentage of the pool's total size If the volume group does not exist, the role creates it. If a logical volume exists in the volume group, it is resized if the size does not match what is specified in the playbook. If you are reducing a logical volume, to prevent data loss you must ensure that the file system on that logical volume is not using the space in the logical volume that is being reduced. Prerequisites You have prepared the control node and the managed nodes You are logged in to the control node as a user who can run playbooks on the managed nodes. The account you use to connect to the managed nodes has sudo permissions on them. Procedure Create a playbook file, for example ~/playbook.yml , with the following content: --- - name: Manage local storage hosts: managed-node-01.example.com tasks: - name: Create logical volume ansible.builtin.include_role: name: rhel-system-roles.storage vars: storage_pools: - name: myvg disks: - sda - sdb - sdc volumes: - name: mylv size: 2G fs_type: ext4 mount_point: /mnt/data The settings specified in the example playbook include the following: size: <size> You must specify the size by using units (for example, GiB) or percentage (for example, 60%). For details about all variables used in the playbook, see the /usr/share/ansible/roles/rhel-system-roles.storage/README.md file on the control node. Validate the playbook syntax: Note that this command only validates the syntax and does not protect against a wrong but valid configuration. Run the playbook: Verification Verify that specified volume has been created or resized to the requested size: Additional resources /usr/share/ansible/roles/rhel-system-roles.storage/README.md file /usr/share/doc/rhel-system-roles/storage/ directory 4.2.3. Creating a striped logical volume With striped logical volume (LV), you can distribute the data across multiple physical volumes (PVs), potentially increasing the read and write speed by utilizing the bandwidth of multiple disks simultaneously. When creating a striped LV, it is important to consider the stripe number and size. The stripe number is the count of PVs across which data is distributed. Increasing the stripe number can enhance performance by utilizing multiple disks concurrently. Stripe size is the size of the data chunk written to each disk in the stripe set before moving to the disk and is specified in kilobytes (KB). The optimal stripe size depends on your workload and the filesystem block size. The default is 64KB and can be adjusted. Prerequisites Administrative access. Procedure List the names of volume groups and their size: Create a striped LV: Replace NumberOfStripes with the number of stripes. Replace StripeSize with the stripe size in kilobytes. The --stripesize is not a required option. If you do not specify the stripe size it defaults to 64KB. Replace LogicalVolumeName with the name of the LV. Replace VolumeGroupName with the name of the volume group. Verification Verify that the striped LV is created: Additional resources The vgs(8) lvs(8) , lvcreate(8) man pages 4.2.4. Creating a RAID logical volume RAID logical volumes enable you to use multiple disks for redundancy and performance. LVM supports various RAID levels, including RAID0, RAID1, RAID4, RAID5, RAID6, and RAID10. With LVM you can create striped RAIDs (RAID0, RAID4, RAID5, RAID6), mirrored RAID (RAID1), or a combination of both (RAID10). RAID 4, RAID 5, and RAID 6 offer fault tolerance by storing parity data that can be used to reconstruct lost information in case of a disk failure. When creating RAID LVs, place each stripe on a separate PV. The number of stripes equals to the number of PVs that should be in the volume group (VG). Table 4.1. Minimal RAID configuration requirements RAID level Type Parity Minimum number of devices Minimum stripe number RAID0 Striping None 2 2 RAID1 Mirroring None 2 - RAID4 Striping Uses first device to store parity 3 2 RAID5 Striping Uses an extra device to store parity 3 2 RAID6 Striping Uses two extra devices to store parity 5 3 RAID10 Striping and mirroring None 4 2 Prerequisites Administrative access. Procedure List the names of volume groups and their size: Create a RAID LV: To create a striped raid, use: Replace level with the RAID level 0, 4, 5, or 6. Replace NumberOfStripes with the number of stripes. Replace StripeSize with the stripe size in kilobytes. Replace Size with the size of the LV. Replace LogicalVolumeName with the name of the LV. To create a mirrored RAID, use: Replace MirrorsNumber with the number of mirrors. Replace Size with the size of the LV. Replace LogicalVolumeName with the name of the LV. To create a mirrored and striped RAID, use: Replace MirrorsNumber with the number of mirrors. Replace NumberOfStripes with the number of stripes. Replace StripeSize with the stripe size in kilobytes. Replace Size with the size of the LV. Replace LogicalVolumeName with the name of the LV. Verification Verify that the RAID LV is created: Additional resources lvmraid(7) , vgs(8) , lvs(8) , lvcreate(8) man pages 4.2.5. Creating a thin logical volume Under thin provisioning, physical extents (PEs) from a volume group (VG) are allocated to create a thin pool with a specific physical size. Logical volumes (LVs) are then allocated from this thin pool based on a virtual size, not limited by the pool's physical capacity. With this, the virtual size of each thin LV can exceed the actual size of the thin pool leading to over-provisioning, when the collective virtual sizes of all thin LVs surpasses the physical capacity of the thin pool. Therefore, it is essential to monitor both logical and physical usage closely to avoid running out of space and outages. Thin provisioning optimizes storage efficiency by allocating space as needed, lowering initial costs and improving resource utilization. However, when using thin LVs, beware of the following drawbacks: Improper discard handling can block the release of unused storage space, causing full allocation of the space over time. Copy on Write (CoW) operation can be slower on file systems with snapshots. Data blocks can be intermixed between multiple file systems leading to random access limitations. 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 List the names of volume groups and their size: Create a thin pool: Replace PoolSize with the maximum amount of disk space the thin pool can use. Replace ThinPoolName with the name for the thin pool. Replace VolumeGroupName with the name of the volume group. Create a thin LV: Replace MaxVolumeSize with the maximum size the volume can grow to within the thin pool. Replace ThinPoolName with the name for the thin pool. Replace VolumeGroupName with the name of the volume group. Note You can create other thin LVs within the same thin pool. Verification Verify that the thin LV is created: Additional resources The lvs(8) , lvcreate(8) man pages 4.3. Resizing logical volumes With Logical Volume Manager (LVM), you can resize logical volumes (LVs) as needed without affecting the data stored on them. 4.3.1. Extending a linear logical volume You can extend linear (thick) LVs and their snapshots with the lvextend command. Prerequisites Administrative access. Procedure Ensure your volume group has enough space to extend your LV: Extend the linear LV and resize the file system: Replace AdditionalSize with how much space to add to the LV. The default unit of measurement is megabytes, but you can specify other units. Replace VolumeGroupName with the name of the volume group. Replace LogicalVolumeName with the name of the thin volume. Verification Verify that the linear LV is extended: 4.3.2. Extending a thin logical volume You can extend the thin logical volume (LV) with the lvextend command. Prerequisites Administrative access. Procedure Ensure the thin pool has enough space for the data you plan to add: Extend the thin LV and resize the file system: Replace AdditionalSize with how much space to add to the LV. The default unit of measurement is megabytes, but you can specify other units. Replace VolumeGroupName with the name of the volume group. Replace ThinVolumeName with the name of the thin volume. Verification Verify the thin LV is extended: 4.3.3. Extending a thin pool The virtual size of thin logical volumes can exceed the physical capacity of the thin pool resulting in over-provisioning. To prevent running out of space, you must monitor and periodically extend the capacity of the thin pool. The data_percent metric indicates the percentage of the allocated data space that the thin pool currently uses. The metadata_percent metric reflects the percentage of space used for storing metadata, which is essential for managing the mappings within the thin pool. Monitoring these metrics is vital to ensure efficient storage management and to avoid capacity issues. LVM provides the option to manually extend the data or metadata capacity as needed. Alternatively, you can enable monitoring and automate the expansion of your thin pool. 4.3.3.1. Manually extending a thin pool Logical Volume Manager (LVM) provides the option to manually extend the data segment, the metadata segment, or the thin pool. 4.3.3.1.1. Extending a thin pool You can use the lvextend command to extend the thin pool. Prerequisites Administrative access. Procedure Display the data and metadata space used: Extend the thin pool: Replace Size with the new size for your thin pool. Replace VolumeGroupName with the name of the volume group. Replace ThinPoolName with the name of the thin pool. The data size will be extended. The metadata size will be extended if necessary. Verification Verify that the thin pool is extended: Additional resources The lvs(8) , lvextend(8) man pages lvs -o help 4.3.3.1.2. Extending a thin pool data segment You can use the lvextend command to extend the data_percent segment. Prerequisites Administrative access. Procedure Display the data_percent segment: Extend the data_percent segment: Replace Size with the size for your data segment. Replace VolumeGroupName with name of the volume group. Replace ThinPoolName with the name of the thin pool. Verification Verify that the data_percent segment is extended: Additional resources The lvs(8) , lvextend(8) man pages lvs -o help 4.3.3.1.3. Extending a thin pool metadata segment You can use the lvextend command to extend the metadata_percent segment. Prerequisites Administrative access. Procedure Display the metadata_percent segment: Extend the metadata_percent segment: Replace Size with the size for your metadata segment. Replace VolumeGroupName with name of the volume group. Replace ThinPoolName with the name of the thin pool. Verification Verify that the metadata_percent segment is extended: Additional resources The lvs(8) , lvextend(8) man pages lvs -o help 4.3.3.2. Automatically extending a thin pool You can automate the expansion of your thin pool by enabling monitoring and setting the thin_pool_autoextend_threshold and the thin_pool_autoextend_percent configuration parameters. Prerequisites Administrative access. Procedure Check if the thin pool is monitored: Enable thin pool monitoring with the dmeventd daemon: Replace VolumeGroupName with the name of the volume group. Replace ThinPoolName with the name of the thin pool. As the root user, open the /etc/lvm/lvm.conf file in an editor of your choice. Uncomment the thin_pool_autoextend_threshold and thin_pool_autoextend_percent lines and set each parameter to a required value: thin_pool_autoextend_threshold determines the percentage at which LVM starts to auto-extend the thin pool. For example, setting it to 70 means LVM will try to extend the thin pool when it reaches 70% capacity. thin_pool_autoextend_percent specifies by what percentage the thin pool should be extended when it reaches threshold. For example, setting it to 20 means the thin pool will be increased by 20% of its current size. Save the changes and exit the editor. Restart the lvm2-monitor : Additional resources The lvs(8) , lvchange(8) , dmeventd(8) man pages 4.3.4. Shrinking logical volumes When the size of the LV is reduced, the freed up logical extents are returned to the volume group and then can be used by other LVs. Warning Data stored in the reduced area is lost. Always back up the data and resize the file system before proceeding. Prerequisites Administrative access. Procedure List the logical volumes and their volume groups: Check where the logical volume is mounted: Replace /dev/VolumeGroupName/LogicalVolumeName with the path to your logical volume. Unmount the logical volume: Replace /MountPoint with the mounting point for your logical volume. Check and repair any file system errors: Resize the LV and the file system: Replace TargetSize with the new size of the LV. Replace VolumeGroupName/LogicalVolumeName with the path to your logical volume. Remount the file system: Replace /MountPoint with the mounting point for your file system. Verification Verify the space usage of the file system: Replace /MountPoint with the mounting point for your logical volume. Verify the size of the LV: 4.4. Renaming logical volumes You can rename an existing logical volume, including snapshots, using the lvrename command. Prerequisites Administrative access. Procedure List the logical volumes and their volume groups: Rename the logical volume: Replace VolumeGroupName with name of the volume group. Replace LogicalVolumeName with the name of the logical volume. Replace NewLogicalVolumeName with the new logical volume name. Verification Verify that the logical volume is renamed: Additional resources lvrename(8) man page on your system 4.5. Removing logical volumes You can remove an existing logical volume, including snapshots, using the lvremove command. Prerequisites Administrative access. Procedure List the logical volumes and their paths: Check where the logical volume is mounted: Replace /dev/VolumeGroupName/LogicalVolumeName with the path to your logical volume. Unmount the logical volume: Replace /MountPoint with the mounting point for your logical volume. Remove the logical volume: Replace VolumeGroupName/LogicalVolumeName with the path to your logical volume. Additional resources lvs(8) , lvremove(8) man pages on your system 4.6. Activating logical volumes You can activate the logical volume with the lvchange command. Prerequisites Administrative access. Procedure List the logical volumes, their volume groups, and their paths: Activate the logical volume: Replace VolumeGroupName with the name of the volume group. Replace LogicalVolumeName with the name of the logical volume. Note When activating a thin LV that was created as a snapshot of another LV, you might need to use the --ignoreactivationskip option to activate it. Verification Verify that the LV is active: Replace VolumeGroupName with the name of the volume group. Replace LogicalVolumeName with the name of the logical volume. Additional resources The lvs(8) lvchange(8) , lvdisplay(8) man pages 4.7. Deactivating logical volumes By default, when you create a logical volume, it is in an active state. You can deactivate the logical volume with the lvchange command. Warning Deactivating a logical volume with active mounts or in use can lead to data inconsistencies and system errors. Prerequisites Administrative access. Procedure List the logical volumes, their volume groups, and their paths: Check where the logical volume is mounted: Replace /dev/VolumeGroupName/LogicalVolumeName with the path to your logical volume. Unmount the logical volume: Replace /MountPoint with the mounting point for your logical volume. Deactivate the logical volume: Replace VolumeGroupName with name of the volume group. Replace LogicalVolumeName with the name of the logical volume. Verification Verify that the LV is not active: Replace VolumeGroupName with name of the volume group. Replace LogicalVolumeName with the name of the logical volume. Additional resources The lvs(8) lvchange(8) , lvdisplay(8) man pages

Commands:
vgs -o vg_name,vg_size VG VSize VolumeGroupName 30.75g
lvcreate --name LogicalVolumeName --size VolumeSize VolumeGroupName
lvs -o lv_name,seg_type LV Type LogicalVolumeName linear
--- - name: Manage local storage hosts: managed-node-01.example.com tasks: - name: Create logical volume ansible.builtin.include_role: name: rhel-system-roles.storage vars: storage_pools: - name: myvg disks: - sda - sdb - sdc volumes: - name: mylv size: 2G fs_type: ext4 mount_point: /mnt/data
ansible-playbook --syntax-check ~/playbook.yml
ansible-playbook ~/playbook.yml
ansible managed-node-01.example.com -m command -a 'lvs myvg'
vgs -o vg_name,vg_size VG VSize VolumeGroupName 30.75g
lvcreate --stripes NumberOfStripes --stripesize StripeSize --size LogicalVolumeSize --name LogicalVolumeName VolumeGroupName
lvs -o lv_name,seg_type LV Type LogicalVolumeName striped
vgs -o vg_name,vg_size VG VSize VolumeGroupName 30.75g
lvcreate --type raid level --stripes NumberOfStripes --stripesize StripeSize --size Size --name LogicalVolumeName VolumeGroupName
lvcreate --type raid1 --mirrors MirrorsNumber --size Size --name LogicalVolumeName VolumeGroupName
lvcreate --type raid10 --mirrors MirrorsNumber --stripes NumberOfStripes --stripesize StripeSize --size Size --name LogicalVolumeName VolumeGroupName
lvs -o lv_name,seg_type LV Type LogicalVolumeName raid0
vgs -o vg_name,vg_size VG VSize VolumeGroupName 30.75g
lvcreate --type thin-pool --size PoolSize --name ThinPoolName VolumeGroupName
lvcreate --type thin --virtualsize MaxVolumeSize --name ThinVolumeName --thinpool ThinPoolName VolumeGroupName
lvs -o lv_name,seg_type LV Type ThinPoolName thin-pool ThinVolumeName thin
lvs -o lv_name,lv_size,vg_name,vg_size,vg_free LV LSize VG VSize VFree LogicalVolumeName 1.49g VolumeGroupName 30.75g 29.11g
lvextend --size + AdditionalSize --resizefs VolumeGroupName / LogicalVolumeName
lvs -o lv_name,lv_size LV LSize NewLogicalVolumeName 6.49g
lvs -o lv_name,lv_size,data_percent LV LSize Data% MyThinPool 20.10g 3.21 ThinVolumeName 1.10g 4.88
lvextend --size + AdditionalSize --resizefs VolumeGroupName / ThinVolumeName
lvs -o lv_name,lv_size,data_percent LV LSize Data% MyThinPool 20.10g 3.21 ThinVolumeName 6.10g 0.43
lvs -o lv_name,seg_type,data_percent,metadata_percent LV Type Data% Meta% ThinPoolName thin-pool 97.66 26.86 ThinVolumeName thin 48.80
lvextend -L Size VolumeGroupName/ThinPoolName
lvs -o lv_name,seg_type,data_percent,metadata_percent LV Type Data% Meta% ThinPoolName thin-pool 24.41 16.93 ThinVolumeName thin 24.41
lvs -o lv_name,seg_type,data_percent LV Type Data% ThinPoolName thin-pool 93.87
lvextend -L Size VolumeGroupName/ThinPoolName _tdata
lvs -o lv_name,seg_type,data_percent LV Type Data% ThinPoolName thin-pool 40.23
lvs -o lv_name,seg_type,metadata_percent LV Type Meta% ThinPoolName thin-pool 75.00
lvextend -L Size VolumeGroupName/ThinPoolName _tmeta
lvs -o lv_name,seg_type,metadata_percent LV Type Meta% ThinPoolName thin-pool 0.19
lvs -o lv_name,vg_name,seg_monitor LV VG Monitor ThinPoolName VolumeGroupName not monitored
lvchange --monitor y VolumeGroupName/ThinPoolName
thin_pool_autoextend_threshold = 70 thin_pool_autoextend_percent = 20
systemctl restart lvm2-monitor
lvs -o lv_name,vg_name,lv_size LV VG LSize LogicalVolumeName VolumeGroupName 6.49g
findmnt -o SOURCE,TARGET /dev/VolumeGroupName/LogicalVolumeName SOURCE TARGET /dev/mapper/VolumeGroupName-NewLogicalVolumeName /MountPoint
umount /MountPoint
e2fsck -f /dev/VolumeGroupName/LogicalVolumeName
lvreduce --size TargetSize --resizefs VolumeGroupName/LogicalVolumeName
mount -o remount /MountPoint
df -hT /MountPoint/ Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolumeGroupName-NewLogicalVolumeName ext4 2.9G 139K 2.7G 1% /MountPoint
lvs -o lv_name,lv_size LV LSize NewLogicalVolumeName 4.00g
lvs -o lv_name,vg_name LV VG LogicalVolumeName VolumeGroupName
lvrename VolumeGroupName/LogicalVolumeName VolumeGroupName/NewLogicalVolumeName
lvs -o lv_name LV NewLogicalVolumeName
lvs -o lv_name,lv_path LV Path LogicalVolumeName /dev/VolumeGroupName/LogicalVolumeName
findmnt -o SOURCE,TARGET /dev/VolumeGroupName/LogicalVolumeName SOURCE TARGET /dev/mapper/VolumeGroupName-LogicalVolumeName /MountPoint
umount /MountPoint
lvremove VolumeGroupName/LogicalVolumeName
lvs -o lv_name,vg_name,lv_path LV VG Path LogicalVolumeName VolumeGroupName VolumeGroupName/LogicalVolumeName
lvchange --activate y VolumeGroupName / LogicalVolumeName
lvdisplay VolumeGroupName / LogicalVolumeName LV Status available
lvs -o lv_name,vg_name,lv_path LV VG Path LogicalVolumeName VolumeGroupName /dev/VolumeGroupName/LogicalVolumeName
findmnt -o SOURCE,TARGET /dev/VolumeGroupName/LogicalVolumeName SOURCE TARGET /dev/mapper/VolumeGroupName-LogicalVolumeName /MountPoint
umount /MountPoint
lvchange --activate n VolumeGroupName / LogicalVolumeName
lvdisplay VolumeGroupName/LogicalVolumeName LV Status NOT available