How to resize the root LVM partition of Ubuntu

When we resize the virtual hard disk of a virtual machine or restore a disk image to a larger disk, the free space of the partition detected by Ubuntu will not increase because the partition table is unchanged. In the past, we could easily resize the ext4 root partition with the help of resize2fs. However, things get complex when Ubuntu utilizes LVM partition as their default root partition.

Quick Intro to LVM

Logical Volume Manager (LVM) is similar to Dynamic Disks under Windows, which can take several GPT / MBR partitions on different hard disks as a storage pool (LVM call it Volume Groups, VG), and allocate spaces from this pool, then Linux will recognize each space (LVM call it Logical Volume, LV) as an useable partition.

Lvm Layout
Lvm Layout

Thus, we should modify not only the GPT / MBR partition table, but also the LVM configuration.

Update GPT / MBR partition table

I suggest all the operations should be done under live CD environment to avoid the occurrence of unpredictable problems. I didn't test online resizing on the root partition so far.

  1. The following instructions in this section assume the last partition on your disk is the LVM Physical Volume. You could verify this with the command lsblk --fs. nvme0n1p3 is the last GPT partition on the disk nvme0n1, and it is easy to identify this partition is a LVM PV, and ubuntu--vg-ubuntu--lv is the corresponding LV.
1
2
3
4
5
6
7
8
9
10
tonny@vm:~$ lsblk --fs
NAME FSTYPE FSVER
loop3
└─loop3p1 LVM2_member LVM2 001
└─test--vg-test--lv ext4 1.0
nvme0n1
├─nvme0n1p1 vfat FAT32
├─nvme0n1p2 ext4 1.0
└─nvme0n1p3 LVM2_member LVM2 001
└─ubuntu--vg-ubuntu--lv ext4 1.0

Note that ubuntu--vg-ubuntu--lv is the root partition of the system here.

1
2
3
4
5
6
tonny@vm:~$ df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv ext4 78G 7.7G 67G 11% /
/dev/nvme0n1p2 ext4 974M 87M 820M 10% /boot
/dev/nvme0n1p1 vfat 511M 3.6M 508M 1% /boot/efi
/dev/mapper/test--vg-test--lv ext4 464M 24K 429M 1% /home/tonny/mnt

Also you could check the LVM Volume Group status by vgs.

1
2
3
4
tonny@vm:~$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
test-vg 1 1 0 wz--n- 496.00m 0
ubuntu-vg 1 1 0 wz--n- <78.50g 0
  1. Update the GPT / MBR partition table using fdisk. I will use an emulated disk /dev/loop3 to demonstrate the whole process. Don't worry, you won't loss your data under normal circumstances. These commands will only modify the partition table, but make sure DO NOT remove the LVM's signature, otherwise the system may no longer recognize your LVM PV.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
tonny@vm:~$ sudo fdisk /dev/loop3 # replace with your hard disk, such as /dev/nvme0n1p3

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/loop3: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C5F55056-8C56-5448-81E4-567F59AD93ED

Device Start End Sectors Size Type
/dev/loop3p1 2048 1026047 1024000 500M Linux filesystem

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-2097118, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097118, default 2097118):

Created a new partition 1 of type 'Linux filesystem' and of size 1023 MiB.
Partition #1 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p

Disk /dev/loop3: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C5F55056-8C56-5448-81E4-567F59AD93ED

Device Start End Sectors Size Type
/dev/loop3p1 2048 2097118 2095071 1023M Linux filesystem

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Update LVM Configuration

  1. Notify LVM there is an update on the partition table.
1
2
3
4
tonny@vm:~$ sudo partprobe # ask kernel to read the new partition table
tonny@vm:~$ sudo pvresize /dev/loop3p1 # replace with your partition
Physical volume "/dev/loop3p1" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

At this moment, the LVM Volume Group status has changed to,

1
2
3
4
tonny@vm:~$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
test-vg 1 1 0 wz--n- 1020.00m 524.00m
ubuntu-vg 1 1 0 wz--n- <78.50g 0

Observe that VFree of test-vg has increased by 524.00 MB.

  1. Resize LVM Logical Volume. The following command will allocate all the free space of VG to the LV.
1
2
3
tonny@vm:~$ sudo lvextend -l +100%FREE /dev/mapper/test--vg-test--lv
Size of logical volume test-vg/test-lv changed from 496.00 MiB (124 extents) to 1020.00 MiB (255 extents).
Logical volume test-vg/test-lv successfully resized.

The free space of VG is used up now.

1
2
3
4
tonny@vm:~$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
test-vg 1 1 0 wz--n- 1020.00m 0
ubuntu-vg 1 1 0 wz--n- <78.50g 0

Resize ext4 File System

Up to now, although LVM LV is resized, the ext4 file system is not aware of the extra available space. Simply run resize2fs to let it know.

1
2
3
4
5
tonny@vm:~$ sudo resize2fs /dev/mapper/test--vg-test--lv
resize2fs 1.46.3 (27-Jul-2021)
Filesystem at /dev/mapper/test--vg-test--lv is mounted on /home/tonny/mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/test--vg-test--lv is now 261120 (4k) blocks long.

We can see the available space of test--vg-test--lv has been enlarged.

1
2
3
tonny@vm:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/test--vg-test--lv 973M 1.3M 917M 1% /home/tonny/mnt

References