Using Python to automate LVM-Partitioning

What is LVM?

Logical Volume Manager (or.. LVM) is a Device Mapper that acts as a Logical Volume manager for a Linux system. Most modern Linux distros are LVM-ready to the point that they are able to have their root file-systems on a LV.

Allocating storage divisions within a single or multiple storage devices. Ex: C: drive ,D: drive and E: drive

🔹LVM can be used to create, deleted and resized logical volumes ,online, without any restarts.

🔹Logical volumes can also be resized dynamically

🔹LVM allows the creation of instant logical volume snapshots while the operating system is running and supports advanced encryption features.

There are 3 concepts that LVM manages:

1️⃣ Physical Volume: A physical volume is a collection of disk partitions used to store all server data.

2️⃣ Volume Groups: is a collection of physical volumes of varying sizes and types.

3️⃣ Logical Volumes: are groups of information located on physical volumes.

Task Objective:

🔹 Automating LVM Partition using Python-Script


I’ve Imported OS , using os.system() we can run any system/os commands .

Making this procedure much easier than any other way

→ Inorder to use a disks in VG , we have to label them as LVM physical volumes with the pvcreate cmd.

pvcreate <disk_name1> <disk_name2> <disk_nameN>

→ For creating a volume group that consists of the LVM physical volumes you have created. to creates the volume group we use cmd : new_vol_group.

vgcreate new_vol_group <disk_name1> <disk_name2> <disk_nameN>

→ For creating the logical volume from the volume group you have created. The following command creates the logical volume new_logical_volume from the volume group new_vol_group .

lvcreate --size <size> --name <new_logical_volume> <new_vol_group>

→ For creating a file system on the logical volume. To create an ext4 file-system on the logical volume we use:

mkfs.ext4 /dev/new_vol_group/new_logical_volume

→For mounting the logical volume to the folder /test

mount /dev/new_vol_group/new_logical_volume /test

→For increasing the size of the lvm by size of our choice.

lvextend --size <size> /dev/new_vol_group/new_logical_volume

→ Format the extended part using the below command.

resize2fs /dev/new_vol_group/new_logical_volume

These have been used to create a CLI-Menu for LVM :

This will display disk info
Using this, we can create new PV
Creates a VG
Creating a new LVM
Expanding it
Checking its existence

GitHub Repo:

Thank you for the time~!