Skip to content

Instantly share code, notes, and snippets.

@chriha
Created September 23, 2020 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriha/a289257e41b328f50b25adf7496247f7 to your computer and use it in GitHub Desktop.
Save chriha/a289257e41b328f50b25adf7496247f7 to your computer and use it in GitHub Desktop.
Resize EC2 storage

copied from https://stackoverflow.com/a/42800989/10760563

Before resizing the filesystem by "resize2fs" command you should first resize your partition:

let's list block devices attached to our box:

lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  16G  0 disk
└─xvda1 202:1    0   8G  0 part /

As you can see /dev/xvda1 is only 8 GiB partition on a 16 GiB device and there are no other partitions on the volume.


step-1) We will use "growpart" to resize 8G partition up to 16G:

# install "cloud-guest-utils" if it is not installed already
apt install cloud-guest-utils

# resize partition
growpart /dev/xvda 1

Let's check the result (you can see /dev/xvda1 is now 16G):

lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  16G  0 disk
└─xvda1 202:1    0  16G  0 part /

Lots of SO answers suggest to use fdisk with delete / recreate partitions, which is nasty, risky, error-prone process especially when we change boot drive.


step-2) resize file system to grow all the way to fully use new partition space

# Check before resizing ("Avail" shows 1.1G):
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      7.8G  6.3G  1.1G  86% /

# resize filesystem
resize2fs /dev/xvda1

# Check after resizing ("Avail" now shows 8.7G!-):
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       16G  6.3G  8.7G  42% /

And by the way, there's no need to stop instance and detach EBS volume to resize it anymore! 13-Feb-2017 Amazon announced: "Amazon EBS Update -- New Elastic Volumes Change Everything" See my other SO answer for details.

Update: Use sudo xfs_growfs /dev/xvda1 instead of resize2fs when XFS filesystem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment