LVM (Logical Volume Management) Cheatsheet

Logical Volume Management (LVM) is a powerful storage management solution for Linux systems that provides flexibility in disk space allocation and management. This cheatsheet covers essential LVM operations for creating, managing, and troubleshooting logical volumes.

LVM Architecture Overview

LVM operates on three main components:

  • Physical Volumes (PV): Raw storage devices or partitions
  • Volume Groups (VG): Collections of physical volumes
  • Logical Volumes (LV): Virtual partitions created from volume groups

Basic LVM Setup

Creating a Complete LVM Setup

# Create a partition (use cfdisk for interactive partitioning)
cfdisk /dev/sdb
 
# Create physical volume
pvcreate /dev/sdb1
 
# Create volume group
vgcreate myvg /dev/sdb1
 
# Create logical volume using all available space
lvcreate -l +100%FREE -n mylv myvg
 
# Create filesystem on logical volume
mkfs.ext4 /dev/myvg/mylv
 
# Mount the logical volume
mkdir /mnt/mylv
mount /dev/myvg/mylv /mnt/mylv

Physical Volume Management

Creating and Managing Physical Volumes

# Create physical volume
pvcreate /dev/sdb1
 
# Display physical volume information
pvdisplay
pvs
 
# Show detailed information about specific PV
pvdisplay /dev/sdb1
 
# Remove physical volume
pvremove /dev/sdb1
 
# Move data from one PV to another within the same VG
pvmove /dev/sdb1 /dev/sdc1
 
# Resize physical volume after partition resize
pvresize /dev/sdb1

Volume Group Management

Creating and Managing Volume Groups

# Create volume group with single PV
vgcreate myvg /dev/sdb1
 
# Create volume group with multiple PVs
vgcreate myvg /dev/sdb1 /dev/sdc1
 
# Display volume group information
vgdisplay
vgs
 
# Add physical volume to existing volume group
vgextend myvg /dev/sdd1
 
# Remove physical volume from volume group
vgreduce myvg /dev/sdd1
 
# Rename volume group
vgrename oldname newname
 
# Remove volume group
vgremove myvg
 
# Activate/deactivate volume group
vgchange -a y myvg  # activate
vgchange -a n myvg  # deactivate

Logical Volume Management

Creating Logical Volumes

# Create logical volume with specific size
lvcreate -L 10G -n mylv myvg
 
# Create logical volume using percentage of VG
lvcreate -l 50%VG -n mylv myvg
 
# Create logical volume using all free space
lvcreate -l +100%FREE -n mylv myvg
 
# Create logical volume with specific number of extents
lvcreate -l 2560 -n mylv myvg

Managing Logical Volumes

# Display logical volume information
lvdisplay
lvs
 
# Show detailed information about specific LV
lvdisplay /dev/myvg/mylv
 
# Rename logical volume
lvrename myvg oldname newname
 
# Remove logical volume
lvremove /dev/myvg/mylv
 
# Activate/deactivate logical volume
lvchange -a y /dev/myvg/mylv  # activate
lvchange -a n /dev/myvg/mylv  # deactivate

Resizing Operations

Extending Logical Volumes

# Extend logical volume to specific size
lvextend -L 20G /dev/myvg/mylv
 
# Extend logical volume by specific amount
lvextend -L +5G /dev/myvg/mylv
 
# Extend logical volume to use all available space
lvextend -l +100%FREE /dev/myvg/mylv
 
# Extend logical volume and resize filesystem in one command
lvextend -l +100%FREE -r /dev/myvg/mylv

Resizing Filesystems After LV Extension

# For ext2/ext3/ext4 filesystems
resize2fs /dev/myvg/mylv
 
# For xfs filesystems
xfs_growfs /mount/point
 
# For reiserfs filesystems
resize_reiserfs /dev/myvg/mylv

Reducing Logical Volumes

Warning: Always backup data before reducing logical volumes. XFS filesystems cannot be reduced.

# First, unmount the filesystem
umount /dev/myvg/mylv
 
# Check filesystem for errors
e2fsck -f /dev/myvg/mylv
 
# Reduce filesystem size (ext2/ext3/ext4)
resize2fs /dev/myvg/mylv 8G
 
# Reduce logical volume size
lvreduce -L 8G /dev/myvg/mylv
 
# Remount filesystem
mount /dev/myvg/mylv /mount/point

Snapshot Management

Creating and Managing Snapshots

# Create snapshot
lvcreate -L 1G -s -n mylv-snapshot /dev/myvg/mylv
 
# Mount snapshot (read-only by default)
mkdir /mnt/snapshot
mount /dev/myvg/mylv-snapshot /mnt/snapshot
 
# Remove snapshot
umount /mnt/snapshot
lvremove /dev/myvg/mylv-snapshot
 
# Merge snapshot back to original LV
lvconvert --merge /dev/myvg/mylv-snapshot

Monitoring and Information

System Information Commands

# Display all LVM information
pvs && vgs && lvs
 
# Show physical volume usage
pvdisplay -m
 
# Show volume group free space
vgdisplay | grep -E "VG Name|Free"
 
# Show logical volume details with size
lvdisplay -C
 
# Check LVM version
lvm version
 
# Display LVM configuration
lvmconfig

Monitoring Disk Usage

# Show filesystem usage on LVM volumes
df -h | grep "/dev/mapper"
 
