How to create LVM(Logical Volume Manager) in Linux

Future Techno India
5 min readFeb 9, 2022

In Linux, logical volume management is provided by the Logical Volume Manager, a device mapper framework. Modern Linux distributions support LVM, allowing their root filesystem to be placed on a logical volume. It allows us to increase the size and the storage capabilities of two devices at once, which is the biggest advantage

What is Partition?

Using partitions, you can separate your hard drive into sections that can run independently. Storage devices (USB and hard drives) must be structured before being used in Linux. You can also use partitioning when installing multiple operating systems on the same computer.

What is Logical Volume?

In a non-LVM system, a Logical Volume is equivalent to a disk partition. Physical extents within a volume group are the basis for creating logical volumes. Lvcreate can be used to create a logical volume within an existing volume group.

What is Volume Group?

Logical Volume Manager (LVM) is structured around volume groups (VGs). Eventually, you create one storage structure based on the combined storage capacities of multiple physical volumes. We cannot create a Volume Group without a physical volume.

What is Physical Volume?

LVM allows you to create physical volumes, e.g. Hard Disk Drives (HDDs), Solid State Drives (SSDs), and partitions, that are configured as physical volumes initially. Creating Volume Groups and Logical Volumes is not possible without properly initialized physical volumes.

Future Tech India/ Logical Volume Manager

Shutdown your VM and increase the disk size

First, shut down your VM and increase the disk size. Here, I have extended the disk or added three new disks /dev/sdb /dev/sdc and /dev/sdd of size 15 GB. Then start your VM and go to the console.

We can see disks from below

#fdisk -l

Creating an LVM

Before creating LVM we can see new devices not mounted.

#df -Th

Creating a PV

Create a new physical disk using the command.

#pvcreate /dev/sdb (disk name)

Display physical volumes

Use the pvdisplay, pvs, and pvscan commands to display the PV we just created.

#pvdisplay [disk_name (optional)]

Creating Volume Group and Displaying the VG information

You can now create a new volume group using the three PVs you just created using the vgcreate command. You can specify the extension with the -s option and the maximum number of PVs and LVs in VG with the -p and -l options, respectively. There is no need to use any of these options.

#vgcreate [group_name][disk1][disk2]

#vgdisplay [group_name]

We can run the command vgs and vgdisplay to get more information about the VG we just created:

Creating Logical Volume

A logical volume can now be created using the lvcreate command in the VG.

The LV is assigned the name lvol# if no LV name is specified in the command.

Logical volumes are normally created on the PV on a next-free basis if you don’t specify the PV to span the LV.

To create a logical volume my_LVM of size 14 GB, follow these steps:

#lvcreate — size 14G — name [LV name][VG name]

Displaying the LV information

The lvdisplay, lvs, and lvscan commands can be used to display information about the LV we just created.

Creating File System

We now need to mount the new LV on a directory to be able to access it and store data. This step is the final step to creating a new file system. You can use mkfs to create a file system on top of the LV.

Create a mount point and mount lvm

Apart from that, it’s a relatively simple process. First, create a directory to serve as a mount point. Mount the partition manually to the mount point next.

# mkdir mount_point

#mount /dev/mapper/myvg_01-my_LVM mount_point

#df -Th

We can see in the above image that your logical volume has been mounted in one of the folder

Increase Size of Logical Volume

Add newly created pv named /dev/vde to an existing lv

We can see disks from below

#fdisk -l

Here new hard disk is attached /dev/sde

Type the following command to add a physical volume /dev/vde to “myvg_01” volume group:

#vgextend myvg_01 /dev/sde

Use the pvdisplay, pvs, and pvscan commands to display the PV we just created.

#vgdisplay myvg_01

you can see here that the size of vg is extend 14.99GB to 19.89GB

Finally, you need to expand /dev/myvg_01/my_LVM to make a total of 20GB (/dev/sde (5G)+ existing /dev/myvg_01/my_LVM (15G) Will happen.

#lvextend –size +5G /dev/myvg_01/my_LVM

If you run df -h or any other command, you will still see 14G in /dev/mapper/myvg_01-my_LVM . To enlarge the filesystem created inside the “root” volume you will need to run the following command:

#resize2fs /dev/mapper/myvg_01-my_LVM

Verify it :

#df -Th

The size is increase 14 GB to 19GB

--

--