Automating LVM Partition using Python-Script

Arjun Chauhan
3 min readNov 7, 2020

In the previous article we talked about LVM storage and how it can make things convenient for us. I created a python script that can make our work really simple when creating the LVM storage.

In this article I would be running the script and sharing how it does everything. So this one is going to be brief. I have added the link of the github repository at the end of the article.

I would be explaining the internal working of the script wherever necessary.

The script is being run on RHEL8 on AWS with 2 GB of EBS storage attached to it.

As soon as we run the script it directly installs the lvm2 because it is needed to perform the actions further.

This is so because the lvm commands are not directly supported in RHEL.

Then the user is prompted to tell the name of the storage drive and name the volume group. In the background the pvcreate and vgcreate command are running like the ones we saw last time.

Basically this creates physical volume and volume group and shows the information about the volume group.

Now since everything is created. We can create a new partition. Hence the user can enter the size of the new partition. Then the program formats the new partition and creates the storage.

Hence after this command the storage is ready to be mounted.

Finally the user is asked to specify the directory where he wants to mount the storage. Once he has specified the work of created a LVM storage is done. In future the user just needs to run one command if he needs to increase the storage.

I wrote this article because I wanted to show the power of scripts and how automation can ease the tasks and make the technology availiable for a not so technical user and also for those who want to get a certain task done without having to dwell upon the intricate details.

Because when I wrote time about LVM I realized that not all users can follow the procedures and hence just running a script is much easier and convenient.

The link to the python script is here if you want to have a look or fork it