Hello everyone In this Article we are going to learn What is Logical Volume Manager (LVM) in Linux in a very simple and easy step-by-step approach
What is lvm in Linux
What is Logical volume manger ?
When we use the partition in the Linux then we cannot able to expand and shrink the size of that partition also we cannot able to assign the name of the partition according to us.
To overcome this problem Logical volume manager ( LVM ) comes into the picture with the help of LVM we can able to expand and shrink the size of the disk and also we can able to assign a custom name to harddisk
This is the high-level architecture of Logical Volume Manager in LVM we have 3 types of volumes
1 Physical Volume ( PV ) – The Physical volume is the hard disk that is attached to our server. According to our diagram, there are 3 hard disks attached to our server each size of 10 GB
2 Volume Group ( VG ) – The Volume Group is the combination of Physical volume that is attached to our server. When we combine all the Physical Volumes then we get the volume group According to our diagram there are 3 hard disks attached to our server each size of 10 GB. So our volume group will be of 30 Gb in total
3 Logical Volume ( LV ) – The Logical volume is the space which is come from the Volume group. According to our diagram, we have assigned the 10 Gb of space to HR-Team, 10 Gb space to Admin Team and 10 Gb space to the development team
- If we need 50 Gb space but we have only 30 Gb of space according to our diagram. In this scenario, we need to add more hard disk to our physical volume and then we can increase the size of the Volume group after that we can get desire size of the Logical volume
To Work with the LVM, We have attached 3 Hard disks of each size of 10 GB to our Linux server. Toal of 30 GB of the hard disk is attached to our server.
To check how many hard disk is attached to our server we can use lsblk command.
# lsblk
We have attached 3 hard disk as /dev/xvdf , /dev/xvdg , dev/xvdh soo we are making Physical volume on these hardisk
How to create Physical Volume
Syntax of Physical Volume command
- # pvcreate -v [hard disk name]
- The pvcreate is the command that is used to create the Physical volume in the Linux kernel we can define For which hard disk we want to create the Physical volume.
# pvcreate /dev/xvdf
- How to get the Information for pvcreate when we making physical volume– If we add -v option in the pvcreate command then this will tell us that process that is running in the background while making the Physical Volume.
# pvcreate -v /dev/xvdg
- # pvcreate -v /dev/xvdh – We have to use pvcreate command every time when we are creating the Physical volume.
# pvcreate -v /dev/xvdh
How to display the physical volume that we created
With the help of pvdisplay command, we can able to see the detailed information of the Physical volume that we create.
# pvdisplay
As we can see above that we created 3 Physical Volume successfully now.
How to create Volume Group
Syntax of Volume Group command
- # vg -v [volumegroup name] [disk name on which the Physical volume is created ]
- How to create the volume group
The vgcreate is the command that is used to make the Volume volume in the Linux kernel we can define For which hard disk we want to create the Volume volume.
# vgcreate -v volumecontainer /dev/xvdf /dev/xvdg
- How to Display Volume container that we created – With the help of vgdisplay command we can able to see that what volume group is created. with the name volumecontainer.
# vgdisplay
Now we created 2 Physical volume names as /dev/xvdf /dev/xvdg to our volume group only 1 Physical volume named as /dev/xvdh is left. So we are expanding the /dev/xvdh to our volumecontainer now.
- How to add the Volume container that we created above
The vgextend is the command that is used to add the current volume group.
# vgextend -v volumecontainer /dev/xvdf /dev/xvdg /dev/xvdh
The pvs are the command that is used to seeing which hard disk is attached to what volume group.
# pvs
- Till now we have created the Volume group soo we have a total of 30 Gb of the Volume group.
How to create Logical Volume
Syntax of Logical volume command
- lvcreate -L [size we need] -n [Name of Logical volume that we need ][Name of volume container]
We are creating the Logical Volume of size 12 Gb now
- How to create Logical Volume
The lvcreate is the command that is used to make the logical volume from the volume group we defined that we are creating 12GB of Logical Volume. with the name HR-Team-LV.
# lvcreate -L 12G -n HR-Team-LV volumecontainer
- # lvdisplay – The lvdisplay is the command that is used to show how much Logical volume is created .
How to format the Logical volume that we created
Syntax
mkfs -t [which format we need partiotn] name of the [logicalvolume]
- How to format the logical volume disk that we created
The mkfs is the command to format the logical volume and we are formatting our logical volume in ext4 format.
# mkfs -t ext4 /dev/volumecontainer/HR-Team-LV
# blkid – Now when we execute the blkid command we can see that partition that we formatted above is display here
Now we are mounting this to our /hr-team-work directory
We need to do an entry to /etc/fstab file with the UUID of the Logical volume partition
Run # mount -a command now to verify everything is working perfect.
# df-h – With the help of df -h command we can now verify that our file is mounted perfectly
How to Extend the logical volume that we create
Syntax
- lvextend -L [size we need ][ Name of file system]
- How to increase the size for the Logical Volume
With the help of lvextend command we can increase the size of the logical volume that we created we are increasing the size 3Gb.
# lvextend -L +3G /dev/mapper/volumecontainer-HR--Team--LV
Now we need to assign the file system to the volume group that we extended
The resize2fs is the command that is use to assign the file system.
The block id for the volume group that we created
# resize2fs /dev/mapper/volumecontainer-HR--Team--LV
# df -h – Now the size is increase to 15 Gb
Now we are reducing the size of the newly created filesystem to 8GB
Syntax
# lvreduce-L [Size we want to reduce]pFile system name]
We are reducing 7GB from the total space by using lvreduce command and -r is used to resize the file system soo we not need to execute resize2fs command .
# lvreduce -L -7G -r /dev/mapper/volumecontainer-HR--Team--LV
# df -h – Now the file system is reduced to 7.9 Gb
Please note that we can skip both the #lvextend & #lvreduce command with the #lvresize command
Syntax
#lvresize -L [Desire size we want ]
We just put our need what size we want accordingly LVM do this for us. We want only 15GB of file system soo we are defining that only.
# lvresize -L 15G -r /dev/mapper/volumecontainer-HR--Team--LV
#df -h – Now when we execute this command file system size will be 12 Gb
Conclusion
If you want to learn Linux in the most simple language then please click on this link.
This is the complete knowledge of Logical volume manager in Linux kernel if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.