事情により新しいアカウントを作って Amazon EC2 を再出発。



Amazon EC2 で CentOS 5 i386 Base AMI (ami-0459bc6d) を使って自分の AMI を作るときには、いつもこのエラーが起こる。そういえば何か起こる気がするなと思いつつも、エラーメッセージを見るその瞬間までは、何が起こるのかを忘れている。



<pre class="prettyprint">[EC2]
-bash-3.1# ec2-bundle-vol -d /mnt -k /mnt/pk-xxx.pem -c
/mnt/cert-xxx.pem -u xxx -r i386 -p test2
Copying / into the image file /mnt/test2…
Excluding:
/sys
/proc
/dev/pts
/proc/sys/fs/binfmt_misc
/dev
/media
/mnt
/proc
/sys
/mnt/test2
/mnt/img-mnt
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.003021 seconds, 347 MB/s
mke2fs 1.39 (29-May-2006)
mount: Could not find any loop device. Maybe this kernel does not know
about the loop device? (If so, recompile or `modprobe loop’.)
execution failed: “mount -o loop /mnt/test2 /mnt/img-mnt”
ec2-bundle-vol failed.
</pre>

そのたびに、どうするんだっけと思ってググるのだが、今日見たら注意書きがあった。



<blockquote>Edited to add:

Run yum -y upgrade to upgrade to CentOS 5.2.

Run depmod -a; modprobe loop before rebundling image.

</blockquote>

なんだー。それだけでいいのか。



yum -y upgrade は10分ほどで完了。depmod -a; modprobe loop

の後に念のために reboot してから bundling 開始。



<pre class="prettyprint">[EC2]
-bash-3.2# rm -rf /mnt/img-mnt /mnt/test2
-bash-3.1# ec2-bundle-vol -d /mnt -k /mnt/pk-xxx.pem -c
/mnt/cert-xxx.pem -u xxx -r i386 -p test2
Copying / into the image file /mnt/test2…
Excluding:
/sys
/proc
/dev/pts
/proc/sys/fs/binfmt_misc
/dev
/media
/mnt
/proc
/sys
/mnt/test2
/mnt/img-mnt
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.002869 seconds, 365 MB/s
mke2fs 1.39 (29-May-2006)
Bundling image file…
(略)
Created test2.part.48
Created test2.part.49
Created test2.part.50
Generating digests for each part…
Digests generated.
Unable to read instance meta-data for ancestor-ami-ids
Unable to read instance meta-data for kernel-id
Unable to read instance meta-data for ramdisk-id
Unable to read instance meta-data for product-codes
Creating bundle manifest…
ec2-bundle-vol complete.
</pre>

これも10分ほどで完了。さて、Amazon S3 にアップロードしておこう。ん、新しいアカウントだから bucket

から作らないといけないな。ruby で作ることにする。



<pre class="prettyprint">[Local]
-bash-3.2$ sudo gem i aws-s3
Password:
Successfully installed xml-simple-1.0.11
Successfully installed builder-2.1.2
Successfully installed mime-types-1.15
Successfully installed aws-s3-0.5.1
4 gems installed
Installing ri documentation for builder-2.1.2…
ERROR: While generating documentation for builder-2.1.2
… MESSAGE: Unhandled special: Special: type=17, text=”
… RDOC args: –ri –op /usr/lib/ruby/gems/1.8/doc/builder-2.1.2/ri
–title Builder – Easy XML Building –main README –line-numbers
–quiet lib CHANGES Rakefile README doc/releases/builder-1.2.4.rdoc
doc/releases/builder-2.0.0.rdoc doc/releases/builder-2.1.1.rdoc
(continuing with the rest of the installation)
Installing ri documentation for mime-types-1.15…
Installing ri documentation for aws-s3-0.5.1…
Installing RDoc documentation for builder-2.1.2…
Installing RDoc documentation for mime-types-1.15…
Installing RDoc documentation for aws-s3-0.5.1…
</pre>

なんかエラーが出ているけど無視。bucket を作成する。



<pre class="prettyprint">[Local]
-bash-3.2$ s3sh
» AWS::S3::Base.establish_connection!(
?> :access_key_id => ‘xxx’,
?> :secret_access_key => ‘xxx’
» )
=> #<AWS::S3::Connection:0xb77764d0 @access_key_id=”xxx”,
@options={:persistent=>false, :server=>”s3.amazonaws.com”,
:access_key_id=>”xxx”, :secret_access_key=>”xxx”, :port=>80},
@http=#<Net::HTTP s3.amazonaws.com:80 open=false>,
@secret_access_key=”xxx”>
» Bucket.create(‘mybucket’)
=> true
</pre>

S3 にアップロードする。



<pre class="prettyprint">[EC2]
-bash-3.2# ec2-upload-bundle -b mybucket/ami/test2 -m /mnt/test2.manifest.xml -a [Your Access Key ID] -s [Your Secret Access Key] –retry
Setting bucket ACL to allow EC2 read access …
Uploading bundled image parts to
https://s3.amazonaws.com:443/mybucket/ami/test2 …
Uploaded test2.part.00 to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.part.00
Uploaded test2.part.01 to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.part.01
Uploaded test2.part.02 to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.part.02
Uploaded test2.part.03 to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.part.03
(略)
Uploaded test2.part.48 to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.part.48
Uploaded test2.part.49 to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.part.49
Uploaded test2.part.50 to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.part.50
Uploading manifest …
Uploaded manifest to
https://s3.amazonaws.com:443/mybucket/ami/test2/test2.manifest.xml
Bundle upload completed.
</pre>

AMI を登録して、今動いているインスタンスを止め、自分で登録した AMI を動かす。



<pre class="prettyprint">[Local]
$ ec2-register mybucket/ami/test2/test2.manifest.xml
IMAGE ami-a2799dcb

$ ec2-terminate-instances i-7bdf6312
INSTANCE i-7bdf6312 running shutting-down

$ ec2-run-instances ami-a2799dcb
RESERVATION r-4c822125 987518932573 default
INSTANCE i-e5d66a8c ami-a2799dcb
pending m1.small 2008-12-05T05:06:36+0000 us-east-1a
</pre>

MySQLのデータ格納場所として、Elastic Block Store を追加する。データ量はそんなに増えないと思うから1GBだけ。



<pre class="prettyprint">[Local]
$ ec2-create-volume –size 1 -z us-east-1a
VOLUME vol-538c683a 1 us-east-1a creating
2008-12-05T06:37:22+0000

$ ec2-describe-volumes vol-538c683a
VOLUME vol-538c683a 1 us-east-1a available
2008-12-05T06:37:22+0000

$ ec2-attach-volume vol-538c683a -i i-9dd66af4 -d /dev/sdh
ATTACHMENT vol-538c683a i-9dd66af4 /dev/sdh
attaching 2008-12-05T06:38:17+0000

$ ec2-describe-volumes
VOLUME vol-538c683a 1 us-east-1a in-use
2008-12-05T06:37:22+0000
ATTACHMENT vol-538c683a i-9dd66af4 /dev/sdh
attached 2008-12-05T06:38:17+0000
</pre>

ファイルシステムを作成してマウントする。



<pre class="prettyprint">[EC2]
-bash-3.2# ls -al /dev/sd*
brw-r—– 1 root disk 8, 1 Dec 5 00:15 /dev/sda1
brw-r—– 1 root disk 8, 2 Dec 5 00:15 /dev/sda2
brw-r—– 1 root disk 8, 3 Dec 5 00:15 /dev/sda3
brw-r—– 1 root disk 8, 112 Dec 5 01:50 /dev/sdh

-bash-3.2# mkfs -t ext3 /dev/sdh
mke2fs 1.39 (29-May-2006)
/dev/sdh is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
131072 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

Writing inode tables: …done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

-bash-3.2# mount /dev/sdh /vol/vol1
</pre>

既存の環境からデータをコピーしてディスク容量チェックをしたら、すでに EBS を 73% も消費していることがわかって、ちょっとびっくり。1GB で月に 10セント(約10円)なのだから、けちけちせずに10GBくらい確保しておけばよかった。でも、足りなくなってもすぐに追加とか交換ができるから問題なし。



<pre class="prettyprint">[EC2]
-bash-3.2# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 10321208 4724484 5072436 49% /
none 870472 0 870472 0% /dev/shm
/dev/sda2 153899044 192072 145889348 1% /mnt
/dev/sdh 1032088 708372 271288 73% /vol/vol1
</pre>