What is LVM and How To Create LVM Partition in Linux.
What is LVM ?
In Linux, Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. Most modern Linux distributions are LVM-aware to the point of being able to have their root file systems on a logical volume.
With LVM we can create, increase and decrease the size of partition anytime. We can increase and decrease the size of partition only if that partition is created with LVM.
How To Create LVM Partition ?
In this demo I will be using RedHat Enterprise Linux 8. We can use any version of Linux from any distribution. All the commands below are being executed by the root user, if you are gonna execute this commands from normal user use ‘ sudo ’ in the start of all commands.
Here I am using a 10 GB drive :
Steps to create LVM Partition in Linux :
Step 1 :- Create physical Volume(PV). To create physical volume use “ pvcreate (volume name/storage name) ” command in your terminal.
To confirm our Physical Volume is created or not use “pvdisplay (volume name/storage name)” command in your terminal.
Step 2 :- Create Volume Group(VG). To Create Volume Group use “ vgcreate (Name for our VG) (Name of our volume/drive)” command.
To confirm our Volume Group is created or not use “vgdisplay (vg name)” command.
Step 3 :- Create Logical Volume. Creating Logical volume is similar as creating partition. For Creating Logical volume use “lvcreate — size (size we need) — name (name for the partition) (our Volume Group name)” command.
Here I have created a partition of 4GB. To check the Logical Volume created or not use “lvdisplay /dev/(VG name)/(LV name)” command.
We can see the new created partition with “fdisk -l” command.
Now we have to format the created partition and mount to a folder. Here I am formatting the partition with mkfs.ext4. We can use any extension for formatting the partition. Command for formatting partition with mkfs.ext4 is “mkfs.ext4 (partition name)”.
Now we have to mount the partition. Let’s create a directory and then mount the partition to that directory.
Here I have created a directory named LVM_partition . And mounted that partition with the directory.
We can check is the partition mounted to the directory or not with “df -h” command.
It’s mounted successfully and have a size of approximately 4GB. Now We Can Use The Partition and Also Change the size of Partition.
Creating partition with 1 external drive is static and creating partition with the concept of LVM it’s dynamic partition.
Let’s Increase the size of Partition :
To increase the size of partition use “lvextend — size (+ how much size we want to extend ) (partition name)” command.
Now we have to format the extended partition. For this we will use “resize2fs (partition name)” command.
We can confirm it with “df -h” command.
Now in future if we have a requirement to increase the size of Logical Volume urgently but if we don’t have extra drive/storage to increase the size of Volume Group so what can we do is decrease the size of other Logical Volume which has extra space in it. As we decrease the size of Logical Volume then that volume will be returned to the Volume Group.
Let’s see how to decrease the size of Logical Volume Few Steps are as follows:
Step 1 :- First unmount the Logical Volume from which we are going to decrease the size. Command to unmount the LV is “umount (Mount point name / directory to which we have mounted the Logical Volume)”.
Step 2 :- Clean/Clear the Inode table with “ e2fsck -f (partition/LV name)”.
Step 3 :- Now Format the inode table with “resize2fs (Partition/LV name) (Size we want to reduce)”. Here I want to have 2GB as my Logical Volume means I will reduce the size by 7 GB.
Step 4 :- Reduce Size of Logical Volume with “lvreduce — size (Size we want to reduce) (name of the logical volume from where we are reducing the size)”.
Step 5 :- Mount it.
To can confirm it use “lvdisplay (partition name)” command.
We can also reduce the size of logical volume which contains some data. The only problem we will face is that our data in that part of logical volume will be destroyed from which we are reducing the size.
For example:
In a logical volume of 10 GB if 6 GB is used then by simple mathematics 4 GB is left. If we reduce the size by less than 4 GB then the data from the drive will not be lost. But if we reduce the size by more than 4 GB then the amount of data will be lost according to the size decreased.
Now again I will increase the size to 9GB with lvextend comamnd.
From our 10 GB drive we have created partition of around 9 GB so if we want to extend more space than 1 GB then we have to add more volume to our Volume Group.
To Extend the size of Volume Group we have some steps as follows:
Step 1 :- Attach a drive/storage. Here I have attached a volume of 10 GB named /dev/sdc.
Step 2 :- Convert it into Physical Volume (Refer the above commands to create Physical Volume).
Step 3 :- Now Extend the Volume Group with “ vgextend ( Name of Volume Group) ( Name of new drive/storage)”
Now the size of Volume Group is increased by 10 GB.
By this way we can extend the size of Volume Group and also the size Logical Volume.
So with LVM we can increase and decrease the size of partition/LV.
Thanks For Reading.