Automating LVM Partition using Python Script

Hello Fellow Programmers !!!

Today I would to share how LVM(Logical Volume Management) could automated using Python Script.

Content of this Blog

  • Basic Concepts
  • Setup Procedure

Basic Concepts

Before understanding procedure required for the setup, let’s understand the basic terminologies regarding the same:

Physical Volume (PV) : A Physical Volume is a collection of disk partitions used to store all server data.

Volume Group (VG) : Volume Group is created when multiple physical volumes are combined to create a single storage structure, equal to the storage capacity of the combined physical devices.

Logical Volume (LV) : Logical Volume is defined as subdivisions of Volume Group .

Setup Procedure

Procedure

Additional Storage

  • Right click on VM, go to Settings -> Storage
  • Click on Controller : SATA, and create new Hard Disk
  • Select “VDI(VirtualBox Disk Image)” in the Hard disk file type
  • Select “Dynamically allocated” on Storage on physical hard disk
  • Select the size as per requirement in File location and size
  • Choose the Hard Disk created to be attached to the VM

LVM Made Easy (Python Script)

import osprint("#################################################################")
print(" LVM Made Easy ")
print("#################################################################")
print("\t\t\t")while True:
print("""
1. Check Disk Partitions
2. Create Physical Volume
3. Create Volume Group
4. Create Logical Volume
5. Extend Logical Volume Size
6. Reduce Logical Volume Size
7. Check Physical Volume
8. Check Volume Group
9. Check Logical Volume
10. Remove Physical Volume
11. Remove Volume Group
12. Remove Logical Volume
13. Exit
""")
print("Enter your choice : ", end="")
choice=input()
if int(choice) == 1:
os.system("fdisk -l")
elif int(choice) == 2:
print("Enter disk name")
disk_name=input()
os.system("pvcreate {}".format(disk_name))
elif int(choice) == 3:
print("Enter PV(s) name (space between them)")
pv_name=input()
print("Enter Volume Group")
vg_name=input()
os.system("vgcreate {0} {1}".format(vg_name, pv_name))
elif int(choice) == 4:
print("Is mount directory present already(Y/N)?")
response=input()
if response == 'Y':
print("Enter mount directory with the path")
directory=input()
print("Enter Logical Volume")
lv_name=input()
print("Enter Volume Group")
vg_name=input()
print("Enter the LV size with units(K,M,G,T,P)")
size=input()
os.system("lvcreate --size {0} --name {1} {2}".format(size, lv_name, vg_name))
os.system("mkfs.ext4 /dev/{0}/{1}".format(vg_name, lv_name))
os.system("mount /dev/{0}/{1} {2}".format(vg_name, lv_name, directory))
os.system("echo mount /dev/{0}/{1} {2} >> /etc/rc.d/rc.local".format(vg_name, lv_name, directory))
elif response == 'N':
print("Enter mount directory to be created with the path")
directory=input()
os.system("mkdir {}".format(directory))
print("Enter Logical Volume")
lv_name=input()
print("Enter Volume Group")
vg_name=input()
print("Enter the LV size with units(K,M,G,T,P)")
size=input()
os.system("lvcreate --size {0} --name {1} {2}".format(size, lv_name, vg_name))
os.system("mkfs.ext4 /dev/{0}/{1}".format(vg_name, lv_name))
os.system("mount /dev/{0}/{1} {2}".format(vg_name, lv_name, directory))
os.system("echo mount /dev/{0}/{1} {2} >> /etc/rc.d/rc.local".format(vg_name, lv_name, directory))
else:
print("Invalid Option")
elif int(choice) == 5:
print("Enter Logical Volume")
lv_name=input()
print("Enter Volume Group")
vg_name=input()
print("Enter the increse in size of LV with units(K,M,G,T,P)")
size_increase=input()
os.system("lvextend --size +{0} /dev/{1}/{2}".format(size_increase, vg_name, lv_name))
os.system("resize2fs /dev/{0}/{1}".format(vg_name, lv_name))
elif int(choice) == 6:
print("Enter Logical Volume")
lv_name=input()
print("Enter Volume Group")
vg_name=input()
print("Enter mounted directory with the path")
directory=input()
print("Enter the decrese in LV size with units(K,M,G,T,P)")
size_decrease=input()
os.system("umount {}".format(directory))
os.system("e2fsck -f /dev/mapper/{0}-{1}".format(vg_name, lv_name))
os.system("resize2fs /dev/mapper/{0}-{1} {2}".format(vg_name, lv_name, size_decrease))
os.system("echo Y | lvreduce -L {0} /dev/mapper/{1}-{2} ".format(size_decrease, vg_name, lv_name))
os.system("e2fsck -f /dev/mapper/{0}-{1}".format(vg_name, lv_name))
os.system("mount /dev/{0}/{1} {2}".format(vg_name, lv_name, directory))
elif int(choice) == 7:
print("Enter Physical Volume")
pv_name=input()
os.system("pvdisplay {}".format(pv_name))
elif int(choice) == 8:
print("Enter Volume Group")
vg_name=input()
os.system("vgdisplay {}".format(vg_name))
elif int(choice) == 9:
print("Enter Logical Volume")
lv_name=input()
print("Enter Volume Group")
vg_name=input()
os.system("lvdisplay /dev/{0}/{1}".format(vg_name, lv_name))
elif int(choice) == 10:
print("Enter Physical Volume(s)(space between them) to be removed")
pv_name=input()
os.system("pvremove {}".format(pv_name))
elif int(choice) == 11:
print("Enter Volume Group to be removed")
vg_name=input()
os.system("vgchange -an {}".format(vg_name))
os.system("vgremove {}".format(vg_name))
elif int(choice) == 12:
print("Enter Volume Group")
vg_name=input()
print("Enter Logical Volume to be removed")
lv_name=input()
print("Enter mounted directory with the path")
directory=input()
os.system("umount {}".format(directory))
os.system("lvchange -an /dev/{0}/{1}".format(vg_name, lv_name))
os.system("lvremove /dev/{0}/{1}".format(vg_name, lv_name))
elif int(choice) == 13:
exit()
else:
print("Option not supported")
input("Enter to continue...")
os.system("clear")

