The Problem with Traditional Partitions
In the early days of Linux, administrators formatted hard drives using rigid, fixed partitions. If you assigned 50GB to your /var partition for log files and it filled up, you were in serious trouble. Resizing a traditional partition requires taking the server offline, booting from a Live CD, manually adjusting the partition boundaries, and praying you didn’t accidentally overwrite data.
Modern Linux distributions avoid this entirely by using the Logical Volume Manager (LVM). LVM acts as an abstraction layer between the physical hard drive and the file system. Instead of creating fixed partitions, LVM pools your hard drives together into a massive “Volume Group.” You then carve out flexible “Logical Volumes” (like /var or /home) from that pool. If a logical volume runs out of space, you can seamlessly expand it while the server is running, without ever unmounting the drive.
Understanding the LVM Hierarchy
LVM relies on three distinct layers:
- Physical Volumes (PV): The actual, physical hard drives (e.g.,
/dev/sdb) or raw partitions. - Volume Groups (VG): A pool of storage created by combining one or more Physical Volumes. (Think of this as a massive bucket of raw storage).
- Logical Volumes (LV): The virtual partitions carved out of the Volume Group. These are formatted with a file system (like ext4) and mounted (e.g.,
/dev/mapper/vg01-var).
Step-by-Step: Expanding a Logical Volume
Assume your database server is running out of space on the /var logical volume. You just physically inserted a new 100GB hard drive (recognized by the system as /dev/sdc) into the server.
Step 1: Initialize the New Hard Drive
First, you must format the raw hard drive so LVM can recognize it as a Physical Volume.
sudo pvcreate /dev/sdc
You can verify it was created by running sudo pvs.
Step 2: Add the Drive to the Volume Group
Next, we add this new 100GB physical volume into our existing storage pool. First, find the name of your Volume Group by typing sudo vgs. Let’s assume the name is vg01.
sudo vgextend vg01 /dev/sdc
If you run sudo vgs again, you will see that vg01 suddenly has 100GB of “VFree” (Free space) available.
Step 3: Extend the Logical Volume
Now that the pool is larger, we can pull some of that space to expand our specific /var volume. To find the exact path of the logical volume, type sudo lvs. Let’s assume it is /dev/vg01/var.
To add 50 Gigabytes of space to the volume:
sudo lvextend -L +50G /dev/vg01/var
Note: You can also use -l +100%FREE to assign all available free space in the pool to that volume.
Step 4: Resize the File System
This is the most critical step. LVM has expanded the logical volume container, but the file system inside the container (usually ext4 or XFS) does not automatically know it has more room to breathe. You must tell the file system to expand.
If you are using an ext4 file system (default on Debian/Ubuntu):
sudo resize2fs /dev/vg01/var
If you are using an XFS file system (default on RHEL/CentOS):
sudo xfs_growfs /var
Run df -h. You will instantly see that your /var partition is 50GB larger, and you accomplished it all without a single second of downtime.
Conclusion
LVM completely abstracts the rigidity of physical hard drives. By utilizing Physical Volumes, Volume Groups, and Logical Volumes, Linux administrators gain the power to seamlessly scale storage infrastructure up or down to meet the dynamic needs of enterprise applications, completely eliminating the need for maintenance windows.