3

My goal was to create a Virtual Machine programmatically like you would do for a container with docker and be able to export it in an OVF.

So I used Vagrant to create the VM. Combining Vagrant and Packer I found 3 ways to achieve this with success.

I have two questions remaining:

  1. What the difference between the 3 methods displayed below? Or which one is the best and why?
  2. The user vagrant:vagrant will still be active that need to be removed afterward, and the root password changed from vagrant to something stronger. Is there a way to do this with Vagrant/Packer to not have to do it manually? Is there some other Vagrant box specific stuff I need to clean after the VM is built?

Below the 3 ways:

Base box > Vagrant Provisioning > Package as base box OVF

# Starts and provisions the vagrant environment
$ vagrant up

# Stops the vagrant machine
$ vagrant halt

# List VMs
$ vboxmanage list vms

# Package a vagrant box to a base box and so creating an OVF
$ vagrant package --base <VM_name> --output package.tar.gz

With the Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure('2') do |config|
  config.vm.box = 'archlinux/archlinux'
  config.vm.hostname = 'myhostname'
  config.vm.provision :shell, path: 'bootstrap.sh'
end

Base box > Vagrant Provisioning > Packer virtualbox-vm with export

# Starts and provisions the vagrant environment
$ vagrant up

# Stops the vagrant machine
$ vagrant halt

# List VMs
$ vboxmanage list vms

# Export to OVF
$ packer build packer.json

With the Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure('2') do |config|
  config.vm.box = 'archlinux/archlinux'
  config.vm.hostname = 'myhostname'
  config.vm.provision :shell, path: 'bootstrap.sh'
end

With the packer.json:

{
  "builders": [{
    "type"                  : "virtualbox-vm",
    "communicator"          : "ssh",
    "headless"              : "true",
    "ssh_username"          : "vagrant",
    "ssh_password"          : "vagrant",
    "ssh_wait_timeout"      : "30s",
    "shutdown_command"      : "echo 'packer' | sudo -S shutdown -P now",
    "guest_additions_mode"  : "disable",
    "output_directory"      : "./builds-vm",
    "vm_name"               : "<vm_name>",
    "attach_snapshot"       : null,
    "target_snapshot"       : null,
    "force_delete_snapshot" : "false",
    "keep_registered"       : "false",
    "skip_export"           : "false"
  }]
}

With this method I have more flexibility for the output.

Base box OVF > Packer virtualbox-ovf builder with provisioning + export

# Download vagrant base box
$ vagrant box add archlinux/archlinux --provider virtualbox

# Provisions and exports to OVF
packer build packer.json

With the packer.json:

{
  "builders": [{
    "type"                  : "virtualbox-ovf",
    "source_path"           : "/home/noraj/.vagrant.d/boxes/archlinux-VAGRANTSLASH-archlinux/2020.04.02/virtualbox/box.ovf",
    "communicator"          : "ssh",
    "headless"              : "true",
    "ssh_username"          : "vagrant",
    "ssh_password"          : "vagrant",
    "shutdown_command"      : "echo 'packer' | sudo -S shutdown -P now",
    "skip_export"           : "false",
    "output_directory"      : "packer-export"
  }],
  "provisioners": [{
    "type": "shell",
    "script": "bootstrap.sh"
  }]
}
noraj
  • 241
  • 2
  • 8

0 Answers0