top of page
Writer's pictureMukesh Chanderia

Linux Commands Part 20 - LVM : Logical Volume Manager

Updated: Aug 21, 2021

Hello Friends,


Let us first understand What is LVM & Why it is required.


Suppose you have four hard disk of size 30 GB each and you require to create six hardisk of 20 GB.


So we can only create one partition from each hard disk and each disk is left with 10 GB.


To utilize 10 GB volume we have to create logical virtual group (VG) of hard disk by adding all four hard disk.


So now total volume will be 30*4=120 GB and thus total 6 logical volume (LV) of 20 GB each can be created.


Now this logical volume can be named as per wish say HR Team.


One VG and LV along with PV (Physical Volume) is created by default.


Step 1) Add two physical hardisk.


Now list block (lsblk) to see how many physical disks are present.


[root@RHEL2 admin123]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

sda 8:0 0 64G 0 disk

sdc 8:32 0 9G 0 disk

sdd 8:48 0 6G 0 disk


Step 2) Create Physical Volume (PV) with sdc & sdd


[root@RHEL2 admin123]# pvcreate /dev/sdc

Physical volume "/dev/sdc" successfully created.


[root@RHEL2 admin123]# pvcreate /dev/sdd

Physical volume "/dev/sdd" successfully created.


Step 3) List all Physical Volume


[root@RHEL2 admin123]# pvs

PV VG Fmt Attr PSize PFree

/dev/sda2 rootvg lvm2 a-- <63.02g <40.02g


/dev/sdc lvm2 --- 9.00g 9.00g

/dev/sdd lvm2 --- 6.00g 6.00g


Step 4) Now create Virtual Group "Mukesh"


[root@RHEL2 admin123]# vgcreate Mukesh /dev/sdc

Volume group "Mukesh" successfully created


Step 5) Verify Virtual Group


[root@RHEL2 admin123]# vgs

VG #PV #LV #SN Attr VSize VFree

Mukesh 1 0 0 wz--n- <9.00g <9.00g

rootvg 1 5 0 wz--n- <63.02g <40.02g


Step 6) Now add /dev/sdd of 6 GB in VG "Mukesh" it's size must be increased to 9+6=15 GB


[root@RHEL2 admin123]# vgextend Mukesh /dev/sdd

Volume group "Mukesh" successfully extended


[root@RHEL2 admin123]# vgs

VG #PV #LV #SN Attr VSize VFree

Mukesh 2 0 0 wz--n- 14.99g 14.99g

rootvg 1 5 0 wz--n- <63.02g <40.02g


Step 7) Re - Verify Virtual Group


[root@RHEL2 admin123]# vgs

VG #PV #LV #SN Attr VSize VFree

Mukesh 2 0 0 wz--n- 14.99g 14.99g

rootvg 1 5 0 wz--n- <63.02g <40.02g


So we can see that physical volumes /dev/sdc & /dev/sdd are part of LV "Mukesh"


[root@RHEL2 admin123]# pvs

PV VG Fmt Attr PSize PFree

/dev/sda2 rootvg lvm2 a-- <63.02g <40.02g

/dev/sdc Mukesh lvm2 a-- <9.00g <9.00g

/dev/sdd Mukesh lvm2 a-- <6.00g <6.00g


Step 8) Create logical volume


[root@RHEL1 admin123]# lvcreate -L 6G -n LV1 Mukesh

Logical volume "LV1" created.

[root@RHEL1 admin123]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert

LV1 Mukesh -wi-a----- 6.00g

homelv rootvg -wi-ao---- 1.00g


Step 9) Format this logical volume "LV1"


[root@RHEL1 admin123]# mkfs -t ext4 /dev/Mukesh/LV1

mke2fs 1.45.4 (23-Sep-2019)

Discarding device blocks: done

Creating filesystem with 1572864 4k blocks and 393216 inodes

Filesystem UUID: 2827c612-b8a1-4c91-b3ab-776ab202bab8

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736


Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done


Step 10) Check this lsblk


bash: i: command not found

[root@RHEL1 admin123]# blkid | grep LV1

/dev/mapper/Mukesh-LV1: UUID="2827c612-b8a1-4c91-b3ab-776ab202bab8" TYPE="ext4"


Step 11) Mount this logical volume on dir "M"


[root@RHEL1 ~]# mkdir M

[root@RHEL1 ~]# mount /dev/mapper/Mukesh-LV1 M or mount /dev/Mukesh/LV1 M


Step 12) Partition has been mounted


