Logical Volume Manager (LVM) plays an important role in the Linux operating system by improving the availability, disk I/O, performance and capability of disk management.
LVM is a widely used technique that is extremely flexible for disk management.
This adds an extra layer between the physical disks and the file system, allowing you to create a logical volume instead of a physical disk.
LVM allows you to easily resize, extend and decrease the logical volume when you need it.
We need to identify the correct disk which are to be used in the LVM using the fdisk command or any other disk management command [lsblk].
For example, Lets say we have three 8GB disk /dev/sdc , /dev/sdd and /dev/sde are available.
# fdisk -l
- Create a Physical Volumes(PV) on the disk.
- Create the Volume Group(VG) on the Physical Volumes
- Create Logical Volumes(LV) on the Volume Group
- Create a filesystem for the logical volumes**
We can create the physical volumes using pvcreate command as shown below which initialize a disk or partition for use by LVM. Physical volumes (PV) are the partitions on hard disk, or hard disk itself. PV are the base of LVM structure and referred as physical volumes.
pvcreate [Physical Volume Name]
Make a note:
The above command erases any data on the given disks /dev/sde, /dev/sdc and /dev/sdd.
Physical disk can be added directly into the LVM PV instead of the disk partition.
Use pvs command to list out your physical volumes
Volume groups are nothing but a pool of storage that consists of one or more physical volumes. Once you create the physical volume, you can create the volume group (VG) from these physical volumes (PV).
Use the vgcreate command to create a Volumegroup on the physical volumes and vgs to show volume group.
vgcreate [Volume Group Name] [Physical Volume Name]
vgcreate vcontainer /dev/sdc /dev/sdd
Now if you want to extend the volume again, Ist check if you have any physical volume or not..
so here we have a extra physical volume [ dev/sde ]
vgextend [Existing Volume Group Name] [Physical Volume Name]
vgextend vcontainer /dev/sde
After creating the volumegroup, use “lvcreate” command to create the logical volumes (LV) within the Volume group (VG).
Lets create a Logical volume of 20GB “mylv”(logical vol name) using the command “lvcreate” as shown in the below.
lvcreate –L [Logical Volume Size] –n [Logical Volume Name] [Name of the Volume Group where the LV to be created]
lvcreate -L 20GB -n mylv vcontainer
Lets create a filesystem, so the logical volume will be ready to use.
- mkdir /vol-1 Here vol-1 is a directory name where we want to mount our lvm.)
Before mounting , we need to format it using mkfs command
Now mount that lvm on the vol-1 directory using mount command
To mount it permanently use that UUID number and paste it inside etc/fstab path
- vi /etc/fstab /dev/vg01/lv001 /lvmtest xfs defaults 0 0
How to Extend/Increase LVM’s (Logical Volume Resize)
Expanding the logical volume is extremely easy, it takes very few steps and can be done online without unmounting a certain logical volume.
The main purpose of LVM is flexible disk management, which allows you to easily resize, extend and reduce the logical volume size when you need it.
Use the following command to increase the existing logical volume.
lvextend [Additional space to be added] [Existing Logical Volume Name]
lvextend -L +2G /dev/mapper/vcontainer-mylv
Now, the logical volume is extended and you need to resize the file system to extend the space inside the logical volume.
How to Reduce/Shrink LVM’s (Logical Volume Resize)
Reducing/Shrinking the logical volume is the highest risk of data corruption.
So try to avoid this kind of situation if possible, but go ahead if you have no other options.
It is always recommended to make a backup before shrinking an LVM.
When you are running out of disk space in LVM, you can make some free space on the volume group by reducing the exsisting LVM that no longer uses the full size, instead of adding a new physical disk.
Use the umount command to unmount the file system
- umount -v [file system]
resize2fs /dev/mapper/vcontainer-mylv 10G
Reduce the Logical Volume (LVM)
lvreduce [New Size of LVM] [Existing Logical Volume Name]
Mount the file system and check the reduced size