Back to Articles
Linux

Logical Volume Management (LVM): Flexible Storage Administration

Understanding Physical Volumes, Volume Groups, and Logical Volumes

Alex Lux2023-11-248 min read
LinuxLVMStorageVolume ManagementRHCSA
Logical Volume Management (LVM): Flexible Storage Administration
Use this link for review flash cards

Logical Volume Management (LVM): Flexible Storage Administration

Logical Volume Management (LVM) provides a flexible approach to managing disk space that goes beyond traditional partitions. LVM allows you to create virtual storage containers that can be resized, moved, and managed dynamically without repartitioning. This article explores the LVM architecture and how to manage physical volumes, volume groups, and logical volumes.

Understanding LVM Architecture

LVM maps whole physical devices and partitions into virtual containers called volume groups, which can be divided into logical volumes. This abstraction provides flexibility that traditional partitions cannot offer.

LVM Components

Physical Volume (PV):

  • A physical disk or partition prepared for LVM
  • Created from block devices using pvcreate
  • Contains metadata about the physical storage

Volume Group (VG):

  • A collection of physical volumes
  • Acts as a storage pool
  • Can span multiple physical disks
  • Divided into logical volumes

Logical Volume (LV):

  • A virtual partition created from a volume group
  • Can be resized dynamically
  • Appears as a regular block device (e.g., /dev/vgname/lvname)
  • Can be formatted with a filesystem

Device Mapper:

  • Kernel-level framework that maps logical block devices onto physical devices
  • Used by LVM to manage logical volumes
  • Creates virtual devices and passes data to physical devices

Physical Volume Management

Creating Physical Volumes

pvcreate: Prepare a device or partition for use in LVM

Basic Usage:

# Create physical volume from entire disk
sudo pvcreate /dev/sdb

# Create physical volume from partition
sudo pvcreate /dev/sda3

# Create multiple physical volumes
sudo pvcreate /dev/sdb /dev/sdc

Displaying Physical Volume Information

pvdisplay: Show detailed information about physical volumes

# Display all physical volumes
sudo pvdisplay

# Display specific physical volume
sudo pvdisplay /dev/sdb

# Brief display
pvs

pvs: Brief summary of physical volumes

pvs
# Output: PV         VG     Fmt  Attr PSize  PFree
#         /dev/sdb   myvg   lvm2 a--  100.00g 50.00g

Physical Volume Operations

pvscan: Scan all disks for physical volumes

sudo pvscan

pvchange: Change physical volume attributes

sudo pvchange -x y /dev/sdb  # Allow allocation
sudo pvchange -x n /dev/sdb  # Prevent allocation

pvremove: Remove a physical volume

# Remove physical volume (must be removed from VG first)
sudo pvremove /dev/sdb

pvck: Check physical volume metadata

sudo pvck /dev/sdb

Volume Group Management

Creating Volume Groups

vgcreate: Create a volume group from physical volumes

Basic Usage:

# Create volume group from physical volumes
sudo vgcreate myvg /dev/sdb /dev/sdc

# Create with specific extent size
sudo vgcreate -s 32M myvg /dev/sdb

Common Options:

  • -s <size>: Set physical extent size (default: 4MB)
  • -l <max_lv>: Maximum number of logical volumes
  • -p <max_pv>: Maximum number of physical volumes

Displaying Volume Group Information

vgdisplay: Show detailed volume group information

# Display all volume groups
sudo vgdisplay

# Display specific volume group
sudo vgdisplay myvg

vgs: Brief summary of volume groups

vgs
# Output: VG   #PV #LV #SN Attr   VSize  VFree
#         myvg   2   3   0 wz--n- 200.00g 50.00g

Modifying Volume Groups

vgextend: Add physical volumes to a volume group

# Add physical volume to volume group
sudo vgextend myvg /dev/sdd

vgreduce: Remove physical volumes from a volume group

# Remove physical volume (must move data first)
sudo vgreduce myvg /dev/sdb

vgchange: Change volume group attributes

