How To Create Disk Storage With LVM in Linux - Part 1
How To Create Disk Storage With LVM in Linux - Part 1
Menu
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:
Before proceeding with the LVM setup, consider the following prerequisites:
IP – 192.168.0.200
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
So, from here we come to know that there is not enough free space in the VDA disk.
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
Partitions defined on the default disk are as follows: (sda1 = boot), (sda2 = /).
Additionally, added disks are mentioned as Disk1, Disk2, and Disk3.
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
VG Status – The Volume Group can be resized. We can expand it if we need to add more
space.
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.
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.
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
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.
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
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.
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.
12. Next, verify the volume group by running the vgs command again.
# vgs
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.
13. To display more information about the volume group use the command.
# vgs -v
14. To get more information about newly created volume groups, run the following command.
# vgdisplay tecmint_add_vg
List LVM Volume Groups
First, list the current Logical Volumes using the following command..
# lvs
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.
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.
# 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.
# 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.
# lvs
List Created Logical Volumes
While creating Logical Volume using GB size we cannot get the exact size. So, the better way is to
create using extend.
# 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.
# mkfs.ext4 /dev/tecmint_add_vg/tecmint_documents
# mkfs.ext4 /dev/tecmint_add_vg/tecmint_public
# mkfs.ext4 /dev/tecmint_add_vg/tecmint_manager
# df -h
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
/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
22. Finally, run the command mount -a to check for the fstab entry before restarting.
# mount -av
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:
What are Ext2, Ext3 & Ext4? How to Create and Convert Linux File
Systems
Ravi Saive
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
← Older Comments
Majid Khatib Shahidi
December 1, 2019 at 1:25 am
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?
Reply
Babin Lonston
September 19, 2017 at 10:57 pm
@Swati,
Reply
Macfa
August 8, 2017 at 8:53 pm
Thx, It’s helpful lol
Reply
Raghu
November 2, 2016 at 10:25 pm
Reply
← Older Comments
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...
Advanced Copy Command – Shows Progress Bar While Copying/Moving Files in Linux
Kurly – An Alternative to Most Widely Used Curl Program
How to Run MySQL/MariaDB Queries Directly from the Linux Command Line
Learn Why ‘less’ is Faster Than ‘more’ Command for Effective File Navigation
How to Split Large ‘tar’ Archive into Multiple Files of Certain Size
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