<< Blog Index

CentOS 7 in Vagrant
September 19, 2021

I spent today fiddling with Vagrant. It's pretty cool, though I wonder how practical it is compared to Docker. Having entire VMs set up is certainly convenient, but I'm not sure what Docker's containers can't do that VMs can.

Anyway, my goal today ended up being to successfully automate building a CentOS 7 box with VirtualBox Guest Additions installed.

The easy way is supposed to be just installing the vagrant-vbguest plugin, and you also need to set another flag to have it work "out of the box":

# Using vagrant-vbguest to install the guest additions.
config.vagrant.plugins = ["vagrant-vbguest"]
config.vbguest.installer_options = { allow_kernel_upgrade: true }

However, when using this, I noticed that it does not detect the guest additions properly and reinstalls it every time you vagrant up.

I'm not sure if this is specific to the centos/7 box, but I didn't like that extra overhead when booting up the system. The plugin could clearly use some improvement before we can use it here.

Another approach I had was just building another custom box. I'll describe a bit of that here.

What I kind of miss from Docker is the ability to reuse intermediate containers during the build process. With Vagrant, it seems like you do not have that convenience of layers. You just have the base box, and a saved state on top of that.

In order to produce another layer that you can base things off of, you can package a new box. Here is the script that I ended up with to create a base image with VBox Guest Additions installed:

build.bat

REM boot a new centos/7 box
vagrant init centos/7
vagrant up

REM Update everything - this is required in order to run
REM  the guest additions installer.
vagrant ssh -c "sudo yum -y update"
vagrant ssh -c "sudo yum -y install curl wget nano kernel-devel gcc"

REM Restart so that the new OS versions take effect,
REM  otherwise the guest additions won't install.
vagrant ssh -c "sudo shutdown -r 0"

REM Guest additions installation script.
type install-guest-additions.sh | vagrant ssh
vagrant ssh -c "Installation complete. Ready for packaging."

REM Halt and package
vagrant halt
vagrant package --output centos7-vbox.box

REM Cleanup.
vagrant destroy -f

install-guest-additions.sh

sudo cd /opt
sudo yum -y install curl wget

# Get the latest version from here.
latest=$(curl -s https://download.virtualbox.org/virtualbox/LATEST-STABLE.TXT)
sudo wget http://download.virtualbox.org/virtualbox/$latest/VBoxGuestAdditions_$latest.iso -O /opt/vbga.iso

# Mount and install. No x11 needed.
sudo mount /opt/vbga.iso -o loop /mnt
sudo sh /mnt/VBoxLinuxAdditions.run --nox11
sudo umount /mnt
sudo rm /opt/VBGAdd.iso

# Restart to make sure that guest additions is fully
#  installed when we package the box.
sudo shutdown -r 0

Sourced mostly from here: https://gist.github.com/jonasschultzmblox/f15fe3c10769d5f269635a54394c84d4

This builds a handy centos7-vbox.box box that you can reference in other Vagrantfiles, for example:

config.vm.box = "file://centos7-vbox/centos7-vbox.box"

The file: URL format seemed a bit awkward to me when writing my Vagrantfile. I got errors when I tried to use a normal relative file path.

Furthermore, I think you can save yourself some trouble if you look into the Bento CentOS builds.

config.vm.box = "bento/centos7"

I tried this just now, and vagrant up seemed to work exactly as expected - everything ready to go with the shared folder mounted properly.

<< Blog Index