Linux LVM Configuration and Management
# Linux LVM Configuration and Management ## Introduction Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. LVM abstracts physic
# Linux LVM Configuration and Management
Introduction
Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. LVM abstracts physical storage into logical volumes, enabling flexible disk management, online resizing, snapshots, and storage pooling without downtime.
This tutorial covers LVM concepts, physical volumes, volume groups, logical volumes, snapshots, resizing, removal, and advanced LVM operations including thin provisioning and LVM with RAID.
By the end of this guide, you will be able to configure and manage LVM storage on Linux systems from physical volumes through logical volume snapshots.
Technical Background
LVM Architecture Layers
Physical Disk (/dev/sda)
↓
Physical Volume (PV) - pvcreate
↓
Volume Group (VG) - vgcreate (pools multiple PVs)
↓
Logical Volume (LV) - lvcreate (allocated from VG)
↓
Filesystem (ext4/XFS) - mkfs on LV
↓
Mount Point (/mnt/data)LVM Components
| Component | Description | Command | |-----------|-------------|---------| | Physical Volume (PV) | Physical disk or partition initialized for LVM | pvcreate | | Volume Group (VG) | Pool of one or more PVs | vgcreate | | Logical Volume (LV) | Virtual block device allocated from VG | lvcreate | | Physical Extent (PE) | Smallest allocatable unit in PV (default 4 MB) | - | | Logical Extent (LE) | Mapped to PE in LV | - |
LVM Advantages
- **Online resizing**: Grow or shrink logical volumes without unmounting (for some filesystems)
- **Snapshots**: Point-in-time copies for backup
- **Striping**: Distribute data across multiple PVs for performance
- **Mirroring**: RAID-like redundancy within LVM
- **Thin provisioning**: Allocate storage on demand
- **Data relocation**: Move data between PVs while online
Prerequisites
- Linux system with root or sudo access
- Available unpartitioned disks or partitions
- lvm2 package installed
- Backup of data before destructive operations
Architecture
LVM Layout Example
Physical Disks:
├── /dev/sdb (500 GB) → PV0
└── /dev/sdc (500 GB) → PV1
Volume Group: vg0 (1 TB total)
├── PV0 (/dev/sdb) - 500 GB
└── PV1 (/dev/sdc) - 500 GB
Logical Volumes:
├── lv-root (50 GB) → / (ext4)
├── lv-home (100 GB) → /home (ext4)
├── lv-var (50 GB) → /var (ext4)
├── lv-database (200 GB) → /var/lib/mysql (XFS)
└── Free space: 600 GB (available for new LVs)Step-by-Step Implementation
Step 1: Install LVM Tools
**Purpose**: Install LVM2 user-space tools.
**Commands**:
# Ubuntu/Debian
sudo apt update
sudo apt install lvm2
# RHEL/CentOS/Fedora
sudo dnf install lvm2
# Verify installation
which pvcreate vgcreate lvcreate
lvm version**Expected output**: LVM commands available; version displayed.
**Verification**:
pvs --version
vgs --version
lvs --versionStep 2: Create Physical Volumes
**Purpose**: Initialize physical disks or partitions for LVM use.
**Commands**:
# List available disks
lsblk
# Create physical volumes (replace /dev/sdb, /dev/sdc with your disks)
sudo pvcreate /dev/sdb
sudo pvcreate /dev/sdc
# Verify physical volumes
sudo pvs
sudo pvdisplay /dev/sdb**Explanation**: `pvcreate` writes LVM metadata to the disk, marking it as available for volume groups. Existing data on the disk will be destroyed.
**Expected output**: Physical volumes created and listed.
**Verification**:
pvs
# Shows PV, VG, format, attributes, size, free spaceStep 3: Create Volume Group
**Purpose**: Pool physical volumes into a volume group.
**Commands**:
# Create volume group named vg0 from two PVs
sudo vgcreate vg0 /dev/sdb /dev/sdc
# Verify volume group
sudo vgs
sudo vgdisplay vg0**Explanation**: `vgcreate` combines physical volumes into a single storage pool. The total VG size is the sum of PV sizes minus LVM metadata overhead.
**Expected output**: Volume group created with combined capacity.
**Verification**:
vgs
# Shows vg0 with total size and free spaceStep 4: Create Logical Volumes
**Purpose**: Allocate logical volumes from the volume group.
**Commands**:
# Create 50 GB logical volume for root
sudo lvcreate -L 50G -n lv-root vg0
# Create 100 GB logical volume for home
sudo lvcreate -L 100G -n lv-home vg0
# Create 50 GB logical volume for var
sudo lvcreate -L 50G -n lv-var vg0
# Verify logical volumes
sudo lvs
sudo lvdisplay vg0/lv-root**Explanation**: `lvcreate` allocates space from the volume group. LVs appear as block devices at `/dev/vg0/lv-root` and `/dev/mapper/vg0-lv-root`.
**Expected output**: Logical volumes created and available as block devices.
**Verification**:
lvs
# Shows LV, VG, attributes, size, origin, move, log, copy%, convert%Step 5: Create Filesystem and Mount Logical Volume
**Purpose**: Format logical volume with filesystem and mount it.
**Commands**:
# Create ext4 filesystem on logical volume
sudo mkfs.ext4 /dev/vg0/lv-home
# Create mount point
sudo mkdir -p /mnt/home
# Mount logical volume
sudo mount /dev/vg0/lv-home /mnt/home
# Add to /etc/fstab for automatic mounting
echo "/dev/mapper/vg0-lv-home /mnt/home ext4 defaults,noatime 0 2" | sudo tee -a /etc/fstab**Expected output**: Logical volume formatted, mounted, and configured for automatic mounting.
**Verification**:
df -h /mnt/home
mount | grep lv-homeStep 6: Extend Logical Volume
**Purpose**: Increase the size of a logical volume.
**Commands**:
# Check free space in volume group
sudo vgs
# Extend logical volume by 20 GB
sudo lvextend -L +20G /dev/vg0/lv-home
# Resize filesystem to fill new space (ext4)
sudo resize2fs /dev/vg0/lv-home
# For XFS filesystem:
sudo xfs_growfs /mnt/home
# Verify new size
df -h /mnt/home**Explanation**: `lvextend` adds space to the LV. `resize2fs` or `xfs_growfs` expands the filesystem to use the new space. Both operations can be performed online (while mounted).
**Expected output**: Logical volume and filesystem increased in size.
**Verification**:
df -h /mnt/home
# Shows increased available spaceStep 7: Create LVM Snapshot
**Purpose**: Create a point-in-time copy of a logical volume for backup.
**Commands**:
# Create snapshot of lv-home (10 GB snapshot size)
sudo lvcreate -L 10G -s -n lv-home-snapshot /dev/vg0/lv-home
# Mount snapshot for read-only access
sudo mkdir -p /mnt/snapshot
sudo mount -o ro /dev/vg0/lv-home-snapshot /mnt/snapshot
# Access files from snapshot
ls /mnt/snapshot
# Unmount and remove snapshot when done
sudo umount /mnt/snapshot
sudo lvremove /dev/vg0/lv-home-snapshot**Explanation**: Snapshots use copy-on-write. Changes to the original LV after snapshot creation are preserved in the snapshot. Snapshot size should be large enough to hold expected changes.
**Expected output**: Snapshot created; files accessible at snapshot mount point.
**Verification**:
lvs
# Shows lv-home-snapshot with origin=lv-homeStep 8: Remove Logical Volume
**Purpose**: Remove an unused logical volume and free space.
**Commands**:
# Unmount logical volume
sudo umount /mnt/data
# Remove logical volume
sudo lvremove /dev/vg0/lv-data
# Confirm removal (type y)
# Verify space is freed in volume group
sudo vgs**Warning**: Removing an LV destroys all data on it. Ensure backups exist before removal.
**Expected output**: Logical volume removed; space returned to volume group.
**Verification**:
vgs
# Shows increased free space in VGConfiguration Examples
LVM Commands Quick Reference
# Physical Volume commands
pvcreate /dev/sdb # Initialize PV
pvremove /dev/sdb # Remove PV
pvs # List PVs
pvdisplay /dev/sdb # Detailed PV info
pvscan # Scan for PVs
# Volume Group commands
vgcreate vg0 /dev/sdb # Create VG
vgextend vg0 /dev/sdc # Add PV to VG
vgreduce vg0 /dev/sdc # Remove PV from VG
vgs # List VGs
vgdisplay vg0 # Detailed VG info
vgsplit # Split VG
# Logical Volume commands
lvcreate -L 10G -n lv0 vg0 # Create LV
lvextend -L +5G /dev/vg0/lv0 # Extend LV
lvreduce -L -5G /dev/vg0/lv0 # Reduce LV (unmount first!)
lvremove /dev/vg0/lv0 # Remove LV
lvs # List LVs
lvdisplay /dev/vg0/lv0 # Detailed LV infoThin Provisioning Example
# Create thin pool
sudo lvcreate -L 100G --thin -n thin-pool vg0
# Create thin volume (virtual size can exceed physical)
sudo lvcreate -V 500G -T vg0/thin-pool -n thin-volume
# Monitor thin pool usage
lvs -a -o name,vg,attr,size,origin,data_percent,metadata_percentLVM with RAID1 Mirroring
# Create mirrored logical volume (2 copies across PVs)
sudo lvcreate -L 50G -m 1 -n lv-mirror vg0
# Verify mirror segments
lvs -a -o name,segtype,devicesVerification
LVM Status Commands
pvs -- Physical volume summary
vgs -- Volume group summary
lvs -- Logical volume summary
pvdisplay -- Detailed PV info
vgdisplay -- Detailed VG info
lvdisplay -- Detailed LV info
pvscan -- Scan all disks for PVs
vgscan -- Scan for VGs
lvscan -- Scan for LVsExpected Output
PV VG Fmt Attr PSize PFree
/dev/sdb vg0 lvm2 a-- <500.00g 400.00g
/dev/sdc vg0 lvm2 a-- <500.00g 400.00g
VG #PV #LV #SN Attr VSize VFree
vg0 2 4 0 wz--n- <1.00t 800.00g
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv-home vg0 -wi-ao---- 100.00g
lv-root vg0 -wi-ao---- 50.00g
lv-var vg0 -wi-ao---- 50.00g Troubleshooting
| Symptom | Possible Cause | Diagnostic Command | Corrective Action | Verification | |---------|---------------|-------------------|-------------------|--------------| | pvcreate fails | Disk already has filesystem or partition | `wipefs -a /dev/sdb` | Wipe disk or use empty partition | PV created successfully | | vgcreate fails | PV already in another VG | `pvs` | Remove PV from other VG or use different disk | VG created | | lvcreate fails | Insufficient free space in VG | `vgs` | Add PV to VG or reduce LV size | LV created | | Cannot extend filesystem | Filesystem doesn't support online resize | `df -T` | Unmount and resize, or use XFS/ext4 with online resize | Filesystem resized | | Snapshot full | Snapshot ran out of space | `lvs` | Extend snapshot or merge/remove | Snapshot healthy | | VG not found after reboot | LVM not scanning disks | `vgscan`; `vgchange -ay` | Activate VG | VG active | | LV not available | LV not activated | `lvscan` | `lvchange -ay /dev/vg0/lv0` | LV active | | Data missing after snapshot | Snapshot not mounted correctly | `mount | grep snapshot` | Mount snapshot read-only | Data accessible |
Best Practices
1. **Leave free space in VG**: Maintain 10-20% free space for snapshots and extensions. 2. **Use descriptive names**: Name VGs and LVs by purpose (e.g., vg-data, lv-database). 3. **Monitor VG free space**: Alert when VG free space drops below 20%. 4. **Test snapshot removal**: Snapshots consume space as original changes; remove when no longer needed. 5. **Use XFS for large LVs**: XFS handles large filesystems better than ext4. 6. **Backup LV metadata**: `vgcfgbackup` and `vgcfgrestore` for disaster recovery. 7. **Don't fill snapshots 100%**: Full snapshots become invalid and are automatically removed. 8. **Use thin provisioning carefully**: Monitor thin pool usage to avoid running out of physical space. 9. **Document LVM layout**: Record PV, VG, LV names, sizes, and purposes. 10. **Test LVM recovery**: Practice restoring VG from metadata backup.
Conclusion
Linux LVM provides flexible, powerful storage management that overcomes the limitations of traditional partitioning. With physical volumes, volume groups, and logical volumes, administrators can create, resize, snapshot, and manage storage dynamically without downtime. LVM's snapshot feature enables consistent backups, while thin provisioning and RAID support provide advanced storage capabilities. Mastery of LVM is essential for Linux system administrators managing storage in production environments.
References
- LVM2 HOWTO (https://tldp.org/HOWTO/LVM-HOWTO/)
- Red Hat LVM Administration (https://access.redhat.com/documentation/)
- Linux LVM Documentation (https://sourceware.org/lvm2/)
- Ubuntu LVM Guide (https://ubuntu.com/server/docs/lvm)
- lvm2 man pages (pvcreate, vgcreate, lvcreate, etc.)