Did you ever install Ubuntu Linux with the default LVM (logical volume manager) partition layout and give it a really small disk size to begin with? This usually happens while setting up an EC2 instance in AWS, since the root volume gets 8 GB EBS storage by default. After using your instance for some time, you realize that there is no free space left on the disk. In this post, I will cover how to extend the LVM space in Ubuntu Linux.
Latest posts by Surender Kumar (see all)

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:

  1. To check the disk usage summary, use the following command:
    sudo df -Th
    Viewing disk usage summary in Linux

    Viewing disk usage summary in Linux

    You can see in the screenshot that my root file system is running out of disk space (90% space has already been consumed).

  2. Use the sudo vgs or vgdisplay commands to verify whether there is any free space available in the volume group.
    sudo vgdisplay
    Viewing volume group information with vgdisplay command

    Viewing volume group information with vgdisplay command

    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.

  3. 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
    Extending LVM with 100 free space and simultaneously resizing the underlying file system

    Extending LVM with 100 free space and simultaneously resizing the underlying file system

    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.

  1. 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
    Extending the physical partition by adding free space with the cfdisk utility

    Extending the physical partition by adding free space with the cfdisk utility

    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.

  2. Press Enter one more time to confirm the new size. Notice that the size of the /dev/sda3 partition is now extended.
  3. To write the changes to disk, select the Write option, and press Enter. Type yes to confirm and press Enter one more time.

    Writing changes to disk with the cfdisk utility

    Writing changes to disk with the cfdisk utility

  4. You can now quit the cfdisk utility.
  5. 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

    Resizing the physical volume with the pvresize command

    Resizing the physical volume with the pvresize command

  6. The volume group should now show free space. To check, use the sudo vgdisplay command:
    sudo vgdisplay

    Checking the free space in the volume group with the vgdisplay command

    Checking the free space in the volume group with the vgdisplay command

  7. 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
    
    Extending the LVM with 100 free space and resizing the underlying file system

    Extending the LVM with 100 free space and resizing the underlying file system

    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.

    Viewing the updated disk usage summary

    Viewing the updated disk usage summary

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.

avataravatar
1 Comment
  1. mehdi 8 months ago

    Does -r option works on BTRFS too?

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account