LVM Partition Management: A Beginner's Guide to Logical Volumes on Linux

Updated on
6 min read

Logical Volume Manager (LVM) is a powerful tool that provides a flexible solution for managing disk storage in Linux. It allows users to pool physical disks, resize logical volumes, create snapshots, and migrate data easily. This guide is designed for beginners familiar with basic Linux commands (like ls, fdisk, and mount) who want to acquire practical LVM skills. By the end of this article, you’ll have the knowledge to understand core LVM concepts, create and manage logical volumes, safely resize them, and troubleshoot common issues.

Core LVM Concepts

Understanding LVM’s core components is essential:

  • Physical Volume (PV): A block device that LVM manages, which can be a whole disk (e.g., /dev/sdb) or a partition (e.g., /dev/sdb1). Use the pvdisplay command to show PV details.

  • Volume Group (VG): A VG pools one or more PVs into a single storage pool. Logical volumes (LVs) are allocated from this pool. The vgdisplay command provides VG information, including free space and extent size.

  • Logical Volume (LV): LVs are the usable block devices you format and mount (e.g., /dev/vg_data/lv_home). Display LV details using the lvdisplay command.

  • Extents: A VG’s space is divided into fixed-size chunks called physical extents (PE). When allocating space to an LV, LVM assigns whole extents, with sizes set during VG creation (commonly between 4MiB and 64MiB).

  • LVM Metadata: LVM stores metadata that describes the relationships between LVs and PVs. Metadata files are found under /etc/lvm/ and in the headers of PVs. Corrupted metadata can risk volume access, so regularly back it up with vgcfgbackup.

Quick commands to inspect the LVM state:

lsblk -f        # Shows block devices and mounts
pvs              # Summary of physical volumes
vgs              # Summary of volume groups
lvs              # Summary of logical volumes
pvdisplay /dev/sdb
vgdisplay vg_data
lvdisplay /dev/vg_data/lv_data

When and Why to Use LVM

LVM is particularly beneficial when you need storage flexibility, such as:

  • Dynamic resizing of storage for VMs, containers, or filesystems.
  • Snapshots for backups and testing scenarios.
  • Pooling multiple disks into a unified namespace and migrating extents between them.

Conversely, LVM might not be the best option for very simple single-disk systems where traditional partitions suffice. Additionally, if an integrated volume manager and filesystem with built-in features like checksumming and RAID are required, consider alternatives like ZFS or Btrfs.

Preparation and Safety

Implement the following safety checklist before changing configurations:

  • Backup important data. Snapshots do not replace the need for backups.
  • Ensure the lvm2 package is installed:
    • For Debian/Ubuntu: sudo apt update && sudo apt install lvm2
    • For RHEL/CentOS/Fedora: sudo yum install lvm2 or sudo dnf install lvm2
    • Refer to the Ubuntu community help on LVM.
  • Carefully identify devices using lsblk -f, blkid, and fdisk -l. It is advisable to use UUIDs in /etc/fstab to avoid issues if device names change.
  • Understand filesystem limitations: XFS cannot be shrunk, while ext4 can often shrink after an offline resize.
  • Ensure console access if performing operations on remote servers to prevent connection loss during critical phases.

Commands to check your environment:

lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,UUID
blkid
sudo fdisk -l

If experimenting, use a virtual machine or spare disks to mitigate risk as LVM operations, while generally safe, can lead to data loss if performed incorrectly.

Basic LVM Workflow — Step-by-step with Commands

Assuming you have a new disk /dev/sdb (or partition /dev/sdb1), follow these steps:

  1. Create a Physical Volume (PV):

    sudo pvcreate /dev/sdb
    # If using a partition:
    # sudo pvcreate /dev/sdb1
    
  2. Create a Volume Group (VG):

    sudo vgcreate vg_data /dev/sdb
    
  3. Create a Logical Volume (LV):

    • To create a 50 GiB LV named lv_data:
    sudo lvcreate -n lv_data -L 50G vg_data
    
    • Or create LV using extents (e.g., 100%VG to use all free space):
    sudo lvcreate -n lv_all -l 100%VG vg_data
    
  4. Create a Filesystem and Mount It:

    • For ext4:
    sudo mkfs.ext4 /dev/vg_data/lv_data
    sudo mkdir -p /mnt/data
    sudo mount /dev/vg_data/lv_data /mnt/data
    
    • For XFS (note: XFS cannot be shrunk later):
    sudo mkfs.xfs /dev/vg_data/lv_data
    sudo mount /dev/vg_data/lv_data /mnt/data
    
  5. Persist Mount in /etc/fstab Using UUID:

    sudo blkid /dev/vg_data/lv_data
    # Output will provide UUID
    # Add the UUID entry to /etc/fstab
    UUID=xxxx-xxxx /mnt/data ext4 defaults 0 2
    
  6. Verify Configuration:

    lsblk -f
    