# Activate volume group
sudo vgchange -a y myvg

# Deactivate volume group
sudo vgchange -a n myvg

vgrename: Rename a volume group

sudo vgrename oldvg newvg

vgsplit: Split a volume group

sudo vgsplit myvg newvg /dev/sdb

Removing Volume Groups

vgremove: Remove a volume group

# Remove volume group (must remove LVs first)
sudo vgremove myvg

vgscan: Scan for volume groups

sudo vgscan

vgck: Check volume group metadata

sudo vgck myvg

Logical Volume Management

Creating Logical Volumes

lvcreate: Create a logical volume from a volume group

Basic Usage:

# Create logical volume with specific size
sudo lvcreate -L 10G -n mylv myvg

# Create logical volume using all free space
sudo lvcreate -l 100%FREE -n mylv myvg

# Create logical volume using percentage of free space
sudo lvcreate -l 50%FREE -n mylv myvg

# Create logical volume with specific number of extents
sudo lvcreate -l 100 -n mylv myvg

Common Options:

  • -L <size>: Size in bytes, KB, MB, GB, TB
  • -l <extents>: Size in logical extents
  • -n <name>: Logical volume name
  • -i <stripes>: Number of stripes (for performance)
  • -I <stripe_size>: Stripe size

Example:

# Create 20GB logical volume named 'data'
sudo lvcreate -L 20G -n data myvg

Displaying Logical Volume Information

lvdisplay: Show detailed logical volume information

# Display all logical volumes
sudo lvdisplay

# Display specific logical volume
sudo lvdisplay /dev/myvg/mylv

lvs: Brief summary of logical volumes

lvs
# Output: LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
#         mylv myvg -wi-ao---- 10.00g

Resizing Logical Volumes

lvextend: Extend a logical volume

# Extend by specific size
sudo lvextend -L +5G /dev/myvg/mylv

# Extend to specific size
sudo lvextend -L 15G /dev/myvg/mylv

# Extend using all free space
sudo lvextend -l +100%FREE /dev/myvg/mylv

After Extending: Resize the filesystem

# For ext2/ext3/ext4
sudo resize2fs /dev/myvg/mylv

# For XFS (can only grow)
sudo xfs_growfs /mnt/data

lvreduce: Reduce a logical volume (risky, requires filesystem shrink first)

# Reduce filesystem first (ext4 example)
sudo resize2fs /dev/myvg/mylv 10G
sudo lvreduce -L 10G /dev/myvg/mylv

lvresize: Resize a logical volume (combines extend/reduce)

# Resize to specific size
sudo lvresize -L 15G /dev/myvg/mylv
sudo resize2fs /dev/myvg/mylv

Modifying Logical Volumes

lvchange: Change logical volume attributes

# Activate logical volume
sudo lvchange -a y /dev/myvg/mylv

# Deactivate logical volume
sudo lvchange -a n /dev/myvg/mylv

lvrename: Rename a logical volume

sudo lvrename myvg oldlv newlv

Removing Logical Volumes

lvremove: Remove a logical volume

# Remove logical volume (unmount first)
sudo umount /mnt/data
sudo lvremove /dev/myvg/mylv

Complete LVM Workflow Example

Step 1: Create Physical Volumes

sudo pvcreate /dev/sdb /dev/sdc

Step 2: Create Volume Group

sudo vgcreate myvg /dev/sdb /dev/sdc

Step 3: Create Logical Volume

sudo lvcreate -L 50G -n data myvg

Step 4: Format Logical Volume

sudo mkfs.ext4 /dev/myvg/data

Step 5: Mount Logical Volume

sudo mkdir /mnt/data
sudo mount /dev/myvg/data /mnt/data

Step 6: Add to /etc/fstab

# Add to /etc/fstab for persistence
/dev/myvg/data  /mnt/data  ext4  defaults  0  2

Advanced LVM Features

Extending Storage

Add New Disk to Existing Volume Group:

# 1. Create physical volume
sudo pvcreate /dev/sdd

