Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created March 11, 2010 23:20

Revisions

  1. hubgit revised this gist Mar 25, 2010. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion ami-to-ebs.sh
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@
    # http://uec-images.ubuntu.com/releases/karmic/release/
    # canonical karmic 64-bit AMI:
    # ami-55739e3c
    # canonical karmic 32-bit AMI:
    # ami-bb709dd2

    # https://console.aws.amazon.com/ec2/home#c=EC2&s=Instances
    # Launch Instance > Community AMIs
    @@ -88,14 +90,16 @@ release=9.10
    codename=karmic
    tag=server
    arch=x86_64
    #arch=i386
    now=$(date +%Y%m%d-%H%M)

    prefix=ubuntu-$release-$codename-$tag-$arch-$now
    description="Ubuntu $release $codename $tag $arch $now"

    kernelid='aki-fd15f694'
    #ramdiskid='ari-7b739e12'
    #kernelid='aki-5f15f636'
    ramdiskid='ari-c515f6ac'
    #ramdiskid='ari-0915f660'

    ebsid=$(ec2-register --region $region --snapshot "$snapshotid" --architecture $arch --kernel $kernelid --ramdisk $ramdiskid --block-device-mapping /dev/sdb=ephemeral0 --description "$description" --name "$prefix" --root-device-name /dev/sda1 | cut -f2)

  2. hubgit revised this gist Mar 25, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ami-to-ebs.sh
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ sudo -E aptitude dist-upgrade -y &&
    sudo -E aptitude install -y sun-java6-jre ec2-api-tools &&
    sudo apt-get clean

    # all the ubuntu user to read the private key and certificate; set up environment variables
    # allow the ubuntu user to read the private key and certificate; set up environment variables
    sudo chown ubuntu /mnt/*.pem &&
    export EC2_CERT=$(echo /mnt/cert-*.pem) &&
    export EC2_PRIVATE_KEY=$(echo /mnt/pk-*.pem)
  3. hubgit revised this gist Mar 11, 2010. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ami-to-ebs.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    #!/bin/bash

    # in web browser

    # http://uec-images.ubuntu.com/releases/karmic/release/
  4. hubgit revised this gist Mar 11, 2010. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ami-to-ebs.sh
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,6 @@

    # local settings from the instance launched above
    host='YOURHOST.compute-1.amazonaws.com' &&
    region='us-east-1' &&
    zone='us-east-1b' &&
    keypair=`ls ~/.ec2/ec2-keypair.pem` # full path

    # send your private key and certificate to the instance
  5. hubgit created this gist Mar 11, 2010.
    111 changes: 111 additions & 0 deletions ami-to-ebs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    #!/bin/bash

    # in web browser

    # http://uec-images.ubuntu.com/releases/karmic/release/
    # canonical karmic 64-bit AMI:
    # ami-55739e3c

    # https://console.aws.amazon.com/ec2/home#c=EC2&s=Instances
    # Launch Instance > Community AMIs
    # us-east-1b

    # on client

    # local settings from the instance launched above
    host='YOURHOST.compute-1.amazonaws.com' &&
    region='us-east-1' &&
    zone='us-east-1b' &&
    keypair=`ls ~/.ec2/ec2-keypair.pem` # full path

    # send your private key and certificate to the instance
    rsync --rsh="ssh -i $keypair" --rsync-path="sudo rsync" ~/.ec2/{cert,pk}-*.pem ubuntu@$host:/mnt/

    # connect to the instance
    ssh -i "$keypair" ubuntu@$host

    # on server

    # add multiverse repositories (for Sun Java)
    sudo perl -pi -e 's%(universe)$%$1 multiverse%' /etc/apt/sources.list

    # add ec2-tools PPA to get the most recent version
    echo "deb http://ppa.launchpad.net/ubuntu-on-ec2/ec2-tools/ubuntu karmic main" | sudo tee /etc/apt/sources.list.d/ubuntu-on-ec2-ec2-tools.list &&
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9EE6D873

    # update all packages and install ec2-api-tools
    sudo aptitude update &&
    sudo -E aptitude dist-upgrade -y &&
    sudo -E aptitude install -y sun-java6-jre ec2-api-tools &&
    sudo apt-get clean

    # all the ubuntu user to read the private key and certificate; set up environment variables
    sudo chown ubuntu /mnt/*.pem &&
    export EC2_CERT=$(echo /mnt/cert-*.pem) &&
    export EC2_PRIVATE_KEY=$(echo /mnt/pk-*.pem)

    # local settings from launching the instance above
    region='us-east-1' &&
    zone='us-east-1b'

    # create a new 15GB ebs volume
    instanceid=$(wget -qO- http://instance-data/latest/meta-data/instance-id) &&
    volumeid=$(ec2-create-volume --size 15 --availability-zone $zone | cut -f2)

    # attach the ebs volume
    ec2-attach-volume $volumeid --instance $instanceid --device /dev/sdh &&
    while [ ! -e /dev/sdh ]; do echo -n .; sleep 1; done

    # create an ext3 filesystem on the ebs volume and mount it
    sudo mkfs.ext3 -F /dev/sdh &&
    sudo mkdir /mnt/ebsimage &&
    sudo mount /dev/sdh /mnt/ebsimage

    # set up some system stuff on the ebs volume - not sure if all this is needed
    sudo mkdir /mnt/ebsimage/mnt
    sudo mkdir /mnt/ebsimage/proc
    sudo mkdir /mnt/ebsimage/sys
    sudo mkdir /mnt/ebsimage/dev
    sudo mknod /mnt/ebsimage/dev/null c 1 3
    sudo mknod /mnt/ebsimage/dev/zero c 1 5
    sudo mknod /mnt/ebsimage/dev/tty c 5 0
    sudo mknod /mnt/ebsimage/dev/console c 5 1
    sudo ln -s null /mnt/ebsimage/dev/X0R

    # copy the current instance onto the ebs volume
    sudo rsync --stats -av --exclude=/sys --exclude=/mnt --exclude=/proc --exclude=/dev --exclude=/media --exclude=/tmp --exclude=/sys / /mnt/ebsimage

    # unmount the ebs volume and detach it from the instance
    sudo umount /mnt/ebsimage &&
    ec2-detach-volume $volumeid --instance $instanceid

    # create a snapshot from the ebs volume
    snapshotid=$(ec2-create-snapshot --region $region $volumeid | cut -f2)
    #ec2-delete-volume $volumeid

    # wait for the snapshot to finish creating
    while ec2-describe-snapshots "$snapshotid" | grep -q pending
    do echo -n .; sleep 10; done

    # register the snapshot as a new ec2 ami
    release=9.10
    codename=karmic
    tag=server
    arch=x86_64
    now=$(date +%Y%m%d-%H%M)

    prefix=ubuntu-$release-$codename-$tag-$arch-$now
    description="Ubuntu $release $codename $tag $arch $now"

    kernelid='aki-fd15f694'
    #ramdiskid='ari-7b739e12'
    ramdiskid='ari-c515f6ac'

    ebsid=$(ec2-register --region $region --snapshot "$snapshotid" --architecture $arch --kernel $kernelid --ramdisk $ramdiskid --block-device-mapping /dev/sdb=ephemeral0 --description "$description" --name "$prefix" --root-device-name /dev/sda1 | cut -f2)

    # display the ids, for reference
    cat <<EOF
    ami-instance $instanceid
    snapshot $snapshotid
    ebs-instance $ebsid
    EOF