Enlarging an EC2 instance is easy like a breath (for instance, create an AMI, launch an instance from it and then change the storage size).

But reducing it becomes more difficult. I’d like to reduce an Amazon Web Services (AWS) EC2 instance Elastic Block Store (EBS) root volume size. There are a couples of old high level procedures on the net. The more detailed version I found is a one year old answer on a StackOverflow question: how to can i reduce my ebs volume capacity, steps have a pretty high level:

Create a new EBS volume that is the desired size (e.g. /dev/xvdg)

Launch an instance, and attach both EBS volumes to it

Check the file system (of the original root volume): (e.g.) e2fsck -f /dev/xvda1

Maximally shrink the original root volume: (e.g. ext2/3/4) resize2fs -M -p /dev/xvda1

Copy the data over with dd:

  • Choose a chunk size (I like 16MB)

  • Calculate the number of chunks (using the number of blocks from the resize2fs output): blocks*4/(chunk_size_in_mb*1024) - round up a bit for safety

  • Copy the data: (e.g.) dd if=/dev/xvda1 ibs=16M of=/dev/xvdg obs=16M count=80

Resize the filesystem on the new (smaller) EBS volume: (e.g.) resize2fs -p /dev/xvdg

Check the file system (of the original root volume): (e.g.) e2fsck -f /dev/xvdg

Detach your new EBS root volume, and attach it to your original instance

I’m unable to find a detailed step by step “how to” solution.

My EBS root volume is attached to a HVM Ubuntu instance.

Any help would be really appreciated.

  • That looks like a pretty good step-by-step "how to" solution. – ceejayoz Mar 4 '15 at 19:47
  • dd - copy disk-to-disk dd if=/dev/xvdf of=/dev/xvdh bs=4k count=227613 e2fsck -f /dev/xvdh1 resize2fs -p /dev/xvdh1 – sirkubax Jul 3 at 13:29

In AWS Console:

  1. Stop the instance you want to resize

  2. Create a snapshot of the active volume and then create a "General Purpose SSD" volume from that snapshot.

  3. Create another "General Purpose SSD" volume to the size you want.

  4. Attach these 3 volumes to the instance as:

    • /dev/sda1 for the active volume.
    • /dev/xvdf for the volume that is the target size.
    • /dev/xvdg for the volume made from the snapshot of the active volume.
  5. Start the instance.

  6. Log onto the new instance via SSH.

  7. create these new directories:

mkdir /source /target

  1. create an ext4 filesystem on new volume:

mkfs.ext4 /dev/xvdf

  1. mount it to this directory:

mount -t ext4 /dev/xvdf /target

  1. This is very important, the file system needs an e2label for linux to recognize it and boot it, use "e2label /dev/xvda1" on an active instance to see what it should be, in this case the label is: "/"

e2label /dev/xvdf /

  1. mount the volume created from the snapshot:

mount -t ext4 /dev/xvdg /source

  1. Copy the contents:

rsync -ax /source/ /target

Note: there is no "/" following "/target". Also, there may be a few errors about symlinks and attrs, but the resize was still successful

  1. Umount the file systems:

umount /target
umount /source

  1. Back in AWS Console: Stop the instance, and detach all the volumes.

  2. Attach the new sized volume to the instance as: "/dev/sda1"

  3. Start the instance, and it should boot up.

STEP 10 IS IMPORTANT: Label the new volume with "e2label" as mentioned above, or the instance will appear to boot in aws but wont pass the connection check.

  • 9
    I have ran through these steps several times (Ubuntu 14.04) and every time I attach the new volume, the instance just stops. Any one else experiencing this issue? This is racking my brain! – thiesdiggity Oct 23 '15 at 6:12
  • 2
    You are not the only one. I have tried this and other solutions and like your good self, my instance just shuts down as well. – blairmeister Dec 8 '16 at 15:29
  • 1
    @blairmeister I had the same issue, but managed to get it to work! Have a look at my answer below if you're still stucked :) – Ruben Serrate Mar 6 '17 at 15:10
  • my e2label is cloudimg-rootfs...following all these steps I can confirm on Ubuntu 14.04 doesn't work – NineCattoRules Apr 19 '17 at 9:40
  • 1
    I'm downvoting this answer as it doesn't cover enough use cases for a volume (like as a boot volume) to protect users from inadvertent damage. – Jesse Adelman Oct 24 '17 at 16:08

None of the other solutions will work if the volume is used as a root (bootable) device.

The newly created disk is missing the boot partition, so it would need to have GRUB installed and some flags set up correctly before an instance can use it as a root volume.

My (as of today, working) solution for shrinking a root volume is:

Background: We have an instance A, whose root volume we want to shrink. Let's call this volume VA. We want to shrink VA from 30GB to let's say 10GB

  1. Create a new ec2 instance, B, with the same OS as the instance A. As storage, pick a volume that's the same type as VA, but with a size of 10GB. (or whatever your target size is). So now we have an instance B which uses this new volume (let's call it VB) as a root volume.
  2. Once the new instance (B) is running. Stop it and detach it's root volume (VB).

