- Kubernetes DaemonSets - Wed, Sep 6 2023
- Static Pods in Kubernetes - Fri, Sep 1 2023
- Encrypt Kubernetes Secrets at rest - Mon, Aug 28 2023
Requirements
To follow along with this guide, you need a physical or virtual machine running Ubuntu Linux with an LVM partition scheme. I am running Ubuntu 22.04 LTS in a VM, but the steps will be pretty much the same for other versions. Note that this guide does not cover the basics of LVM. Check out this article to learn the LVM basics.
Extend LVM when there is free space
Open a terminal or SSH session on your Ubuntu system, and follow these steps:
- To check the disk usage summary, use the following command:
sudo df -Th
You can see in the screenshot that my root file system is running out of disk space (90% space has already been consumed).
- Use the sudo vgs or vgdisplay commands to verify whether there is any free space available in the volume group.
sudo vgdisplay
You can see there is 10 GB of free space in my case. If you do not see free space in your case, read on in the next section.
- If you see free space, use the lvdisplay command to view the LV path. Then run the lvextend command, as shown below:
sudo lvdisplay sudo lvextend -l +100%FREE -r /dev/ubuntu-vg/ubuntu-lv
The lvextend command with the -l (lowercase L) option specifies the size in extents. If you use -L (uppercase L), you need to specify the size (+10 GB to extend by 10 GB, for example).
- The +100%FREE option indicates that all remaining free space in the volume group should be added.
- The -r option resizes the underlying file system together with the logical volume. If you skip this option, you need to use the sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv command separately to extend the actual file system.
Extend LVM when there is no free space
If you do not have any free space in the volume group, you need to follow some additional steps. Your options depend on the type of system you have. If you have a physical machine, you can add an additional physical disk to your system. If you're working on a VM, like me, you could simply extend your existing virtual disk. The procedure may vary depending on the type of hypervisor you're using.
- To check whether new free space is recognized by the system, use the sudo cfdisk command. If you did online resizing and free space is not recognized, run the following command as the root user to force rescan:
echo 1 > /sys/class/block/sda/device/rescan
As you can see in the screenshot, 30 GB of free space has been detected by the system. You can now easily extend the last partition (/dev/sda3) by adding 30 GB of free space. Just highlight the last partition, select the Resize option as shown in the screenshot, and press Enter.
- Press Enter one more time to confirm the new size. Notice that the size of the /dev/sda3 partition is now extended.
- To write the changes to disk, select the Write option, and press Enter. Type yes to confirm and press Enter one more time.
- You can now quit the cfdisk utility.
- Since we have extended the physical partition, we need to resize the physical volume using the pvresize command, as shown below:
sudo pvresize /dev/sda3 sudo pvdisplay
- The volume group should now show free space. To check, use the sudo vgdisplay command:
sudo vgdisplay
- We can now extend the LVM with the lvextend command, as we did previously, to add 30 GB space.
sudo lvdisplay sudo lvextend -l +100%FREE -r /dev/ubuntu-vg/ubuntu-lv
You can see that LVM is now extended to 39.11 GB. We can confirm the updated disk usage summary with the df -Th command.
Conclusion
That's it! You successfully extended the LVM space in your Ubuntu Linux system, and the root file system now has 29 GB of free space available. This is the beauty of the LVM.
Subscribe to 4sysops newsletter!
When you start running out of disk space, you can add more disks and continue to extend the LVM space online.
Does -r option works on BTRFS too?