6

I know that I can use Packer to create my own VM images in a scripted way. If I use the VirtualBox builder, I can choose from one of two flavors: Building everything from scratch, or building on top of an existing VM.

Basically, what I would like to achieve is to build on top of an existing Vagrant box (Ubuntu 15.04 with Docker by Boxcutter).

Is this possible using Packer, and if so, how? I was not able to find anything on this in the documentation. The samples always only refer to OVF/OVA files. Any hints?

Golo Roden
  • 140,679
  • 96
  • 298
  • 425

1 Answers1

7

This isn't really a workflow natively supported by Packer, but you could write a small shell script to download the Vagrant box, export the OVF and then kick off Packer's virtualbox-ovf builder.

The shell script (note this is hardcoded to the 1.1.0 version of the box)

#!/bin/sh

vagrant init boxcutter/ubuntu1504-docker
vagrant up --provider virtualbox --no-provision
vagrant halt

rm -rf output-virtualbox-ovf
packer build packer.json

packer.json

{
  "variables": {
    "home": "{{env `HOME`}}"
  },
  "builders": [{
    "type": "virtualbox-ovf",
    "source_path": "{{user `home`}}/.vagrant.d/boxes/boxcutter-VAGRANTSLASH-ubuntu1504-docker/1.1.0/virtualbox/box.ovf",
    "ssh_username": "vagrant",
    "ssh_password": "vagrant",
    "ssh_wait_timeout": "30s",
    "shutdown_command": "echo 'packer' | sudo -S shutdown -P now"
  }],
  "provisioners": [{
    "type": "shell",
    "inline": ["echo 'my additional provisioning steps'"]
  }],
  "post-processors": [{
    "type": "vagrant",
    "keep_input_artifact": true,
    "output": "box/modified-boxcutter-VAGRANTSLASH-ubuntu1504-docker.box"
  }]
}

This will create a new Vagrant box packaged up in box/modified-boxcutter-VAGRANTSLASH-ubuntu1504-docker.box.

Sneal
  • 2,546
  • 20
  • 22
  • 3
    A `vagrant box add boxcutter/ubuntu1504-docker --provider virtualbox` is sufficient to download and extract the Vagrant.box without starting any VM. – Stefan Scherer Jul 12 '15 at 19:25
  • Also FWIW, the above box start/halt workflow also disregards any state created by launching the instance (ie. ssh host keys, log files, etc). – EmmEff Apr 08 '18 at 14:13