続報 PackerでVagrant用のBoxを作成する
Packerってなに?という人は前回のエントリを先にどうぞ
0.10と0.11だと作成されたVagrantのboxの中のディスクイメージの命名の問題があります(詳細はこちら)。ソースを持ってきてビルドしたPackerを使えばとりあえず問題ありません。→0.12で修正されました!
前のエントリで紹介したPackerですが、Vagrantのboxの作り方が把握できたので紹介しておきます。 今回はUbuntuのboxの作成を例にして解説します。 なお、CentOSの例は以下に置いておきました。
まずは設定ファイルです。前の記事で紹介したものより長くなっています。
{
"builders":[{
"type": "virtualbox",
"guest_os_type": "Ubuntu_64",
"iso_url": "http://releases.ubuntu.com/12.04/ubuntu-12.04.2-server-amd64.iso",
"iso_md5": "af5f788aee1b32c4b2634734309cc9e9",
"ssh_username": "vagrant",
"ssh_password": "vagrant",
"ssh_wait_timeout": "3000s",
"vm_name": "box",
"http_directory": "./",
"boot_wait": "30s",
"boot_command":[
"<esc></esc><esc><enter><wait>",
"/install/vmlinuz noapic ",
"preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg ",
"debian-installer=en_US auto locale=en_US kbd-chooser/method=us ",
"hostname={{ .Name }} ",
"fb=false debconf/frontend=noninteractive ",
"keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA ",
"keyboard-configuration/variant=USA console-setup/ask_detect=false ",
"initrd=/install/initrd.gz -- ",
"<enter><wait>"
]
}],
"post-processors": [
{
"type": "vagrant",
"output": "./ubuntu-12.04.2-server-amd64.box"
}
]
}
項目毎に説明します。
- typeはvirtualboxにします
- guest_os_typeは、vboxmanage list ostypes のコマンドを実行して自分が作成するOSのvirtualboxでのIDを指定します
- iso_urlとiso_md5はOSのインストールイメージに関する情報です。httpでなくてもローカルディレクトリでも大丈夫です
- ssh_usernameとssh_passwordはインスタンスにログインする際に使うものです。実際のインストール時のパスワードの設定についてはpreseed.cfgで指定(後述)します
- http_directoryは、インストール時にローカルにWebサーバをたててkickstartなどの設定情報を持っていくための設定です。今回は同じ場所に設定しました
- vm_nameはVirtualBoxなどで出力する際のDiskなどのファイル名です。Vagrantはboxという名前を期待するようです
- boot_waitは、最初にVirtualboxが起動されてから待つ時間で、短すぎるとコマンドの送信に失敗する可能性があるので長くしています
- boot_commandは、インストール時にPacker側から送り込む自動操作のコマンド群です。enterやreturnやwaitやescはそのキーを送信することを意味しています。このboot_commandはOSの種類によって変わりますが、この設定はVirtualBoxやVMwareなどのためにインストーラー経由でインストールする場合には必ず必要です
- post-processorsの箇所では、インストール終了後の出力処理について記載しており、ここではvagrant用のboxとしてoutputで指定した場所と名前で保存するように指定します
さてboot_command
についてはさらに注意が必要です。上の例を見て分かるとおり、preseed.cfgというファイルを自動インストール用に使っているので、これを用意する必要があるのです。このファイルがないとOSのインストーラーはすべて対話モードでインストールを行おうとするので自動化できなくなってしまいます(このあたりはOSの種類によってファイルの書き方や挙動が違いますが、Kickstartのような仕掛けはどのOSも用意しているのでその流儀にしたがってください) 今回はUbuntuということで以下のようにしました。(Veeweeから持ってこれます)。各項目の設定内容は、Ubuntuのサイトを確認してください。 このファイルを先ほどのhttp_directoryを設定したディレクトリに配置します(今回でいえば設定ファイルと同じ場所ということになります)
## Options to set on the command line
d-i debian-installer/locale string en_US.utf8
d-i console-setup/ask_detect boolean false
d-i console-setup/layout string USA
#d-i netcfg/get_hostname string dummy
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i time/zone string UTC
d-i clock-setup/utc-auto boolean true
d-i clock-setup/utc boolean true
d-i kbd-chooser/method select American English
d-i netcfg/wireless_wep string
d-i base-installer/kernel/override-image string linux-server
# Choices: Dialog, Readline, Gnome, Kde, Editor, Noninteractive
d-i debconf debconf/frontend select Noninteractive
d-i pkgsel/install-language-support boolean false
tasksel tasksel/first multiselect standard, ubuntu-server
d-i partman-auto/method string lvm
d-i partman-lvm/confirm boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman/confirm_write_new_label boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
# Write the changes to disks and configure LVM?
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto-lvm/guided_size string max
## Default user, we can get away with a recipe to change this
d-i passwd/user-fullname string vagrant
d-i passwd/username string vagrant
d-i passwd/user-password password vagrant
d-i passwd/user-password-again password vagrant
d-i user-setup/encrypt-home boolean false
d-i user-setup/allow-password-weak boolean true
## minimum is puppet and ssh and ntp
d-i pkgsel/include string openssh-server ntp
# Whether to upgrade packages after debootstrap.
# Allowed values: none, safe-upgrade, full-upgrade
d-i pkgsel/upgrade select full-upgrade
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i finish-install/reboot_in_progress note
#For the update
d-i pkgsel/update-policy select none
choose-mirror-bin mirror/http/proxy string
あとは普通にPackerを実行すればOKです。
packer build machine.json
すると以下のように出力されます。
virtualbox output will be in this color.
==> virtualbox: Downloading VirtualBox guest additions. Progress will be shown periodically.
==> virtualbox: Copying or downloading ISO. Progress will be reported periodically.
==> virtualbox: Starting HTTP server on port 8081
==> virtualbox: Creating virtual machine...
==> virtualbox: Creating hard drive...
==> virtualbox: Creating forwarded port mapping for SSH (host port 4382)
==> virtualbox: Starting the virtual machine...
==> virtualbox: Waiting 30s for boot...
==> virtualbox: Typing the boot command...
==> virtualbox: Waiting for SSH to become available...
==> virtualbox: Connected via SSH!
==> virtualbox: Uploading VirtualBox version info (4.2.12)
==> virtualbox: Uploading VirtualBox guest additions ISO...
==> virtualbox: Halting the virtual machine...
==> virtualbox: Exporting virtual machine...
==> virtualbox: Unregistering and deleting virtual machine...
==> virtualbox: Running post-processor: vagrant
==> virtualbox (vagrant): Creating Vagrant box for 'virtualbox' provider
virtualbox (vagrant): Copying: virtualbox/packer-disk1.vmdk
virtualbox (vagrant): Copying: virtualbox/packer.ovf
virtualbox (vagrant): Compressing box...
Build 'virtualbox' finished.
==> Builds finished. The artifacts of successful builds are:
--> virtualbox: 'virtualbox' provider box: ./ubuntu-12.04.2-server-amd64.box
分かってみるとそんなに大変ではないような気がします。ただ、いまのところ情報が少ないのでちょっと大変なのと、他のOSを使う場合にも、boot_commandあたりを頑張って調べて書かなきゃいけない(しかもデバッグ難しい)というところがちょっと悩ましいですが、そのうちboot_commandの例のようなやつは公開されてくるのではないかと思います。
なお、上記の例ではBoxのVirtualBoxのGuestAdditionなどを導入していませんので、これだけでは実用になりません。 前で紹介したようにプロビジョニングを行うようにしてください。
アジャイルコーチングやトレーニングを提供しています
株式会社アトラクタでは、アジャイル開発に取り組むチーム向けのコーチングや、認定スクラムマスター研修などの各種トレーニングを提供しています。ぜひお気軽にご相談ください。
詳細はこちら