How to Resize AWS EBS Volume (NVMe) and Extend Linux Partitions
This tutorial can be applied to extend your EC2 Linux instance’s EBS root volume size running on a current-generation instance without detaching and reattaching the volume by using the Amazon EBS Elastic Volumes feature. If you are confusing how to do it because it is your first time doing it, hope it helps!
Before modifying the EBS Volume — Snapshot
Create a snapshot for it, if something happens after extending the volume and you need to roll back the changes. Here are the steps:
- Login to AWS Console
- Choose EC2 from the services list
- Choose Snapshots from the left side menu
4. Wait until the snapshot is ready
Extend the EBS using Elastic Volumes
Use the following steps:
- Choose EC2 from the services list
- Choose Volumes under the Elastic Block Store menu
- Select the volume to modify, and then choose Actions — Modify Volume
4. An options window will show as below one:
Set the new size for your EBS volume (for the above example extended a 20GB to 80GB volume). After you have finished modifying the volume, choose Modify. When prompted for confirmation, choose Yes.
Extend Linux Partition
Modifying volume size has no practical effect until you also extend the volume’s file system to make use of the new storage capacity. Extend the partition by following these steps:
- Connect SSH to your EC2 instance, log in as root, and run this df -h command:
[fanio ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 7.7G 0 7.7G 0% /dev
tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs 7.7G 727M 7.0G 10% /run
tmpfs 7.7G 0 7.7G 0% /sys/fs/cgroup
/dev/nvme0n1p2 20G 19G 1.4G 94% /
tmpfs 1.6G 0 1.6G 0% /run/user/1000
See the above output, /dev/nvme0n1p2 hasn’t reflected the new size, it still has the original size of 20GB.
2. Check the volume and partition that must be extended, type the lsblk command to list your NVMe block devices.
[fanio ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 50G 0 disk
|-nvme0n1p1 259:1 0 1M 0 part
`-nvme0n1p2 259:2 0 20G 0 part /
The above output shows the size of root volume, nvme0n1 reflects the new size 50GB. And the size of the partition nvme0n1p2 (partition number 2) reflects the original size, 20GB, and must be extended before you can extend the file system.
3. To extend the partition, type the following command: sudo growpart /dev/nvme0n1 2
Notice that there is a space between the device name and the partition number
[fanio ~]# growpart /dev/nvme0n1 2
CHANGED: partition=2 start=4096 old: size=41938911 end=41943007 new: size=104853471 end=104857567
Use the lsblk command to check the volume size of the partition
[fanio ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 50G 0 disk
|-nvme0n1p1 259:1 0 1M 0 part
`-nvme0n1p2 259:2 0 50G 0 part /
Cool! The partition has a new size already!
4. Check the file system type the following command: df -T
[fanio ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
devtmpfs devtmpfs 8002616 0 8002616 0% /dev
tmpfs tmpfs 8033140 0 8033140 0% /dev/shm
tmpfs tmpfs 8033140 736160 7296980 10% /run
tmpfs tmpfs 8033140 0 8033140 0% /sys/fs/cgroup
/dev/nvme0n1p2 xfs 20959212 19531080 1428132 94% /
tmpfs tmpfs 1606628 0 1606628 0% /run/user/1000
For this partition, nvme0n1p2 has a boot volume with an XFS file system.
5. Last step! In this example, / is the volume mount points shown in the output for df -h.
To extend the file system on each volume — since the file system is an XFS, use the xfs_growfs command:
[fanio ~]$ sudo xfs_growfs -d /
If your filesystem is an ext2, ext3, or ext4, type this command:
[fanio ~]$ sudo resize2fs /dev/xvda1
or please refer to the documentation for your file system for instructions.
6. Finally, if everything went smoothly, check that the file system reflects the increased volume size, use the df -h command:
[fanio ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 7.7G 0 7.7G 0% /dev
tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs 7.7G 727M 7.0G 10% /run
tmpfs 7.7G 0 7.7G 0% /sys/fs/cgroup
/dev/nvme0n1p2 50G 19G 32G 38% /
tmpfs 1.6G 0 1.6G 0% /run/user/1000
Yes yes done! Woohoooo