Red Hat System Administration II
Abstract
| Goal |
Create and manage logical volumes that contain file systems or swap spaces from the command line. |
| Objectives |
|
| Sections |
|
| Lab |
|
Describe logical volume manager components and concepts, and implement LVM storage and display LVM component information.
Use the Logical Volume Manager (LVM) system to create logical storage volumes as a layer on the physical storage. This storage system provides greater flexibility than using physical storage directly. LVM hides the hardware storage configuration from the software, and enables you to resize volumes without stopping applications or unmounting file systems. LVM provides comprehensive command-line tools to manage storage.
- Physical devices
Logical volumes use physical devices for storing data. These devices might be disk partitions, whole disks, RAID arrays, or SAN disks. You must initialize the device as an LVM physical volume. An LVM physical volume must use the entire physical device.
- Physical Volumes (PVs)
LVM uses the underlying physical device as the LVM physical volume. LVM tools segment the physical volumes into Physical Extents (PEs) to form small chunks of data that act as the smallest storage block on a PV.
- Volume Groups (VGs)
Volume groups are storage pools that are made from one or more PVs. It is the functional equivalent of a whole disk in physical storage. A PV must be allocated only to a single VG. LVM sets the PE size automatically, although it is possible to specify it. A VG might consist of unused space and several logical volumes.
- Logical Volumes (LVs)
Logical volumes are created from free physical extents in a VG, and are provided as the storage device for applications, users, and operating systems. LVs are a collection of Logical Extents (LEs), which map to physical extents. By default, each LE gets mapped to one PE. Setting specific LV options changes this mapping; for example, mirroring causes each LE to map to two PEs.
Creating LVM storage requires building structures in a logical workflow.
Determine the physical devices that are used for creating physical volumes, and initialize these devices as LVM physical volumes.
Create a volume group from multiple physical volumes.
Create the logical volumes from the available space in the volume group.
Format the logical volume with a file system and mount it, or activate it as swap space, or pass the raw volume to a database or storage server for advanced structures.
Note
The examples here use a /dev/vdb device name and its storage partitions. The device names on your classroom system might be different. Use the lsblk, blkid, or cat /proc/partitions commands to identify your system's devices.
Creating a logical volume involves creating physical device partitions, physical volumes, and volume groups. After creating an LV, format the volume and mount it to access it as storage.
Partitioning is optional when already present. Use the parted command to create a partition on the physical device. Set the physical device to the Linux LVM partition type. Use the udevadm settle command to register the new partition with the kernel.
[root@host ~]#parted /dev/vdb mklabel gpt mkpart primary 1MiB 769MiB...output omitted... [root@host ~]#parted /dev/vdb mkpart primary 770MiB 1026MiB[root@host ~]#parted /dev/vdb set 1 lvm on[root@host ~]#parted /dev/vdb set 2 lvm on[root@host ~]#udevadm settle
Use the pvcreate command to label the physical partition as an LVM physical volume. Label multiple devices simultaneously by using space-delimited device names as arguments to the pvcreate command. This example labels the /dev/vdb1 and /dev/vdb2 devices as PVs that are ready for creating volume groups.
[root@host ~]# pvcreate /dev/vdb1 /dev/vdb2
Physical volume "/dev/vdb1" successfully created.
Physical volume "/dev/vdb2" successfully created.
Creating devices file /etc/lvm/devices/system.devicesThe vgcreate command builds one or more physical volumes into a volume group. The first argument is a volume group name, followed by one or more physical volumes to allocate to this VG. This example creates the vg01 VG by using the /dev/vdb1 and /dev/vdb2 PVs.
[root@host ~]# vgcreate vg01 /dev/vdb1 /dev/vdb2
Volume group "vg01" successfully createdThe lvcreate command creates a logical volume from the available PEs in a volume group. Use the lvcreate command to set the LV name and size, and the VG name to contain this logical volume. This example creates the lv01 LV with 300 MiB in the vg01 VG.
[root@host ~]# lvcreate -n lv01 -L 300M vg01
Logical volume "lv01" created.This command might fail if the volume group does not have enough free physical extents. The LV size rounds up to the next PE size value when the size does not exactly match.
The lvcreate command -L option requires sizes in bytes, mebibytes (binary megabytes, 1048576 bytes), and gibibytes (binary gigabytes), or similar. The lowercase -l requires sizes that are specified as a number of physical extents. The following commands are two choices for creating the same LV with the same size:
lvcreate -n lv01 -L 128M vg01: create an LV of size 128 MiB, rounded to the next PE.lvcreate -n lv01 -l 32 vg01: create an LV of size 32 PEs at 4 MiB each, total 128 MiB.
RHEL 9 uses an LVM Virtual Data Optimizer (VDO) implementation for managing VDO volumes. The previous Python-based VDO management tools are still available but are no longer needed.
The VDO provides inline block-level deduplication, compression, and thin provisioning for storage. Configure a VDO volume to use up to 256 TB of physical storage. Manage VDO as a type of LVM logical volume (LVs), similar to LVM thinly provisioned volumes. An LVM VDO is composed of two logical volumes:
- VDO pool LV
This LV stores, deduplicates, compresses data, and sets the size of the VDO volume that is backed by the physical device. VDO is deduplicated and compresses each VDO LV separately, because each VDO pool LV can hold only one VDO LV.
- VDO LV
A virtual device is provisioned on top of the VDO pool LV, and sets the logical size of the VDO volume to store the data before deduplication and compression occur.
LVM VDO presents the deduplicated storage as a regular logical volume (LV). The VDO volume can be formatted with standard file systems, or shared as a block device, or used to build other storage layers, the same as any normal logical volume.
To use VDO deduplication and compression, install the vdo and kmod-kvdo packages.
[root@host ~]# dnf install vdo kmod-kvdoVerify that the selected LVM volume group has enough free storage capacity. Use the lvcreate command with the --type vdo parameter to create a VDO LV.
[root@host ~]# lvcreate --type vdo --name vdo-lv01 --size 5G vg01
Logical blocks defaulted to 523108 blocks.
The VDO volume can address 2 GB in 1 data slab.
It can grow to address at most 16 TB of physical storage in 8192 slabs.
If a larger maximum size might be needed, use bigger slabs.
Logical volume "vdo-lv01" created.Specify the logical volume by using either the /dev/ traditional name, or the vgname/lvname/dev/mapper/ kernel device mapper name.vgname-lvname
Use the mkfs command to create a file system on the new logical volume.
[root@host ~]# mkfs -t xfs /dev/vg01/vdo-lv01
...output omitted...Create a mount point by using the mkdir command.
[root@host ~]# mkdir /mnt/dataTo make the file system available persistently, add an entry to the /etc/fstab file.
/dev/vg01/vdo-lv01 /mnt/data xfs defaults 0 0
Mount the LV by using the mount command.
[root@host ~]# mount /mnt/data/Note
You can mount a logical volume by name or by UUID, because LVM parses the PVs by looking for the UUID. This behavior is successful even when the VG was created by using a name, because the PV always contains a UUID.
LVM provides various utilities to display the status information of PV, VG, and LV. Use the pvdisplay, vgdisplay, and lvdisplay commands to show the status information of the LVM components.
The associated pvs, vgs, and lvs commands are commonly used and show a subset of the status information, with one line for each entity.
The pvdisplay command displays information about the PVs. Use the command without arguments to list information of all PVs on the system. Providing the name of the PV as the argument to the command shows the information that is specific to the PV.
[root@host ~]#pvdisplay /dev/vdb1--- Physical volume --- PV Name /dev/vdb1VG Name vg01
PV Size 731.98 MiB / not usable 3.98 MiB
Allocatable yes PE Size 4.00 MiB
Total PE 182 Free PE 107
Allocated PE 75 PV UUID zP0gD9-NxTV-Qtoi-yfQD-TGpL-0Yj0-wExh2N
The vgdisplay command shows the information about volume groups. To list information about all VGs, use the command without arguments. Provide the name of the VG as an argument to list information that is specific to the VG.
[root@host ~]#vgdisplay vg01--- Volume group --- VG Name vg01System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 1012.00 MiB
PE Size 4.00 MiB Total PE 253
Alloc PE / Size 75 / 300.00 MiB Free PE / Size 178 / 712.00 MiB
VG UUID jK5M1M-Yvlk-kxU2-bxmS-dNjQ-Bs3L-DRlJNc
The lvdisplay command displays information about logical volumes. Use the command without arguments to list information of all LVs. Providing the name of the LV as an argument displays the information that is specific to the LV.
[root@host ~]#lvdisplay /dev/vg01/lv01--- Logical volume --- LV Path /dev/vg01/lv01LV Name lv01 VG Name vg01
LV UUID FVmNel-u25R-dt3p-C5L6-VP2w-QRNP-scqrbq LV Write Access read/write LV Creation host, time servera.lab.example.com, 2022-04-07 10:45:34 -0400 LV Status available # open 1 LV Size 300.00 MiB
Current LE 75
Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
After creating a logical volume, you can extend the volume to expand the file system. You might need to extend PV or VG to increase the storage capacity of the LV.
You might need to add more disk space to extend a VG. You can add physical volumes to a VG to extend its available size.
Prepare the physical device and create the physical volume when not present.
[root@host ~]#parted /dev/vdb mkpart primary 1072MiB 1648MiB...output omitted... [root@host ~]#parted /dev/vdb set 3 lvm on...output omitted... [root@host ~]#udevadm settle[root@host ~]#pvcreate /dev/vdb3Physical volume "/dev/vdb3" successfully created.
The vgextend command adds the new PV to the VG. Provide the VG and PV names as arguments to the vgextend command.
[root@host ~]# vgextend vg01 /dev/vdb3
Volume group "vg01" successfully extendedThis command extends the vg01 VG by the /dev/vdb3 PV size.
A benefit of using logical volumes is increasing their size without experiencing any downtime. Add free physical extents to the LV from the VG, to extend the LV capacity and to expand the file system. Use the vgdisplay command to confirm that the volume group has enough free space for the LV extension.
Use the lvextend command to extend the LV.
[root@host ~]# lvextend -L +500M /dev/vg01/lv01
Size of logical volume vg01/lv01 changed from 300.00 MiB (75 extents) to 800.00 MiB (200 extents).
Logical volume vg01/lv01 successfully resized.This command increases the size of the lv01 logical volume by 500 MiB. The (+) plus sign in front of the size means adding this value to the existing size; otherwise, without the plus sign, the value defines the final size of the LV.
The lvextend command -l option expects the number of PE as the argument. The lvextend command -L option expects sizes in bytes, mebibytes, gibibytes, and similar.
The xfs_growfs command helps to expand the file system to occupy the extended LV. The target file system must be mounted before you use the xfs_growfs command. You can continue to use the file system when resizing.
[root@host ~]# xfs_growfs /mnt/data/
...output omitted...
data blocks changed from 76800 to 204800Important
Always run the xfs_growfs command after executing the lvextend command. Use the lvextend command -r option to run the two steps consecutively. After extending the LV, resize the file system by using the fsadm command. This option supports several other file systems.
The steps for extending LV by using the ext4 file system are the same as for LV by using the XFS file system, except for the step to resize the file system.
The resize2fs command expands the file system to occupy the new extended LV. You can continue to use the file system when resizing.
[root@host ~]# resize2fs /dev/vg01/lv01
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/vg01/lv01 to 256000 (4k) blocks.
The filesystem on /dev/vg01/lv01 is now 256000 (4k) blocks long.The xfs_growfs command expands an XFS file system. This command typically takes the mount point as an argument, or otherwise can indirectly use a device name by mounting the device and then specifying the mount point. In contrast, the resize2fs command resizes ext2, ext3, or ext4 file systems and takes the device name as an argument. The xfs_growfs command supports only online resizing, whereas the resize2fs command supports both online and offline resizing. Although you can resize an ext4 file system up or down, you can resize an XFS file system only up.
You can extend the LVs that are used as swap space; however, the process differs from expanding the ext4 or XFS file system. Logical volumes that are used as swap space must be offline to extend them.
Use the swapoff command to deactivate the swap space on the LV.
[root@host ~]# swapoff -v /dev/vg01/swap
swapoff /dev/vg01/swapUse the lvextend command to extend the LV.
[root@host ~]# lvextend -L +300M /dev/vg01/swap
Size of logical volume vg01/swap changed from 500.00 MiB (125 extents) to 800.00 MiB (200 extents).
Logical volume vg01/swap successfully resized.Use the mkswap command to format the LV as a swap space.
[root@host ~]# mkswap /dev/vg01/swap
mkswap: /dev/vg01/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 800 MiB (838856704 bytes)
no label, UUID=25b4d602-6180-4b1c-974e-7f40634ad660Use the swapon command to activate the swap space on the LV.
[root@host ~]# swapon /dev/vg01/swapReducing a VG involves removing unused PVs from the VG. The pvmove command moves data from extents on one PV to extents on another PV with enough free extents in the same VG. You can continue to use the LV from the same VG when reducing. Use the pvmove command -A option to automatically back up the metadata of the VG after a change. This option uses the vgcfgbackup command to back up metadata automatically.
[root@host ~]# pvmove -A y /dev/vdb3Warning
Before using the pvmove command, back up the data that is stored on all LVs in the VG. An unexpected power loss during the operation might leave the VG in an inconsistent state, which might cause a loss of data on LVs.
Use the vgreduce command to remove a PV from a VG.
[root@host ~]# vgreduce vg01 /dev/vdb3
Removed "/dev/vdb3" from volume group "vg01"Important
The GFS2 and XFS file systems do not support shrinking, so you cannot reduce the size of an LV.
Use the lvremove, vgremove, and pvremove commands to remove an LVM component that is no longer required.
Move to another file system all data that must be kept. Use the umount command to unmount the file system, and then remove any /etc/fstab entries that are associated with this file system.
[root@host ~]# umount /mnt/dataWarning
Removing a logical volume destroys any data that is stored on the logical volume. Back up or move your data before you remove the logical volume.
Use the lvremove command to remove a logical volume that is no longer needed. Unmount the LV file system before running this command. The command prompts for confirmation before removing the LV.DEVICE-NAME
[root@host ~]#lvremove /dev/vg01/lv01Do you really want to remove active logical volume vg01/lv01? [y/n]:yLogical volume "lv01" successfully removed.
The LV's physical extents are freed and available to assign to existing or new LVs in the volume group.
Use the vgremove command to remove a volume group that is no longer needed.VG-NAME
[root@host ~]# vgremove vg01
Volume group "vg01" successfully removedThe VG's physical volumes are freed and available to assign to existing or new VGs on the system.
Use the pvremove command to remove physical volumes that are no longer needed. Use a space-delimited list of PV devices to remove more than one device at a time. This command deletes the PV metadata from the partition (or disk). The partition is now available for reallocation or reformatting.
[root@host ~]# pvremove /dev/vdb1 /dev/vdb2
Labels on physical volume "/dev/vdb1" successfully wiped.
Labels on physical volume "/dev/vdb2" successfully wiped.References
fdisk(8), gdisk(8), parted(8), partprobe(8), lvm(8), pvcreate(8), vgcreate(8), lvcreate(8), mkfs(8), pvdisplay(8), vgdisplay(8), lvdisplay(8), vgextend(8), lvextend(8), xfs_growfs(8), resize2fs(8) swapoff(8), mkswap(8), swapon(8), pvmove(8), vgcfgbackup(8), vgreduce(8), lvremove(8), vgremove(8), and pvremove(8) man pages
For further information, refer to Configuring and Managing Logical Volumes at https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/configuring_and_managing_logical_volumes/index