Create Logical volume management LVM file-system in Linux

Introduction to LVM

LVM, The topic which we are about to cover today is most important for every Linux system administrators. Let see how to set up Logical volume management from scratch and how to manage it to the extreme end. This series of the guide will cover with a number of topics which make sure yourself as a pro in handling Logical volume management in Linux.

LVM, The topic which we are about to cover today is most important for every Linux system administrators. Let see how to set up Logical volume management from scratch and how to manage it to the extreme end. This series of the guide will cover with a number of topics which make sure yourself as a pro in handling Logical volume management in Linux.

Logical Volume Management Series:
  1. Create Logical volume management LVM file-system in Linux
  2. Extend and Reduce LVM Logical Volume Management in Linux
  3. Shrink a Volume Group in Logical Volume Management (LVM)
  4. Striped Logical Volume in Logical volume management (LVM)
  5. Migrate from single-partition boot device to LVM in CentOS7
  6. Creating a Logical volume-based file system using Ansible

What is the use of LVM?

Logical volume management is a flexible file system that can be created small in size and later we can expand it to huge by adding multiple disks on the fly. In case, initially, you got a regular filesystem small in size. But later your business growth was good and you may require to increase your storage size to store more data. In this situation, we can accomplish our filesystem extension using LVM.

Where to use LVM?

You can use LVM across all the Linux platforms including your Linux-based desktop computers.

Abbreviations used in this guide.

  • LVM    Logical Volume Management
  • PV       Physical Volume
  • VG       Volume Group
  • LV        Logical Volume
  • PE       Physical Extent

Creating a Logical volume we need to follow only four steps, they are

  1. Create a partition on the RAW disk.
  2. Define Physical volume on the partitioned disk.
  3. Prepare Volume group using the created Physical volume.
  4. Creating a Logical Volume from Volume group’s available space.

This is how Logical volume can be shown in a picture.

logical volume management (LVM)
logical volume management

Step 1: Partitioning the disk for creating Physical volume.

Initially, we need to start with partitioning the disk to create a physical volume. Let see how to partition a disk using FDISK.

Read more about “Creating a partition using FDISK in Linux“.

List the disk using “-l” option with “fdisk” command.

# fdisk -l /dev/sdb
listing the disks using fdisk
listing the disks using fdisk

It’s good to know about the currently available setup before creating with any partitions, physical volume, volume group, and logical volume.

List the available PV, VG, and LV to verify.

  • PVS     Display small information about Physical volumes.
  • VGS     Display small information about Volume Groups.
  • LVS      Display small information about logical volumes.
pvs vgs lvs commands in Linux for LVM
pvs vgs lvs commands in Linux

Begin with creating the partition, Let’s briefly explain what are the options and their uses.

[stextbox id=”alert” caption=”Warning”]All the data in this disk will be lost and unrecoverable, Make sure to select the right disk.[/stextbox]
# fdisk -uc /dev/sdb
  • Fdisk      Command used to create the disk partitions
  • u              Using the unit (Sectors or Cylinders)
  • c              Compatibility mode for DOS or NON-DOS
Partitioning the disk for LVM
Partitioning the disk for LVM
Partitioning the disk for LVM 1
Partitioning the disk for LVM 1

Options which we used inside the fdisks are as follows.

  1. P   print the existing partition
  2. n   create a new partition
  3. p   create with a new primary partition
  4. 1   Assign the number to mark the partition as the first primary partition.
  5.      Entre to choose 2048 as the first sector.
  6.      Enter once again to utilize the full disk space for a single primary partition.
  7. p   Print and verify the created disk
  8. t   Change the type of partition to LVM using “8e”.
  9. p   Again print and verify before writing changes.
  10. w   write and exit from the fdisk utility.

Once completed with creating the partition we will get the output as follows.

list and verify the created partition for LVM
list and verify the created partition

Inform to the kernel about new partition

# partprobe /dev/sdb1

That’s it, we have created with partitioning using FDISK.

Step 2: Creating with Physical volume (PV).

Method 1:

We have a partition ready now, by following we need to create with a physical volume. This is a super easy step but we need to make sure to pick the right disk we about to create with the physical volume.

# pvcreate /dev/sdb1
pvcreate to create a physical volume in LVM
pvcreate to create a physical volume

List the created physical volume using “pvs” for short summary, else verify with more detailed information using “pvdisplay“. Running only “pvs” command will list all the physical volumes. If we need to list only with the created one use “pvs” or “pvdisplay” by following created partition.

# pvs
# pvdisplay
# pvs /dev/sdb1
# pvdisplay /dev/sdb1
List the Physical Volumes
pvdisplay to list the created physical volume

More explanation about “pvdisplay” output is as follows.

  1. Name of the physical volume, This will be partition we are using.
  2. If this PV associated with any VG it will display here with VG name.
  3. Size of the disk used as PV
  4. Not yet allocated for any logical volume so it will be “NO”.
  5. PE size, This will be with some value once we create the Volume Group.
  6. Point 6, 7, 8 will be applied once VG created.
  7. A Universal Unique identifier (UUID) of Physical volume.

if you are puzzled by asking yourself what is PE? It will be covered in step 3 while we create with a Volume group.

PE   â‡’   Physical extent

Method 2:

Another example of how to create a physical volume with multiple disks in a single shot. If the RAW disks are partitioned use 1 at the end of the command else use only the drive letter.

Here I have 10 numbers of the 10GB disk and I need to create 10 physical volumes.

List the lists of newly added disks.

# fdisk -l | grep /dev/sd
listing newly added 10G disks
listing newly added 10G disks

Create the partitions in all 10 disks.

partition created disks
partition created disks

Create the physical volumes in bulk using a single command.

# pvcreate /dev/sd[c-l]1
Creating physical volumes in bulk disks
Creating physical volumes in bulk disks

This is the end of method two.

We have completed step 2 by creating a Physical volume.

Step 3: Creating with Volume Group (VG).

Volume groups are a group of physical volumes to form a single pool structure. For example, we have 10 physical volumes each with 1 TB in size. If we use all the 1TB to create a “Volume group” your single volume group will have 10TB of capacity.

To get a complete understanding of the Volume group first we need to get an idea about what is the physical extent. Extends are block size of each physical volume, By default, a single extent size will be 4 MB it can be range from 8KB to 16GB in size. If we need to create extends in different sizes it is possible while create a Volume Group by using the “-s” option.

For instance, A volume group called “vg01” has one physical volume, with 20 GB in size. How to know the total number of extents in “vg01” volume group? By running vgdisplay it will show us the available extent sizes, however, we can calculate it using “Total Size / 4“. To understand the concept of extent let us do a simple calculation.

Small Calculation

(1GB) 1024 X 20 (GB) = 20480 ( 20 GB) / 4 (MB) = 5120 PE.

For 1 number of 20GB disk, we will get 5120 Physical extends, Let’s create a Volume group and confirm the above calculation.

Create a volume group in the name of “vg01” using default PE size under physical volume “/dev/sdb1” and list it with “vgs” and “vgdisplay” to know more information.

# vgcreate vg01 /dev/sdb1
# vgs
# vgdisplay vg01
Volume group creation using physical volume in LVM
Volume group creation using the physical volume
  1. Name of the Volume group.
  2. what type of LVM used. (LVM2)
  3. VG Status for access.
  4. VG can we extended or reduced.
  5. Currently how many PV under this VG.
  6. Currently active PV under this VG.
  7. The total size of the VG.
  8. Each PE size used while created this VG.
  9. Total available size of PE.
  10. Used or allocated PE.
  11. The total number of Free PE in VG.
  12. UUID of Volume group.

We have used the default size of PE 4MB to create this VG, Let see how to create a VG with 32 MB of PE size.

# vgcreate -s 32 vg01 /dev/sdb1 # vgs vg01 # vgdisplay vg01

A small calculation for PE size.

1024 x 20 = 20480 / 32 = 640 PE
Volume Group with specific Extend size
Volume Group with specific Extend size

FYI, Now when you run “pvdisplay /dev/sdb1” you will get more information which we discussed in Step 2 at points 6, 7, and 8.

pvdisplay /dev/sdb1
[root@fileserver ~]# pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg01
PV Size <20.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 5119
Allocated PE 0
PV UUID 7lqtbw-Yg6s-Usy3-RMlA-snwX-6Haz-t9Daeh
[root@fileserver ~]#

Creating a VG using multiple Physical volumes (PV).

Before we have seen how to create a Volume group using a single disk. Now let us create a Volume Group in the name of “VG02” with 8 MB physical extent using 10 number of physical volumes.

In a hard way by pointing to every device.

