Expanding a filesystem is easiest done when there’s adequate storage available to house two full copies of the larger partition size. A common use-case is upgrading a storage medium to a larger one; for that example:

  1. Clone smaller storage medium to larger one
    1. sudo dd if=<smaller device> of=<larger device> bs=4M status=progress conv=fsync
      1. Alternatively, if both endpoints are small and/or not simultaneously mountable, take a middle step via ... if=<smaller device of=backup.img ..., switch drives, then ... if=backup.img of=<larger device> ....
  2. Use parted to expand the partition
    1. sudo parted <device> (typically /dev/sdX)
    2. If necessary, move other partitions around to create free space as needed
    3. Identify the partition number you want to expand; use print list. Make sure you identify the right device!
      1. For this example, I’m looking at /dev/sda, partition 2.
    4. Note its start / end / size
      1. In my example, partition 2 runs 273MB - 31.9GB for a total size of 31.6GB.
    5. Resize the partition: resizepart <partition> <new end>
      1. You can specify the end value as a disk location in a few ways (see help resizepart); in my example, I’m expanding it to all but the last 512MB of the disk, so resizepart 2 -512MB.
    6. Print the partition list again and confirm the partition changed as you expected
      1. In my example, it now runs 273MB - 63.4GB for a total size of 63.1GB (exactly as expected on a 64GB drive)
    7. quit parted once you’re done
  3. Confirm the kernel recognizes the new partition
    1. lsblk should show the size of the new partition (it might be slightly different than the parted table; this is normal and due to decimal gigabytes vs binary gibibytes)
  4. Clean the filesystem: sudo e2fsck -f <device>
  5. Resize the filesystem: sudo resize2fs <device>
  6. Re-clean the filesystem: sudo e2fsck -f <device>

At this point, you should be able to remount the filesystem and continue using it as normal. Viola!

Filesystem types

These notes are fairly specific for Linux systems. In particular, steps 4, 5, and 6 assume an ext2/ext3/ext4 filesystem. For other filesystems (FAT family, NTFS, HFS+, XFS, etc.), you’ll need to find alternative filesystem checkers and expanders.

You can check filesystem types beyond any doubt with sudo df -T, or sudo lsblk -f if it’s not mounted.

Be particularly careful when messing with Windows “C drives” — though it’s easy to expand NTFS filesystems, be very careful to make sure both the partition and filesystem UUIDs remain unchanged, or Windows won’t boot!