Logical Volume Manager

Jagadish Gowda P
5 min readNov 18, 2020

--

What is LVM?

LVM is a tool for logical volume management which includes allocating disks, striping, mirroring and resizing logical volumes.

With LVM, a hard drive or set of hard drives is allocated to one or more physical volumes. LVM physical volumes can be placed on other block devices which might span two or more disks.

The physical volumes are combined into logical volumes, with the exception of the /boot partition. The /boot partition cannot be on a logical volume group because the boot loader cannot read it. If the root (/) partition is on a logical volume, create a separate /boot partition which is not a part of a volume group.

Since a physical volume cannot span over multiple drives, to span over more than one drive, create one or more physical volumes per drive.

Figure 11.1. Logical Volumes

The volume groups can be divided into logical volumes, which are assigned mount points, such as /home and / and file system types, such as ext2 or ext3. When "partitions" reach their full capacity, free space from the volume group can be added to the logical volume to increase the size of the partition. When a new hard drive is added to the system, it can be added to the volume group, and partitions that are logical volumes can be increased in size.

Logical Volumes

On the other hand, if a system is partitioned with the ext3 file system, the hard drive is divided into partitions of defined sizes. If a partition becomes full, it is not easy to expand the size of the partition. Even if the partition is moved to another hard drive, the original hard drive space has to be reallocated as a different partition or not used.

To create a LVM in linux we should follow these steps:

  1. Add Physical Disk
  2. Partition the disk
  3. Then create Physical Volume using a Partition Disk
  4. create volume Group
  5. last step, Create the logical Volume

In the Below we can see that 2 new hard disks are added and they are partitioned using commands: fdisk <disk_name>

After Partition completion then we have to convert partition into Physical Volume(pv).

commands:

pvcreate <part._disk>

pvdisplay <part._disk>

To group all the Physical Volume we will create a Volume Group

vgcreate <vg_name> <pv1> <pv2> …

vgdisplay <vg_name>

For Volume Group we can create any number of partitions and here there will be no primary or extended Partition

Now let us create a Partition:-

lvcreate — size <size> — name <lv_name> <vg_name>

lvdisplay vg_name/lv_name

as we know that after creation of any partition we must format the partition here we ext4 format.

now lets mount this logical volume to any directory then we can start store the data in that Directory

LVM is dynamic so here without unmounting we can increase the size of Partition

now let’s see how to increase the size of the partition

we have 15 GB of Partition let’s try to increase this to 17 GB to this we have a command

lvextend — size <+size> /dev/vg_name/lv_name

As we know that mkfs.ext4 this will create a new partition table(Inode table) while creating new table we will loose old data.

But our requirement is to make only partition of2Gb to do this we have a command resize2fs /dev/<vg_name>/<lv_name> when we use this we will not loose any old data.

now we understood that for increasing size we must perform 2 steps:

  1. lvextend — size <+size> /dev/vg_name/lv_name
  2. resize2fs /dev/<vg_name>/<lv_name>

For Decreasing the size we must follow 5 steps:

  1. unmount the Directory because if any end user using the data from this logical volume when decrease the logical volume size they may loose some data and and their services may hang-up so better unmount the directory.
  2. clean/scan the partition-> e2fsck /dev/vg_name/lv_name it will remove the unwanted garbage links from Inode table
  3. Format the Inode table it will alter the partition table resize2fs /dev/<vg_name>/<lv_name.>
  4. lvreduce— size <-size> /dev/vg_name/lv_name here we reduce the physical space
  5. mount /dev/vg_name/lv_name

here we can see that size of the partition is decreased.

--

--