8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Vm » Here

Create a Vagrant Base Box (VirtualBox)

When using Vagrant you often use an existing Box as the base for your installation. In some situations you will want to create your own boxes. This could be because a box doesn't already exist, you have common setup you would prefer not the repeat for every build, or because you prefer not to use other people's boxes. In this article we demonstrate the creation of a new Vagrant base box using a Fedora distribution and VirtualBox. The approach would be similar for other Linux distributions.

Related articles.

Create a Virtual Machine (VM)

We start by manually creating a virtual machine (VM) in VirtualBox in the normal way. In this example we are building a box for Fedora 30 beta, because at the time of writing there isn't a box available for the beta release. The VM was created with the following details.

The VM definition is purposely simple, as we will tailor it for every installation using the Vagrantfile for that specific installation. That includes changing the VM name, CPU, memory, network configuration and any additional storage. The only thing we really care about is the virtual disk is big enough to be useful for basic work, and there is a NAT network adaptor, which is mandatory for a Vagrant box.

In this example we installed the operating system using the "Fedora-Server-dvd-x86_64-30_Beta-1.8.iso" media. We did a basic installation, trying not to deviate from the defaults, and with minimal additional software. Once complete, we made sure the installation media was no longer attached to the VM.

At this point we have a functioning VM, with very little installed on it.

Remember not to put anything private on the box, as anyone else using the box may have access to it!

Mandatory Vagrant Configuration

With the VM in place we need to do the mandatory Vagrant-specific configuration. A Vagrant base box must conform to a few specific standards in terms of configuration. All this configuration is done as the "root" user inside the VM.

The "root" user password must be set to "vagrant" and there needs to be a user called "vagrant" with a password of "vagrant". You could do this setup during the installation, but if not here is what you need to do.

# Set root Password to vagrant.
echo -e "vagrant\nvagrant" | passwd

# Vagrant user with password of vagrant.
useradd vagrant
echo -e "vagrant\nvagrant" | passwd vagrant

We need to give the "vagrant" user password-less sudo, by amending the "/etc/sudoers" file.

# Give vagrant user passwordless sudo.
cat >> /etc/sudoers <<EOF

All vagrant boxes use a known insecure public key, so Vagrant can make the initial connection when provisioning a new VM. It sounds scary, but when you look at the output when creating a new VM from a box you will see something like this.

    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...

We can see the insecure key was replaced by a newly generated keypair, and the insecure keypair removed.

The insecure public key is available from github. The commands below download it to create the "authorized_keys" file for the "vagrant" user.

# Add insecure public key.
mkdir /home/vagrant/.ssh
wget -O /home/vagrant/.ssh/authorized_keys https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub
chown -R vagrant:vagrant /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
chmod 0600 /home/vagrant/.ssh/authorized_keys

Next we need to install the VirtualBox guest additions. Before we can do that, we need to make sure some prerequisite packages are present. We will also take the opportunity to update all the existing packages and reboot the VM.

# Install the packages required for the guest additions.
dnf install -y gcc kernel-devel kernel-headers dkms make bzip2 perl
dnf update -y

If you were doing this on Oracle Linux 7 (OL7), you would have to enable EPEL, and use Yum instead.

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y gcc kernel-devel kernel-headers dkms make bzip2 perl
yum update -y

Once the VM is rebooted, we can install the guest additions.

We attach the guest additions image using the "Devices > Insert Guest Additions CD image..." menu option on the VM window. We then need to mount the CD and install the guest additions using the following commands. Notice we eject the guest additions media once complete.

mkdir /media/VirtualBoxGuestAdditions
mount -r /dev/cdrom /media/VirtualBoxGuestAdditions
export KERN_DIR=/usr/src/kernels/`uname -r`/build
cd /media/VirtualBoxGuestAdditions
cd /

We've not installed X or any window manager, so the graphics modules will fail to compile. This is fine.

The Vagrant configuration is complete, so we need to shut down the VM.

shutdown -h now

Package the Box

Once the VM is created and the Vagrant configuration is complete, we can turn the VM into a Vagrant box using the "vagrant package" command. We switch to a directory where we want the package to be created, then we issue the "vagrant package" command. The "--base" flag is used to specify which VM should be packaged into a box, and the "--output" flag allows us to name the resulting package. Depending on the size of the VM disks, this can take a while to complete.

cd \temp
vagrant package --base fedora-30 --output fedora-30.box

Once we have the package, we can make it available for use by adding it to our list of available boxes. The follow commands add the new box and list the currently available boxes.

vagrant box add C:\temp\fedora-30.box --name oraclebase/fedora-30

vagrant box list
bento/oracle-7.6     (virtualbox, 201812.27.0)
oraclebase/fedora-30 (virtualbox, 0)

Now you have the box, you can use it like any other box by referencing it in a Vagrantfile for a new build.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.