Vyatta Core 6.3のAMIを作ってみた
Vyatta Core 6.1のAMIはあるが、6.3のが見当たらなかったので作ってみた(出来た東京リージョンのAMIはami-0204b003)。
■追記(2012/01/28)
他のリージョンにも同等AMIを作成しました
ami-0204b003 (ap-northeast-1)
ami-5c1d590e (ap-southeast-1)
ami-3d1b4278 (us-west-1)
ami-6c56db5c (us-west-2)
ami-eda67784 (us-east-1)
ami-87457bf3 (eu-west-1)
ami-c84d92d5 (sa-east-1)
ローカル(VM)にインストールする
作業にはOracle VM VirtualBoxを使いました。
後々の作業用にLinuxが入っているとよいです(Ubuntu 11.10を使いました)。
vyatta.orgのダウンロードページから、 VC6.3 - Virtualization isoをダウンロード
VirtualBoxで新規仮想マシン作成時に、Linux/Debianを指定、メモリは適当(512MB?)、新規ハードディスクで1000MBを指定
先ほどダウンロードしたISOで起動後 vyatta/vyatta でログインし、HDDへのインストールを行い、再起動する(CD-ROMイメージを抜くのを忘れないように)
vyatta@vyatta:~$ install-system (snip) How big of a root partition should I create? (1000MB-1049MB) [1049]MB: 1000MB (snip) vyatta@vyatta:~$ reboot
ローカル(VM)で最低限の設定をする
この時点で設定を行うのは、eth0のDHCP設定、MACアドレス指定の削除(EC2上では変わってしまうので)、sshサービス起動。
vyatta@vyatta:~$ configure [edit] vyatta@vyatta# set interfaces ethernet eth0 address dhcp [edit] vyatta@vyatta# delete interfaces ethernet eth0 hw-id xx:xx:xx:xx:xx:xx [edit] vyatta@vyatta# set service ssh [edit] vyatta@vyatta# commit [edit] vyatta@vyatta# save
再起動してちゃんとIPなどが取れていて、sshログインできれば作業完了。shutdownする。
インストール後のdiskイメージをEBS snapshotにし、AMI登録する
別のVM(Linux)にVyatta Core用のHDDイメージを追加し(screenshot)、起動する。/dev/sdbに見えていたので、
vm$ sudo dd if=/dev/sdb1 of=vyatta_root.bin 1953062+0 記録始め 1953062+0 記録終わり 999967744 バイト (1.0 GB) コピー終了, 37.8674 s, 26.4 MB/s
としてdiskイメージファイルとする。
作業用EC2インスタンスを起動して、diskイメージファイルをコピー
vm$ scp -i /path/to/keypair.pem vyatta_root.bin ec2-user@instance-address:.
EBSボリューム(1GB)を/dev/sdf等にattachして、diskイメージを書き込み、マウント後にpvgrub用ファイルを設定する
ec2$ sudo dd if=vyatta_root.bin of=/dev/sdf ec2$ sudo mount /dev/sdf /mnt ec2$ sudo vi /mnt/boot/grub/menu.lst default=0 timeout=3 hiddenmenu title Vyayya Core 6.3 root (hd0) kernel /boot/vmlinuz root=UUID=db768d4a-4bee-4cbc-8031-8aedcdfefea6 console=hvc0 initrd /boot/initrd.img # UUIDは各自違うので、/mnt/boot/grub/grub.cfgを確認のこと
以下EC2 API Toolsで
$ ec2-create-snapshot vol-7572aa19 -d "vyatta_root" SNAPSHOT snap-f334f099 vol-7572aa19 pending 2011-11-13T13:18:41+0000 407613804811 1 vyatta_root $ ec2-register -s snap-f334f099 --kernel aki-ec5df7ed -n "vyatta core 6.3" IMAGE ami-0e0fbb0f
これで仮AMIの作成が完了したので、起動チェックを行ってみる。設定したパスワードでログインできたらおk。
もしうまくログインできない場合には、ec2-get-consoleなどでトラブルシュートするとよい。
EC2用にカスタマイズする
前述のVC6.1 AMIには、user-dataからのconfig読み込みや、keypairの自動設定機能があったので、パクり参考にします。
/etc/rc.localに
su -c "/etc/import_vyatta_config.exp" su -c "/etc/import_ssh_pubkey.exp" vyatta
をexit 0の前に入れる
それぞれのスクリプトを設置、実行権限を付与しておく
/etc/import_vyatta_config.exp
#!/usr/bin/expect set timeout 5 spawn $env(SHELL) send "configure\r" expect -re ".*# $" send "load http://169.254.169.254/latest/user-data \r" expect { "? \\\[no\\\] " {send "n\r"} -re "### 100.0%.*# $" {send "commit \r"} timeout {send_user "timeout @1\n"; exit} eof {send_user "eof @1\n"; exit} } #expect -re ".*# $" #send "save\r" expect -re ".*# $" send "exit\r" #expect eof
/etc/import_ssh_pubkey.exp
#!/usr/bin/expect set timeout 30 spawn $env(SHELL) send "configure\r" expect -re ".*# $" send "loadkey vyatta http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key \r" expect { -re "### 100.0%.*# $" {send "set service ssh disable-password-authentication \r"} timeout {send_user "timeout @1\n"; exit} eof {send_user "eof @1\n"; exit} } expect -re ".*# $" send "commit\r" #expect -re ".*# $" #send "save\r" expect -re ".*# $" send "exit\r" expect { -re ":\[~/\]\[#$\] " {send "cat ~vyatta/.ssh/authorized_keys\r"} timeout {send_user "timeout @2\n"; exit} eof {send_user "eof @2\n"; exit} } expect { -re ":\[~/\]\[#$\] " {send "exit\r"} timeout {send_user "timeout @3\n"; exit} eof {send_user "eof @3\n"; exit} } #expect eof
ここまでの設定を行ったら、最後に -b "/dev/sdb=ephemeral0"オプションをつけてregisterしておきました。
TODO
VPC上ではmicroが使えないので、EBSを使用せずに動くようs3 backedなAMIもそのうち作ってみたいと思います。