Skip to content

Instantly share code, notes, and snippets.

@JDFagan
Last active April 13, 2023 06:06
Show Gist options
  • Save JDFagan/b7689416140736094f30b240b2c92c02 to your computer and use it in GitHub Desktop.
Save JDFagan/b7689416140736094f30b240b2c92c02 to your computer and use it in GitHub Desktop.
Amazon EBS (Elastic Block Store) storage is not easy to lower volume sizes. They seem to be easy to expand though. This is especially problematic when the EBS volume is mounted on root. This gist describes steps one can take to re-size an EBS volume on an instance whether its to lower or raise the volume size.

Warning - Steps to lower Root EBS Volume still a work in progress and being tested

Steps to Lower Root EBS Volume Size on an Instance

  1. Create new Ubuntu Micro instance (e.g., Name = Tools)
  2. Stop instance you want to lower EBS volume.
  3. Create AMI image as backup of your instance.
  4. Create an empty X GB Amazon EBS volume in the same availability zone where X is smaller size you desire.
  5. Detach the volume you wish to resize from the stopped instance from step 2 above.
  6. Attach the original volume to the Tools instance (e.g., as /dev/xvdf)
  7. Attach new downsized volume to the Tools instance (e.g., as /dev/xvdg)
  8. SSH into Tools instance and run these steps:
  9. Verify newly attached partitions via cat /proc/partitions
  10. To ensure that the existing original file system is in order, run e2fsck -f /dev/xvdf.
  11. If the e2fsck command ran without errors, now run resize2fs -M -p /dev/xvdf.
  12. The last line from the resize2fs command should tell you how many 4k blocks the filesystem now is. To calculate the number of 16MB blocks you need, use the following formula: blockcount * 4 / (16 * 1024). Round this number up to nearest integer and use this rounded number to give yourself a little buffer.
  13. If you dont yet have a partition on your new volume (/dev/xvdg1), use fdisk as follows: 1. fdisk /dev/xvdg 1. p (print to view partitions) 1. n (new partition)
    1. p (primary partition)
    2. Press enter to accept default first sector
    3. Press enter to accept default last sector
    4. w (write out changes)
  14. Execute the following command, using the number you came up with in the previous step. dd bs=16M if=/dev/xvdf of=/dev/xvdg count=numberfrompreviousstep. Depending on how large your volume is this may take several minutes to run -- let it finish.
  15. After the copy finishes, resize and check and make sure that everything is in order with the new filesystem by running: 1. resize2fs -p /dev/xvdg 1. e2fsck -f /dev/xvdg
  16. After this step is complete, time to wrap this up:
  17. Detach downsized volume from the Tools instance you created.
  18. Attach the shrunken volume to the old EC2 instance as /dev/sda1 (your boot device)
  19. Restart your old instance.
  20. Ensure Instance boots up correctly and is working well.
  21. If all is well, cleanup: 1. Save the previous, larger volume until you've validated that everything is working properly. 1. When you've verified things are working well, feel free to delete the new EC2 Tools instance you created, plus the larger volume and any backup AMI's and/or snapshots you created.

Steps to Lower Non-Root EBS Volume Size on an Instance

  1. Stop instance you want to lower EBS volume.
  2. Create an empty X GB Amazon EBS volume in the same availability zone where X is smaller size you desire.
  3. Attach new volume to the instance and again note all device name details.
  4. Start instance.
  5. SSH into instance.
  6. Run these commands:
cat /proc/partitions
cat /etc/fstab
mkfs -t ext4 /dev/xvdg (assuming xvdg is the new device you just attached)
  1. Create mount directory and mount new volume
mkdir /mnt/small
mount /dev/xvdg /mnt/small
  1. Sync the files to smaller volume (assumes /mnt/original/ is where your original volume you want to resize - substitute as appropriate)
rsync -aHAXxSP  /mnt/original/ /mnt/small > rsync-aHAXxSP.out 2>&1 &
diff -qr /mnt/original/ /mnt/small
  1. Unmount smaller volume
umount /dev/xvdg
  1. Stop the instance
  2. Detach both old and newly resized volumes
  3. Attach just the newly resized volume now and make sure to use same /dev/… as detailed in step 3 of original volume you are replacing (this ensures that nothing bad will happen in case of auto-mounting scripts - e.g., referenced by rc0.d startup scripts referencing /etc/fstab).
  4. Start instance
  5. SSH into instance
  6. Verify new volume is auto-mounted and working properly as expected.
  7. review ownership and permissions

Steps to Lower EBS Volume Size on an AMI

  1. Create new fresh instance from AMI you want to modify.
  2. Follow Steps to Lower EBS Volume Size on an Instance (see above)
  3. [Optional] Rename large sized AMI to prior AMI name -old or -large
  4. Create new AMI from running instance to snapshot with lowered volume size
  5. [Optional] Delete old/larger AMI

Steps to Raise EBS Volume Size on an Instance

  1. TODO

Steps to Raise EBS Volume Size on an AMI

  1. Launch new instance from AMI
  2. Increase storage size from default AMI
  3. [Optional] Copy small sized AMI to prior AMI name -old or -small
  4. [Optional] Delete prior AMI to make room for replacement
  5. Wait for instance to be running
  6. Resize the file system so that it fills up the entire newly increased EBS volume
df -h
resize2fs /dev/xvdf # (where xvdf is the device representing increased EBS volume)
df -h
  1. Create new AMI from running instance to snapshot with increased volume size

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment