Logical Volume Manager(LVM)


LVM simply stands for “Logical Volume Manager”. It is a tool for logical volume management. LVM is an advanced filesystem management concept in the Unix based systems. LVM provides much more flexibility than the traditional way of partitioning and using a disk. This technology is widely used for the servers.

In this article, we will see the basic terminology related to LVM and how you can use it in your system.

LVM Functionality-

There are three basic layers that LVM used to abstract physical storage devices that are given below-

  • Physical volumes
  • Volume groups
  • Logical volumes

Physical Volume –

Physical volumes correspond to disks(for example devices created by device-mapper like RAID) or you can say these are the regular storage devices that provide the space to store the logical volumes.

Volume Group –

LVM combines the physical volumes into volume groups. Generally, a system needs one volume group to contain all the physical volumes on it.

Logical Volume

A group can be divided into multiple logical volumes. The functionality of logical volume is similar to a partition on a hard disk but it provides more flexibility. Later these volumes are mounted with a filesystem. Unlike a partition on a hard drive, the logical volume does not have to be physically contiguous.

The Extents

Each volume of a volume group is segmented into a small fixed-size chunk which is known as extents. The extent on a physical volume is known as the physical extent and the extents on a logical volume are known as a logical extent. When LVM allocates disk space to a logical volume it automatically creates a mapping of logical to physical extents.

How To Start Using LVM

Now you have a basic understanding of the LVM concept and terminology.  We can now proceed to use it in our systems. Generally, it comes preinstalled in most of the Linux distribution but still, if it is not in your system you can download it by using the following command in your terminal-

sudo apt-get install lvm2 (for debian based systems)
sudo yum install lvm2* (in systems that use an rpm package manager like CentOS)
Once it gets installed, use the following step to create a logical volume and used it-

1. Scan to list the available disks and partitions –

Use the given command to scan the available device that LVM can interact with-
sudo lvmdiskscan

2. Create the physical volumes –

Identify the disks that you want to use, mark them as physical volumes within the LVM by using the given command but before that please confirm that you don’t have any important data on the device otherwise you may lose it. I will suggest taking the backup of that device then proceed with the given steps –
sudo pvcreate /dev/sda5

You can see here, I choose /dev/sda5 to mark as a physical volume. You can select multiple devices also including external ones. You can check if physical volumes are gets registered with LVM or not by using the given command-
sudo pvs

You can see the listed physical volumes under the PV column. In my system, I have /dev/sda5 as a physical volume.

3. Create volume groups –

Now the next step is to add the physical volumes to a volume group. Use the following command to create and add the physical volumes to the created group-
sudo vgcreate vg0 /dev/sda5

now you can see a volume group with the name vg0 has been created and physical volume /dev/sda5 has been added to it. To check the created groups you can use sudo vgs the volume groups will be listed under the VG column.

4. Create logical volumes –

Create logical volumes from the volume groups. We have a volume group we can use it to allocate the space to logical volumes. We will create three volumes Vol1 of size 50G vol2 of size 5G vol3 will be created on the remaining space in the group.

Now, look at the creation of vol3 on the remaining space of volume group vg0. The flag  -l work in extents. The remaining space of the group can be allocated by using 100%FREE instead of using a particular size for a volume.

Again execute the lvmdiskscan command to see the available volumes. /dev/vg0/vol1, /dev/vg0/vol2 and /dev/vg0/vol3 are logical volumes and /dev/sda5 is an LVM physical volume on which these logical volumes are created.

Or use lvdisplay to list the logical volumes with their description. Execute sudo lvdisplay -v /dev/vg0/vol1 for a specific volume-

5. Format and mount the created logical volumes –

Now the last step is to format and mount the volumes. Use sudo mkfs.ext4 /dev/vg0/vol1 in terminal to format the vol1. Similarly, apply this command to other volumes i.e vol2 and vol3.

Create the mount points by using sudo mkdir -p /mnt/{vol1,vol2,vol3}. And then mount the logical volumes to an appropriate location. by using the given commands-

sudo mount /dev/vg0/vol1 /mnt/vol1
sudo mount /dev/vg0/vol2 /mnt/vol2
sudo mount /dev/vg0/vol3 /mnt/vol3

Now the operating system should mount the logical volumes automatically on the next boot of the system. Some other operations that can be performed in lvm are-

Resizing Logical Volumes –

A logical volume can be resized by using the command sudo lvextend -L +5G vg0/vol1. After extending the logical volume you should expand the filesystem to use space. Use the command sudo resize2fs /dev/vg0/vol1 to resize the filesystem.

Moving Logical Volume

If you have only one physical volume then there is no need to move but if later you add a new disk then you might want to. Suppose we want vol2 to move from a physical volume  /dev/sda5 then we have to use the following command-

sudo pvmove -n vol2 /dev/sda5

Snapshots

Creating a snapshot means you are cloning a logical volume which acts like original ones. Initial it does not take any space but as changes are made to the original volume the changed blocks are copied to the snapshot that means if you make a change to the origin that will also get reflected in the snapshot and more space needed to snapshots. You can use the following command to create the snapshot of a volume –

sudo lvcreate -s -n snap -L 5G vg0/vol1

This command will create a snapshot named snap of vol1 of vg0 with a space of 5GB. A snapshot volume only stores the changes that are made in the original volume so it requires much lesser space than the original volume.

Now I think you have a basic understanding of the LVM and its uses. but if still you have a query regarding this topic you can write to us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.