Increase or Decrease the Size of Static Partition in Linux

Akhil Sukhnani
4 min readMar 20, 2021

--

Hola readers! Welcome back to another article where we’ll be learning to “Increase or Decrease the Size of Static Partition in Linux”

The only pre-requisite for this practical is a working Linux Virtual Machine(I'll be using Oracle VirtualBox for virtualization and Centos:7 as Linux OS).

Disk Partitioning in Linux?

large storage devices are divided into separate sections called partitions. Partitioning also allows you to divide your hard drive into isolated sections, where each section behaves as its own hard drive.

And now without any further ados let’s get started….!

Step1:- Adding an external hard disk to your OS/Virtual Machine

Creating a VDI in Oracle VirtualBox for the VM I’ll be using to perform the practical.

You can allocate it any size, I choose to give 15Gib because that would be more than enough to demonstrate the practical.

Attaching the VDI(Virtual HardDisk) to the VM.

Step 2:- Creating the partiton

And as you can see in the above screenshot vdi has been created and attached to the VM.

Now,it’s time to boot the VM

And after the system starts, you can verify the virtual hard-disk attached to VM by this command:-

fdisk -l

As you can see in the above screenshot /dev/sdb is the attached hard-disk.

And to interact with the hard-disk we can again use the “fdisk” command, like this:-

Syntax:- fdisk <disk name>fdisk /dev/sdb

Now to create the new partition in this disk press n

Then press P for creating a primary partition

Then press enter one time.

Then allocate the size of the partition here i have given 4G (i.e. 4Gib) and press enter.

And to save the changes made till now press W.

Now the partiton has been created , and its time to format it using command

Syntax:- mkfs.<type of format> <partition_name>mkfs.ext4 /dev/sdb1

Now our /dev/sdb1 partition is formated in ext4 format.

Now we can store the data in the partition by simply mounting it to a folder, similar to the above screenshot.

mount /dev/sdb1 <folder_name>

And now you can store your data in this folder , I have created a demo file (demo.txt) to demonstrate the setup that even after increasing/decreasing the size of the partition this file won’t be lost.

Step3:- Deleting the partition

And now we delete the partition that we created earlier by:-

fdisk /dev/sdbPress d -> to delete the partitionPress w -> to save changes

And now as a result of this the older partition that we created will be deleted.

Step4:- Creating the partition with increased size

Create a new partition again withe the same command

fdisk /dev/sdbpress npress ppress Enterallocate sizepress Ypress W

As a result of which a new partition of size 8Gib would be created.

But we have to clean some bad sectors , clean the older file system before mounting the partition again using the command:-

e2fsck -f /dev/sdb1

Note:- Do not format the partition again because it will erase the data.

And now we have to just mount the partition again and use it.

And now as you can see after mounting the partition , which is of size 8Gib our file is still there.

Conclusion

With the help Linux Partitioning concept we can increase and decrease the storage of the partition without losing our important data.I hope you learned something new from this article stay tuned for more such interesting articles…!

--

--