Automating Logical Volume Manager with Python

Photo by Maria Teneva on Unsplash

From this blog you will come to know about logical volume, how to increase or decrease the volume, automating the process with python.

Logical Volume Manager: - In Linux, Logical Volume Manager 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.

You may get a doubt why to use LVM?

Storage is one of the many components in a computer device, without it we can’t do anything. As storage is only limited to a number for example 10 Gb (Gigabytes). Where there is more information to store our system run out of storage. Then LVM comes into play to increase the volume and sometimes we don’t need the extra storage, then we can reduce.

Process for creating the logical volume

First, we need to create a PV (Physical volume). It is useful to provide a volume group from a storage device.

$ fdisk -l # to view your storage paths

Now we must create a volume group. In this we can combine multiple storage drives to work together and share their storage.

$ vgcreate vg_name /disk_path_1 # for only single drive

here <vg_name> is the name of your volume group

We can use vgdisplay to display the properties of volume group

#For showing the disk information

Now we can create a logical volume from volume group. Let’s say that we have 30Gb of VG from this we can create 20 GB of Logical volume and provide it to our system with the help of a folder.

#For partitioning the above drive

To display the properties of logical volume.

#For displaying the lvm

To use this storage devices we have to format the disk. There are lots of formatting available, here we use ext4.

#Formatting with ext4

Your newly created logical volume has a specific disk path. Commonly it will be as below.

#Here the /disk_path has many names

Or you can also know it by

$ fdisk -l

Finally, we can mount the storage to a folder.

$ mount /dev/mapper/vg_name-lvm_name /myfolder

To extend the logical volume

We can extend the logical volume upto extra 10 GB as we mentioned 30 GB above. Here we can give the storage size to be improved.

#Extending the size of lvm disk

The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on a device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing.

Reduce the logical volume.

First, we want to unmount the storage drive from the folder.

$ umount /dev/vg_name/lvm_name

e2fsck is used to examine ext2/ext3/ext4 filesystems for errors and fsck checks and can optionally repair a Linux filesystem; it is basically a front-end for a range of filesystem checkers (fsck.fstype for example fsck.ext3, fsck.sfx etc) offered under Linux.

As we are doing ext4 formatting we can use e2fsck.

Here we want to reduce 4Gb from the 14 Gb drive. So that our drive size becomes 10Gb.

#Reducing the size of lvm disk

LVM automation with Python

The above process is huge and takes time, living in the fast-paced environment everything is automated and runs in the cloud.

So, we can take scenario where we are running our application in Amazon web services EC2 instance. You can also use any other cloud provider with a ssh key. Redhat OS is mandatory.

I am using python programming language; this script can only run in Linux operating systems as ssh is not supported by Windows.

script running

provide your ec2 instance name and ip, most probably your instance name would be ec2-user.

Options for making our task extremely easy

options for LVM

By default, lvm commands are provided by the RedHat operating system. You can only install the commands in Redhat OS.

mounting lvm to drive

Increasing the drive from 8 Gb to 9 Gb.

Increase storage size

Now our drive size is 9 Gb, I want to reduce 4 Gb of the drive. So, the final drive size is 5Gb.

So, you have learned about LVM, step by step process of creating LVM and using it through a folder.

If you have any doubts or suggestions, please comment below.

Thank you for reading the blog and the python script is provided below.

Aspiring MLOps Engineer, learning opensource tools and contributing. Love to collaborate and share the knowledge. Integrating different technologies.