Extending Amazon AWS EC2 instance volumes
Step-by-step tutorial on how to extend EC2 instance volumes with zero downtime.
I did write this tutorial for my team's wiki some time ago and thought that I could share it here. Skip to the end of the article to see a short version :)
Long version
Open Elastic Block Storage -> Volumes in EC2 instances. Select the volume you want to extend and click Actions -> Modify Volume.
Enter a new volume size in the input field, and save it.
Connect to EC2 instance using your terminal.
Check current volume size:
rytis@aws:~$ lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTnvme0n1 259:0 0 40G 0 disk └─nvme0n1p1 259:1 0 30G 0 part /
Extend partition on volume:
rytis@aws:~$ sudo growpart /dev/nvme0n1 1CHANGED: partition=1 start=2048 old: size=62912479 end=62914527 new: size=83883999,end=83886047
Confirm that the new partition size is updated:
rytis@aws:~$ lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTnvme0n1 259:0 0 40G 0 disk └─nvme0n1p1 259:1 0 40G 0 part /
Check on filesystems type and size:
rytis@aws:~$ df -ThFilesystem Type Size Used Avail Use% Mounted onudev devtmpfs 3.8G 0 3.8G 0% /devtmpfs tmpfs 764M 81M 683M 11% /run/dev/nvme0n1p1 ext4 30G 26G 3.5G 89% /tmpfs tmpfs 3.8G 8.0K 3.8G 1% /dev/shmtmpfs tmpfs 5.0M 0 5.0M 0% /run/locktmpfs tmpfs 3.8G 0 3.8G 0% /sys/fs/cgrouptmpfs tmpfs 764M 0 764M 0% /run/user/1002
A. If filesystem type is ext2, ext3 or ext4:
rytis@aws:~$ sudo resize2fs /dev/nvme0n1p1resize2fs 1.42.13 (17-May-2015)Filesystem at /dev/nvme0n1p1 is mounted on /; on-line resizing requiredold_desc_blocks = 2, new_desc_blocks = 3The filesystem on /dev/nvme0n1p1 is now 10485499 (4k) blocks long.
Confirm that partition was extended:
rytis@aws:~$ df -hFilesystem Size Used Avail Use% Mounted onudev 3.8G 0 3.8G 0% /devtmpfs 764M 81M 683M 11% /run/dev/nvme0n1p1 39G 26G 14G 66% /tmpfs 3.8G 8.0K 3.8G 1% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 3.8G 0 3.8G 0% /sys/fs/cgrouptmpfs 764M 0 764M 0% /run/user/1002
B. If filesystem type is XFS:
1. If xfsprogs is not installed, install it:
sudo apt install xfsprogs
2. Extend partition:
sudo xfs_growfs -d /
3. Confirm changes:
rytis@aws:~$ df -hFilesystem Size Used Avail Use% Mounted onudev 3.8G 0 3.8G 0% /devtmpfs 764M 81M 683M 11% /run/dev/nvme0n1p1 39G 26G 14G 66% /tmpfs 3.8G 8.0K 3.8G 1% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 3.8G 0 3.8G 0% /sys/fs/cgrouptmpfs 764M 0 764M 0% /run/user/1002
TL;DR;
sudo sulsblkgrowpart /dev/nvme0n1 1df -Thresize2fs /dev/nvme0n1p1df -h
Even shorter:
sudo sugrowpart /dev/nvme0n1 1 && resize2fs /dev/nvme0n1p1 && df -h