Working with Logical Volumes (LVM)
Logical Volume Management (LVM) allows for very flexible disk space management on your server. It provides the ability to add disk space to a logical volume and it’s filesystem and the ability to allows you to use multiple physical hard drives and partitions into a single volume group which can then be divided into logical volumes.
The structure of LVM is made up of Physical volumes, Volume groups and Logical volumes:
Physical volumes (PVs): storage devices (physical disks, partitions, RAID arrays etc.) formatted in a certain way, providing raw storage space
Volume groups (VGs): gather PVs together into a pool from which LVs are created
Logical volumes (LVs): devices which you create from space within a VG, which you can use as if they were partitions (e.g. create a filesystem on them)
Before you use LVM
Be aware that using LVM’s across different physical disks can put your data at risk.
If one drive that is part of the same Logical volume fails then you may loose access to all data on that Logical volume.
As always, make sure you are regularly backing up your data first!
Your server may already be setup with LVM. Red Hat distributions and it derivatives (e.g. CentOS). Here are some commands for you to get more information about your LVM setup.
To list the physical devices
List the volume groups
List the logical volumes
Creating a PV, VG and LV
Create a Physical Volume (PV)
Create a Volume Group (VG)
vgcreate [new volume group name] /dev/sd[x] /dev/sd[x] ...
Create a Logical Volume (LV)
lvcreate -L [Size in GB]G [Volume Group Name]
Or set the size by Percentage
lvcreate -l 50% [Volume Group Name]
Extend a Volume Group
A PV will need to be created on the new drive or partition first, then you can add it to an existing VG:
vgextend [volume group name] /dev/sd[x]
Extend a Logical Volume
If you have space in your volume group and want to increase the size of a LV:
lvextend -L+[Number of GB to add]G -n /dev/[volume group name]/[logicial volume name]
You will then need to resize the filesystem
resize2fs /dev/[volume group name]/[logical volume name]
xfs_growfs -d /dev/[volume group name]/[logical volume name]
FSCK on LVM
If you are running the main root (/) on a logical volume, it can be tricky to get the OS to start when your filesystem needs a check.
To manually run a filesystem check, you can do it from a live CD.
First boot off a Live CD
Scan the physical volumes, volume group and logical volumes to get the required information:
pvscan vgscan lvscan
Activate (make availiable) the logical volume:
lvchange -ay "yourlogicalvolume"
Finally, run a filsystem check:
e2fsck -yfv /yourlogicalvolume