Changing the Disk Size of your AWS Instance


I do not want to pay more using more space than I need, so in this post I am going to reduce my disk size on the AWS EC2 instance to use less disk space. Let me start off by saying I do not know if AWS charges for “empty” space on their drives.  If they don’t then reducing your hard drive size is not really needed.  However, since I cannot determine that, forward ho!

It should also be noted that this procedure could be used for creating a larger drive as well, with a couple of minor changes.  There are lots of blog posts on that, but not so many that instruct you to reduce the size.

Once question you might ask is where did the “big” disk drive come from?  When I was tying to recover my AWS snapshot, I accidently added more disk space than I wanted.  Granted I only went from 8G to 100G, but I wanted to determine how to change the disk size of my root Linux partition.   As always, I ran into a couple of hiccups, but was able to get it to work once I stopped blindly copying instructions on other websites and really tried to understand what they were doing.

The best articles I found via research I listed below, but keep in mind that I did not follow them step by step.

Resizing:

http://serverfault.com/questions/332648/decreasing-root-disk-size-of-an-ebs-boot-ami-on-ec2/332670

https://www.howtoforge.com/linux_resizing_ext3_partitions

Creating Images:

http://robpickering.com/2010/07/create-a-full-backup-image-of-your-amazon-ec2-instance-2-129

How to Reduce a Disk Size on AWS

My current EC2 instance that is up and running using a 100G drive.  My old instance that crashed had 8G.  Here is what I did to reduce my operating drive size

Starting in the AWS Console

    1. Since I just did a snapshot of my working system, I did not need another one.  But if you are just starting to follow along at home, you need to create a snapshot of your working image (Mine was called “Current”) and then a new Volume from that snapshot (I labeled mine “Small Source”)
    2. My first step was to bring up a new EC2 instance with the min 8G.  I went with the free t2.micro instance and labeled that “New 8”
    3. Then I created a snapshot of “New 8” and from there created a new 8G volume and labeled it “Small Dest”
    4. I then restarted my “New 8” Ec2 instance and waited for it to come up.
    5. Once it was up, I attached “Small Source” – my 100G working drive to /dev/sdz. and I attached “Small Dest” – my new 8G drive to /dev/sdm

In summary “New 8” has three hard drives:

/dev/xcda – the drive it came with

/dev/sdz – the 100G “Small Source”

/dev/sdm – the 8G “Small Dest”

Launch a Console to the “New 8” EC2 instance

My goal now is to reduce the size of the “Small Source” such that it will fit on the “Small Dest” and copy the files across.  the next few commands are all done in the “New 8” Linux console.

See what the two drives look like

# fdisk -l /dev/sdz1
Disk /dev/sdz1: 107.4 GB, 107372068352 bytes, 209711071 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
# fdisk -l /dev/sdm
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdm: 8589 MB, 8589934592 bytes, 16777216 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 label type: gpt

# Start End Size Type Name
 1 4096 16777182 8G Linux filesyste Linux
128 2048 4095 1M BIOS boot parti BIOS Boot Partition
# fdisk -l /dev/sdm1
Disk /dev/sdm1: 8587 MB, 8587820544 bytes, 16773087 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

Check the existing filesystem on “Small Source”

# e2fsck /dev/sdz1
e2fsck 1.42.12 (29-Aug-2014)
/: clean, 119138/507904 files, 1327858/2022400 blocks

Reduce the size of files on “Small Source”

# resize2fs -p /dev/sdz1 7900M

Change the partition table on “Small Source” to fit on “Small Dest”

# fdisk /dev/sdz
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

– print the table (redundant as we did this previously)

Command (m for help): p
Disk /dev/sdz: 107.4 GB, 107374182400 bytes, 209715200 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 label type: gpt
# Start End Size Type Name
 1 4096 16183295 7.7G Linux filesyste
128 2048 4095 1M BIOS boot parti BIOS Boot Partition
Command (m for help):

– delete the partition

Command (m for help): d
Partition number (1,128, default 128): 1
Partition 1 is deleted

– Create a new partition.  Start it at 4096.  Make it 7900MB (the same size as the resize above)

Command (m for help): n
Partition number (1-127, default 1): 1
First sector (34-209715166, default 4096): 4096
Last sector, +sectors or +size{K,M,G,T,P} (4096-209715166, default 209715166): +7900M

– write the table (& quit)

w
q

Copy the “Small Source” partition to “Small Dest”

# dd if=/dev/sdz1 of=/dev/sdm1 bs=32M

Check the new filesystem on “Small Dest”

# e2fsck /dev/sdm1

Mount the drive just to make sure it has the contents you want.

# mount /dev/sdm1 sdm/
# ls sdm/
bin cgroup dev home lib64 lost+found mnt proc run selinux sys usr
boot config etc lib local media opt root sbin srv tmp var

On AWS launch with the Smaller Disk Size

All that is left to do now is associate the new “Small Dest” volume with an EC2 instance and you will be back up and running.  For me, I simply stopped the “Current” EC2, detached the booting volume and then attached “Small Dest” as the booting volume for “Current”

If you needed 100% uptime, you could launch a new EC2 instance, or use “New 8” and attach “Small Dest” as the booting partition, then use Elastic IP to point to your new small server.

Cleanup the Old Items

My next step is to clean up the snapshots, volumes, and instances that are no longer needed leaving me with a new clean instance an the associated backups. I am not going into detail on this one, as I feel we have used those features enough over the past couple of posts

And that is how I reduced my disk size.  Thanks for reading.

Leave a comment

Your email address will not be published. Required fields are marked *