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 so that the disk can be accessed. An alternative to creating fixed partitions and file systems is to use Logical Volume Management (LVM) to create logical disks made up of 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 as needed without the need to spread 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 certain 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 root 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 for the purpose of adding additional space to the root file system of an Ubuntu system.
1.1 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 together disk space into logical volumes which 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 simply add more space without having to move the user files to a different file system. LVM consists of the following components:
1.1.1 Volume Group (VG)
The Volume Group is the high level container which holds one or more logical volumes and physical volumes.
1.1.2 Physical Volume (PV)
A physical volume represents a storage device such as a disk drive or other storage media.
1.1.3 Logical Volume (LV)
A logical volume is the equivalent to a disk partition and, as with a disk partition, can contain a file system.
1.1.4 Physical Extent (PE)
Each physical volume (PV) is divided into equal size blocks known as physical extents.
1.1.5 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 in order 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 made up of the three physical volumes.
If we run out of space in LogVol001 we simply add more disk partitions as physical volumes and assign them to the volume group and logical volume.
1.2 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 (assuming, of course, that LVM partitioning option was selected during the Ubuntu installation process), 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 important to first gather information.
Running the mount command will output information about a range of mount points, including the following entry for the root filesystem:
/dev/mapper/vgubuntu-root on / type ext4 (rw,relatime,errors=remount-ro)
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 <73.75 GiB PE Size 4.00 MiB Total PE 18879 Alloc PE / Size 18879 / <73.75 GiB Free PE / Size 0 / 0 VG UUID hqaagb-OgB5-3DhK-qLoN-bRHU-jsFm-LrdXtT
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 approximately 73GB available for allocation to logical volumes. Currently 18879 physical extents are allocated equaling the total capacity. If we want to increase the space allocated to any logical volumes in the vgubuntu volume group, therefore, we will need to add one or more physical volumes. The vgs tool is also useful 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- <73.75g 0
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 iLfsLf-pVzy-yCfd-wKim-EdbW-efvm-J6p1f4 LV Write Access read/write LV Creation host, time ubuntu, 2020-04-06 11:17:53 -0400 LV Status available # open 1 LV Size <72.79 GiB Current LE 18634 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 14Cr74-x5EW-V1k1-c5z8-8NUn-DTqC-PLEg7F LV Write Access read/write LV Creation host, time ubuntu, 2020-04-06 11:17:54 -0400 LV Status available # open 2 LV Size 980.00 MiB Current LE 245 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
As shown in the above example approximately 72 GiB of the space in volume group vgubuntu is allocated to logical volume root (for the / file system) and 980 MiB to swap_1 (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/sda1 VG Name vgubuntu PV Size <73.75 GiB / not usable 2.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 18879 Free PE 0 Allocated PE 18879 PV UUID nwp55K-Chay-x5eB-kZcc-sonL-cm3E-3SWnKG
Clearly the space controlled by logical volume vgubuntu is provided via a physical volume located on /dev/sda1.
Now that we know a little more about our LVM configuration we can embark on the process of adding space to the volume group and the logical volume contained within.
1.3 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 it is being seen by the operating system as /dev/sdb. We shall also assume that this is a new disk that does not contain any existing partitions. If existing partitions are present they should be backed up and then the partitions deleted from the disk using the fdisk utility. For example, assuming a device represented by /dev/sdb containing one partition as follows:
# fdisk -l /dev/sdb Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors 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: gpt Disk identifier: 6C7E15CA-C9B1-4FEB-B10A-BE75F8B6D483 Device Start End Sectors Size Type /dev/sdb1 2048 20971486 20969439 10G Linux filesystem
Once any filesystems on this partition have been unmounted, they can be deleted as follows:
# fdisk /dev/sdb Welcome to fdisk (util-linux 2.31.1). 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.
Before moving to the next step, be sure to 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 Physical volume "/dev/sdb" successfully created.
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: Success
With the physical volume created we now need to add it to the volume group (in this case vgubuntu) using the vgextend command:
# vgextend vgubuntu /dev/sdb Volume group "vgubuntu" successfully extended
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 logical volume by 9 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+9G /dev/vgubuntu/root Size of logical volume vgubuntu/root changed from <72.79 GiB (18634 extents) to <81.79 GiB (20938 extents). Logical volume vgubuntu/root successfully resized.
The last step in the process is to resize the file system residing on the logical volume so that it uses the additional space. The way this is performed will depend on the filesystem type which can be identified using the following command and checking the Type column:
# df -T / Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/mapper/vgubuntu-root ext4 83890408 5186940 74496972 7% /
If root is formatted using the XFS filesystem, this can be achieved using the xfs_growfs utility:
# xfs_growfs /
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/root
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 even having to restart the server. As far as any users on the system are concerned nothing has changed (except, of course, 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, logical volumes and physical volumes while demonstrating how to manage these using command-line tools.