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

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:
- To create PV (Physical Volume)
- To create a volume group
- To Create Logical Volumes
- To create a new partition using fdisk -l
- To mount the newly created partition
- to display Volume Group
- To display Logical Volume
- To display all hard-disks connected to the system
- 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: ")
breakelif(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




