How to Resize root EBS Volume on AWS Linux Instance

I have created a new CentOS Linux machine. I have selected 50GB of root volume during creation of instance but when system came online it was displaying only 8GB of disk is usable.

When i tried to resize root disk using resize2fs command, I got the following message:

resize2fs /dev/xvda1

The filesystem is already 16775167 blocks long. Nothing to do!

So, By following below steps you will be able to successfully resize volume to its full size which you have selected during instance creation.

Step 1. Keep Backups

We strongly recommended to take full backup (AMI) of your instance before implementing any changes. You can also create a snapshot of root disk.

Step 2. Check Current Partitioning

Now check the disk partitions using below command. You can see that /dev/xvda is 53GB in size but

df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 8G 3.7G 4.2G 45% /
tmpfs 1.8G 0 1.8G 0% /dev/shm
/usr/tmpDSK 485M 111M 349M 25% /tmp

fdisk -l

Disk /dev/xvda: 53.7 GB, 53687091200 bytes
Device Boot Start End Blocks Id System
/dev/xvda1 * 1 1045 8387584 83 Linux

Step 3. Increase Size of Volume

Now start with the disk re partitioning using set of following commands. Be carefull while executing the commands.

fdisk /dev/xvda

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Now change the display units to sectors using u switch.

Command (m for help): u
Changing display/entry units to sectors

Now print the partition table to check for disk details

Command (m for help): p

Disk /dev/xvda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00098461

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 16777215 8387584 83 Linux

Now delete the first partition using following command.

Command (m for help): d
Selected partition 1

Now create a new partition using below commands. For the first sector enter 2048 (as shows in above command output) and for last second just press enter to select all partition.

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-104857599, default 63): 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): [PRESS ENTER]
Using default value 104857599

Print the partition table again. You will see that new partition has occupied all disk space.

Command (m for help): p

Disk /dev/xvda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00098461

Device Boot Start End Blocks Id System
/dev/xvda1 2048 104857599 52427776 83 Linux

Now set the bootable flag on partition 1.

Command (m for help): a
Partition number (1-4): 1

Write disk partition permanently and exit.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Now reboot your system after making all above changes.

reboot

Now let’s resize file system using resize2fs command. Remember that you are resizing filesystem not formatting.

resize2fs /dev/xvda1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 4
Performing an on-line resize of /dev/xvda1 to 13106944 (4k) blocks.
The filesystem on /dev/xvda1 is now 13106944 blocks long.

Step 4. Verify Upgraded Disk

At this point your root volume has been resized successfully. Just verify your disk has been resizes properly

df -h

Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 50G 6.7G 40G 15% /
tmpfs 1.8G 0 1.8G 0% /dev/shm
/usr/tmpDSK 485M 111M 349M 25% /tmp