How to extend a Linux ext3 partition when it’s mounted


Many times when using Virtual Machines it becomes necessary to extend the main disc partition because the VM ran out of space without loosing data, so here is how to do it.

Caution: BEFORE to proceed I strongly recommend that you back up your virtual machine!!! You are going to use the following method at your own risk.

Let’s assume we are extending a disc which was originally 16GB to 40GB on VMware…

Please note: the following procedure is for ext3 partitions on modern Linux distros on discs that are NOT using LVM. For LVM partitioned disc there are different commands and procedures to follow.

Preparing VM disk

First of all let’s shutdown your VM and then extend the disc size using:

vmware-vdiskmanager -x 40Gb youre-vm-virtual-disk-name.vmdk

Once vdiskmanager is done with extending your disk you can boot up your virtual machine and begin to resize your mounted partition.

Extending ext3 mounted partition size

1) When you Virtual Machine is up and running, login as root and check if fdisk correctly see the new disc size with:

fdisk -l

(Please take note of the Total Sectors that fdisk will display)

2) Now let’s create a partition table configuration file for sda (you can change sda disc with your own disc if different) using sfdisk utility:

sfdisk -d /dev/sda > ~/sfdisk_sda.orig

3) Create a backup copy of this configuration:

cp ~/sfdisk_sda.orig ~/sfdisk_sda.new

4) At this point let’s edit the file and change the end of your desired partition, let’s assume you are extending sda1:

vi sfdisk_sda.new

Modify where it says size:
/dev/sda1 : start=63, size=1743246, Id= 83

with the number of [total sectors] shown by fdisk output at point 1 start sector shown on the line you are modifying, save and exit.

Please Note: If you have multiple partitions on the same disk then the math is a bit more complicated than just [total sectors] – [start sector], in fact, you’ll have to subtract all the sizes of all the partitions you have on your sda disc as well as their starting points.

5) Now let’s update your partition table configuration:

sfdisk --force /dev/sda < ~/sfdisk_sda.new

6) Now update Kernel using:

partprobe

Some Linux distro can’t update kernel live so you may need to reboot after partprobe, if partprobe ask you so then reboot using:

reboot

7) When the kernel partition table is updated (if you had to reboot then login again as root) extend your partition to the full available disk space with:

resize2fs /dev/sda1

At this point, if everything went well, your sda1 should be of 40GB.

Thanks for reading and, if you enjoyed this post, please support my blog by visiting my on-line hacking and engineering merchandise shop on redbubble.com by clicking here, thank you! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s