Elasticity in Storage Using LVM

Sarthak Agarwal
5 min readMar 11, 2021

What is LVM?

In Linux, Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. Most modern Linux distributions are LVM-aware to the point of being able to have their root file systems on a logical volume.

Uses of LVM

LVM is used for the following purposes 👉

🌟Creating single logical volumes of multiple physical volumes or entire hard disks (somewhat similar to RAID 0, but more similar to JBOD), allowing for dynamic volume resizing.
🌟Managing large hard disk farms by allowing disks to be added and replaced without downtime or service disruption, in combination with hot swapping.
🌟On small systems (like a desktop), instead of having to estimate at installation time how big a partition might need to be, LVM allows filesystems to be easily resized as needed.
🌟Performing consistent backups by taking snapshots of the logical volumes.
🌟Encrypting multiple physical partitions with one password.

LVM can be considered as a thin software layer on top of the hard disks and partitions, which creates an abstraction of continuity and ease-of-use for managing hard drive replacement, repartitioning and backup.

Volume Groups (VG)

Physical volumes are combined into volume groups (VGs). This creates a pool of disk space out of which logical volumes can be allocated.

Within a volume group, the disk space available for allocation is divided into units of a fixed-size called extents. An extent is the smallest unit of space that can be allocated. Within a physical volume, extents are referred to as physical extents.

A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents.

Physical Volumes (PV)

The underlying physical storage unit of an LVM logical volume is a block device such as a partition or whole disk. To use the device for an LVM logical volume, the device must be initialized as a physical volume (PV). Initializing a block device as a physical volume places a label near the start of the device.

By default, the LVM label is placed in the second 512-byte sector. You can overwrite this default by placing the label on any of the first 4 sectors when you create the physical volume. This allows LVM volumes to co-exist with other users of these sectors, if necessary.

An LVM label provides correct identification and device ordering for a physical device, since devices can come up in any order when the system is booted. An LVM label remains persistent across reboots and throughout a cluster.

The LVM label identifies the device as an LVM physical volume. It contains a random unique identifier (the UUID) for the physical volume. It also stores the size of the block device in bytes, and it records where the LVM metadata will be stored on the device.

The LVM metadata contains the configuration details of the LVM volume groups on your system. By default, an identical copy of the metadata is maintained in every metadata area in every physical volume within the volume group. LVM metadata is small and stored as ASCII.

Currently LVM allows you to store 0, 1 or 2 identical copies of its metadata on each physical volume. The default is 1 copy. Once you configure the number of metadata copies on the physical volume, you cannot change that number at a later time. The first copy is stored at the start of the device, shortly after the label. If there is a second copy, it is placed at the end of the device. If you accidentally overwrite the area at the beginning of your disk by writing to a different disk than you intend, a second copy of the metadata at the end of the device will allow you to recover the metadata.

Configuring and Managing LVM Logical Volumes 👉

Step1: An external virtual hard-disk named as rhel8_arth Clone_1.vdi of 4GB is attached to the Slave Node VM

To see all the mounts 👉

Step2: Create the Partition and Format the newly attached disks by command

#parted /dev/sdb

Again we check the mounts here👉

To format 👉

Step3: Create the Physical Volume by command 👉

#pvcreate /dev/sdb1
#pvcreate /dev/sdb2

To display the new physical volume run the command 👉

#pvdisplay /dev/sdb1
#pvdisplay /dev/sdb2

Step4: Now create the Volume Group (VG) by the command 👉

#vgcreate <name of VG> /dev/sdb1 /dev/sdb2

To display the volume group use

# vgdisplay <name of VG>

Step5: Create the Logical Volume(LV) by command 👉

#lvcreate --size 1G --name <name of LV> <name of VG>

For checking the LV created 👉

#lvdisplay <name of VG>/<name of LV>

Step6: Format the created Logical Volume and attach it tot the /dn directory.

To format we use 👉

#mkfs.ext4 /dev/Myvolg/Mylv

Step7: To increase or extend the size of the datanode we use the command 👉

#lvextend --size +2G /dev/Myvolg/Mylv

To resize 👉

#resize2fs /dev/Myvolg/Mylv

Finally To check files attached use the command 👉

#df -h

As u can see that the Volume is attached successfully 💯

Hope you like the blog 😊

Thanks for reading 🙏

--

--

Sarthak Agarwal

Cloud & DevOps Enthusiast ★ARTH Learner ★ AWS ★ GCP ★ Jenkins ★ K8S ★ Ansible ★ MLOps ★ Terraform ★ Networking ★ Python