Logical Volume Management (LVM) is a popular method of managing storage on Linux operating systems. It allows administrators to create and manage logical volumes, which can span multiple physical disks and can be resized on-the-fly. In this blog post, we will walk through the process of configuring LVM on Linux.

LVM Series

Install LVM Tools

The first step is to make sure that the LVM tools are installed on your Linux system. Most Linux distributions come with the LVM tools pre-installed, but you can make sure of it by running the following command:

sudo apt-get install lvm2    # for debian-based distros
sudo yum install lvm2        # for RedHat-based distros

Now that you have it installed let's start creating multiple PVs and then add them to a VG.

Create Physical Volumes

LVM operates on physical volumes (PVs), which can be disks or disk partitions. Before creating logical volumes, you need to create one or more physical volumes. For example, if you have a new disk attached to your system, you can create a physical volume on it using the following command:

sudo pvcreate /dev/sdb

This command creates a physical volume on the /dev/sdb device. By the same command we will create the second Physical Volume on /dev/sdc.

sudo pvcreate /dev/sdc

Create Volume Groups

A volume group (VG) is a collection of one or more physical volumes. You can create a volume group using the following command:

sudo vgcreate vg1 /dev/sdb /dev/sdc

This command creates a volume group named vg1 that contains the physical volumes /dev/sdb and /dev/sdc. And you can now see your volume group by vgs or vgdisplay command:

sudo vgdisplay vg1

Resizing Volume Groups

If you want to add a new physical volume to a volume group you may extend it by using the following command:

sudo vgextend vg1 /dev/sdd

You can also remove a physical volume from volume group with vgreduce command:

sudo vgreduce vg1 /dev/sdd

Create Logical Volumes

A logical volume (LV) is a virtual partition that resides within a volume group. You can create a logical volume using the following command:

sudo lvcreate -L 10G -n lv1 vg1

This command creates a logical volume named lv1 that is 10GB in size and resides within the volume group vg1.

Format and Mount Logical Volumes

Once you have created a logical volume, you need to format it with a file system and mount it. You can format a logical volume with a file system using the following command:

sudo mkfs.ext4 /dev/vg1/lv1

This command formats the logical volume lv1, which resides in the volume group vg1, with the ext4 file system.

You can mount the logical volume to a directory like this:

sudo mount /dev/vg1/lv1 /mnt/lv1

This command mounts the logical volume lv1, which resides in the volume group vg1, to the directory /mnt/lv1.

Resizing Logical Volumes

One of the benefits of using LVM is that you can resize logical volumes on-the-fly. To resize a logical volume, you can use the following command:

sudo lvresize -L +5G /dev/vg1/lv1

This command increases the size of the logical volume lv1 by 5GB.