# 2. Extend volume group
sudo vgextend myvg /dev/sdd

# 3. Extend logical volume
sudo lvextend -l +100%FREE /dev/myvg/data

# 4. Resize filesystem
sudo resize2fs /dev/myvg/data

Moving Data Between Physical Volumes

pvmove: Move data from one physical volume to another

# Move data from /dev/sdb to /dev/sdc
sudo pvmove /dev/sdb /dev/sdc

# Move all data from a physical volume
sudo pvmove /dev/sdb

Snapshots

Create Snapshot:

# Create snapshot of logical volume
sudo lvcreate -L 5G -s -n snapshot /dev/myvg/data

Restore from Snapshot:

# Restore logical volume from snapshot
sudo lvconvert --merge /dev/myvg/snapshot

Device Mapper and DM-Multipath

Device Mapper

Purpose: Kernel-level framework for logical block device management

How It Works:

  • Creates virtual devices
  • Maps logical block devices to physical devices
  • Used by LVM, software RAID, and encryption

Location: /dev/mapper/

DM-Multipath

Purpose: Provides multiple paths to storage devices for fault tolerance and load balancing

Use Cases:

  • SAN storage with multiple paths
  • High availability storage
  • Load balancing across paths

LVM vs Traditional Partitions

Advantages of LVM

  1. Flexibility: Resize volumes without repartitioning
  2. Spanning: Use multiple disks as one volume group
  3. Snapshots: Create point-in-time copies
  4. Striping: Improve performance across multiple disks
  5. Mirroring: Data redundancy
  6. Online Resizing: Resize without unmounting (in many cases)

When to Use LVM

  • Need flexible storage management
  • Expect to resize volumes frequently
  • Want to span multiple disks
  • Need snapshots for backups
  • Enterprise environments requiring flexibility

When to Use Traditional Partitions

  • Simple, single-disk setups
  • Boot partitions (some systems)
  • When LVM overhead is not needed
  • Legacy system compatibility

Best Practices

Planning

  1. Plan Volume Groups: Organize by function or department
  2. Reserve Space: Keep some free space in volume groups
  3. Document Layout: Keep records of LVM structure
  4. Regular Backups: Backup LVM metadata

Performance

  1. Stripe Across Disks: Use -i option for performance
  2. Align with Physical Extents: Match extent sizes
  3. Monitor Usage: Regularly check vgs and lvs
  4. Avoid Fragmentation: Plan logical volume sizes

Maintenance

  1. Regular Scans: Use pvscan, vgscan, lvscan
  2. Check Metadata: Use pvck, vgck
  3. Monitor Growth: Track logical volume usage
  4. Test Procedures: Practice resize operations in test environment

Troubleshooting

Common Issues

Volume Group Not Found:

# Scan for volume groups
sudo vgscan

# Activate volume group
sudo vgchange -a y myvg

Logical Volume Not Accessible:

# Check if activated
sudo lvdisplay

# Activate if needed
sudo lvchange -a y /dev/myvg/mylv

Physical Volume Issues:

# Scan for physical volumes
sudo pvscan

# Check physical volume
sudo pvck /dev/sdb

Recovery

Backup LVM Metadata:

# Backup volume group metadata
sudo vgcfgbackup myvg

# Restore volume group metadata
sudo vgcfgrestore myvg

Conclusion

Logical Volume Management provides powerful flexibility for storage administration. By abstracting physical storage into volume groups and logical volumes, LVM enables dynamic resizing, spanning across disks, and advanced features like snapshots. Understanding physical volumes, volume groups, and logical volumes is essential for managing modern Linux storage systems.

While LVM adds complexity compared to traditional partitions, the flexibility it provides makes it invaluable for systems requiring dynamic storage management. From simple volume resizing to complex multi-disk configurations, LVM enables administrators to adapt storage to changing requirements without downtime.

In the next article, we'll explore network configuration with NetworkManager, essential for managing network connectivity on modern Linux systems. Stay tuned!

Related Reading