0% found this document useful (0 votes)
18 views35 pages

How To Create Disk Storage With LVM in Linux - Part 1

Uploaded by

Samuel Asmelash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views35 pages

How To Create Disk Storage With LVM in Linux - Part 1

Uploaded by

Samuel Asmelash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

eBook - RHCE Certification Preparation

Menu

A Beginner-Friendly Guide for Linux / Start Learning Linux Quickly...

How to Create Disk Storage with Logical Volume Management


(LVM) in Linux – PART 1

Ravi Saive Last Updated: August 7, 2023 Storage 24 Comments

Logical Volume Management (LVM) is a powerful tool that greatly simplifies disk space
management. With LVM, the task of allocating additional space to a file system becomes
effortless.

Whenever a file system requires more space, it can easily draw from the available free space in its
corresponding volume group. As a result, file systems can be dynamically resized to
accommodate our specific needs.

Furthermore, LVM offers robust data protection in the event of disk failures. If a disk begins to
show signs of failure, we can seamlessly integrate a replacement disk into the volume group as a
new physical volume.

By doing so, the logical volumes can be efficiently migrated to the fresh disk, ensuring continuity
and safeguarding against any potential data loss.

In essence, LVM empowers administrators to handle disk space allocation and management with
unparalleled flexibility and resilience. Its dynamic resizing capabilities and built-in redundancy
features make it an indispensable asset for optimizing storage utilization and enhancing data
integrity in any system configuration.
Introducing the comprehensive series “Preparation for Setting Up LVM (Logical Volume
Management)“, thoughtfully crafted across Parts 1 to 6, with an in-depth exploration of the
following essential topics:

1. How to Setup Disk Storage with Logical Volume Management


2. How to Extend or Reduce LVM (Logical Volume Management) in Linux
3. How to Take Snapshot of Logical Volume and Restore in LVM
4. How to Setup Thin Provisioning Volumes in Logical Volume Management (LVM)
5. How to Manage Multiple LVM Disks using Striping I/O
6. How to Migrate LVM Partitions to New Logical Volume (Drive)

Before proceeding with the LVM setup, consider the following prerequisites:

My Server Setup – Requirements

OS – RHEL 9 with LVM Installation

IP – 192.168.0.200

Disks – 3 disks with 20GB each.

Check LVM Disk Storage in Linux

1. To gain insight into our LVM setup, we can utilize the following commands to reveal the distinct
components: Physical Volume (PV), Volume Group (VG), and Logical Volume (LV).

# pvs
# vgs
# lvs
List LVM Setup in Linux

Here, is the description of each parameter shown in the above screenshot.

Physical Disk Size (PV Size)

The disk used was Virtual Disk sda.

Volume Group Size (VG Size)

Volume Group name (vg_tecmint)

Logical Volume name (LogVol00, LogVol01)

LogVol00 Assigned for a swap with 956MB Size

LogVol01 Assigned for/with 18.63GB

So, from here we come to know that there is not enough free space in the VDA disk.

Create a New Volume Group in LVM

2. To create a new Volume Group, we need to add an additional 3 hard disks to this server.
However, it is not compulsory to use 3 drives; just 1 is enough to create a new VG and LV (Logical
Volume) inside that VG.

I am adding the following 3 disks here for demonstration purposes and to provide more feature
command explanations.
sdb, sdc, sdd

3. To list all the disks and their partitions, such as the disk name, size, partition type, start and end
sectors, and more use the fdisk utility as shown.

# fdisk -l
List Disk Partitions in Linux

Here, is the description of each disk shown in the above screenshot.

The default disk used for the Operating System is RHEL 9.

Partitions defined on the default disk are as follows: (sda1 = boot), (sda2 = /).
Additionally, added disks are mentioned as Disk1, Disk2, and Disk3.

Each and every disk is 20 GB in size.

4. Now run the vgdisplay command to view the detailed information about all the Volume Groups
present on the system, including their name, size, free space, physical volume (PV) information,
and more.

# vgdisplay

List Volume Groups in Linux


Here, is the description of each parameter shown in the above screenshot.

VG Name – A volume group name.

Format – LVM architecture used lvm2.

VG Access – The Volume Group is in read-and-write mode and ready to use.

VG Status – The Volume Group can be resized. We can expand it if we need to add more
space.

Cur LV – Currently, there are 2 Logical volumes in this Volume Group.

CurPV and Act PV – Currently, the physical disk in use is 1 (vda), and it’s active. So, we can
use this Volume Group.