# vgcreate -s 8 vg02 /dev/sdc1 /dev/sdd1 /dev/sde1 /dev/sdf1 /dev/sdg1 /dev/sdh1 /dev/sdi1 /dev/sdj1 /dev/sdk1 /dev/sdl1
creating the volume group using mutipile disks 1
creating the volume group using multiple disks in a hard way

In a lazy way and being smart.

# vgcreate -s 8 vg02 /dev/sd[c-l]1
# vgs
# vgdisplay vg02
vgcreate and vgdisplay
vgcreate and vgdisplay
  1. Volume group name.
  2. The number of physical volumes used in vg02.
  3. How many physical volumes are active.
  4. The total size of a volume group.
  5. PE size used while creating the Volume group.
  6. Total number of PE available in this Volume group.
  7. How much PE’s are used (We not yet used, because not yet created with LV)
  8. Free PE in vg02.
  9. UUID of Volume group.

We have completed method 2 for creating the Volume group.

Step 4: Creating with Logical Volume (LV).

Logical volumes can be created only from the Volume group space. If VG does not have enough space we can’t create with a Logical volume.

Now let us create 2 Logical volumes using different methods. To create an LVM using round the unit size we need to use Capital “-L”, Same if we need to create with an extent replace it with small “-l”.

Create a Logical volume using Unit Size. (-L)

Create another logical volume in the name of “data” using 5G under “vg01”.

[stextbox id=”info” caption=”info”]LV can be created using units K for KB, M for MB, G for GB and T for TB in size.[/stextbox]

List the current VG and LV before creating one to verify whether we have enough space in vg01.

Verify the volume group size and logical volumes
Verify the volume group size and logical volumes

Creating logical volume with Unit size.

# lvcreate -n data -L 5G vg01
# lvs
# lvdisplay /dev/mapper/vg01-data
logical volume creation using GB in Size
logical volume creation using GB in Size
  1. lvcreate      the command to create the logical volume
  2. -n                 to give a name to logical volume
  3. data             name of the logical volume we created.
  4. -L                  creating a logical volume using Unit size (K, M, G, T).
  5. 5G                 size of the logical volume in GB
  6. vg01             Under which volume group the LV needs to be created.

We have completed creating a logical volume with 5GB in size.

Create a Logical volume using Physical Extent (PE). (-l)

Create a logical volume in the name of “backup” using 160 PE under “vg01“.

While creating “VG01” we have used 32MB as physical extent (PE) size so need to do a simple calculation to know the 5GB of size in physical extent.

1024 X 5 = 5120 / 32 = 160
1024 (1GB) X 5 (GB) = 5120 (MB) / 32 (PE)= 160 PE

List and verify the existing VG and LV.

list the created Logical volume
list the created Logical volume

Create the Logical volume (LV) using physical extent size.

# lvcreate -l 160 -n backup vg01 
# lvs
# lvdisplay /dev/mapper/vg01-backup
lvcreate using extent size in Linux
lvcreate using extent size in Linux
  1. -n               to give a name
  2. backup     name of the logical volume we created.
  3. -l                creating a logical volume using extend.
  4. 160           Physical extends used. Each PE size is 32M
  5. vg01         Under which volume group the LV has been created.

Understanding “lvdisplay” output.

  1. Under which path the LV created.
  2. Name of the LV
  3. LV underlying in which VG
  4. UUID of LV
  5. The current mode of LV
  6. Size of the LV created.
  7. Logical extends currently used in LV
  8. Under which block device the LV has been created. (/dev/sdb) Refer below step.

knowing the Block Devices and LVM.

# lsblk /dev/sdb
knowing the block device mapped to Logical volume
knowing the block device mapped to Logical volume

We have completed creating two Logical volumes. Finally, we need to move towards Step 5 to create a file system and mount it under any mount point in Linux.

Follow the below article to read about how to create a file system and mount it on Linux servers.

Step 5: Creating and mounting the filesystem in Linux

If you are looking for LVM articles follow the below link to continue reading the series.

Extend and Reduce LVM Logical Volume Management in Linux

Conclusion:

Every Linux sysadmins should familiar with Logical volume management and creating filesystem to provide uninterrupted support in our daily day-to-day operations. In this guide, we have covered briefly how to create a Logical volume. By following we are about to continue with more advanced topics about LVM. We are eager to hear from you any feedback, Never miss an update from us by subscribing to our newsletters.

One thought on “Create Logical volume management LVM file-system in Linux

  1. Pingback: LVM in VMWare Linux Virtual Machine

Comments are closed.