128

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.

2
  • This type of question has been asked and answered a number of times that I am aware of. Have you consulted the archives here?
    – mdpc
    May 22, 2013 at 2:00
  • @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.
    – Shōgun8
    Jun 14, 2021 at 3:50

10 Answers 10

120

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.

  1. Run parted, or gparted if you prefer a GUI, and resize the partition to use the extra space. I prefer gparted as it gives you a nice graphical representation, very similar to the one you've drawn in your question.

  2. resize2fs /dev/whatever

  3. e2fsck /dev/whatever (just to find out whether you are on the safe side)

  4. Remount your partition.

While I've never seen this fail, do back up your data first!

7
  • 17
    The 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. May 21, 2013 at 15:12
  • 1
    True -- 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.
    – Flup
    May 21, 2013 at 15:15
  • 2
    For ext4, might be resize4fs instead on RHEL 5. Sep 4, 2014 at 20:04
  • 2
    Growing 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.
    – kasperd
    Mar 27, 2015 at 11:23
  • 3
    Note that parted's resizepart command understands the size "100%", if you want the single partition to fill the entire device.
    – Joe
    Oct 20, 2019 at 19:28
58

Using growpart and resize2fs example:

Do note the space betwen /dev/sda and 1 in the case of growpart /dev/sda 1:

These are the two commands:

growpart  /dev/sda 1
resize2fs /dev/sda1
$ 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.

If this is not working as expected it might be due to an extended partition, in which case this answer should apply: https://unix.stackexchange.com/a/687489/124109

4
  • This works for me on CE7
    – Fred Flint
    Jan 8, 2021 at 20:27
  • 1
    Also works on Ubuntu 20.04
    – gies0r
    Nov 23, 2021 at 11:05
  • This even works on Centos 6.10 as of Sept 2022. You need to install cloud-utils-growpart (I used yum)
    – site80443
    Sep 8, 2022 at 14:18
  • Best option - just used on Ubuntu 20.04
    – yakatz
    Jan 25, 2023 at 19:20
36

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.

4
  • If you use fdisk be sure to check you use MBR and not GPT (with hybrid MBR for example).
    – stek29
    Oct 4, 2016 at 14:54
  • 3
    On my debian XEN server I resized my logical volume with lvextend +40G /dev/vg0/volumeName, then logged in as root in the vm and entered resize2fs /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.
    – rubo77
    Sep 6, 2017 at 0:43
  • 2
    @rubo77 No, no need for a reboot,
    – Chris
    Sep 11, 2017 at 16:05
  • 2
    2022 update: parted can extend the partition even if it is mounted and used (worked for me without data loss)
    – eNca
    Aug 18, 2022 at 7:02
13

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
3
  • What really helped me, was seeing that you can set END to negative number. Usually you don't know the exact linux size of a resized partition - you don't know how to set END argument of resizepart. Setting it to -1s will resize your partition to max.
    – srigi
    Jul 2, 2018 at 20:27
  • Error: Invalid token -1s Jul 24, 2019 at 18:11
  • 1
    This approach (first growing the primary e.g. vda2, then the extended partition e.g. vda6) also worked when using growpart.
    – Rémi
    Nov 17, 2020 at 8:17
6

The accepted answer is somewhat outdated: resizing an ext4 filesystem is better done online rather than offline, as the online expansion code path is much more commonly used than the offline one.

The more difficult thing probably is to expand the underlying partition, unless you are using LVM and you have free space into your volume group. To expand a partition online, you can use fdisk or parted; then, you had to run kpartx <device> to inform the kernel of the change. If using LVM, you need to pvresize the just-resized partition before lvresize the volume.

Finally, you can issue resize2fs your filesystem.

1
  • Thanks for hinting me to use the pvresize command (e.g. pvresize /dev/sda2), which is the cause why lvresize does not work for me. Dec 5, 2023 at 7:27
3

parted can resize partitions and their filesystems.

2
  • Thanks, 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?
    – mimipc
    May 21, 2013 at 8:00
  • It 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 the s suffix for sectors, B or GB for bytes, % for percentage of device size, and cyl for cylinders. May 21, 2013 at 8:06
3

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
1
  • Used this process but used gdisk for creating the partition. This allowed me to save the data and to create a partition that was > 2TB
    – pcnate
    Sep 18, 2016 at 3:11
2

