Vagrant用のBox(FreeBSD)をPackerで作成(失敗)

無難にVeeWeeでつくってみてうまくいったので、
調子に乗ってPackerでもつくってみることにしました。

まず、定義ファイルを作ります。
ゼロから作るのは大変なので、ここではどっかに転がっている定義ファイルをFreeBSD用に置き換えるようにします。
↓ここを参考にすることにしました。

CentOS - PackerでVagrant用のBoxを作成したときのメモ - Qiita [キータ]

変更点

ISOイメージまでのパス

iso_urlは、httpしかサポートしていないので、あらかじめダウンロードしてきたローカルのイメージを使います。
ということでローカルのISOまでのパスを書きますが、
Windowsの場合は、ファイルの区切りのバックスラッシュは\\のようにエスケープして書かなければいけないようです。

シャットダウンのコマンド(shutdown_command

VeeWeeのときのdefinition.rbを真似して、shutdown -h nowにします。

インストールのコマンド(boot_command

VeeWeeのときのinstall.shの中身を書く
ただし、%NAME%のような変数は{{.Name}}みたいなかんじに
<Enter>のようなものは<enter>に(すべて小文字) 直さないといけない。

インストール後のコマンド(provisionerscript

VeeWeeのpostinstall.shを指定する

で、そんな感じでできた定義ファイルがこれ:

{
  "builders": [
    {
      "type": "virtualbox",
      "vm_name": "box",
      "boot_wait": "10s",
      "disk_size": 10240,
      "guest_os_type": "FreeBSD_64",
      "iso_checksum": "f0a320fd52383c42649d48ea545915a9",
      "iso_checksum_type": "md5",
      "iso_url": "PATH_TO_ISO\\FreeBSD-9.1-RELEASE-amd64-disc1.iso",
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      "ssh_port": 22,
      "ssh_wait_timeout": "10000s",
      "shutdown_command": "shutdown -h now",
      "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
      "virtualbox_version_file": ".vbox_version",
      "vboxmanage": [
        [
          "modifyvm",
          "{{.Name}}",
          "--memory",
          "512"
        ],
        [
          "modifyvm",
          "{{.Name}}",
          "--cpus",
          "1"
        ]
      ],
      "http_directory": ".",
      "boot_command": [
        "<esc>",
        "load geom_mbr",
        "<enter>",
        "load zfs",
        "<enter>",
        "boot -s",
        "<enter>",
        "<wait><wait><wait><wait><wait><wait><wait><wait><wait><wait>",
        "/bin/sh<enter>",
        "mdmfs -s 100m md1 /tmp<enter>",
        "dhclient -l /tmp/dhclient.lease.em0 em0<enter>",
        "<wait><wait><wait>",
        "echo 'sleeping for 10 seconds, then running install script.'<enter>",
        "sleep 10 ; fetch -o /tmp/install.sh http://{{ .HTTPIP }}:{{ .HTTPPort }}/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh {{.Name}}<enter>",
        "<wait10>"
      ]
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "script": "postinstall.sh"
    }
  ],
  "post-processors": [
    {
      "type": "vagrant",
      "output": "freebsd-packer.box"
    }
  ]
}

で、こんな感じで動かしたら風呂上がりにはできてるかな、
と思ってコマンド打ちました

packer build -only=virtualbox freebsd.json

が、案の定失敗していました。うむむ。。

==> virtualbox: Connected to SSH!
==> virtualbox: Uploading VirtualBox version info (4.2.16)
==> virtualbox: Uploading VirtualBox guest additions ISO...
==> virtualbox: Provisioning with shell script: postinstall.sh
    virtualbox: PACKER_BUILD_NAME=virtualbox: Command not found.
    virtualbox:
    virtualbox:
==> virtualbox: Unregistering and deleting virtual machine...
==> virtualbox: Deleting output directory...
Build 'virtualbox' errored: Script exited with non-zero exit status: 1

==> Some builds didn't complete successfully and had errors:
--> virtualbox: Script exited with non-zero exit status: 1

==> Builds finished but no artifacts were created.

まあ続きはまた明日。