Grow ext4 Partition

The problem

I’ve got a VM, and needed to grow the disk from 20GB to 40GB, but the disk was divided into two partitions. Fortunately this wasn’t too hard to do since the main partition I wanted to gro was formatted with ext4, which is easy to do using resize2fs. To summarize:

The solution

Approach: swap was at the end of the disk, so delete partitions, recreate primary with bigger size, then “move” swap to end. Follow steps as root.

  1. swapoff /dev/sda2
  2. fdisk /dev/sda
    1. remove swap partition: d, 2
    2. remove original primary: d, 1
    3. recreate original primary: n, 2048 (default beginning), 1.8G (up to last 8GB of disk)
    4. recreate swap: n, (default which should be 1.8G), (default which should be 100%)
    5. write and exit: w
  3. Grow filesystem on extended partition: resize2fs /dev/sda1 (only works for ext3/4 filesystems I think).
  4. Recreate swap: mkswap /dev/sda2
  5. Reenable swap: swapon /dev/sda2