Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
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 '13 at 2:00
up vote 16 down vote accepted

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!

share|improve this answer
1  
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. – 200_success May 21 '13 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 '13 at 15:15
    
For ext4, might be resize4fs instead on RHEL 5. – Zac Thompson Sep 4 '14 at 20:04
1  
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 '15 at 11:23

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.

share|improve this answer

parted can resize partitions and their filesystems.

share|improve this answer
    
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 '13 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. – 200_success May 21 '13 at 8:06

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.

share|improve this answer

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
share|improve this answer
1  
Welcome to Server Fault! Please summarize the steps in the answer itself, to guard against link rot. – 200_success Dec 6 '14 at 2:28

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

share|improve this answer
    
And yet, you make no backups. – Deer Hunter Dec 5 '15 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.