
The Shape-Shifter: Mastering LVM
Ditch the rigid partitions of the past. Master 'Logical Volume Management' (LVM). Learn to pool your disks together, resize volumes while the system is running, and take instant backups using snapshots. Understand the PV -> VG -> LV hierarchy.
LVM: The Flexible Storage Revolution
In the traditional world, if your /var partition gets full, you have a major problem. You have to shut down the server, boot a GParted live disk, and spend an hour moving partitions around.
LVM (Logical Volume Manager) solves this forever.
LVM adds a "Management Layer" between your physical disks and your operating system. It allows you to treat your hard drives like pool of Lego blocks. You can add a new disk to the pool, extend the space on your web server while people are still using it, and even "Snapshot" the disk before a dangerous update.
1. The LVM Hierarchy: Three Layers of Magic
To understand LVM, you must understand the three layers:
- PV (Physical Volume): The raw hard drive or partition (/dev/sda). You "Prepare" it for LVM.
- VG (Volume Group): You take multiple PVs and glue them into a single giant "Buckets of Space."
- LV (Logical Volume): You carve "Slices" out of the VG. This is what you actually format with ext4 or XFS.
graph TD
Disk1[Hard Drive A] --> PV1[Physical Vol 1]
Disk2[Hard Drive B] --> PV2[Physical Vol 2]
PV1 --> VG[Volume Group: 'storage_pool']
PV2 --> VG
VG --> LV1[Log. Vol: 'web_data']
VG --> LV2[Log. Vol: 'database']
2. Practical: Creating the Stack
Let's say we have a new disk /dev/sdb.
# 1. Prepare the physical disk
sudo pvcreate /dev/sdb
# 2. Create a Volume Group named 'vg_server'
sudo vgcreate vg_server /dev/sdb
# 3. Create a 50GB Logical Volume named 'lv_apps'
sudo lvcreate -L 50G -n lv_apps vg_server
# 4. Use it like a normal disk!
sudo mkfs.ext4 /dev/vg_server/lv_apps
3. The Superpower: Online Expansion
Suppose your lv_apps is 99% full. In the old world, you'd be panicking. In LVM, it's a two-minute job without stopping the server.
# Grow the Logical Volume to 100GB
sudo lvextend -L 100G /dev/vg_server/lv_apps
# Grow the Filesystem inside the volume to match the new size
sudo resize2fs /dev/vg_server/lv_apps
4. Snapshots: Time Travel for Disks
Before you run a major database migration, you can take an "Invisible Backup" in one second.
sudo lvcreate -L 5G -s -n backup_snapshot /dev/vg_server/lv_apps
This doesn't copy the whole 50GB. It only records the changes that happen from now on. If your migration fails, you can "Merge" the snapshot back, and your server instantly reverts to exactly how it was 5 minutes ago.
5. Summary: The Daily Commands
| Command | Layer | Purpose |
|---|---|---|
pvs / pvdisplay | Physical | See raw disk health. |
vgs / vgdisplay | Group | See how much total space is left in the pool. |
lvs / lvdisplay | Logical | See your active drive slices. |
6. Example: An Automated Expansion Script (Python)
Why resize manually? Here is a Python script that monitors your LVM volume and automatically grows it if it reaches 90% full, assuming there is space left in the Volume Group.
import subprocess
import shutil
def auto_extend_vol(mount_point, lvm_path, growth_size="10G"):
"""
Checks disk usage and extends LVM if needed.
"""
total, used, free = shutil.disk_usage(mount_point)
percent = (used / total) * 100
print(f"Checking {mount_point}... Current Usage: {percent:.1f}%")
if percent > 90:
print(f"[!] THRESHOLD EXCEEDED. Growing {lvm_path} by {growth_size}...")
# 1. Extend the Logical Volume
subprocess.run(["sudo", "lvextend", "-L", f"+{growth_size}", lvm_path])
# 2. Resize the filesystem (Assuming ext4)
subprocess.run(["sudo", "resize2fs", lvm_path])
print("[SUCCESS] Volume expanded 'on the fly'.")
if __name__ == "__main__":
# Example usage:
# auto_extend_vol("/var/www", "/dev/vg_server/lv_web")
pass
7. Professional Tip: Use 'Thin Provisioning'
In high-end environments, you can "Lie" to your system. You can create ten 100GB volumes even if you only have 500GB of physical space. This is called Thin Provisioning. It assumes not every volume will be full at the same time. You just keep adding physical disks to the VG as the real usage increases.
8. Summary
LVM is the gold standard for server storage.
- Physical Volumes are the bricks.
- Volume Groups are the wall.
- Logical Volumes are the rooms.
- Expansion can happen while the system is online.
- Snapshots are your safety net for dangerous operations.
In the next lesson, we will learn how to protect your data from physical hardware failure using RAID and Redundancy.
Quiz Questions
- What is the difference between a Physical Volume (PV) and a Volume Group (VG)?
- If you have two 500GB drives in one Volume Group, what is the maximum size of a single Logical Volume you can create?
- Why is it dangerous to delete an LVM snapshot that has filled up its allocated space?
Continue to Lesson 5: RAID and Redundancy—Protecting Your Data.