Linux LVM (Logical Volume Manager): A Step by Step Guide

In this lesson we’re going to discuss something that’s really cool. It’s called Logical Volume Manager which most folks just call LVM.

Introduction

LVM provides an alternative that you can use when you’re partitioning the hard disk drives in your Linux system.

Basically, what it does is create an alternative to the traditional process of creating disk partitions and mounting them in the Linux file system instead of defining and creating partitions.

Instead what you do is define volume groups from all the various storage devices in your system.

Then from the volume group we allocate space to specific Logical Volumes which are managed by the Logical Volume Manager (LVM).

Then instead of mounting a partition in the file system and what you do is instead mount Logical Volumes at the various mount points in your file system.

The great thing about LVM is the fact that it provides you with a huge degree of flexibility when you’re allocating space on your system.

What is the use of LVM?

Let’s take a look at an example here.

LVM Flexibility
LVM Flexibility

On one of my Linux system I have two LVM volumes.

The first one is mounted in /opt where various applications are installed and the other one is mounted at /var where I store my Mail Queues, Log Files, Print Queues and so on.

Now because of the type of data that’s stored in /var it’s very possible that the Logical Volume that I have mounted in /var is going to start running out of disk space at some point.

LVM Flexibility
LVM Flexibility

Maybe I have a ton of users on the system they send a lot of email, they send a lot of print jobs, and my log files are growing.

So, the size required for this volume is going to increase over time. On the other hand, my /opt directory really doesn’t need a whole lot of space.

Once I’ve got my applications installed, I only need a little bit of space for a few minor changes. I don’t need a ton of space here.

What we can do using LVM is we can actually take some of the space those allocated to the Logical Volume mounted at /opt and move it to the Logical Volume mounted at /var.

And the whole process can be done seamlessly transparently while the system remains running and servicing users.

Now could you do this with traditional disk partitions?

Yes.

But would it be difficult?

Yes.

It’d be a pain in the neck because basically what I would have to do to reallocate space from one partition to another is back up both partitions and then cross my fingers say a prayer and resize the partitions and then restore the data from back up and hopefully none of the data would get mangled during the process.

In addition LVM also allows you to dynamically add space to this system for example suppose I have a Logical Volume and I have mounted it at the /home directory this is where all of my end users home directories reside.

LVM Flexibility
LVM Flexibility

For example, Let’s suppose that on my Linux system I have a Logical Volume defined with LVM and I’ve mounted it in the /home directory that means all of my end users home directories are stored inside of this Logical Volume.

And let’s suppose that we are now starting to run out of space this is a very common occurrence as users download and create very large files.

Now to add capacity to this logical volume all I have to do is install a new hard drive in the system and then allocate all the space on that hard drive to the Logical Volume mount of that /home.

And when I do, the size of that volume is instantly increased.

I didn’t have to back up the data, I didn’t have to resize partitions, I didn’t have to restore data from backup as I would have if I needed to increase the size of a traditional partition that would have been mounted at /home.

Structure of Logical Volume Manager (LVM)

Basically, LVM makes life a lot easier for the System Administrator. So, with this in mind Let’s take a look at the various components that are required to create these Logical Volumes we’ve been talking about.

Now understand that LVM creates a pool of storage space called a Volume Group.

From the space contained in the Volume Group we can allocate Logical Volumes. We define the volume and We assign them a certain amount of space.

So, the structure of LVM uses the components that you see here.

LVM Components

First of all, we have our Physical Volumes.

Now a Physical Volume could be one of two different things it could be a partition on a hard disk drive, or it could even be an entire hard disk drive, all the space on the drive either way works.

The key thing to remember is that the storage space available on these Physical Volumes is then assigned and pooled together into the Volume Group.

And this is where things get really cool with LVM because once we assign storage space to the Volume Group we are no longer concerned about Physical Partition or disk boundaries that’s all just available storage space.

Basically, the Volume Group consists of all the space available on these Physical Volumes group together.

The key thing to remember here is the fact that the storage space within the Volume Group can come from many different Physical Volumes on many different storage devices.

And what this means is you can add additional hard disks or additional partitions to the Volume Group whenever you decide you need additional storage space.

And then as we said earlier, we take the space in the Volume Group and we allocate it to individual Logical Volumes.

Now these Logical Volumes are really kind of analogous to Physical Partitions that you might be used to with either gdisk or MBR.

You can format a Logical Volume and add a file system to it, you can create a file system on logical volume such as ext3, ext4 or whatever it is you want to use.

And then you can then mount the Logical Volume in a directory in your file system and store data on it just like you would a Partition.

So the process for creating a Logical Volume is shown here.

