Extend linux file system on

How to extend an ext4 partition and filesystem?

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).

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.

This type of question has been asked and answered a number of times that I am aware of. Have you consulted the archives here?

@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.

10 Answers 10

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!

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.

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.

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.

Note that parted ‘s resizepart command understands the size «100%», if you want the single partition to fill the entire device.

Читайте также:  At команды huawei linux

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. 

This even works on Centos 6.10 as of Sept 2022. You need to install cloud-utils-growpart (I used yum )

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.

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.

2022 update: parted can extend the partition even if it is mounted and used (worked for me without data loss)

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.

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 

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.

Читайте также:  Magic keys in linux

This approach (first growing the primary e.g. vda2, then the extended partition e.g. vda6) also worked when using growpart.

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 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.

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 

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

parted can resize partitions and their filesystems.

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?

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.

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.

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.

Читайте также:  Linux показать путь текущей директории

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.

Источник

Оцените статью
Adblock
detector