THERE IS A DECENT CHANCE THAT IF YOU FUCK THIS UP YOUR
REMOTE SYSTEM SIMPLY WONT BOOT AT ALL. I URGE YOU TO TEST
THIS LOCALLY BEFORE USING THIS METHOD ON A PRODUCTION
SYSTEM. ESPECIALLY IF YOUR SYSTEM IS NOT DEBIAN WHEEZY,
SINCE THAT'S THE ONLY ONE I HAVE TESTED.

THE QUICK WAY:

If you don't want to read the whole thing you can only
execute the commands I run and probably skip the
explanations.

I’ve only done this on Debian Wheezy. If you’re using
another distro the initrd layout and init scripts may be a
bit different. However, I suspect it looks very similar on
almost every distro out there. On Debian my root partition
is an ext3 partition.

The general idea is pretty simple: you can’t shrink a
mounted partition and it’s impossible to unmount or replace
your root partition in a live system, so we have to resize
the partition before it is mounted. What we’ll do to
accomplish this is change the initrd image to make the init
scripts resize the root partition before mounting it. This
is by far the most flexible and easy method to resize your
root partition I could think of. Most suggestions I found
on google required you to create separate OS on a new root
partition and boot into that, but I did not have any space
to create a new root partition on my remote machine.

First, we have to copy all the programs we need to resize
our partition onto the initial ram disk. For my ext3 file
system I need e2fsck and resize2fs. The programs are
depending on a few libraries, so you’ll need to copy those
to the new initrd image too. Libraries can also depend on
other libraries, make sure you recursively check
dependencies until you don’t have any missing dependencies
anymore.

Next, we need to edit the init script. Debian uses busybox
in its initrd image so the init script will be interpreted
by a bourne shell. If you look through the init script file
you’ll find the moment where the script mounts the root
file system:

Simply add in the commands to resize the file system right
before the the file system is mounted. Resize2fs in Debian
does not want to resize the file system before it is
forcefully checked. It may be wise to add the -p or -y
flag to e2fsck. -y will answer yes to all questions, this
could prevent a hung system but may cause more damage to
your files or filesystem. The -p flag will only
automatically answer yes to safe operations. resize2fs
takes two parameters, the first is the block device that
has the ext2 or ext3 file system and the second is the new
size you want to give it. By default the size is in
blocks, but you can append a unit to change that. ‘K’ for
kilobytes, ‘M’ for megabytes, ‘G’ for gigabytes and ‘T’ for
terabytes. If you don’t specify a size, it will enlarge
the file system to the total size of the partition or
logical volume. After adding the commands the init script
will look something like this:

And lastly, we need to add a new default boot option in
grub. In Debian grub’s configuration file is constructed
from various bits under /etc/grub.d. The resulting total
configuration file is put under /boot/grub/grub.cfg. Open
up the grub.cfg file in your favorite text editor and look
for the default entry. It’s usually the first one, but may
vary. Add a copy of the original entry to
/etc/grub.d/40_custom and change the initrd image to the
one we just created.

Finally, you can reboot the system. When it comes back
online (if it comes back grin) your file system will be
resized. Be aware that e2fsck and resizefs take a long
time on big disks. On my system the two took 2 hours for
a 1.5T filesystem. Thus don’t prematurely reboot your
system if it doesn’t come up again quickly.

Don’t forget to remove the new grub entry, so your file
system doesn’t get resized every time you boot.

Original howto at https://thunked.org/general/howto-shrink-a-remote-root-ext3-filesystem-t96.html