How to Configure and Use LVM on Ubuntu Server

Logical Volume Manager (LVM) simplifies disk management on Ubuntu Server by enabling dynamic storage allocation. This guide walks you through configuring LVM, even if you’re new to Linux administration.


What is LVM?

LVM abstracts physical storage into logical units, allowing you to:

  • Combine multiple disks into a single storage pool.
  • Resize volumes without downtime.
  • Create snapshots for backups.
  • Manage storage flexibly across physical and cloud environments.

Step 1: Install LVM Tools

Ensure LVM utilities are installed:

sudo apt update && sudo apt install lvm2 -y

Step 2: Prepare Physical Volumes

  • Identify disks using lsblk:
lsblk
  • Example output:
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT  
sda      8:0    0  20G  0 disk  
sdb      8:16   0  10G  0 disk  

  • Initialize disks as LVM physical volumes (replace sdb with your disk):
sudo pvcreate /dev/sdb

  • Verify with:
sudo pvs

    Step 3: Create a Volume Group

    Pool physical volumes into a volume group (vg01 in this example):

    sudo vgcreate vg01 /dev/sdb
    
    

    Check the group:

    sudo vgs
    

    Step 4: Create Logical Volumes

    • Allocate space from the volume group. To create a 5GB volume named data_vol:
    sudo lvcreate -L 5G -n data_vol vg01
    
    • Verify the logical volume:
    sudo lvs
    

      Step 5: Format and Mount the Volume

      • Create a filesystem (e.g., ext4):
      sudo mkfs.ext4 /dev/vg01/data_vol
      
      • Mount the volume:
      sudo mkdir /mnt/data 
      sudo mount /dev/vg01/data_vol /mnt/data
      
      • Add to /etc/fstab for automatic mounting:
      echo '/dev/vg01/data_vol /mnt/data ext4 defaults 0 0' | sudo tee -a /etc/fstab
      
      • Note: In cloud environment, it’s suggested to use uuid to mount the disk, as the device name might get dynamically changed after stop and start.

      Step 6: Extend a Logical Volume

      • Expand the volume group (if needed):
      sudo vgextend vg01 /dev/sdc  # Add a new disk
      
      
      • Resize the logical volume (e.g., add 2GB):
      sudo lvextend -L +2G /dev/vg01/data_vol
      
      • Resize the filesystem (for ext4):
      sudo resize2fs /dev/vg01/data_vol
      

        Key Commands Cheat Sheet

        TaskCommand
        Create PVpvcreate /dev/sdX
        Create VGvgcreate vg_name /dev/sdX
        Create LVlvcreate -L SIZE -n lv_name vg_name
        Extend LVlvextend -L +SIZE /dev/vg_name/lv_name
        Check Storagepvsvgslvs

        Best Practices

        • Always back up data before modifying volumes.
        • Use xfs_growfs for XFS filesystems instead of resize2fs.
        • Monitor free space with vgs and lvs.

        References


        By following this guide, you’ve learned to configure LVM on Ubuntu Server for scalable storage. Whether managing on-premises hardware or cloud instances, LVM provides the flexibility to adapt to changing storage needs.

        What’s Next?

        Ready to expand your Ubuntu Server expertise? In our upcoming guide, we’ll dive into containerization and virtualization, where you’ll learn:

        Stay tuned to streamline modern infrastructure management!

        Leave a Comment