Knowledge
LVM on Ubuntu 24.04: creating logical volumes and growing a disk online
- LVM adds a layer between disks and file systems: PVs (physical disks) are pooled into a VG (group), then cut into LVs (logical volumes).
lvextend -ris enough to grow ext4: the volume and the file system grow together while mounted.- Shrinking a volume is far more dangerous than growing it; take a backup before any change.
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:
- PV (Physical Volume): a disk or partition registered for LVM to use.
- VG (Volume Group): a group of one or more PVs, the capacity pool.
- LV (Logical Volume): a slice cut from the VG; you create a file system on it and mount it like a normal disk. Its path has the form
/dev/<VG name>/<LV name>.
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:
sudorights and thelvm2package (sudo apt install lvm2if missing).- An empty disk
/dev/sdbto practise on; later add a disk/dev/sdcto grow it. Device names on your machine may differ.
Warning:
pvcreateoverwrites the start of the device and loses the old data on it. Use it only on an empty disk whose name you have confirmed withlsblk.
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
Insufficient free space. The VG does not have enough room; checkvgs, or add a disk as in step 4.- Forgetting
-r. The LV is larger but the file system is still small; handle it as in the Verification section. - Shrinking an LV.
lvreducewith ext4 requires unmounting and shrinking the file system first, then the LV; the wrong order cuts off data. XFS cannot be shrunk. If you do not really need to, do not do it. - Guessing the disk name for
pvcreate. The command may refuse if it recognises a file system signature, but do not rely on that; always checklsblk -ffirst. - Duplicate VG names. Two disks moved from other machines may share a VG name; rename with
vgrenamebefore using them together.
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.