How to Grow an ext2/3/4 File System with resize2fs

Simon Bennett
Simon Bennett · Mar 02, 2020
How to Grow an ext2/3/4 File System with resize2fs Artwork

Resizing or extending a partition is the process of increasing or decreasing the partition size. You can either expand or shrink the partition as per your needs. You will need to resize the partition when you are unsatisfied with the partition size and your system partition is running out of free space. Some partition is complete, while some partition still has sizeable free space.

If you are a system administrator working on the Linux system, resizing or growing the filesystem is challenging. However, when your partition size is complete, you will need to resize an existing partition. In that case, you can use the resize2fs utility to increase and decrease a filesystem size. The resize2fs is a command-line utility that allows you to resize ext2, ext3, or ext4 file systems.

Note: Extending a filesystem is a moderately high-risk operation. So it is recommended to backup your entire partition to prevent data loss.

Prerequisites

For this tutorial, we are using the Linux system with the following partition scheme:

/dev/sda : 15GB Disk with root and swap partitions.

/dev/sdb : 8GB Disk without any partition.

Getting Started

In this section, we will create a partition on the/dev/sdb disk with a size of 1GB, format this partition (/dev/sdb1) with ext4 filesystem, and mount the partition on the/mnt directory, and create some directories inside /mnt directory.

First, create a partition on disk /dev/sdb with 1GB size using the following command:

fdisk /dev/sdb

You should see the following output:

Welcome to fdisk (util-Linux 2.31.1). Changes will remain in memory only until you decide to write them. Be careful before using the write command. Command (m for help): n

Type n and hit Enter to create a new partition:

Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p):

Press Enter to create a primary partition:

Using default response p. Partition number (1-4, default 1): First sector (2048-16777215, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-16777215, default 16777215): +1G

Specify the size of the partition and hit Enter:

Created a new partition 1 of type 'Linux' and size 1 GiB. Partition #1 contains an ext4 signature. Do you want to remove the signature? [Y]es/[N]o: Y A write command will remove the signature. Command (m for help): w

Type w and hit Enter to apply the changes:

The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.

Once the partition has been created, format it with the ext4 filesystem using the following command:

mkfs.ext4 /dev/sdb1

Next, verify the size of your partition with the following command:

lsblk /dev/sdb

You should see the new partition /dev/sdb1 with 1GB size in the following output:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 8G 0 disk └─sdb1 8:17 0 1G 0 part

Next, mount the partition to the /mnt directory with the following command:

mount /dev/sdb1 /mnt

Next, create some directories inside /mnt with the following command:

mkdir /mnt/{dir1,dir2,dir3,dir4,dir5}

You can now verify the created directories with the following command:

ls /mnt

You should see all the directories in the following output:

dir1 dir2 dir3 dir4 dir5.

Resize or Grow Partition

In this section, we will delete the partition (/dev/sdb1), create a new large partition with a size of 4GB then resize the new partition.

Unmount the Partition

Linux supports online partition resizing for filesystem mounted with ext3 and ext4. If you want to resize the partition mounted with the ext2 filesystem, you will need to unmount the partition before resizing it.

Here, we are resizing the ext4 partition, so we do not need to unmount the partition.

If you are using the ext2 filesystem, you can unmount it with the following command:

umount /dev/sdb1

Delete the Partition

Now, run the following command to delete the partition (/dev/sdb1)

fdisk /dev/sdb

You should see the following output:

Welcome to fdisk (util-Linux 2.31.1). Changes will remain in memory only until you decide to write them. Be careful before using the write command. Command (m for help): d

Type d and hit Enter to delete the partition:

Selected partition 1 has been deleted. Command (m for help): p

Type p and hit Enter to print the partition table information:

Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x3d06b4e3 Command (m for help): w

Type w and hit Enter to apply the changes:

The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.

Create a New Partition

Next, run the following command to create a new partition size of 4GB.

fdisk /dev/sdb

You should see the following output:

Welcome to fdisk (util-Linux 2.31.1). Changes will remain in memory only until you decide to write them. Be careful before using the write command. Command (m for help): n

Type n and hit Enter to create a new partition:

Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p):

Press Enter to create a primary partition:

Using default response p. Partition number (1-4, default 1): First sector (2048-16777215, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-16777215, default 16777215): +4G

Specify the size of the partition and hit Enter:

Created a new partition 1 of type 'Linux' and size 4 GiB. Partition #1 contains an ext4 signature. Do you want to remove the signature? [Y]es/[N]o: N

Type N and hit Enter to continue:

Command (m for help): p

Type p and hit Enter to print the partition table:

Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x3d06b4e3 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 8390655 8388608 4G 83 Linux Filesystem/RAID signature on partition 1 will be wiped. Command (m for help): w

Type w and hit Enter to apply the changes:

The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks

Resize the New Partition

Next, you will need to tell the filesystem to use all the space available in the partition.

Before resizing the filesystem, you must run e2fsck on the new partition.

You can run it with the following command:

e2fsck -f /dev/sdb1

You should see the following output:

e2fsck 1.44.1 (24-Mar-2018) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sdb1: 16/65536 files (0.0% non-contiguous), 12960/262144 blocks

Finally, resize the partition using the resize2fs command as shown below:

resize2fs /dev/sdb1

Once the filesystem has been resized successfully, you should get the following output:

resize2fs 1.44.1 (24-Mar-2018) Resizing the filesystem on /dev/sdb1 to 1048576 (4k) blocks. The filesystem on /dev/sdb1 is now 1048576 (4k) blocks long.

Verify New Partition

At this point, your partition has been resized from 1GB to 4GB. You can check it with the following command:

lsblk /dev/sdb

It would be best if you got the following output:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 8G 0 disk └─sdb1 8:17 0 4G 0 part

Next, mount the new partition to the /mnt directory with the following command:

mount /dev/sdb1 /mnt/

Next, verify the /mnt directory and check whether all the directories are still available or not:

ls /mnt/

You can see that all the directories survived:

dir1 dir2 dir3 dir4 dir5 lost+found

Conclusion

This guide shows how to grow/extend the ext4 filesystem with the resize2fs utility. You can also decrease the filesystem using the resize2fs command.