Golo Roden
Golo Roden

Reputation: 150624

Create a VM image from a Vagrant box using Packer?

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?

Upvotes: 6

Views: 5353

Answers (1)

Sneal
Sneal

Reputation: 2586

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.

Upvotes: 7

Related Questions