How to resize root partition without losing data

I signed up for a promotion with my VPS vendor for a one-time fee for a permanent increase in available space. The change was made on the provider’s side – now I had to make changes on my side.
Of course the question arose how to do it, so as not to reinstall the system and not to lose data. And to keep potential VPS downtime as low as possible.
So how did I do it?
As a reminder: my system is Debian 9, ALWAYS do backup and you do it on your own responsibility.

I show you my situation. Login on root account and run fdisk /dev/vda.

Press “p” (to view current situation). I got:

Command (m for help): p
Disk /dev/vda: 420 GiB, 450971566080 bytes, 880803840 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xdb7dd47b

Device     Boot  Start       End   Sectors   Size Id Type
/dev/vda1  *      2048    194559    192512    94M 83 Linux
/dev/vda2       196606 251656191 251459586 119.9G  5 Extended
/dev/vda5       196608 251656191 251459584 119.9G 83 Linux

As you can see disc /dev/vda has 420GB but existing partitions have less.

Press “d” to remove partition 5.

Press “d” to remove partition 2.

Press “n” to create a extended partition. IMPORTANT – as first sector set 196606 as was on beginning (you need to adjust this for your situation). Last sector – use default value.

Press “n” to create a logical partition. IMPORTANT – as first sector use default (yes, I know it’s wrong, you will change this later, at this point set your correct value is not possible). Last sector – use default value.

Press “t”, select partition number 5 and select the write code 83 – if different than your situation.

Press “x” to go to expert mode.

Press “b” to change beginning of partition and select partition 5

When asked for new beginning of data enter 196608 (you need to adjust this for your situation – take a look how it was on old structure)

Press “r” to return to main fdisk menu

Press “p” to print partition table and carefully check that is ok.

Press “w” to write changes to disc.

Quit fdisk.

Reboot your virtual machine.

After restart run: resize2fs /dev/vda5

You can also check fdisk /dev/vda and press “p”:

Command (m for help): p
Disk /dev/vda: 420 GiB, 450971566080 bytes, 880803840 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xdb7dd47b

Device     Boot  Start       End   Sectors   Size Id Type
/dev/vda1  *      2048    194559    192512    94M 83 Linux
/dev/vda2       196606 880803839 880607234 419.9G  5 Extended
/dev/vda5       196608 880803839 880607232 419.9G 83 Linux

That’s it! Have a wonderfull day 🙂

Leave a Comment: