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
How to create a LVM in Linux, these are the below steps to be followed.
- 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**
1)How to Create LVM Physical 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
2)How to Create a Volume Group
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
here vcontainer is the name of volume group.
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
Use the vgs and vgdisplay commands to display information about the VG you created.
3)How to Create Logical Volume Using
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
4)Create filesystem for the logical volumes.
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
Note
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
Here as shown below a lvm of 20GB successfully mounted on a folder called vol-1
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.
resize2fs /dev/mapper/vcontainer-mylv
as you can see that the volume increased by 2 GB.
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]
Check the file system for any Errors using e2fsck -f commands
Shrink the file system.
resize2fs [Existing Logical Volume Name] [New Size of 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
Top comments (0)