Ubuntu: extending the partition after a clean install (LVM, fdisk)

After a clean install of Ubuntu Server with the default layout (LVM), the root file system takes up only part of the disk, roughly half. The rest of the space hasn't gone anywhere; it just isn't assigned to anything. A few commands fix this, with no reboot and no data loss.

What to check first

Before changing anything, it helps to see exactly where the free space is:

lsblk
df -h /
sudo vgs
sudo lvs
  • lsblk shows the disk, its partitions and the logical volumes on them;
  • df -h / shows how much space the root file system sees;
  • vgs shows in the VFree column how much free space is left in the volume group;
  • lvs shows the size of the ubuntu-lv logical volume.

There are two cases, and they determine which steps you need.

  1. The disk hasn't changed, but the volume is smaller than the volume group. The installer didn't give ubuntu-lv all the available space, and VFree shows free gigabytes. You only need steps 3 and 4.
  2. The disk was enlarged after the install (for example, in a hypervisor or in the cloud), and the volume group has no free space, but the disk itself does. You need all four steps.

If in doubt, run all four: the extra commands won't break anything, and where there's nothing to do they'll simply say so.

Four commands

sudo sgdisk -e /dev/sda
sudo growpart /dev/sda 3
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

The disk name (/dev/sda), the partition number (3) and the volume name (ubuntu--vg-ubuntu--lv) come from a standard install on a virtual machine. Yours may differ (/dev/vda, /dev/nvme0n1 and so on), so check the output of lsblk. On NVMe the partition number is written with the letter p, while growpart takes the disk and the partition number as separate arguments: sudo growpart /dev/nvme0n1 3.

1. sgdisk -e: move the backup partition table to the end of the disk

The GPT partition table is duplicated at the very end of the disk. When the disk grows, the copy stays where the old end used to be, and the system thinks there's nothing beyond it. The -e switch moves the backup copy of the table to the real end of the disk. The sgdisk command comes with the gdisk package.

On a regular disk that hasn't been enlarged, this step changes nothing.

2. growpart: grow the partition

sudo growpart /dev/sda 3

The LVM partition (the third one here) is extended to the end of the disk. The partition is resized on the fly, and the data is left untouched. If the partition already takes up all the space, growpart answers NOCHANGE, which is fine. The utility is in the cloud-guest-utils package.

After that, the physical volume needs to be told about its new size:

sudo pvresize /dev/sda3

In many cases growpart on Ubuntu doesn't require this, but the step is harmless, and afterwards sudo vgs will show all the available space in VFree.

3. lvextend: give all the free space to the logical volume

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

-l +100%FREE means: add everything that's free in the volume group to the volume. If you only need part of it, say 20 GB, write -L +20G instead.

4. resize2fs: grow the file system

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

The logical volume has grown, but the file system hasn't yet. resize2fs grows ext4 right on the mounted volume. If the root is formatted as XFS, you need a different command:

sudo xfs_growfs /

You can find out the file system type with df -T /.

Shorter: lvextend with the -r switch

Steps 3 and 4 combine into a single -r (--resizefs) switch: lvextend will call the right utility for the file system itself.

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

Checking the result

df -h /

The Size column should show the new size. There's no need to reboot the machine.

Summary

  • no free space in the volume group, but the disk was enlarged: sgdisk -e, growpart, and pvresize if needed;
  • the volume group has free space: lvextend -r -l +100%FREE;
  • always start with lsblk, vgs and lvs to make sure the commands apply to the right disk and the right volume.

Only registered users can leave a comment.

Sign in or register to leave a comment.