The first thing you have to do is create your Physical Volumes and then you need to assign those physical volumes to a Volume Group and once you’ve done that you create your Logical Volumes out of the Volume Group.

Physical Volume

So, let’s look at the first step in the process where you define your LVM Physical Volumes.

Now it’s important at this point that you don’t confuse Physical Volumes with Logical Volumes.

The Physical Volumes are composed of the disk partitions or even the entire hard disk drives if you want to that we define as physical LVM volumes that are then allocated to a Volume Group.

Now if you decide to use an existing partition as a Physical Volume for your Volume Group then you need to set its Partition type to Linux LVM which I believe the type code is 8e.

Now I have tried it with just Standard Linux partitions type 83 and it works just fine.

Now in most of the time when I setup LVM I actually use partitions but if you want to you could use the entire hard disk itself without creating any partitions on it.

You can use the entire hard disk as a Physical Volume but beaware that if you decide to do this the hard disk cannot contain a partition table.

And if you’ve ever created a partition at all ever on that hard disk drive that it’s going to have a partition table even if there are no partitions in it.

So, if the disk you want to use already does have an existing partition table you need to clear it out and you can use the dd command as shown here to do just that.

In this example, we’re going to clear out the partition table for an MBR disk to do this we just have to overwrite the first 512KB on the hard disk drive.

Because this is where the Master Boot Record (MBR) resides.

So, we’re essentially overriding the first 512KB block on the hard disk drive with nothing.

# dd if=/dev/zero of=/dev/sdb bs=512 count=1
1+0 records in
1+0 records out
512 bytes copied, 0.000399503 s, 1.3 MB/s

Initialize Physical Volumes for use

Once you’ve decided which disks or partitions you want to use you then use the pvcreate command at the shell prompt to define these disks or partitions as Physical Volumes.

The syntax is really easy.

Just type pvcreate followed by the device file name of the device that you want to add.

In this case we are adding an entire hard disk drive.

First, I defines /dev/sdb as a Physical Volume, then I defines /dev/sdc as a Physical Volume and then defines /dev/sdd as a Physical Volume.

# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.
# pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created.

You can create Physical Volume’s by taking multiple hard disks simultaneously.

# pvcreate /dev/sdb /dev/sdc /dev/sdd
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.
  Physical volume "/dev/sdd" successfully created.

Display various attributes of Physical Volumes

Once you’ve done that you can use the pvscan -v command to view all the Physical Volumes that are currently defined on the system along with their size.

And here you can see that I have /dev/sdb, /dev/sdc and, /dev/sdd, it also shows their size over here.

# pvscan -v
  PV /dev/sdb                      lvm2 [3.00 GiB]
  PV /dev/sdc                      lvm2 [3.00 GiB]
  PV /dev/sdd                      lvm2 [3.00 GiB]
  Total: 3 [9.00 GiB] / in use: 0 [0   ] / in no VG: 3 [9.00 GiB]

Alternatively you can get the same output by running the following command.

# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb      lvm2 ---  3.00g 3.00g
  /dev/sdc      lvm2 ---  3.00g 3.00g
  /dev/sdd      lvm2 ---  3.00g 3.00g

You can also run the following command to get the more details information’s on Physical Volume.

# pvdisplay 
  "/dev/sdb" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               wqqKlZ-D23J-MkJq-0shI-6kCS-aJt4-Wz2mcc
   
  "/dev/sdc" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               95V3uT-eO2p-UwcG-NZEb-hkS7-Cr9X-D9tBAk
   
  "/dev/sdd" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               NDRylc-ollV-JFwi-lDES-Vv4B-jLTD-ujpAVX

Removing Physical Volumes

You can remove the Physical Volume anytime. To do this, run the following command.

# pvremove /dev/sdb /dev/sdc /dev/sdd
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.
  Labels on physical volume "/dev/sdd" successfully wiped.

Volume Group

So, once we’ve got our Physical Volumes defined, we then need to assign those Physical Volumes to the Volume Group.

This basically takes the space on those Physical Volumes and allocates it to the Volume Group that can then be later on assigned to a Logical Volume.

Creating a Volume Group

To create a Volume Group you use the vgcreate command.

Now the process for allocating Physical Volumes to a Volume Group is a little bit tricky if the Volume Group doesn’t already exist.

So, in order to create the Volume Group we first specify the vgcreate command followed by the name of the Volume Group we want to create and then the name of the Physical Volumes that we want to assign to that Volume Group.

So, in this case I’m creating a Volume Group named edu_vgroup and then I’m assigning the /dev/sdb and /dev/sdc Physical Volumes to that Volume Group.

