Shrink ext3 Filesystem

Assume we have an ext3 filesystem on an LVM volume that is just way too big.

$ df -hlP /backups/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-service_backup   99G   15G   79G  16% /backups

A 100 GB filesystem for 15 GB of data? That is way too big! Let’s shrink that down and reclaim some of that wasted space.

$ sudo /sbin/resize2fs -p /dev/VolGroup00/service_backup 25G
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/service_backup is mounted on /backups; on-line resizing required
On-line shrinking from 26214400 to 6553600 not supported.

Can’t shrink a mounted filesystem. No problem.

$ sudo umount /backups

$ sudo /sbin/resize2fs -p /dev/VolGroup00/service_backup 25G
resize2fs 1.39 (29-May-2006)
Please run 'e2fsck -f /dev/VolGroup00/service_backup' first.

Now I have to fsck? Ok, fine.

$ sudo e2fsck -f /dev/VolGroup00/service_backup
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/VolGroup00/service_backup: 37/13107200 files (5.4% non-contiguous), 4309966/26214400 blocks

$ sudo /sbin/resize2fs -p /dev/VolGroup00/service_backup 25G
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/VolGroup00/service_backup to 6553600 (4k) blocks.
Begin pass 2 (max = 2562078)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 800)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 8)
Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/VolGroup00/service_backup is now 6553600 blocks long.

Ah, there we go. Let’s make sure we didn’t screw anything up.

$ sudo e2fsck -f /dev/VolGroup00/service_backup
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/VolGroup00/service_backup: 37/3276800 files (5.4% non-contiguous), 3997466/6553600 blocks

Good, no problems found. Now that the filesystem is right-sized, next we have to shrink the logical volume.

$ sudo lvresize -L 25G /dev/VolGroup00/service_backup
  WARNING: Reducing active logical volume to 25.00 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce service_backup? [y/n]: y
  Reducing logical volume service_backup to 25.00 GB
  Logical volume service_backup successfully resized

And, just because I’m paranoid, let’s fsck one more time.

$ sudo e2fsck -f /dev/VolGroup00/service_backup
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/VolGroup00/service_backup: 37/3276800 files (5.4% non-contiguous), 3997466/6553600 blocks

Just need to remount the filesystem and we’re done!

$ sudo mount /backups

$ df -hlP /backups/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-service_backup   25G   15G  8.6G  64% /backups

Ta-da!

Advertisements

2 Responses to Shrink ext3 Filesystem

  1. How can you be sure that 25G is 1000M or 1024M?

  2. Lauren says:

    Thank you Kris. Worked like a charm!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: