LVM on Ubuntu 24.04: creating logical volumes and growing a disk online

  1. Introduction
  2. Goal and preparation
  3. Steps
  4. Step 1: create the PV and VG
  5. Step 2: create the LV, the file system and mount it
  6. Step 3: grow it while the VG has free space
  7. Step 4: grow it when the VG is full
  8. Verification
  9. Common errors
  10. Summary
  11. References

Introduction

Ordinary partitions (see the article on lsblk, parted and mkfs.ext4 at the start of this series) have a weakness: when one fills up it is hard to grow, because the partition sits at a fixed place on one disk.

LVM (Logical Volume Manager) solves this by separating "capacity" from "physical disk".

You pool several disks or partitions into one shared capacity pool, then cut that pool into volumes that can be enlarged when needed, often while the service keeps running.

Three terms to remember:

This article uses Ubuntu 24.04 LTS and the ext4 file system.

Goal and preparation

Goal: create a VG from an empty disk, create an LV, mount it, then grow the LV while it is in use.

Preparation:

Warning: pvcreate overwrites the start of the device and loses the old data on it. Use it only on an empty disk whose name you have confirmed with lsblk.

Steps

Steps

Step 1: create the PV and VG

sudo pvcreate /dev/sdb
sudo vgcreate vg_data /dev/sdb

Check with sudo pvs and sudo vgs; the VFree column of vgs is the capacity not yet allocated.

Step 2: create the LV, the file system and mount it

sudo lvcreate -L 10G -n lv_app vg_data
sudo mkfs.ext4 /dev/vg_data/lv_app
sudo mkdir -p /srv/app
sudo mount /dev/vg_data/lv_app /srv/app

-L 10G sets a size of 10 GiB (your disk must be larger than this) and -n names the LV. To use all remaining space, use -l 100%FREE instead. List volumes with sudo lvs.

To make the mount permanent you need a matching line in /etc/fstab; the safe way to do it is in fstab on Ubuntu 24.04: mounting disks safely at boot with UUID and nofail.

Step 3: grow it while the VG has free space

If VFree has room, add 5 GiB and grow the file system at the same time:

sudo lvextend -r -L +5G /dev/vg_data/lv_app

The -r (--resizefs) option calls the appropriate file system resize tool (resize2fs for ext4) right after growing the LV. With ext4 you do not need to unmount when growing.

-L +5G means add 5 GiB; writing -L 15G (no plus sign) sets the final size to 15 GiB.

Step 4: grow it when the VG is full

Add an empty disk /dev/sdc to the VG, then grow the LV:

sudo pvcreate /dev/sdc
sudo vgextend vg_data /dev/sdc
sudo lvextend -r -l +100%FREE /dev/vg_data/lv_app

At this point the LV spans two physical disks. This is LVM's strength but also a risk: losing one of the two disks can break the whole volume. LVM by itself is not a backup and does not replace RAID.

Verification

sudo vgs
sudo lvs
df -h /srv/app

lvs shows the new LV size, and df -h confirms the file system sees the extra space.

If lvs has grown but df has not changed, the file system was not grown: with ext4 run sudo resize2fs /dev/vg_data/lv_app.

With XFS the equivalent is sudo xfs_growfs /srv/app, run on the mount point.

After resizing, if your service uses this directory, check that it still runs normally by looking at its status as described in systemctl on Ubuntu 24.04: enabling, disabling and restarting services and checking their status.

Common errors

Summary

LVM gives you flexible capacity: pvcreate and vgcreate build the pool, lvcreate cuts volumes, and lvextend -r grows both the volume and the file system while running.

Growing is easy and safe; shrinking is risky, so leave spare capacity from the start and always keep a separate backup.

References

LVM on Ubuntu 24.04: creating logical volumes and growing a disk online