BLOG POSTS
    MangoHost Blog / How to Use LVM to Manage Storage Devices on Ubuntu 24
How to Use LVM to Manage Storage Devices on Ubuntu 24

How to Use LVM to Manage Storage Devices on Ubuntu 24

Logical Volume Management (LVM) has become the go-to solution for dynamic storage management on Linux systems, transforming how we handle disk space allocation, expansion, and maintenance. Unlike traditional fixed partitioning schemes, LVM introduces a flexible abstraction layer that allows you to resize, move, and manage storage volumes on-the-fly without downtime. This guide will walk you through setting up and managing LVM on Ubuntu 24, covering everything from basic concepts to advanced troubleshooting scenarios that you’ll actually encounter in production environments.

Understanding LVM Architecture and Components

LVM operates on three fundamental layers that work together to provide flexible storage management. At the bottom layer, Physical Volumes (PVs) represent your actual storage devices – hard drives, SSDs, or even partitions. These PVs are grouped into Volume Groups (VGs), which act as storage pools that you can allocate from. Finally, Logical Volumes (LVs) are carved out of VGs and function like traditional partitions that you can mount and use.

The beauty of this architecture lies in its flexibility. You can add new disks to a volume group, resize logical volumes across multiple physical devices, and even move data between drives while the system is running. This abstraction layer also enables advanced features like snapshots, thin provisioning, and RAID configurations.

Installing and Setting Up LVM on Ubuntu 24

Ubuntu 24 includes LVM tools by default, but let’s ensure everything is properly installed and start with a clean setup.

sudo apt update
sudo apt install lvm2

# Verify installation
lvm version

Before creating LVM structures, identify your available storage devices:

# List all block devices
lsblk

# Check for existing LVM structures
sudo pvs
sudo vgs
sudo lvs

# View detailed disk information
sudo fdisk -l

Let’s create a complete LVM setup using two example drives (/dev/sdb and /dev/sdc):

# Step 1: Initialize physical volumes
sudo pvcreate /dev/sdb /dev/sdc

# Step 2: Create a volume group named 'storage_vg'
sudo vgcreate storage_vg /dev/sdb /dev/sdc

# Step 3: Create logical volumes
sudo lvcreate -L 50G -n web_data storage_vg
sudo lvcreate -L 30G -n database storage_vg
sudo lvcreate -l 100%FREE -n backups storage_vg

# Step 4: Create filesystems
sudo mkfs.ext4 /dev/storage_vg/web_data
sudo mkfs.ext4 /dev/storage_vg/database
sudo mkfs.xfs /dev/storage_vg/backups

Create mount points and configure automatic mounting:

# Create mount directories
sudo mkdir -p /var/www/data /var/lib/mysql/data /opt/backups

# Mount the logical volumes
sudo mount /dev/storage_vg/web_data /var/www/data
sudo mount /dev/storage_vg/database /var/lib/mysql/data
sudo mount /dev/storage_vg/backups /opt/backups

# Add to fstab for persistent mounting
echo '/dev/storage_vg/web_data /var/www/data ext4 defaults 0 2' | sudo tee -a /etc/fstab
echo '/dev/storage_vg/database /var/lib/mysql/data ext4 defaults 0 2' | sudo tee -a /etc/fstab
echo '/dev/storage_vg/backups /opt/backups xfs defaults 0 2' | sudo tee -a /etc/fstab

Managing and Expanding LVM Volumes

One of LVM’s killer features is the ability to resize volumes dynamically. Here’s how to expand a logical volume when you’re running out of space:

# Check current space usage
df -h /var/www/data

# Extend the logical volume by 20GB
sudo lvextend -L +20G /dev/storage_vg/web_data

# Resize the filesystem to use the new space
sudo resize2fs /dev/storage_vg/web_data

# For XFS filesystems, use:
# sudo xfs_growfs /opt/backups

If your volume group doesn’t have enough free space, add more physical storage:

# Add a new drive to the existing volume group
sudo pvcreate /dev/sdd
sudo vgextend storage_vg /dev/sdd

# Now you can extend logical volumes as needed
sudo lvextend -L +100G /dev/storage_vg/backups
sudo xfs_growfs /opt/backups

For more precise control, you can also use percentage-based allocation:

# Extend to use 50% of available VG space
sudo lvextend -l +50%FREE /dev/storage_vg/web_data

# Extend to use all remaining space
sudo lvextend -l +100%FREE /dev/storage_vg/database

Advanced LVM Features and Real-World Applications

LVM snapshots provide point-in-time copies of your data, perfect for backup operations or testing scenarios:

# Create a snapshot of the database volume
sudo lvcreate -L 10G -s -n database_snapshot /dev/storage_vg/database

# Mount the snapshot for backup
sudo mkdir /mnt/db_backup
sudo mount /dev/storage_vg/database_snapshot /mnt/db_backup

# After backup, remove the snapshot
sudo umount /mnt/db_backup
sudo lvremove /dev/storage_vg/database_snapshot

