How to create LVM partition in linux

LVM stands for Logical Volume Manager, its a disk management tool which is used to create LVM partition in Linux. We can create , modify and resizing disk partitions on the fly without any downtime. Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. This tutorial can be used to create lvm partition on Centos / Rhel and other flavours of linux.

Identify the correct disk that needs to be used for creating LVM partition in linux

We will use lsblk utility to identify the disk that needs to be used for LVM.

[root@master ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom  
vda             252:0    0   50G  0 disk 
├─vda1          252:1    0    1G  0 part /boot
└─vda2          252:2    0   49G  0 part 
  ├─centos-root 253:0    0 45.1G  0 lvm  /
  └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
vdb             252:16   0  100G  0 disk 

From the output, we can see that vdb is the new disk and it doesn’t have any partitions created.

Create Physical Volume (PV) on the new disk

[root@master ~]# pvcreate /dev/vdb
  Physical volume "/dev/vdb" successfully created.

Lets verify the new PV using either of the command

[root@master ~]# pvscan 
  PV /dev/vda2   VG centos          lvm2 [<49.00 GiB / 4.00 MiB free]
  PV /dev/vdb                       lvm2 [100.00 GiB]
  Total: 2 [<149.00 GiB] / in use: 1 [<49.00 GiB] / in no VG: 1 [100.00 GiB]
[root@master ~]# pvdisplay /dev/vdb
  "/dev/vdb" is a new physical volume of "100.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb
  VG Name               
  PV Size               100.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               776fGz-PX61-4Clc-oC04-7717-BLel-nrnZaQ

Create Volume Groups (VG) on the Physical Volume

Let us create volume group myvg on the physical volume

[root@master ~]# vgcreate myvg /dev/vdb
  Volume group "myvg" successfully created

We can view all the information related to “myvg” volume group using below command

[root@master ~]# vgdisplay -v myvg
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               <100.00 GiB
  PE Size               4.00 MiB
  Total PE              25599
  Alloc PE / Size       0 / 0   
  Free  PE / Size       25599 / <100.00 GiB
  VG UUID               ZLx5FD-71Vo-C32i-nLmm-62Ww-NNII-ERAxpK
   
  --- Physical volumes ---
  PV Name               /dev/vdb     
  PV UUID               776fGz-PX61-4Clc-oC04-7717-BLel-nrnZaQ
  PV Status             allocatable
  Total PE / Free PE    25599 / 25599

Create Logical Volume (LV) on the Volume Group

Once our volume groups is created, now we can create the logical volume (LV) on “myvg” volume group

[root@master ~]# lvcreate -L 30G -n datavol1 myvg
  Logical volume "datavol1" created.

Logical volume datavol1 has been created.

  • switches used:
  • -L -> disk size
  • -n -> new logical volume

Creating FileSystem for logical volume

We need to create filesystem to use the new logical volume.

[root@master ~]# mkfs.xfs /dev/myvg/datavol1 
meta-data=/dev/myvg/datavol1     isize=512    agcount=4, agsize=1966080 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=7864320, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=3840, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

We need to mount the new xfs logical volume

[root@master ~]# mount /dev/myvg/datavol1  /mnt

[root@master ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   1.9G     0  1.9G   0% /dev
tmpfs                      1.9G     0  1.9G   0% /dev/shm
tmpfs                      1.9G   11M  1.9G   1% /run
tmpfs                      1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root     46G  4.6G   41G  11% /
/dev/vda1                 1014M  185M  830M  19% /boot
tmpfs                      379M  4.0K  379M   1% /run/user/989
tmpfs                      379M   44K  379M   1% /run/user/1000
tmpfs                      379M     0  379M   0% /run/user/0
/dev/mapper/myvg-datavol1   30G   33M   30G   1% /mnt

We need to add the new Logical volume in /etc/fstab for permanent mounting so that it is available even after system reboot

[root@master ~]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Mar 15 09:27:55 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=e6885093-abc1-48af-aaf5-db222df6e0f2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/myvg/datavol1 /mnt xfs defaults 0 0

Conclusion

We have successfully setup LVM using a new disk and created a xfs logical volume on top of it.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments