Playing with Static Partition

Rishi Agrawal
6 min readMar 17, 2021

This blog is focused on how we can change the size of the a static partition like LVM. We will discuss to how much extent we can do, then discuss why LVM is better for this use case.

This blog is the part of the task 7.1(b) given by Vimal Daga sir, the problem statement is as follows:

ARTH — Task 7.1(B) 👨🏻‍💻

Task Description 📄

Increase or Decrease the Size of Static Partition in Linux

visualization (imaginary)

For this blog, I have freshly installed RHEL 8 OS on VMware, and attached a extra 20 GB hard disk to it. That all we need for this practical. I am using to root user to do this practical.

Little about the partitioning:

When we have the large storage drive, we virtually break the whole volume into parts which works as independent drives. This drives can be attached or detached to OS according to need.

Lets start with creating and attaching a static partition…

Step1: Make sure the 20 GiB is connected and is showing in list, from this command:

[root@localhost ~]# fdisk -l

Note the drive address. In my case, the drive address is /dev/nvme0n2.

For this blog, I am creating a primary partition i.e. the static partition of size 10 GiB.

Step 2: Now, we need to select the drive and create a partition table. We can do by this following commands:

[root@localhost ~] fdisk /dev/nvme0n2

This command will start the program which is used to create the partition. Now, we have to give commands inside the program.

Command (m for help): m

Give m to see the help and there you can find all the commands you can give.

I have added a copy of it below.

Help:DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table

We have to create the partition table, we can clearly see command for it under Create new label:

I am creating a DOS partition table, so my command will be:

Command (m for help): o

Step 3: As the partition table is created, now we can move further to create the partition, to do this, look up in the help which option can help us.

As we are creating a new partition, we can create by:

Command (m for help): n

It will ask for the partition type, select primary. Then the partition number give 1, then it will ask which sector you want to start, give nothing and then in last step give the size of partition you want to make.

Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +10G

And the partition is created!

Step4: now, we have to save the changes what we have done, so give the following command:

Command (m for help): w

This will exit to external command-line by saving the changes.

To check partition is created of not run the lsblk or fdisk -l and note the path of the partition that we created, in my case it is /dev/nvme0n2p1. You will see the following Output to the commands :

Step 5: Now we have to format and mount the partition that we have created.

I am going to create the ext4 format in the partition, because it is latest and widely used.

[root@localhost ~]# mkfs.ext4 /dev/nvme0n2p1

Now we have to mount the partition to the the folders so that we can access it.

[root@localhost ~]# mkdir /static
[root@localhost ~]# mount /dev/nvme0n2p1 /static

After this run df -h to check if it is mounted or not.

Now, lets move to the main part of the blog, How to change size of the static partition without loosing the data.

Right now my static partition is empty, therefore let add the file name abc.txt with some content.

Now, we will see how to increase the size from 10 GB to 20 GB, using the fdisk

But first we need to unmount the partition, thats why it is called a offline process.

[root@localhost ~]# umount /dev/nvme0n2p1

After this, start the fdisk and use the above help to first delete the created partition.(yeah you heard it right, delete the partition).

After that create the new partition, and leave all the options to defaults, It will add the whole volume to the partition.

NOTE: At last it will ask to remove the Ext4 signature give N, this will save your file.

Now, save the changes made and exit using the w.

We have created the new partition but we need to sync with older one so that we can use it normally as we are using it.

[root@localhost ~]# e2fsck -f /dev/nvme0n2p1

This command will fix all the errors and change the size accordingly.

Next command will resize and format the remaining part of the partition.

[root@localhost ~]# resize2fs /dev/nvme0n2p1

Now, lets mount again and see if the size has increased or not. by running df -h and see if it worked or not.

Yes, it worked and lets see if our file is safe or not.

yay! we have done this practical successfully!

Downsides of this…

We have done it successfully, but if you go to my last blog, you will see that using LVM has more benefits and easy to use. To change the size of the static partition we have to unmount it first while in the case of the LVM whole thing can be done online.

Thanks a lot for reading! And huge thanks to Vimal sir for teaching such a great topic.

Any issues/suggestions please let me know in the comments!

--

--

Rishi Agrawal

Aspiring MLOPS engineer with Multi-cloud and Flutter/MERN