Extending the Size of Static Partition and Automating LVM with Python Script
So what is Partitioning of Hard Disk?
It is the concept in which a given hard disk is divided into segments of secondary storage so that each segment is managed separately. This is also done with a view that if the damage is caused to one segment, like in cases of complete deletion of data, the damage is not transferred to the entire disk. It rather remains confined to that particular segment.
So what is a Static Partitioning?
Static partitioning is a type of hard drive configuration to manage all partitions and data on the hard disk, normal partition tables or logical drives are used. It can contain up to four primary partitions, or three primary partitions and an extended partition with multiple logical drives.
So what is Dynamic Partitioning?
A disk that has been initialized for dynamic storage is called a dynamic disk. It gives more flexibility than static partition because it does not use a partition table to keep track of all partitions. The partition can be extended with dynamic disk configuration. It uses dynamic volumes to manage 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 is connected with our OS
Now we will make a partition in this disk following our 3 step process
Step1: make a partition using fdisk command:
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
Step3: mount with a folder:
We will mount our partition with a folder name “dn1” and 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
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
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
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 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 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.
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 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
So let's run our python script using a command
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 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”
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
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
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.