# vgcreate edu_vgroup /dev/sdb /dev/sdc
  Volume group "edu_vgroup" successfully created

Display various attributes of Volumes Group

Run the pvscan -v command and then you can see that /dev/sdb, /dev/sdc all added to the same Volume Group called edu_vgroup.

# pvscan -v
  PV /dev/sdb   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdc   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdd                      lvm2 [3.00 GiB]
  Total: 3 [8.99 GiB] / in use: 2 [5.99 GiB] / in no VG: 1 [3.00 GiB]

Alternatively you can get the same output by running the following command.

# vgs
  VG         #PV #LV #SN Attr   VSize VFree
  edu_vgroup   2   0   0 wz--n- 5.99g 5.99g

You can also run the following command to get the more details information’s on Volume Group.

# vgdisplay 
  --- Volume group ---
  VG Name               edu_vgroup
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               5.99 GiB
  PE Size               4.00 MiB
  Total PE              1534
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1534 / 5.99 GiB
  VG UUID               Gav1FJ-mdeD-7rwH-0BCo-W2Yt-U6QR-4lZGDy

How to extend Volume Group

As you can see I have 3 Physical Volumes that is /dev/sdb, /dev/sdc, /dev/sdd out of which I have created a Volume Group using 2 Physical Volumes.

# pvscan -v
  PV /dev/sdb   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdc   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdd                      lvm2 [3.00 GiB]
  Total: 3 [8.99 GiB] / in use: 2 [5.99 GiB] / in no VG: 1 [3.00 GiB]

This means that I can extend my existing Volume Group using my third Physical Volume that is /dev/sdd.

And to extend any Volume Group we can use the vgextend command.

# vgextend edu_vgroup /dev/sdd 
  Volume group "edu_vgroup" successfully extended

And as you can see we have successfully extended the Volume Group.

# pvscan -v
  PV /dev/sdb   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdc   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdd   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  Total: 3 [<8.99 GiB] / in use: 3 [<8.99 GiB] / in no VG: 0 [0   ]

Removing Physical Volume from a Volume Group

If you want to reduce a physical volume from volume group then you can do this using vgreduce command.

For example, here I have a Volume Group called edu_vgroup with 3 Physical Volumes (/dev/sdb, /dev/sdc, /dev/sdd) added to it.

# pvs
  PV         VG         Fmt  Attr PSize  PFree 
  /dev/sdb   edu_vgroup lvm2 a--  <3.00g <3.00g
  /dev/sdc   edu_vgroup lvm2 a--  <3.00g <3.00g
  /dev/sdd   edu_vgroup lvm2 a--  <3.00g <3.00g

Right now I want to reduce one Physical Volume from that, Suppose I want to reduce the /dev/sdd Physical Volume.

To do this, run the following command.

# vgreduce edu_vgroup /dev/sdd
  Removed "/dev/sdd" from volume group "edu_vgroup"

And here is the result. As you can see, now we have 2 Physical Volumes in our Volume Group that is /dev/sdb and /dev/sdc.

# pvs
  PV         VG         Fmt  Attr PSize  PFree 
  /dev/sdb   edu_vgroup lvm2 a--  <3.00g <3.00g
  /dev/sdc   edu_vgroup lvm2 a--  <3.00g <3.00g
  /dev/sdd              lvm2 ---   3.00g  3.00g

Note: Before removing a Physical Volume from a Volume Group, you can make sure that the Physical Volume is not used by any Logical Volumes by using the pvdisplay command.

Splitting a Volume Group

In the current scenario we have a Volume Group called edu_vgroup to which 3 Physical Volumes are added that is /dev/sdb, /dev/sdc, /dev/sdd.

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree 
  edu_vgroup   3   0   0 wz--n- <8.99g <8.99g
# pvs
  PV         VG         Fmt  Attr PSize  PFree 
  /dev/sdb   edu_vgroup lvm2 a--  <3.00g <3.00g
  /dev/sdc   edu_vgroup lvm2 a--  <3.00g <3.00g
  /dev/sdd   edu_vgroup lvm2 a--  <3.00g <3.00g

For some reason I have to split the existing Volume Group and create a new Volume Group.

To do this, I can use the vgsplit command.

Here in this example I am creating a new Volume Group called split-vg.

Syntax: vgsplit [existing_volume_group_name] [new_volume_group_name] [physical_volume]

# vgsplit edu_vgroup split-vg /dev/sdd
  New volume group "split-vg" successfully split from "edu_vgroup"

As you can see, we have two volume groups right now that is edu_vgroup and split-vg.

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree 
  edu_vgroup   2   0   0 wz--n-  5.99g  5.99g
  split-vg     1   0   0 wz--n- <3.00g <3.00g

