How to resize Ubuntu server LVM disk to all available disk space
I need to resize a virtual machine disks multiple times a year. Most of the time, I'm lazy enough to just jump into the Cockpit web interface and change things there. But in Ubuntu, I need to use command line and I always forget how to resize a Logical Volume Manager (LVM) disk in an Ubuntu server to utilize all available disk space. So, this is my personal notes to remember how to do that next time.
Requirements
- I'm assuming your server with a LVM disk is a virtual machine
- Also, you must already have resized your disk in your hypervisor
Does it work in other Linux distros? Sure, but the tooling may be different. If you are questioning yourself about it, you probably already know how to solve the problem.
Btw, for Fedora and CentOS Stream users, Cockpit is my go-to way to manage disks.
Check if your disk have space
First, run lsblk
to make sure your disk was properly resize by your hypervisor:
# Check current disk space
# in this case, I resized my disk from 32GB to 100GB
lsblk
>> NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
>> [...]
>> sda 8:0 0 100G 0 disk
>> ├─sda1 8:1 0 1G 0 part /boot/efi
>> ├─sda2 8:2 0 2G 0 part /boot
>> └─sda3 8:3 0 28.9G 0 part
>> └─ubuntu--vg-ubuntu--lv
>> 253:0 0 28.9G 0 lvm /
check current disk size
As showed above, I have a total of 100G
with 28.9G
of usage in the main partition. The goal is to fill all the current free space.
Now, check if your physical volume (PV), volume group (VG), and logical volume (LV) are good:
# Check PV, VG, and LV
pvs
>> PV VG Fmt Attr PSize PFree
>> /dev/sda3 ubuntu-vg lvm2 a-- <28.95g 0
vgs
>> VG #PV #LV #SN Attr VSize VFree
>> ubuntu-vg 1 1 0 wz--n- <28.95g 0
lvs
>> LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
>> ubuntu-lv ubuntu-vg -wi-ao---- <28.95g
physical volume (PV), volume group (VG), and logical volume (LV)
If you got similar results, then we are good to go. You have a proper LVM volume and enough space to resize it.
Resizing the physical volume (PV) and the logical volume (LV)
So, the logic for this process is:
- You resized the "physical disk" in your hypervisor. So, it is like your HDD/SSD is bigger now.
- This step will extend the LVM physical volume (PV) to fill all this extra space in your HDD/SSD.
- Then, we need to resize the logical volume (LV), which lives inside the PV, to fill the new extra space.
# Check the current physical volume (PV) status
pvdisplay
>> --- Physical volume ---
>> PV Name /dev/sda3
>> VG Name ubuntu-vg
>> PV Size <28.95 GiB / not usable 2.00 MiB
>> Allocatable yes (but full)
>> PE Size 4.00 MiB
>> Total PE 7410
>> Free PE 0
>> Allocated PE 7410
>> PV UUID 0JoRas-BP0E-euUg-ytug-nevQ-iBTE-DkFQJd
# Extend the physical volume
growpart /dev/sda 3
>> CHANGED: partition=3 start=6397952 old: size=60708864 end=67106816 new: size=203317215 end=209715167
# Check again, now with the new physical volume (PV) status
pvdisplay
>> --- Physical volume ---
>> PV Name /dev/sda3
>> VG Name ubuntu-vg
>> PV Size <96.95 GiB / not usable 2.98 MiB
>> Allocatable yes
>> PE Size 4.00 MiB
>> Total PE 24818
>> Free PE 17408
>> Allocated PE 7410
>> PV UUID 0JoRas-BP0E-euUg-ytug-nevQ-iBTE-DkFQJd
resize the physical volume (PV)
With the phisical volume (PV) resized, now let's resize the logical volume (LV) and the filesystem itself:
# Check the current logical volume (LV) status
lvdisplay
>> --- Logical volume ---
>> LV Path /dev/ubuntu-vg/ubuntu-lv
>> LV Name ubuntu-lv
>> VG Name ubuntu-vg
>> LV UUID blVbfk-h7hx-dy2M-DXmT-ZmMc-ocF8-08CX4i
>> LV Write Access read/write
>> LV Creation host, time ubuntu-server, 2024-01-22 22:03:47 +0000
>> LV Status available
>> # open 1
>> LV Size <28.95 GiB
>> Current LE 7410
>> Segments 1
>> Allocation inherit
>> Read ahead sectors auto
>> - currently set to 256
>> Block device 253:0
# Extend the logical volume (LV)
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
>> Size of logical volume ubuntu-vg/ubuntu-lv changed from <28.95 GiB (7410 extents) to <96.95 GiB (24818 extents).
>> Logical volume ubuntu-vg/ubuntu-lv successfully resized.
# Check again, now with the new logical volume (LV) status
lvdisplay
>> --- Logical volume ---
>> LV Path /dev/ubuntu-vg/ubuntu-lv
>> LV Name ubuntu-lv
>> VG Name ubuntu-vg
>> LV UUID blVbfk-h7hx-dy2M-DXmT-ZmMc-ocF8-08CX4i
>> LV Write Access read/write
>> LV Creation host, time ubuntu-server, 2024-01-22 22:03:47 +0000
>> LV Status available
>> # open 1
>> LV Size <96.95 GiB
>> Current LE 24818
>> Segments 1
>> Allocation inherit
>> Read ahead sectors auto
>> - currently set to 256
>> Block device 253:0
resize the logical volume (LV)
# Check the current may filesystem
# (I'm assuming / is the main mount point for you)
df -h /
>> Filesystem Size Used Avail Use% Mounted on
>> /dev/mapper/ubuntu--vg-ubuntu--lv 29G 24G 3.3G 89% /
# Resize the filesystem to its maximum avaible size
resize2fs /dev/ubuntu-vg/ubuntu-lv
>> resize2fs 1.46.5 (30-Dec-2021)
>> Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
>> old_desc_blocks = 4, new_desc_blocks = 13
>> The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 25413632 (4k) blocks long.
# Check again, now with the new filesystem disk size
df -h /
>> Filesystem Size Used Avail Use% Mounted on
>> /dev/mapper/ubuntu--vg-ubuntu--lv 96G 24G 68G 27% /
resize the filesystem
Done! 😄 everything was properly resized. Take a new snapshot of your VM and be happy.