# Show detailed LV information including size
lvs -o lv_name,vg_name,lv_size,lv_attr
 
# Monitor LVM performance
iostat -x 1
 
# Check for LVM errors in system logs
journalctl -u lvm2-monitor

Advanced Operations

Migration and Backup

# Move logical volume to different physical volumes
pvmove /dev/sdb1
 
# Create exact copy of logical volume
lvcreate -L 10G -s -n backup-lv /dev/myvg/original-lv
 
# Backup volume group metadata
vgcfgbackup myvg
 
# Restore volume group metadata
vgcfgrestore myvg

Performance Optimization

# Create striped logical volume for better performance
lvcreate -L 10G -i 2 -I 64 -n striped-lv myvg
 
# Create mirrored logical volume for redundancy
lvcreate -L 10G -m 1 -n mirror-lv myvg
 
# Enable read-ahead for logical volume
lvchange --readahead 1024 /dev/myvg/mylv

Common Troubleshooting

Fixing Common Issues

# Scan for new physical volumes
pvscan
 
# Activate all volume groups
vgchange -a y
 
# Fix inconsistent volume group metadata
vgck myvg
 
# Repair corrupted physical volume
pvck /dev/sdb1
 
# Force removal of missing physical volumes
vgreduce --removemissing --force myvg
 
# Reactivate logical volume after system issues
lvchange -a n /dev/myvg/mylv
lvchange -a y /dev/myvg/mylv

Recovery Operations

# Recover from missing physical volume
vgreduce --removemissing myvg
 
# Restore from backup when volume group is lost
vgcfgrestore -f /etc/lvm/backup/myvg myvg
 
# Manually activate logical volume
lvchange -a y /dev/myvg/mylv
 
# Force activation of logical volume
lvchange -a y --partial /dev/myvg/mylv

Best Practices

Planning and Design

  • Size Planning: Always leave some free space in volume groups for future expansion
  • Naming Convention: Use descriptive names for volume groups and logical volumes
  • Backup Strategy: Regularly backup LVM metadata with vgcfgbackup
  • Monitoring: Set up monitoring for disk space usage and LVM health

Safety Guidelines

  • Test First: Always test LVM operations in non-production environments
  • Backup Data: Create backups before performing destructive operations
  • Verify Operations: Check results with pvs, vgs, and lvs after changes
  • Document Changes: Keep records of LVM configuration changes

Performance Considerations

  • Stripe Configuration: Use striping across multiple physical volumes for better performance
  • Avoid Fragmentation: Keep logical volumes on contiguous physical extents when possible
  • Monitor I/O: Use tools like iostat to monitor LVM performance
  • Regular Maintenance: Periodically check and optimize LVM configuration

Common Use Cases

Server Storage Management

# Set up storage for database server
pvcreate /dev/sdb1 /dev/sdc1
vgcreate dbvg /dev/sdb1 /dev/sdc1
lvcreate -L 50G -n db-data dbvg
lvcreate -L 10G -n db-logs dbvg
mkfs.ext4 /dev/dbvg/db-data
mkfs.ext4 /dev/dbvg/db-logs

Development Environment

# Create flexible storage for development
pvcreate /dev/sdb1
vgcreate devvg /dev/sdb1
lvcreate -L 20G -n projects devvg
lvcreate -L 10G -n testing devvg
lvcreate -L 5G -n builds devvg

References and Resources

Questions Answered in This Document

Q: How do I extend a logical volume to use all available space in the volume group? A: Use lvextend -l +100%FREE /dev/vgname/lvname to extend the logical volume, then resize2fs /dev/vgname/lvname to resize the filesystem.

Q: What’s the complete process to set up a new disk with LVM? A: Create a partition with cfdisk, create a physical volume with pvcreate, create a volume group with vgcreate, create a logical volume with lvcreate, and format with mkfs.ext4.

Q: How do I safely reduce the size of a logical volume? A: First unmount the filesystem, check it with e2fsck -f, reduce the filesystem with resize2fs, then reduce the logical volume with lvreduce. Always backup data first.

Q: What’s the difference between physical volumes, volume groups, and logical volumes? A: Physical volumes are raw storage devices, volume groups are collections of physical volumes, and logical volumes are virtual partitions created from volume groups.

Q: How do I create a snapshot of a logical volume? A: Use lvcreate -L size -s -n snapshot-name /dev/vgname/lvname to create a snapshot, which can be mounted and used independently.

Q: What should I do if a physical volume fails in my volume group? A: Use vgreduce --removemissing --force vgname to remove the missing physical volume, but this may result in data loss if logical volumes spanned the failed device.

Q: How do I monitor LVM performance and health? A: Use commands like pvs, vgs, lvs for status information, iostat -x for performance monitoring, and journalctl -u lvm2-monitor for error checking.

Q: Can I move data between physical volumes within the same volume group? A: Yes, use pvmove /dev/source-pv /dev/target-pv to move data between physical volumes within the same volume group.

Q: What are the best practices for LVM backup and recovery? A: Regularly backup LVM metadata with vgcfgbackup, create snapshots before major changes, and maintain documentation of your LVM configuration.

Q: How do I create a striped logical volume for better performance? A: Use lvcreate -L size -i number-of-stripes -I stripe-size -n lvname vgname to create a striped logical volume across multiple physical volumes.