Let's take another example on this subject.

Suppose the Physical Volume you want to split from Volume Group has some used Physical Extents(PE), then what would you do?

In this example I want to split the /dev/sdb Physical Volume from the Volume Group.

As you can see, the total size of /dev/sdb Physical Volume is 3GB, out of which 1GB is utilized and 2GB free space is available.

# pvscan 
  PV /dev/sdb   VG edu_vgroup      lvm2 [<3.00 GiB / <2.00 GiB free]
  PV /dev/sdc   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdd   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  Total: 3 [<8.99 GiB] / in use: 3 [<8.99 GiB] / in no VG: 0 [0   ]

In that case, first you have to move those used Physical Extents(PE) into another Physical Volume within the same Volume Group.

You can do this by using the pvmove command.

So, I am moving all the used Physical Extents (PE) of /dev/sdb to /dev/sdc.

# pvmove /dev/sdb /dev/sdc
  /dev/sdb: Moved: 30.86%
  /dev/sdb: Moved: 100.00%

After moving the data, you can see that all of the space on /dev/sdb is free.

# pvscan 
  PV /dev/sdb   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdc   VG edu_vgroup      lvm2 [<3.00 GiB / <2.00 GiB free]
  PV /dev/sdd   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  Total: 3 [<8.99 GiB] / in use: 3 [<8.99 GiB] / in no VG: 0 [0   ]

Now you can safely split /dev/sdb from Volume Group.

# vgsplit edu_vgroup split-vg /dev/sdb
  New volume group "split-vg" successfully split from "edu_vgroup"

And here is the result.

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree 
  edu_vgroup   2   1   0 wz--n-  5.99g  4.99g
  split-vg     1   0   0 wz--n- <3.00g <3.00g

As you can see /dev/sdb is currently added to a Volume Group called split-vg.

# pvscan 
  PV /dev/sdc   VG edu_vgroup      lvm2 [<3.00 GiB / <2.00 GiB free]
  PV /dev/sdd   VG edu_vgroup      lvm2 [<3.00 GiB / <3.00 GiB free]
  PV /dev/sdb   VG split-vg        lvm2 [<3.00 GiB / <3.00 GiB free]
  Total: 3 [<8.99 GiB] / in use: 3 [<8.99 GiB] / in no VG: 0 [0   ]

Merging Volume Groups

You can merge two Volume Groups using the vgmerge command.

But if a Logical Volume is active in any of the Volume Groups, then you have to inactivate that Logical Volume before merging.

Type the following command to deactivate an individual Logical Volume.

# lvchange -an /dev/edu_vgroup/volgroup001

Here I have two Volume Groups that is edu_vgroup and split-vg.

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree 
  edu_vgroup   2   1   0 wz--n-  5.99g  4.99g
  split-vg     1   0   0 wz--n- <3.00g <3.00g

Run the following command to merge these Volume Groups.

# vgmerge edu_vgroup split-vg 
  Volume group "split-vg" successfully merged into "edu_vgroup"

Here is the output.

# pvs
  PV         VG         Fmt  Attr PSize  PFree 
  /dev/sdb   edu_vgroup lvm2 a--  <3.00g <3.00g
  /dev/sdc   edu_vgroup lvm2 a--  <3.00g <2.00g
  /dev/sdd   edu_vgroup lvm2 a--  <3.00g <3.00g

Now type the following command to activate an individual Logical Volume.

# lvchange -ay /dev/edu_vgroup/volgroup001

Renaming a Volume Group

Type the following command to rename an existing Volume Group.

In this example, I am renaming my existing Volume Group from edu_vgroup to new_vgroup.

Syntax: vgrename [existing_volume_group_name] [new_volume_group_name]

# vgrename edu_vgroup new_vgroup
  Volume group "edu_vgroup" successfully renamed to "new_vgroup"

Removing Volumes Group

You can remove the Volumes Group anytime. To do this, run the following command.

# vgremove edu_vgroup
  Volume group "edu_vgroup" successfully removed

Note: Before removing a Volume Group, confirm that no Logical Volume is configured in it. you can do this using lvdisplay command.

Logical Volume

Now once we have our Volume Group defined, we can then use the space that we've allocated to that Volume Group to define our Logical Volumes (LVS).

Creating a Logical Volume

To do this you use the lvcreate command, the syntax is shown here.

You enter lvcreate then use the -L option to specify the size of the Logical Volume that you want to create, then you use the -n option to specify the name of the Logical Volume and then at the end of the command you specify which Volume Group you want to steal that space from to create the Logical Volume.

