Post: How to create LVM
In the previous article, I have explained well about how to create a LVM. In this article, i will show how to extend LVM.
1. To see current LVM in a linux, run the following
[root@localhost ~]# lvscan
ACTIVE '/dev/new_group/new_lvm' [2.00 GiB] inherit
ACTIVE '/dev/rhel/swap' [<3.94 GiB] inherit
ACTIVE '/dev/rhel/root' [<35.06 GiB] inherit
2. Check the current physical volumes labeled.
[root@localhost ~]# pvscan
PV /dev/nvme0n4 VG new_group lvm2 [<3.00 GiB / 1020.00 MiB free]
PV /dev/nvme0n5 VG new_group lvm2 [<3.00 GiB / <3.00 GiB free]
PV /dev/nvme0n6 VG new_group lvm2 [<3.00 GiB / <3.00 GiB free]
PV /dev/nvme0n1p2 VG rhel lvm2 [<39.00 GiB / 0 free]
3. Display the block devices details using lsblk
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
├─nvme0n1p2 259:2 0 39G 0 part
│ ├─rhel-root 253:0 0 35.1G 0 lvm /
│ └─rhel-swap 253:1 0 4G 0 lvm [SWAP]
└─nvme0n1p3 259:3 0 1023.5K 0 part
nvme0n2 259:4 0 5G 0 disk
└─nvme0n2p1 259:5 0 1023M 0 part
nvme0n3 259:6 0 5G 0 disk
└─nvme0n3p1 259:7 0 5G 0 part /mymount1
nvme0n4 259:8 0 3G 0 disk
└─new_group-new_lvm 253:2 0 2G 0 lvm /lvdrive
nvme0n5 259:9 0 3G 0 disk
nvme0n6 259:10 0 3G 0 disk
nvme0n7 259:11 0 3G 0 disk
4. Create a physical volume
[root@localhost ~]# pvcreate /dev/nvme0n7
Physical volume "/dev/nvme0n7" successfully created.
5. Display the volume groups.
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
new_group 3 1 0 wz--n- <8.99g <6.99g
rhel 1 2 0 wz--n- <39.00g 0
6. The new physical volume is added to volume "new_group"
[root@localhost ~]# vgextend new_group /dev/nvme0n7
Volume group "new_group" successfully extended
7. Run vgs again to see the capacity difference after adding new physical volume
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
new_group 4 1 0 wz--n- 11.98g 9.98g
rhel 1 2 0 wz--n- <39.00g 0
8. Run pvscan to display the physical volumes in each volume group
[root@localhost ~]# pvscan
PV /dev/nvme0n4 VG new_group lvm2 [<3.00 GiB / 1020.00 MiB free]
PV /dev/nvme0n5 VG new_group lvm2 [<3.00 GiB / <3.00 GiB free]
PV /dev/nvme0n6 VG new_group lvm2 [<3.00 GiB / <3.00 GiB free]
PV /dev/nvme0n7 VG new_group lvm2 [<3.00 GiB / <3.00 GiB free]
PV /dev/nvme0n1p2 VG rhel lvm2 [<39.00 GiB / 0 free]
Total: 5 [50.98 GiB] / in use: 5 [50.98 GiB] / in no VG: 0 [0 ]
9. Run lvextend to expand the volume group.
In the first command, "lvextend -L3G /dev/new_group/new_lvm"increase total volume to 3GB.
In the second command, "lvextend -L+1G /dev/new_group/new_lvm" increase additional volume 1G to the existing LVM size.
[root@localhost ~]# lvextend -L3G /dev/new_group/new_lvm
Size of logical volume new_group/new_lvm changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).
Logical volume new_group/new_lvm successfully resized.
[root@localhost ~]# lvextend -L+1G /dev/new_group/new_lvm
Size of logical volume new_group/new_lvm changed from 3.00 GiB (768 extents) to 4.00 GiB (1024 extents).
Logical volume new_group/new_lvm successfully resized.
10. Finally run lvdisplay for detailed view of LVM
[root@localhost ~]# lvdisplay /dev/new_group/new_lvm
--- Logical volume ---
LV Path /dev/new_group/new_lvm
LV Name new_lvm
VG Name new_group
LV UUID IdF48L-McZI-inff-3RW0-mNak-ADYO-tVcMxq
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2021-06-12 03:57:35 -0400
LV Status available
# open 1
LV Size 4.00 GiB
Current LE 1024
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
How to Shrink LVM
lvreduce command is used to shrink the size of LVM
In this command, i reduce the entire LVM size to 1 GB.
[root@localhost ~]# lvreduce -L 1GB /dev/new_group/new_lvm
WARNING: Reducing active and open logical volume to 1.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce new_group/new_lvm? [y/n]: y
Size of logical volume new_group/new_lvm changed from 4.00 GiB (1024 extents) to 1.00 GiB (256 extents).
Logical volume new_group/new_lvm successfully resized.
2. But if you only what to reduce 1 GB from total, then run '-' sign before the size value.
[root@localhost ~]# lvreduce -L -1GB /dev/new_group/new_lvm
WARNING: Reducing active and open logical volume to 2.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce new_group/new_lvm? [y/n]: y
Size of logical volume new_group/new_lvm changed from 3.00 GiB (768 extents) to 2.00 GiB (512 extents).
Logical volume new_group/new_lvm successfully resized.
Σχόλια