Bookmark this page

Chapter 10.  Manage Storage Stack

Abstract

Goal

Create and manage logical volumes that contain file systems or swap spaces from the command line.

Objectives
  • Describe logical volume manager components and concepts, and implement LVM storage and display LVM component information.

Sections
  • Create and Extend Logical Volumes (and Guided Exercise)

Lab
  • Manage Storage Stack

Create and Extend Logical Volumes

Objectives

  • Describe logical volume manager components and concepts, and implement LVM storage and display LVM component information.

Logical Volume Manager Overview

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.

Logical Volume Manager Workflow

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.

Figure 10.1: Logical Volume Manager workflow

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.

Build LVM Storage

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.

Prepare Physical Devices

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

Create Physical Volumes

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.devices

Create a Volume Group

The 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 created

Create a Logical Volume

The 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.

Create a Logical Volume with Deduplication and Compression

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-kvdo

Verify 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.

Create a File System on the Logical Volume

Specify the logical volume by using either the /dev/vgname/lvname traditional name, or the /dev/mapper/vgname-lvname kernel device mapper name.

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/data

To 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.

Display LVM Component Status

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.

Display Physical Volume Information

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/vdb1                         1
  VG Name               vg01                              2
  PV Size               731.98 MiB / not usable 3.98 MiB  3
  Allocatable           yes
  PE Size               4.00 MiB                          4
  Total PE              182
  Free PE               107                               5
  Allocated PE          75
  PV UUID               zP0gD9-NxTV-Qtoi-yfQD-TGpL-0Yj0-wExh2N

1

PV Name shows the device name.

2

VG Name shows the volume group where the PV is allocated.

3

PV Size shows the physical size of the PV, including unusable space.

4

PE Size shows the physical extent size.

5

Free PE shows the PE size available in the VG to create new LVs or extend existing LVs.

Display Volume Group Information

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               vg01                1
  System 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         2
  PE Size               4.00 MiB
  Total PE              253                 3
  Alloc PE / Size       75 / 300.00 MiB
  Free  PE / Size       178 / 712.00 MiB    4
  VG UUID               jK5M1M-Yvlk-kxU2-bxmS-dNjQ-Bs3L-DRlJNc

1

VG Name shows the name of the volume group.

2

VG Size displays the total size of the storage pool that is available for LV allocation.

3

Total PE shows the total size of PE units.

4

Free PE / Size shows the available space in the VG to create or extend LVs.

Display Logical Volume Information

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/lv01         1
  LV Name                lv01
  VG Name                vg01                   2
  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             3
  Current LE             75                     4
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

1

LV Path shows the device name of the LV.

2

VG Name shows the VG that is used for creating this LV.

3

LV Size shows the total size of the LV. Use the file-system tools to determine the free and used space for the LV.

4

Current LE shows the number of logical extents that this LV uses.

Extend and Reduce LVM Storage

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.

Extend a Volume Group Size

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/vdb3
  Physical 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 extended

This command extends the vg01 VG by the /dev/vdb3 PV size.

Extend a Logical Volume 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.

Extend an XFS File System to the Logical Volume Size

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 204800

Important

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.

Extend an EXT4 File System to the Logical Volume Size

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.

Extend Swap Space Logical Volumes

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/swap

Use 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-7f40634ad660

Use the swapon command to activate the swap space on the LV.

[root@host ~]# swapon /dev/vg01/swap

Reduce Volume Group Storage

Reducing 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/vdb3

Warning

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.

Remove LVM Storage

Use the lvremove, vgremove, and pvremove commands to remove an LVM component that is no longer required.

Prepare the File System

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/data

Warning

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.

Remove the Logical Volume

Use the lvremove DEVICE-NAME 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.

[root@host ~]# lvremove /dev/vg01/lv01
Do you really want to remove active logical volume vg01/lv01? [y/n]: y
  Logical volume "lv01" successfully removed.

The LV's physical extents are freed and available to assign to existing or new LVs in the volume group.

Remove the Volume Group

Use the vgremove VG-NAME command to remove a volume group that is no longer needed.

[root@host ~]# vgremove vg01
  Volume group "vg01" successfully removed

The VG's physical volumes are freed and available to assign to existing or new VGs on the system.

Remove the Physical Volumes

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

Revision: rh199-9.3-8dd73db