What is LVM? How to create PV, VG and LV partitions??
What is LVM?
LVM is known as Logical Volume Management. When we did the installation of CentOS 7, LVM was the default partitioning scheme. Nowadays the LVM partitioning scheme is the default for most of the Linux distros. Below is the snapshot taken during installation of the CentOS 7, showing the default LVM scheme is selected.
Benefits of LVM
- We can resize and extend the volume easily.
- Combine multiple hard disks together.
- A logical volume can be larger than the single hard disk size.
- Supports mirroring features.
What is the Concept of LVM
Step 1. Create the PV (Physical Volume) – We can configure entire one or multiple hard disks or partitions as PVs.
Step 2. Next step is to create VG (Volume Group) – By combining one or more PVs, VG is created. In simple words, Pvs are combined to create single large storage.
Step 3. Create LV (Logical Volume) – LVs are created by dividing the VG into the small volumes.
we have 2 hard disks of 80 GB each. Now we want to create volume with 120 GB. Is it possible?? Yes, it is possible because of LVM.
– Firstly we will configure both entire hard disk as PVs (Physical Volumes)
– Next step to create the VG (Volume group), so it will consider the 80 G + 80 G = 160 G as single storage.
– Lastly, we can create the desired LG (logical volume) size i.e 120 G.
Manage LVM Partitions
Step 1. How to Create PV (Physical Volume)
Run the following command to create PV on hard disk /dev/sdb and /dev/sdc:
# pvcreate /dev/sdb /dev/sdc
To check created PV:
Create VG (Volume group)
We need to define the name of VG, here we are giving name ‘ itsvg’, which is created by combining either alone or both /dev/sdb and /dev/sdc.
# vgcreate itsvg /dev/sdb /dev/sdc
To check VG, run this command:
Create LV (Logical Volume)
VG is created, its the total size of the combined Pvs. Now, we have one large storage (itsvg) , from which we will create logical volumes. To create an LG name ‘websites_vol’ with size 10 GB, run this command:
# lvcreate -n websites_vol -L 10G itsvg
-n = Name of the Logical Volume
-L = Fixed size of the Logical Volume
itsvg = Name of the Volume Group
To check LV:
How to format newly created Logical Volume:
# mkfs.ext4 /dev/itsvg/websites_vol
Next step is to mount the Logical volume with the directory ‘websites’:
# mount /dev/itsvg/websites_vol /websites
Step 2. How to Extend Volume Group & Logical Volume
To extend the Volume Group, need to add Physical Volume in VG.
- Create PV /dev/sdd.
# pvcreate /dev/sdd
- Add in VG ,Syntax is:
# vgextend volume_group_name /dev/sdd
# vgextend itsvg /dev/sdd
Extend Logical Volume (LV)
Above we have created, Logical Volume name ‘/dev/itsvg/websites_vol‘. Run the following command:
lvextend -L size logical_volume_name
# lvextend -L +5G /dev/itsvg/websites_vol
After this command, need to resize the filesystem. Run the resize2fs command:
# resize2fs /dev/itsvg/websites_vol
Step3. How to Reduce the size of LVM Partitions
To reduce the size of LVM is not difficult, but for safer side firstly you should take the backup of the partition. It is the five steps process:
- Unmount the file system
- e2fsck command – check the file system for any error
- Reduce file system
- Reduce LV size
- Again Check the file system for any error
- Remount again
Unmount the file system
# umount /websites
Check file system for any error using e2fsck command :
# e2fsck -f /dev/itsvg/websites_vol
Where, f = forecfully check.
Reduce the filesystem size to 5GB:
# resize2fs /dev/itsvg/websites_vol 5G
Now reduce the size of Logical Volume:
# lvreduce -L -5G /dev/itsvg/websites_vol
Again check file system for any error:
# e2fsck -f /dev/itsvg/websites_vol
Final step to mount again with the same directory:
# mount /websites
Congratulations !!! You learned how to manage LVM, create, reduce the size of the PV (Physical Volume), VG (Volume Group) and LV (Logical Volume).