LVM
Chapter 13: LVM - Logical Volume Manager
Section titled “Chapter 13: LVM - Logical Volume Manager”Comprehensive Linux Logical Volume Management
Section titled “Comprehensive Linux Logical Volume Management”Why This Matters in DevOps/SRE
Section titled “Why This Matters in DevOps/SRE”LVM is essential for modern infrastructure - it enables dynamic storage management, essential for cloud environments, containers, and systems that need to scale. DevOps engineers must understand LVM for provisioning, resizing, backups, and disaster recovery.
┌─────────────────────────────────────────────────────────────────────────────┐│ LVM IN DEVOPS │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ OPERATIONAL USE CASES │ ││ │ │ ││ │ • Cloud/VM Environments - Resize disks on the fly │ ││ │ • Container Hosts - Flexible storage for many containers │ ││ │ • Database Servers - Easy expansion for growing data │ ││ │ • Backup/Recovery - LVM snapshots for consistent backups │ ││ │ • Development Labs - Quickly clone environments │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ COMMON TASKS │ ││ │ │ ││ │ • Extend logical volume: lvextend -L +10G /dev/vg/lv │ ││ │ • Create snapshot: lvcreate -s -n snap -L 5G /dev/vg/lv │ ││ │ • Thin provisioning: lvcreate --thinpool vg/thinpool │ ││ │ • Remove old snapshot: lvremove /dev/vg/snap │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ PRODUCTION SCENARIOS │ ││ │ │ ││ │ Database Server: │ ││ │ • Separate VGs for data, logs, backups │ ││ │ • Daily snapshots for point-in-time recovery │ ││ │ • Mirror for redundancy │ ││ │ │ ││ │ Container Host: │ ││ │ • Thin pool for container storage │ ││ │ • Over-provisioning for disk efficiency │ ││ │ • Easy volume expansion without downtime │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘13.1 LVM Architecture Deep Dive
Section titled “13.1 LVM Architecture Deep Dive”What is LVM?
Section titled “What is LVM?”LVM (Logical Volume Manager) is a device mapper framework that provides logical volume management for the Linux kernel. It allows for flexible disk storage management, including resizing, snapshots, and thin provisioning.
┌────────────────────────────────────────────────────────────────────────┐│ LVM ARCHITECTURE LAYERS │├────────────────────────────────────────────────────────────────────────┤│ ││ ┌──────────────────────────────────────────────────────────────────┐ ││ │ USER SPACE │ ││ │ ┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐ │ ││ │ │ pvcreate │ │ vgcreate │ │ lvcreate │ │ lvm │ │ ││ │ │ pvs │ │ vgs │ │ lvs │ │ tools │ │ ││ │ └────────────┘ └────────────┘ └────────────┘ └────────────┘ │ ││ └──────────────────────────────────────────────────────────────────┘ ││ │ ││ ▼ ││ ┌──────────────────────────────────────────────────────────────────┐ ││ │ KERNEL MODULES │ ││ │ ┌──────────────────────────────────────────────────────────┐ │ ││ │ │ device-mapper (dm.ko) │ │ ││ │ │ dm-mod.ko (snapshot, thin, cache, mirror, raid) │ │ ││ │ └──────────────────────────────────────────────────────────┘ │ ││ └──────────────────────────────────────────────────────────────────┘ ││ │ ││ ▼ ││ ┌──────────────────────────────────────────────────────────────────┐ ││ │ DEVICE MAPPER │ ││ │ │ ││ │ /dev/mapper/vg_name-lv_name ──► /dev/dm-0, /dev/dm-1 │ ││ │ /dev/vg_name/lv_name ──► Symlink to above │ ││ │ │ ││ └──────────────────────────────────────────────────────────────────┘ ││ │└────────────────────────────────────────────────────────────────────────┘LVM Components
Section titled “LVM Components”| Component | Abbreviation | Description | Example |
|---|---|---|---|
| Physical Volume | PV | Partition or disk initialized for LVM | /dev/sdb1 |
| Volume Group | VG | Pool of storage from PVs | vg_data |
| Logical Volume | LV | Virtual partitions from VGs | lv_data |
| Physical Extent | PE | Smallest allocatable unit (4MB default) | 4MB chunk |
| Logical Extent | LE | Maps to PE for allocation | 4MB chunk |
| Volume Descriptor | VGDA | Metadata at start of PV | Metadata |
LVM Workflow
Section titled “LVM Workflow”┌────────────────────────────────────────────────────────────────────────┐│ LVM DATA FLOW │├────────────────────────────────────────────────────────────────────────┤│ ││ Physical Disk ││ ┌─────────────┐ ││ │ /dev/sdb │ ││ │ │ ││ │ ┌─────────┐ │ ┌─────────────────┐ ┌──────────────────┐ ││ │ │/dev/sdb1│ │────►│ Physical Volume │ │ /dev/sdb1 │ ││ │ │ Partition│ │ │ (PV Header) │ │ PV Header │ ││ │ └─────────┘ │ └─────────────────┘ │ PE Table │ ││ └─────────────┘ └──────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────────────────────────────────────────────┐ ││ │ Volume Group (VG) │ ││ │ │ ││ │ ┌───────────────────────────────────────────────┐ │ ││ │ │ Physical Extents │ │ ││ │ │ PE0 PE1 PE2 PE3 PE4 PE5 PE6 PE7 │ │ ││ │ │ ▓▓▓ ▓▓▓ ░░ ░░ ███ ███ ▓▓▓ ▓▓▓ │ │ ││ │ │ (used) (free) (used) (used) │ │ ││ │ └───────────────────────────────────────────────┘ │ ││ │ │ │ ││ │ ▼ │ ││ │ ┌───────────────────────────────────────────────┐ │ ││ │ │ Logical Extents │ │ ││ │ │ LE0 LE1 LE2 LE3 LE4 LE5 LE6 LE7 │ │ ││ │ │ ▓▓▓ ▓▓▓ ▓▓▓ ▓▓▓ ███ ███ ███ ███ │ │ ││ │ │ (lv_data) (lv_logs) │ │ ││ │ └───────────────────────────────────────────────┘ │ ││ │ │ ││ └─────────────────────────────────────────────────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────────────────────────────────────────────┐ ││ │ Logical Volumes (LV) │ ││ │ │ ││ │ ┌──────────────────────┐ ┌──────────────────────┐ │ ││ │ │ /dev/vg_data/ │ │ /dev/vg_data/ │ │ ││ │ │ lv_data │ │ lv_logs │ │ ││ │ │ (100GB) │ │ (50GB) │ │ ││ │ └──────────────────────┘ └──────────────────────┘ │ ││ │ │ ││ └─────────────────────────────────────────────────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────────────────────────────────────────────┐ ││ │ Filesystem │ ││ │ │ ││ │ ┌──────────────────────┐ ┌──────────────────────┐ │ ││ │ │ /dev/mapper/ │ │ /dev/mapper/ │ │ ││ │ │ vg_data-lv_data │ │ vg_data-lv_logs │ │ ││ │ │ (ext4/xfs) │ │ (ext4/xfs) │ │ ││ │ └──────────────────────┘ └──────────────────────┘ │ ││ │ │ ││ └─────────────────────────────────────────────────────────────┘ ││ │└────────────────────────────────────────────────────────────────────────┘13.2 LVM Installation and Setup
Section titled “13.2 LVM Installation and Setup”Installing LVM
Section titled “Installing LVM”# Debian/Ubuntusudo apt-get install lvm2
# RHEL/CentOS/Fedorasudo yum install lvm2# orsudo dnf install lvm2
# Arch Linuxsudo pacman -S lvm2
# Verify installationlvm versionPreparing Disks
Section titled “Preparing Disks”# Using fdisk to create partitionsudo fdisk /dev/sdb
# In fdisk:# n - new partition# p - primary partition# 1 - partition number# Enter - default first sector# Enter - default last sector (use whole disk)# t - change partition type# 8e - Linux LVM type# w - write and quit
# Alternative: Use partedsudo parted /dev/sdb(parted) mklabel gpt(parted) mkpart primary 0% 100%(parted) set 1 lvm on(parted) quit13.3 Creating LVM Volumes
Section titled “13.3 Creating LVM Volumes”Step 1: Create Physical Volumes
Section titled “Step 1: Create Physical Volumes”# Create PV on single disksudo pvcreate /dev/sdb1
# Create PV on multiple diskssudo pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1
# Force creation (if disk has existing data)sudo pvcreate -ff /dev/sdb1
# View PV informationsudo pvs # Brief summarysudo pvdisplay # Detailed infosudo pvdisplay /dev/sdb1 # Specific PV
# PV details with specific fieldssudo pvs -o +pv_used,pv_freeStep 2: Create Volume Group
Section titled “Step 2: Create Volume Group”# Create VG from single PVsudo vgcreate vg_data /dev/sdb1
# Create VG from multiple PVssudo vgcreate vg_data /dev/sdb1 /dev/sdc1
# Create VG with specific PE sizesudo vgcreate -s 8M vg_data /dev/sdb1
# Add PV to existing VGsudo vgextend vg_data /dev/sdd1
# Remove PV from VGsudo vgreduce vg_data /dev/sdd1
# View VG informationsudo vgs # Brief summarysudo vgs -a # Show all VGssudo vgdisplay # Detailed infosudo vgdisplay vg_data # Specific VGStep 3: Create Logical Volume
Section titled “Step 3: Create Logical Volume”# Create LV with fixed sizesudo lvcreate -L 10G -n lv_data vg_data
# Create LV using all free spacesudo lvcreate -l 100%FREE -n lv_backup vg_data
# Create LV using percentage of VGsudo lvcreate -l 50%VG -n lv_data vg_data
# Create LV with specific PE countsudo lvcreate -l 2560 -n lv_data vg_data
# Create linear LV (default)sudo lvcreate -L 20G -n lv_linear vg_data
# Create striped LV (for performance)sudo lvcreate -L 20G -i 3 -n lv_striped vg_data# -i 3 = 3 stripes across PVs
# Create mirrored LVsudo lvcreate -L 10G -m 1 -n lv_mirror vg_data# -m 1 = 1 mirror copy
# View LV informationsudo lvs # Brief summarysudo lvs -a # Show all LVssudo lvdisplay # Detailed infosudo lvdisplay /dev/vg_data/lv_dataStep 4: Create Filesystem and Mount
Section titled “Step 4: Create Filesystem and Mount”# Create ext4 filesystemsudo mkfs.ext4 /dev/vg_data/lv_data
# Create XFS filesystemsudo mkfs.xfs /dev/vg_data/lv_data
# Create with specific block sizesudo mkfs.ext4 -b 4096 /dev/vg_data/lv_data
# Mount temporarilysudo mount /dev/vg_data/lv_data /mnt/data
# Mount persistently (/etc/fstab)# /dev/mapper/vg_data-lv_data /mnt/data ext4 defaults 0 2
# Verify mountdf -h /mnt/data13.4 LVM Management Operations
Section titled “13.4 LVM Management Operations”Extending Logical Volumes
Section titled “Extending Logical Volumes”# Extend LV by specific sizesudo lvextend -L +10G /dev/vg_data/lv_data
# Extend LV to specific sizesudo lvextend -L 50G /dev/vg_data/lv_data
# Extend to use all free spacesudo lvextend -l +100%FREE /dev/vg_data/lv_data
# Extend with specific PEsudo lvextend -l +2560 /dev/vg_data/lv_data
# Online extension (without unmounting)# For ext4sudo resize2fs /dev/vg_data/lv_data
# For XFS (online, no unmount needed)sudo xfs_growfs /mnt/data
# Check resultsudo lvsdf -h /mnt/dataReducing Logical Volumes
Section titled “Reducing Logical Volumes”# WARNING: Reducing LV can cause data loss!# Always backup first!
# Unmount the filesystemsudo umount /mnt/data
# Check filesystemsudo e2fsck -f /dev/vg_data/lv_data
# Resize filesystem to new smaller sizesudo resize2fs /dev/vg_data/lv_data 20G
# Reduce LVsudo lvreduce -L 20G /dev/vg_data/lv_data
# Remountsudo mount /dev/vg_data/lv_data /mnt/data
# Alternative: Use lvreduce with -r (auto-resize filesystem)# This combines the above steps:sudo lvreduce -r -L 20G /dev/vg_data/lv_dataRemoving LVM Components
Section titled “Removing LVM Components”# Remove LVsudo lvremove /dev/vg_data/lv_data# Will ask for confirmation
# Force remove (with -f)sudo lvremove -f /dev/vg_data/lv_data
# Remove VGsudo vgremove vg_data# Must remove all LVs first
# Remove PVsudo pvremove /dev/sdb1# Must not be part of any VGMoving Data Between PVs
Section titled “Moving Data Between PVs”# Move all extents from one PV to otherssudo pvmove /dev/sdb1
# Move specific extentssudo pvmove /dev/sdb1 /dev/sdc1
# Move with progresssudo pvmove -i 10 /dev/sdb1
# Note: Can take long time for large volumes# Use screen or tmux for long operations13.5 LVM Snapshots
Section titled “13.5 LVM Snapshots”Creating Snapshots
Section titled “Creating Snapshots”# Create snapshot (read-only by default)sudo lvcreate -L 5G -s -n lv_snap /dev/vg_data/lv_data
# Create writable snapshotsudo lvcreate -L 5G -s -n lv_snap_rw /dev/vg_data/lv_data# Note: Changes won't persist after merge
# Create snapshot with specific extentssudo lvcreate -l 1000 -s -n lv_snap /dev/vg_data/lv_data
# View snapshotssudo lvs -asudo lvdisplay /dev/vg_data/lv_snapMounting and Using Snapshots
Section titled “Mounting and Using Snapshots”# Mount snapshot (read-only)sudo mkdir /mnt/snapsudo mount -o ro /dev/vg_data/lv_snap /mnt/snap
# Browse snapshot contentsls /mnt/snapsudo cp -a /mnt/snap /backup/snapshot-$(date +%Y%m%d)
# Unmountsudo umount /mnt/snapRestoring from Snapshot
Section titled “Restoring from Snapshot”# Method 1: Merge snapshot into original# This reverts the original LV to snapshot state# All changes since snapshot creation are lost
# Unmount original filesystemsudo umount /mnt/data
# Merge snapshotsudo lvconvert --merge /dev/vg_data/lv_snap
# Mount restored volumesudo mount /dev/vg_data/lv_data /mnt/data
# Note: Snapshot is removed after merge
# Method 2: Create new LV from snapshotsudo lvcreate -L 10G -n lv_restored -s /dev/vg_data/lv_data
# Mount and copy datasudo mount /dev/vg_data/lv_restored /mnt/restoredcp -a /mnt/restored/* /new/location/Snapshot Maintenance
Section titled “Snapshot Maintenance”# View snapshot statussudo lvdisplay /dev/vg_data/lv_snap
# Check snapshot space usagesudo lvs -o+lv_sn%_size
# Remove snapshotsudo lvremove /dev/vg_data/lv_snap
# Extend snapshotsudo lvextend -L +2G /dev/vg_data/lv_snap
# Check what snapshot is fullsudo lvs -a | grep snap13.6 LVM Thin Provisioning
Section titled “13.6 LVM Thin Provisioning”Thin Pool Creation
Section titled “Thin Pool Creation”# Create thin poolsudo lvcreate -L 100G --thinpool vg_data/thin_pool
# Create thin pool with metadata sizesudo lvcreate -L 100G --metadatavol 1G --thinpool vg_data/thin_pool
# View thin poolsudo lvs -asudo lvs -o +thin_pool,thin_size,data_sizeThin Volume Creation
Section titled “Thin Volume Creation”# Create thin volume (virtual size can exceed physical)sudo lvcreate -V 200G --thin -n thin_data vg_data/thin_pool
# View thin volumessudo lvs -a
# Create thin volume with specific virtual sizesudo lvcreate -V 500G --thin -n thin_archive vg_data/thin_pool
# Extend thin poolsudo lvextend -L +50G vg_data/thin_poolThin Pool Monitoring
Section titled “Thin Pool Monitoring”# Monitor thin pool usagesudo lvs -o lv_name,thin_size,data_size,metadata_size,thin_percent
# Check pool full percentagesudo lvs -o lv_name,data_percent
# Example output:# LV Thin Size Data Size Metadata %Used# thin_pool 100.00g 100.00g 1.00g 45.00# thin_data 500.00g Pool dataThin Pool Management
Section titled “Thin Pool Management”# Enable autoextensionsudo lvchange --autobackup y --thinpool vg_data/thin_pool
# Set autoextend thresholdsudo lvchange --thinpool_autoextend_threshold 80 --thinpool_autoextend_percent 20 vg_data/thin_pool
# Trim unused space (if supported)sudo fstrim /mnt/thin_data13.7 LVM RAID and Mirroring
Section titled “13.7 LVM RAID and Mirroring”RAID Levels with LVM
Section titled “RAID Levels with LVM”| RAID Level | LVM Option | Description |
|---|---|---|
| RAID 0 | -i | Striping (performance, no redundancy) |
| RAID 1 | -m 1 | Mirroring (redundancy) |
| RAID 4 | -m 1 —type raid4 | Dedicated parity |
| RAID 5 | -m 1 —type raid5 | Distributed parity |
| RAID 6 | -m 2 —type raid6 | Double parity |
| RAID 10 | -m 1 -i 2 | Striped mirrors |
Creating Mirrored LV
Section titled “Creating Mirrored LV”# Create 2-way mirrorsudo lvcreate -L 20G -m 1 -n lv_mirror vg_data
# Create 2-way mirror on specific PVssudo lvcreate -L 20G -m 1 -n lv_mirror vg_data /dev/sdb1 /dev/sdc1 /dev/sdd1
# Convert existing LV to mirrorsudo lvconvert -m 1 /dev/vg_data/lv_data /dev/sdc1
# Remove mirror (keep data on one disk)sudo lvconvert -m 0 /dev/vg_data/lv_data /dev/sdb1Creating Striped LV
Section titled “Creating Striped LV”# Create 3-stripe RAID0sudo lvcreate -L 30G -i 3 -n lv_striped vg_data
# Create stripe on specific PVssudo lvcreate -L 30G -i 3 -n lv_striped vg_data /dev/sdb1 /dev/sdc1 /dev/sdd1
# Extend striped LV (must add PVs)sudo vgextend vg_data /dev/sde1sudo lvextend -i 3 -l +100%FREE /dev/vg_data/lv_stripedRAID Maintenance
Section titled “RAID Maintenance”# Check RAID statussudo lvs -a -o lv_name,lv_health_status,devices
# Repair failed mirrorsudo lvconvert --repair /dev/vg_data/lv_mirror
# Replace failed disksudo lvconvert --replace /dev/sdb1 /dev/vg_data/lv_mirror /dev/sdc1
# Monitor resyncsudo lvs -o lv_name,sync_percent13.8 LVM Cache
Section titled “13.8 LVM Cache”Creating Cache
Section titled “Creating Cache”# Create cache pool from fast SSDsudo lvcreate -L 20G -n cache_pool vg_data /dev/ssd1
# Create cache volume from poolsudo lvcreate -L 20G -n cache_data vg_data /dev/ssd1
# Attach cache to LVsudo lvconvert --type cache-pool /dev/vg_data/lv_data# Or using cache poolsudo lvconvert --type cache --cachepool vg_data/cache_pool /dev/vg_data/lv_data
# Verify cachesudo lvs -o lv_name,cache_mode,cache_policyCache Modes
Section titled “Cache Modes”| Mode | Description | Best For |
|---|---|---|
writethrough | Write to both, acknowledge after slow | Data safety priority |
writeback | Write to fast first, then slow | Performance priority |
passthrough | Bypass cache | Migration/safety |
Cache Management
Section titled “Cache Management”# Change cache modesudo lvchange --cachemode writethrough /dev/vg_data/lv_data
# Detach cache (split cache pool)sudo lvconvert --splitcache /dev/vg_data/lv_data
# Remove cachesudo lvremove /dev/vg_data/cache_pool13.9 LVM Troubleshooting
Section titled “13.9 LVM Troubleshooting”Common Issues and Solutions
Section titled “Common Issues and Solutions”| Issue | Symptoms | Solution |
|---|---|---|
| PV not found | VG won’t activate | Check device, run pvscan |
| No free space | LV extension fails | Add PV or remove snapshots |
| Snapshot full | Read-only snapshot | Extend snapshot or remove |
| VG activation fails | System won’t boot | Check /etc/lvm/archive |
| Corrupted metadata | Can’t access VG | Restore from archive |
| Disk failure | PV missing | Use pvmove, replace disk |
Diagnostic Commands
Section titled “Diagnostic Commands”# Scan for all PVssudo pvscan
# Scan for VGssudo vgscan
# Scan for LVssudo lvscan
# Display detailed PV infosudo pvdisplay -v
# Display detailed VG infosudo vgdisplay -v
# Display detailed LV infosudo lvdisplay -v
# Check LVM configurationsudo lvm dumpconfig
# View LVM logssudo less /var/log/lvm.log
# Kernel device mapper infosudo dmsetup infosudo dmsetup table
# Check /proc/mdstat for LVM devicescat /proc/mdstatMetadata Recovery
Section titled “Metadata Recovery”# List archived VG metadatals /etc/lvm/archive/
# Restore VG from archivesudo vgcfgrestore -f /etc/lvm/archive/vg_data_00001-123456789.vg vg_data
# List backup filessudo vgcfgrestore --list vg_data
# Force restore (if needed)sudo vgcfgrestore -f /etc/lvm/archive/vg_data_*.vg vg_dataEmergency Procedures
Section titled “Emergency Procedures”# If PV is missing but data intactsudo vgchange -a n vg_data # Deactivatesudo vgchange -a y vg_data # Reactivate (if fixed)
# Recreate missing PVsudo pvcreate --restorefile /etc/lvm/backup/vg_data /dev/sdb1sudo vgcfgrestore vg_data
# If LVM won't startsudo lvm pvscansudo vgscansudo lvchange -a y /dev/vg_data/lv_data
# Emergency mount (read-only)sudo mount -o ro /dev/mapper/vg_data-lv_data /mnt/rescue13.10 Production Examples
Section titled “13.10 Production Examples”Database Server LVM Setup
Section titled “Database Server LVM Setup”# Physical volumes on fast storagesudo pvcreate /dev/nvme0n1p1sudo pvcreate /dev/nvme0n1p2
# Create volume groupssudo vgcreate vg_db /dev/nvme0n1p1sudo vgcreate vg_logs /dev/nvme0n1p2
# Database data (fast SSD/NVMe)sudo lvcreate -L 200G -n lv_data vg_dbsudo mkfs.xfs /dev/vg_db/lv_data
# Database logssudo lvcreate -L 100G -n lv_logs vg_logssudo mkfs.xfs /dev/vg_logs/lv_logs
# Archive/backup (slow storage)sudo pvcreate /dev/sdb1sudo vgcreate vg_backup /dev/sdb1sudo lvcreate -L 500G -n lv_backup vg_backupWeb Server LVM Setup
Section titled “Web Server LVM Setup”# Create PVsudo pvcreate /dev/sdb1
# Create VGsudo vgcreate vg_web /dev/sdb1
# Webrootsudo lvcreate -L 50G -n lv_www vg_websudo mkfs.ext4 /dev/vg_web/lv_www
# Logs (separate to manage space)sudo lvcreate -L 20G -n lv_log vg_websudo mkfs.ext4 /dev/vg_web/lv_log
# Tmpsudo lvcreate -L 10G -n lv_tmp vg_websudo mkfs.ext4 /dev/vg_web/lv_tmpBackup and Snapshot Strategy
Section titled “Backup and Snapshot Strategy”# Daily snapshot of data LVsudo lvcreate -L 20G -s -n lv_data_daily /dev/vg_data/lv_data
# Weekly snapshotsudo lvcreate -L 40G -s -n lv_data_weekly /dev/vg_data/lv_data
# Backup script#!/bin/bash# /usr/local/bin/backup-lvm.shset -eDATE=$(date +%Y%m%d)sudo lvcreate -L 20G -s -n lv_backup_$DATE /dev/vg_data/lv_datasudo mount -o ro /dev/vg_data/lv_backup_$DATE /mnt/snapsudo tar -czf /backup/lv_data_$DATE.tar.gz -C /mnt/snap .sudo umount /mnt/snapsudo lvremove -f /dev/vg_data/lv_backup_$DATE13.11 Interview Questions
Section titled “13.11 Interview Questions”Q1: What are the three main components of LVM?
Section titled “Q1: What are the three main components of LVM?”Answer:
- Physical Volume (PV): A partition or whole disk that has been initialized for use by LVM. Contains LVM headers and physical extents.
- Volume Group (VG): A collection of one or more PVs. Acts as a pool of storage that can be allocated to logical volumes.
- Logical Volume (LV): Virtual partitions created from a VG. These are what users and applications interact with, like regular partitions.
Data flow: Physical disks/partitions (PV) → Pooled storage (VG) → Virtual partitions (LV) → Filesystem
Q2: What is the difference between LVM thin provisioning and regular LVM?
Section titled “Q2: What is the difference between LVM thin provisioning and regular LVM?”Answer:
- Regular LVM: Logical volumes are allocated with a fixed size that uses physical space immediately
- Thin Provisioning: Logical volumes can be created with virtual sizes larger than physically available storage. Space is allocated only as data is written
Benefits of thin provisioning:
- Over-commitment of storage
- More efficient space utilization
- Ability to create many large volumes
- Better for virtual environments
Q3: How do you extend a logical volume online?
Section titled “Q3: How do you extend a logical volume online?”Answer:
# Extend the LVsudo lvextend -L +10G /dev/vg_data/lv_data
# Resize filesystem (ext4)sudo resize2fs /dev/vg_data/lv_data
# For XFS (online, no unmount needed)sudo xfs_growfs /mount/pointFor ext4, you can extend while mounted in recent kernels. For XFS, always online-extendable.
Q4: What is an LVM snapshot and how does it work?
Section titled “Q4: What is an LVM snapshot and how does it work?”Answer: An LVM snapshot is a read-only copy of a logical volume at a specific point in time. It works using copy-on-write:
- When created, snapshot uses minimal space
- When data changes on original LV, old data is copied to snapshot before overwrite
- Snapshot grows as changes accumulate
- Can be used for backups, testing, or recovery
Snapshots are temporary - they must be explicitly removed or merged.
Q5: How do you recover from a failed disk in an LVM setup?
Section titled “Q5: How do you recover from a failed disk in an LVM setup?”Answer:
- Identify failed disk:
sudo pvsshows failed PV - If mirror/RAID: LVM automatically recovers if configured
- For linear/striped:
- Replace failed disk
- Create new PV:
sudo pvcreate /dev/newdisk - Add to VG:
sudo vgextend vg_name /dev/newdisk - Move data:
sudo pvmove /dev/failed_disk /dev/newdisk - Remove failed:
sudo vgreduce vg_name /dev/failed_disk
Q6: What is the difference between lvreduce and lvresize?
Section titled “Q6: What is the difference between lvreduce and lvresize?”Answer:
- lvreduce: Specifically reduces LV size
- lvresize: Can both increase and decrease LV size
- In practice,
lvresizeis more commonly used as it handles both directions
Both require:
- Unmounting the filesystem (for reduction)
- Running filesystem check (
e2fsck) - Resizing filesystem before reducing LV
Q7: How do you monitor LVM space usage?
Section titled “Q7: How do you monitor LVM space usage?”Answer:
# VG free spacesudo vgs
# LV usagesudo lvs -o lv_name,lv_size,data_percent
# Thin pool usagesudo lvs -o lv_name,thin_size,data_size,metadata_size
# PV spacesudo pvs
# Detailed infosudo pvdisplay -m # shows extent mappingQ8: What are LVM cache volumes and when would you use them?
Section titled “Q8: What are LVM cache volumes and when would you use them?”Answer: LVM cache uses fast storage (SSD) to cache data from slower storage (HDD):
- Creates a small logical volume on fast storage
- Attaches as cache to slow LV
- Frequently accessed data stays in fast storage
- Improves performance without replacing all storage
Use cases:
- Hybrid storage (SSD + HDD)
- Caching database writes
- Accelerating file servers
Quick Reference
Section titled “Quick Reference”Essential Commands
Section titled “Essential Commands”# Physical Volumepvcreate /dev/sdb1pvspvdisplaypvremove /dev/sdb1
# Volume Groupvgcreate vg_data /dev/sdb1vgsvgdisplayvgextend vg_data /dev/sdc1vgreduce vg_data /dev/sdb1vgremove vg_data
# Logical Volumelvcreate -L 10G -n lv_data vg_datalvslvdisplaylvextend -L +10G /dev/vg_data/lv_datalvreduce -L 10G /dev/vg_data/lv_datalvremove /dev/vg_data/lv_data
# Snapshotslvcreate -L 5G -s -n lv_snap /dev/vg_data/lv_datalvconvert --merge /dev/vg_data/lv_snap
# Thin Provisioninglvcreate -L 100G --thinpool vg_data/thin_poollvcreate -V 200G --thin -n thin_vol vg_data/thin_poolDevice Paths
Section titled “Device Paths”| Type | Path |
|---|---|
| LV (mapper) | /dev/mapper/vg_name-lv_name |
| LV (by-id) | /dev/vg_name/lv_name |
| LV (by-uuid) | /dev/disk/by-uuid/uuid |
Common Mistakes & Anti-Patterns
Section titled “Common Mistakes & Anti-Patterns”1. Not Extending Filesystem After LVM Extension
Section titled “1. Not Extending Filesystem After LVM Extension”WRONG:
# Extended LV but forgot filesystemlvextend -L +10G /dev/vgdata/lvmysql# Filesystem still shows old size!df -h /var/lib/mysql# /dev/mapper/vgdata-lvmysql# 50G 45G 5G 90% /var/lib/mysqlCORRECT:
# Extend filesystem after extending LVext4fs /dev/vgdata/lvmysqlresize2fs /dev/vgdata/lvmysql
# OR for xfsxfs_growfs /var/lib/mysql
# Verifydf -h /var/lib/mysqlWhy: LV extension doesn’t automatically grow filesystem - must resize separately.
2. Running Out of Space in Thin Pool
Section titled “2. Running Out of Space in Thin Pool”WRONG:
# Thin pool full - writes fail silently# No space left in pool but VGs show spaceCORRECT:
# Monitor thin pool usagelvs -o+pool_lv
# Set up alerts at 80%# Add metadata space if neededlvextend --poolmetadata +1G vg/pool
# Or expand thin poollvextend -L +10G vg/poolWhy: Thin pools can fill up even with space available - need monitoring.
3. Keeping Too Many Snapshots
Section titled “3. Keeping Too Many Snapshots”WRONG:
# Created daily snapshots for 2 years# Performance degraded, space exhaustedlvscan ACTIVE '/dev/vg/snap_2023_01_01' [10.00 GiB] inherit ACTIVE '/dev/vg/snap_2023_01_02' [10.00 GiB] inherit ... (730 more)CORRECT:
# Automate snapshot cleanup#!/bin/bash# Delete snapshots older than 7 daysfind /dev/vg -name 'snap_*' -mtime +7 | while read snap; do lvremove -f "$snap"done
# Keep rotation policy# Daily: 7 days# Weekly: 4 weeks# Monthly: 12 monthsWhy: Old snapshots accumulate and degrade performance.
4. Reducing LV While Mounted
Section titled “4. Reducing LV While Mounted”WRONG:
# Dangerous! Reducing mounted LV causes data losslvreduce -L -10G /dev/vg/data# Filesystem may be corrupted!CORRECT:
# Unmount firstumount /data
# Check filesysteme2fsck -f /dev/vg/data
# Reduce filesystemresize2fs /dev/vg/data 20G
# Reduce LVlvreduce -L 20G /dev/vg/data
# Remountmount /dev/vg/data /dataWhy: Never reduce mounted filesystems - always unmount first.
5. Not Using pvmove for Maintenance
Section titled “5. Not Using pvmove for Maintenance”WRONG:
# Need to replace failing disk# Can't remove - data would be lostpvs PV VG Fmt Attr PSize PFree /dev/sdb1 vg lvm2 a-- 10.00g 0 /dev/sdc1 vg lvm2 a-- 10.00g 10.00gCORRECT:
# Move data off failing diskpvmove /dev/sdb1
# Remove disk from VGvgreduce vg /dev/sdb1
# Now safe to replace/repair diskWhy: pvmove relocates data without downtime before disk removal.
Summary
Section titled “Summary”In this chapter, you learned:
- ✅ LVM architecture and components (PV, VG, LV)
- ✅ Creating physical volumes, volume groups, and logical volumes
- ✅ Extending and reducing logical volumes
- ✅ LVM snapshots for backups and recovery
- ✅ Thin provisioning for efficient storage
- ✅ LVM RAID and mirroring
- ✅ LVM caching for hybrid storage
- ✅ Troubleshooting and recovery
- ✅ Production configuration examples
- ✅ Interview questions and answers
Next Chapter
Section titled “Next Chapter”Chapter 14: Mounting and fstab
Last Updated: February 2026