HomeLinuxHow to Extend a Logical Volume in Linux

How to Extend a Logical Volume in Linux

Haven’t you ever faced the frustrating issue of low disk space on your VPS??? Running out of space, especially on critical partitions like the root (/), can lead to unexpected downtime and performance issues. Are you feeling tense about how to increase the space of a specific partition without disrupting your server’s operations?

Worry not! In this guide, we’ll walk you through the steps to extend a logical volume using LVM, helping you efficiently allocate more space to a partition, such as the root, and resolve disk space problems quickly. By the end of this tutorial, you’ll know exactly how to extend a logical volume without causing any interruption to your system.

Prerequisites

  • You need administrative (root) access to the server.
  • Ensure you have unpartitioned space or an additional disk to extend the logical volume.
  • It is better to take a full backup of your system before proceeding with disk modifications to prevent if any data loss.

Handling Low Disk Space on VPS

If you are running low on disk space, you may need to purchase additional storage from your VPS provider. In most cases, this additional space will be added as an unpartitioned disk. You can then partition this space according to your requirements. If the low disk space issue concerns an already partitioned volume, you can extend that particular partition to meet your storage needs by following the steps below.

Extend a Logical Volume

It is not difficult to extend a logical volume, but before that, we need to familiarize ourselves with some key terminologies involved in LVM (Logical Volume Management).

Here, you can see some physical volume on the left side of the diagram. This are the partition on your physical storage that is available for LVM. The volume group (VG) in the middle portion is the collection/group of physical volumes. The LV on the right side are Logical Volume. This LV is a “piece” of volume group that you can use as a partition for your operating system. 

There are 5 different step to extend a logical volume.

  1. Create a new partition.
  2. Add the partition you just created as a Physical Volume.
  3. Add this Physical Volume to Volume Group
  4. Extend the Logical Volume. ie: Assign space to Logical Volume from Volume Group.
  5. Finally, resize the extended Logical Volume.

Steps to Extend a Logical Volume

1. Check Disk Usage and Identify the Full Partition

Start by checking the disk usage and identifying which partition is full. In our case, the root partition (/) was 100% utilized:

df -h

This will display the disk space usage. Look for the partition with 100% usage. In our case, the root partition (/) was 100% utilized:

2. Check Existing Logical Volumes

To get a summary of the existing logical volumes (LV) and their sizes, use:

lsblk

This command will list all block devices. Here, /dev/sda is the primary disk, and /dev/sdb is the unpartitioned secondary disk we wanted to use.

3. Partition the New Disk

If the disk you want to use for extending the logical volume is unpartitioned, you need to create a partition first. Using the fdisk utility, you can create a partition:

  • Select n to create a new partition.
  • Choose p for a primary partition.
  • Press Enter to accept default values for the first sector.
  • For the last sector, enter the size, for example, +500G for 500 GB.
  • Press w to write the partition table and exit.

4. Create a Physical Volume

Once the partition is created (/dev/sdb1), you need to create a physical volume on this partition:

pvcreate /dev/sdb1

This initializes the partition as a physical volume (PV) that can be used by LVM. To display the created Physical volume, run

pvs

5. Extend the Volume Group

Next, extend the volume group (VG) that contains the logical volume you want to expand. The volume group was named as cl. To extend it using the new partition /dev/sdb1, run:

vgextend cl /dev/sdb1

This adds the new physical volume to the existing volume group.

6. Extend the Logical Volume

Now that the volume group has more space, extend the logical volume

lvextend -L+500G /dev/mapper/cl-root

This will add 500 GB to the logical volume.

7. Resize the Filesystem

For the changes to take effect, you need to resize the filesystem. The method depends on the filesystem type. In our case, the filesystem was XFS, so we used:

xfs_growfs /

For other filesystems like ext4, you would use:

resize2fs /dev/mapper/cl-root

8. Verify the Extension

To confirm that the logical volume and filesystem have been extended, run the following command again to check the updated disk usage:

df -h

Root partition now has more available space as above.

Conclusion

Extending a logical volume is a critical task for maintaining optimal performance and ensuring your server has sufficient disk space for ongoing operations. By following the steps outlined in this article, you can seamlessly add more space to your logical volume, preventing issues like partition overflow.

If you continue to experience disk space issues or need further assistance, always consider cleaning up old files and monitoring your server’s disk usage regularly. Extending the logical volume is a one-time solution, but routine maintenance is essential for smooth server operations.

How to Partition C Drive in Windows Without Formatting?
How to Merge Partitions in Windows Without Data Loss?
Scroll to Top