How can I increase the size of the root partition of a system at runtime?

I have a partition that is not allocated after the root partition (which is also ext4), how can I add that unallocated space to the space allocated to the root partition without having to shutdown the server?

From man resize2fs: The resize2fs program does not manipulate the size of partitions. If you wish to enlarge a filesystem, you must make sure you can expand the size of the underlying partition first. This can be done using fdisk(8) by deleting the partition and recreating it with a larger size or using lvextend(8),if you're using the logical volume manager lvm(8). This question is about resizing the partition, not the filesystem. The distinction is subtle but very important.
–
Eliah KaganJun 3 '12 at 7:07

6

You can use fdisk to delete the root partion and then recreate it at the same starting block. fdisk will write out the change, but it won't take effect till after a reboot. after the reboot you can use the resize2fs program to send the disk to fill the partion.
–
JbecwarJun 14 '12 at 15:15

3

I have just resized an ext4 root partition online. Therefore I can confirm it's possible. But instead of passing /dev/sda* as parameter to resize2fs, you need to pass the logical volume name.
–
CDRDec 4 '12 at 22:37

5

I find the first paragraph of the resize2fs manpage most interesting for the initial question: The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing. (As of this writing, the Linux 2.6 kernel supports on-line resize for filesystems mounted using ext3 and ext4.).
–
mo'Dec 23 '12 at 14:33

There are two steps to this: First, the partition must be resized. If you're using LVM, it's easy; if you're using classic partitions, it's a bit more complicated, and might require a reboot (though you never have to boot another system or live CD).

One fragile but feasible approach (worked for me) is to use fdisk by first deleting the partition, then carefully recreating it with a larger size at the same position. You can do this while the partition is mounted, but you will need to reboot for the kernel to notice the changed partition table.

Again, it is critical that the new partition starts at the same block as the old. The Id should also match (83 for Linux systems). Be prepared to lose all your data at the slightest typo.

Remember to add the boot flag again, if necessary.

By now it should be apparent why people recommend using a live CD. ;-)

However, once the partition is resized (and the system rebooted, if necessary), it's a simple matter of running resize2fs on the file system, and you can do this even when it's mounted as the root partition.

The question asked how to do this without shutting down. You will need a program that can update the kernel entries for a partition table of a disk w/ mounted partition(s). I've read that running partprobe [<device>] should work, but I don't currently have a system where I can test it.
–
jbo5112Dec 2 '13 at 18:22

1

@jbo5112: As fdisk says, partprobe or kpartx may work instead of a reboot; see also this question. Even if you reboot, the solution is still preferable to using a live CD when it comes to downtime, where a simple reboot can be less than 10 s for a virtual machine. It's also faster in operator time, which is why I usually use this approach myself. :)
–
Søren LøvborgDec 3 '13 at 18:07

I booted my system with Ubuntu 12.04 Live CD and i resized ext4 partition with GParted. Worked good for me. Anyway, before this operation I backed up all my important data.
–
StandDuPpMay 28 '13 at 20:16

As stated before:
-exanding live from a root system is possible.(no difficulties, as the boot section ain't to be moved)

-shrinking a live root partition needs to be done from external boot device (boot from live system cd/usb-stick), as if theres any fault, mismatch ..whatever..your system hangs, nedds to be rebooted and will eventualy not be able to boot correctly.
Any sort of "but I did it and it works" is pure luck.