Cyberithub

How to configure LVM in Linux (pvcreate, vgcreate and lvcreate) using 6 Easy Steps

Advertisements

In this article, we will see how to configure LVM in Linux using 6 Easy Steps. LVM(Logical Volume Manager) is a very important concept to Manage and utilize storage devices effectively. Let's understand the concept of LVM through an example where suppose if you have any two traditional storage devices with size 50 GB each and one of the storage device has 5GB free space and another has 10GB. Lets say you have some file of size 13GB which you want to save in your storage.

Now you cannot save this file in either of the storage devices since one has 5GB free space and another has 10GB. So if you have some mechanism where you can add free spaces from all the storage devices and create a separate storage then you will get 5GB+10GB=15GB. Now you have enough storage to save 13GB of file and hence you have used your free spaces from both the device very effectively. This feature is provided by the LVM where you can take out the storage as you need and then add it back once you are done.

There are additional Operations that can be performed using LVM which we will see in Later Articles. In this article, we will try to see the steps required to Configure LVM.

Some Important Terminology

Extents : It is a logical unit of volume. It is frequently used in LVM Technology to denote the basic unit which constitutes a volume. So a volume is always divided into smaller units of extents.

Logical Extents: Extent used in Logical Volume is known as Logical Extent. If Physical Extent size is 4MB and assigned space is 200MB, then logical extent will be 200MB/4MB = 50. Hence LE will be 50.

Physical Extents: Extent used in Physical Volume is known as Physical Extent. It's size is usually 4MB.

Physical Volume: It is RAW volume used by LVM created by formating any raw disk with LVM partition type.

Volume Group: It is a collection of Physical Volumes.

Logical Volume: It is sizeable space taken out from Volume Group.

 

How to configure LVM in Linux (pvcreate, vgcreate and lvcreate) using 6 Easy Steps 1

Configure LVM in Linux

In this example, we have two disks /dev/sdb and /dev/sdc with 8GB size each. We will use these disks to configure LVM in Linux.

1. Create a Linux LVM Partition 

First you need to create a Linux LVM partition on both the disks using fdisk command as shown below. You can follow the below steps to create partition on both /dev/sdb and /dev/sdc disks. In this step you need to create a partition on /dev/sdb disk and then change it to Linux LVM type by assigning type of partition to 8e and then save and re-sync the partition table by providing w command.

[root@localhost ~]# fdisk -c /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-16777215, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215):
Using default value 16777215
Partition 1 of type Linux and of size 8 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands.For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

2. Create Physical Volume using pvcreate command

Now in next steps to configure LVM you need to create physical volume for both disks /dev/sdb1 and /dev/sdc1 using pvcreate command as shown below.

[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdc1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.

Check the physical volume using pvscan command. This command will scan and detect all the physical volumes created on the System.

[root@localhost ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<39.00 GiB / 4.00 MiB free]
PV /dev/sdb1 lvm2 [<8.00 GiB]
PV /dev/sdc1 lvm2 [<8.00 GiB]
Total: 3 [54.99 GiB] / in use: 1 [<39.00 GiB] / in no VG: 2 [<16.00 GiB]

Also you can display the current physical volume information using pvdisplay command as shown below. This command is very useful in checking the size and other stats before proceeding to configure LVM.

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <39.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 9983
Free PE 1
Allocated PE 9982
PV UUID uJMT8K-6jPs-OeYy-Qt1w-PLdB-P090-NjbkFB

"/dev/sdb1" is a new physical volume of "<8.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <8.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID nDXlXQ-LuKp-d4X0-UPUa-WSnA-BS9c-QTOBZJ

"/dev/sdc1" is a new physical volume of "<8.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size <8.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ZpLhRg-GG6c-PgX0-Vtbj-sKnF-Xvct-OCR8I1

VG Name - Volume Group name(vol_grp).
VG Access - Permissions on Volume Group
VG Status -Current Volume group size
LV Name - Logical volume name(log_grp).
PV Name - Physical Volume Name.
PE Size - Physical Extends Size. Default size is 4MB
Total PE - Total Physical Extent
Free PE - Free Physical Extent
Allocated PE - Allocated Physical Extent
PV UUID - Physical Volume UUID

3. Create Volume Group using vgcreate command

Now you can create Volume Group using both Physical volume disks /dev/sdb1 and /dev/sdc1 as shown below.

[root@localhost ~]# vgcreate vol_grp /dev/sdb1 /dev/sdc1
Volume group "vol_grp" successfully created

Scan the volume group using vgscan command to check if the system is detecting the newly create volume group.

[root@localhost ~]# vgscan
Reading volume groups from cache.
Found volume group "vol_grp" using metadata type lvm2
Found volume group "centos" using metadata type lvm2

Check Volume group stats using vgdisplay command as shown below.

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vol_grp
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 15.99 GiB
PE Size 4.00 MiB
Total PE 4094
Alloc PE / Size 0 / 0
Free PE / Size 4094 / 15.99 GiB
VG UUID 2W12Cx-zE0Q-r35g-tTE4-3vfp-fzE6-5FBRLD

--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <39.00 GiB
PE Size 4.00 MiB
Total PE 9983
Alloc PE / Size 9982 / 38.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID tA52Mr-InE5-O80P-NX7r-XZ35-bONj-FMST8E

4. Create Logical Volume using lvcreate command

Next step in configure LVM storage is to create a logical volume. This is the volume which you are finally going to use as a partition by mounting it to a mount point. You can create a logical volume of size 200 MB.

[root@localhost ~]# lvcreate -L 200M -n log_vol vol_grp
Logical volume "log_vol" created.

-L : Size of Logical Volume

-n : Name of Logical Volume

Run lvscan command to detect the newly created logical volume as shown below.

[root@localhost ~]# lvscan
ACTIVE '/dev/vol_grp/log_vol' [200.00 MiB] inherit
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
ACTIVE '/dev/centos/root' [36.99 GiB] inherit

Check stats using lvdisplay command as shown below.

[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vol_grp/log_vol
LV Name log_vol
VG Name vol_grp
LV UUID JLSCKV-jJTS-VZc5-FeJ3-Rpgi-eDgP-R6H3UG
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-05-05 02:23:33 -0400
LV Status available
# open 0
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID xcPG6O-oaGI-itD2-9LBN-SsvT-IkIt-fzvfGu
LV Write Access read/write
LV Creation host, time localhost, 2020-04-23 12:36:29 -0400
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID fVY6mC-enqD-pSlK-EYlA-Artm-RJU5-Jn0LzT
LV Write Access read/write
LV Creation host, time localhost, 2020-04-23 12:36:30 -0400
LV Status available
# open 1
LV Size 36.99 GiB
Current LE 9470
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

5. Create an EXT4 filesystem using mkfs command

Once the logical volume created from volume group you need to create a filesystem over the volume using mkfs command as shown below. In this example we are using mkfs -t ext4 /dev/vol_grp/log_vol command to create an ext4 filesystem over log_vol.

[root@localhost ~]# mkfs -t ext4 /dev/vol_grp/log_vol
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
51296 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=209715200
7 block groups
32768 blocks per group, 32768 fragments per group
7328 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

-t : types of filesystem

6. Mount the Partition

After formating the logical volume you need to mount it over a mount point. To do that first you need to create a directory. Here we are creating a directory /u01.

[root@localhost ~]# mkdir /u01

Then you need to mount the logical volume log_vol using mount /dev/vol_grp/log_vol /u01 command as shown below.

[root@localhost ~]# mount /dev/vol_grp/log_vol /u01/

Now if you check the mount point, you can see Logical volume is now mounted to /u01 partition. This complete the steps to configure LVM storage and now you can use this storage as any other storage through /u01 mount point.

[root@localhost ~]# df -h
Filesystem                  Size Used  Avail Use% Mounted on
devtmpfs                    484M   0   484M   0%    /dev
tmpfs                       496M   0   496M   0%   /dev/shm
tmpfs                       496M  6.8M 489M   2%    /run
tmpfs                       496M   0   496M   0%  /sys/fs/cgroup
/dev/mapper/centos-root     37G   1.5G 36G    4%      /
/dev/sda1                   1014M 193M 822M   19%   /boot
tmpfs                       100M   0   100M   0%   /run/user/0
/dev/mapper/vol_grp-log_vol 190M  1.6M 175M   1%    /u01

 

 

Popular Recommendations:-

How to Install and Use telnet command in Linux Using 5 Easy Steps

How to Enable or Disable SELinux Temporarily or Permanently on RedHat/CentOS 7/8

10 Popular Examples of sudo command in Linux(RedHat/CentOS 7/8)

9 useful w command in Linux with Examples

12 Most Popular rm command in Linux with Examples

25 Useful Linux SS Command Examples to Monitor Network Connections

Configure LVM in Linux

Configure LVM: Complete Administration

Leave a Comment