Logical Volume Management in Linux

Heshan Dharmasena
5 min readSep 20, 2022

--

What We’ll Cover

  • What is LVM ?
  • Why need LVMs ?
  • LVM Architecture
  • How to create LVM ?
  • Conclusion

What is LVM ?

In Linux, LVM stands for Logical Volume Manager. It is a system of managing logical volumes, or filesystems, that is much more advanced and flexible than the traditional method of partitioning a disk into one or more segments and formatting that partition with a filesystem.

In other words, LVM is a device mapper framework that provides logical volume management for the Linux kernel. Logical Volume management includes allocating disks, striping, mirroring and resizing logical volumes.

Why need LVMs ?

Main purpose of LVM is to create resizable partitions. Most of the times, when you create a partition in Linux its size can not be shrink or extend on the fly. That means your current partition can not be extend or shrink without delete it.

Eg: Let’s assume you have a 10GB partition mounted for /var/log. All your rsyslog logs are logging to /var/log directory. However due to increase of logs content your 10GB is going to exceed soon. Now what are the options available for you? There are 2 options.

  1. You have to delete or archive older logs to keep free some space.

2. You have to move logs to another partition (mount point) and then delete current logs

Option 1, is okay for a sudden increase but not for regular solution. Also it may loose necessary logs too. What is you want to keep all the older logs in quick readable way, then this solution might be a big problem.

Option 2 is okay, if you could able to find another partition which has same free space. And then you have delete original files. What if the log file growth is faster than coping and deletion. Then again that might be a big problem.

What if you could able to extend the partition of 10GB into 15GB without coping, moving, deleting logs ? Seems like interesting idea right !

Yes, this is where LVM came into the picture. If you use LVM concepts to create /var/log partition, you can easily extend the size of partition on the fly.

Other purposes:

  • increased abstraction, flexibility, ease of control, snapshotting, striping, and mirroring.

LVM Architecture

In General we follow,

Partition -> Format -> Mount

stages for storage management in Linux.

But when it comes to LVM;

Partition -> LVM Architecture (PV -> VG -> LV) -> Format -> Mount.

Before learn about LVM creation it is important to learn about special terms.

  • PV (Physical Volume) — Physical Volumes correspond to disks; they are block devices (initial partition created from a hard disk) that provide the space to store logical volume via volume groups. Physical Volume is must to create Volume Group. There can be many Physical Volumes attached to a single Volume Group or Different Volume Groups.
2 PVs created, but only 1 PV attached to VG and VG attached to LV
  • VG (Volume Group) — Volume Group is a collection of physical volumes and logical volumes. Volume Group can be extended or reduced by controlling Physical Volumes. Extending or Reducing volume groups effect to the attached LVM of that volume group. When creating VG you should pay attention on PE (physical extend) size. By default it is 4MB (2²). It can be customized to any 2 to the power value (2^n) .

Below figure represent 2 PVs (/dev/vda1 and /dev/vda2) created and attached to 2 separate VGs. And each VG attached to LVs. Then LV is formatted and mounted to a filesystem.

2 PVs created, and both attached to different VGs and VGs attached to LVs

Below figure represent 2 PVs (/dev/vda1 and /dev/vda2) created and attached toa one VG. And the VG attached to LVs. Then LVs are formatted and mounted to a filesystem.

2 PVs created, and both attached to same VG and VG attached to LVs
  • LVM (Logical Volume Manager) — Logical volumes correspond to partitions: they hold a filesystem. Once you created Logical Volumes only you would be able to Format and Mount. Usually logical volume device path is the important factor when formatting and mounting. It goes and /dev/VG-NAME/LV-NAME (or /dev/mapper/VG-NAME-LV-NAME). They can span across multiple disks, and do not have to be physically contiguous. When creating a LV you can decide the size from Capacity or from PE (Physical Extends).
https://www.redhat.com/sysadmin/lvm-vs-partitioning

How to create a LVM ?

First create partition from a disk and enable LVM Flag

#fdisk /dev/vda   OR  #gdisk /dev/vda  OR  #parted /dev/vda
#lsblk

Create a PV from partition /dev/vda1

#pvcreate /dev/vda1
#pvs OR #pvdisplay

Create a VG called myvg from /dev/vda1 PV

#vgcreate myvg /dev/vda1   OR  #vgcreate -s 8MB myvg /dev/vda1
#vgs OR #vgdisplay

Create a LV called mylv from myvg VG

#lvcreate -n mylv -L 800MB myvg  OR  #lvcreate -n mylv -l 100 myvg
#lvs OR #lvdisplay

Above creates 800MB LVM (-L represents total end size and -l represent how many PEs needed). 100 x 8MB = 800MB

Format mylv (/dev/myvg/mylv)

#mkfs -t xfs /dev/myvg/mylv
#blkid

Mount to /mnt

#mount /dev/myvg/mylv /mnt
#df -h
LVM Architecture

VGs and LVs can be extend or shrink any given time. Each time you made changes to LVs, you need to update mount point by executing resize2fs or xfs_growfs commands.

Above detailed diagram shows some sample command for LVM management. You can visit man (manual) pages of each commands for further knowledge.

Conclusion

Hope you would have fairly good understanding of the various components that LVM. This basic understanding of LVM concepts architecture and few basic commands helps your knowledge.

Please comment if there is anything needed to be changed or corrected. Thanks for reading !

--

--

Heshan Dharmasena

Passionate on Linux | DevOps | Cloud | Automation | Platform Engineering | Red Hat Certified Architect | Technical Trainer | Organizer DevOps Sri Lanka