‍🚀Increase or Decrease the Size of Static Partition in Linux without losing the data.‍🚀

Yadvi Bhalla
3 min readNov 3, 2020

In this article, I am gonna tell you the trick by which we can increase the size of static partition, although the good practice is to create a dynamic partition using LVM if in future you want to increase or decrease the size of your hard-disk.

So the first step is to launch an operating system, you can use either AWS Instance or virtual machine. Attach a hard-disk (size can be specified depending on the hard-disk of your base OS, I have given 10Gib space to the hard-disk). Before putting some data on the hard disk, we always need to create a partition, format and mount it.

Creating a physical partition on the hard-disk:

To create the physical partition on the hard-disk, we have to use fdisk command.

fdisk /dev/sda>>To create new partition press "n"
>>Now we have to press enter to select default primary partition
>>Again press enter to select the default partition number
>>Press enter again
>>Now we have to specify the size of physical partition(I am gonna provide 2Gib): +2G
>>Press "w" to save
>>Physical partition /dev/sda1 of size 2Gib will be created

Formatting the physical partition:

To format the physical partition of 2Gib we created in the previous step, use the following command:

mkfs.ext4 /dev/sda1

Mounting the partition on a file:

To store some data in our hard-disk, we need to mount it on a file system. First, let’s create a directory on which we can mount the partition.

mkdir /task7.1.1/

After creating the directory, let’s mount the partition:

mount   /dev/sda1   /task7.1.1/

Now the hard-disk is ready to store some data. Create a file (let say practice.txt) in the task7.1.1 folder.

cd /task7.1.1/
cat > practice.txt
hello world!
press "ctrl+D" to save and exit.

So, our hard-disk of 2Gib is successfully created and we have stored some data in this hard-disk. But let say, in future, we want to increase the size of hard-disk to 4Gib, then how to increase it?

It might look a bit complex at first, but stay with me, you will get it. So to do that let’s unmount and delete this partition first.

NOTE: Deleting the partition will only delete the partition table, the data will not be deleted, and partition table contains the address of the data, so after deleting the partition our system will not be able to find the data, but data will still exists.

To delete the partition we have to again use fdisk command.

cd
umount /task7.1.1
>>Before deleting, we have to unmount the partition.

fdisk /dev/sda
>>press "d" to delete the partition
>>press "w" to save
>>The partition will be deleted successfully.

The next step is to create a partition of 4Gib.

Note: It will prompt you to remove the signature, press “n” so that we won’t lose our older data.

fdisk /dev/sda>>To create new partition press "n"
>>Now we have to press enter to select default primary partition
>>Again press enter to select the default partition number
>>Press enter again
>>Now we have to specify the size of physical partition: +4G
>>Press "w" to save

Now this time, we gonna retrieve the previous data using “e2fsck” command.

e2fsck -f /dev/sda1

After running this command, the previous data will be retrieved, but again if we want to access it, we have to format and mount the partition. The challenge here is we can’t format the whole partition using “mkfs” command because 2Gib partition is already formatted and it has some data stored on it. To format the left-out partition, use the following command:

mkfs.ext4 /dev/sda1

The final step is to mount the partition:

mount  /dev/sda1  /task7.1.1/

Now if you run “lsblk” command, you will notice that the static partition size has been increased from 2Gib to 4Gib and there is no loss of data. You can check it by navigating to the location where you stored the data.

Thank you!

--

--

Yadvi Bhalla

RedHat Certified Specialist in Containers and Kubernetes