What Is LVM?

LVM is a storage management technology that provides an easier way to manage disk space dynamically. It is very useful when you are running out of disk space on your server. You just need to attach another hard disk and extend the logical volume partition on the fly. If you are using more than one hard disk, LVM allows you to extend over more than one disk.

Unlike standard partitioning systems where the file system is created with fixed-size storage, LVM creates a file system with a flexible logical volume. So you can increase and decrease the partition size as per your needs.

The Advantages of LVM

Here are some advantages of LVM over standard partition:

It is flexible so you can reduce and extend the file system storage with just one command. It provides a snapshot feature to back up your logical volume by taking a snapshot. It allows you to change a failed hard disk without affecting the real data. You can increase the storage capacity by adding more disks and add them to the Volume Group. Offers some advanced features including, striping, and mirroring.

LVM is made from three components. A brief explanation of each is shown below:

Physical Volume (PV): This may be a single hard disk or partition of a disk. Volume Group (VG): A group of physical volumes. It combines all physical volumes into single storage pools. Logical Volume (LV): This is a portion or space of volume group, where you can create a file system. A volume group can be divided into multiple logical volumes. The space allocated to each logical volume can be increased and decreased depending on your needs.

How to Create an LVM

Before starting, be sure to have the following:

A Linux operating system installed on your machine. A root user account, or user with Sudo privileges.

You also need to add three additional hard disks to your system. For the demonstration purpose, use the following hard disks with 50GB in size:

/dev/sdb /dev/sdc /dev/sdd

Run the following command to see all attached hard disks:

You should see all additional disks in the below image:

Install LVM

By default, LVM does not comes pre-installed on all operating systems. You will need to run the appropriate command to install LVM based on your operating system.

To install LVM on RHEL, CentOS or Fedora, run the following command:

To install LVM on Ubuntu, Debian, and Linux Mint, run the following command:

After installing LVM, you can proceed to the next step.

Create Partitions

First, you will need to create a partition on each hard disk using the fdisk utility.

Open your terminal and run the following command to create a partition on the first hard disk (/dev/sdb)

Follow the below steps to create a partition:

Type n to create a new partition. Type p to create a primary partition. Type the partition number that you want to create. Just hit Enter twice to use the full disk space. Type t to change the partition type. Use e to create an LVM partition. Type p to confirm the partition. Press w and hit Enter to write the changes.

You will need to repeat the above steps for the remaining two disks /dev/sdc and /dev/sdd.

Next, run the following command to verify all partitions.

You should see your newly created partitions:

Create Physical Volumes

Next, you will need to create physical volumes using all three hard disks. You can use the pvcreate command to do the same.

You should see the following output:

You can now verify the information about all the physical volumes using the following command:

You should see the information about all Physical Volumes on the following screen:

Create a Volume Group

Next, you will need to create a volume group using all three physical volumes. You can use the vgcreate command to create a volume group named vg01 on the physical volumes.

You should see the following output:

Next, verify the volume group.

Output:

Run this command to see more information about the volume groups.

You should see the detailed information of volume group on the following screen.

Create the Logical Volumes

At this point, you have a volume group named vg01 with 150GB in size. Next, create three logical volumes named lv01, lv02, and lv03 on volume group vg01.

First, run the following command to create a logical volume named lv01 with a size of 5000MB.

Next, create a logical volume named lv02 with a size of 5000MB.

Next, create a logical volume named lv03 with a size of 5000MB.

Next, run the lvs command to see the information of all logical volumes.

Create a File System and Mount Logical Volumes

At this point, all logical volumes are created. Now, you will need to create a file system on it in order to use it.

To create a file system on all logical volumes, run the following command:

Next, you will need to mount all logical volumes to the appropriate location.

First, create a mount point for each logical volume.

Next, mount each logical volume using the command below:

Next, run the following command to confirm the mount point:

Next, you will need to add the mount point entry to /etc/fstab file in order to mount all logical volumes permanently.

Edit the /etc/fstab file using the nano command:

Add the following lines:

Save and close the file then run the following command to apply the changes.

You should get the following output:

LVM Is Ready to Use

Here, you learned how to set up a flexible file system storage using Logical Volume Management (LVM). You can now extend and reduce the logical volumes, volume groups, add additional logical volumes, create a snapshot of logical volume and other useful stuff using LVM.