Resizing the Partition of an AWS Marketplace AMI

By Tony Sui, Thu 03 August 2017, in category Linux

AWS

  
@

The Problem

When launching an EC2 instance on AWS, you can choose from not just the AMIs (Amazon Machine Instances) offered by AWS, but also from the Marketplace, which has a lot more options. All is generally great with the Marketplace AMIs, until you want to create a root device that is bigger than the default (8GB), for example 50GB.

You will have to resize both the partition and the file system, because the HVM (Hardware Virtual Machine) version of the AMI is created with a partitioned disk, instead of having the file system written directly onto the block device. While the file system can be resized online using the AWS console, the partition can't.

First use the following to list information about all available block devices:

lsblk

This will give us the output below. Note the partition on the bottom row only having a size of 8GB.

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

The partition can actually be resized (as in deleted and recreated), but the updated partition table won't be available because the partition is in use (and you can't unmount the root partition).

The Solution

1 - Install all available updates using yum

yum update -y

2 - Install the EPEL helper package

yum install -y epel-release

3 - Install cloud-init and dracut-modules-growroot

yum install -y cloud-init dracut-modules-growroot

This has to be done after installing epel-release, because otherwise YUM won't look in the EPEL repository, where dracut-modules-growroot is available.

4 - Rebuild all initramfs images.

rpm -qa kernel | sed 's/^kernel-//'  | xargs -I {} dracut -f /boot/initramfs-{}.img {}

5 - And Reboot

reboot

The Result

Now if you rerun

lsblk

You would expect to see the following on success:

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

Happy days :D

Full credit to Backslasher