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?
GUI (Ubuntu 14.04 and later): GParted v0.17 and later provide a nice GUI for this. (Older versions will refuse to resize a mounted partition).
Command line (any Ubuntu version): There are three steps to this.
Step 1. The partition must first be resized. If you're using LVM, it's easy, and you presumably know how to proceed. If you're using classic partitions, it's a bit more complicated, and may require a reboot (though you never have to boot another system or live CD).
This is how I do it: Use
fdisk
to first delete the partition (the idea is that the data on disk will be preserved), then carefully recreate it with a larger size at the same position.Example:
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.
To be on the safe side, you may also restore the boot flag (which according to Wikipedia is still required on some computers) by pressing
a
.See the comment section for what to do if your swap partition is in the way.
By now it should be apparent why people recommend using a live CD. ;-)
Step 2. As
fdisk
helpfully reminds you, you must reload the partition table before proceeding. The safest way is to simply reboot; but you can also usepartprobe
orkpartx
(more information).Step 3. Once the partition is resized and the partition table reloaded, 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.Example:
An easier solution - use
growpart <device> <partition>
:As always, back up your partition table (
sfdisk -d /dev/xvda > partition_bak.dmp
) just in case.It is possible to do a on-line resize of a ext4 filesystem, even if it's your root partition. Use the
resize2fs
command.EDIT: On-line shrinking is not allowed:
Yes, you can shrink/move/grow an online 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. So if you only want to grow your ext4 partition, you can stick to the conventional working
resize2fs
solutions.The general solution I've lnked will work on any type of dedicated or VPS solution for instance.
TLDR; this solution implies to
pivot_root
totmpfs
so you canumount
safely your root partition live and fiddle with it. Once done, you'llpivot_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...).
No reboot are required in the process, and this allows to bypass limitation of
resize2fs
not being able to shrinkext4
partitions.I have personally used this, and it works very well on debian system also, so it should work on Ubuntu. I'm very surprised not to see this in-depth solution a little more linked to the many question in stackexchange web sites that deals with the same issue.
Note: Of course if you want to grow your partition, a simple
resize2fs
will be enough as stated in numerous places and in other answers here.You could also just make use of GParted - as long as the partition you are resizing is not the one you booted from - else I suggest the live CD option is somewhat easier for newbies.
GParted basically does all of the steps - just based on a GUI fronted.
I would like to make an extension on the answer of @Søren Løvborg: extending the partition with a swap partition present.
First the layout of the disk after extending it:
So sda1 needs to be extended with the free space at the end of the disk, but the swap partition is in between them. This is how you can do it:
First we need to disable swap. Check how much it is used and if you can turn it off.
swap is unused here so we can turn it off
Now we will change the partition table:
(note: if you happen to have the first partition start at sector 63 instead of 2048, you need to add the option
-c=dos
)Note 1: the size of sda1 is the total amount of sectors minus the sector size of your swap partition: 14335999-2093058=12242941
Note 2: the command
a
is used to set the boot flag to a partition. Check the output of the firstparted
command. Usually this is the first partition.As fdisk mentions: the kernel is still using the old partition table so we need to reload it.
Now we need to run resize2fs on sda1 (do not forget this!)
Now, things are not over yet. As you've probably noticed sda2 is partitioned as type Linux (Ext4). For some reason, there is no way in fdisk to choose the type. So we have to alternate it using
cfdisk
Choose sda2 and change type to
82 Linux swap / Solaris
and make sure you write it (type yes to confirm)Now we can re-activate the swap
And finally turn it on:
The only thing we need to do is to update fstab to mount the swap partition automatically upon booting
And change the UUID of the swap partition to the output above:
Now all is well and you can reboot without problems.
Just finished resizing an ext4 root partition on a live system while the root was mount.
I just did this successfully without
umount
,pivot_root
, or temporary removal of the main partition, using parted 3.2 on Ubuntu 16.04, 4.4.0 kernel. To be cautious I did everything from a virtual console with networking disabled, and took a snapshot beforehand just in case, but the snapshot wasn't needed, so I could have just as well done this via SSH and without changing runlevels.Determine partition size:
parted /dev/sda1 print | egrep "Disk.*GB"
Optionally switch to multi-user mode without networking (must be done from a console, not SSH):
Optionally take a VM snapshot to be cautious.
Resize partition:
Resize filesystem:
resize2fs /dev/sda1
If anything goes wrong, you could restore your snapshot here. If all went fine, return to normal runlevel (obtained above) - normally 5:
init 5
. It may be better to do a full reboot at this point to make sure everything comes back properly (I had a date/ntp issue afterward).Follow these steps.
su
parted
p
to see the available partitionsrm PARTITION NUMBER
to create free space.resizepart ROOT NUMBER
and reboot system if neededparted
by typingexit
and in terminal typepartprobe
and hit enter (this can be done even after rebooting)resize2fs /dev/sda PARTITION NUMBER
and enjoy spacious root partition.As stated before:
expanding 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 there’s any fault, mismatch ..whatever..your system hangs, needs to be rebooted and will eventually not be able to boot correctly.
Any sort of "but I did it and it works" is pure luck.