# lvcreate -L 1G -n volgroup001 edu_vgroup
  Logical volume "volgroup001" created.

# lvcreate -L 1G -n volgroup002 edu_vgroup
  Logical volume "volgroup002" created.

So, in this case I am creating a 1 GB Logical Volume named volgroup001 and then the space is going to come from the edu_vgrouph Volume Group.

And I did the same thing a second time to create a Second Logical Volume within that same Volume Group named volgroup002.

This is really cool essentially what we just did is to find two Logical Volumes within a single Volume Group which itself was created by pooling together all of the storage space from the three hard disk drives.

Increase the size of a Logical Volume

The cool thing about it is if I ended up needing more space for any one of these volumes volgroup001 or volgroup002 I would have to do is install a new storage device in the system, define it as a Physical Volume allocate a space to the Volume Group and then run the lvextend command to increase the size of the volume.

I didn't have to back up any data, I didn't have to resize any partitions, I just dynamically add the new storage space in the Volume Group do the appropriate Logical Volume.

Let us increase the size of both these Logical Volumes.

First check how much free space is there in the Volume Group.

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree 
  edu_vgroup   3   2   0 wz--n- <8.99g <6.99g

And as you can see, 7GB of space is free.

The following command extends the logical volume /dev/edu_vgroup/volgroup001 to 1 GB.

# lvextend -L+1G /dev/edu_vgroup/volgroup001
  Size of logical volume edu_vgroup/volgroup001 changed from 1.00 GiB (256 extents) to 2.00 GiB (512 extents).
  Logical volume edu_vgroup/volgroup001 successfully resized.

After extending, type the following command resize the file system.

# resize2fs /dev/edu_vgroup/volgroup001

The following command extends the logical volume /dev/edu_vgroup/volgroup002 to 500 MB.

# lvextend -L+500M /dev/edu_vgroup/volgroup002
  Size of logical volume edu_vgroup/volgroup002 changed from 1.00 GiB (256 extents) to <1.49 GiB (381 extents).
  Logical volume edu_vgroup/volgroup002 successfully resized.

Again after extending, type the following command resize the file system.

# resize2fs /dev/edu_vgroup/volgroup002

You must have noticed that we are using the + sign to extend the size of the Logical Volume.

But if you do not use the + sign, then the command will set the same size that you have mentioned.

For example, I have a Logical Volume that is 1GB in size.

# lvs
  LV          VG         Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  volgroup001 edu_vgroup -wi-a----- 1.00g

Now I will set the size without using the + sign and let's see what happens.

# lvextend -L3G /dev/edu_vgroup/volgroup001

# lvs
  LV          VG         Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  volgroup001 edu_vgroup -wi-a----- 3.00g

And as you can see, the command set the size of the Logical Volume that we had mentioned.

Note: But ensure the new size should be larger than existing size.

Creating Logical Volume based on Percentage(%)

We can also allocate size to Logical Volumes based on percentage(%).

Let us first check how much free space is in the Volume Group.

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree 
  edu_vgroup   3   2   0 wz--n- <8.99g <6.99g

As you can see, there is about 7GB of free space in the Volume Group right now.

I want to create one more Logical Volume(LV) that occupy 100% of the free space. To do this type the following command.

# lvcreate -l 100%VG -n volgroup003 edu_vgroup
  Reducing 100%VG to remaining free space <6.99 GiB in VG.
  Logical volume "volgroup003" created.

Here is the Output:

# lvs
  LV          VG         Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  volgroup001 edu_vgroup -wi-a-----  1.00g                                                    
  volgroup002 edu_vgroup -wi-a-----  1.00g                                                    
  volgroup003 edu_vgroup -wi-a----- <6.99g

Alternatively you can get the same result by running the following command.

# lvcreate -l 100%FREE -n volgroup003 edu_vgroup

Now as you can see, the remaining free space in the Volume Group is now zero.

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree
  edu_vgroup   3   3   0 wz--n- <8.99g    0

Creating Logical Volumes using Physical Extents(PE)

First check the available free Physical Extents(PE) and for that you can use the vgdisplay command.

And as you can see 2301 Physical Extends (PE) are free.

# vgdisplay 
  --- Volume group ---
  VG Name               edu_vgroup
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  25
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <8.99 GiB
  PE Size               4.00 MiB
  Total PE              2301
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2301 / <8.99 GiB
  VG UUID               QA9RfB-BJSs-4S3S-dwxa-6hS2-qQea-eKlG8x

Type the following command to create a new Logical Volume using Physical Extents(PE).

Syntax: lvcreate -l [Extent Size] -n [name_of_logical_volume] [volume_group]

