Resizing the Root Partition on an Ext4 File System: A Guide to Swapping Out Your Swap Partition

Have you ever run out of space on your root partition and wished you could make it bigger? Or maybe you had a separate swap partition that you wanted to get rid of? Well, fear not, my friend, because today we’re going to be diving into the world of resizing partitions and making the switch to using a swap file instead of a partition.

First of all, let’s talk about why this is possible. The ext4 file system, which is the default file system for most modern Linux distributions, allows for resizing and modifying the partition layout on the fly. This is thanks to the advanced features of ext4, such as its ability to handle online resizing and the use of an advanced journaling system.

Now that we’ve got the basics out of the way, let’s get down to business.

  1. Backup your data

Before you do anything, it’s essential to backup your data. You never know what might go wrong during the resizing process, so it’s always better to be safe than sorry. You can use tools like rsync or tar to backup your important files to another location.

  1. Disable swap

Before we begin resizing the root partition, we need to disable the swap partition. This is because the swap partition may be in use while we are trying to resize it. You might also need to remove it so that you can increase the boundaries of the resize you need. To disable swap, you can use the following command:

sudo swapoff -a

  1. Resize the root partition

Next, we need to resize the root partition. We can do this using the resize2fs tool. In this example, we will be increasing the size of the root partition to 20GB:

sudo resize2fs /dev/sda2 20G

Note that you’ll need to replace “/dev/sda2” with the name of your root partition.

  1. Create the swap file

Now that we’ve resized the root partition, it’s time to create the swap file. A swap file is a file on your file system that is used as virtual memory. To create the swap file, we will use the fallocate tool. In this example, we will be creating a 4GB swap file:

sudo fallocate -l 4G /swapfile

  1. Configure the swap file

Once the swap file has been created, we need to configure it as a swap space. To do this, we will use the mkswap tool:

sudo mkswap /swapfile

  1. Enable the swap file. Finally, we need to enable the swap file so that it can be used as virtual memory. To enable the swap file, use the following command:

sudo swapon /swapfile

  1. Update /etc/fstab

At this point, the swap file is fully configured and ready to use. However, we need to update /etc/fstab to enable the swap file on boot. To do this, add the following line to /etc/fstab:

/swapfile none swap sw 0 0

Also, make sure you remove the old swap partition line. Otherwise, the system will try to check it every time you book taking more time!

And that’s it! You’ve successfully resized your root partition and switched from a swap partition to a swap file. Your system should now boot faster since it no longer has to test the swap partition on each boot.

In conclusion, resizing partitions and switching from a swap

partition to a swap file is a simple and effective way to manage your disk space and optimize your system’s performance. With the ext4 file system, the process is straightforward and can be done without having to take your system offline. Whether you’re running out of space on your root partition or just looking to streamline your system, I hope this guide has helped you accomplish your goals.

As always, when working with system configurations and disk partitions, it’s important to proceed with caution and to backup your data before making any changes. If you follow the steps outlined in this guide, you should have no trouble successfully resizing your root partition and switching to a swap file.

So, grab your terminal and get ready to play around with partitions and swap files. Who knows, you might just discover a new love for system administration.


Published by

Iván Mosquera Paulo

Software Engineer

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s