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:
- Current State: 20GB disk /dev/sda, 12GB for /dev/sda1 primary, 8GB for /dev/sda2 swap
- Desired State: 40GB, w/ 32GB primary and same 8GB swap.
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.
swapoff /dev/sda2fdisk /dev/sda- remove swap partition:
d,2 - remove original primary:
d,1 - recreate original primary:
n,2048(default beginning),1.8G(up to last 8GB of disk) - recreate swap:
n, (default which should be1.8G), (default which should be100%) - write and exit:
w
- remove swap partition:
- Grow filesystem on extended partition:
resize2fs /dev/sda1(only works for ext3/4 filesystems I think). - Recreate swap:
mkswap /dev/sda2 - Reenable swap:
swapon /dev/sda2