skip to content

Search

Syspirit
EN

LVM

Logical volume management with LVM - creation, resizing, and flexible storage!

Linux
Published on

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 overviewlsblk
💾 Physical volumespvdisplay
🗂️ Volume groupsvgdisplay
📁 Logical volumeslvdisplay
📋 Quick PV listpvs
📋 Quick VG listvgs
📋 Quick LV listlvs

💿 Physical Volume (PV) Management

📌 Action🧠 Command
➕ Create a PVpvcreate /dev/sdb1
📊 PV informationpvdisplay /dev/sdb1
🗑️ Remove a PVpvremove /dev/sdb1
📏 Resize PVpvresize /dev/sdb1
🔄 Move PV datapvmove /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 VGvgcreate data_vg /dev/sdb1
📈 Extend a VGvgextend data_vg /dev/sdc1
📉 Reduce a VGvgreduce data_vg /dev/sdc1
🔄 Activate a VGvgchange -ay data_vg
💤 Deactivate a VGvgchange -an data_vg
🗑️ Remove a VGvgremove 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 percentagelvcreate -l 50%VG -n data_lv data_vg
📈 Extend an LVlvextend -L +5G /dev/data_vg/data_lv
📉 Reduce an LVlvreduce -L -2G /dev/data_vg/data_lv
🔄 Resize + FSlvresize -L 20G -r /dev/data_vg/data_lv
🗑️ Remove an LVlvremove /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 snapshotlvcreate -L 2G -s -n snap_lv /dev/vg/orig_lv
📋 List snapshotslvs -o name,size,snap_percent
🔄 Restore snapshotlvconvert --merge /dev/vg/snap_lv
📈 Extend snapshotlvextend -L +1G /dev/vg/snap_lv
🗑️ Remove snapshotlvremove /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 foundvgscan && vgchange -ay
Corrupted LVfsck -f /dev/vg/lv
Failed PVpvmove /dev/failed_pv then vgreduce
Full snapshotlvextend -L +2G /dev/vg/snap_lv
Mount with execmount -o remount,exec /data