In the previous chapter, we looked at adding a new disk drive to a CentOS Stream 9 system, creating a partition and file system, and then mounting that file system to access the disk. An alternative to creating fixed partitions and file systems is to use Logical Volume Management (LVM) to create logical disks comprising space from one or more physical or virtual disks or partitions. The advantage of using LVM is that space can be added to or removed from logical volumes without spreading data over multiple file systems.
Let us take, for example, the home (/home) file system of a CentOS Stream 9-based server. Without LVM, this file system would be created with a specific size when the operating system is installed. If a new disk drive is installed, there is no way to allocate any of that space to the /home file system. The only option would be to create new file systems on the new disk and mount them at particular mount points. In this scenario, you would have plenty of space on the new file system, but the / home file system would still be nearly full. The only option would be to move files onto the new file system. With LVM, the new disk (or part thereof) can be assigned to the logical volume containing the home file system, thereby dynamically extending the space available.
In this chapter, we will look at the steps necessary to add new disk space to both a volume group and a logical volume to add additional space to the home file system of a CentOS Stream 9 system.
An Overview of Logical Volume Management (LVM)
LVM provides a flexible and high-level approach to managing disk space. Instead of each disk drive being split into partitions of fixed sizes onto which fixed-size file systems are created, LVM provides a way to group disk space into logical volumes that can be easily resized and moved. In addition, LVM allows administrators to carefully control disk space assigned to different groups of users by allocating distinct volume groups or logical volumes to those users. When the space initially allocated to the volume is exhausted, the administrator can add more space without moving the user files to a different file system. LVM consists of the following components:
Volume Group (VG)
The Volume Group is the high-level container with one or more logical and physical volumes.
Physical Volume (PV)
A physical volume represents a storage device such as a disk drive or other storage media. 32.1.3 Logical Volume (LV)
A logical volume is equivalent to a disk partition and, as with a disk partition, can contain a file system.
Physical Extent (PE)
Each physical volume (PV) is divided into equal size blocks known as physical extents.
Logical Extent (LE)
Each logical volume (LV) is divided into equal size blocks called logical extents.
Suppose we are creating a new volume group called VolGroup001. This volume group needs physical disk space to function, so we allocate three disk partitions /dev/sda1, /dev/sdb1, and /dev/ sdb2. These become physical volumes in VolGroup001. We would then create a logical volume called LogVol001 within the volume group comprising the three physical volumes.
If we run out of space in LogVol001, we add more disk partitions as physical volumes and assign them to the volume group and logical volume.
Getting Information about Logical Volumes
As an example of using LVM with CentOS Stream 9, we will work through an example of adding space to the /home file system of a standard CentOS Stream 9 installation. Anticipating the need for flexibility in the sizing of the home partition, CentOS Stream 9 sets up the /home file system as a logical volume (called home) within a volume group called cs. Before making any changes to the LVM setup, however, it is essential first to gather information.
Running the mount command will output information about a range of mount points, including the following entry for the home filesystem:
/dev/mapper/cs-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)Code language: plaintext (plaintext)
Information about the volume group can be obtained using the vgdisplay command:
# vgdisplay --- Volume group --- VG Name cs System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 3 Max PV 0 Cur PV 1 Act PV 1 VG Size <297.09 GiB PE Size 4.00 MiB Total PE 76054 Alloc PE / Size 76054 / <297.09 GiB Free PE / Size 0 / 0 VG UUID Rhc20G-MpSC-o14e-27re-vMLW-qneF-uVkfSOCode language: plaintext (plaintext)
As we can see in the above example, the cs volume group has a physical extent size of 4.00MiB and has a total of 297.09GB available for allocation to logical volumes. Currently, 76054 physical extents are allocated, equaling the total capacity. Therefore, we must add one or more physical volumes to increase the space allocated to any logical volumes in the cs volume group. The vgs tool is also helpful for displaying a quick overview of the space available in the volume groups on a system:
# vgs VG #PV #LV #SN Attr VSize VFree cs 1 3 0 wz--n- <297.09g 0Code language: plaintext (plaintext)
Information about logical volumes in a volume group may similarly be obtained using the lvdisplay command:
# lvdisplay --- Logical volume --- LV Path /dev/cs/swap LV Name swap VG Name cs LV UUID cKtGBw-s1Qq-Gl3a-nl6d-DPZA-2uGs-D9Tqnz LV Write Access read/write LV Creation host, time centos-server, 2023-06-05 10:42:22 -0400 LV Status available # open 2 LV Size 3.75 GiB Current LE 961 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Path /dev/cs/home LV Name home VG Name cs LV UUID 7EXkIu-MHR9-wbt8-8jLJ-8u2p-hhu2-RWcGMm LV Write Access read/write LV Creation host, time centos-server, 2023-06-05 10:42:22 -0400 LV Status available # open 1 LV Size 223.33 GiB Current LE 57173 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Logical volume --- LV Path /dev/cs/root LV Name root VG Name cs LV UUID QTaGmY-rew0-7ZEH-kOlp-NnSw-e7SL-KCoswy LV Write Access read/write LV Creation host, time centos-server, 2023-06-05 10:42:24 -0400 LV Status available # open 1 LV Size 70.00 GiB Current LE 17920 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0Code language: plaintext (plaintext)
As shown in the above example, 70 GiB of the space in volume group cs is allocated to logical volume root (for the / file system), approximately 223.34 GiB to the home volume group (for / home), and 3.75 GiB to swap (for swap space).
Now that we know what space is being used, it is often helpful to understand which devices are providing the space (in other words, which devices are being used as physical volumes). To obtain this information, we need to run the pvdisplay command:
# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name cs PV Size <297.09 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 76054 Free PE 0 Allocated PE 76054 PV UUID bHWRHo-0MED-XEtL-WNXW-4hje-vYxa-NPSIOcCode language: plaintext (plaintext)
Clearly, the space controlled by logical volume cs is provided via a physical volume located on / dev/sda2.
Now that we know more about our LVM configuration, we can add space to the volume group and the logical volume contained within.
Adding Additional Space to a Volume Group from the Command-Line
Just as with the previous steps to gather information about the current Logical Volume Management configuration of a CentOS 9 system, changes to this configuration can be made from the command line.
In the remainder of this chapter, we will assume that a new disk has been added to the system and that the operating system sees it as /dev/sdb. We shall also assume this is a new disk with no existing partitions. If existing partitions are present, they should be backed up, and then the partitions should be deleted from the disk using the fdisk utility. For example, assuming a device represented by /dev/sdb containing two partitions as follows:
# fdisk -l /dev/sdb Disk /dev/sdb: 14.46 GiB, 15525216256 bytes, 30322688 sectors Disk model: USB 2.0 FD Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x4c33060b Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 30322687 30320640 14.5G 83 LinuxCode language: plaintext (plaintext)
Once any filesystems on these partitions have been unmounted, they can be deleted as follows:
# fdisk /dev/sdb Welcome to fdisk (util-linux 2.37.4). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): d Selected partition 1 Partition 1 has been deleted. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.Code language: plaintext (plaintext)
Before moving to the next step, remove any entries in the /etc/fstab file for these filesystems so that the system does not attempt to mount them on the next reboot.
Once the disk is ready, the next step is to convert this disk into a physical volume using the pvcreate command (also wiping the dos signature if one exists):
# pvcreate /dev/sdb WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y Wiping dos signature on /dev/sdb. Physical volume "/dev/sdb" successfully created.Code language: plaintext (plaintext)
If the creation fails with a message that reads “Device /dev/<device> excluded by a filter”, it may be necessary to wipe the disk using the wipefs command before creating the physical volume:
# wipefs -a /dev/sdb /dev/sdb: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54 /dev/sdb: 8 bytes were erased at offset 0x1fffffe00 (gpt): 45 46 49 20 50 41 52 54 /dev/sdb: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa /dev/sdb: calling ioctl to re-read partition table: SuccessCode language: plaintext (plaintext)
With the physical volume created, we now need to add it to the volume group (in this case, cs) using the vgextend command:
The new physical volume has now been added to the volume group and is ready to be allocated to a logical volume. To do this, we run the lvextend tool providing the size by which we wish to extend the volume. In this case, we want to extend the size of the home logical volume by 14 GB. Note that we need to provide the path to the logical volume, which can be obtained from the lvdisplay command (in this case, /dev/cs/home):
# lvextend -L+14G /dev/cs/home Size of logical volume cs/home changed from <223.34 GiB (57174 extents) to <237.34 GiB (60758 extents). Logical volume cs/home successfully resized.Code language: plaintext (plaintext)
The last step is to resize the file system residing on the logical volume to use the additional space. The way this is performed will depend on the filesystem type, which can be identified using the following df command and checking the Type column:
# df -T /home Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/mapper/cs-home xfs 234070356 3345116 230725240 2% /homeCode language: plaintext (plaintext)
If /home is formatted using the XFS filesystem, it can be resized using the xfs_growfs utility:
# xfs_growfs /home meta-data=/dev/mapper/cs-home isize=512 agcount=4, agsize=14636544 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 bigtime=1 inobtcount=1 data = bsize=4096 blocks=58546176, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=28587, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 58546176 to 62216192Code language: plaintext (plaintext)
If, on the other hand, the filesystem is of type ext2, ext3, or ext4, the resize2fs utility should be used instead when performing the filesystem resize:
# resize2fs /dev/cs/homeCode language: plaintext (plaintext)
Once the resize completes, the file system will have been extended to use the additional space provided by the new disk drive. All this has been achieved without moving a single file or restarting the server. As far as users on the system are concerned, nothing has changed (except that there is now more disk space).
Adding Additional Space to a Volume Group Using Cockpit
In addition to the command-line utilities outlined so far in this chapter, it is also possible to access information about logical volumes and make volume group and logical volume changes from within the Cockpit web interface using the Storage page, as shown in Figure 32-1:
If the Storage option is not listed, the cockpit-storaged package will need to be installed, and the cockpit service restarted as follows:
# dnf install cockpit-storaged # systemctl restart cockpit.socketCode language: plaintext (plaintext)
Once the Cockpit service has restarted, log back into the Cockpit interface, at which point the Storage option should now be visible.
To add a new disk drive to an existing volume group from within the Cockpit console, start at the above Storage page and click on a filesystem associated with the volume group to be extended from the list marked A above.
On the resulting screen, click on the + button highlighted in Figure 32-2 below to add a physical volume:
Select the new drive to be added to the volume group and click on the Add button:
On returning to the volume group screen, scroll down to the logical volume to be extended and click on it to unfold additional information. Figure 32-4, for example, shows details of the home logical volume:
To extend the logical volume using the new space, click the Grow button and use the slider in the resulting dialog to select how much space should be added to the volume. Then, click the Grow button to commit the change (the available space can be shared among different volume groups if required):
Once these steps are complete, the volume group will have been configured to use the newly added space.
Volume groups and logical volumes provide an abstract layer on top of the physical storage devices on a CentOS Stream 9 system to provide a flexible way to allocate the space provided by multiple disk drives. This allows disk space allocations to be made and changed dynamically without the need to repartition disk drives and move data between filesystems. This chapter has outlined the basic concepts of volume groups and logical and physical volumes while demonstrating how to manage these using command-line tools and the Cockpit web interface.