LVM (Logical Volume Manager) is a logical volume management system that enables creation, resizing, and flexible management of Linux storage spaces.
🧩 LVM Principle
Hierarchical structure:
💿 Physical disk (/dev/sdb)
↓
💾 PV (Physical Volume) - Disk prepared for LVM
↓
📦 VG (Volume Group) - Storage pool (one or more PVs)
↓
📁 LV (Logical Volume) - Virtual partition in the VG
↓
🗂️ Filesystem (ext4, xfs...) mounted on /data, /db, /app...
Concrete example:
- PV:
/dev/sdb(100GB disk) - VG:
datavg(group containing sdb) - LV:
datalv(100GB volume in datavg) - Mount:
/dev/mapper/datavg-datalv→/data
🔍 Inspection and Status
| 📌 Action | 🧠 Command |
|---|---|
| 📊 LVM overview | lsblk |
| 💾 Physical volumes | pvdisplay |
| 🗂️ Volume groups | vgdisplay |
| 📁 Logical volumes | lvdisplay |
| 📋 Quick PV list | pvs |
| 📋 Quick VG list | vgs |
| 📋 Quick LV list | lvs |
💿 Physical Volume (PV) Management
| 📌 Action | 🧠 Command |
|---|---|
| ➕ Create a PV | pvcreate /dev/sdb1 |
| 📊 PV information | pvdisplay /dev/sdb1 |
| 🗑️ Remove a PV | pvremove /dev/sdb1 |
| 📏 Resize PV | pvresize /dev/sdb1 |
| 🔄 Move PV data | pvmove /dev/sdb1 |
Example: Prepare a new disk
sudo fdisk /dev/sdb # Create a partition
sudo pvcreate /dev/sdb1 # Initialize as PV📦 Volume Group (VG) Management
| 📌 Action | 🧠 Command |
|---|---|
| ➕ Create a VG | vgcreate data_vg /dev/sdb1 |
| 📈 Extend a VG | vgextend data_vg /dev/sdc1 |
| 📉 Reduce a VG | vgreduce data_vg /dev/sdc1 |
| 🔄 Activate a VG | vgchange -ay data_vg |
| 💤 Deactivate a VG | vgchange -an data_vg |
| 🗑️ Remove a VG | vgremove data_vg |
Example: Create a group with multiple disks
sudo vgcreate storage_vg /dev/sdb1 /dev/sdc1 /dev/sdd1📁 Logical Volume (LV) Management
| 📌 Action | 🧠 Command |
|---|---|
| ➕ Create an LV (fixed size) | lvcreate -L 10G -n data_lv data_vg |
| 🎯 LV with percentage | lvcreate -l 50%VG -n data_lv data_vg |
| 📈 Extend an LV | lvextend -L +5G /dev/data_vg/data_lv |
| 📉 Reduce an LV | lvreduce -L -2G /dev/data_vg/data_lv |
| 🔄 Resize + FS | lvresize -L 20G -r /dev/data_vg/data_lv |
| 🗑️ Remove an LV | lvremove /dev/data_vg/data_lv |
Example: Create and format an LV
sudo lvcreate -L 20G -n web_lv data_vg
sudo mkfs.ext4 /dev/data_vg/web_lv📊 Resizing with Filesystem
📈 Extension (safe)
# Automatic LV + filesystem extension
sudo lvextend -L +10G -r /dev/vg/lv
# Manual extension
sudo lvextend -L +10G /dev/vg/lv
sudo resize2fs /dev/vg/lv # ext4
# or
sudo xfs_growfs /mount/point # XFS📉 Reduction (⚠️ risky)
# 1. Unmount required
sudo umount /mount/point
# 2. Check filesystem
sudo fsck -f /dev/vg/lv
# 3. Reduce FS then LV
sudo resize2fs /dev/vg/lv 15G
sudo lvreduce -L 15G /dev/vg/lv📸 Snapshots
| 📌 Action | 🧠 Command |
|---|---|
| 📸 Create snapshot | lvcreate -L 2G -s -n snap_lv /dev/vg/orig_lv |
| 📋 List snapshots | lvs -o name,size,snap_percent |
| 🔄 Restore snapshot | lvconvert --merge /dev/vg/snap_lv |
| 📈 Extend snapshot | lvextend -L +1G /dev/vg/snap_lv |
| 🗑️ Remove snapshot | lvremove /dev/vg/snap_lv |
Example: Backup with snapshot
sudo lvcreate -L 5G -s -n backup_snap /dev/data_vg/prod_lv
sudo mount /dev/data_vg/backup_snap /mnt/backup
# ... perform backup ...
sudo umount /mnt/backup
sudo lvremove /dev/data_vg/backup_snap🔧 Complete Workflow
🆕 Complete Creation of a /data Volume
🔍 Detect new disk (after VM addition)
# Rescan without reboot
echo "- - -" | sudo tee /sys/class/scsi_host/host*/scan >/dev/null
# Check available disks
lsblk -a🏗️ Volume creation
# 1. Create Volume Group on entire disk
sudo vgcreate datavg /dev/sdb
# 2. Create Logical Volume (100% of free space)
sudo lvcreate -l 100%FREE -n datalv datavg
# 3. Format as XFS
sudo mkfs -t xfs /dev/mapper/datavg-datalv
# 4. Create mount point and permissions
sudo mkdir /data
sudo chown -R $USER:root /data
sudo chmod 770 /data
# 5. Mount the volume
sudo mount /dev/mapper/datavg-datalv /data📝 Permanent mount (fstab)
# Edit fstab
sudo nano /etc/fstab
# Add the line:
/dev/mapper/datavg-datalv /data xfs rw,user,auto 0 0
# To allow binary execution, add "exec"
/dev/mapper/datavg-datalv /data xfs rw,user,auto,exec 0 0
# Reload configuration
sudo systemctl daemon-reload🔄 Rename a Volume (data → docker)
# 1. Unmount
sudo umount /data
# 2. Rename LV and VG
sudo lvrename datavg datalv dockerlv
sudo vgrename datavg dockervg
# 3. Rename folder
sudo mv /data /docker
# 4. Remount
sudo mount /dev/mapper/dockervg-dockerlv /docker
# 5. Modify fstab
sudo nano /etc/fstab
# /dev/mapper/dockervg-dockerlv /docker xfs rw,user,auto 0 0
sudo systemctl daemon-reload🗑️ Complete Volume Deletion
# 1. Unmount the disk
sudo umount /data
# 2. Comment/remove fstab entry
sudo nano /etc/fstab
# #/dev/mapper/datavg-datalv /data xfs rw,user,auto 0 0
# 3. Delete in order: LV → VG → PV
sudo lvremove /dev/datavg/datalv
sudo vgremove datavg
sudo pvremove /dev/sdb
# 4. Remove directory
sudo rm -rf /data
# 5. Verify
lsblk -a🚨 Troubleshooting
| 🆘 Problem | 🧠 Solution |
|---|---|
| VG not found | vgscan && vgchange -ay |
| Corrupted LV | fsck -f /dev/vg/lv |
| Failed PV | pvmove /dev/failed_pv then vgreduce |
| Full snapshot | lvextend -L +2G /dev/vg/snap_lv |
| Mount with exec | mount -o remount,exec /data |