Skip to content

EBS

Created: 2016-02-17 11:24:55 -0800 Modified: 2016-03-07 09:52:49 -0800

Basics:

  • EBS is not elastic in terms of the size of an individual volume, so going from, say, 8GB to 16GB on an instance would require copying everything from one volume to the other.
  • To see how many EBS volumes you have, go to your EC2 dashboard and then look at Elastic Block Store —> Volumes.
  • You can’t shrink volumes. If you want to set up images of size 4GB instead of 8GB, then follow this guide. WARNING: THIS GUIDE HAS TYPOS AS OF 2/21/16 (which likely means the author is never fixing them), so read the comments.

READ THIS

Should I ever determine that I’m stupid enough to try this again, here’s what went wrong last time: as mentioned here, I need to find the kernel ID (kernels aren’t copied by ‘dd’; they’re managed by AWS). This will not show up in my volume or my AMI, because my AMI is a “sub AMI”, which means the original AMI has the kernel ID. This is what the ”-” in the UI means - either it’s deferred to the parent or it’s disabled.

After I get the kernel ID, I should take a snapshot, then make an AMI from the snapshot.

Spin up an EC2 instance based on an AMI

Create ANOTHER volume from your AMI. To do this, EBS —> Snapshots —> Actions —> Create Volume

Create an empty 4GB volume in the same availability zone

Enable termination-protection, stop the instance, and attach the two volumes

Small instance: /dev/sdf

Large instance: /dev/sdg

Start the instance, SSH into it.

lsblk

this will show you your attached volumes

sudo mount

My main drive showed

/dev/xvdg1 on / type ext4 (rw,noatime,data=ordered)

[ec2-user@ip-10-0-0-4 ~]$ lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

xvda 202:0 0 8G 0 disk

+-xvda1 202:1 0 8G 0 part

xvdf 202:80 0 4G 0 disk

xvdg 202:96 0 8G 0 disk

+-xvdg1 202:97 0 8G 0 part /

FYI: disks do not have numbers; the partitions have numbers

Run fsck on the copy of your AMI. Because my main drive is xvdg1, I want to run fsck on xvda1

sudo e2fsck -f /dev/xvda1

If there were no errors

sudo resize2fs -M -p /dev/xvda1

The filesystem on /dev/xvda1 is now 560194 (4k) blocks long.

136.766113281

2188.2578125 MB

I want it to be 2.5 GB, so I would want 160 16-MB blocks.

My small volume is /dev/xvdf, so do this:

sudo fdisk /dev/xvdf

Type ‘n’ to make a new partition

Then use all of the default options, which will create one main partition whose number is 1 and whose size is the entire volume.

After doing that, make sure to type ‘w’ to write out the changes instead of ‘q’ to quit without saving.

To make sure you did everything correctly, type “lsblk” and make sure your partition shows.

sudo dd bs=16M if=/dev/xvda1 of=/dev/xvdf1 count=160

sudo resize2fs -p /dev/xvdf1

sudo e2fsck -f /dev/xvdf1

This did not do anything: I need to attach both volumes, then run fdisk and set the bootable flag on the 4GB volume.

Maybe it was a combination of setting both that AND the boot device to /dev/sda1 instead of /dev/xvda

Next, I should be okay to stop the instance and attach just the smaller volume as the root device:

http://stackoverflow.com/questions/6377669/can-i-change-the-root-ebs-device-of-my-amazon-ec2-instance

You just type “/dev/xvda” when attaching despite that the UI says you need to specify “/dev/sdf through whatever”.

After booting into the device, try “df -h” to see the remaining size.

3/6/2016

I tried again. This time, I tried copying over the entire device as opposed to just a partition based on the advice of a comment from the main reference. It still didn’t work. Still, here are the steps I followed:

Find starting partition of current filesystem

sudo fdisk -l /dev/xvdf

Mine starts at 4096

Make smaller partition

sudo fdisk /dev/xvdf

d

1

n

1

4096

+3000M <— I picked a number slightly bigger than the 2.4 GB for the file system since this is the partition.

w

lsblk to make sure your xvdf1 is now 3 GB.

You can verify that the filesystem is intact by doing “sudo mount /dev/xvdf1 /mnt” (and then maybe /mnt/usr/bin/node —version), then “sudo umount /dev/xvdf1” when you’re done to unmount.

sudo dd bs=16M if=/dev/xvdf of=/dev/xvdg count=150

Now, using “sudo fdisk /dev/xvdg” followed by the “p” command, I can see “/dev/xvdg1” on there with a start block of 1 and an end of 16777215