In this article, we are going to learn How to Setup Linux LVM (Logical Volume Manager) using pvcreate, vgcreate, lvcreate commands. LVM stands for Logical Volume Manager OR Logical Volume Management. As its name Linux LVM used to Manage the Logical Volumes by Extend, Resize, Reduce, Rename and so many other features.
Why we should use Logical Volume Manager (LVM)?
As we already discussed Linux LVM is used to Extend the Logical Volume Partition, Reduce the Partition, Resize the Partition, and so on. Now you might be thinking that Where we would be required to Extend the Partition. Let me give you a quick Example so that your concept will get more clear toward Logical Volume Manager (LVM).
Example of a Normal Harddisk Server Setup (Without LVM) :
Suppose You have a server in your office whose Harddisk size let’s say about 500 GB and all users are storing their data in that server. One day you came to know that the Harddisk got full and you have no space left for storing more data any further. In that case, you have two options to make space available for user i.e either delete unwanted data OR move the data manually to some different hard disk. But you have no options to Increase or extend the disk space in case of the normal hard disk.
Example of Linux LVM Configured Server :
Now let’s take the above scenario with Linux LVM configured Server. In this case, if your 500 GB harddisk got full then you can take a New Harddisk and just connect it and Extend your current Partition Live without losing any data and also without any downtime. You don’t even require to shut down or restart the server during harddisk extend process.
Advantages of Linux LVM (Logical Volume Manager) :
Linux LVM (Logical Volume Manager) capable of performing the below tasks :
- Extend the Physical Volume, Volume Group, Logical Volume Partition’s
- Reduce the Physical Volume, Volume Group, Logical Volume Partition’s
- Resize the Physical Volume, Volume Group, Logical Volume Partition’s
- Take a snapshot of Logical Volume using Linux LVM Snapshot…and so on.
Follow the below Steps to Configure Linux LVM (Logical Volume Manager) :
To configure Linux LVM we need at least a Physical Harddisk or a Partition. Here I have 3 Harddisk’s i.e : /dev/sdb, /dev/sdc, /dev/sdd with size 2 GB each.
[root@localhost ~]# fdisk -l # To List Partition Table Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000817a9 Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2350 18566144 83 Linux /dev/sda3 2350 2611 2097152 82 Linux swap / Solaris Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/sdc: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/sdd: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
The Three major steps we have to follow to Setup a Logical Volume Manager (LVM). The Steps are :
- Physical Volume: Prepare the Physical Harddisk’s some way so that Linux LVM can understand the Device and can be used to configure Volume Group and Logical Volumes is call as Physical Volume.
- Volume Group: Combination of Multiple Physical Volumes is called Volume Group.
- Logical Volume: We can create Partitions out of Volume Group to store data is called a Logical Volume. Logical Volumes are the same as we are using normal Disk Partitions.
Create Physical Volume using pvcreate (PV)
First, we have to prepare the Physical Harddisk’s for Linux LVM so that Linux LVM can understand that yes these Harddisks are now prepared and can be used to Configure Logical Volume Manager. For that, we have to convert Physical Harddisks as a Physical Volume. To do so we can use the command pvcreate. Here I am creating my First Physical Volume using my Harddisk /dev/sdb. Refer to the command below.
Syntax: pvcreate [Physical Harddisk]
[root@localhost ~]# pvcreate /dev/sdb # Create Physical Volume Physical volume "/dev/sdb" successfully created
As you can see above we created physical volume successfully. To check the details of Physical Volume use the command pvdisplay.
You can use only pvdisplay command to show all available Physical Volumes’s in the system.
[root@localhost ~]# pvdisplay # Check Physical Volume Details
OR you can mention particular physical volume to check the details.
[root@localhost ~]# pvdisplay /dev/sdb # Check Physical Volume Details "/dev/sdb" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID Mefmtv-XiDU-FWAo-KTgM-t54Z-7ZCE-n8WQDj
Where :
- PV Name: Physical Harddisk Name
- VG Name: Volume Group Name
- PV Size: Physical Volume Size
- Allocatable: The amount of Space we can allocate, Currently It’s NO.
- PE Size: PE stands for Physical Extent. Normally the size of the Physical Extent is 4 MB. Here It’s 0 as we haven’t created any Volume Group out of this Physical Volume. Once We create Volume Group from this Physical Volume then PE Size, Total PE, Free PE, and Allocated PE options will get an update.
- Total PE: Total available Physical Extents.
- Free PE: Remaining Physical Extents.
- Allocated PE: Already Allocated Physical Extent to Volume Group.
Note: The Physical Extent (PE) details are updated in Physical Volume only after create a Volume Group out of it.
Below I have explained what is Physical Extent and How to calculate it.
We have two more Physical Harddisk’s i.e. /dev/sdc and /dev/sdd which need to be configured as Physical Volume. So follow the below commands to so.
[root@localhost ~]# pvcreate /dev/sdc Physical volume "/dev/sdc" successfully created [root@localhost ~]# pvcreate /dev/sdd Physical volume "/dev/sdd" successfully created
You can also use PVS command to check the quick short summary of all available Physical Volumes. vgs referred to as Volume Group Summery.
[root@localhost ~]# pvs # Check available Physical Volumes PV VG Fmt Attr PSize PFree /dev/sdb lvm2 a-- 2.00g 2.00g /dev/sdc lvm2 a-- 2.00g 2.00g /dev/sdd lvm2 a-- 2.00g 2.00g
Also Read – How to Configure Linux LVM (Logical Volume Manager) Using Software RAID 5
Create Volume Group (VG) using vgcreate
So we have completed our first step and ready with Physical Volumes. Now let’s move to our next step and create Volume Groups by using vgcreate command.
Syntax: vgcreate [Name of Volume Group] [Physical Volumes]
[root@localhost ~]# vgcreate vgroup001 /dev/sdb /dev/sdc /dev/sdd # Create Volume Group Volume group "vgroup001" successfully created
To check the details of Volume Group you can use the command vgdisplay.
[root@localhost ~]# vgdisplay # Check the details of Volume Group --- Volume group --- VG Name vgroup001 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 5.99 GiB PE Size 4.00 MiB Total PE 1533 Alloc PE / Size 0 / 0 Free PE / Size 1533 / 5.99 GiB VG UUID xn4b80-1CeV-KYJu-1x6Z-ipTQ-2fSZ-axLx3D
Where :
- VG Name: Name of the Volume Group
- Format: Linux LVM (Logical Volume Manager) Version 2
- VG Access: Volume Group can be Read and Writable
- VG Status: Volume Group can be Resizable
- MAX LV: You can create an unlimited number of Logical Volumes in this Volume Group till available Physical Extent (PE).
- Cur LV: Currently created Logical Volumes is 0
- Max PV: You can add an unlimited number of Physical Volumes to this Volume Group.
- Cur PV: Currently we have 3 Physical Volumes assigned this Volume Group.
- Act PV: Active Physical Volumes are 3.
- VG Size: Total Size of the Volume Group.
- PE Size: As I already discussed above the size of Physical Extent (PE) is 4 MB.
- Alloc PE / Size: Allocated Physical Extent. Here we haven’t allocated any of PE to Logical Volume. and the Volume Group is completely free to use and create Logical Volumes.
- Free PE / Size : The available Physical Extent (PE) / Size.
Now let’s discuss What is PE (Physical Extent)?
Physical Volume is divided into small pieces of size is called a Physical Extent. For example Here our Volume Group vgroup001 has 1533 PE means, the Physical Volume is divided into 1533 pieces of Physical Extents. Normally the size of a single PE is 4 MB. We can calculate the Physical Extent to find the size of the Volume Group. The calculation would be something like as shown below.
Total Number of PE: 1533
1 PE: 4 MB
So the size of the Volume Group is: 1533*4 = 6132MB (5.99 GB)
The size of the PE (Physical Extents) is the same as LE (Logical Extents).
To check quick short details of Volumes Groups you can use vgs command. vgs Referred to as Volume Group Summary
[root@localhost ~]# vgs # Check Volume Group Details VG #PV #LV #SN Attr VSize VFree vgroup001 3 0 0 wz--n- 5.99g 5.99g
Now as we created Volume Groups out of our Physical Volumes, let’s check what all changes happened to our Physical Volumes.
As I explained earlier after creating volume groups out of physical volume the PE (Physical Extent) details will get an update. Refer to the sample output below.
[root@localhost ~]# pvdisplay # Check Physical Volume Details --- Physical volume --- PV Name /dev/sdb VG Name vgroup001 PV Size 2.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 511 Free PE 511 Allocated PE 0 PV UUID Mefmtv-XiDU-FWAo-KTgM-t54Z-7ZCE-n8WQDj --- Physical volume --- PV Name /dev/sdc VG Name vgroup001 PV Size 2.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 511 Free PE 511 Allocated PE 0 PV UUID Sq28cI-hNH2-k0Y5-rDOA-GfXi-PWRQ-fjcpYY --- Physical volume --- PV Name /dev/sdd VG Name vgroup001 PV Size 2.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 511 Free PE 511 Allocated PE 0 PV UUID HAl141-kQj9-sZyO-v5t2-xpzn-ZPAx-r4Lfkg
As you can see above all Physical Volumes updated with PE (Physical Extent). and all Physical Extent and Size of Physical Volumes are free to create Logical Volumes.
Check quick short details of Physical Volume.
[root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb vgroup001 lvm2 a-- 2.00g 2.00g /dev/sdc vgroup001 lvm2 a-- 2.00g 2.00g /dev/sdd vgroup001 lvm2 a-- 2.00g 2.00g
Create Logical Volumes using lvcreate
We have created Physical Volumes and Volume Group. Now it’s time to create Logical Volume and use that logical volume to store data. Logical Volumes are like normal partitions.
So to create Logical Volume we can use lvcreate command. The syntax to create Logical Volume is :
Syntax : lvcreate -L [Size] -n [Name of the Logical Volume] Volume Group
Here I am creating my first logical volume with size 3 GB with name lvolume001 from Volume Group vgroup001.
[root@localhost ~]# lvcreate -L 3G -n lvolume001 vgroup001 # Create Logical Volume Logical volume "lvolume001" created
To check the Logical Volume details use the command lvdisplay.
[root@localhost ~]# lvdisplay # Check details of Logical Volume --- Logical volume --- LV Path /dev/vgroup001/lvolume001 LV Name lvolume001 VG Name vgroup001 LV UUID kEpMd5-PKhi-2S5W-T8p4-cW1S-dfFS-RR9jB9 LV Write Access read/write LV Creation host, time localhost.localdomain, 2017-06-07 09:07:42 -0700 LV Status available # open 0 LV Size 3.00 GiB Current LE 768 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
You can also quick details of Logical Volumes using lvs command. lvs referred to as Logical Volume Summary.
[root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lvolume001 vgroup001 -wi-a----- 3.00g
After created the first Logical Volume let’s check what changes happened to our Volume Group using vgdisplay command.
As you can see below the total Physical Extent of the volume group is 5.99 GB and the total Physical Extent is 1533 out of which we create a Logical Volume of size 3 GB. so Now available free space is 2.99 GB and the Available Physical Extent is 765.
Available Free Space and PE are highlighted in Blue Color and Utilized Space and PE are highlighted in Red color.
[root@localhost ~]# vgdisplay --- Volume group --- VG Name vgroup001 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 4 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 5.99 GiB PE Size 4.00 MiB Total PE 1533 Alloc PE / Size 768 / 3.00 GiB Free PE / Size 765 / 2.99 GiB VG UUID xn4b80-1CeV-KYJu-1x6Z-ipTQ-2fSZ-axLx3D
Now let’s create another Logical Volume by using the remaining space of Volume Group. as you can see above currently we have 2.99 GB of Space. If you want to know the exact available size then just calculate by using the Available Physical Extent. The calculation would be something like this :
Available PE: 765
PE Size (Size of a single PE) : 4 MB So…
Total Remaining Size is: 765*4=3060 MB
To create our second Logical Volume using the below command. Here I am creating the Second Logical Volume with the name lvolume002.
[root@localhost ~]# lvcreate -L 3060M -n lvolume002 vgroup001 Logical volume "lvolume002" created
Now again check the Volume Group details. As you can see below now we have utilized all PE and Size of the Volume Group and cant create any further Logical Volumes.
Note: If you want to create more Logical Volumes you have to add a new physical hard disk to the physical volume then need to extend the volume group.
[root@localhost ~]# vgdisplay --- Volume group --- VG Name vgroup001 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 5.99 GiB PE Size 4.00 MiB Total PE 1533 Alloc PE / Size 1533 / 5.99 GiB Free PE / Size 0 / 0 VG UUID xn4b80-1CeV-KYJu-1x6Z-ipTQ-2fSZ-axLx3D
To check all available Volume Groups follow the below command.
[root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lvolume001 vgroup001 -wi-a----- 3.00g # Logical Volume 1 lvolume002 vgroup001 -wi-a----- 2.99g # Logical Volume 2
OR for complete descriptive details, you can use the below command.
[root@localhost ~]# lvdisplay
Now we are all set to store data on logical volumes. For that, we have to create a file system in Logical Volume by formatting it like we usually create during normal partition creation. So here I am formatting the first logical volume i.e. lvolume001 by with the ext4 file system. Refer to the command below.
[root@localhost ~]# mkfs.ext4 /dev/vgroup001/lvolume001 # Format the Logical Volume mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 196608 inodes, 786432 blocks 39321 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=805306368 24 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 22 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Then just create a directory for mount the Logical Volume.
[root@localhost ~]# mkdir /lvm
For manual mounting just refer the below command.
[root@localhost ~]# mount /dev/vgroup001/lvolume001 /lvm/
Confirm the mounted device using the below command.
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 2.5G 15G 16% / tmpfs 935M 228K 935M 1% /dev/shm /dev/sda1 291M 39M 238M 14% /boot /dev/mapper/vgroup001-lvolume001 3.0G 69M 2.8G 3% /lvm
For permanent mounting, you have to create an entry in /etc/fstab file. So do the below-highlighted entry in your /etc/fstab file.
[root@localhost ~]# nano /etc/fstab /dev/vgroup001/lvolume001 /lvm ext4 defaults 0 0
Where :
- /dev/vgroup001/lvolume001 – Logical Volume Path/Name
- /lvm : Mount Point
- ext4: File System
To refresh all the mount points refer to the below command.
[root@localhost ~]# mount -a [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 2.6G 15G 16% / tmpfs 935M 228K 935M 1% /dev/shm /dev/sda1 291M 39M 238M 14% /boot /dev/mapper/vgroup001-lvolume001 3.0G 69M 2.8G 3% /lvm
That’s all, In this article, we have explained the How to setup Linux LVM (Logical Volume Manager) Using Pvcreate, Vgcreate, Lvcreate with Example. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.