How To Create and Extend logical volumes by using LVM on Debian 11

To Create and Extend logical volumes by using LVM on Debian 11.3

Introduction:

In Linux, the Logical Volume Manager (LVM) is a tool for virtualizing disks. It can create "virtual" hard drive partitions, which can be expanded, shrunk, or moved. It also allows you to use multiple small disks to create larger partitions.

Prerequisite: lvm2 package.

Step 1: Check the installed OS version by using the below command

[root@linuxhelp ~]# lsb_release-a
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye

Step 2: List all physical disk available in system by using the below command

[root@linuxhelp ~]# fdisk -l | grep /dev/sd

Step 3: Open fdisk to create partition on /dev/sda disk by using the below command

[root@linuxhelp ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.23.2).
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5GiB is set
Command (m for help): wq

Selected partition 1
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Step 4: Create physical volume in /dev/sdb1 drive by using the below command

[root@linuxhelp ~]# pvcreate /dev/sda1
  Physical volume "/dev/sda1" successfully created.

Step 5: List the physical volume by using the below command

[root@linuxhelp ~]# pvs

Step 6: Create the volume group named “myvg” with PVs contained by using the below command

[root@linuxhelp ~]# vgcreate myvg /dev/sda1
  Volume group "myvg" successfully created

Step 7: Create logical volume named “mylv” with 1GB size in volume group “myvg” by using the below command

[root@linuxhelp ~]# lvcreate -L 1G -n mylv myvg
  Logical volume "mylv" created.

Step 8: View the volume group with details by using the below command

[root@linuxhelp ~]# lvdisplay myvg

Step 9: Format the logical volume “mylv” to ext4 file system by using the below command

[root@linuxhelp ~]# mkfs.ext4 /dev/myvg/mylv
mke2fs 1.45.6 (20-jun-2022)
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: ad44c4a1-6c8b-4335-96fe-56e68916c4b4
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Step 10: Mount the logical volume “mylv” to the directory /mnt by using the below command

[root@linuxhelp ~]# mount /dev/myvg/mylv /mnt

Step 11: View the mounted status by using the below command

[root@linuxhelp ~]# df -h

Step 12: Open the fdisk to create partions on /dev/sdc disk by using the below command

[root@linuxhelp ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5GiB is set
Command (m for help): wq

Selected partition 1
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Step 13: Extend the volume group with “/dev/sdc1” drive by using the below command

[root@linuxhelp ~]# Vgextend  myvg /dev/sdc1
Physical  volume “/dev/sdc1 “ successfully created.
Volume Group “myvg” successfully extented

Step 14: List the volume group by using the below command

[root@linuxhelp ~]#vgs
VG	#PV	#LV	#SN	ATTR	Vsize	Vfree
Myvg	2	1	0	wn—n-	6.99g	5.99g

Step 15: Extend the “mylv” logical volume by 5GB using the below command

[root@linuxhelp ~]# lvextend -L +5G /dev/myvg/mylv
Logical Volume myvg/mllv Successfully resized.

Step 16: Use resize2fs command to change effect on logical volume by using the below command

[root@linuxhelp ~]# resizefs /dev/myvg/mylv

resize2fs 1.46.2 (28-feb-2022)

Step 17: List the mounted drives to see the changes in memory by using the below command

[root@linuxhelp ~]# fdisk -l

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to Create and Extend logical volumes by using LVM on Debian 11.3. Your feedback is much welcome.

FAQ
Q
How to Format the logical volume File System?
A
Mkfs.ext4 /dev/
Q
What are Logical Volumes?
A
Logical Volumes are composed of a group of PV's and LV’s. It is the organizational group for LVM.
Q
What are volume Groups?
A
Volume Groups are user-defined groups of drives
Q
What is Physical Volume?
A
Physical volumes (PVs) are which can be either hard disks, hard disk partitions
Q
What is logical volume management (LVM)?
A
Logical Volume Management provides a method of elastic storage space on mass-storage devices that is more flexible than conventional partitioning schemes.