Resizing Filesystems on RHEL 8

Reading Time: 6 minutes

Resizing Filesystems on RHEL 8 shows some examples of resizing filesystems using Red Hat Enterprise Linux.

Warning: Before anything, you must understand that this task can be hazardous. All the steps that are being performed here are being performed in a lab environment using a virtual machine. So, if you plan to do it in a production environment, we’ll not be responsible for any data loss or data unavailability. Be careful!

Note: We will show some examples using the ext4 and xfs filesystems!

Preparing the Disk: Creating the Partition Table

The first step is to create the partition table in our block device. In this case, for instance, our block device is the /dev/sdg with 100GB.

1- Check the block device size. As we can see, the block device /dev/sdg has no partitions and has a size of 100 GB:

lsblk -l | grep -i -E "name|sdg"

2- We can create an MBR/DOS or a GPT partition table. For this example, let’s create a GPT partition table. We’ve written an article about MBR and GPT. Click here to access this article:

parted -s /dev/sdg mklabel gpt

3- To confirm if the partition table was created:

parted /dev/sdg print

So, the GPT partition table has been created successfully!

Creating the Disk Partition and the Filesystem

After creating the partition table, we need to create the disk partition. So, let’s do that:

1- Define the unit as MiB and create a disk partition using all the available space:

parted -s /dev/sdg unit MiB
parted -s /dev/sdg mkpart primary ext4 1MiB 100%

2- Inspect the partition table to ensure that the disk partition has been created:

parted /dev/sdg print

3- The next step is to create the filesystem over the disk partition. We’ll use the ext4 filesystem:

mkfs.ext4 /dev/sdg1

4- Inspect details of the filesystem like the block count and block size:

dumpe2fs /dev/sdg1 | grep -i -E "block count|block size"

Note: In this case, the ext4 block size is 4KiB (4096 bytes).

Mounting the Disk Partition

To effectively use the disk partition, we need to mount it in a system directory. So, let’s break down all the necessary steps for that:

1- Create a local directory. In this case, for instance, we’ll create a directory under /mnt:

mkdir -p /mnt/dir-ext4-SDG1

2- To mount the disk partition /dev/sdg1 in the created directory:

mount -t ext4 /dev/sdg1 /mnt/dir-ext4-SDG1

The basic syntax for the mount command is:

-t ext4 = Defining the filesystem type to be used during the mount operation

/dev/sdg1 = Disk partition to be mounted

/mnt/dir-ext4-SDG1 = Local directory to be used as a mount point

3- Inspect to check if the disk partition was mounted:

lsblk -fi | grep -i -E "name|sdg"

As we can see, the disk partition has been mounted successfully.

4- Let’s create some files in this partition:

dd if=/dev/zero of=/mnt/dir-ext4-SDG1/file1.dd bs=1M count=250
dd if=/dev/zero of=/mnt/dir-ext4-SDG1/file2.dd bs=1M count=250
dd if=/dev/zero of=/mnt/dir-ext4-SDG1/file3.dd bs=1M count=250

5- Inspect the partition usage with “df”:

df -Th | grep -i -E "filesystem|sdg|used"

6- To remember, if you reboot your system, the disk partition will not mount automatically during the system boot. To persist the mount during reboots, we need to add the appropriate entry in the /etc/fstab. Let’s do that. Add the following entry in /etc/fstab configuration file:

/dev/sdg1       /mnt/dir-ext4-SDG1      ext4    defaults        0       0

7- Mount:

mount -a

After executing the “mount -a”, if everything is okay, it’s not expected to receive any error!

Shrink the Ext4 Filesystem

To shrink an ext4 filesystem, we must unmount it first!
There are two ways to do that:

  • Shrink the filesystem to the smallest safe size.
  • Resize the filesystem to a specific block count.

First and foremost, to resize an ext4 filesystem, you need to learn how to work with “blocks”. Let’s break down an example of that:

1- Let’s inspect the block details of our disk partition/filesystem /dev/sdg1:

dumpe2fs /dev/sdg1 | grep -i -E "block count|block size"

As we can see, our filesystem has 26213888 blocks, and each block has 4096 bytes. So, if we multiply the block count by the block size, we’ll have the disk partition/filesystem size in bytes. Let’s do this math using the terminal:

echo $((26213888 * 4096 / 1024 /1024 /1024))

The result of this operation will be the size of the filesystem in GB. – Note that this value is near the value shown with the “df” command:

2- Going forward, there is a way to identify the minimum block size that we can safely use, if we need to shrink the filesystem:

resize2fs -P /dev/sdg1

So, let’s work with this value. If the estimated minimum size of the filesystem is 634947 blocks, how can we find the value in GB?

echo $((VALUE_REPORTED_BY_RESIZE2FS_-P * BLOCK_SIZE / 1024 / 1024 / 1024))
                                                    KB     MG     GB

So, using the formula, the result is:

echo $((634947 * 4096 / 1024 / 1024 / 1024 ))
2

Answer: The minimum safe value to shrink the filesystem is 2GB.

Step-by-step to shrink the filesystem to the minimum block size:

1- If the filesystem is mounted, unmount it:

umount /dev/sdg1

2- Execute the “e2fsck”:

e2fsck -f /dev/sdg1

3- Shrink the filesystem to 634947 blocks (to remember, this number of blocks was provided by the resize2fs -P command):

resize2fs /dev/sdg1 634947

4- Mount and inspect to confirm:

mount -a
systemctl daemon-reload
df -Th | grep -i -E "filesystem|sdg"

As we can see, the new size is around 2GB!

5- Check if all created files are in the mount point:

As we can confirm, all created files are in the mount point!

Expand/Resize the Ext4 Filesystem

To expand an ext4 filesystem, we do not need to unmount it if it is mounted!
Let’s break down this task:

1- The first recommended step is to confirm the size of the block device – in our case, for instance, there is only one partition in our block device – if you have more than one partition, you may need to adjust the procedure:

lsblk /dev/sdg1

2- Confirm the current size of the filesystem using the “df”:

Subtract the current filesystem size from the total device size to find how much you can grow.

3- To grow using all the available space:

resize2fs /dev/sdg1

4- Inspect and confirm it:

df -Th | grep -i -E "filesystem|sdg"

As we can confirm, the filesystem was grown successfully!

Working with the XFS Filesystem

An XFS filesystem cannot be shrunk!

You can resize XFS with xfs_growfs, but only online (while mounted).

To wrap this up:

FilesystemResize ToolShrink SupportUnmount Required?
ext4resize2fs✅ Yes✅ Only for shrinking
XFSxfs_growfs❌ No❌ Always online

That’s it for now 🙂