Extending the Size of Static Partition and Automating LVM with Python Script

Image for post
Image for post

So what is Partitioning of Hard Disk?

So what is a Static Partitioning?

So what is Dynamic Partitioning?

In this article

1: Static Partitioning

→First, we will make a static partition using

→further, we will extend that created static partition without loosing data

2: Dynamic Partitioning:

→Then we will automate LVM concepts with the help of Python Scripts

So let's start by Creating Static Partition:

Image for post
Image for post

Now we will make a partition in this disk following our 3 step process

Step1: make a partition using fdisk command:

fdisk /dev/xvdf

Image for post
Image for post

We have created a partition of 500MB in size and we can create only 4 primary partitions in this disk as it only allows to create 4 primary partitions but there is also a way to create more partition by making 4th primary partition as Extended partition which allows us to create more logic partition.

Step2: format partition:

Now we will format out created Logical Volume as to store something we first need to format it and we will format our partition in “ext4” format

mkfs.ext4 /dev/xvdf1

Image for post
Image for post

Step3: mount with a folder:

We will mount our partition with a folder name “dn1” and command for mounting is

mount /dev/xvdf1 /dn1

Image for post
Image for post

Now let's check if this partition is actually mounted with this folder or not using a command

df -hT

Image for post
Image for post

This command will display all the partition info, their mount point and on which format partition is formatted like in my case I formatted my partition in “ext4” format.

I have created a text file in the “/dn1” folder to check if we extend this size of partition then is my file will be deleted or remains as it is

Image for post
Image for post

So for extending the size of static partition we first need to delete this partition and create a new partition of increased size this is only the possible way to extend size in a static partition so let's do it

Unmount the partition from the folder using the command

umount -l /dn1

Image for post
Image for post

Here “-l” is to forcefully unmount the partition it's not good practice as this might hamper your ongoing read or load operation in that folder.

Now we will delete this partition and will create a new partition of size 1GB.

Image for post
Image for post

It is important to remember to press “N” when it asks to remove the previous signature as if you press “Y” your inode table will get deleted and new inode table will get formed but we don't want this we want our previous inode table so this is the reason we don't remove the previous signature.

Now let's mount our new partition with the same folder and check if the size is increased

Image for post
Image for post

Why the size is still not increased?

As I have earlier said we can use only that part of storage which is formatted and in our case, we again have to reformat our partition using “resize2fs” and this command will automatically resize and merge the unallocated space by recreating the inode table without erasing data of the previous one.

resize2fs /dev/xvdf1

Image for post
Image for post

Using the “df -h” command to check if the size increased or not

Image for post
Image for post

We can see that our size is extended successfully and now we will check if my text file is present or not that we created previously.

Image for post
Image for post

So our data remains the same and safe, we successfully extended size without losing our data.

Now we will automate the process of LVM using python scripts but if you want to learn manual you can refer my previous article in which I explained everything about LVM manually

I would be run the script and share how it’s everything. So this one is going to be brief. I have added the link of the GitHub repository at the end of the article.

We have added a new Storage of 1GB with our OS named “/dev/xvdf” which we can check from “fdisk -l” command

Image for post
Image for post

So let's run our python script using a command

python3 lvm.py

As soon as the program gets started it first install lvm2 software to use LVM commands we should have this software installed

Image for post
Image for post

Then it will ask to tell the name of the storage drive and name the volume group name which we want to create. In the background, the pvcreate and vgcreate command are running like the ones we saw last time. I have created a volume group named “_my_vg”.

Image for post
Image for post

After running this much I have provided with 500MB of input which signifies the size which we want to give to the partition and after that created a partition named “my_partition”

Image for post
Image for post

Now we provided with the input of “dn1” it’s the mount point where we want to mount our created partition

We can check the status if our script actually worked or not by using “df -hT” command which displays all the partition with their mount point

Image for post
Image for post

As we can confirm that my “/dn1” folder is getting storage from my created partition which is approx 500MB, So our script is working fine.

Here is the GitHub Link of my created LVM automated python script

Conclusion:

Thanks for reading this article! Leave a comment below if you have any questions.

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