Online expansion of Ubuntu LVM disk
LVM overview

Physical Volume (PV)
Refers to a disk partition, or a device (such as RAID) that has the same function as a disk partition. It is the basic storage logical block of LVM, but compared with basic physical storage media (such as partitions, disks, etc.), it contains LVM-related Management parameters.
Volume Group (VG)
Similar to a physical disk in a non-LVM system, it is composed of one or more physical volumes PV. One or more LVs (logical volumes) can be created on the volume group.
Logical Volume (LV)
Similar to disk partitions in non-LVM systems, logical volumes are built on the volume group VG. A file system (such as /home or /usr, etc.) can be established on the logical volume LV.
LVM expansion operation
Create a new primary partition
If you expanded the hard disk size of the virtual machine, and you are still using one hard disk such as /dev/sda, you can do this:
1 | sudo fdisk /dev/sda |
Enter m to view the help, enter n to create a new partition, select the primary partition, step by step, press w to write and save, and get /dev/sda2.
You can also add a new hard drive to the machine, and the new hard drive may be shown as /dev/sdb. The operation of creating a new partition is similar.
Format the partition as ext4 format
1 | sudo mkfs -t ext4 /dev/sda2 |
/dev/sda2 represents the new partition, you can replace it with other paths according to your situation, for example /dev/sdb1.
Find the VG Name
1 | ~# vgdisplay |
Add the new partition to the original VG of lvm
1 | vgextend ubuntu--vg-root /dev/sda2 |
Check the VG
You should find that there are some free space in the volume group.
1 | ~# vgdisplay |
Check the LV
1 | ~# lvdisplay |
Extend the LV
1 | ~# lvextend -L 700G /dev/ubuntu--vg-root/lv-root |
Find the root file system name
1 | ~# df -h |
Resize file system
1 | ~# resize2fs -p /dev/mapper/ubuntu--vg-root |
View the latest hard drive size
1 | ~# df -h |
How to reduce the LV size
You can use the lvreduce command.