Skip to content

Instantly share code, notes, and snippets.

@GWuk
Last active December 21, 2015 21:05
Show Gist options
  • Save GWuk/05427fb924a51d8d4769 to your computer and use it in GitHub Desktop.
Save GWuk/05427fb924a51d8d4769 to your computer and use it in GitHub Desktop.
how to reduce AWS EC2 AMI volume size for Debian Jessie
# -----------------------------------------------------------------------------
# set of commands to reduce AWS EC2 AMI volume size for Debian Jessie
# tested on debian-jessie-amd64-hvm-2015-06-07-12-27-ebs (ami-e31a6594)
#
# !! 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 primary partition
fdisk -l
--> pick smaller, empty one. In this case /dev/xvdf
fdisk /dev/xvdf
n
p
<enter><enter><enter>
w
# make file system
mkfs -t ext4 /dev/xvdf1
# mount old and new
mkdir /mnt/small
mount /dev/xvdf1 /mnt/small
mkdir /mnt/snap
mount /dev/xvdg1 /mnt/snap
# copy filesystem contents from snapshot copy volume
cd /mnt/snap; tar cfp - ./* |(cd /mnt/small; tar xvfp -)
# reinstall grub
mount -o bind /dev /mnt/small/dev
mount -o bind /sys /mnt/small/sys
mount -t proc /proc /mnt/small/proc
cp /proc/mounts /mnt/small/etc/mtab
chroot /mnt/small /bin/bash
mount
# use root parent-device
grub-install /dev/xvdf
update-grub
# change UUID in /etc/fstab
blkid
# note from root file sys (in chroot)
vi /etc/fstab
# 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