Shrink existing partition mdraid linux ext4 using resize2fs

How do I know what filesystem type it is?

df -T

How do I view the current disk(s) layout?

lsblk -f

1. unmount the drive's partition you want to shrink

umount /dev/md3

If you see umount: : target is busy. then you may need to cd out of the directory you're trying to umount. (e.g. cd / to go to root).

2. Run e2fsck

e2fsck -f /dev/md3

e2fsck 1.45.5 (07-Jan-2020)
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/md3: 12/115474432 files (0.0% non-contiguous), 7531170/461869424 blocks

3. Shrink the partition

e.g. this is resizing a 1TB partition to 500G

resize2fs /dev/md3 500G

resize2fs 1.45.5 (07-Jan-2020)Resizing the filesystem on /dev/md3 to 131072000 (4k) blocks.
The filesystem on /dev/md3 is now 131072000 (4k) blocks long.

The sizes you can specify are:

  • S - 512 byte sectors
  • K - kilobytes
  • M - megabytes
  • G - gigabytes

4. Re-mount the partition

mount /dev/md3 /home

5. Verify new disk layout

 lsblk -f

Done.

More useful commands

View disk layout:

lsblk -f

What happens if I shrink a partition smaller than the used space, will I loose all my data?

Warning: This may cause data loss.

e.g. Say you partition is 500GB, and 90% full with files, and you resize it using resize2fs, what will happen? Obviously there's not enough free space. But what will resize2fs actually do? Lets find out!

Here we're going to

  1. Download an ubuntu ISO image (2.1G) to our 500G partition (this is an example large file- it could be, your database or other important data you don't want to loose.
  2. Next, we'll unmount and then shrink the partition to 10 kilobytes (smaller than our ubuntu ISO image)
  3. Observe what happens to our precious data

Get some large data (e.g. your database)

wget https://releases.ubuntu.com/18.04.5/ubuntu-18.04.5-desktop-amd64.isols -lh
total 2.1G
-rw-r--r-- 1 root root    6 Aug 22 13:47 hello.txt
-rw-r--r-- 1 root root 2.1G Aug  7  2020 ubuntu-18.04.5-desktop-amd64.iso

Unmount the partition where the data is

umount /dev/md3

e2fsck -f /dev/md3
e2fsck 1.45.5 (07-Jan-2020)
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/md3: 13/32768000 files (0.0% non-contiguous), 2873265/131072000 blocks

Resize the partition smaller than the size of the used data

Here we're resiziing the partition to 10 kilobytes which is smaller than the existing used data size.

resize2fs /dev/md3 10K

So, what happens?

resize2fs 1.45.5 (07-Jan-2020)
`resize2fs: New size smaller than minimum (1303550)`
`echo $?`
1 # we get a > 0 exit code from $? meaning the previous command failed.

Thankfully, resize2fs stops us from shrinking a partition to a smaller size than the used disk space. But what if we want to?

We can force it with the -f option, making our data inaccessible:

resize2fs -f /dev/md3 10K
resize2fs 1.45.5 (07-Jan-2020)
Resizing the filesystem on /dev/md3 to 2 (4k) blocks.
resize2fs: Not enough space to build proposed filesystem while trying to resize /dev/md3
Please run 'e2fsck -fy /dev/md3' to fix the filesystem
after the aborted resize operation.

Notice that, even though the command was forced, we still get "Not enough space to build proposed filesystem while trying to resize". Please run 'e2fsck -fy /dev/md3' to fix the filesystem

e2fsck -fy /dev/md3

e2fsck 1.45.5 (07-Jan-2020)

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/md3: 13/32768000 files (0.0% non-contiguous), 2873265/131072000 blocks

Let's remount: mount /dev/md3 /home and check our data:

Surprisingly,  out data appears to be intact.

-rw-r--r-- 1 root root 2.1G Aug  7  2020 ubuntu-18.04.5-desktop-amd64.iso

Summary: It's surprisingly hard (thankfully) to forcibly and mistakenly shrink a partition using resize2fs smaller than than data currently used.

How to Shrink an ext2/3/4 File system with resize2fs - Red Hat Customer Portal
This article describes how to shrink an ext2, ext3, ext4 file system with resize2fs. resize2fs does not alter the size of partitions, to do this refer to How to Resize a File System with fdisk, parted [LINK TO COME], or lvextend [LINK TO COME].