[root@RHEL1 ~]# df -h | grep /root/M

/dev/mapper/Mukesh-LV1 5.9G 24M 5.6G 1% /root/M


Step 13) Extend this logical partition "LV1" by 3 GB and it must become 9GB


[root@RHEL1 ~]#

lvextend -L +3G /dev/Mukesh/LV1

or

lvextend -L 9G /dev/Mukesh/LV1

Size of logical volume Mukesh/LV1 changed from 6.00 GiB (1536 extents) to 9.00 GiB (2304 extents).

Logical volume Mukesh/LV1 successfully resized


Step 14) Verify : Changed in logical volume but not in physical volume


[root@RHEL1 ~]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert

LV1 Mukesh -wi-ao---- 9.00g


[root@RHEL1 ~]# df -h | grep /root/M

/dev/mapper/Mukesh-LV1 5.9G 24M 5.6G 1% /root/M


Step 15 ) Resize "LV1"


[root@RHEL1 ~]# resize2fs /dev/Mukesh/LV1

resize2fs 1.45.4 (23-Sep-2019)

Filesystem at /dev/Mukesh/LV1 is mounted on /root/M; on-line resizing required

old_desc_blocks = 1, new_desc_blocks = 2

The filesystem on /dev/Mukesh/LV1 is now 2359296 (4k) blocks long.


[root@RHEL1 ~]# df -h | grep /root/M

/dev/mapper/Mukesh-LV1 8.8G 27M 8.4G 1% /root/M


Step 16 ) You can also Do resize while extending volume "-r"


[root@RHEL1 ~]# lvextend -L +2G -r /dev/Mukesh/LV1

Size of logical volume Mukesh/LV1 changed from 9.00 GiB (2304 extents) to 11.00 GiB (2816 extents).

Logical volume Mukesh/LV1 successfully resized.

[root@RHEL1 ~]# lvs

LV1 Mukesh -wi-ao---- 11.00g

[root@RHEL1 ~]# df -h | grep /root/M

/dev/mapper/Mukesh-LV1 11G 30M 11G 1% /root/M


Step 17 ) Reduce Logical Volume by 3 GB so that it would become 8 GB


[root@RHEL1 ~]#

lvreduce -L -3G -r /dev/Mukesh/LV1

or

lvreduce -L 8G -r /dev/Mukesh/LV1


Do you want to unmount "/root/M" ? [Y|n] y

fsck from util-linux 2.32.1


/dev/mapper/Mukesh-LV1: 11/720896 files (0.0% non-contiguous), 69312/2883584 blocks

resize2fs 1.45.4 (23-Sep-2019)

Resizing the filesystem on /dev/mapper/Mukesh-LV1 to 2097152 (4k) blocks.

The filesystem on /dev/mapper/Mukesh-LV1 is now 2097152 (4k) blocks long.


Size of logical volume Mukesh/LV1 changed from 11.00 GiB (2816 extents) to 8.00 GiB (2048 extents).

Logical volume Mukesh/LV1 successfully resized.


Step 18 ) Verify


[root@RHEL1 ~]# df -h | grep /root/M

/dev/mapper/Mukesh-LV1 7.9G 27M 7.5G 1% /root/M

[root@RHEL1 ~]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert

LV1 Mukesh -wi-ao---- 8.00g


Step 19 ) Use Resize for both increase and decrease


[root@RHEL1 ~]# lvresize -L 10G -r /dev/Mukesh/LV1

Do you want to unmount "/root/M" ? [Y|n] y

fsck from util-linux 2.32.1


/dev/mapper/Mukesh-LV1: 11/720896 files (0.0% non-contiguous), 69312/2883584 blocks

resize2fs 1.45.4 (23-Sep-2019)

Resizing the filesystem on /dev/mapper/Mukesh-LV1 to 2621440 (4k) blocks.

The filesystem on /dev/mapper/Mukesh-LV1 is now 2621440 (4k) blocks long.


Size of logical volume Mukesh/LV1 changed from 11.00 GiB (2816 extents) to 10.00 GiB (2560 extents).

Logical volume Mukesh/LV1 successfully resized.

[root@RHEL1 ~]# df -h | grep /root/M

/dev/mapper/Mukesh-LV1 9.8G 27M 9.3G 1% /root/M



Step 20 ) To remove /sdd from Volume group "Mukesh"


[root@RHEL1 ~]#vgreduce Mukesh /dev/sdd



13 views0 comments

Commentaires


bottom of page