Do you want to create many virtual storage volumes on top of a single storage device for easier and flexible administration? Here's how to set up flexible file system storage with Logical Volume Management (LVM).

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:

        fdisk -l
    

You should see all additional disks in the below image:

list all hard disks

Related: Control Linux Disk Partitions With fdisk Commands

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:

        dnf install lvm2 -y
    

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

        apt-get install lvm2 -y
    

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)

        fdisk /dev/sdb
    
create partition

Follow the below steps to create a partition:

  1. Type n to create a new partition.
  2. Type p to create a primary partition.
  3. Type the partition number that you want to create.
  4. Just hit Enter twice to use the full disk space.
  5. Type t to change the partition type.
  6. Use e to create an LVM partition.
  7. Type p to confirm the partition.
  8. 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.

        fdisk -l
    

You should see your newly created partitions:

verify all 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.

        pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1
    

You should see the following output:

          Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc1" successfully created.
  Physical volume "/dev/sdd1" successfully created.

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

        pvs
    

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

Verify physical volumes

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.

        vgcreate vg01 /dev/sdb1 /dev/sdc1 /dev/sdd1
    

You should see the following output:

        Volume group "vg01" successfully created
    

Next, verify the volume group.

        vgs
    

Output:

          VG   #PV #LV #SN Attr   VSize    VFree   
  vg01 3 0 0 wz--n-

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

        vgdisplay vg01
    

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

verify volume groups

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.

        lvcreate -L 5000 -n lv01 vg01
    

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

        lvcreate -L 5000 -n lv02 vg01
    

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

        lvcreate -L 5000 -n lv03 vg01
    
create logical volumes

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

        lvs
    
logical volumes information

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:

        mkfs.ext4 -m 0 /dev/vg01/lv01
mkfs.ext4 -m 0 /dev/vg01/lv02
mkfs.ext4 -m 0 /dev/vg01/lv03
create file system on logical volumes

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

First, create a mount point for each logical volume.

        mkdir /mount1
mkdir /mount2
mkdir /mount3

Next, mount each logical volume using the command below:

        mount /dev/vg01/lv01 /mount1
mount /dev/vg01/lv02 /mount2
mount /dev/vg01/lv03 /mount3

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

        df -h
    
verify mount points

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:

        nano /etc/fstab
    

Add the following lines:

        /dev/vg01/lv01 /mount1 ext4    defaults 0 0
/dev/vg01/lv02 /mount2 ext4 defaults 0 0
/dev/vg01/lv03 /mount3 ext4 defaults 0 0
Edit fstab file

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

        mount -av
    

You should get the following output:

        /                        : ignored
/mount1 : successfully mounted
/mount2 : successfully mounted
/mount3 : successfully mounted

Related: How to Mount a Hard Drive in Linux Using the Command Line

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.