Online resizing ext3 partitions

      16 Comments on Online resizing ext3 partitions

First, we have to delete our current partition and create a bigger one (don’t be afraid, no data will be lost):

# fdisk /dev/xvda

Type m to get a list of all commands:

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Let’s print out the partition table and look for the ext3 partition, we want to enlarge (it’s easy here, there is just one partition):

Command (m for help): p

Disk /dev/xvda: 5218 MB, 5218304000 bytes
255 heads, 63 sectors/track, 634 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1         634     5092573+  83  Linux

Now we delete the old partition (/dev/xvda1) and create a new one with the maximum available size:

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-634, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-634, default 634): 
Using default value 634

Our original /dev/xvda1 had the bootable flag (see fdisk -l output), so we must add it to our new /dev/xvda1 again:

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

Now let’s write our new partition table and exit fdisk:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

After rebooting the system (!) we can online-resize the filesystem with a simple command:

# resize2fs /dev/xvda1 
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/xvda1 to 1522150 (4k) blocks.
The filesystem on /dev/xvda1 is now 1522150 blocks long.

16 thoughts on “Online resizing ext3 partitions

  1. Pingback: /dev/blog » Howto resize xen disk images

  2. Pingback: Online resize ext3 partitions - Ext3 Ext4 Tutorial

  3. avatarJan

    I am looking for a way to increased the size of my root partition before I read your article.
    Thanks for this info. It gives me some hope.
    I would like to clarify if you use a live cd when you do the changes on the partitions?

  4. avatarRaftaMan Post author

    @Jan
    The article is specifically about resizing partitions while they are mounted (hence the “online”). So there is absolutely no need for any kind of live os or rescue system.

  5. avatarJonathan Bayer

    These instructions only work if there is free space behind the partition you are resizing. The fdisk is done from within the OS, no need for a live cd if you are careful.

  6. avatarAndrew

    Your print of the partition table says it starts at cylinder 1 and ends at cylinder 634.
    Then your new partition seems to start and end at the same cylinders. Are you sure you changed anything?

  7. avatarRaftaMan Post author

    @Andrew
    Wow, you’re absolutely right. Seems like i copy/pasted the wrong bit of console output here. But I think you get the idea. Be assured that i tried this and used it many times even on production servers.

  8. avatarTristan

    Gave me the old “bad magic number in superblock while trying to open…” error when trying to reboot the machine.

  9. avatarCilard

    Why are you referring to this procedure as online? It won’t enlarge the partition until you reboot the machine which for sure needs to unmount the partition before rebooting (if you haven’t done if before the fdisk part already).

    You can actually do it without reboot, but still it’s not an online resize!

  10. avataradmin Post author

    @Cilard It’s called online-resize, because the filesystem can be mounted during “resize2fs”. Of course, if there’s not enough space behind the filesystem on the partition, you have to expand the partition first, which needs a reboot. Probably should have called it “Online resizing ext3 filesystems”…

  11. Pingback: Can you expand a HDD on an online Linux VM?

  12. avatarSebneu

    I always get:
    Error: Partition /dev/vda1 is being used. You must unmount it before you modify it.
    So, no luck for me or is there a special trick?

  13. avatarRay

    It is not working for me. After changing the partition table I get dropped into “grub rescue” and when i try grub commands to start up linux grub gives me “uknown filesytem”

  14. avatarLorenzo

    Same problem as Ray: after reboot I get grub recovery with “unknown filesystem”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.