AWSにCentOS6.0をMyAMIとして作成する
AWSが用意してくれているサーバでもいいのですが、テスト環境は非AWSでCentOS6を使っているので、どうせなら合わせたいなというのがそもそもの需要です。まああとは興味本位。
今回はこちらのsuz-labさんのサイトを思いっきり参考にさせて頂きました。
※というか、AMI作成のコマンド手順などはまんまです。
であとは、私がわからなくて調べた点を追記して載せます。
こちらのサイトの完成度が高くすばらしいので、言葉尻は変えてますが前半はsuz-labさんの内容です。
自分の作業手順として残させて下さい。問題ありましたらご指摘下さい。
http://blog.suz-lab.com/2011/07/suz-lab-centos-ami-600.html
まずはRH6で土台となるサーバのインスタンスを生成します。
コンソールのVolumes→Create Volumeで新規ボリュームを作る
Sizeは6GiB、Zoneはお好きに(私はUS-Westで作りました)
作ったVolumeを右クリックして、Attache Volumeで紐付け
Device名を/dev/sdb1に
■元々土台になっているRHサーバにSSHしてext4でフォーマット
ls /dev/xvdf1 ←私はコレでした
/dev/xcdf1sudo su -
mkfs.ext4 /dev/xvdf1
■作成したVolume(/dev/xvdf1)をmount
mkdir /mnt/ami
mount -t ext4 /dev/xvdf1 /mnt/ami/
/sbin/MAKEDEV -d /mnt/ami/dev -x console
/sbin/MAKEDEV -d /mnt/ami/dev -x null
/sbin/MAKEDEV -d /mnt/ami/dev -x zerols /mnt/ami/dev/
console null tty zero
※lsで確認するとこんな感じで表示されるはず
■/etc/fstabの作成と、procファイルシステムのマウント
mkdir /mnt/ami/etc
vi /mnt/ami/etc/fstab
-
-
-
- -
-
-
/dev/xvda1 / ext4 defaults 1 1
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
-
-
-
- -
-
-
mkdir /mnt/ami/proc
mount -t proc noe /mnt/ami/proc/
■yumでCentOS6.0のCoreパッケージとカーネルのインストール
mv /etc/yum.repos.d /etc/yum.repos.d.bak
yum clean allvi /mnt/yum.conf
-
-
-
- -
-
-
※x86-64用の設定
[base]
name=CentOS-6 - Base
baseurl=http://mirror.centos.org/centos/6/os/x86_64/
[updates]
name=CentOS-6 - Updates
baseurl=http://mirror.centos.org/centos/6/updates/x86_64/
yum -c /mnt/yum.conf --installroot=/mnt/ami/ -y groupinstall Core
cp /mnt/ami/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 /etc/pki/rpm-gpg/
yum --installroot=/mnt/ami/ -y install kernel
■User Provided Kernelの設定
User Provided Kernelとは要するに
-
-
-
- -
-
-
ユーザがkernelをマシンイメージ内で普通に管理し、他の一般的な環境と同じようにマシンイメージ内のkernelを使えるよという事みたいです
vi /mnt/ami/boot/grub/menu.lst
-
-
-
- -
-
-
default=0
timeout=0
hiddenmenu
title CentOS 6
root (hd0)
kernel /boot/vmlinuz-2.6.32-71.29.1.el6.x86_64 ro root=/dev/xvda1
initrd /boot/initramfs-2.6.32-71.29.1.el6.x86_64.img
※kernelとinitrdは私が実際のファイルに合わせて変更対応入れてます
■ネットワークの設定
vi /mnt/ami/etc/sysconfig/network
-
-
-
- -
-
-
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain
vi /mnt/ami/etc/sysconfig/network-scripts/ifcfg-eth0
-
-
-
- -
-
-
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=on
vi /mnt/ami/etc/hosts
-
-
-
- -
-
-
127.0.0.1 localhost localhost.localdomain
■SELinux切ります
vi /mnt/ami/etc/sysconfig/selinuxSELINUX=disabled
SELINUXTYPE=targeted
■SSHの設定
起動時にAWSから公開鍵を取得し、事前に作成した秘密鍵でログインできるようにします。
"sshd"の設定も秘密鍵でしかログインできないようにしておきます。
vi /mnt/ami/etc/rc.local作成もとのRHからコピー
-
-
-
- -
-
-
#!/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
if [ ! -d /root/.ssh ] ; then
mkdir -p /root/.ssh
chmod 0700 /root/.ssh
restorecon /root/.ssh
fi
# bz 707364
if [ ! -f /etc/blkid/blkid.tab ] ; then
blkid /dev/xvda &>/dev/null
fi
ATTEMPTS=5
FAILED=0
# Fetch public key using HTTP
while [ ! -f /root/.ssh/authorized_keys ]; do
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/aws-key 2>/dev/null
if [ $? -eq 0 ]; then
cat /tmp/aws-key >> /root/.ssh/authorized_keys
chmod 0600 /root/.ssh/authorized_keys
restorecon /root/.ssh/authorized_keys
rm -f /tmp/aws-key
echo "Successfully retrieved AWS public key from instance metadata"
else
FAILED=$*1
if [ $FAILED -ge $ATTEMPTS ]; then
echo "Failed to retrieve AWS public key after $FAILED attempts, quitting"
break
fi
echo "Could not retrieve AWS public key (attempt #$FAILED/$ATTEMPTS), retrying in 5 seconds..."
sleep 5
fi
done
同じ設定の物は元のRHからコピー
cp -r /root/.ssh /mnt/ami/root/
cat /mnt/ami/root/.ssh/authorized_keys
cp /etc/ssh/sshd_config /mnt/ami/etc/ssh/sshd_config
■アンマウント
umount /mnt/ami/proc/
umount /mnt/ami/
ついでにwestの時はなぜか
ssh -i aws.pem root@ecx-xxx-xxx-xxx-xxx.compute-1.amazonaws.com
みたいな感じで出来たのに、eastだと「Please login as the ec2-user user rather than root user.」と言われてしまうので
(コレが正しいはずですが。。。)
その対策を取っておくcp -ar /home/ec2-user /mnt/ami/home/
vi /mnt/ami/etc/passwd
ec2-user:x:222:500:EC2 Default User:/home/ec2-user:/bin/bashvi /mnt/ami/etc/shadow
ec2-user:!!:15239:0:99999:7:::vi /mnt/ami/etc/group
wheel:x:10:root,ec2-user
ec2-user:x:500:vi /mnt/ami/etc/sudoers
## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALLssh -i aws.pem ec2-user@ecx-xxx-xxx-xxx-xxx.compute-1.amazonaws.com
■AWS Management Consoleにてスナップショットの作成
Volumes→該当のvolumeを右クリックしてDetach Volume
Statusがavailableになったら、同様に右クリックしてCreate snap shotするSnapshotsを見ると今作成した物が出来ているはずなので、そこでSnapshot IDを控えておく
ここから作成したスナップショットをAMIに登録します
■AMI-TOOLSの取得
mv /etc/yum.repos.d.bak /etc/yum.repos.d ← 最初にこれ変更しちゃったのでyum install ruby ruby-devel rdoc irb
cd /usr/local/src/
wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm
rpm -Uvh ec2-ami-tools.noarch.rpm
■AWSのPHPSDKを取得
yum -y install php
yum -y install gitcd /tmp
# gitでSDKを取得
git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP# SDKドキュメント
http://docs.amazonwebservices.com/AWSSDKforPHP/latest/index.html
■登録用のPHPの用意
us_west用の設定にしてaws.phpとかで適当にvi
お約束のAWS_KEYとAWS_SECRET_KEYはAWSWebサイトのAccount→Security Credentials(セキュリティ証明書)から取得して下さい※リージョンによってKernelIDは違うようです。詳しくはこれまたsuz-labさんを確認して下さい
http://blog.suz-lab.com/2011/07/user-provided-kernelpv-grub-hd000102.htmlset_region(AmazonEC2::REGION_US_W1);
$response = $ec2->register_image(array(
"Name" => "caa_ebs_centos-core-x86_64-6.0.0",
"Architecture" => "x86_64",
"KernelId" => "aki-8d396bc8",
"RootDeviceName" => "/dev/sda1",
"BlockDeviceMapping" => array(
array(
"DeviceName" => "/dev/sda1",
"Ebs" => array(
"SnapshotId" => "snap-807a33ee"
)
)
)
));var_dump($response);
?>
成功しているとImages→AMIsにPlatform Cent OSの物が出来ている
で、InstancesからMy AMIで作成するとインスタンスが出来上がるよ
それであとはいつも通りSSHして中身を確認
[root@ip- ~]# cat /etc/redhat-release
CentOS Linux release 6.0 (Final)
[root@ip- ~]#
できたよー
SSHできなかったりしたら、コンソールのGet System Log見てみるのがいいと思います。
私がミスしたのは、「/mnt/ami/boot/grub/menu.lst」の記述ミスでboot出来てなかったりしました
root (hd0)
Filesystem type is ext2fs, using whole disk
kernel /boot/vmlinuz-2.6.32-71.29.1.el6.i686 ro root=/dev/xvda1
vmlinuz-2.6.32-71.29.1.el6.x86_64Error 15: File not found
Press any key to continue...
*1:$FAILED + 1