# lvcreate -l 1000 -n volgroup003 edu_vgroup
  Logical volume "volgroup003" created.

And as you can see our new Logical Volume has been assigned 1000 PE's.

# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/edu_vgroup/volgroup003
  LV Name                volgroup003
  VG Name                edu_vgroup
  LV UUID                exICDF-497z-Ga9i-FCFS-85Hb-Wsbt-SZwSMh
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-04-24 00:15:46 -0700
  LV Status              available
  # open                 0
  LV Size                <3.91 GiB
  Current LE             1000
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

And 1000 PE's are reduced from Volume Group.

# vgdisplay 
  --- Volume group ---
  VG Name               edu_vgroup
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  26
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <8.99 GiB
  PE Size               4.00 MiB
  Total PE              2301
  Alloc PE / Size       1000 / <3.91 GiB
  Free  PE / Size       1301 / 5.08 GiB
  VG UUID               QA9RfB-BJSs-4S3S-dwxa-6hS2-qQea-eKlG8x

Display various attributes of Logical Volume

You can use the following commands to check the attributes of Logical Volumes.

Command #1

# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/edu_vgroup/volgroup001
  LV Name                volgroup001
  VG Name                edu_vgroup
  LV UUID                oewjop-cp3S-4M2G-zzm6-t7pp-SQ8C-EKwVj5
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-04-23 03:11:59 -0700
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/edu_vgroup/volgroup002
  LV Name                volgroup002
  VG Name                edu_vgroup
  LV UUID                gBEX5S-xbfl-cwmG-3Fzc-j4aa-MUel-Zc2wiG
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-04-23 03:12:06 -0700
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/edu_vgroup/volgroup003
  LV Name                volgroup003
  VG Name                edu_vgroup
  LV UUID                y8ybEq-rVWc-l8GV-bMRB-roAO-EjpT-hxhHcG
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-04-23 03:41:25 -0700
  LV Status              available
  # open                 0
  LV Size                <6.99 GiB
  Current LE             1789
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

Command #2

# lvs
  LV          VG         Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  volgroup001 edu_vgroup -wi-a-----  1.00g                                                    
  volgroup002 edu_vgroup -wi-a-----  1.00g                                                    
  volgroup003 edu_vgroup -wi-a----- <6.99g

Command #3

# lvscan 
  ACTIVE            '/dev/edu_vgroup/volgroup001' [1.00 GiB] inherit
  ACTIVE            '/dev/edu_vgroup/volgroup002' [1.00 GiB] inherit
  ACTIVE            '/dev/edu_vgroup/volgroup003' [<6.99 GiB] inherit

Activate and Deactivate an Logical Volume

Logical volumes can be activated or deactivated. To do this you can use the lvchange command.

You can use this feature in different circumstances.

As you can see, both Logical Volumes are currently active.

# lvscan 
  ACTIVE            '/dev/edu_vgroup/volgroup001' [2.00 GiB] inherit
  ACTIVE            '/dev/edu_vgroup/volgroup002' [<1.49 GiB] inherit

Activate and Deactivate individual Logical Volumes:

Run the following command to deactivate an individual Logical Volume.

# lvchange -an /dev/edu_vgroup/volgroup001
# lvchange -an /dev/edu_vgroup/volgroup002

Output:

# lvscan 
  inactive          '/dev/edu_vgroup/volgroup001' [2.00 GiB] inherit
  inactive          '/dev/edu_vgroup/volgroup002' [<1.49 GiB] inherit

Type the following command to activate an individual Logical Volume.

# lvchange -ay /dev/edu_vgroup/volgroup001
# lvchange -ay /dev/edu_vgroup/volgroup002

Output:

# lvscan 
  ACTIVE            '/dev/edu_vgroup/volgroup001' [2.00 GiB] inherit
  ACTIVE            '/dev/edu_vgroup/volgroup002' [<1.49 GiB] inherit

Activate and Deactivate all Logical Volumes:

Type the following command to deactivate all of the Logical Volumes in a Volume Group.

# vgchange -an edu_vgroup 
  0 logical volume(s) in volume group "edu_vgroup" now active

Run the following command to activate all of the Logical Volumes in a Volume Group.

# vgchange -ay edu_vgroup 
  2 logical volume(s) in volume group "edu_vgroup" now active

Modifying permissions of Logical Volume

When we create a new Logical Volume, the read and write permissions are set by default.

# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/edu_vgroup/volgroup003
  LV Name                volgroup003
  VG Name                edu_vgroup
  LV UUID                exICDF-497z-Ga9i-FCFS-85Hb-Wsbt-SZwSMh
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-04-24 00:15:46 -0700
  LV Status              available
  # open                 0
  LV Size                1.95 GiB
  Current LE             500
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

But these default permissions can be modified and for that you can use the lvchange command.

I allow this Logical Volume to be read-only and Here is the command for that.

# lvchange -pr /dev/edu_vgroup/volgroup003

Output:

# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/edu_vgroup/volgroup003
  LV Name                volgroup003
  VG Name                edu_vgroup
  LV UUID                exICDF-497z-Ga9i-FCFS-85Hb-Wsbt-SZwSMh
  LV Write Access        read only
  LV Creation host, time localhost.localdomain, 2021-04-24 00:15:46 -0700
  LV Status              available
  # open                 0
  LV Size                1.95 GiB
  Current LE             500
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

Resizing Logical Volumes

You can resize an existing Logical Volume and for that you can use the lvreduce command.

Here in this example I am reducing 500 Logical Extents(LE) from a Logical Volume called volgroup003.

Syntax: lvreduce -l [-logical_extent_size] [logical_volume_path]

# lvreduce -l -500 /dev/edu_vgroup/volgroup003
  WARNING: Reducing active logical volume to 1.95 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce edu_vgroup/volgroup003? [y/n]: y
  Size of logical volume edu_vgroup/volgroup003 changed from <3.91 GiB (1000 extents) to 1.95 GiB (500 extents).
  Logical volume edu_vgroup/volgroup003 successfully resized.

Renaming Logical Volumes

To rename an existing Logical Volume, you can use the lvrename command.

In this example, I am renaming my existing Logical Volume from volgroup001 to newvolgroup001.

# lvrename /dev/edu_vgroup/volgroup001 /dev/edu_vgroup/newvolgroup001
  Renamed "volgroup001" to "newvolgroup001" in volume group "edu_vgroup"

Output:

# lvs
  LV             VG         Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  newvolgroup001 edu_vgroup -wi-a-----  2.00g                                                    
  volgroup002    edu_vgroup -wi-a----- <1.49g

Removing Logical Volumes

To remove an inactive Logical Volume, you can use the lvremove command.

In this example I am removing a logical volume named volgroup001.

# lvremove /dev/edu_vgroup/volgroup001
Do you really want to remove active logical volume edu_vgroup/volgroup001? [y/n]: y
  Logical volume "volgroup001" successfully removed

Note: If the Logical Volume is currently mounted, you must close the volume with the umount command before removing it. In addition, in a clustered environment you must deactivate a logical volume before it can be removed.

Create file system on Logical Volume

So, once we have the Logical Volumes created, we then need to treat them basically like we would an MBR or GPT partition.

Here I have two Logical Volumes that is volgroup001 and volgroup002.

In order to store data on an MBR/GPT partition I have to create a file system on it and then mount it in the file system and the same holds true for Logical Volumes.

The first thing I need to do is create a file system on it just like it would a partition and use the exact same command to do so.

I enter mke2fs -t followed by the type of file system that I want to create and then instead of specifying a partition that I want to create the file system on I instead specified the device file for the Logical Volume which uses a slightly different syntax than a partition.

We enter /dev/ followed by the Volume Group that the Logical Volume resides in and then the name of the Logical Volume itself.

Syntax: /dev/name_of_volume_group/logical_volume

# mke2fs -t ext4 /dev/edu_vgroup/volgroup001

# mke2fs -t ext4 /dev/edu_vgroup/volgroup002

Mount Logical Volumes

Once the file system is created on the Logical Volume you can mount it in the file system.

Temporarily mount the File System

Use the mount command just like I would with a MBR or GPT disk partition and I specify -t followed by the type of file system that I just created on the Logical Volume and then once again I specify the device file for the Logical Volume using the exact same syntax (/dev/edu_vgroup/volgroup002) that I did before and then I end the command with where I want to mount it in the file system.

# mount -t ext4 /dev/edu_vgroup/volgroup001 /volgroup001/
# mount -t ext4 /dev/edu_vgroup/volgroup002 /volgroup002/

Permanently mount the File System

To mount Logical Volumes permanently, you have to enter them in the /etc/fstab file.

# /etc/fstab
# Created by anaconda on Sun Jan 24 10:20:39 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=b119d862-07b7-4842-a6ea-cd2da64f8eae /                       xfs     defaults        0 0
UUID=e9f23f2d-118e-4a5e-94ab-b08afb78a3be /boot                   ext4    defaults        1 2
UUID=d3e3cc9a-6c24-4d58-9d33-4aeaa2eef5d1 swap                    swap    defaults        0 0
/dev/edu_vgroup/volgroup001	/volgroup001	ext4	defaults        0 0

