Automating LVM partition using python script

shreyash kotgire
4 min readMar 14, 2021

--

In this article we will automate the process of LVM and partitioning in Linux with the help of python script program. I am using Red hat Enterprise Linux8 for this demonstration.

Let’s understand …

💻 what is LVM?🤔

Logical volume management (LVM) is a form of storage virtualization that offers system administrators a more flexible approach to managing disk storage space than traditional partitioning. … The goal of LVM is to facilitate managing the sometimes conflicting storage needs of multiple end-users.

To create this we first have to create PV (physical volume) and make it available with VG (Volume group) and make it available for LVM to understand this — refer to the following image.

Now let’s see how our script works for this practical I attached hard disk of 20 GB to system.

We can run the script with command

python3 lvm.py

after selecting choice to list available storage device as u can see a disk of 20 GB is connected i.e /dev/sdb . Now we need to convert this disk as persistent volume.

after selecting choice to create persistent volume script takes input for disk name . Now we have to create volume group

here volume group with name ‘myvg’ has been created successfully u can see more info about it with option of display volume group

now our volume group is also ready and we are good to go for creating logical volume which we gonna use in our requirements

Finally our logical volume is created now we can format it with file system an mount it for our use also we can extend or decrease size of logical volume with the help of our script.

Following is the script i created

import subprocess
import os
will = True
while will:
print('1. create Presistent volume.\n2. create volume group\n3. create logical volume\n4. show volumes atached\n5. display persistant volumes\n6. Display volume group\n7. display logical volume\n8. List available storage devices \n9. extend size of LVM \n10.decrease size of lvm \n11.mount storage \n12.unmount storage\n0. Exit')
t = int(input())
if t==0:
will = False
break
elif t==1:
name = input('Enter name/path for volume: ')
print(subprocess.getoutput("pvcreate {}".format(name)))
elif t==2:
name = input("Enter name for volume group: ")
print(subprocess.getoutput("vgcreate {}".format(name)))
elif t==3:
name=input("Enter name for logical volume group: ")
size=int(input("Enter size in G: "))
vgname=input('Enter volume group name: ')
print(subprocess.getoutput("lvcreate --size {} --name {} {}".format(size,name,vgname)))
elif t==4:
print(subprocess.getoutput("df -hT"))
elif t==5:
name=input('Enter pv name: ')
print(subprocess.getoutput("pvdisplay {}".format(name)))
elif t==6:
name=input("Enter name of volume group: ")
print(subprocess.getoutput('vgdisplay {}'.format(name)))
elif t==7:
name=input("Enter name of volume group: ")
print(subprocess.getoutput("lvdisplay {}".format(name)))
elif t==8:
print(subprocess.getoutput('fdisk -l'))
elif t==9:
size=int(input('Enter size to extennd: '))
lvm=input('Enter your lvm path: ')
print(subprocess.getoutput('lvextend --size +{}G {}'.format(size,lvm)))
print(subprocess.getoutput('resize2fs {}'.format(lvm)))
print()
elif t==10:
size=int(input('Enter size to reduce: '))
lvm = input('Enter path of your lvm: ')
lvsize=int(input('Enter size to resize: '))
folder = input('Enter path where your lvm attached: ')
os.system('umount {}'.format(lvm))
os.system('e2fsck -f {}'.format(lvm))
os.system('resize2fs {} {}'.format(lvm,lvsize))
print(subprocess.getoutput('lvreduce --size -{}G {} -y'.format(size,lvm)))
os.system('mount {} {}'.format(lvm,directory))
print()
elif t==11:
directory = input('Enter your directory path: ')
lvm = input('Enter lvm name/path: ')
os.system('mount {} {}'.format(lvm,directory))
print('mounted successfully..')
elif t==12:
lvm = input('Enter directory name/path: ')
os.system('umount {}'.format(lvm))
print('umount successfully...')
else:
print('Enter valid option...from above')

Thank you for reading …!

--

--