When working with ext4 filesystems, we often need to resize partitions to match the exact filesystem size. The common scenario is when you've already shrunk your filesystem using resize2fs
, but now need to adjust the partition boundaries precisely.
# Current filesystem status example
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 493G 64G 404G 14% /
The discrepancy occurs because partition tools and filesystem tools use different units and calculation methods. While resize2fs
operates in filesystem blocks, partition tools like parted
work with sector-based measurements.
Here's the accurate approach to determine the correct partition size:
# First, get exact filesystem block count
$ sudo dumpe2fs -h /dev/sda2 | grep 'Block count'
Block count: 12935168
# Then get block size
$ sudo dumpe2fs -h /dev/sda2 | grep 'Block size'
Block size: 4096
# Calculate exact size in bytes
$ echo $((12935168 * 4096))
52998422528
With the exact byte count, use parted
to resize:
# Start parted interactively
$ sudo parted /dev/sda
# In parted shell:
(parted) unit B
(parted) print free
(parted) resizepart 2 52998422528
(parted) quit
For non-interactive operation, you can use this one-liner:
echo "resizepart 2 52998422528" | sudo parted /dev/sda ---pretend-input-tty
After resizing, always verify:
# Check filesystem consistency
$ sudo e2fsck -f /dev/sda2
# Verify the new partition boundaries
$ sudo parted /dev/sda unit B print
For those preferring fdisk
, the process is similar:
# Delete and recreate the partition with exact size
$ sudo fdisk /dev/sda
Command (m for help): d
Partition number (1-3, default 3): 2
Command (m for help): n
Partition type: p
Partition number (2-4, default 2): 2
First sector: (use original start sector)
Last sector: +52998422528B
Watch out for these issues:
- Alignment problems - ensure partition starts and ends on proper boundaries
- Filesystem metadata overhead - leave about 1% extra space
- Backup your data before any partition operation
For frequent use, create a script like:
#!/bin/bash
DEVICE="/dev/sda"
PARTITION=2
BLOCK_COUNT=$(sudo dumpe2fs -h ${DEVICE}${PARTITION} | grep 'Block count' | awk '{print $3}')
BLOCK_SIZE=$(sudo dumpe2fs -h ${DEVICE}${PARTITION} | grep 'Block size' | awk '{print $3}')
BYTE_SIZE=$((BLOCK_COUNT * BLOCK_SIZE))
echo "resizepart ${PARTITION} ${BYTE_SIZE}" | sudo parted ${DEVICE} ---pretend-input-tty
When working with Linux storage systems, it's common to encounter situations where you need to resize both the filesystem and its underlying partition. The key challenge is ensuring the partition precisely matches the filesystem size to avoid corruption or wasted space.
In the given example where we have a 493GB ext4 filesystem (showing as 500GB in df -h
due to binary/GB conversion), attempting to resize the partition to exactly 500GB actually creates a slightly smaller partition because:
- Tools interpret size values differently (binary vs decimal)
- Filesystem metadata requires additional space
- Partition alignment constraints affect final size
The most reliable approach is to use the filesystem's actual block count rather than human-readable sizes:
# Get exact filesystem size in bytes
sudo dumpe2fs /dev/sda2 | grep "Block count"
sudo dumpe2fs /dev/sda2 | grep "Block size"
# Calculate total size: block_count * block_size
# Example output might show:
# Block count: 25837568
# Block size: 4096
# Total size: 25837568 * 4096 = 105,830,678,528 bytes
Here's how to properly resize the partition using parted
:
# 1. Unmount the filesystem if possible
sudo umount /dev/sda2
# 2. Start parted in interactive mode
sudo parted /dev/sda
# 3. View current partition table
(parted) print free
# 4. Calculate exact end position (using previous calculation)
# Add 1 block (typically 4096 bytes) for safety margin
end_position=$((105830678528 + 4096))
# 5. Resize the partition (example for partition 2)
(parted) resizepart 2 105834772624B
# 6. Verify the changes
(parted) print
# 7. Exit parted and check filesystem
(parted) quit
sudo e2fsck -f /dev/sda2
If you prefer working directly with the filesystem tools:
# First shrink the filesystem to minimum size
sudo resize2fs /dev/sda2 -M
# Then expand to exact desired size (in 4K blocks)
sudo resize2fs /dev/sda2 25837568
# Now resize partition to match using sector count
# (assuming 512-byte sectors)
sudo parted /dev/sda resizepart 2 206719744s
- Always backup important data before resizing operations
- The filesystem must be clean (unmounted or mounted read-only)
- For LVM setups, you'll need to resize the LV first
- Different filesystem types may require different tools (xfs_growfs for XFS)
If you encounter errors after resizing:
# Check filesystem integrity
sudo e2fsck -fv /dev/sda2
# Verify partition boundaries
sudo parted /dev/sda unit s print
# Compare filesystem vs partition size
sudo blockdev --getsize64 /dev/sda2
sudo tune2fs -l /dev/sda2 | grep 'Block count'