Increase or Decrease the Size of Static Partition in Linux

In this blog, I am going to discuss how to resize the static partition in Linux with the help of the resize2fs command. The basic prerequisite for this task is Linux partition.

A hard disk can be divided into several partitions. Each partition functions as if it were a separate hard disk. There are several reasons why we create the partition:

  • multiple operating systems on the same disk.
  • different file systems on different partitions.
  • more efficient disk space management.
  • different security settings on different partitions.
  • easier backup procedure.

In this task, we have to increase or decrease the size of static partition Linux. In Linux, the primary partition that we crease don’t allow to increase or decrease the partition size if we do we have to change the format it again but the issues are that if we format the partition again then we will not be able to access the data that we had stored. So we have to find some way through which we can increase or decrease the size of the static partition by formatting it again.

In the first step add one hard disk to the VM, we can see the hard disk with the fdisk command.

fdisk -l

Here we can see that one new hard disk with the name /dev/sdb of 50GiB is added.

In this step, we will create one primary partition of 30GiB with the help of fdisk command.

fdisk /dev/sdb

Now we can check the partition is a crate or not.

Here we can see that one partition of size /dev/sdb1 with the size of 30GiB.

In this step, we will format the partition with the ext4 file system, and then we will mount with /data directory.

mkfs.ext4 /dev/sdb1

mount /dev/sdb1 /data

We can verify that the partition is mounted with /data folder or not with df command.

df -h

In this step, we will put some data in the directory so that we can check if the data get lost or not after resizing the partition.

In this step, we will unmount the partition because we have to resize the partition size and the static partition doesn’t allow us to resize the partition on-line.

To unmount the partition we can use the umount command.

umount /dev/sdb1

Here the partition /dev/sdb1 has been mounted from the /data directory.

We can check that there is no data in the /data directory.

In this step first, we will create the partition /dev/sdb1 so that we can change the size of the partition.

As we have to resize the partition so in this step we will create the partition again but the starting sector will be the same as the previous partition.

In my case the previous partition was started from the 2048 sector so again I will create the partition from that sector only and this time I am going to increase the size from 30GiB to 40GiB.

We can check the partition is create or not by listing the partition command.

We can clearly see that one partation /dev/sdb1 of size 40GiB is created.

In this step, we will verify the partition consistency by running the e2fsck command.

e2fsck -f /dev/sdb1

Here it is showing that there is some mismatch in the file system configuration and current partition size. To fix this issue we have to use the resize2fs command.

resize2fs /dev/sdb1

Now the file system block size is the same as for partition configuration. Let’s mount the resized volume and check if our data is still there or not.

Here we have mounted the volume with the /data directory.

We can clearly see that our data is still there in the directory.

Thank You !!

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