You can use fdisk or cfdisk to modify (or re-create) the partition (just be sure you won't change its start boundary), then reboot and resize2fs. But in general it's preferable to use LVM-2 instead of MBR/GPT due it it allows you to bring in those changes w/o kernel rebooting.

1
  • Yes, before resize2fs it's mandatory to cfdisk in order for the underlying partition grow. Thx Feb 14, 2023 at 14:03
2

Just to clarify how I do this for anyone that is still reading this thread.
If it is the boot partition you want to resize, then you must boot from a bootcd or bootusb "rescue" drive which is just a Live Linux. This allows you to run Linux on the machine other then the drive you want to make changes to.

I think the best "rescue" cd or usb in this case would be a bootable gparted USB or CD
Cases in which you need a rescue image
1. Partition to be expanded is the last partition, but you booted from it and cannot unmount it.
sda1 = boot (or swap)
sda2 = swap (or boot)
sda3 = /
Unused space

  1. If partition to be expanded is NOT the last partition, you must use gparted boot image to move or resize the partitions.
    sda1 = boot
    sda2 = / (partition to be expanded is not last)
    sda3 = swap
    unused space

If you want to change a mounted partition that is not the "root" partition (/), like /home which is a different partition, then there is no need to use a rescue image. This is especially true if is the last partition.
sda1 = boot
sda2 = /
sda3 = /home
unused space
-or-
sda1 = /
sda2 = /home
unused space

This is the situation I have when I take a image from a smaller drive or SSD and move it to a larger SSD.

In this case, you just need to unmount /home while you extend it. But to unmount /home, you need to make sure you are not logged into any user account whose home directory is within it. Since "root" user's home directory is directly under the system root / as in /root, if you can log into root, then you can unmount /home

I logout of a GUI (KDE / Gnome / etc) session and use [CTRL] + [ALT] + [F1] to bring up a shell session.
If you just logged out of a user log in, it can take Linux 20-30 seconds to finish closing any files, so you might get errors trying to umount /home.
1. umount /home (I retry this command if it fails for up to 30 seconds, then go look at other sessions to see if I am "cd /home/xxx" somewhere.)
2. parted /dev/sda
a. resize x (x = partition you want to resize, use "p" to get a list of partitions)
b. Enter last sector of resize = "-1" (minus 1 means 1 sector from end of disk)
c. q (quits parted)
3 resize2fs /dev/sda"x" (x = partition to be resized. This also remounts the filesystem)
4. df -m (I check /home to verify it resized)
I have never lost files or corrupted the drive using this method.

Good luck

2
  • And yet, you make no backups. Dec 5, 2015 at 9:21
  • I was able to grow a mounted / partition with no issues. Feb 14, 2023 at 14:04
1

I was asked to grow a disk mounted as /scratch on a Dell server running CentOS 7 without interrupting current users. This was an XFS partition on a RAID 0 disk with a normal GPT disklabel. This would work for an ext4 partition in almost exactly the same way - see later.

This is how I did this without dismounting, rebooting or interrupting operations on the system. I just used Linux commands (and Dell's RAID tools):

  1. Growing the physical RAID volume was done by adding two new disks to the server and using the OpenManage Server Administrator tool to add them to the existing RAID volume. This took several days to complete, but involved no further human effort. The server continued to work with lower disk speeds during the reconstruction of the RAID disk.
  2. The size of the disk in blocks had grown from 15623782400 to 23435673600 (512-byte) blocks, but the value of /sys/block/sdb/size was still at the lower value. This was corrected using the command: echo 1 > /sys/block/sdb/device/rescan
    to make the disk driver update its knowledge of the disk size and shape. This seems a poorly-advertised feature: I guessed what it did from the name!
  3. I ran parted to edit the disklabel and move its copy at the end of the disk to the new end-of-disk. Fortunately, it guessed exactly what I wanted to do without encouragement and prompted me. First, the copy of the disklabel: Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix, by moving the backup to the end (and removing the old backup)? Fix/Ignore/Cancel? F
    And then the size of the disk as reflected in the disklabel: Warning: Not all of the space available to /dev/sdb appears to be used, you can fix the GPT to use all of the space (an extra 7811891200 blocks) or continue with the current setting? Fix/Ignore? F
    So I had to enter "F" twice and then "q" to exit with the disklabel (and its copy) corrected. This fixed most of what had to be fixed without any hard thinking or work.
  4. I had to edit the partition size to fill the disk, but parted wouldn't let me do that while the disk was still mounted - so I used fdisk instead: fdisk /dev/sdb d 1 n 1 (defaults were correct for full size of disk) t 1 11 (That's Microsoft basic data - which is the same as xfs). w
    Notice that I had established that it was using the correct starting block number, 2048. Otherwise, it would not have worked. I was fortunate to use two tools which had the same convention for starting block, but I did check the initial start block first.
  5. I also used parted at this point to give the partition the same name as it used to have since fdisk doesn't support naming partitions. A minor detail.
  6. I used partprobe /dev/sdb (and partprobe /dev/sdb1 for good measure) to make the OS read the partition table again. I think only the first command actually made a difference.
  7. Finally, I used xfs_growfs -d /scratch which took just 1.6 seconds to grow the partition to its final size, filling the newly-grown disk.

This would also work with ext4 volumes: the only difference would be using resize2fs /dev/sdb1 instead of xfs_growfs -d /scratch - and using the correct partition type number for ext4 instead of xfs. The fdisk command can list the type numbers for common disk systems, including ext4.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .