LVM series continues. If you want to start from the beginning, check our first post. You can review our last post, a quick example about creating a filesystem using LVM.
In this post we are going to review how to manage Volume Groups (VG) with LVM.
As seen in the introduction post, VG is the core of LVM. We assign storage devices to VG and we create logical volumes in a VG with space available.
To begin with, we will prepare a storage device to be used with LVM.
Creating Physical Volumes (PV)
To make a storage device ready for LVM, we need to use “pvcreate” tool.
Let’s prepare /dev/sdb:
pvcreate /dev/sdb
Get info about PVs
To get PV info you can use “pvdisplay“.
For instance, if you want to get info about /dev/sdb, you can type:
pvdisplay /dev/sdb
Creating Volume Groups (VG)
We are going to create a VG named vg_storage1 and we are going to use /dev/sdb and /dev/sdc.
“vgcreate” is the tool you need.
vgcreate vg_storage1 /dev/sdb /dev/sdc
Getting info about VGs
With “vgdisplay” you can get info about all the VGs in the system.
If you need to get more detailed info about one VG, which is quite normal, you can obtain it this way:
vgdisplay -v vg_storage1
It’s especially important to get the amount of available space to resize or create LVs and to get the devices (PVs) assigned to this VG.
Adding another device to an existing VG
If you need more space in one VG, usually you will achieve this by adding more storage devices to that VG.
First you need to create a PV out of this new device. Review the first part of this post for further information.
After that, use “vgextend” to add it. In this case, we are adding /dev/sdd to the vg_storage1.
vgextend vg_storage1 /dev/sdd
Removing a storage unit from a VG
Removing a storage unit from a VG can be a pain. As you can imagine, to remove a storage unit we need to make sure first that this unit does not have any data.
This is why we need a tool called “pvmove“. This tool is going to move data from one storage device to others.
pvmove
This tool moves all data from one unit to the rest until it is empty. This way you can remove it from the VG.
In this example we want to empty /dev/sdc.
pvmove /dev/sdc
Of course, you need enough space in the rest of the units to storage all data in /dev/sdc.
vgreduce
“vgreduce” is the tool that you need to use to remove an empty device from a VG.
Let’s remove /dev/sdc from vg_storage1.
vgreduce vg_storage1 /dev/sdc
Deleting a VG
If one of our VG is not useful anymore, you can remove it with “vgremove” tool.
Let’s remove vg_storage1:
vgremove vg_storage1
We will continue our LVM series with Logical Volume Management.