The Sysadmin’s Nightmare: “Out of Disk Space”
Early in my career, I once stayed up until 3 AM because of a silly mistake. At the time, I used traditional partitioning (Standard Partition) and allocated 50GB to the /var directory. After just three months of operation, the log files swelled, causing the disk to hit 99% capacity, which crashed the customer’s entire database service.
To remedy the situation, I had to shut down the server and use a Live CD to resize the partition. It was an extremely risky and time-consuming process. If I had used LVM (Logical Volume Manager) back then, everything would have been resolved smoothly in 5 minutes with a few commands, without a single second of downtime.
Traditional partitioning is like building fixed brick walls in a house. To expand the living room, you are forced to tear down the walls and rebuild. LVM is different. It’s like movable partitions, allowing you to flexibly resize storage space based on a virtualization layer between the physical hard drive and the operating system.
Three Core Concepts: PV, VG, and LV
Don’t rush to type commands just yet. To use LVM effectively on CentOS Stream 9, you need to clearly distinguish these three components through a real-world example of land management:
- Physical Volume (PV): These are raw physical hard drives (such as
/dev/sdb). Think of them as individual plots of land you just purchased. - Volume Group (VG): A giant “pool” combined from multiple PVs. You combine all the plots into a single consolidated land title. At this point, the boundaries between physical drives disappear.
- Logical Volume (LV): Virtual partitions carved out from the VG for use. These are the rooms in the house. You can expand this room or shrink that room as you wish.
Setting Up the Environment on CentOS Stream 9
Although CentOS Stream 9 often configures LVM automatically during installation, setting it up manually on a new hard drive will help you gain a deeper understanding. Suppose the system has just been fitted with an additional 20GB hard drive /dev/sdb.
First, check the list of existing drives:
lsblk
If you see sdb appear and it has no partitions yet, we are ready to take action.
Step-by-Step LVM Configuration Process
Step 1: Initialize Physical Volume (PV)
We need to tell the operating system that the /dev/sdb drive will participate in the LVM system.
sudo pvcreate /dev/sdb
Confirm the status with the pvs command. You will see the device is ready to be added to a group.
Step 2: Create a Volume Group (VG)
Next, we group the /dev/sdb drive into a common group named vg_data.
sudo vgcreate vg_data /dev/sdb
The advantage here is that if you need another 100GB later, you just need to buy a new hard drive, create a PV, and then add it to this vg_data.
Step 3: Create a Logical Volume (LV)
Now, let’s carve out 10GB from the vg_data pool to create an lv_storage partition for a web application.
sudo lvcreate -L 10G -n lv_storage vg_data
The -L parameter is used to specify the capacity, and -n is used to name the virtual partition.
Step 4: Format the Filesystem and Use It
XFS is the top choice for stability on CentOS 9. Let’s format the newly created partition:
sudo mkfs.xfs /dev/vg_data/lv_storage
Finally, mount it to a system directory to start storing data:
sudo mkdir /mnt/data
sudo mount /dev/vg_data/lv_storage /mnt/data
To ensure the partition is not lost after a reboot, add a configuration line to /etc/fstab. This ensures the system always automatically mounts the drive at startup.
Online Capacity Expansion: The Most Valuable Feature
Imagine your web server is running a promotional campaign and the volume of uploaded images spikes. You need to increase the capacity from 10GB to 15GB immediately.
Perform this very quickly with 2 commands:
# 1. Extend the Logical Volume by 5GB
sudo lvextend -L +5G /dev/vg_data/lv_storage
# 2. Update the XFS filesystem size
sudo xfs_growfs /mnt/data
The best part is that this entire process happens while the website is still running. No restart required, no maintenance needed. If you are using the EXT4 format, you just need to change the second command to resize2fs.
Professional LVM System Monitoring
In actual operation, I always check periodically using the trio of commands: lvs (view virtual partitions), vgs (view free space in the group), and pvs (check physical hard drives).
If you prefer not to type commands, install Cockpit. This is the default web interface on CentOS 9. In the “Storage” section, you can drag a slider to resize partitions very intuitively.
Conclusion
Using LVM is no longer an option but a mandatory standard for Production environments. It helps you manage resources more proactively and professionally.
My advice is to always choose LVM right from the first OS installation step. Although it takes a few extra minutes for the initial setup, it will save you from emergency situations in the future. Good luck mastering your system!
