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

So what is Partitioning of Hard Disk?

Partitioning is the concept in which the disk is divided into different segments of secondary storage as a result of easy management of segments separately. Partition is also be performed to reduce the chance of data loss as if 1 segment also of the partition fails then the entire partition data is getting lost. So we create more partition to reduce the loss of the data.

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:

We have a new Hard Drive of 1GB that is connected with our OS

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

Step1: make a partition using the fdisk command:

fdisk /dev/xvdf

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 the 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 to store something we first need to format it and we will format our partition in “ext4” format

mkfs.ext4 /dev/xvdf1

Step3: mount with a folder:

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

mount /dev/xvdf1 /dn1

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

This command will display all the partition info, their mount point, and 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

So for extending the size of the 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

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.

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 a 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

Why the size is still not increased?

As I have earlier said we can use only that part of storage that 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

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

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.

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

Now we will automate the process of LVM using python scripts but if you want to learn the manual you can refer to 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 to 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 the “fdisk -l” command

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

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”.

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

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

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


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:

In this article, we have seen how we can extend the size of the static partition and automated the concept of LVM using python script. So hopefully you have really enjoyed this article.

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

Comments

Popular posts from this blog

How to configure Hadoop cluster using Ansible playbook

Automation Using Python Menu-Driven Program