df -h /mnt/data pvs; vgs; lvs


## Resizing Volumes — Extend and Shrink (with Examples)
### Growing an LV:
1. Add free space to the VG if needed:
```bash
sudo pvcreate /dev/sdc
sudo vgextend vg_data /dev/sdc
  1. Extend the LV and filesystem (ext4 example):
    sudo lvextend -L +10G /dev/vg_data/lv_data
    sudo resize2fs /dev/vg_data/lv_data
    
  2. For XFS:
    sudo lvextend -L +10G /dev/vg_data/lv_data
    sudo xfs_growfs /mnt/data
    

Shrinking an LV:

  1. Unmount and run fsck:
    sudo umount /mnt/data
    sudo e2fsck -f /dev/vg_data/lv_data
    
  2. Resize filesystem:
    sudo resize2fs /dev/vg_data/lv_data 30G
    
  3. Reduce the LV to match:
    sudo lvreduce -L 30G /dev/vg_data/lv_data
    
  4. Remount and verify:
    sudo mount /dev/vg_data/lv_data /mnt/data
    

df -h /mnt/data


**Using `lvresize --resizefs`** combines operations into one command:
```bash
sudo lvresize --size +10G --resizefs /dev/vg_data/lv_data

Snapshots, Thin Provisioning, and Thin Pools

Snapshots:

  • A snapshot is a point-in-time copy of an LV. Traditional LVM snapshots use copy-on-write (COW).
  • Example:
    sudo lvcreate -s -n lv_data_snap -L 5G /dev/vg_data/lv_data
    

Thin Provisioning:

Thin provisioning allows for overcommitting storage:

sudo lvcreate -L 100G -T -n thinpool vg_data
sudo lvcreate -V 500G -T -n thinvol vg_data/thinpool
sudo mkfs.ext4 /dev/vg_data/thinvol

Disk Replacement & Data Migration

LVM simplifies replacing disks and rebalancing storage:

  1. Add a new disk:
    sudo pvcreate /dev/sdc
    sudo vgextend vg_data /dev/sdc
    
  2. Migrate data using pvmove:
    sudo pvmove /dev/sdb
    
  3. Remove a PV from the VG:
    sudo vgreduce vg_data /dev/sdb
    sudo pvremove /dev/sdb
    

Best Practices & Common Pitfalls

Best Practices:

  • Back up before making changes and prefer UUIDs in /etc/fstab.
  • Regularly monitor space and document changes.

Common Pitfalls:

  • Shrinking without resizing the filesystem first can lead to data loss.

Troubleshooting & FAQ

Common Issues:

  • Device or resource busy when unmounting? Use:
    sudo lsof +D /mnt/data
    
  • Not enough free space to extend? Consider adding PV or freeing up space.
  • Need to restore metadata? Use:
    sudo vgcfgrestore -f /etc/lvm/backup/vg_data vg_data
    

When to Seek Help:

  • If LV or VG metadata is corrupted without a backup, professional support may be necessary.

Conclusion & Next Steps

LVM provides a flexible approach to managing disk storage in Linux. Key takeaways include the ability to resize, create snapshots, and manage volumes dynamically. For practical experience, try setting up a virtual machine to perform hands-on tasks such as creating and resizing volume groups and logical volumes. Explore further documentation on LVM and related technologies to enhance your skills.

References

TBO Editorial

About the Author

TBO Editorial writes about the latest updates about products and services related to Technology, Business, Finance & Lifestyle. Do get in touch if you want to share any useful article with our community.