LVM means Logical Volume Management. The combination of two or more physical disk in order to make a big logical disk is called Logical Volume. If normal Linux partition is full and an application requires some more disk space, then normal partition cannot be extended for that particular application requirement. To achieve this first you have to take a backup of that normal partition, delete that partition and again create that partition with more disk space, format and mount the partition and then restore that application from the backup. This process requires down time. So this problem can be overcome by LVM. Using this LVM you can extend or reduce the file systems as per requirement without loss of any data and without downtime. This tutorial describes how to configure LVM (Logical Volume Management) in Linux.
How to Create Physical Volumes for LVM
Disk, RAID array, SAN disk, or a partition can be used as the LVM physical volume. Suppose, you already added three disks, these disks are /dev/sdb, and /dev/sdc, /dev/sdd.
Syntax for the creation of a physical volume (pvcreate) is as follow.
pvcreate [Physical Volume Name]
When the disk is detected in the OS, execute the pvcreate command to initialize the LVM PV (Physical Volumes).
# pvcreate /dev/sdb /dev/sdc /dev/sdd Physical volume "/dev/sdb" successfully created Physical volume "/dev/sdc" successfully created Physical volume "/dev/sdd" successfully created
Note: This command erases data stored in the given disks /dev/sdb, /dev/sdc and /dev/sdd. You can add Physical disk directly into the LVM PV instead of the disk partition. You can use the pvdisplay and pvs commands to display the PVs you have created earlier.
The pvs command displays summarize output and the pvdisplay gives the detailed output of the PVs.
# pvs PV VG Fmt Attr PSize PFree /dev/sdb lvm2 a-- 15.00g 15.00g /dev/sdc lvm2 a-- 15.00g 15.00g /dev/sdd lvm2 a-- 15.00g 15.00g # pvdisplay "/dev/sdb" is new physical volume of "15.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 15.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 69d9dd18-36be-4631-9ebb-78f05fe3217f "/dev/sdc" is new physical volume of "15.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc VG Name PV Size 15.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID a2092b92-af29-4760-8e68-7a201922573b "/dev/sdd" is new physical volume of "15.00 GiB" --- NEW Physical volume --- PV Name /dev/sdd VG Name PV Size 15.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID d92fa769-e00f-4fd7-b6ed-ecf7224af7faS
How to Create a Volume Group
Volume group is the next layer in LVM structure. Basically, volume group contains the physical volumes you created. Physical volumes can be added into an existing volume group or create a new volume group for physical volume as needed.
Simple syntax for Volume Group creation (vgcreate).
vgcreate [Volume Group Name] [Physical Volume Name]
Use the below command to add a new physical volume to the new volume group.
# vgcreate vg01 /dev/sdb /dev/sdc /dev/sdd Volume group "vg01" successfully created
Note: By default, it uses 4MB for physical extent, but it can be changed based on your need.
Now use the vgs and vgdisplay commands to get information about the Volume Group you created.
# vgs vg01 VG #PV #LV #SN Attr VSize VFree vg01 3 0 0 wz--n- 44.99g 44.99g # vgdisplay vg01 --- Volume group --- VG Name vg01 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 44.99 GiB PE Size 4.00 MiB Total PE 11511 Alloc PE / Size 0 / 0 Free PE / Size 11511 / 44.99 GiB VG UUID d17e3c31-e2c9-4f11-809c-94a549bc43b7
How to Create Logical Volume
How to Create Logical Volume Using GB Size
Logical Volume is the layer next to volume group in LVM structure. Logical volumes are nothing but block devices created from the volume group. It is a virtual disk partition and can be easily managed using LVM commands.
General syntax for creating logical volume (lvcreate)
lvcreate –L [Logical Volume Size] –n [Logical Volume Name] [Name of the Volume Group where the LV to be created]
Run the below mentioned command to create a logical volume lv001 of size 10 GB.
# lvcreate -n lv001 -L 10G vg01 Logical volume "lv001" created
Now use the lvs and lvdisplay commands to get information about the Logical Volumes you created.
# lvs /dev/vg01/lvol01 LV VG Attr LSize Pool Origin Data% Move Log Cpy% Sync Convert lv001 vg01 mwi-a-m-- 10.00g lv001_mlog 100.00 # lvdisplay /dev/vg01/lv001 --- Logical volume --- LV Path /dev/vg01/lv001 LV Name lv001 VG Name vg01 LV UUID ca307aa4-0866-49b1-8184-004025789e63 LV Write Access read/write LV Creation host, time localhost.localdomain, 2020-09-10 11:43:05 -0700 LV Status available # open 0 LV Size 10.00 GiB Current LE 2560 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:4
Creating Logical Volume Using PE (Physical Extent) Size
Logical Volumes can be created using Physical Extends (PE) size as well.
How can we calculate PE Value?
It’s very simple if you have a volume group of 10 GB, then what is the PE size?
By default, physical extent is of 4MB, but you can check the correct PE size by running the vgdisplay command, as this can be changed based on requirements.
10 GB = 10240MB / 4MB (PE Size) = 2560 PEs
General syntax for Logical Volume Creation with PE Size’s (lvcreate).
lvcreate –l [Physical Extends (PE) Size] –n [Logical Volume Name] [Name of the Volume Group where the LV to be created]
To create 10 GB of logical volume using the PE size the command would be:
# lvcreate -n lv001 -l 2560 vg01
How to Create File system
Logical volumes cannot be used until you create a valid file system on them.
General syntax to create a file system.
mkfs –t [File System Type] /dev/[Name of the Volume_Group]/[LV_name]
Use the below mentioned command to format the logical volume with the ext4 file system.
# mkfs -t ext4 /dev/vg01/lv001
For xfs file system.
# mkfs -t xfs /dev/vg01/lv001
Mounting Logical Volume
Finally, you have to mount the logical volume to use it. Make sure you add an entry into /etc/fstab file so that it loads automatically when the system boots.
Create a directory to mount the logical volume.
# mkdir /lvmtest
Next use the mount command to mount the logical volume.
# mount /dev/vg01/lv001 /lvmtest
Add new logical volume details in the /etc/fstab file to mount automatically when the system boots.
# vi /etc/fstab /dev/vg01/lv001 /lvmtest xfs defaults 0 0
Finally check the newly mounted volume,
# df -h /lvmtest Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg01-lv001 15360M 34M 15326M 4% /lvmtest
Hence, we learnt here How to Configure LVM (Logical Volume Management) in Linux. Please do send queries in below comment box.