Oct 26, 2010

Shrink EXT3 partition Without Losing Data

Shrink EXT3 partition Without Losing Data


RedHat Enterprise Linux


Resize the ext3 partition without losing data


This article shows how to shrink the ext3 partition without losing data. This can be quite useful if you do not use LVM

Note: The partition that is to be resized must be unmounted when we do the resizing

Shrinking an ext3 Partition:

1) Check the disk usage of the server before proceeding

          # df –h

          Filesystem   Size       Used     Avail      Use%       Mounted on
          /dev/sda3    4.4G       2.4G     1.8G       58%          /
          /dev/sda1    99M        14M      80M        15%          /boot
          tmpfs       125M       0        125M        0%          /dev/shm

2) Boot the server into linux rescue mode

Note: If the partition you want to resize doesn't hold any system files, you can do everything from the original system; the steps are the same, just omit booting into rescue system

3) Run the filesystem-specific fsck on the filesystem.

               # fsck –n /dev/sda3

         e2fsck 1.39 (29-May-2006)
         /dev/sda3: clean, 115974/577152 files, 636274/1152640 blocks

4) Remove the journal from /dev/sda3, thus turning it into an ext2 partition

         # tune2fs -O ^has_journal /dev/sda3

         tune2fs 1.39 (29-May-2006)

5) Run fsck on the filesystem

         # e2fsck –f /dev/sda3

         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
         /dev/sda3: clean, 115974/577152 files (0.5% non-contiguous), 636274/1152640 blocks

6) Now resize the filesystem using resize2fs. resize2fs can resize ext2 file systems, but not ext3 file systems

         # resize2fs /dev/sda1 3600M

         resize2fs 1.39 (29-May-2006)
         Resizing the filesystem on /dev/sda3 to 921600 (4k) blocks.
         The filesystem on /dev/sda3 is now 921600 blocks long.

Please take note of the amount of blocks (921600) and their size (4k).

Note: Currently, 2.3 GB are used on /dev/sda3 (see the df -h output above), so it's safe to shrink it from 4.4GB to about 3.6GB (if you make it smaller than 2.3GB, you will lose data!).

7) Now we delete our /dev/sda1 partition (don't be afraid, no data will be lost) and create a new, smaller one (but still big enough to hold our resized file system!).

        # fdisk /dev/sda

        Command (m for help): d
        Partition number (1-4): 3
        Command (m for help): n
        Command action
        l logical (5 or over)
        p primary partition (1-4)
        Partition number (1-4): 3
        First cylinder (79-652, default 79):
        Using default value 79
        Last cylinder or +size or +sizeM or +sizeK (79-652, default 652): +3870720K

        Command (m for help): w
        The partition table has been altered!
        Calling ioctl() to re-read partition table.
        Syncing disks.

Note: We multiply the amount of blocks from the resize2fs output (921600) by the size of a block (4k), and to go sure the partition is big enough, we add 3 to 5% to it (5% was enough for me, but if you want to go sure take 3%)

                   921600 * 4k * 1.05 = 3870720k

8) Now restart the server once and boot into the rescue mode once again

        # exit

9) Once again run the filesystem-specific fsck on the filesystem

        # fsck –n /dev/sda3

        fsck 1.39 (29-May-2006)
        e2fsck 1.39 (29-May-2006)
        /dev/sda3: clean, 115962/464928 files, 599592/921600 blocks

10) Now create the journal on our new /dev/sda3, thus turning it into an ext3 partition again

        # tune2fs -j /dev/sda3

        tune2fs 1.39 (29-May-2006)
        Creating journal inode: done
        This filesystem will be automatically checked every 25 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.

11) Now restart the server and boot from the harddisk.

        # exit

No comments: