Skip to content
This repository has been archived by the owner on Jun 8, 2018. It is now read-only.

64bit ebs ami pvgrub

zextra edited this page Mar 4, 2011 · 29 revisions

Creating a Debian EC2 EBS PVGRUB Image

aka: How to create a persistently bootable image that uses the native operating system kernel and runs on Amazon's Elastic Compute Cloud platform.

Step 1: Create a disk image

Do this first: Setup EC2 Credentials and Tools

Option 1: on a currently running amazon instance

Create the image on amazon

This can also be done from the web interface

See this for a list of regions and availability zones.

I create the images with a 1 gigabyte size because it is easier to make it bigger than it is to shrink it

Create a 1 gb volume for our image
ec2-create-volume --size 1 --region us-east-1 --availability-zone us-east-1c
Attach the volume to our running instance
BUILD_INSTANCE_ID=i-xxxxxxxx
BUILD_VOLUME=vol-xxxxxxxx

ec2-attach-volume ${BUILD_VOLUME} --instance ${BUILD_INSTANCE_ID} --device /dev/sdf

Now when that device gets attached it will show up as /dev/sdf OR /dev/xvdf depending on the kernel you are using.

Use fdisk -l so see attached disks.

DEVICE=/dev/xvdf
mkdir -p chroot.x86_64/
echo y | mkfs.ext3 ${DEVICE}
mount ${DEVICE} chroot.x86_64/

Step 2: Install the required software

apt-get update
apt-get -y dist-upgrade
apt-get install -y debootstrap

We use the --no-check-certificate due to a bug in wget with wildcard certificates bugs.debian.org

wget --no_check-certificate http://github.com/downloads/tomheady/ec2debian/debian_config_ec2-latest.tar.gz
tar xvfz debian_config_ec2-latest.tar.gz

Step 3: Install and configure the base OS

debootstrap --arch amd64 squeeze chroot.x86_64/ http://ftp.debian.org

Having the same devices is required for when we install grub, so lets link the host OS /dev to the chroot's /dev

rm -r chroot.x86_64/dev
mkdir -p chroot.x86_64/dev
mount --bind /dev chroot.x86_64/dev

Now copy over the customized configuration files:

cp src/root/etc/{fstab,hosts,rc.local} chroot.x86_64/etc/
cp src/root/etc/apt/sources.list       chroot.x86_64/etc/apt/
cp src/root/etc/network/interfaces     chroot.x86_64/etc/network/
cp src/root/usr/local/bin/*            chroot.x86_64/usr/local/bin/
cp src/root/boot/grub/menu.lst         chroot.x86_64/root/
cp src/root/etc/ssh/sshd_config        chroot.x86_64/root/

chroot into the new image and do some mounts

chroot chroot.x86_64/
mount /proc
mount /sys
mount -t devpts none /dev/pts

update the packages inside our image

apt-get update

install and configure locales. All supported locales will be generated, and en_US.UTF8 will be used as default.

apt-get install -y locales
cat /usr/share/i18n/SUPPORTED > /etc/locale.gen
dpkg-reconfigure --priority=critical locales

Now upgrade the system, remove some dhcp clients that cause problems

apt-get dist-upgrade
apt-get remove --purge isc-dhcp-client isc-dhcp-common dhcp3-client

Install some required packages

apt-get install -y openssh-server less locate grub-legacy curl file linux-image-2.6.32-5-xen-amd64 dhcpcd

Now that openssh-server is installed, move the config file into place

mv /root/sshd_config /etc/ssh/

Inside Xen, CMOS clock is irrelevant, so we disable it

update-rc.d -f hwclock.sh remove
update-rc.d -f hwclockfirst.sh remove

Configure grub

mkdir /boot/grub
grub-set-default default
mv /root/menu.lst  /boot/grub/
update-grub

cleanup our image

apt-get autoremove --purge
apt-get clean
apt-get autoclean

rm -f /root/.ssh/authorized_keys /etc/hostname
rm -rf /var/log/*.gz /var/log/{bootstrap,dpkg}.log /var/cache/apt/*.bin /var/lib/apt/lists 
rm -rf /tmp/*
rm -f /root/.bash_history
touch /root/.bash_history 
mkdir -p /var/lib/apt/lists/partial

umount /proc
umount /sys
umount /dev/pts

exit

umount chroot.x86_64/dev
umount chroot.x86_64

Step 4: create EBS AMI

We now have a good base image, let's create our EBS AMI. This is done by taking a snapshot of our disk image and then registering it.

# detaching is optional
ec2-detach-volume ${BUILD_VOLUME} --instance ${BUILD_INSTANCE_ID} --device /dev/sdf
ec2-create-snapshot --description "My description" ${BUILD_VOLUME}

SNAPSHOT=snap-xxxxxxxx
ec2-register --architecture=x86_64 --name 'My Name' --description "My description" --kernel aki-427d952b --snapshot ${SNAPSHOT}

You should now have a 64 bit EBS backed ami that boots the debian kernel.

Clone this wiki locally