Pack it up, pack it in, let me begin, err, umm… build 2 – DevOps Automation Tools – Terraform, Ansible, Packer, and More

Share this post on:

IMPORTANT NOTE
Notice the modifyvm command, which sets –nat-localhostreachable1 to on. This is a new setting that is off by default in Oracle VM VirtualBox 7. Setting this value to on fixes a networking issue that prevents Packer from sending the kickstart file to the VM. In other words, it’s important to include this if you’re using VirtualBox 7 to build your VM, but if you’re on VirtualBox 6, you’ll need to remove this line or your build will fail.

  1. So, in the preceding code, we specified the guest_os_type as “Oracle_64” because we’re building from an Oracle Linux ISO. Next, we input the URL for the ISO as well as the checksum. For ssh_username, we’ll use “root” and for ssh_password, we’ll use “vagrant” because these are the defaults that Vagrant expects in order to make things easy – especially if you plan to distribute your box publicly. If you intend to keep your box for private use only, it is best to use different values, as this will keep the box more secure.
    INFO
    For more information on Vagrant usernames and passwords, visit https://www.vagrantup.com/docs/boxes/base#vagrant-user.
  2. We’ll set headless to “true” in order to prevent the virtual machine from starting the VirtualBox GUI. We increase the time for ssh_wait_timeout to give the operating system sufficient time to install and to get up and running. All of this is self-explanatory; that is, until we get to the http_directory setting.
  3. All you really need to know is that we’ll leverage the http_directory option as a convenient way to serve a directory using an HTTP server. It does this so that the boot command can point to a kickstart file – which we’ve placed in the http directory. http://{{ .HTTPIP }}:{{ .HTTPPort }}/ol8-ks.cfg tells the operating system exactly what it needs to reach the kickstart file. You don’t really need to worry about the syntax of {{ .HTTPIP }} and {{ .HTTPPort }}; these are just template variables that are processed by the Packer templating engine.
  4. For nic_type, the default value is 82540EM, which equates to Intel PRO/1000 MT Desktop. That’s a safe choice and is great when running old operating systems, but Oracle Linux 8 is a modern operating system so we’re setting this to virtio. VirtIO is a para-virtualized driver and will give us better networking performance in the VM.
  5. Now, on to the boot_command. Basically, we need to figure out what sequence of key presses are required in order to send a boot command to the operating system at boot time. In the case of Oracle Linux 8, we send key presses of up and Tab, and then we send instructions on where to find the kickstart file, and finally we tell it to press Enter to begin installing the OS.
  6. For vboxmanage, I found that, by default, Packer tried to launch my VM with only 512 MB of memory, and that resulted in an error during the installation of the OS. In order to resolve this issue, I increased the memory to 2048. I also gave it two CPUs just for good measure.
  7. Finally, with the shutdown_command, we simply tell Packer how to shut down the system gracefully once all the provisioning is done. If we leave this blank Packer would forcefully shut down the machine.
    INFO
    For a complete overview of available configuration options, see https://www.packer.io/plugins/builders/virtualbox/iso.
  8. At this point, we have enough to install the OS automatically, but we need to do a few more things to prepare the virtual machine for Vagrant. To be specific, we need to add a user called vagrant, populate that user’s authorized keys with a known public key for that user, and finally we’ll want to install VirtualBox Guest Additions.
    For these tasks, we’ll leverage a provisioner to execute a shell script. This task takes place after the operating system is installed and while the virtual machine is still running. It’s the last thing we’ll do before we shut down the system and convert it to a Vagrant box.
    Go ahead and add the following to the vagrant-ol8.pkr.hcl file:

Figure 8.38 – Packer file for building Vagrant boxes (continued)

Share this post on:

Author: Stacy Atkins Prince

View all posts by Stacy Atkins Prince >

Leave a Reply

Your email address will not be published. Required fields are marked *