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?
I hate to sound like a stick in the mud, but this entails a fair bit of risk? Why does this need to happen? Is uptime the main constraint?
You can’t resize a partition to the left, because that would actually be a move.
Increasing the size of ext4 parititions online is easy. The difficult part would be shrinking (your headline is about “resizing”). For people interested by ANY manipulation on a root partition (move, shrink, change filesystem, device) at runtime should consult my answer: askubuntu.com/a/728141/21888
318
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:
$ sudo fdisk /dev/sda
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 9437183 4717568 83 Linux
Command (m for help): d
Selected partition 1
Command (m for help): p
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Created a new partition 1 of type 'Linux' and of size 10 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: N
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sda1 2048 10485759 5241856 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
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 use partprobe
or kpartx
(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:
$ sudo resize2fs /dev/sda1
This worked perfectly for me. However I did additionally ensure that the boot flag kept is original state.
– Augustus KlingAug 10, 2012 at 20:56
@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, 2013 at 18:07
@Raymond: If memory pressure allows (see free -h
), disable the swap (swapoff /dev/sda2
), change the partition table (including deleting and recreating the swap partition) and either 1) reboot or 2) reload the partition table and swapon
again. (If memory’s too tight to disable swap temporarily, you can still create and enable a new swap partition (/dev/sda3
), then swapoff sda2
; but then you’ll have to update /etc/fstab
with the new swap device name.)
– Søren LøvborgJul 8, 2015 at 21:15
If you are using vmware and have extended the size of the disk, you will have to run sudo lshw -C disk to rescan the filesystems so the vm recognises the bigger drive. Then follow the instructions above.
What about shrinking?
– Aaron FrankeJul 28, 2018 at 7:09
An easier solution – use growpart <device> <partition>
:
growpart /dev/xvda 1 # Grows the partition; note the space
resize2fs /dev/xvda1 # Grows the filesystem
As always, back up your partition table (sfdisk -d /dev/xvda > partition_bak.dmp
) just in case.
growpart
is part of cloud-utils. In case you don’t have it, you can install with:
sudo apt-get install cloud-utils
- 4This was perfect for resizing the root partition and filesystem of my AWS VM. Cheers. – MetaFight Aug 10, 2018 at 0:00
- 17growpart is part of cloud-utils. In case you don’t have installed, you can install with
apt-get install cloud-utils
– klor Aug 21, 2018 at 19:50 - 2@klor : from cloud-guest-utils package – Pierre-Damien Sep 27, 2018 at 11:39
- 3@NickODell – I would say that the main reason for the “designed for the cloud” part is that it would be… atypical to see HDDs changing size when using physical hosts and physical HDDs (with no abstraction layer on top, like LVM). But I see no reason why it would not work on a non-virtualized machine. – Bogd Jan 19, 2021 at 9:26
It is possible to do a on-line resize of a ext4 filesystem, even if it’s your root partition. Use the resize2fs
command.
sudo resize2fs /dev/sda1
EDIT: On-line shrinking is not allowed:
root@brunojcm-htpc:/home# resize2fs /dev/sda5 2654693
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sda5 is mounted on /; on-line resizing required
resize2fs: On-line shrinking not supported
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, 2012 at 7:07
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.
– James BecwarJun 14, 2012 at 15:15
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.
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.).
Please don’t muck with fdisk
when growpart
will do this very easily for you.
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
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…).
No reboot are required in the process, and this allows to bypass limitation of resize2fs
not being able to shrink ext4
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.
source from https://askubuntu.com/questions/24027/how-can-i-resize-an-ext-root-partition-at-runtime