Resize Or Remove Logical Volumes With LVM

When working with logical volumes in Linux for file systems, one of the big advantages that it brings is the ease of resizing volumes along with the ability to do so on the fly. In previous articles, we’ve looked at logical volume management detail and covered the terminology involved. We’ve also set up and configured a basic logical volume system for use, so this time we’ll look at how you can go about resizing your logical volumes.

Note that while it is possible to resize a logical volume while it is mounted and in use, it is recommended that you do unmount the filesystem first as file corruption can occur. This is especially true when shrinking a filesystem rather than expanding. Whether you are resizing mounted or unmounted we do recommend you always have a backup of your files before starting.

How To Grow The Size Of A Logical Volume System

Let’s look at one of the first tasks you are likely to want to do with a logical volume system, which is growing the size of a logical volume once it fills up. We’ll be assuming you currently have unused space on the volume group within which the logical volume resides. So for this, we can use the lvresize or lvextend commands:

sudo lvresize --resizefs --size +50GB /dev/volume1/uploads

The lvresize command can automatically resize the filesystem for you by supplying the “–resizefs” flag. By specifying the + in front of the size, we are saying that we would like to increase the size by 50GB. Without specifying the size would be seen as an absolute and the partition would be resized to that size. Finally, we provide the path to the logical volume that we want to resize.

sudo lvextend -L+50GB /dev/volume1/uploads

When using the lvextend command the -L flag is used to denote the size. By using the + we are saying to increase the size of the logical volume by 50GB, and again we are providing the path to the logical volume we want to resize. Unlike lvresize, this does not resize the filesystem inside for us, but just simply changes the size of the logical volume itself. To resize the filesystem we can use the following command:

sudo resize2fs /dev/volume1/uploads

How To Shrink A Logical Volume

If you need to shrink a logical volume to free up space in a logical volume, you can use the lvresize command again. Here are a few examples:

sudo lvresize --resizefs --size 20GB /dev/volume1/uploads

sudo lvresize --resizefs --size -10GB /dev/volume1/uploads

The first command sets the size of the /dev/volume1/uploads volume to 20GB. The second command shrinks the volume by 10GB from its current size. As before the command resizes both the volume size as well as the filesystem. You can do both separately, but you run the risk that a typo could mean resizing the volume to be smaller than the filesystem and so breaking the filesystem.

Add Or Remove Physical Volumes

In addition to resizing the logical volumes, you may also need to add or remove a physical volume from a volume group. First, you’ll need to prepare the partition for use by using the pvcreate command,. After which it can be added to an existing volume group with the following command:

sudo vgextend volume1 /dev/sde1

As with vgcreate, the first option is the name of the volume group being extended. Once complete, it can be followed by multiple partitions that can be added later. If you wish to remove a physical volume from the volume group, you can do so using the following commands:

sudo pvmove /dev/sdc1

sudo vgreduce volume1 /dev/sdc1

The first command moves all the data stored on the physical volume /dev/sdc1 to the other physical volumes in the volume group. Take note that this can take a long time. Make sure that you have enough free space in the other physical volumes to take all of the data from the volume you are removing. Once all of the data has been moved, the vgreduce command reduces the size of the volume group by removing the specified partition, in this case,/dev/sdc1.

If you have added a new partition to the volume group in order to replace an old one, then you can move the physical volumes data directly from one disk to another using pvmove by also specifying a destination physical volume:

sudo pvmove /dev/sdc1 /dev/sde1

While it can be daunting at first, the processes to work with logical volumes and manipulate them is relatively straight forward and can allow for a great deal of flexibility. With systems able to grow to meet their changing requirements over time rather than being limited to the specification with which they were initially configured, LVM is a very useful tool to have.