How to Expand a Linux Filesystem When Running Out of Space

When managing a Linux server, you may encounter a situation where the root filesystem runs out of space. In this guide, we will walk through how to expand a Linux filesystem, focusing on cases where the root partition is nearly full.

Step 1: Check Disk Usage

Before expanding your filesystem, it is important to assess the current disk space usage. Use the following command to check the disk usage:

 df -H

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       2.2G  2.0G   87M  96% /

In this case, the root (/) partition is nearly full.

Step 2: Check Available Disk Space

To see if there is unallocated space on your disk, run:

lsblk

Example output:

NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda       8:0    0   10G  0 disk
├─sda1    8:1    0  2.1G  0 part /

Here, /dev/sda is 10GB, but /dev/sda1 (the root partition) is only 2.1GB, meaning there is unallocated space available.

Step 3: Check Filesystem Type

Different filesystems require different commands to resize. Run the following command to determine the filesystem type:

df -T /

Example output:

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sda1      ext4  2.2G  2.0G   87M  96% /

If the filesystem is ext4, use resize2fs. If it were XFS, we would use xfs_growfs.

Step 4: Expand the Partition

Since the root partition is actively in use, you need to modify it while the system is running. First, install the necessary tool if it’s not available:

apt update && apt install cloud-guest-utils -y

Then, expand the partition:

growpart /dev/sda 1

Verify the change with:

lsblk

Step 5: Resize the Filesystem

Now, resize the filesystem using the appropriate command:

If Using ext4:

resize2fs /dev/sda1

If Using XFS:

xfs_growfs /

(Note: The XFS command will only work if the filesystem is XFS.)

Step 6: Confirm the Expansion

Finally, check if the filesystem has been successfully resized:

df -H

The root partition should now have more space available.

Conclusion

Expanding a Linux filesystem is crucial when your root partition is running low on space. By following these steps, you can safely extend your partition and avoid system crashes due to insufficient disk space. If you encounter any issues, ensure your disk has unallocated space before resizing the filesystem.

Leave a Comment