Pack it up, pack it in, let me begin, err, umm… build
In this recipe, we’re going to use Packer to start from source media (such as an ISO) to create our very own Vagrant box from scratch.
Getting started
You will need the following for this recipe:
• Oracle Linux
• Oracle VM VirtualBox
• Packer
Refer to the Technical requirements section at the beginning of this chapter if you need help installing Oracle VM VirtualBox and Packer.
How to do it…
More often than not, you can search the Vagrant catalog and find pre-made Vagrantfiles containing the operating system you’re looking for. But what happens if what you need is not there? Or maybe you don’t trust the author of the Vagrantfile and/or you simply prefer to create your own. You can do this manually, or you can do this entirely with code, by leveraging Packer. In this recipe, we’ll use Packer to bake up a fresh Vagrant box from the Oracle Linux 8.8 source ISO. At a high level, Packer will download the ISO image for the operating system we wish to use, it will then spin up a VM using Oracle VM VirtualBox and install the VM. Afterward, it will export the VM in Open Virtualization Format (OVF). Finally, it will compress this file and convert it into a Vagrant box file.
Oracle Linux 8 kickstart file
- For this magic to work, you’ll need to provide a kickstart file to automate the installation of the ISO. You can create your own, or use one from the Oracle Linux Image Tools found in the official Oracle Linux repository on GitHub. For this recipe, I’ll be using one from the Oracle Linux Image Tools:
https://github.com/oracle/oracle-linux/blob/main/oracle-linux-image-tools/distr/ol8-slim/ol8-ks.cfg - Now, to get started with this recipe, we’ll create a new directory called ol8-vagrant. In this directory, create a Packer file and name it vagrant-ol8.pkr.hcl, and go ahead and create a folder called http and place the kickstart file there. Once this is done, your file structure should look like this:
└── vagrant-ol8
├── http
│ └── ol8-ks.cfg
└── vagrant-ol8.pkr.hcl
- In our kickstart file, we just need to set the password for the root user. So, in this case, we’ll change the line that reads rootpw –lock to rootpw –plaintext vagrant.
- Next, we’ll go ahead and work on the vagrant-ol8.pkr.hcl file. The first thing we’ll need to do here is specify the plugins we’ll need. Let’s use the VirtualBox Builder since we want to create a VM from an ISO image, and the Vagrant Builder since we’re going to convert our OVF file into a Vagrant box file. These can be added by placing the following code into your Packer configuration file:

Figure 8.36 – Packer VirtualBox and Vagrant builder plugins
INFO:
More info on the Packer VirtualBox plugin can be found here: https://www.packer.io/plugins/builders/virtualbox.
- Now we need to define a source. In this case, we want to use the Oracle Linux 8.8 ISO. Add the following to your vagrant-ol8.pkr.hcl file:

Figure 8.37 – Packer file for building Vagrant boxes