47

Today I tried this on my machine with OpenSUSE 12.3 (kernel 3.7):

# resize2fs /dev/mapper/system-srv 2G
resize2fs 1.42.6 (21-Sep-2012)
Filesystem at /dev/mapper/system-srv is mounted on /srv; on-line resizing required
resize2fs: On-line shrinking not supported

/dev/mapper/system-srv is an EXT4 volume.

Is it really unsupported or I am missing something?

66

As the message said, you can only grow a filesystem on-line. If you want to shrink it, you will need to unmount it first.

According to the ext4 filesystem maintainer, Ted Ts'o:

Sorry, on-line shrinking is not supported.

  • 11
    That's a pity... – Alicia Aug 1 '13 at 19:06
  • 5
    What's with the downvote? – Michael Hampton Sep 23 '14 at 17:43
  • 15
    @MichaelHampton - some moron shooting the messenger? :D – tink Sep 29 '15 at 22:49
  • 1
    That was 10 years ago. Has this been implemented yet? – duane Jun 17 at 18:07
  • @duane No. It has not. It is not likely ever to be, either. – Michael Hampton Jun 18 at 0:49
16

Yes, you can shrink/move/grow a root partition without any reboots (nor livecd, nor usbkey): consult this answer. It's very well written and easy to follow, although quite long and a little risky.

resize2fs can't shrink online ext4 partitions. This limitation, when applied on the root partition may lead you to think you can't avoid rebooting the host to allow shrinking the root partition, which might be something you want to avoid. The linked process will allow you to umount the root partition without any reboots. So in this light it is not strictly speaking online resizing in regards to the mounted state of the partition, however it remains an online resizing in regards to the network online status or reachability of the host.

Of course, if you only want to grow your ext4 partition, you should stick to the conventional working resize2fs solutions.

The general solution I've linked will work on any type of dedicated or VPS solution for instance.

TLDR; this solution implies to pivot_root to tmpfs so you can umount safely your root partition live and fiddle with it. Once done, you'll pivot_root back on your new root partition.

This allows pretty much any manipulation on the root file system (move it, change filesystem, changing it's physical device...).

I have personally used this, and it works very well on debian system also, but the guide was initially written in 2007 for redhat, the answer I've linked was updated for CentOS7. It's highly probable that it'll work on your OpenSUSE, although probably with some adaptation.

  • 2
    This is not 'online' in regards to comparison with 'online grow'. All services must be down and your root filesystem becomes inaccessible. Might be worth contrasting this with the livecd or initramfs/rdshell approach in this comment. I'd hate for people to be confused by this. – Brian Chrisman Jun 14 '16 at 23:16
  • 1
    @BrianChrisman This is indeed a gotcha that merited clarification and that I hope I've covered with an edit in my post. Many thanks for the feedback. – vaab Jun 15 '16 at 7:35
  • What I like about the referenced solution is that it's 'almost online'. I did this particular thing by encoding a small script and tools into the dracut command and launched the resultant initramfs from kexec. If this could somehow keep an ssh session open through the process, that would be best. I suppose if you build the faked root with sshd and needed files, start another sshd (because you're ssh'd in) on another port, then you can ssh to the other port and verify connectivity before continuing to lobotomize the system. – Brian Chrisman Sep 3 '16 at 23:33
12

If the case is remote server without console, you can shrink filesystem using initramfs while rebooting. You need to add resize2fs to initramfs and run it before mounting root.

Debian/Ubuntu example:

/etc/initramfs-tools/hooks/resizefs (executable):

#!/bin/sh

set -e

PREREQS=""

prereqs() { echo "$PREREQS"; }

case $1 in
    prereqs)
        prereqs
        exit 0
    ;;
esac

. /usr/share/initramfs-tools/hook-functions

copy_exec /sbin/e2fsck
copy_exec /sbin/resize2fs

exit 0

/etc/initramfs-tools/scripts/local-premount/resizefs (executable)

#!/bin/sh

set -e

PREREQS=""

prereqs() { echo "$PREREQS"; }

case "$1" in
    prereqs)
        prereqs
        exit 0
    ;;
esac

/sbin/e2fsck -yf /dev/sda1
/sbin/resize2fs /dev/sda1 5G
/sbin/e2fsck -yf /dev/sda1

Now run update-initramfs, reboot, check size, remove these scripts and update-intiramfs again.

For dracut (Fedora,CentOS) use the same logic (/usr/lib/dracut/modules.d).

This is very usefull for crypting or moving rootfs, with no free space available for the new partition.

  • 2
    This works like a charm. Note that you need to run update-initramfs with the -u option. – Diomidis Spinellis Jan 25 '18 at 18:19
  • Cool. In my case I also needed to update-grub. – Wojciech Kaczmarek Aug 3 '18 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.