Resizing & Moving EXT3 (And EXT2) Partitions Without GNU Parted

Last Updated: August 04 2005
By: Paul Stanisci, 2byteconsulting.com

Why?

Typically, to resize an ext2 partition in linux you would use GNU Parted. However GNU Parted doesn't understand EXT3 file systems, nor does it support newer ext2 file systems.

This leaves linux users with ext3 file systems or new ext2 file systems with few options for resizing their partitions. You could use Partition Magic, but not only is it non-free, it's been known to destroy ext2 & ext3 file systems. I have personally had this happen to me on two seperate occasions.

A common suggestion is to use resize2fs. However resize2fs is not a complete solution. It simply resizes file systems without modifying it's associated partition. If you resize a file system with resize2fs, you must also delete & re-create a new partition to contain the file system. Although this will allow you to resize your file system, it cannot be moved since the new file system must begin at the same sector as the original.

The Solution

So, with this being said, how do you resize and move ext3 partitions in linux? To follow is a procedure we've developed that works quite well.

In this example, I will refer to /dev/hdaX as our subject for this procedure. This procedure will cover the steps required to expand and move this partition to /dev/hdaY.

It should also be noted that this procedure requires that all the drive's partitions be unused and unmounted. Typically I'll boot Knoppix if I need to opperate in a seperate environment from the on-disk system.

  1. Boot up with Knoppix (or any other cd-bootable distro with the required Ext2 tools). I'll usually boot Knoppix with the boot argument 'knoppix 2' for text-only mode.

  2. If the partition you wish to resize or move contains an ext3 file system, you must first convert it to ext2. You can restore the journal once the procedure is complete. To remove journaling, use tune2fs:

        tune2fs -O ^has_journal /dev/hdaX

  3. Determine how much space is used on the subject partition. You can do this by temporarily mounting the partition and checking the used space with df.

  4. Run an e2fsck on the source file system (This MUST be ran before using resize2fs, don't forget the '-f'):

        e2fsck -f /dev/hdaX

  5. Now, you will use resize2fs to shrink the file system of the subject partition to be as small as possible. This will allow for a faster overall procedure. The file system cannot be shrunk smaller than the total size of it's data. Resize the partition like so:

        resize2fs -p -d 27 /dev/hdaX 20000M
    The last argument '20000M' indicates you wish for the file system to be resized to 20 000 MB. Try and make it as small as possible but big enough to contain all your data. If you request a size that's too small, resize2fs will give an error and not make any changes.

  6. Now that the file system is small and workable, you must back it up. You can either transfer it to another system on you network, to another hard disk, or simply another partition you will not be modifying on the same disk. Here is how you would backup the partition using dd:

        dd bs=1M count=20001 if=/dev/hdaX of=/mnt/dest/hdaX_backup.hd
    The 'if' argument points to the input file stream, in our case it's our subject partition. 'of' is the destination, I'll usually just save it to another local partition. 'bs=1M' sets the block size to 1 MB, and 'count' states how many blocks you wish to copy. The above example copies 20 thousand and 1, 1MB blocks, which is 1 MB larger than the actual file system. You don't want to miss part of the file system because of disk geometry.

  7. Now that you have a copy of your partition saved, you must setup the destination partition. Create a partition with your desired final size, it can be anywhere on the same disk or another disk. You may destroy the original partition and re-create it with a new size if you desire.
    Important: Ensure that the destination partition is large enough to contain your backed up file system!! If it is not, when you restore the file system, you may destroy file systems placed after the destination partition!!

  8. Once the destination partition is prepared (a reboot may be necessary for the kernel to recognize the new partition table), you can restore the file system. Here is how you would restore the saved file system to hdaY:

        dd 'bs=1M' count='20001' if=/mnt/dest/hdaX_backup.hd of=/dev/hdaY
    Just as before 'bs' & 'count' represent the block size and block count respectively. However, 'if' references the source file which is the backup of the file system, and 'of' refers to the destination partition on disk.

  9. Run an e2fsck on the file system (once again this must be done before using resize2fs):

        e2fsck -f /dev/hdaY

  10. Now that the file system is in place, it must be grown to fill the partition. For this we will once again use resize2fs:

        resize2fs -p -d 27 /dev/hdaY

  11. If your file system was originally ext3, convert it back:

        tune2fs -j /dev/hdaY

  12. Now update any necessary filesystem mount points in /etc/fstab


Good luck!

If you would like to contribute or recommend changes/updates please e-mail Paul