Creating a LVM Logical Volume
Problem
The following describes the process of creating the three layers in the LVM architecture using an installation of RHEL/CentOS.
Notable
- XFS file systems do not allow logical volume size reduction, which is used as the default on RHEL7.
- By default
vgcreate
will automatically flag a physical volume that is doesn’t have a type associated with it (e.g.8e
for MBR or8300
for GPT).
Solution
Layer 1: Physical Volume (PV)
- Create a MBR or GPT partition.
-
Assign partition to PV.
pvcreate /dev/${DEVICE}
-
Verify the PV.
# Listing pvs # Summary pvdisplay # Hierarchical lsblk
Layer 2: Volume Group (VG)
-
Assign PV to VG.
vgcreate ${VG_NAME} /dev/${DEVICE}
-
Verify the VG.
# Listing vgs # Summary vgdisplay
Physical Extent (PE)
When creating a VG, a PE size is used. The PE size defines the size of the building blocks used to create logical volumes.
By default, the extent size is 4MiB. Also, the PE size is always specified as a multiple of 2MiB, with a maximum size of 128 MiB. Use the -s
option to specify the PE size.
Logical Extent (LE)
When working with an Ext4 file system, a LE size is used. The LE size defines the size of the building blocks used to create logical volumes.
Notice the extent size on LVM is in no way related to the extent sizes that are used on the file systems.
Layer 3: Logical Volume (LV)
-
Assign LV to VG.
# Absolue size lvcreate -n ${LV_NAME} -L 100M ${VG_NAME} # Relative size lvcreate -n ${LV_NAME} -l 50%FREE ${VG_NAME}
-
Verify the LV.
# Listing lvs # Summary lvdisplay
-
Create a file system on top of the LV.
mkfs.xfs /dev/${VG_NAME}/${LV_NAME}
Summary
The idea is simple:
- If you are running out of disk space on a LV, take available from the VG.
- If there isn’t disk space available in the VG, add a PV.