Run the following command to check if it is successfully mounted.

# df -h | grep -i volgroup001
/dev/mapper/edu_vgroup-volgroup001  976M  2.6M  907M   1% /volgroup001

Suggested Read: How to Mount and Unmount File Systems in Linux

Configure LVM using partitions

So far we have seen How we can configure LVM using hard disks.

But as I mentioned, LVM can also be configured using partitions. Here I am going to tell you step by step process.

I have 3 disk partitions that is /dev/sdb1, /dev/sdb2, /dev/sdb3.

# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 3 GiB, 3221225472 bytes, 6291456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x1340475a

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdb1          2048 1026047 1024000  500M 83 Linux
/dev/sdb2       1026048 2050047 1024000  500M 83 Linux
/dev/sdb3       2050048 3074047 1024000  500M 83 Linux

Step #1 Change the Partition Type

First of all change the Partition type of all the three Partitions.

The standard partition code is 83, by changing it we need to set LVM partition code that is 8e.

Following steps explains How to change Partition type.

# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): t
Partition number (1-3, default 3): 2
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Note: t option is used to change the partition type.

As you can see all the three partitions are currently ready for LVM configuration.

# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 3 GiB, 3221225472 bytes, 6291456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x1340475a

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdb1          2048 1026047 1024000  500M 8e Linux LVM
/dev/sdb2       1026048 2050047 1024000  500M 8e Linux LVM
/dev/sdb3       2050048 3074047 1024000  500M 8e Linux LVM

Suggested Read:

Step #2 Create Physical Volumes

Type the following command to create Physical Volumes.

# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.
  Physical volume "/dev/sdb3" successfully created.

To check various attributes of Physical Volume use the pvdisplay command.

# pvdisplay 
  "/dev/sdb1" is a new physical volume of "500.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               500.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               7voXTm-FEl3-ntLN-1W4R-NsWU-DX0b-S3Sr13
   
  "/dev/sdb2" is a new physical volume of "500.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               500.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               tPf9oI-5ZCp-2Gdk-eNmL-f3Z2-7PNZ-l9fvcT
   
  "/dev/sdb3" is a new physical volume of "500.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               500.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               jIsed8-5aBX-IY9D-ujCS-cn00-6DQA-ZVSg9C

Step #3 Create Volume Group

To create a Volume Group use the vgcreate command.

# vgcreate new_vgroup /dev/sdb1 /dev/sdb2 /dev/sdb3
  Volume group "new_vgroup" successfully created

To check various attributes of Volume Group use the vgdisplay command.

# vgdisplay 
  --- Volume group ---
  VG Name               new_vgroup
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               1.45 GiB
  PE Size               4.00 MiB
  Total PE              372
  Alloc PE / Size       0 / 0   
  Free  PE / Size       372 / 1.45 GiB
  VG UUID               PbuShG-XBNb-7Iu0-Uzd7-JBQO-TdXO-RcgplZ

Step #4 Create Logical Volumes

Logical Volumes can be created using lvcreate command.

# lvcreate -L 500M -n newlv001 new_vgroup
  Logical volume "newlv001" created.

# lvcreate -L 500M -n newlv002 new_vgroup
  Logical volume "newlv002" created.

To check various attributes of Logical Volume use the lvdisplay command.

# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/new_vgroup/newlv001
  LV Name                newlv001
  VG Name                new_vgroup
  LV UUID                e3SdGE-nL2T-xBm0-N5dO-bE0S-c5qG-2npyWu
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-04-24 06:08:55 -0700
  LV Status              available
  # open                 0
  LV Size                500.00 MiB
  Current LE             125
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/new_vgroup/newlv002
  LV Name                newlv002
  VG Name                new_vgroup
  LV UUID                YKC7bR-hYr5-phJi-3auD-hMhU-9icq-AqAx2N
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-04-24 06:09:03 -0700
  LV Status              available
  # open                 0
  LV Size                500.00 MiB
  Current LE             125
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

Step #5 Create file system and Mount Logical Volumes

Type the following command to create file system.

# mke2fs -t ext4 /dev/new_vgroup/newlv001
# mke2fs -t ext4 /dev/new_vgroup/newlv002

Now run the following command to mount Logical Volumes.

# mount -t ext4 /dev/new_vgroup/newlv001 /newlv001/
# mount -t ext4 /dev/new_vgroup/newlv002 /newlv002/

You can visit at following website to get more information on Logical Volume Manager(LVM).

Conclusion

I hope you have learned something from this article and you may have found that Logical Volume Manager (LVM) is a very important topic in Linux.

I have tried my best to include all the features of LVM in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.