Skip to main content

Creating logical volumes in Linux with LVM

In this third LVM article, you'll learn how to create and use a logical volume which is the final piece of the LVM puzzle.
Image
How to create a logical volume

Image by PIRO4D from Pixabay

Logical Volumes (LV) are the final storage unit in the standard LVM architecture. These units are created from the volume group, which is made up of physical volumes (PV). If you have been following along in the series, you will have initialized your physical volumes and combined them into a volume group (VG). We will be continuing our LVM exploration by further splicing our newly created volume group LVMvgTEST into various logical volumes.

If you are interested in the previous articles, you can find part 1 and part 2 here.

As with all things, variation is the spice of life, and that's no different when it comes to technology—specifically, logical volumes. You have a few different options at your disposal here, and they each have unique use cases that sysadmins can employ to best fit a given situation. Your options are as follows:

  • Linear logical volume
  • Striped logical volume
  • Mirrored logical volume

I will be discussing each of these volume types in further detail, walking you through examples of when and why you would want to use each one. I will also walk you through a basic configuration of each. Let's get to it!

Linear logical volume

Linear logical volumes are the LVM default when it comes to logical volume creation. They are generally used to combine one or more disks to create one usable storage unit. We created a 2G volume group called LVMvgTEST in our last article. That volume group was created by joining two unique 1G physical volumes. Here I am going to use a small portion of that volume group to create a linear logical volume titled, very creatively, lv_linear. Seen below:

[root@rhel ~]# lvcreate -L 500M -n lv_linear LVMvgTEST 
Logical volume "lv_linear" created.

You can use the lvdisplay for detailed information on the logical volumes currently in existence on your system.

[root@rhel ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/LVMvgTEST/lv_linear
  LV Name                lv_linear
  VG Name                LVMvgTEST
  LV UUID                hxBk3i-deYU-OjG1-KdR8-noDm-yeYh-EiF8Mc
  LV Write Access        read/write
  LV Creation host, time rhel.test, 2020-03-12 12:38:16 -0400
  LV Status              available
  # open                 0
  LV Size                500.00 MiB
  Current LE             125
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

You can also use the lvs command if verbosity isn't your thing:

[root@rhel ~]# lvs
  LV        VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_linear LVMvgTEST -wi-a----- 500.00m                                                    
  root      rhel      -wi-ao---- <26.00g                                                    
  swap      rhel      -wi-ao----   3.00g 

These are the most common logical volume types and are very straightforward to create.

[ Free online course: Red Hat Enterprise Linux technical overview. ]

Striped logical volume

Striped logical volumes allow the administrator to control the way that the data is written to the physical volumes. For high volume read/write scenarios, striped logical volumes would be ideal, as they allow for read and write operations to be done in parallel.

When using striped logical volumes, you can set the number of stripes (this number cannot exceed the number of physical volumes) and the stripe size. This allows the user a greater level of control over how I/O is performed on the system.

Here, we are going to create a striped logical volume of 500Mb. The -i2 denotes the number of stripes (because we only have two physical volumes, we are using two). The -I64 denotes the size of the stripes as the default 64Kb. We named the striped volume lv_stripe, and it is a part of the volume group LVMvgTEST.

 [root@rhel ~]# lvcreate -L 500M -i2 -I64 -n lv_stripe LVMvgTEST 
      Rounding size 500.00 MiB (125 extents) up to stripe boundary size 504.00 MiB(126 extents).
      Logical volume "lv_stripe" created.

Now, using the lvdisplay command, you can see both the linear volume and the newly created striped volume:

[root@rhel ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/LVMvgTEST/lv_linear
  LV Name                lv_linear
  VG Name                LVMvgTEST
  LV UUID                hxBk3i-deYU-OjG1-KdR8-noDm-yeYh-EiF8Mc
  LV Write Access        read/write
  LV Creation host, time rhel.test, 2020-03-12 12:38:16 -0400
  LV Status              available
  # open                 0
  LV Size                500.00 MiB
  Current LE             125
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/LVMvgTEST/lv_stripe
  LV Name                lv_stripe
  VG Name                LVMvgTEST
  LV UUID                tqtkco-QZgj-TvOq-hzSk-G2Ti-jfsU-5bhMlz
  LV Write Access        read/write
  LV Creation host, time rhel.test, 2020-03-13 12:42:38 -0400
  LV Status              available
  # open                 0
  LV Size                504.00 MiB
  Current LE             126
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

These kinds of logical volumes are really helpful in the right circumstances! If you need high volume read/write, consider striping your volumes.

Mirrored logical volume

Mirrored logical volumes do exactly what you would expect them to do. They allow you to "reflect" the data on one device to an identical copy. This ensures your data is available. If one part of the mirror breaks, the remaining drive changes its characteristics to that of a linear volume and is still accessible. LVM keeps a log on what data is where, which allows the changes to be persistent. Let's look at how to create a mirror with LVM.

[root@rhel ~]# lvcreate -L 100M -m1 -n lv_mirror LVMvgTEST 
  Logical volume "lv_mirror" created.

You can see that we created a mirror drive of 100Mb, named the mirror lv_mirror, and created it on the LVMvgTEST volume group. All of this was done using the same lvcreate command from the previous examples. We can verify creation by using the lvdisplay command.

 [root@rhel ~]# lvdisplay
   --- Logical volume ---
  LV Path                /dev/LVMvgTEST/lv_mirror
  LV Name                lv_mirror
  VG Name                LVMvgTEST
  LV UUID                0eTHem-rw8b-PK0J-wibU-f94M-bypL-1IM7AG
  LV Write Access        read/write
  LV Creation host, time rhel.test, 2020-03-13 13:01:41 -0400
  LV Status              available
  # open                 0
  LV Size                100.00 MiB
  Current LE             25
  Mirrored volumes       2
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:8

Wrap up

So we looked at logical volumes as a whole, the three kinds of logical volumes that LVM allows you to create, and how to configure these volumes. LVM allows you to create a storage unit to fit almost any need you may have as an administrator, and that's what makes it such a great utility. I recommend that you give LVM a try the next time you need to accomplish any disk manipulation. In my opinion, there is no better tool for the job!

[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]

Topics:   Storage   Linux  
Author’s photo

Tyler Carrigan

Tyler is a community manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX