We build Web & Mobile Applications.

< All Articles

Create a bootable EBS AMI from a running instance

A quick set of notes on how to create a bootable EBS snapshot from a running EC2 instance - for example, an instance that has been started from an S3 backed AMI.

We’ve had to do this a few times over the last few months - for the benefit of others, we’ve outlined how we currently do it - this is based on a number of articles that were surfacing at the time of our research, but I don’t have the links to hand. If there’s a better way out there feel free to jump in!

  1. Create an EBS volume (if converting from an S3 backed AMI then create it as 10Gb in size)

  2. Attach the EBS volume to your running instance

  3. Stop running services that may be locking files, etc, such as MySQL

    At this point, you’re ready to copy the contents of your root drive onto the EBS volume. A couple of additional points here - it makes sense to stop any services possible to make the transfer as clean as possible. One of our readers has suggested an alternative approach to dd - I’ve included it here as an option for completeness.

  4. You have two options for copying the disk:

    Option 1 - Use dd to copy the root drive to the EBS volume

    # Replace SOURCE with the boot volume, e.g. /dev/sda1 and TARGET with the EBS volume, e.g. /dev/sdj
    dd bs=65536 if=SOURCE of=TARGET
    fsck TARGET
    mkdir /migrate
    mount TARGET /migrate

    Option 2 - Use rsync

    I suspect with this route you’d need to exclude any directories used for mounting other volumes, otherwise your rsync will run out of space on the target device.

    mkfs.ext3 TARGET
    mount TARGET /migrate
    rsync -ax --progress / /migrate
    rm -rf /migrate/migrate
  5. At this point, you can make any necessary modifications to the fstab to allow your EBS based AMI to boot - the file can be found in /migrate/etc/fstab

  6. umount TARGET

  7. Create a snapshot of the EBS volume

  8. Register the snapshot as an AMI by running

    ec2-register -n NAME --architecture ARCH --block-device-mapping /dev/sda1=SNAPSHOT:SIZE:false

    There are a number of options here - some of which you can set when starting the instance if you prefer.

    the name of the AMI
    the architecture (i386 or x86_64)

    The block device mapping paramaters set the EBS snapshot to use as the boot device, the second parameter sets the size to 15Gb and final parameter sets the delete-on-termination attribute to false.

  9. Run an instance based on the newly registered AMI - if you’ve set the EBS volume size to anything greater then 10Gb then expand the filesystem to the size of the device.

More details, as usual, can be found in the command line reference.

Updated on 15 October 2015
First published by Chris Anderton on 19 April 2010
© TheWebFellas Limited 2016
"Create a bootable EBS AMI from a running instance" by Chris Anderton at TheWebFellas is licensed under a Creative Commons Attribution 4.0 International License.