Skip to content

Instantly share code, notes, and snippets.

@dalehamel
Last active January 4, 2016 17:47
Show Gist options
  • Save dalehamel/397486478b2c14ee3883 to your computer and use it in GitHub Desktop.
Save dalehamel/397486478b2c14ee3883 to your computer and use it in GitHub Desktop.
Live root filesystem expansion in AWS
#!/bin/bash
# This script will resize the root filesystem on an AWS VM.
# When you create an AMI, you snapshot the root filesystem to be a particular size.
# When you actually launch an AMI, you can give it a volume that is actually larger than your filesystem.
# For instance, you can have an AMI with a root filesystem that's 8GB, but you can launch your host on a 40GB volume.
# It's perfectly safe to resize the system live to be the full extend of the volume it was launched on.
# This is done by deleting the primary partition (live), and recreating it to be the fully size of the actual device.
# Once that's done, we just grow the filesystem onto the now available space).
device=/dev/xvda # change if necessary, but this is a sensible default for AWS
# Get the starting sector, which is almost always 2048s, but script it to be sure
start=$(parted -m -s $device unit s print | grep $device -A1 | tail -n1 | cut -d : -f 2)
# Delete the primary partition - if the system reboots before it gets recreated you'll have real problems
cat <<EOF | fdisk $device
d
w
EOF
# Recreate the partition with the same start point, but using 100% of the sectors on the device
parted -s /dev/xvda mkpart primary $start 100%
# Resize the actual filesystem
resize2fs "${device}1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment