Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 454
Golo Roden Avatar asked Jul 06 '15 21:07

Golo Roden


People also ask

Does Packer use Vagrant?

Packer will not install vagrant, nor will it install the underlying virtualization platforms or extra providers; We expect when you run this builder that you have already installed what you need.

How do you make a Vagrant box with Packers?

Create a Packer build These steps take ISO for Windows 10 build 1803, install the OS in Oracle VM VirtualBox, run a few PowerShell scripts and then export the build to a Vagrant box. There are a few things to point out in this Packer build. First, VirtualBox is the only provider on which the build will run.


1 Answers

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.

like image 66
Sneal Avatar answered Sep 19 '22 12:09

Sneal