Linux - LVM (Logical Volume Manager)

LVM is a flexible and powerful system for managing storage in Linux.
Instead of fixed-size partitions, LVM lets you create, resize, combine, and move storage volumes easily—even while the system is running.

Think of LVM as an advanced layer between your disks and your filesystem.


Why Use LVM?

Traditional disk partitions are fixed and hard to modify.
With LVM, storage becomes dynamic, meaning you can:

✔ Extend storage anytime
✔ Combine many disks into one
✔ Create snapshots
✔ Resize partitions easily
✔ Move data between disks without downtime


LVM Components Overview

LVM has three main components:

1. Physical Volume (PV)

A PV is a physical device used by LVM:

  • A full disk (/dev/sdb)

  • Or a partition (/dev/sdb1)

You convert a disk/partition into an LVM PV using:

sudo pvcreate /dev/sdb1

2. Volume Group (VG)

A VG is like a storage pool made of one or more PVs.

Example:

sudo vgcreate myvg /dev/sdb1 /dev/sdc1

Now, myvg contains total storage of both PVs combined.


3. Logical Volume (LV)

An LV is similar to a partition, but flexible.
You create it inside a VG.

Example:

sudo lvcreate -L 20G -n mydata myvg

Then format it:

sudo mkfs.ext4 /dev/myvg/mydata

Finally mount it:

sudo mount /dev/myvg/mydata /mnt/data

How LVM Works (Simple View)

[Physical Disks] → PVs → VG → LVs → Filesystem

Key Features of LVM

1. Extend Storage Anytime

If an LV becomes full:

sudo lvextend -L +10G /dev/myvg/mydata
sudo resize2fs /dev/myvg/mydata   # For ext4

Zero downtime in most cases.


2. Shrink Storage (Carefully)

For ext4:

sudo umount /mnt/data
sudo resize2fs /dev/myvg/mydata 10G
sudo lvreduce -L 10G /dev/myvg/mydata

3. Add New Disks Easily

Add a new disk /dev/sdd1:

sudo pvcreate /dev/sdd1
sudo vgextend myvg /dev/sdd1

Your VG grows without touching existing volumes.


4. Create Snapshots

Snapshots allow backup of the LV at a point in time.

sudo lvcreate -L 5G -s -n snapdata /dev/myvg/mydata

Great for:

  • backups

  • system updates

  • recovery


5. Move Data Between Disks (pvmove)

Move data from one PV to another without unmounting:

sudo pvmove /dev/sdb1 /dev/sdc1

Useful when replacing HDD with SSD.


Common LVM Commands Summary

PVs

pvcreate /dev/sdb1
pvs
pvdisplay

VGs

vgcreate myvg /dev/sdb1
vgextend myvg /dev/sdc1
vgs
vgdisplay

LVs

lvcreate -L 10G -n mylv myvg
lvextend -L +5G /dev/myvg/mylv
lvreduce -L 5G /dev/myvg/mylv
lvs
lvdisplay

Snapshot

lvcreate -L 2G -s -n mysnap /dev/myvg/mylv

Advantages of LVM

✔ Resize partitions easily
✔ Add/remove disks without downtime
✔ Combine multiple disks
✔ Create snapshots
✔ Better use of disk space
✔ Useful for servers and virtual machines


Disadvantages

⚠ Slight overhead due to abstraction
⚠ If VG fails, all LVs inside may become unreadable
⚠ Snapshots can slow performance if overused


Where LVM Is Commonly Used

  • Servers

  • Datacenters

  • Virtualized systems (KVM, VMware)

  • Cloud environments

  • Any system needing flexible storage