PE Size – Physical Extents (PEs) and size for a disk can be defined using either PE or GB
size. The default PE size of LVM is 4 MB. For example, if we need to create a 5 GB logical
volume, we can use a sum of 1280 PEs. Do you understand what I’m saying?

Here’s the explanation: 1 GB is equal to 1024 MB, so 1024 MB x 5 = 5120 PE = 5 GB. Now,
divide 5120 by 4 = 1280. 4 is the default PE size.

Total PE – This Volume Group has.

Alloc PE – Total PE Used, full PE already Used, 5008 x 4PE = 20032.

Free PE – Here it’s already used so there was no free PE.

5. Now list the file system disk space information, here only sda is used with /boot, /, and swap
on the sda physical disk using LVM. There is no space remaining on this disk.

# df -TH
List File System Disk Space

The above image shows the mount point we are using, and the 19GB is fully used for the root, so
there is no free space available.

Create a Disk Partition

6. So, let’s create a new physical volume (PV) and volume group (VG) named tecmint_add_vg,
and create logical volumes (LVs) within it. Here, we can create 4 logical volumes with the names
tecmint_documents, tecmint_manager, and tecmint_public.

We can extend the Volume Group of the currently used VG to get more space. However, in this
case, we are going to create a new Volume Group and experiment with it. Later, we can see how
to extend the file systems of the Volume Group that is currently in use.

Before using a new disk, we need to partition the disk using the fdisk command as shown.

# fdisk -c /dev/sdb
Create /dev/sdb Disk Partition

Next, follow the below steps to create a new partition.

Choose n to create new.

Choose p to create a primary partition.


Choose which number of partitions we need to create.

Press enter twice to use the full space of the disk.

We need to change the type of newly created partition type t .

Which number of partition need to change, choose the number which we created its 1 .

Here we need to change the type, we need to create LVM so we going to use the type code
of LVM as 8e , if we do not know the type code Press L to list all types of codes.

Print the partition that we created to just confirm.

Here we can see the ID as 8e LINUX LVM.

Write the changes and exit the fdisk.

7. Do the above steps for the other 2 disks sdc and sdd to create new partitions. Then restart
the machine to verify the partition table using the fdisk command.

# fdisk -l
Confirm Disk Partitions

Create LVM Physical Volume

8. Now, it’s time to create Physical Volumes using all 3 disks. Here, I have listed the physical
disks using the ‘pvs‘ command, and now only one default PV is listed.
# pvs

9. Then create the new physical disks and confirm the newly created physical disks.

# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1


# pvs

Create LVM Physical Volumes

Creating LVM Volume Groups

10. Create a Volume Group named tecmint_add_vg using the available free PV and a PE size of
32. To display the current volume groups, we can see that there is one volume group with 1 PV in
use.

# vgs
11. This will create the volume group named tecmint_add_vg using a 32MB PE size and the 3
physical volumes we created in the last steps.

# vgcreate -s 32M tecmint_add_vg /dev/sdb1 /dev/sdc1 /dev/sdd1

12. Next, verify the volume group by running the vgs command again.

# vgs

Confirm LVM Volume Groups

Understanding vgs command output:

Volume Group name.

Physical Volumes used in this Volume Group.

Shows free space available in this volume group.


Total Size of the Volume Group.

Logical Volumes inside this volume group, Here we have not yet created so there is 0.

SN = Number of Snapshots the volume group contains. (Later we can create a snapshot).

Status of the Volume group as Writeable, readable, resizeable, exported, partial, and
clustered, Here it is wz–n- that means w = Writable, z = resizeable.

Number of Physical Volume (PV) used in this Volume Group.

13. To display more information about the volume group use the command.

# vgs -v

View LVM Volume Groups Info

14. To get more information about newly created volume groups, run the following command.

# vgdisplay tecmint_add_vg
List LVM Volume Groups

Here, is the description of each parameter shown in the above screenshot.

Volume group name

LVM Architecture used.

It can be read and write state, ready to use.

This volume group can be resizeable.

No Physical disk was used and they are active.

Volume Group total size.

A Single PE size was 32 here.

Total number of PE available in this volume group.


Currently, we have not created any LV inside this VG so it’s totally free.

UUID of this volume group.

Creating LVM Logical Volumes

15. Now, create 3 Logical Volumes named tecmint_documents, tecmint_manager, and


tecmint_public. Here, we will demonstrate how to create Logical Volumes using both PE size and
GB size.

First, list the current Logical Volumes using the following command..

# lvs

List LVM Logical Volumes

16. These Logical Volumes are in the vg_tecmint Volume Group. To see how much free space is
available to create logical volumes, list the Volume Group and available Physical Volumes using
the ‘vgs‘ command.

# vgs
List Volume Groups

The volume group size is almost 60GB, and it is unused, so we can create LVs in it. Let us divide
the volume group into equal sizes to create 3 Logical Volumes. That means 60GB/3 = 20GB. Each
Logical Volume will be 20GB in size after creation.

Method 1: Creating Logical Volumes using PE Size

First, let us create Logical Volumes using the Physical Extent (PE) size. We need to know the
default PE size assigned to this Volume Group and the total available PEs to create new Logical
Volumes.

Run the following command to get this information.

# vgdisplay tecmint_add_vg
Create a New Logical Volume

The default PE Assigned for this VG is 32MB, Here Single PE size will be 32MB.

Total Available PE is 1917.

Just do and see a little Calculation using the bc command.

# bc

1917PE/3 = 639 PE.


639 PE x 32MB = 20448 --> 20GB

Calculate Disk Space

Press CRTL+D to exit from bc.

Let us now create 3 Logical Volumes using 639 PE’s. Here -l used to extend the size and -n to
assign a logical volume name.

# lvcreate -l 639 -n tecmint_documents tecmint_add_vg


# lvcreate -l 639 -n tecmint_manager tecmint_add_vg
# lvcreate -l 639 -n tecmint_public tecmint_add_vg

List the created Logical Volumes using lvs command.

# lvs
List Created Logical Volumes

Method 2: Creating Logical Volumes using GB Size

While creating Logical Volume using GB size we cannot get the exact size. So, the better way is to
create using extend.

# lvcreate -L 20G -n tecmint_documents tecmint_add_vg


# lvcreate -L 20G -n tecmint_manager tecmint_add_vg
# lvcreate -L 20G -n tecmint_public tecmint_add_vg
# lvcreate -L 20G -n tecmint_public tecmint_add_vg

List the Created logical Volumes using lvs command.

# lvs

Here, we can see while creating the 3rd LV we can’t Round-up to 20GB, it is because of small
changes in size, but this issue will be ignored while creating LV using Extend size.

Creating File System


17. For using the logical volumes we need to format. Here I am using the ext4 file-system to
create the volumes and going to mount them under /mnt/.

# mkfs.ext4 /dev/tecmint_add_vg/tecmint_documents
# mkfs.ext4 /dev/tecmint_add_vg/tecmint_public
# mkfs.ext4 /dev/tecmint_add_vg/tecmint_manager

Create Ext4 File System


18. Let us create directories in /mnt and mount the Logical volumes that we have created file-
system.

# mount /dev/tecmint_add_vg/tecmint_documents /mnt/tecmint_documents/


# mount /dev/tecmint_add_vg/tecmint_public /mnt/tecmint_public/
# mount /dev/tecmint_add_vg/tecmint_manager /mnt/tecmint_manager/

19. List and confirm the Mount point using.

# df -h

Mount Logical Volumes

Permanent Mounting of Logical Volumes

20. It’s now temporarily mounted, for permanent mount, we need to add the entry in fstab, for
that let us get the mount entry from mtab using
# cat /etc/mtab

21. We need to make slight changes in the fstab entry while entering the mount entry contents
copies from mtab, we need to change the rw to defaults

# vi /etc/fstab

Our fstab entries should look similar to the below sample.

/dev/mapper/tecmint_add_vg-tecmint_documents /mnt/tecmint_documents
/dev/mapper/tecmint_add_vg-tecmint_public /mnt/tecmint_public
/dev/mapper/tecmint_add_vg-tecmint_manager /mnt/tecmint_manager

Permanent Mount Logical Volumes

22. Finally, run the command mount -a to check for the fstab entry before restarting.
# mount -av

Confirm Mount Points

Here we have seen how to set up flexible storage with logical volumes by using physical disk to
physical volume, physical volume to the volume group, and volume group to logical volumes.

In my upcoming future articles, I will see how to extend the volume group, and logical volumes,
reduce logical volume, take snapshots, and restore from snapshots. Till then stay updated to
TecMint for more such awesome articles.

If you read this far, tweet to the author to show them you care. Tweet a thanks

PREVIOUS ARTICLE:

How to Setup SSH Passwordless Login in Linux [3 Easy Steps]


NEXT ARTICLE:

What are Ext2, Ext3 & Ext4? How to Create and Convert Linux File
Systems

Ravi Saive

I am an experienced GNU/Linux expert and a full-stack software developer with over a


decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system


administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have
Subscribed)

Was this article helpful? Please add a comment or buy me a coffee to show your
appreciation.

Related Posts
How to Install OwnCloud to Create Own Cloud Storage in Linux

How to Install OwnCloud on Ubuntu 18.04


How to Install NextCloud on CentOS 8

How to Install OwnCloud in Debian 10


How to Install OwnCloud on CentOS 8

How to Create a VDO Volume On a Storage Device on RHEL 8

24 thoughts on “How to Create Disk Storage with Logical Volume Management


(LVM) in Linux – PART 1”

← Older Comments
Majid Khatib Shahidi
December 1, 2019 at 1:25 am

Thanks, very helpful article…

Reply

Madhav
June 10, 2019 at 9:42 am

What is the purpose of /etc/mtab file? what is difference between fstab and mtab?

Reply

Priyashree
February 4, 2018 at 11:17 pm

Can we use a full disk for LVM without partitioning or is it necessary to partition the disk
for LVM

Reply

Bobin Lonston
February 5, 2018 at 12:48 pm
@ Priyashree,
We can use full disk without partitioning.

Reply

Imad
December 12, 2017 at 9:19 pm

Thanks, please add more information about why we should choose a bigger or smaller
PE size.

Reply

Bobin Lonston
January 2, 2018 at 11:50 pm

@Imad,

Bigger PE size will be good, a large number of extents will slow down the tools but it
won’t affect any I/O performance. Large size file system large value of PE is good by
default it will select 4 MB.

Regards,

Reply
swati
September 15, 2017 at 7:05 pm

Hello,

Good article.

I have 1 query. Can we mount more than 1 directory in same logical volume?

For example: /mnt_techmint_documents , /mnt_techmint_public on same logical volume


(/dev/mapper/techmint_add_vg_techmint_documents)

Reply

Babin Lonston
September 19, 2017 at 10:57 pm

@Swati,

No, You can’t mount a single Logical Volume to multiple directories.

Thanks & Regards,


Babin Lonston

Reply

Macfa
August 8, 2017 at 8:53 pm
Thx, It’s helpful lol

Reply

Raghu
November 2, 2016 at 10:25 pm

please send interview questions and answers to my mail…

Reply

← Older Comments

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision
to leave a comment and value your contribution to the discussion. It's important to note that
we moderate all comments in accordance with our comment policy to ensure a respectful
and constructive conversation.

Rest assured that your email address will remain private and will not be published or
shared with anyone. We prioritize the privacy and security of our users.
Name *

Email *

Save my name, email, and website in this browser for the next time I comment.

Post Comment

Search...

A Beginners Guide To Learn Linux for Free [with Examples]

Linux Commands and Tools

How to Run a Linux Command Without Saving It in History

How to Copy File Permissions and Ownership to Another File in Linux

10 Lesser Known Linux Commands – Part 2

Advanced Copy Command – Shows Progress Bar While Copying/Moving Files in Linux
Kurly – An Alternative to Most Widely Used Curl Program

8 Netcat (nc) Command with Examples

Linux Server Monitoring Tools

How to Monitor MySQL/MariaDB Databases using Netdata on CentOS 7

How to Install LibreNMS Monitoring Tool on Debian 11/10

How to Add Windows Host to Nagios Monitoring Server

How to Monitor Linux Server Security with Osquery

Linux Performance Monitoring with Vmstat and Iostat Commands

Understand Linux Load Averages and Monitor Performance of Linux

Learn Linux Tricks & Tips

How to Run MySQL/MariaDB Queries Directly from the Linux Command Line

How to Recover a Deleted File in Linux

Learn Why ‘less’ is Faster Than ‘more’ Command for Effective File Navigation

How to Split Large ‘tar’ Archive into Multiple Files of Certain Size

How to Increase Number of Open Files Limit in Linux

Find Top 15 Processes by Memory Usage with ‘top’ in Batch Mode

Best Linux Tools

5 Best Platforms for Hosting Your Web Development Projects

8 Top Open Source Reverse Proxy Servers for Linux


The Top 5 Open-Source Microsoft 365 Alternatives for Linux

Best Open Source Internet Radio Player for Linux

16 Best RSS Feed Readers for Linux in 2021

10 Tools to Take or Capture Desktop Screenshots in Linux

Tecmint: Linux Howtos, Tutorials & Guides © 2023. All Rights Reserved.
The material in this site cannot be republished either online or offline, without our permission.
Hosting Sponsored by : Linode Cloud Hosting

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy