How to extend an ext4 partition and filesystem?
Solution 1
You must begin with the partition unmounted. If you can't unmount it (e.g. it's your root partition or something else the system needs to run), use something like System Rescue CD instead.
Run
parted
, orgparted
if you prefer a GUI, and resize the partition to use the extra space. I prefergparted
as it gives you a nice graphical representation, very similar to the one you've drawn in your question.resize2fs /dev/whatever
e2fsck /dev/whatever
(just to find out whether you are on the safe side)Remount your partition.
While I've never seen this fail, do back up your data first!
Solution 2
Yes, you can grow EXT4 fs online if you have partition already sorted. Have you got partition sorted? Have you got LVM?
sudo resize2fs /dev/drive_to_grow
fdisk
will resize your partition, true, but if this a root partition (or if fact any mounted partition) it will have to be unmounted first. So offline most likely!
As with anything related to disk/fs operations I strongly recommend to have backup, and tested, well understood, recovery process.
Solution 3
Using growpart
and resize2fs
example:
$ growpart /dev/sda 1
CHANGED: partition=1 start=2048 old: size=39999455 end=40001503 new: size=80000991,end=80003039
$ resize2fs /dev/sda1
resize2fs 1.45.4 (23-Sep-2019)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/sda1 is now 10000123 (4k) blocks long.
Solution 4
Note, on some VDS servers you could have non-primary root partition and need to resize Extended partition container first
For example, you've just upgraded your plan and have something like:
Disk /dev/vda: 83886080s
Number Start End Size Type File system Flags
1 2048s 194559s 192512s primary ext2 boot
2 196606s 51197951s 51001346s extended
5 196608s 51197951s 51001344s logical ext4
Here /dev/vda2 - is your Extended container. And /dev/vda5 - main partition that we need to resize to full available space.
The simpliest way:
apt-get -y install parted
parted /dev/vda unit s print all # print current data for a case
parted /dev/vda resizepart 2 yes -- -1s # resize /dev/vda2 first
parted /dev/vda resizepart 5 yes -- -1s # resize /dev/vda5
partprobe /dev/vda # re-read partition table
resize2fs /dev/vda5 # get your space
Solution 5
Parted doesn't work on ext4 on Centos. I had to use fdisk to delete and recreate the partition, which (I validated) works without losing data. I followed the steps at http://geekpeek.net/resize-filesystem-fdisk-resize2fs/. Here they are, in a nutshell:
$ sudo fdisk /dev/sdx
> c
> u
> p
> d
> p
> w
$ sudo fdisk /dev/sdx
> c
> u
> p
> n
> p
> 1
> (default)
> (default)
> p
> w
Related videos on Youtube
mimipc
Updated on September 18, 2022Comments
-
mimipc over 1 year
I have a 400GB disk with a 320GB ext4 partition. I would like to grow the ext4 partition to use the left space (80GB of free space).
+--------------------------------+--------+ | ext4 | Free | +--------------------------------+--------+
How could I do this?
I've seen people using
resize2fs
but I don't understand if it resizes the partition.Another solution would be to use
fdisk
but I don't want to delete my partition and loose data. How could I simply grow the partition without loosing any file?Note: I'm talking about an un-mounted data partition without LVM and I have backups, but I'd like to avoid spending some time on recovery.
-
mdpc almost 11 yearsThis type of question has been asked and answered a number of times that I am aware of. Have you consulted the archives here?
-
Shōgun8 almost 3 years@mimipc; have you tried the suggestion offered by @Chris Stryczynski? It is so much easier than making a CD, then booting from that CD, then doing all sorts of unnecessary steps.
-
-
mimipc almost 11 yearsThanks, I was using the graphical one and it doesn't support partition resizing. I've seen the option on the cli program, but could you tell me how to use it surely without loosing data? In
resize NUMBER START END
, are START and END the sector numbers? How would I know that? -
Icebreaker almost 11 yearsIt is always recommended to back up your data before doing any risky operation like resizing a partition.
parted
supports a choice of units — for example, use thes
suffix for sectors,B
orGB
for bytes,%
for percentage of device size, andcyl
for cylinders. -
Icebreaker almost 11 yearsThe
resize2fs
man page says: 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.). However, resizing a mounted filesystem is a more dangerous operation, since the kernel could easily freeze or crash while running rarely exercised code, leaving your filesystem in a bad state. -
Flup almost 11 yearsTrue -- but you don't want to be messing around with your partition table with the filesystem mounted. That's why I started with the fs unmounted.
-
Zac Thompson over 9 yearsFor ext4, might be
resize4fs
instead on RHEL 5. -
Icebreaker over 9 yearsWelcome to Server Fault! Please summarize the steps in the answer itself, to guard against link rot.
-
kasperd about 9 yearsGrowing the partition with your root file system should work just fine as long as:
1.
You don't change the starting sector number.2.
You reboot after changing the partition table before taking the next step. -
Deer Hunter over 8 yearsAnd yet, you make no backups.
-
pcnate over 7 yearsUsed this process but used gdisk for creating the partition. This allowed me to save the data and to create a partition that was > 2TB
-
stek29 over 7 yearsIf you use
fdisk
be sure to check you use MBR and not GPT (with hybrid MBR for example). -
rubo77 over 6 yearsOn my debian XEN server I resized my logical volume with
lvextend +40G /dev/vg0/volumeName
, then logged in as root in the vm and enteredresize2fs /dev/xvda2
which grows the running root filesystem to the new max. I am not sure if that was needed, but i rebooted the vm after this action. Everything seems fine. -
Chris over 6 years@rubo77 No, no need for a reboot,
-
starbeamrainbowlabs almost 5 yearsError: Invalid token -1s
-
Joe over 4 yearsNote that
parted
'sresizepart
command understands the size "100%", if you want the single partition to fill the entire device. -
Rémi over 3 yearsThis approach (first growing the primary e.g. vda2, then the extended partition e.g. vda6) also worked when using growpart.
-
Fred Flint over 3 yearsThis works for me on CE7
-
Shōgun8 almost 3 yearsThis is by far the best and the easiest suggestion; this should be the accepted answer.
-
rfrp almost 3 yearsThis works perfect for me. I had a VM, increase the size of the disk and then applied this steps. Run parted (print list, resizepart with the ID selected in the first command, put the new size of the disk and finally run resize2fs and works perfect.). The server it's a Ubuntu server 20.04.
-
gies0r over 2 yearsAlso works on Ubuntu 20.04
-
Admin almost 2 yearsThank you for this solution. Prevented me from pulling my hair out.