Resize EXT3 Filesystem

Table of Contents

Intro

In this paper I'm going to demonstrate the manual process of shrinking and expanding an ext3 Filesystem under Redhat Linux, of course there are a lot of good open source tools that can assist with this process I feel it is always good to have a strong understanding of how the process is working and how to perform the action manually if the tools are not available.

As mentioned above there are a lot of good tool for modifying Filesystems, one of the ones I have found to be useful is GParted (http://gparted.sourceforge.net/), GParted is a Linux based LiveCD which can be burned to a CD or even USB media and then booted from.

Shrinking

First off we will want to see which Filesystem we wish to shrink, we will want to see how large the Filesystem is, and most importantly how much is used/free.

# df -h /dev/hda1
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda1              99M  5.6M   89M   6% /mnt

From the above output we see this is a 99M Filesystem and only 5.6M is being used, this means we can shrink this down pretty far if we wish.

Before we begin shrinking this Filesystem we will first want to unmount it, after which we can then use resize2fs to shrink the Filesystem it self.

# umount /dev/hda1

# resize2fs /dev/hda1 25M
resize2fs 1.39 (29-May-2006)
Please run 'e2fsck -f /dev/hda1' first.

Seen above before we can't resize the Filesystem until a e2fsck is ran, this is to resolve any Filesystem inconsistency before we resize in hope to avoid disaster, so lets do just that.

# e2fsck -f /dev/hda1
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/boot: 15/26104 files (6.7% non-contiguous), 8961/104388 blocks

Alright from the above output it appears the fsck was successful, lets go ahead and shrink our Filesystem to say 25M.

# resize2fs /dev/hda1 25M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/hda1 to 25600 (1k) blocks.
The filesystem on /dev/hda1 is now 25600 blocks long.

Now for the sake of argument lets mount this new Filesystem and see how large it appears to be.

# mount /dev/hda1 /mnt

# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda1              25M  4.8M   19M  21% /mnt

Shown above /dev/hda1 went from 99M before the resize2fs to a 25M after, so far we appear to be on track, however we may also be concerned that are data is still in place, lets have a look.

# ls -l /mnt
total 7
-rw-r--r-- 1 root root    4 Mar  1 02:58 data
-rw-r--r-- 1 root root    4 Mar  1 02:58 file
-rw-r--r-- 1 root root    4 Mar  1 02:59 info
drwx------ 2 root root 1024 Mar  1 03:20 lost+found

As everything is good above all that is left is to shrink the partition which in a real world environment may later be used to create new partitions out of. To do this we will use a utility called fdisk, but before using this we will want to umount the Filesystem for safety.

# umount /dev/hda

# fdisk /dev/hda

Command (m for help):

Now that we are in the fdisk Command prompt we should have a look at the partition currently created, to do this we use the "p" command.

Command (m for help): p

Disk /dev/hda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         652     5132767+  8e  Linux LVM

Seen above we have two different partitions, the one we will be altering is /dev/hda1.

As we now know the starting cylinder for the hda1 partition (cylinder 1) we can delete this partition and recreate it at a smaller size.

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

Command (m for help): p

Disk /dev/hda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda2              14         652     5132767+  8e  Linux LVM

Now that /dev/hda1 is gone we can re-create it at the new size, and starting at the same Starting cylinder.

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

Command (m for help): p

Disk /dev/hda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1           4       32129+  83  Linux
/dev/hda2              14         652     5132767+  8e  Linux LVM

If everything looks good in the above p output we should be fine to save the changes to disk.

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

Calling ioctl() to re-read partition table.

The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

From the output above you notice the kernel still has the old partition table and a reboot is required, rather than rebooting we can use the command partprobe to have the kernel reread the partition table.

# partprobe

And lately lets go ahead and remount our Filesystem and be sure all the data is still intact.

# mount /dev/hda1 /mnt/

# ls -l /mnt
total 7
-rw-r--r-- 1 root root    4 Mar  1 02:58 data
-rw-r--r-- 1 root root    4 Mar  1 02:58 file
-rw-r--r-- 1 root root    4 Mar  1 02:59 info
drwx------ 2 root root 1024 Mar  1 03:20 lost+found

Author: Jeffrey Ness <jness@flip-edesign.com>

Date: 2010-04-13 10:23:50 CDT

HTML generated by org-mode 6.21b in emacs 23