Managing disk space on a Linux server or workstation often involves modifying existing partitions. As your storage requirements change, you may need to expand a partition to accommodate more data or shrink it to make room for a new operating system. While resizing partitions is a routine administrative task, it carries a significant risk of catastrophic data loss if performed incorrectly.
This guide demonstrates how to safely resize an Ext4 partition—the most common Linux file system—using standard command-line tools. We will cover how to expand a partition to use unallocated space and how to safely shrink a partition.
Critical Pre-requisites and Warnings
Before executing any partition modification commands, you must observe the following rules:
- Backup your data: Do not proceed without a complete, verified backup of your critical files. A single typographical error or a sudden power failure during a resize operation can destroy your file system.
- Do not shrink active root partitions: You cannot shrink a file system that is currently mounted and actively running the operating system (e.g.,
/). To shrink the root partition, you must boot from a Live USB or recovery environment. - Understand the two-step process: Resizing involves two distinct layers: the partition (the physical boundary on the disk) and the file system (the software structure formatting that boundary). You must modify both, and the order of operations is crucial.
How to Expand an Ext4 Partition
Expanding a partition is generally safer than shrinking one because it can often be done while the file system is mounted and active (online resizing).
Step 1: Identify the Disk and Partition
First, locate the partition you wish to expand and ensure there is unallocated space available immediately after it on the disk.
lsblk
Note the device name (e.g., /dev/sda1 or /dev/nvme0n1p2).
Step 2: Resize the Partition Boundary
We will use growpart, a utility specifically designed to extend a partition to fill available space.
sudo growpart /dev/sda 1
Note: There is a mandatory space between the disk name (/dev/sda) and the partition number (1).
If the command is successful, the partition boundary has been expanded. However, the Ext4 file system inside it is still the original size.
Step 3: Resize the File System
To instruct the file system to expand into the newly provided space, use the resize2fs command.
sudo resize2fs /dev/sda1
This command will automatically grow the Ext4 file system to match the new boundaries of the partition. You can verify the new size by running df -h.
How to Shrink an Ext4 Partition
Shrinking is much more dangerous than expanding. You must shrink the file system first, and then shrink the partition boundary. Doing this in the reverse order will instantly truncate your data.
Important: You cannot shrink a mounted file system. You must unmount it first.
Step 1: Unmount the File System
sudo umount /dev/sda1
Step 2: Check the File System for Errors
You must ensure the file system is completely clean before shrinking it. The resize2fs tool will refuse to operate if the file system is not checked.
sudo e2fsck -f /dev/sda1
Step 3: Shrink the File System
Now, shrink the Ext4 file system to your desired target size. For example, to shrink it to 50 Gigabytes:
sudo resize2fs /dev/sda1 50G
Step 4: Shrink the Partition Boundary
Once the file system is reduced, you must shrink the physical partition using a tool like fdisk or parted. We will use parted.
sudo parted /dev/sda
(parted) resizepart 1 50G
(parted) quit
Caution: Ensure the size you specify in parted is exactly the same or slightly larger than the size you specified in resize2fs. If the partition boundary is smaller than the file system, you will destroy your data.
Step 5: Remount and Verify
Finally, remount the partition and check the available space.
sudo mount /dev/sda1 /mnt
df -h