Let’s automate the LVM partition with a Python script.

Adarsh Saxena
3 min readMar 14, 2021

What is LVM?

In Linux, Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel.

Goal of Program

To create a python script that can automate the LVM partitioning and related stuff. In the program, we’ll create a menu that will do the following things:

  1. To create PV (Physical Volume)
  2. To create a volume group
  3. To Create Logical Volumes
  4. To create a new partition using fdisk -l
  5. To mount the newly created partition
  6. to display Volume Group
  7. To display Logical Volume
  8. To display all hard-disks connected to the system
  9. exit

Head over to the program:

import osos.system("tput setaf 6")
print("\t\t\tLVM Automation Program")
mycheck=True
mayexit=True
while(mayexit):
os.system("tput setaf 3")
print("\n")
print("")
print("\t1. To create PV (Physical Volume) ")
print("\t2. To create a volume group")
print("\t3. To Create Logical Volumes")
print("\t4. To create a new partition using fdisk -l")
print("\t5. To mount the all partition")
print("\t6. To Display Volume Group")
print("\t7. To Display Logical Volume")
print("\t8. All Hard-Disk List Connected To System")
print("\t9. Exit")
print("")
mycheck=True
os.system("tput setaf 7")
print("\n")

a=int(input("Select any option from the above menu"))
while(mycheck==True):
if(a==1):
os.system("fdisk -l")
c=input("Enter the device name : ")
os.system("pvcreate {}".format(c))
print("physical volume created")
break
elif(a==2):
vgname=input("Enter the volume group name: ")
parti = input("enter the storage device: ")
os.system("vgcreate {} {}".format(vgname,parti))
print("\nvolume group created\n\n")
os.system("vgdisplay {}".format(vgname))
print()
break
elif(a=3):
lvmsize=int(input("Enter the logical volume size you want to create : "))
lvmname=input("Enter the name of logical volume: ")
print("creating the logical volume of size-{} GIB with name- {}".format(lvmsize,lvmname))
os.system("lvcreate --size {}G --name {} {}".format(lvmsize,lvmname,vgname))
print("\tLVM of size-{}GB with name- {} created successfully".format(lvmsize,lvmname))
os.system("lvdisplay")
print()
input("Press enter for LVM menu: ")
break
elif(a==4):
parti = input("Enter the device name for which you want to create the partition: ")
os.system("fdisk {}".format(parti))
input("press enter to continue")
break
elif(a==5):
os.system("df -h")
input("Press enter to continue")
break
elif(a==6):
os.system("vgdisplay")
input("Press enter to continue")
break
elif(a==7):
os.system("lvdisplay")
input("Press enter to continue")
break
elif(a==8):
os.system("fdisk -l")
input("Press enter to continue")
break
elif(a==9):
mayexit=False
break
Start of the program
After selecting the option number 6
On selecting the option 7 from menu (display logical volume)
On selecting the option number 8 from the menu
on exit

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Adarsh Saxena
Adarsh Saxena

Written by Adarsh Saxena

Hey Everone, I am DevOps Practitioner, Cloud Computing, BigData, Machine Learning are my favorite parts. Connect me on LinkedIn to know more about me.

No responses yet

Write a response