NOTE: The following steps are mostly taken from @bill 's solution:

  1. Stop the instance you want to resize (A).

  2. Create a snapshot of the volume VA and then create a "General Purpose SSD" volume from that snapshot. This volume we'll call it VASNAP.

  3. Spin a new instance with amazon Linux, we'll call this instance C. We will just use this instance to copy the contents of VASNAP to VB. We could probably also use instance A to do these steps, but I prefer to do it in an independent machine.

  4. Attach the following volumes to instance C. /dev/xvdf for VB. /dev/xvdg for VASNAP.

  5. Reboot instance C.

  6. Log onto instance C via SSH.

  7. Create these new directories:

mkdir /source /target

  1. Format VB's main partition with an ext4 filesystem:

mkfs.ext4 /dev/xvdf1

If you get no errors, proceed to Step 11. Otherwise, if you do not have /dev/xvdf1, you need to create the partition by doing the following i-vii:

i) If /dev/xvdf1 does not exist for whatever reason, you need to create it. First enter:

sudo fdisk /dev/xvdf.

ii) Wipe disk by entering: wipefs

iii) Create a new partition by entering: n

iv) Enter p to create primary partition

v) Keep pressing enter to continue with default settings.

vi) When it asks for a command again, enter w to write changes and quit.

vii) Verify you have the /dev/xvdf1 partition by doing: lsblk

You should see something like:

NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  250G  0 disk
└─xvda1 202:1    0  250G  0 part
xvdf    202:80   0   80G  0 disk
└─xvdf1 202:81   0   80G  0 part 
xvdg    202:96   0  250G  0 disk
└─xvdg1 202:97   0  250G  0 part

Now proceed to Step 11.

  1. Mount it to this directory:

mount -t ext4 /dev/xvdf1 /target

  1. This is very important, the file system needs an e2label for Linux to recognize it and boot it, use "e2label /dev/xvda1" on an active instance to see what it should be, in this case the label is: "/"

e2label /dev/xvdf1 /

  1. Mount VASNAP on /source:

mount -t ext4 /dev/xvdg1 /source

  1. Copy the contents:

rsync -vaxSHAX /source/ /target

Note: there is no "/" following "/target". Also, there may be a few errors about symlinks and attrs, but the resize was still successful

  1. Umount VB:

umount /target

  1. Back in AWS Console: Dettach VB from instance C, and also dettach VA from A.

  2. Attach the new sized volume (VB) to the instance as: "/dev/xvda"

  3. Boot instance A, now it's root device is 10GB :)

  4. Delete both instances B and C, and also all volumes but VB, which is now instance A's root volume.

  • What's your OS? – NineCattoRules Apr 19 '17 at 9:57
  • @NineCattoRules Amazon Linux – Ruben Serrate May 10 '17 at 7:53
  • I tried on Ubuntu 14.04 and it doesn't work – NineCattoRules May 10 '17 at 10:15
  • @NineCattoRules Ouch... I can confirm it works for Amazon Linux, had to do it recently. – Ruben Serrate May 11 '17 at 11:24
  • It gives "Error 15" on boot menu. @RubenSerrate – alper Sep 7 '17 at 18:20

The following steps worked for me

Step 1. Create snapshot of root ebs volume and create new volume from snapshot (let's call this volume-copy)

Step 2. Create new instance with ebs root volume with desired size. (let's call this volume-resized) This ebs volume will have the correct partition for booting. (Creating a new ebs volume from scratch didn't work for me)

Step 3. Attach volume-resize and volume-copy to an instance.

Step 4. Format volume-resize.

sudo fdisk -l
    sudo mkfs -t ext4 /dev/xvdf1

Note: ensure partition volume is entered /dev/xvdf1 not /dev/xvdf

Step 5. Mount volume-resize and volume-copy mkdir /mnt/copy mkdir /mnt/resize

sudo mount /dev/xvdh1 /mnt/copy
sudo mount /dev/xvdf1 /mnt/resize

Step 6. Copy files

rsync -ax /mnt/copy/ /mnt/resize

Step 7. Ensure e2label is same as root volume

sudo E2label /dev/xvdh1 > cloudimg-rootfs
sudo E2label /dev/xvdf1 cloudimg-rootfs

Step 8. Update grub.conf on volume-copy to match new volume udid

Search and replace uudid in /boot/grub/grub.cfg

ubuntu@server:~/mnt$ sudo blkid
/dev/xvdh1: LABEL="cloudimg-rootfs" UUID="1d61c588-f8fc-47c9-bdf5-07ae1a00e9a3" TYPE="ext4"
/dev/xvdf1: LABEL="cloudimg-rootfs" UUID="78786e15-f45d-46f9-8524-ae04402d1116" TYPE="ext4"

Step 9. Unmount volumes

Step 10. Attach new resized ebs volume to instance /dev/sda1

  • Combining @ruben serrate answer with the grub UUID update is what worked for me. – Jonathan Maim May 15 '17 at 9:35

Here's an alternate approach;

Attach and mount the old EBS volume on a running EC2 instance. If you are wanting to copy a boot volume, it's best to do it on a different instance, with the old volume mounted as data, not with the volume being used as a live system.

Create a new EBS volume of the desired size.

Attach the new volume to the instance and (carefully) format a new filesystem on it (e.g., using mkfs). Mount it.

Copy the old filesystem content from the old volume to the new volume:

rsync -vaxSHAX /oldvol/ /newvol/

Unmount the new volume and detach it from the instance.

If you were copying the root filesystem, then:

Create an EBS snapshot of the new volume.

Register the snapshot as a new AMI.

protected by Community Oct 26 '16 at 19:06

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.