Skip to content

Instantly share code, notes, and snippets.

@GWuk
Last active December 21, 2015 21:06
Show Gist options
  • Save GWuk/6aa9eda9b8dbfc717fc3 to your computer and use it in GitHub Desktop.
Save GWuk/6aa9eda9b8dbfc717fc3 to your computer and use it in GitHub Desktop.
how to reduce AWS EC2 AMI volume size for Debian Wheezy
# -----------------------------------------------------------------------------
# set of commands to reduce AWS EC2 AMI volume size for Debian Wheezy
# tested on debian-wheezy-amd64-hvm-2015-01-28-ebs (ami-61e56916)
#
# !! DO NOT EXECUTE as script, think before you type and hit ENTER !!
#
# -----------------------------------------------------------------------------
# Copyright (c) 2015 GWu
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -----------------------------------------------------------------------------
# first get system up to date
apt-get update
apt-get upgrade
apt-get clean
# Stop the Instance
# Create a snapshot of the root volume
# Create volume from snapshot
# Create empty volume in the same availability zone - size 1GB for example
# Attach both volumes to the instance
# create GPT partition
apt-get install parted
fdisk -l
parted /dev/xvdf print
parted /dev/xvdf mklabel gpt
parted -a optimal /dev/xvdf mkpart primary 0% 100%
parted /dev/xvdf print
# make file system
mkfs -t ext4 /dev/xvdf
# mount old and new
mkdir /mnt/small
mount /dev/xvdf /mnt/small
mkdir /mnt/snap
mount /dev/xvdg /mnt/snap
# copy filesystem contents from snapshot copy volume
cd /mnt/snap; tar cfp - ./* |(cd /mnt/small; tar xvfp -)
# change UUID
blkid /dev/xvda
tune2fs -U 6f816946-42f1-4dcb-90f9-1c8b9daeafe2 /dev/xvdf
# reinstall extlinux
mount -o bind /dev /mnt/small/dev
mount -o bind /sys /mnt/small/sys
mount -t proc /proc /mnt/small/proc
mount --bind /dev/pts /mnt/small/dev/pts
chroot /mnt/small /bin/bash
extlinux-update
extlinux --install /boot/extlinux
# stop instance
# detach all volumes
# attach small volume to mount point /dev/xvda
# start
# remove unused volumes + snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment