📅 April 1, 2024
We have our physical volumes created, but what do we do with them?
Can we format them? Can we store data on them?
No, not yet. A physical volume merely specifies a drive or partition that is to be used with LVM. The next step is to assign physical volumes to volume groups, and this is were the fun and flexibility of LVM becomes apparent.
The Volume Group
Physical volumes by themselves are not associated with anything. We must assign physical volumes to volume groups. Think of a volume group as a group of physical volumes — a storage pool — from which we can divvy up into logical drives as needed. This is very flexible because we can create many groups if we need them, and each group is separate from the others. We can make the pool larger or smaller by adding or removing physical volumes.
In the future, we can expand the capacity of either volume group by adding another physical volume and then assigning it to a volume group. Physical volumes cannot be shared between volume groups. A physical volume must belong to one volume group only. Not shared. However, different partitions on a single physical device can be assigned to different volume groups as different physical volumes, thus, “sharing” one physical drive between volume groups via partitions.
Personally, I avoid this kind of partitioning assignment because what if /dev/sdb fails? We would lose data in both volume groups. However, LVM lets you perform this kind of assignment in case you need it.
Volume Group Commands
Just like the pv commands handling physical volumes (pvcreate, pvremove, and more) all begin with pv, volume group commands all begin with vg. There are more of them than there are pv commands because the volume group layer is where you will spend most of your time customizing storage options.
vgcfgbackup - Backup volume group metadata to /etc/lvm/backup directory vgcfgrestore - Restore volume group configuration vgchange - Change the volume group attributes vgck - Check volume group metadata for consistency vgcreate - Create a volume group (the most important) vgconvert - Converts LVM1 to LVM2. Support removed. No longer used, so avoid it. vgdisplay - Show volume group information vgexport - Export. Puts volume group into an unusable, offline state until vgimport vgextend - Add a physical volume to an existing volume group. Adds more storage. vgimport - Registers an exported volume group with the system. Used for migration. vgimportclone - Imports a volume group from cloned physical volumes vgmerge - Combine two volume groups into one vgmknodes - Create /dev files for volume groups that might be missing vgreduce - Remove a physical volume from a volume group. The opposite of vgextend vgremove - Remove a volume group vgrename - Rename a volume group. Yes, volume groups must have names vgs - A brief overview of detected volume groups vgscan - Check if any volume groups exist on the system vgsplit - Move a physical volume from one volume group to another.
Just like with pv commands, all volume group commands require sudo privilege. We will only cover the most essential commands in this article.
Create a Volume Group
Let’s create a very simple volume group from our three physical volumes.
sudo vgcreate vgdata /dev/sdb /dev/sdc /dev/sdd
Output
Volume group "vgdata" successfully created
In this example, we created a volume group named vgdata using the vgcreate command. We can make up almost any name that we want, but keep it reasonable. Avoid spaces, special characters, and excessive length. Remember, you will need to type this and identify it should you ever want to modify the volume group. I included the “vg” prefix to help identify this name as a volume group.
List the device files for the physical volumes. Order does not matter. If you have partitions on a drive, use the partition device files, such as /dev/sdb1 or /dev/sdb2.
Check if a Volume Group Exists
sudo vgscan
View Brief Volume Group Details
Above, we found that the volume group we created exists, and its name is vgdata. What if we need more information about it?
sudo vgs
sudo vgs vgdata
View Detailed Volume Group Details
Just like the pvdisplay command, we can use the vgdisplay command to view further details about a volume group.
sudo vgdisplay
Which Physical Volumes in the Volume Group?
“How can I view which exact physical volumes are in the volume group?”
Remember the pvdisplay command? Run it again, and look at the VG Name row that is now populated. All physical volumes of the same VG Name will show which drives are assigned to the volume group in the PV Name row.
sudo vgdisplay
sudo pvs
If multiple volume groups exist, this is more selective:
sudo pvs | egrep vgdata
Or if you want to be fancy:
sudo pvdisplay -S vgname=vgdata -C -o pv_name
Oops! How to Remove a Volume Group
Suppose we made a mistake or we need to eliminate a volume group. The vgremove command removes a given volume group.
sudo vgremove vgdata
Keep in mind that the physical volumes still exist. We would also need to run pvremove on each to unmark them for use by LVM.
Creating Two Volume Groups
From our three 10GB physical volumes that we still have, let’s create two separate volume groups named popcorn and fluffybear.
sudo vgcreate popcorn /dev/sdc sudo vgcreate fluffybear /dev/sdb /dev/sdd
In this case, which drives we assign does not matter since we are going to change things. Now, let’s look at what we have created:
sudo pvs
sudo vgs popcorn sudo vgs fluffybear
Removing a Physical Volume
We want to remove the physical volume from the popcorn volume group. First, find out the device file of the physical volume you want to remove. In this case, it is /dev/sdc obtained from vgs above. Then, use vgreduce, *NOT* vgremove, to remove the physical volume from the volume group.
sudo vgreduce popcorn /dev/sdc
The vgreduce command reduces the size of a volume group by removing a physical volume, but we cannot remove the last physical volume from a volume group. If there is only one volume left in the group, then the command fails. Let’s try removing a volume from the fluffybear group.
sudo vgreduce fluffybear /dev/sdb
This is why volume groups have names. Multiple groups can exist, and we need to be specific about which one to modify by using its name.
Now, try to remove the last volume from the fluffybear group.
sudo vgreduce fluffybear /dev/sdd
Merging Volume Groups
As it stands now, we have two volume groups, popcorn and fluffybear, that each contain one physical volume. We want to combine these two volume groups into one.
sudo vgmerge fluffybear popcorn
The volume popcorn merges its data and physical volumes into fluffybear (listed first) and preserves all data. Right now, we have no data, so this is going to work. However, vgmerge can involve some thought and considerations due to many factors, so be careful when merging if containing data is involved. LVM allows advanced data migration techniques like this with minimal effort.
The popcorn volume group is removed automatically. Only the fluffybear volume group remains, and it contains the physical volume from popcorn.
sudo vgs sudo pvs | egrep fluffybear
Renaming a Volume Group
Right now, we have a volume group named fluffybear, but we want to rename it to vgdata like we had before. We use the vgrename command to rename a volume group.
sudo vgrename fluffybear vgdata
Adding a Physical Volume to a Volume Group
What about the third, unused physical volume /dev/sdb? We want to add it to the vgdata volume group, and we can do this using the vgextend command.
sudo vgextend vgdata /dev/sdb
Conclusion
We are getting closer to a working LVM system, but users cannot use it yet. We still need to create a logical volume that can be formatted and mounted, and that will be covered in part 3.
Volume groups are the main part of LVM, so definitely peruse the man pages for the vg commands. There are many, many options that we cannot possibly cover here, but it shows how flexible LVM can be for almost any storage scenario that you might need.
Have fun!