💿 Increase partition size EC2 instance

Vikash Kumar Choubey
3 min readMay 13, 2023

--

While using AWS EC2 instance and EBS volumes, we often require to increase the volume size. AWS provides a very easy way to increase the volume size through AWS console, just to take a note we cannot decrease the size of EBS volume once we increase it.

Once we increase the volume size it will not directly reflect in ubuntu OS via df -h. To get the increased volume size we need to run some set of commands to leverage the extra size.

Steps to follow expand the volume size:

  • Check the existing size by df -h.
~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 3.0T 2.7T 274G 91% /
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 3.1G 1.2M 3.1G 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/nvme0n1p15 105M 6.1M 99M 6% /boot/efi
tmpfs 1.6G 4.0K 1.6G 1% /run/user/1000
  • Run lsblk to get the partition details of system. As we can see I have a single disk with 3 partition.
~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 24.4M 1 loop /snap/amazon-ssm-agent/6312
loop2 7:2 0 55.6M 1 loop /snap/core18/2721
loop3 7:3 0 63.3M 1 loop /snap/core20/1852
loop4 7:4 0 111.9M 1 loop /snap/lxd/24322
loop5 7:5 0 53.2M 1 loop /snap/snapd/18933
loop6 7:6 0 53.2M 1 loop /snap/snapd/19122
loop7 7:7 0 63.3M 1 loop /snap/core20/1879
loop8 7:8 0 55.6M 1 loop /snap/core18/2745
nvme0n1 259:0 0 3.3T 0 disk
├─nvme0n1p1 259:1 0 3T 0 part /
├─nvme0n1p14 259:2 0 4M 0 part
└─nvme0n1p15 259:3 0 106M 0 part /boot/efi
  • Once after getting the partition details, time to check the file system on partition. We can see below my partition type is ext4.
~$ sudo file -s /dev/nvme?n*
/dev/nvme0n1: DOS/MBR boot sector, extended partition table (last)
/dev/nvme0n1p1: Linux rev 1.0 ext4 filesystem data, UUID=<uuid>, volume name "cloudimg-rootfs" (needs journal recovery) (extents) (64bit) (large files) (huge files)
/dev/nvme0n1p14: data
/dev/nvme0n1p15: DOS/MBR boot sector, code offset 0x28+2, OEM-ID "mkfs.fat", Media descriptor 0xf8, sectors/track 32, heads 8, sectors 217088 (volumes > 32 MB), FAT (32 bit), sectors/FAT 1670, reserved 0x1, serial number 0x000, label: "UEFI"
  • Here comes the final 2 commands to complete the process, first is growpart, we have to grow the partition before extending the partition
~$ sudo growpart /dev/nvme0n1 1
CHANGED: partition=1 start=227328 old: size=6511429599 end=6511656927 new: size=7140575199 end=7140802527
  • Lastly we need to do resize2fs to extend the partition size. Here my partition was of ext4 type that's why I used resize2fs command but for xfs partition you can use xfs_growfs command.
~$ sudo resize2fs /dev/nvme0n1p1
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/nvme0n1p1 is mounted on /; on-line resizing required
old_desc_blocks = 389, new_desc_blocks = 426
The filesystem on /dev/nvme0n1p1 is now 892571899 (4k) blocks long.
  • Lets check the updated partition size:
~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 3.3T 2.7T 563G 83% /
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 3.1G 1.2M 3.1G 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/nvme0n1p15 105M 6.1M 99M 6% /boot/efi
tmpfs 1.6G 4.0K 1.6G 1% /run/user/1000

Conclusion

AWS provide a easy way to extend the EBS volume size, reiterating we cannot reduce the volume size. We need to extend the size in OS manually to get the changes.

--

--