Mastering Linux Volume Management (LVM)
From Basics to Dynamic Storage Scaling
Table of contents
- Why LVM Matters: Breaking Free from Storage Limitations
- LVM Core Concepts
- Real-life Scenario: Expanding Cloud Storage on AWS EC2
- The Art of Safe Resizing: Let’s Break Down All Possibilities
- Add Volume Without LVM (Direct Mounting)
- Key Scenarios Compared
- When to Use Direct Mounting vs. LVM
- Using a Loop Device Locally (For Practice)
- Working with Disk Partitions on Loop Devices
- Unmounting: Safe Disconnection
- Dynamic Storage Management: When Size Does Matter
- Useful Commands Cheat Sheet
- Pro Tips and Pitfalls to Avoid
- Conclusion: Embrace Storage Flexibility
Why LVM Matters: Breaking Free from Storage Limitations
Imagine you're building a house with fixed-size rooms. Once constructed, you can't easily expand the kitchen or merge bedrooms. Traditional disk partitioning works similarly—fixed partitions that are hard to modify. Linux Volume Management (LVM) revolutionizes this by letting you create flexible "rooms" (logical volumes) that you can resize, move, or even snapshot while the house is still occupied!
In this comprehensive guide, we'll explore how LVM empowers you to:
Create storage pools from multiple disks
Resize filesystems on-the-fly
Migrate data between physical drives
Handle cloud storage like a pro
LVM Core Concepts
1. The Building Blocks: PV, VG, and LV
Component | Real-World Analogy | Key Superpower |
Physical Volume (PV) | Individual Lego blocks | Raw storage from disks/partitions |
Volume Group (VG) | Lego baseplate | Pool combining multiple PVs |
Logical Volume (LV) | Lego structures on baseplate | Flexible partitions you create |
2. Device Naming in Modern Systems
Linux uses different naming conventions based on storage types:
/dev/sdX: Classic SATA/SAS drives (sda, sdb...)
/dev/nvmeXnY: NVMe SSDs (nvme0n1, nvme1n1p2...)
/dev/xvdX: Common in cloud VMs (xvda, xvdf...)
Pro Tip: Always verify with
lsblk
before working with disks!
Real-life Scenario: Expanding Cloud Storage on AWS EC2
Situation
Your web server is running out of space. You need to:
Add a new 20GB EBS volume
Expand existing storage without downtime
Step-by-step Guide
Open your AWS EC2 Dashboard.
Go to the Elastic Block Store (EBS) section and create a new volume.
Set the Size (GiB) to 20, and Volume Type gp3
And MUST specify correct Availability Zone
Attach the volume to your EC2 instance as
/dev/sdf
NOTE: The region and availability zone must be same for the newly created Volume and EC2 instance (to which you want to Attach the volume).
- SSH into your instance and verify the new volume:
lsblk
This will list all storage devices. You should see the new volume as /dev/xvdf
(or similar).
NOTE: You can follow the below commands as given, but you can also utilize
lvm
command by first logging in as root,sudo su
. After that you can just use those commands without usingsudo
at prefix.You can simply write
exit
to get out of LVM command-line input.
- Create Physical Volume
sudo pvcreate /dev/xvdf # Initialize the volume as a Physical Volume
sudo pvs # Verify
- Create a Volume Group
sudo vgcreate vg_data /dev/xvdf # Create Volume Group named vg_data
sudo vgs # Verify
OR, Extend Volume Group (ONLY IF already exist one and you need to extend specifically that one)
sudo vgextend vg_data /dev/xvdf # Add to existing VG
sudo vgs # Check free space (VFree) of the Volume Group
- Create a Logical Volume
sudo lvcreate -L 20G -n lv_data vg_data # Create a 20GB LV named lv_data
sudo lvs # Verify
OR, Expand Logical Volume (ONLY IF you need to extend an existing Logical Volume)
sudo lvextend -L +20G /dev/vg_data/lv_data
- Format Logical Volume: Format it to the
ext4
filesystem
sudo mkfs.ext4 /dev/vg_data/lv_data
- Mount the Logical Volume: Create a directory and mount the volume
sudo mkdir /mnt/lv_mount_data
sudo mount /dev/vg_data/lv_data /mnt/lv_mount_data
OR, Resize Filesystem Live (ONLY IF required)
sudo resize2fs /dev/vg_data/lv_data # For ext4
# For xfs: sudo xfs_growfs /data
Result: Your /data mountpoint now has 20GB more space—no downtime required!
Verify the mount:
df -h
The Art of Safe Resizing: Let’s Break Down All Possibilities
When Do You Need Formatting?
Formatting prepares a storage device with a filesystem so it can store files. You need to format:
New physical disks/partitions: Before first use
Logical Volumes (LVs): After creating an LV
Changing filesystems: If switching from ext4 to XFS
Corrupted filesystems: To wipe and start fresh
Example:
# Format /dev/sdb (non-LVM disk) to ext4
sudo mkfs.ext4 /dev/sdb
# Format an LV to XFS
sudo mkfs.xfs /dev/vg_data/lv_data
When Do You Need Mounting?
Mounting makes a filesystem accessible (or usable) to the OS. You need to mount:
After formatting: To use the storage
On system reboot: Unless added to
/etc/fstab
Temporary access: e.g., USB drives
Example:
# Mount a formatted disk to /mnt/data
sudo mount /dev/sdb /mnt/disk_mount_data
# Mount an LV
sudo mount /dev/vg_data/lv_data /mnt/lv_mount_data
When Do You Need Resizing?
Resizing adjusts storage capacity dynamically. You need to resize:
Growing logical volumes: When more space is needed
Shrinking volumes: Rare, but possible (risky!)
Cloud storage expansion: After expanding an EBS/GCP disk
Pooling new disks: Adding PVs to a VG
Example:
# Extend an LV and resize filesystem
sudo lvextend -L +10G /dev/vg_data/lv_data
sudo resize2fs /dev/vg_data/lv_data # For ext4
Steps To Extend a Logical Volume
Check VG free space:
sudo vgdisplay vg_data | grep "Free PE"
Extend LV (20GB or 100% free space):
sudo lvextend -L +20G /dev/vg_data/lv_data # Specific size # OR sudo lvextend -l +100%FREE /dev/vg_data/lv_data # All free space
Resize filesystem:
resize2fs /dev/vg_data/lv_data # ext2/3/4 xfs_growfs /mountpoint # XFS
To make the mount persistent across reboots, add it to /etc/fstab
:
/dev/vg_data/lv_data /mnt/lv_mount_data ext4 defaults 0 0
Critical Note: Always resize the filesystem after extending the LV!
Add Volume Without LVM (Direct Mounting)
Sometimes you don’t need LVM’s flexibility. For example:
Single-purpose disks (backup drives)
Temporary storage
Boot partitions
Step-by-Step Workflow:
Identify the disk:
sudo lsblk # Let’s assume the new disk is /dev/sdb
Format directly (no LVM):
sudo mkfs.ext4 /dev/sdb
Create mount point:
sudo mkdir /mnt/disk_mount_data
Mount temporarily:
sudo mount /dev/sdb /mnt/disk_mount_data
Mount permanently (add to
/etc/fstab
):# Get UUID sudo blkid /dev/sdb # Add to fstab echo "UUID=e0ae19c8-abce /mnt/disk_mount_data ext4 defaults 0 0" | sudo tee -a /etc/fstab
Key Scenarios Compared
Scenario | Format? | Mount? | Resize? | LVM? |
New disk (direct use) | Yes | Yes | Limited | No |
New LVM setup | Yes | Yes | Easy | Yes |
Expanding existing LV | No | No* | Yes | Yes |
Reusing a disk | Yes | Yes | N/A | Optional |
Recovering corrupted FS | Yes | Yes | No | Optional |
When to Use Direct Mounting vs. LVM
Use Direct Mounting (No LVM) When:
You need simplicity (single disk, no scaling)
Working with boot partitions
Using removable media (USB drives)
Storage needs are static
Choose LVM When You Need:
Flexibility: Growing/shrinking partitions
Scalability: Combining multiple physical disks
Advanced Features: Snapshots, thin provisioning
Live Operations: Resizing without unmounting
Using a Loop Device Locally (For Practice)
If you're practicing on a local machine without additional disks, you can create a loop device:
What is a Loop Device?
A loop device is a virtual block device that maps a regular file to a "virtual disk." It allows files to behave like physical disks, letting you:
Format files as disk images
Mount files as filesystems
Test partitioning schemes
Create encrypted containers
Real-World Analogy:
Think of it as a virtual CD/DVD drive that uses an ISO file instead of physical media.
Create a Loop Device
Create a file to act as a virtual disk:
sudo dd if=/dev/zero of=/storagefile bs=1M count=1024
Associate it with a loop device:
sudo losetup -fP /storagefile # -f: Find next available loop device # Output: Now using /dev/loop0
Verify Loop Devices:
losetup -a # Output: /dev/loop0: []: (/storagefile)
Follow the same steps to create a Physical Volume, Volume Group, and Logical Volume on this loop device.
Working with Disk Partitions on Loop Devices
Case 1: Using the Entire Disk (No Partitions)
Good for: Simple use cases (single filesystem).
Workflow:
# Mount directly
sudo mount /dev/loop0 /mnt
Case 2: Creating Partitions
Good for: Simulating multi-partition disks (e.g., testing boot/root setups).
Workflow:
Create partitions on the loop device:
sudo fdisk /dev/loop0
Press
n
to create new partitions (e.g., 500MB and 500MB).Press
w
to save changes.
Reload partition table:
sudo partprobe /dev/loop0
Format partitions:
sudo mkfs.ext4 /dev/loop0p1 # First partition sudo mkfs.xfs /dev/loop0p2 # Second partition
Mount partitions:
sudo mount /dev/loop0p1 /mnt/part1 sudo mount /dev/loop0p2 /mnt/part2
Unmounting: Safe Disconnection
Mounting and unmounting do not alter data on the storage medium. They only manage access to the data.
Data persistence: Files remain intact unless explicitly modified or deleted.
No formatting required: Mounting a pre-formatted device doesn’t erase it.
Metadata preservation: Ownership, permissions, and timestamps stay unchanged.
Example:
sudo umount /mnt/usb
Exception: Data loss can occur if:
You format the device (e.g.,
mkfs.ext4
).You unplug hardware without unmounting (risking partial writes).
There’s physical damage or filesystem corruption.
Dynamic Storage Management: When Size Does Matter
Common Use Cases for LVM Operations
Operation | Real-World Scenario | Command Toolkit |
Extend LV | Database growing beyond initial allocation | lvextend , resize2fs |
Add PV to VG | Adding new SSDs to increase storage pool | vgextend , pvcreate |
Reduce LV | Reallocating space between services | lvreduce (CAUTION!) |
Migrate PV | Replacing failing drives | pvmove , vgreduce |
Useful Commands Cheat Sheet
Command | Description |
lsblk | List all block devices |
df -h | Show mounted file systems |
pvcreate | Initialize a Physical Volume |
vgcreate | Create a Volume Group |
lvcreate | Create a Logical Volume |
mkfs.ext4 | Format a partition to ext4 |
mount | Mount a filesystem |
losetup | Manage loop devices |
Pro Tips and Pitfalls to Avoid
✅ Best Practices
Always back up data before resizing operations
Use
-r
flag withlvextend
to resize filesystem automatically
lvextend -r -L +5G /dev/vg_data/lv_data
- Monitor VG free space with
vgs
❌ Common Mistakes:
Forgetting to resize filesystem after LV extension
Assuming all space is available (check VG free space first)
Using LVM on flash drives (can reduce lifespan)
Conclusion: Embrace Storage Flexibility
LVM transforms storage management from a rigid chore into a dynamic, flexible process. Whether you're:
Scaling cloud storage in AWS
Optimizing local disk or drives
Creating development environments
The power to resize, reshape, and reconfigure storage on-demand is an essential skill for any Linux professional.
I hope this blog helps you get started and feel confident in managing Linux storage. Happy learning!