How to Resize ext4 Partition on Linux Without Data Loss: Step-by-Step Guide for Expanding /dev/sdb1


4 views

When working with cloud servers, storage expansion is a common operation. In this case, we have:

  • Original disk: /dev/sda (200GB) with /dev/sda1 mounted as root (/)
  • Additional disk: /dev/sdb (recently expanded from 200GB to 400GB)
  • Existing partition: /dev/sdb1 (199.5GB ext4 filesystem mounted at /mnt)

Before proceeding:

  1. Backup important data (even though we're doing non-destructive operations)
  2. Ensure the disk is not mounted or in active use
  3. Verify the filesystem is healthy:
sudo umount /mnt
sudo e2fsck -f /dev/sdb1

We'll use parted to resize the partition:

sudo parted /dev/sdb
(parted) print free
(parted) resizepart 1 400GB
(parted) quit

For ext4 filesystems, use resize2fs:

sudo resize2fs /dev/sdb1

For XFS filesystems (alternative approach):

sudo xfs_growfs /mnt

After completion:

lsblk
df -h
sudo mount /dev/sdb1 /mnt

For older systems without parted:

sudo fdisk /dev/sdb
# Delete partition (d), create new (n) with same start sector
# Then write changes (w)
sudo partprobe /dev/sdb
sudo resize2fs /dev/sdb1
  • Partition alignment: Ensure the new partition ends at sector boundaries
  • Virtual environments: Some hypervisors require rescanning the disk:
echo 1 > /sys/class/block/sdb/device/rescan

For frequent operations, consider this bash script:

#!/bin/bash
DEVICE="/dev/sdb"
PARTITION="/dev/sdb1"
MOUNTPOINT="/mnt"

umount $MOUNTPOINT
e2fsck -f $PARTITION
parted $DEVICE resizepart 1 100%
resize2fs $PARTITION
mount $PARTITION $MOUNTPOINT

When working with cloud servers like OVH, we often need to expand storage capacity dynamically. The current setup shows:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   200G  0 disk 
└─sda1   8:1    0   200G  0 part /
sdb      8:16   0   400G  0 disk 
└─sdb1   8:17   0 199.5G  0 part /mnt

Before making any changes:

  • Create a complete backup using rsync or snapshot the volume
  • Unmount the partition if possible (umount /mnt)
  • Ensure no processes are using the mount point (lsof | grep /mnt)

1. Delete and Recreate the Partition

Using fdisk to modify the partition table:

sudo fdisk /dev/sdb
# Command sequence:
d (delete partition)
n (new partition)
p (primary)
1 (partition number)
[Enter] (default first sector)
[Enter] (default last sector)
w (write changes)

2. Force Kernel to Reread Partition Table

After fdisk, run:

partprobe /dev/sdb
# Or alternatively:
echo 1 > /sys/block/sdb/device/rescan

3. Resize the Filesystem Online

For ext4 filesystems (most common case):

sudo resize2fs /dev/sdb1

For XFS filesystems (alternative approach):

sudo xfs_growfs /mnt

Check the new size:

df -h /mnt
lsblk

Common issues and fixes:

  • If resize2fs reports "filesystem is mounted", add -f flag to force
  • For "no space left" errors during resize, check kernel messages (dmesg)

For GUI lovers or complex scenarios:

  1. Download GParted ISO
  2. Attach to your cloud server as virtual CD
  3. Boot into recovery mode
  4. Use graphical interface to resize
  • Always have backups before partition operations
  • The process is safer when the filesystem isn't mounted
  • For production systems, consider doing this during maintenance windows
  • LVM provides more flexible resizing options for future expansions