You can easily resize a volume group by extending it with a physical volume using the vgextend command. The command is simple, and you only need to add the physical volume as an argument when using the vgextend command.
If you haven’t worked with volume groups or are looking for a safe way of extending your volume group, we will cover a practical example in this guide. Let’s get started.
What Is LVM?
LVM is a Linux system responsible for managing file systems and logical volumes in a Linux system. Although there are other volume management tools in Linux, using LVM is recommended for its advanced features. As we will see in this guide, there is so much that you can achieve using this command line tool.
To better understand how to use the vgextend command, we will create two physical volumes and a volume group. Once completed, we will use the vgextend to add one physical volume to the volume group of the other physical volume.
Creating Physical Volumes
Start by checking if any physical volumes are already created using the following command:
We currently have no physical volume. We need a block device to initialize the physical volume. We can list the block devices using the following command:
Since we need to create two physical volumes, we will use the /dev/sda1 and /dev/sdb1. But before that, we must unmount the block devices.
To unmount a block device, use the following commands and replace the block device to match your case:
$ sudo umount /dev/sdb1
With our block devices unmounted, we can proceed to use the pvcreate command to initialize the physical volumes.
To create two physical volumes, the following command will be:
$ sudo pvcreate /dev/sdb1
We successfully created our physical volumes and can confirm by using the pvs command.
Creating Volume Groups
We need a physical volume to create a volume group. Let’s start by verifying that we have no volume group in either of the physical volumes using the following command:
We can now proceed to create a volume group for one of the physical volumes, which we will extend its size by adding the other physical volume to it. So, to create a volume group for /dev/sdb1, the following command will be:
We named our volume group volgroup. We can verify it using the vgs command.
To get more details about the created volume group, use the following command:
What we need to focus on is the Free PE size for the volume group. We currently have 1919 Free PE. To extend this size, we can use the vgextend command and add the name of our other physical volume, /dev/sda1, as an argument.
The following command would be:
You should get a success message like the one in the previous illustration confirming that the volume group is successfully extended. We can verify the new size as shown in the following:
Bingo! You can note our new free PE size extended from 1919 to 2046. That’s how you can easily use the vgextend Linux command to expand the size of your volume groups by adding a physical volume.
Conclusion
Working with LVM allows you to create partitions and logical volumes and extend them whenever needed. Various Linux commands come in handy when working with LVM. In this guide, we discussed most of the commands for creating physical volumes and volume groups. Our main focus is extending the size of volume groups using the vgextend command. You now understand how to use the vgextend command in Linux.