Linux - LVM

🧱 Everything You Need to Know About LVM in Linux

If you’re working with Linux systems, sooner or later you’ll encounter LVMLogical Volume Manager. It’s a powerful system for managing storage that goes far beyond traditional disk partitioning.

In this post, we’ll cover everything about LVM: what it is, why it’s useful, and how to use it — with real-world commands and examples.


πŸ“– What is LVM?

LVM (Logical Volume Manager) is a system that lets you manage your storage devices more flexibly than traditional partitions. Instead of dividing your disk into fixed partitions, LVM allows you to create logical volumes on top of physical storage devices, and you can resize, extend, or move them easily — even while the system is running.


🧩 LVM Structure

To understand how LVM works, let’s look at the 3 main layers:

  1. Physical Volumes (PV)
    Actual hard drives or partitions (/dev/sda1, /dev/nvme0n1p3, etc.)

  2. Volume Groups (VG)
    A pool of storage made by combining multiple PVs.

  3. Logical Volumes (LV)
    These are like partitions, created inside a VG. You mount and format LVs just like regular partitions.

[ Hard Drive ] -> [ PV ] -> [ VG ] -> [ LV ]



πŸš€ Why Use LVM?

✅ Benefits of LVM:

  • Resize volumes on the fly

  • Combine multiple disks into one volume

  • Create snapshots for backups or testing

  • Easily manage growing storage needs

  • Migrate volumes to other disks with minimal downtime


πŸ› ️ How to Use LVM – Step by Step

Let’s go through a real example.

πŸ”Ή Step 1: Install LVM (if not installed)

sudo apt install lvm2 # Ubuntu/Debian sudo yum install lvm2 # RHEL/CentOS

πŸ”Ή Step 2: Create Physical Volume (PV)

sudo pvcreate /dev/sdb

Check PVs:

sudo pvdisplay

πŸ”Ή Step 3: Create Volume Group (VG)

sudo vgcreate my_vg /dev/sdb

List VGs:

sudo vgdisplay

πŸ”Ή Step 4: Create Logical Volume (LV)

sudo lvcreate -L 10G -n my_lv my_vg
  • -L 10G: size of the LV

  • -n my_lv: name

  • my_vg: volume group


πŸ”Ή Step 5: Format the Logical Volume

sudo mkfs.ext4 /dev/my_vg/my_lv

πŸ”Ή Step 6: Mount It

sudo mkdir /mnt/mydata sudo mount /dev/my_vg/my_lv /mnt/mydata

πŸ”„ Resize Volumes

Increase LV size (if VG has free space):

sudo lvextend -L +5G /dev/my_vg/my_lv sudo resize2fs /dev/my_vg/my_lv # for ext4

Reduce LV size (⚠️ Risky – backup first):

sudo umount /mnt/mydata sudo e2fsck -f /dev/my_vg/my_lv sudo resize2fs /dev/my_vg/my_lv 5G sudo lvreduce -L 5G /dev/my_vg/my_lv

πŸ“Έ Snapshots with LVM

Create a snapshot:

sudo lvcreate -L 1G -s -n my_lv_snap /dev/my_vg/my_lv

Mount snapshot if needed and use for backups or testing.


πŸ’‘ Useful LVM Commands

Command                                              Description
pvcreate   Initialize physical volume
vgcreate   Create volume group
lvcreate   Create logical volume
lvextend   Increase LV size
lvreduce   Reduce LV size
lvremove   Remove logical volume
vgextend   Add PV to existing VG
vgreduce   Remove PV from VG
vgremove   Remove volume group
pvremove   Remove physical volume
lvdisplay, vgdisplay, pvdisplay   Show detailed info

🧯 Real-World Use Cases

  • Creating storage pools for virtual machines

  • Using snapshots before risky software upgrades

  • Expanding file systems without downtime

  • Managing external disks or RAID devices


⚠️ Things to Watch Out For

  • Shrinking LVs is dangerous — always back up first.

  • Don’t remove a PV that still has active data.

  • Snapshots take space — monitor usage or they will become invalid.


✅ Summary

LVM is a must-know tool for any Linux sysadmin, DevOps engineer, or power user. It gives you flexibility, control, and scalability over your disk storage. Whether you’re running a home server or managing an enterprise system, LVM is one of the most valuable storage tools in your arsenal.

Comments

Popular Posts