Sitemap
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Automating LVM Partition using Python-Script

5 min readMar 14, 2021

--

What is LVM?

LVM is a tool for logical volume management which includes allocating disks, striping, mirroring and resizing logical volumes. With LVM, a hard drive or set of hard drives is allocated to one or more physical volumes. LVM physical volumes can be placed on other block devices which might span two or more disks.

The physical volumes are combined into logical volumes, with the exception of the /boot partition. The /boot partition cannot be on a logical volume group because the boot loader cannot read it. If the root (/) partition is on a logical volume, create a separate /boot partition which is not a part of a volume group. Since a physical volume cannot span over multiple drives, to span over more than one drive, create one or more physical volumes per drive.

The volume groups can be divided into logical volumes, which are assigned mount points, such as /home and / and file system types, such as ext2 or ext3. When "partitions" reach their full capacity, free space from the volume group can be added to the logical volume to increase the size of the partition. When a new hard drive is added to the system, it can be added to the volume group, and partitions that are logical volumes can be increased in size.

On the other hand, if a system is partitioned with the ext3 file system, the hard drive is divided into partitions of defined sizes. If a partition becomes full, it is not easy to expand the size of the partition. Even if the partition is moved to another hard drive, the original hard drive space has to be reallocated as a different partition or not used.

It is a system of managing logical volumes or filesystems, that is much more advanced and flexible than the traditional method of partitioning a disk into one or more segments and formatting that partition with a filesystem.

There are 3 concepts that LVM manages:-

  • Physical Volume
  • Volume Groups
  • Logical Volumes

Physical Volumes:- LVM combines physical volumes into storage pools known as volume groups. Volume groups abstract the characteristics of the underlying devices and function as a unified logical device with a combined storage capacity of the component physical volumes.

Volume Groups:- LVM combines physical volumes into storage pools known as volume groups. Volume groups abstract the characteristics of the underlying devices and function as a unified logical device with a combined storage capacity of the component physical volumes.

Get Ananya Sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

Logical Volumes:- A volume group can be sliced up into any number of logical volumes. Logical volumes are functionally equivalent to partitions on a physical disk, but with much more flexibility. Logical volumes are the primary component that users and applications will interact with.

LVM Architecture

Python:-

Python is object-oriented and functional language. It lets programmers decide if functions or classes are better for the needs at hand. This is a major boon for test automation because

(a) Stateless functions avoid side effects.

(b) Simple syntax for those functions make them readable.

Code for Automation:-

import os
import sys
from time import sleep
import getpass as g
import pyfiglet
from termcolor import colored
from pyfiglet import figlet_format
while(1):print("**************************************************************************************")
print((colored(figlet_format("Welcome to LVM Menu","standard"), color='yellow')))
print("**************************************************************************************")
print("1] Check Disk Information")
print("2] Create a Physical Volume")
print("3] Create a Volume Group")
print("4] Create, Format, Mount LVM")
print("5] Extend LVM")
print("6] Exit")
print()
option = input("Select an option: ")
if(option == "1"):
os.system("fdisk -l")
sleep(2)
elif(option == "2"):
disk_name = input("Please spcify the disk name: ")
os.system(f"pvcreate {disk_name}")
sleep(2)
elif(option == "3"):
vgname = input("Name of the Volume Group: ")
disks = input("Please specify all the DiskNames ( with spaces ): ")
os.system(f"vgcreate {vgname} {disks}")
sleep(2)

elif(option == "4"):
vgname = input("Name of the Volume Group: ")
lvmname = input("Name of the LVM: ")
size = input("Enter the size: ")
db=input("Create a Directory:- Name it:")
os.system(f"mkdir /{db}")
mount_point = input("Specify the Mount Point: ")
os.system(f"lvcreate --size {size} --name {lvmname} {vgname}")
os.system(f"mkfs.ext4 /dev/{vgname}/{lvmname}")
os.system(f"mount /dev/{vgname}/{lvmname} {mount_point}")
sleep(2)
elif(option == "5"):
vgname = input("Specify the name of the Volume Group: ")
lvmname = input("Specify the name of the LVM: ")
size = input("Size to be increased: ")
os.system(f"lvextend --size +{size} /dev/{vgname}/{lvmname}")
os.system(f"resize2fs /dev/{vgname}/{lvmname}")
sleep(2)
elif(option == "6"):

print("Exiting LVM Configuration .......Please Wait..............")
sleep(2)
break

Now, let’s run the code and see the result:-

Press enter or click to view image in full size
Menu
Press enter or click to view image in full size
Disk Information
Press enter or click to view image in full size
Physical Volume Created
Press enter or click to view image in full size
Logical Volume Created
Press enter or click to view image in full size
Logical Volume Resized
Press enter or click to view image in full size
Disk Information
Press enter or click to view image in full size
Exit Program

So likewise, we perform automation of LVM partition using Python Script.

Thank you :)

--

--

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com