How to Safely Resize an Ext4 Partition Without Data Loss Using resize2fs and fdisk


3 views

Before proceeding with partition resizing, ensure:

  • The partition is unmounted (check with umount /dev/sdX#)
  • You have a complete backup (even though we're using safe methods)
  • The filesystem is clean (run e2fsck -f /dev/sdX#)
  • You're working on a physical machine or have console access for virtual machines

First, examine your current disk configuration:

fdisk -l /dev/sdX

Example output for a 400GB disk:

Disk /dev/sda: 400 GiB, 429496729600 bytes, 838860800 sectors
Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1        2048 671088639 671086592  320G 83 Linux

Using fdisk to extend the partition:

fdisk /dev/sdX

Interactive commands sequence:

d (delete partition)
n (create new partition)
p (primary)
1 (partition number)
2048 (first sector - must match original)
[Enter] (accept default last sector)
w (write changes)

Critical note: This process is safe because:

  • We're recreating the partition with identical starting sector
  • Only the ending sector changes to utilize free space
  • No actual data is overwritten during this operation

After modifying the partition table, resize the ext4 filesystem:

resize2fs /dev/sdX#

For our example case:

resize2fs /dev/sda1

The command will output progress information:

resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
resize2fs: On-line resizing not supported.

For more modern systems, parted can be simpler:

parted /dev/sdX

Then in the interactive shell:

print free
resizepart 1
[New end size in MB or %]
quit

After resizing, verify the changes:

df -h
lsblk
fdisk -l /dev/sdX

Common issues and solutions:

# If resize2fs reports "filesystem is mounted"
umount /dev/sdX#

# If partition table changes aren't recognized
partprobe /dev/sdX

# For filesystem check
e2fsck -f /dev/sdX#

For those who frequently perform this operation:

#!/bin/bash
DEVICE="/dev/sda"
PARTITION=1

# Unmount if mounted
umount ${DEVICE}${PARTITION} 2>/dev/null

# Resize partition using parted
parted ${DEVICE} --script resizepart ${PARTITION} 100%

# Resize filesystem
resize2fs ${DEVICE}${PARTITION}

echo "Resize operation completed successfully"

The key thing to remember when extending an ext4 partition is that there are two distinct operations:

  1. Resizing the partition itself (modifying the partition table)
  2. Resizing the filesystem (expanding ext4 to fill the new space)

Before proceeding, ensure:

# Check current partition layout
lsblk
sudo fdisk -l /dev/sdX

# Verify filesystem health
sudo e2fsck -f /dev/sdX1

1. Delete and Recreate the Partition

Using fdisk (this won't erase data as we're preserving the start sector):

sudo fdisk /dev/sdX
# Command sequence:
# p (print current layout)
# d (delete partition)
# n (new partition)
# p (primary)
# 1 (partition number)
# [Enter] (same first sector)
# [Enter] (new end sector - use all space)
# w (write changes)

2. Inform Kernel About Partition Changes

sudo partprobe /dev/sdX
# Alternative if partprobe not available:
sudo blockdev --rereadpt /dev/sdX

3. Resize the ext4 Filesystem

sudo resize2fs /dev/sdX1

For newer systems, parted might be more straightforward:

sudo parted /dev/sdX
# Command sequence:
# print free
# resizepart 1
# [NewEndPoint] (like 400GB)
# quit
# Check new partition size
sudo fdisk -l /dev/sdX

# Verify filesystem filled the space
df -h
sudo dumpe2fs -h /dev/sdX1 | grep 'Block count'
  • Extended vs Primary Partitions: If working with extended partitions, you'll need to resize the extended container first
  • Filesystem Errors: Always run e2fsck before resizing
  • Mounted Partitions: Absolutely must be unmounted during this process

For scripted solutions, consider this safe approach:

#!/bin/bash
DISK="/dev/sdX"
PARTITION="${DISK}1"

# Unmount if mounted
umount $PARTITION 2>/dev/null

# Resize partition
echo -e "d\nn\np\n1\n\n\nw\n" | fdisk $DISK

# Refresh partition table
partprobe $DISK

# Resize filesystem
resize2fs $PARTITION