A set of Packer templates and accompanying files to provision a needed Ubuntu VM work environment step-by-step.
Provisioning is done in several stages, through VM snapshots for connecting steps, for example -
- Minimum Ubuntu installation in VM - Creates a VM with
basesnapshot. - Provisioning minimum VM - Install needed packages, config files,
etc. Process starts from
basesnapshot, going through intermediate custom-named snapshots, and finally createsprovisionedsnapshot. - Prepare for export, perform export - Do cleanup, minimization of
provisionedsnapshot, then export VM to OVF.
This staging is done in order to:
- Speed up learning how to use Packer - Snapshots allow for faster retrying of a certain provisioning phase.
- To eliminate duplicated provisioning for different VMs that share a common basis.
- Packer template - Specifies the particular provisioning phase.
- Machine type - Set of Packer variables that describe a particular machine (eg. Ubuntu 19.10) for the given Packer template. Used to perform different provisioning phases on the same machine.
- VM name - Name for the VM on which Packer will operate. Defaults to the machine type.
- Packer templates are provided as
.jsonfiles in the repo root. They may be grouped through naming convention (see bellow on that). - You can find a template-accompanying stuff in the repo root:
scripts- Scripts to be executed during provisioning, as needed by a given template.files- Files that may be deployed on the target, either by these scripts, or manually.
- Each template (or a template group) has a same-named directory with
accompanying stuff, as described for the repo root. In addition,
there is:
var-files- Machine types.
- VM disks are located in
outputdirectory - Output is generated in the following directories:
output-<machine-name>for OVA files.
Packer templates are grouped through a naming scheme -
<category>_<name>.*. This is a pure syntactical thing used to more
easily find necessary files.
You'll need to have on your machine installed -
- Packer (https://www.packer.io/) - Tested with
>= 1.4.4. - VirtualBox (https://www.virtualbox.org/) - Tested with
>= 6.1.2.
Run Packer by providing:
- A template name for the desired operation.
- A var file for selecting the machine.
Example for Ubuntu 19.10 (preseed) initial setup -
packer build \
-var 'shared_folder_path=/c/temp' \
-var-file=minimum-ubuntu-install/var-files/preseed/ubuntu1910-desktop.json \
minimum-ubuntu-install_preseed.jsonFor Ubuntu 22.04 (autoinstall) it is -
packer build \
-var 'shared_folder_path=/c/temp' \
-var-file='minimum-ubuntu-install/var-files/autoinstall/ubuntu2204-desktop.pkr.hcl' \
-only "virtualbox-iso.autoinstall" \
.These may be followed by setup, and export runs. Here's an example for 19.10, to set up graphical interface, do Buildroot build and network boot setup for RPi, and export the machine -
packer build \
-var 'attach_snapshot=base' -var 'target_snapshot=xfce' \
-var-file=provision-ubuntu-vm/var-files/ubuntu1910-desktop.json \
-var 'ansible_playbooks=xfce4,doublecmd' \
ansible-provision-ubuntu-vm.json
packer build \
-var 'attach_snapshot=xfce' \
-var-file=provision-ubuntu-vm/var-files/ubuntu1910-desktop.json \
provision-ubuntu-vm_rpi-buildroot.json
packer build \
-var-file=export-ubuntu-ovf/var-files/ubuntu1910-desktop.json \
export-ubuntu-ovf.jsonNote in the previous example that there are two ways provisioning how provisioning was done:
- Through standalone Packer template running shell scripts -
provision-ubuntu-vm_rpi-buildroot.json. - Through common Ansible provisioning template running provisioning
for the given components -
ansible-provision-ubuntu-vm.json. In this case provisioned components arexfce4, anddoublecmd.
Ansible-way is preferred for anything but the base provisioning. The first way was the original one and will be fully transitioned to Ansible with time.
For intermediate provisioning, snapshot names have to be changed from
the default ones (base as an attach, provisioned as a target
snapshot). Sample -
packer build \
-var 'attach_snapshot=xfce' -var 'target_snapshot=rpi' \
-var 'buildroot_archive_path=//media/sf_virtualbox/buildroot.tgz' \
-var-file=provision-ubuntu-vm/var-files/ubuntu1910-desktop.json \
provision-ubuntu-vm_rpi-buildroot.jsonThis sample also contains an additional configuration var,
buildroot_archive_path.
For building up the VM, it is useful to have a shared directory
attached. So, this template requires specifying a shared directory to be
mounted to the VM, host path specified as a shared_folder_path var.
Vagrant box export is optional. In order to get it, it is necessary to:
- Set
install_vagrant_keytotruewhen provisioning the VM. - Run:
packer build \ -var-file=export-vagrant-box/var-files/ubuntu1910-desktop.json \ export-vagrant-box.json
- Marko Oklobdzija - https://github.com/kibihrchak
See also the list of GitHub contributors who participated in this project.
- Base Packer templates by fasmat - https://github.com/fasmat/ubuntu
- Readme file basis by PurpleBooth - https://gist.github.com/PurpleBooth/109311bb0361f32d87a2
- Ubuntu 20.04 image link - https://github.com/boxcutter/ubuntu/blob/master/ubuntu2004.json