devops2025-06-08·9·39/348

Expanding LVM Disk Volumes on Linux

Step-by-step guide to expanding LVM logical volumes, including adding new disks, extending volume groups, and resizing filesystems.

Expanding LVM Disk Volumes on Linux

Running out of disk space on a production server requires careful expansion. LVM (Logical Volume Manager) makes this easier than traditional partitioning, but the process involves multiple steps. I expanded a 100GB volume to 500GB on a trading server without downtime.

Environment

  • OS: Ubuntu 22.04 LTS
  • Current setup: 100GB LVM volume on single disk
  • Goal: Add 400GB new disk and expand volume
$ lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                   8:0    0  100G  0 disk
├─sda1                8:1    0  512M  0 part /boot
└─sda2                8:2    0 99.5G  0 part
  └─vg0-root        253:0    0   98G  0 lvm  /
sdb                   8:16   0  400G  0 disk  # New disk

$ df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   98G   85G   13G  87% /

The Problem

$ df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   98G   85G   13G  87% /

# Only 13GB free - running low on space
$ sudo du -sh /var/lib/docker
45G     /var/lib/docker

# Docker alone uses almost half the volume

Analysis

Step 1: Check Current LVM Configuration

$ sudo pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  vg0  lvm2 a--  <99.50g  <1.50g

$ sudo vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  vg0    1   1   0 wz--n- <99.50g <1.50g

$ sudo lvs
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%
  root vg0  -wi-ao---- <98.00g

Step 2: Prepare New Disk

# Check if new disk is detected
$ lsblk | grep sdb
sdb                   8:16   0  400G  0 disk

# Check disk details
$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 400 GiB, 429496729600 bytes, 838860800 sectors

Solution

Step 1: Create Physical Volume

# Initialize the new disk as LVM physical volume
$ sudo pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

# Verify
$ sudo pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  vg0  lvm2 a--  <99.50g  <1.50g
  /dev/sdb       lvm2 ---  400.00g 400.00g

Step 2: Extend Volume Group

# Add new PV to existing VG
$ sudo vgextend vg0 /dev/sdb
  Volume group "vg0" successfully extended

# Verify
$ sudo vgs
  VG   #PV #LV #SN Attr   VSize    VFree
  vg0    2   1   0 wz--n- <499.50g <498.50g

Step 3: Extend Logical Volume

# Extend LV by 400GB
$ sudo lvextend -L +400G /dev/vg0/root
  Size of logical volume vg0/root changed from <98.00 GiB to <498.00 GiB.
  Logical volume vg0/root successfully resized.

# Or extend to use all free space
$ sudo lvextend -l +100%FREE /dev/vg0/root

# Verify
$ sudo lvs
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%
  root vg0  -wi-ao---- <498.00g

Step 4: Resize Filesystem

# Check current filesystem type
$ sudo blkid /dev/vg0/root
/dev/vg0/root: UUID="xxxx-xxxx" TYPE="ext4"

# For ext4
$ sudo resize2fs /dev/vg0/root
resize2fs 1.46.5 (30-Dec-2021)
The filesystem on /dev/vg0/root is now 130088960 (4k) blocks long.

# For XFS
$ sudo xfs_growfs /

# Verify
$ df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root  498G   85G  413G  17% /

Complete Expansion Script

#!/bin/bash
# expand-lvm.sh

set -e

NEW_DISK="/dev/sdb"
VG_NAME="vg0"
LV_NAME="root"

echo "Step 1: Creating physical volume..."
sudo pvcreate "$NEW_DISK"

echo "Step 2: Extending volume group..."
sudo vgextend "$VG_NAME" "$NEW_DISK"

echo "Step 3: Extending logical volume..."
sudo lvextend -l +100%FREE "/dev/${VG_NAME}/${LV_NAME}"

echo "Step 4: Resizing filesystem..."
FSTYPE=$(sudo blkid -o value -s TYPE "/dev/${VG_NAME}/${LV_NAME}")
case "$FSTYPE" in
    ext4) sudo resize2fs "/dev/${VG_NAME}/${LV_NAME}" ;;
    xfs)  sudo xfs_growfs / ;;
    *)    echo "Unknown filesystem: $FSTYPE"; exit 1 ;;
esac

echo "Done! New size:"
df -h /

Troubleshooting

Cannot Resize Mounted Filesystem

# For root filesystem, resize2fs works online for ext4
$ sudo resize2fs /dev/vg0/root
# This works while mounted for ext4

# For XFS, xfs_growfs works online
$ sudo xfs_growfs /

Not Enough Free Space in VG

$ sudo vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  vg0    1   1   0 wz--n- <99.50g    0 

# Need to add another disk first
$ sudo pvcreate /dev/sdc
$ sudo vgextend vg0 /dev/sdc

PV Already Used by Another VG

$ sudo pvcreate /dev/sdb
  Can't open /dev/sdb exclusively.  Physical volume /dev/sdb is already in use.

# Check what VG it belongs to
$ sudo pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sdb   vg1  lvm2 a--  400.00g 400.00g

# Remove from old VG first (DANGER - only if PV is empty)
$ sudo vgreduce vg1 /dev/sdb
$ sudo pvremove /dev/sdb

Add Disk to Existing Partition

If adding disk to existing partition:

# 1. Add partition to disk
$ sudo fdisk /dev/sdb
# n (new partition)
# p (primary)
# 1 (partition number)
# (accept defaults)
# t (change type)
# 8e (Linux LVM)
# w (write)

# 2. Create PV
$ sudo pvcreate /dev/sdb1

# 3. Extend VG
$ sudo vgextend vg0 /dev/sdb1

# 4. Extend LV
$ sudo lvextend -l +100%FREE /dev/vg0/root

# 5. Resize filesystem
$ sudo resize2fs /dev/vg0/root

Lessons Learned

  1. LVM makes expansion non-destructive - no need to recreate partitions.
  2. Always backup before LVM operations - mistakes can be catastrophic.
  3. pvcreate, vgextend, lvextend, resize2fs - the four commands you need.
  4. Check filesystem type before resizing - ext4 and XFS use different commands.
  5. Monitor VG free space - set alerts before running out.

This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.