LVM – How to extend a partition using Logical Volume Manager

ComputerCarriage > Posts > Howto > LVM – How to extend a partition using Logical Volume Manager
lvm

LVM

LVM – In this blog we are going to see how to  extend a partition using the Logical Volume Manager(LVM)
The Logical Volume Manager (LVM) provides tools to create virtual block devices from physical devices. Virtual devices are easier to manage, flexible than the physical partitions.

LVM has the features like Extend / Reduce / migrate the partition. Volume Group (VG) can have more than one Physical Device, each called a Physical Volume (PV). In a VG, one or more LV’s can be created in the Volume Group.

1. Display Information(LVM)

a. Physical Volume

The pvs command used to display the physical volumes that are already added

[root@control opt]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 30.71g 0

b. Volume Groups

vgs command will show already existing volume groups and sizes.

[root@control opt]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 30.71g 0

c. Logical Volume

lvs command will show the existing lv’s that are present in the node

[root@control opt]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- <28.55g
swap cl -wi-ao---- <2.17g
database_lv database_vg -wi-ao---- 1.99g

3. List the block disk

lsblk command will list the block devices

[root@control ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 31.7G 0 disk
|-sda1 8:1 0 1G 0 part /boot
-sda2 8:2 0 30.7G 0 part |-cl-root 253:0 0 28.6G 0 lvm / -cl-swap 253:1 0 2.2G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
sr0 11:0 1 1024M 0 rom

4. Adding New Disk

Consider if a new 1 GB disk has been added in the server

[root@control ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 31.7G 0 disk
|-sda1 8:1 0 1G 0 part /boot
-sda2 8:2 0 30.7G 0 part |-cl-root 253:0 0 28.6G 0 lvm / -cl-swap 253:1 0 2.2G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
`-sdb1 8:17 0 1023M 0 part
sdc 8:32 0 1G 0 disk
sr0 11:0 1 1024M 0 rom

In the /dev/sdb physical device there is no lvm partitions

5. Adding the new disk to physical volume

pvcreate command will add the raw disk to the physical volume.

[root@control ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.

6. Create a new volume group

Using vgcreate we are going to create and add PV disk to the appgroup_vg

[root@control ~]# vgcreate backup_vg /dev/sdb1
Volume group "appgroup_vg" successfully created

a. Listing the volume group

vgs command will list the volume group that are created

[root@control ~]# vgs
VG #PV #LV #SN Attr VSize VFree
appgroup_vg 1 0 0 wz--n- 1020.00m 1020.00m
cl 1 2 0 wz--n- 30.71g 0

now you can see appgroup_vg is created

7. Creating a Logical volume

We are going new logical volume using lvcreate command, since we are going to create all the available space, we will use 100%FREE to the appfs_lv

[root@control ~]# lvcreate -l+100%FREE backup_vg -n appfs_lv
Logical volume "appfs_lv" created.

Now we can see new logical volume has been created

a. Check the logical volume

lvs command will  list the available logical volume, we can see appfs_lv was created with all available space in the appgroup_vg

[root@control ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
backup_lv backup_vg -wi-a----- 1020.00m
root cl -wi-ao---- <28.55g
swap cl -wi-ao---- <2.17g

8. Make a entry in fstab

By adding the entry in /etc/fstab entry for the filesystem to mount during the system boot. use the following command.

echo "/dev/appgroup_vg/appfs_lv /app ext4 defaults 0 0" >> /etc/fstab

9. Create filesystem

The two most commonly used filesystem is linux are xfs and ext4, according to your requirements you can format the partition as ext4 or xfs

[root@control ~]# mkfs.ext4 /dev/appgroup_vg/appfs_lv
mke2fs 1.44.6 (5-Mar-2019)
Creating filesystem with 261120 4k blocks and 65280 inodes
Filesystem UUID: 9e6f3940-cc96-4e61-8ef9-5b586d121fb4
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

After successful creation of filesystem, we need to create the mount point for the filesystem, for example we are going to create /app folder as a mount point,

a. Create a mount directory

[root@control ~]# mkdir /app

b. Mounting the filesystem using mount command

[root@control ~]# mount /app

or

[root@control ~]# mount -t ext4 /dev/appgroup_vg/appfs_lv /app

c. Check the mounted filesystem

Check the partition is mounted properly

[root@control ~]# df -Th /app
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/appgroup_vg-appfs_lv ext4 988M 2.6M 919M 1% /app

In the above output we could see the filesystem type is ext4, and Size is 988MB and mounted in /backup

-T option is used for display filesystem type

-h is human readable size like displays MB,GB,TB

Hope you got the information that you are looking for, in case any issues please let us know. thanks in advance.

Also read lsblk command useful information https://computercarriage.com/2020/05/18/list-block-devices-using-lsblk-command/

Refer Redhat Official docs for more information on LVM https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/4/html/cluster_logical_volume_manager/lvm_cli

5 thoughts on “LVM – How to extend a partition using Logical Volume Manager

Leave a Reply