Thin provisioning allows you to allocate more space than physically available, useful for virtual machine environments:

# Create a thin pool
sudo lvcreate -L 100G --thinpool thin_pool storage_vg

# Create thin volumes
sudo lvcreate -V 50G --thin storage_vg/thin_pool -n vm1_disk
sudo lvcreate -V 50G --thin storage_vg/thin_pool -n vm2_disk

Performance Optimization and Best Practices

LVM performance largely depends on your underlying storage configuration and usage patterns. Here’s a comparison of different setups:

Configuration Sequential Read (MB/s) Random Read IOPS Use Case
Single SSD 520 95,000 Small databases, boot volumes
LVM Linear (2 SSDs) 540 98,000 Large file storage
LVM Striped (2 SSDs) 1,040 185,000 High-performance databases
LVM RAID1 (2 SSDs) 520 95,000 Critical data with redundancy

For high-performance applications, consider striped logical volumes:

# Create a striped logical volume across multiple PVs
sudo lvcreate -L 100G -i 2 -I 64k -n high_perf storage_vg

# The -i flag specifies stripe count, -I sets stripe size

Key performance optimization practices:

  • Align your logical volumes to physical extent boundaries using the default 4MB extent size
  • Use appropriate stripe sizes for your workload (64KB for databases, 1MB for large files)
  • Monitor volume group fragmentation with vgs -o +vg_free_count,vg_extent_count
  • Place frequently accessed LVs on faster storage devices within the same VG
  • Use thin provisioning carefully in production – monitor usage to prevent over-allocation

Troubleshooting Common LVM Issues

LVM problems can be tricky to diagnose, but most issues fall into predictable categories. Here are the most common scenarios you’ll encounter:

Volume Group Not Found After Reboot

# Scan for available volume groups
sudo vgscan

# Activate all volume groups
sudo vgchange -ay

# If specific VG isn't activated
sudo vgchange -ay storage_vg

Cannot Extend Volume – No Space Left

# Check available space in volume group
sudo vgs
sudo pvs

# If PV shows as full but VG has space, you might have fragmentation
sudo pvs -o +pv_used,pv_free

# Move extents to consolidate free space
sudo pvmove /dev/sdb

Corrupted LVM Metadata

# Backup metadata before making changes
sudo vgcfgbackup

# Restore from backup if needed
sudo vgcfgrestore storage_vg

# Force scan if metadata is inconsistent
sudo pvscan --cache --activate ay

Removing a Failed Drive

# First, move data off the failing drive
sudo pvmove /dev/sdc

# Remove the physical volume from the volume group
sudo vgreduce storage_vg /dev/sdc

# Remove the PV designation
sudo pvremove /dev/sdc

Monitoring and Maintenance

Regular monitoring prevents most LVM disasters. Set up these monitoring practices:

# Create a monitoring script
cat << 'EOF' > /usr/local/bin/lvm-monitor.sh
#!/bin/bash

# Check VG usage
echo "Volume Group Usage:"
vgs -o vg_name,vg_size,vg_free,vg_free_count

echo -e "\nLogical Volume Usage:"
lvs -o lv_name,lv_size,data_percent,metadata_percent

echo -e "\nPhysical Volume Health:"
pvs -o pv_name,pv_size,pv_free,pv_attr

# Alert if any VG is over 90% full
vgs --noheadings --separator ' ' -o vg_name,vg_free_count | while read vg free_extents; do
    if [ "$free_extents" -lt 1000 ]; then
        echo "WARNING: Volume group $vg is low on space!"
    fi
done
EOF

chmod +x /usr/local/bin/lvm-monitor.sh

# Add to crontab for daily monitoring
echo "0 8 * * * /usr/local/bin/lvm-monitor.sh | mail -s 'LVM Status' admin@domain.com" | crontab -

LVM vs Alternative Storage Solutions

Understanding when to use LVM versus alternatives helps you make informed architectural decisions:

Solution Flexibility Performance Overhead Complexity Best For
Traditional Partitions Low None Low Simple, static setups
LVM High Minimal (~2%) Medium Dynamic environments
ZFS High Moderate (~5-10%) High Data integrity focus
Btrfs High Variable High Modern features, still maturing

LVM excels in scenarios where you need:

  • Frequent storage resizing in virtualized environments
  • Complex storage hierarchies with multiple mount points
  • Snapshot-based backup strategies
  • Migration capabilities between different storage hardware

For comprehensive LVM documentation and advanced configuration options, refer to the Red Hat LVM Administrator Guide and the Linux Kernel Device Mapper documentation.

LVM transforms storage management from a rigid, partition-based approach into a flexible, software-defined solution that adapts to your changing requirements. With proper planning and monitoring, it provides enterprise-grade storage management capabilities that scale from single-server setups to complex multi-tier applications. The investment in learning LVM pays dividends when you inevitably need to expand, reorganize, or migrate your storage infrastructure without service interruption.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked