In the previous chapter, we looked at adding a new disk drive to an Ubuntu 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 root (/) file system of an Ubuntu-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 / 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 / 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 an Ubuntu 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.
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 Ubuntu, we will work through an example of adding space to the / file system of a standard Ubuntu installation. Anticipating the need for flexibility in the sizing of the root partition, Ubuntu sets up the / file system as a logical volume (called root) within a volume group called vgubuntu. 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/vgubuntu-root on / type ext4 (rw,relatime,errors=remount-ro)Code language: plaintext (plaintext)
Information about the volume group can be obtained using the vgdisplay command:
# vgdisplay --- Volume group --- VG Name vgubuntu System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 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 8 / 32.00 MiB VG UUID OdH8dC-jysL-JEei-Odks-KSJf-Uk0R-HhsYQ5Code language: plaintext (plaintext)
As we can see in the above example, the vgubuntu 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 vgubuntu 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 vgubuntu 1 2 0 wz--n- <297.09g 32.00mCode 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/vgubuntu/root LV Name root VG Name vgubuntu LV UUID TtXoiY-2Qx3-m1Uv-yNOL-Tpr0-oYkn-UsAKNB LV Write Access read/write LV Creation host, time ubuntu, 2023-06-14 15:37:05 -0400 LV Status available # open 1 LV Size <293.73 GiB Current LE 75194 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/vgubuntu/swap_1 LV Name swap_1 VG Name vgubuntu LV UUID CwNf3T-t0jx-C8Gs-l5Ft-kVcI-1q6f-lFFoKV LV Write Access read/write LV Creation host, time ubuntu, 2023-06-14 15:37:05 -0400 LV Status available # open 2 LV Size <3.83 GiB Current LE 980 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1Code language: plaintext (plaintext)
As shown in the above example, 293 GiB of the space in volume group vgubuntu is allocated to logical volume root (for the / file system), and 3.83 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 vgubuntu 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 vgubuntu 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 an Ubuntu 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:
Adding a New Disk to an Ubuntu 22.04 Volume Group and Logical Volume
# 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, vgubuntu) 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 root 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/vgubuntu/root):
# lvextend -L+14G /dev/vgubuntu/root Size of logical volume cs/home changed from <223.34 GiB (57174 extents) to <237.34 GiB (60758 extents). Logical volume vgubuntu/root 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 / Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/mapper/vgubuntu-root xfs 234070356 3345116 230725240 2% /Code language: plaintext (plaintext)
If / is formatted using the XFS filesystem, it can be resized using the xfs_growfs utility:
# xfs_growfs /Code 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/vgubuntu/rootCode 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).
Volume groups and logical volumes provide an abstract layer on top of the physical storage devices on an Ubuntu 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.