Resize ext3 partition on a VPS live!


Many times you will purchase a VPS and go with the lowest resources you think you can get away with.  As your website grows you may find you need to add more disk space.

This can be very easy to do with some basic tools in Linux.  I will show you how to quickly and easily do this in Debian – but you shouldn’t have issue doing this in any other distro that includes fdisk and resize2fs.

First, you will need to have your disk expanded in the backend.  This will have to be done by the VPS provider.  Once the disk has been expanded you will want to reboot the machine.  This will allow fdisk to see the larger disk.

Before reboot:

# fdisk -ls

Disk /dev/sda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c64d7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          12       96358+  83  Linux
/dev/sda2              13         498     3903795   82  Linux swap / Solaris
/dev/sda3             499       10443    79883212+  83  Linux

After reboot:

# fdisk -ls

Disk /dev/sda: 161.1 GB, 161061273600 bytes
255 heads, 63 sectors/track, 19581 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c64d7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          12       96358+  83  Linux
/dev/sda2              13         498     3903795   82  Linux swap / Solaris
/dev/sda3             499       10443    79883212+  83  Linux

The next step is to delete the current partition, and recreate it with the new larger size. Do not worry, the process of deleting a partition doesn’t actually remove any data, just the reference to the partition.

So run:

fdisk /dev/sda

Where sda is the disk in question.

The first command to run is: p which simply prints the current partition table.

Command (m for help): p

Disk /dev/sda: 161.1 GB, 161061273600 bytes
255 heads, 63 sectors/track, 19581 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c64d7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          12       96358+  83  Linux
/dev/sda2              13         498     3903795   82  Linux swap / Solaris
/dev/sda3             499       10443    79883212+  83  Linux

Make note of the start number for the partition you want to expand, which is sda3 and start #:499 in my case.

So next delete the partition:

Command (m for help): d
Partition number (1-4): 3

Next, create a new partition using the same starting cylinder, but change the end to be the size you want the disk to be. Which in my case is as big as it can get (or 19581):

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (499-19581, default 499): 
Using default value 499
Last cylinder, +cylinders or +size{K,M,G} (499-19581, default 19581): 
Using default value 19581

I just hit enter for the first and last cylinder, as it defaults to the next available one for the start, and the last for the end.

Next, you will have to reboot the machine. This lets the machine re-read the partition table.

reboot

Now that the partition has been expanded, it is time to resize the ext3 file system. This may work on ext4, but I haven’t tested it. This is a simple one line command:

# resize2fs /dev/sda3
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sda3 is mounted on /; on-line resizing required
old desc_blocks = 5, new_desc_blocks = 10
Performing an on-line resize of /dev/sda3 to 38321049 (4k) blocks.
The filesystem on /dev/sda3 is now 38321049 blocks long.

This is pretty much magic in my book.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             144G   65G   73G  48% /
tmpfs                 1.5G     0  1.5G   0% /lib/init/rw
udev                  1.5G  104K  1.5G   1% /dev
tmpfs                 1.5G     0  1.5G   0% /dev/shm
/dev/sda1              92M   38M   49M  44% /boot

Beautiful and simple isn’t it? That’s all folks!

Be Sociable, Share!
  1. #1 by Suat on May 2, 2014 - 4:11 am

    Thank you! Straight and flawless explanation. Worked like a charm!

    Only one tiny little point took my attention: after creating the new partition, just before the reboot, it – naturally – gives an error saying “WARNING: Re-reading the partition table failed with error 16: Device or resource busy.” and at first look, it made me feel like it failed. But after reading next lines of the error it says it needs to reboot and makes sense that it just needs to do that, however that WARNING indicator is really annoying as a newbie linux user like something didn’t go well. Lesson learned from this experience once again: always read the messages until the end and make sure to understand what it actually means :)

    Thanks again.

Comments are closed.