Disk Partitioning using Logical Volume Management (LVM) in Linux

multiple smaller disks pointing to one big disk

There are many methods used for volume management and disk partitioning, like parted or fdisk. We have already learnt how to partition disks using different methods. But these methods have limitation of their own, and lacks performance and management facilities like LVM.

Suppose I have 3 disks of 1 TB and I can make different partitions on these respectively. I have to take disk one by one, make required partitions on it. If I need more volume, I have to add a new disk to increase capacity.

But while using LVM, we can create a pool of storage of 3 TB combining those 3 disks together, logically, and then create partitions from that 3 TB. Even if I need more space, I can add some space to the pool, and then extend my partition to avail that space.

What is LVM

LVM stands for Logical Volume Management, and it is a system for managing logical volumes and filesystems. It was first written back in 1998.

In dynamic systems where drives and partitions are frequently moved or resized, LVM can be quite useful. While regular partitions can also be adjusted, LVM is far more flexible and offers additional features. LVM provides abstraction, better performance & maintainability between physical disks and filesystems on it. LVM also offers advanced features like snapshotting, striping, and mirroring.

LVM is a mature technology that is also highly stable, and it is supported by default by every Linux distribution.

In order to experiment with LVM, we must first understand the LVM architecture.

word image
LVM architecture

LVM architecture

LVM incorporates three types of objects:

  1. PV: Physical Volume
    The physical volume is the hard disk device itself like (/dev/sdb2, for example). This partition will be formatted and used for LVM creation. There can be many disks, and we will create a PV as per using the hard disks we need
  2. VG: Volume Group
    It is mainly a group or pool of PVs (like in a RAID-0). All logical and physical volumes are grouped together in a Volume Group
  3. LV: Logical Volume
    Logical Volumes are located in a VG. Here we will actually create a file system. After they are created, they can be mounted on the machine.

Install the LVM tools

In new systems, LVM is installed by default. But if it is not, you need to install the LVM tools.

Debian users:

sudo apt-get install lvm2

RHEL-based system users:

sudo yum install lvm2-cluster

Prepare a physical partition

Let us check our current block device list. We have two SATA disks, /sda and /sdb having 10G and 20G. We can create a physical volume using a full disk, or partition of a disk, or using both combined.

word image

First we will create a partition on /sda using fdisk with default configuration, occupying full disk space.

word image 1

Now using lsblk we can see the block devices list now.

lsbk

word image 2

Create a Physical Volume (PV)

We will now create our physical volume using both /dev/sda1 partition and /dev/sbd disk.

Command format for creating physical volume (PV) is:

pvcreate device name

Here our command will be

pvcreate /dev/sda1 /dev/sdb

word image 3

Let us understand the command. The first attribute /dev/sda1 designates partition 1 on storage disk a as a PV. The second attribute /dev/sdb sets the total capacity of storage disk b as a PV.

Now we can check the newly created PV using below command:

pvs

word image 4

We can check the capacity and additional information using the below command:

pvdisplay

word image 5

Create a Virtual Group (VG)

Once one or more of the disks are available to LVM as Physical Volumes, then this physical volume storage can be used to create Volume Groups (VGs).

There may be many VGs on a server. The disks or partitions can be members of more than one VG. But the PVs themselves may only be members of one VG.

Now we will create a new VG using the PV /dev/sdb.

The command syntax to creating a new VG is:

vgcreate [vg_name] [pv_members]

Let us give our VG name as myvg. So, our command will be

vgcreate myvg /dev/sdb

word image 6

For checking the VGs, the commands are similar to checking PVs. They are

vgs

or

vgscan [vg_name]

If we don’t give any name, vgs will display all the available VG information.

word image 7

We can check the capacity and additional information using the below command:

vgdisplay

word image 8

Create a Logical Volume (LV)

To create a logical volume from our newly created VG, the command format is:

lvcreate -L size -n lvname vgname

So, I want to now create a 5G logical volume, named lv-test, from VG myvg. The command will be

lvcreate -L 5G -n lv-test myvg

word image 9

Thus, our new 5G LV lv-test is ready. W can check the basic information of LVs using command

lvs

or

lvscan [lv_name]

If we don’t give any name, lvs will display all the available LV information.

word image 10

We can check the capacity and additional information using the below command:

lvdisplay

word image 11

Mount the Logical Volume permanently

I hope we already know the basic procedure of mounting and using a partition permanently. For LVs, the procedures are the same. We have to first format the LV as per our desired filesystem type.

Here I will format the filesystem xfs on our LV /dev/myvg/lv-test. The command for this is:

mkfs.xfs /dev/myvg/lv-test

word image 12

Then we create a mount point named lvm_test. To do this run:

mkdir lvm_test

word image 13

Now we mount the volume using the mount command.

mount /dev/myvg/lv-test lvm_test/

We can check if the mount is successful or not using command

df -hT

word image 14

And finally, we know we have to add an entry of this new filesystem in /etc/fstab to mount this permanently. For this we open the file using nano and then add the entry as per fstab format.

nano /etc/fstab

word image 15

Here we’ll add the entry of the LV in the following format:

/dev/myvg/lv-test /root/lvm_test/ xfs defaults 0 0

And then save the file using CTRL+X and then Y, if you’re using nano like I am:

word image 16

Now we can again check using command

df -hT

word image 17

We can also check the block device and their mount points now using command

lsblk

word image 18

Extend the Volume Group

We have created our VG myvg using only /dev/sdb having 20G. Now we can extend our VG space.

Here we will add another available PV /dev/sda1 to our existing VG:

The command format is

vgextend [vg_name] [pv_name]

So, our command will be:

vgextend myvg /dev/sda1

word image 19

Extend the Logical Volume

We can extend our created LVs as per our desired size, there will be no data loss for doing so.

If we want our lv to a particular size, the command format is

lvextend -L [desired_size] [vg_name]

As of now, we have a lv of 5G. Now I want this LV lvm-test to be of 9G. So, the command will be

lvextend -L 9G /dev/myvg/lvm-test

word image 20

Now to check, use:

df -hT

or

lsblk

word image 21

We can also add some space to our existing LVs. Like now I will add 1G space to our LV lvm-test and the command will be

lvextend -L +1G /dev/myvg/lvm-test

word image 22

Checking using lsblk:

lsbk

word image 23

Conclusion

In this article I want to give you a glimpse of LVM and how it works. We also saw how to add a filesystem, mount the partitions, and extend the logical volumes. If you have any feedback or questions then feel free to leave us a commend and we’ll get back to you as soon as we can.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Comments
Read More

Bash Comments

Comments are used in programming languages as well as in Bash script for writing descriptions. Often you want…