Skip to content

Instantly share code, notes, and snippets.

@dysinger
Created March 31, 2009 22:36
Show Gist options
  • Select an option

  • Save dysinger/88453 to your computer and use it in GitHub Desktop.

Select an option

Save dysinger/88453 to your computer and use it in GitHub Desktop.
Bare Minimum to boot Stage3 gentoo @ ec2
#!/bin/sh
# -*- sh -*-
# NOTE: This is the bare min needed to make an ec2 image from a stage3 gentoo image.
# !st Build a stage3 from somewhere. I use metro. ...then... ->
#
# Configure
#
# Bootstrap for EC2 (TODO Most of this below needs to be added to metro)
mkdir -p /mnt/gentoo
tar xjof /home/mirror/linux/gentoo/$TYPE/stage3-$TYPE-current.tar.bz2 \
-C /mnt/gentoo
mkdir -p /mnt/gentoo/lib/modules
cp /etc/resolv.conf /mnt/gentoo/etc
mount -t proc none /mnt/gentoo/proc
mount -o bind /dev /mnt/gentoo/dev
chroot /mnt/gentoo /bin/bash
env-update
source /etc/profile
etc-update ; # FIXME Metro left behind crufty locale.gen file !?!
emerge --sync
# rc
emerge dhcpcd
rc-update add net.eth0 default
rc-update add sshd default
# /etc/fstab
cat >/etc/fstab <<\EOF
# /etc/fstab: static file system information.
#
# noatime turns off atimes for increased performance (atimes normally aren't
# needed; notail increases performance of ReiserFS (at the expense of storage
# efficiency). It's safe to drop the noatime options if you want and to
# switch between notail / tail freely.
#
# The root filesystem should have a pass number of either 0 or 1.
# All other filesystems should have a pass number of 0 or greater than 1.
#
# See the manpage fstab(5) for more information.
#
# <fs> <mountpoint> <type> <opts> <dump/pass>
# NOTE: If your BOOT partition is ReiserFS, add the notail option to opts.
### vvv ###
/dev/sda1 / ext3 user_xattr,noatime 0 1
/dev/sda2 /mnt ext3 user_xattr,noatime 0 2
/dev/sda3 swap swap sw 0 0
### ^^^ ###
# glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
# POSIX shared memory (shm_open, shm_unlink).
# (tmpfs is a dynamically expandable/shrinkable ramdisk, and will
# use almost no memory if not populated with files)
shm /dev/shm tmpfs nodev,nosuid,noexec 0 0
EOF
# boothook
cat >/etc/conf.d/local.start <<EOF
# /etc/conf.d/local.start
# This is a good place to load any misc programs
# on startup (use &>/dev/null to hide output)
### vvv ###
mkdir -p /root/.ssh 2>/dev/null
wget -O - http://169.254.169.254/2008-02-01/meta-data/public-keys/0/openssh-key\
>/root/.ssh/authorized_keys
chmod -R go-rwsx /root
wget -O - http://169.254.169.254/2008-02-01/user-data | sh
### ^^^ ###
EOF
# done
exit
for i in {1..3}
do
umount /mnt/gentoo/{dev,proc} ; sleep 1
done
#
# Image
#
# keys
mkdir -p /etc/ssl/ec2
cat >/etc/ssl/ec2/key.pem <<\THEEND
-----BEGIN PRIVATE KEY-----
<your ec2 priv key>
-----END PRIVATE KEY-----
THEEND
cat >/etc/ssl/ec2/crt.pem <<\THEEND
-----BEGIN CERTIFICATE-----
<your ec2 cert>
-----END CERTIFICATE-----
THEEND
chmod -R go-rwsx /etc/ssl/ec2
# bundle
modprobe loop
umount /mnt/im*
rm -rf /mnt/im*
rm -rf /mnt/gentoo/usr/portage ; # dont need this
rm -rf /mnt/gentoo/tmp /mnt/gentoo/var/tmp/*
rm /mnt/gentoo/etc/resolv.conf
rm /mnt/gentoo/root/.bash_history
if [ "$(uname -m)" == 'i686' ]; then
export ARCH="i386"
else
export ARCH="x86_64"
fi
export KERNEL=$(\
wget -q -O - http://169.254.169.254/2008-02-01/meta-data/kernel-id\
)
ec2-bundle-vol -u <your ec2 owner id> \
-k /etc/ssl/ec2/key.pem -c /etc/ssl/ec2/crt.pem \
--no-inherit -r $ARCH --kernel $KERNEL -d /mnt \
-v /mnt/gentoo/ --fstab /mnt/gentoo/etc/fstab
export BUNDLE=gentoo-$(\
wget -q -O - http://169.254.169.254/2008-02-01/meta-data/instance-type\
)-$(date +%Y%m%d)
echo $BUNDLE
# NOW: Create an S3 bucket w/ $BUNDLE as the name
ec2-upload-bundle \
-a <your ec2 access key> -s <your ec2 private access key> \
--retry --batch -b $BUNDLE -m /mnt/image.manifest.xml
ec2-register \
-K /etc/ssl/ec2/key.pem -C /etc/ssl/ec2/crt.pem \
$BUNDLE/image.manifest.xml
# Optional - make it available to everyone
ec2-modify-image-attribute \
-K /etc/ssl/ec2/key.pem -C /etc/ssl/ec2/crt.pem \
-l -a all \
<ami-id>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment