Amazon EC2用Centos5イメージを作って、EC2上で動作させてみる(3)
![]() |
前回の記事で作成したCentos5のイメージを、EC2向けにカスタマイズする。 |
3.EC2向けのカスタマイズ
(1)EC2上でマウントされる追加のディスクエントリをfstabに追記
# vi /mnt/ec2-fs/etc/fstab
/dev/sda2 /mnt ext3 defaults 0 0 /dev/sda3 swap swap defaults 0 0
(2)起動時に「ec2-add-keypair で作った公開鍵」を受け取るためのスクリプトを用意
# vi /mnt/ec2-fs/usr/local/sbin/get-credentials.sh
#!/bin/bash
# Retreive the credentials from relevant sources.
# Fetch any credentials presented at launch time and add them to
# root's public keys
PUB_KEY_URI=http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key
PUB_KEY_FROM_HTTP=/tmp/openssh_id.pub
PUB_KEY_FROM_EPHEMERAL=/mnt/openssh_id.pub
ROOT_AUTHORIZED_KEYS=/root/.ssh/authorized_keys
# We need somewhere to put the keys.
if [ ! -d /root/.ssh ] ; then
mkdir -p /root/.ssh
chmod 700 /root/.ssh
fi
# Fetch credentials...
# First try http
curl --retry 3 --retry-delay 0 --silent --fail -o $PUB_KEY_FROM_HTTP $PUB_KEY_URI
if [ $? -eq 0 -a -e $PUB_KEY_FROM_HTTP ] ; then
if ! grep -q -f $PUB_KEY_FROM_HTTP $ROOT_AUTHORIZED_KEYS
then
cat $PUB_KEY_FROM_HTTP >> $ROOT_AUTHORIZED_KEYS
echo "New key added to authrozied keys file from parameters"|logger -t "ec2"
fi
chmod 600 $ROOT_AUTHORIZED_KEYS
rm -f $PUB_KEY_FROM_HTTP
elif [ -e $PUB_KEY_FROM_EPHEMERAL ] ; then
# Try back to ephemeral store if http failed.
# NOTE: This usage is deprecated and will be removed in the future
if ! grep -q -f $PUB_KEY_FROM_EPHEMERAL $ROOT_AUTHORIZED_KEYS
then
cat $PUB_KEY_FROM_EPHEMERAL >> $ROOT_AUTHORIZED_KEYS
echo "New key added to authrozied keys file from ephemeral store"|logger -t "ec2"
fi
chmod 600 $ROOT_AUTHORIZED_KEYS
chmod 600 $PUB_KEY_FROM_EPHEMERAL
fi
if [ -e /mnt/openssh_id.pub ] ; then
if ! grep -q -f /mnt/openssh_id.pub /root/.ssh/authorized_keys
then
cat /mnt/openssh_id.pub >> /root/.ssh/authorized_keys
echo "New key added to authrozied keys file from ephemeral store"|logger -t "ec2"
fi
chmod 600 /root/.ssh/authorized_keys
fi
(3)スクリプトに実行権限付与
# chmod +x /mnt/ec2-fs/usr/local/sbin/get-credentials.sh
(4)OS起動時に上記スクリプトが呼び出されるように起動スクリプトに追記
# vi /mnt/ec2-fs/etc/rc.d/rc.local
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /usr/local/sbin/get-credentials.sh
(5)イメージファイルをアンマウント
# umount /mnt/ec2-fs/proc
# umount -d /mnt/ec2-fs
次は、作成したOSイメージを加工して、S3にアップし、EC2のマシンイメージとして登録する。
1.AWSのアカウントを取得して、ツールのセットアップを行う
2.Centos5のディスクイメージを作成する。
3.Centos5のイメージを、EC2向けにカスタマイズする。
4.作成したOSイメージを加工して、S3にアップし、EC2のマシンイメージとして登録する。
5.マシンイメージの実行、停止、マシンイメージの廃棄。


Leave a Reply