How to Resize LVM Root Partition on Ubuntu Server When / Is 99% Full


2 views

When working with Ubuntu Server on USB drives, particularly with LVM configurations, we often encounter situations where the root partition fills up unexpectedly. Your system shows several important indicators:

# Current disk layout
sudo fdisk -l /dev/sdm
# Shows 13.6G available in /dev/sdm3 but only 3.9G used for root

# LVM volume groups
sudo vgs
# Shows 9.58G free space in ubuntu-vg

# Logical volumes
sudo lvs
# Shows ubuntu-lv is only 4.00G

The Ubuntu installer creates a small logical volume by default when using LVM, even when plenty of disk space is available. This is a safety measure but often needs adjustment for production systems.

1. Check Current Filesystem

sudo df -hT

2. Extend the Logical Volume

First, extend the logical volume to use all available space in the volume group:

sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv

Alternatively, to extend by a specific amount (e.g., 5G):

sudo lvextend -L+5G /dev/mapper/ubuntu--vg-ubuntu--lv

3. Resize the Filesystem

For ext4 filesystems (most common with Ubuntu):

sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

For xfs filesystems:

sudo xfs_growfs /

4. Verify the Changes

sudo df -h
sudo vgs
sudo lvs

To prevent this in future installations, you can customize the LVM layout during Ubuntu Server installation:

# Example preseed.cfg snippet for automated installs
d-i partman-auto/method string lvm
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-auto-lvm/guided_size string max

For systems where you might need to shrink volumes later (not recommended for root without careful planning):

# First reduce filesystem (example for ext4)
sudo umount /mnt
sudo e2fsck -f /dev/mapper/ubuntu--vg-ubuntu--lv
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv 8G

# Then reduce LV
sudo lvreduce -L 8G /dev/mapper/ubuntu--vg-ubuntu--lv

Set up monitoring to alert before filesystem fills up:

# Simple cron job example
*/10 * * * * df -h | awk '$6 == "/" && $5 > "90%" {print "Root filesystem is " $5 " full!"; exit 1}' | mail -s "Disk Space Alert" admin@example.com

When examining an Ubuntu Server 18.04 LTS installation running from a 16GB USB drive with LVM, we observe an interesting storage allocation scenario through several diagnostic commands:

# Checking disk partitions
sudo fdisk -l /dev/sdm

# Viewing mounted filesystems
sudo df -h

# Examining volume groups
sudo vgs

# Listing logical volumes
sudo lvs

The key findings reveal:

  • The physical disk has 13.6GB allocated to LVM (sdm3)
  • Volume group (ubuntu-vg) shows 9.58GB available space
  • Logical volume (ubuntu-lv) is only 4GB despite available space

The root cause becomes clear when analyzing the LVM structure. Here's how to properly extend the root partition:

# First, extend the logical volume to use all available space
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv

# Then resize the filesystem (for ext4)
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

# Verify the new size
df -h /

For XFS filesystems (common in newer Ubuntu versions), the process differs slightly:

# Extend the LV first
sudo lvextend -L +9G /dev/mapper/ubuntu--vg-ubuntu--lv

# Then grow the XFS filesystem
sudo xfs_growfs /

To avoid similar situations in future installations:

  1. During Ubuntu Server installation:
    # When using manual partitioning:
    - Allocate minimal space to /boot (1GB sufficient)
    - Assign remaining space to LVM physical volume
    - Create volume group using entire PV
    - Allocate 100% of VG space to root LV initially
  2. For existing systems, consider adding monitoring:
    # Simple cron job to check disk space
    */30 * * * * df -h / | awk '{print $5}' | grep -v Use | cut -d'%' -f1 | \
    while read usage; do [ $usage -gt 90 ] && \
    echo "Warning: Root FS at ${usage}%" | mail -s "Disk Alert" admin@example.com; done

Common problems and solutions:

Issue Solution
resize2fs fails with "filesystem is mounted" Boot from live USB and perform resize offline
lvextend reports "insufficient free space" Check vgs output and ensure VFree matches expectations
Filesystem doesn't show new space after resize Reboot or remount the filesystem to refresh