Check Disk Partitions : Provides list of Disk Partitions

Create Physical Volume : Creates Physical Volume(PV) from disk(s) mentioned.

Create Volume Group : Creates Volume Group using the Physical Volume specified.

Create Logical Volume : Creates Logical Volume from the Volume Group mentioned, formats it and mounts it to the DataNode directory specified. Since mount operation is temporary in nature , it needs to be performed after every reboot. But it could be made permanent by specifying the command respective to mount in /etc/rc.d/rc.local file. It also asks if the mount directory is already created, and if no , it would create the same.

Extend Logical Volume Size : Provides the functionality of online resizing of Logical Volume Size.

Reduce Logical Volume Size : Provides the functionality of reducing Logical Volume Size.

Check Physical Volume List : Provides the list of Physical Volumes present in system.

Check Volume Group List : Provides the list of Volume Group present in system.

Check Logical Volume List : Provides the list of Logical Volumes present in system.

Remove Physical Volume : Removes the specified Physical Volume.

Remove Volume Group : Removes the specified Volume Group.

Remove Logical Volume : Removes the specified Logical Volume.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Momentum helps ChargeLab keep Salesforce updated and workflows streamlined

Which CSP should be home to your organization’s applications and data?

Apache BookKeeper Insights Part 2 — Closing Ledgers Safely

How to Solve the Shortest Word Distance II?

The Polymorphic Software Engineer

Kubernetes Job & Cronjob

Introduction to Linked-list in Python

Running n8n on ships: An interview with Maranics 🛳

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Satyam Singh

More from Medium

Python with Elasticsearch All Notes

Unit Testing In Python

Lambda () in Python

Python sigma rule #1