Skip to content

Use VirtualBox to create an appliance

The web is full of recipes  on how to use VirtualBox to create a virtual machine image from scratch using an ISO image (and “orchestrated” this process by using  another tool like packer, unfortunately, packer does not yet “orchestrate” the following recipe).

This page describes the way to install using only the boot files belonging to the operating system (OS) you are installing.  VirtualBox comes equipped with a built-in PXE-boot server. VirtualBox takes care of the tftp and dhcp servers involved.

After installation, you can use VirtualBox to create an appliance ready to import in most hypervisors or use as a base box to build other appliances with Vagrant and VirtualBox as a Provider.

Operating systems supported

You should be able to use this recipe on every host operating system that is supported by VirtualBox. All commands below are written with Unix/Linux in mind. So for, e.g. OS X, you should change the ~/.config part to ~/Library (and think of some alternative for /etc/hosts or use IP addresses). Everything is expected to work the same way on Windows, so feel free to translate everything to Windows speak, the “VBoxManage” command is also included on Windows (maybe you need to find an alternative for “ln” or just copy).

As guest operating system (the one you are installing) you should be able to use everything that supports PXE boot (e.g. see this page for OmniOS). For Linux this means you need to use SYSLINUXPXELINUX and the way to install your favourite distribution hands free (Kickstart for any Enterprise Linux (e.g. CentOS) or even Ubuntu and ESXi, for most Debian based flavours you might prefer Preseed). Of course you can also install that other family of operating systems.

Create a virtual machine

Use the VirtualBox command line to create a virtual machine definition. In the example the lowercase alias is used, <myVMs> refers to the directory where your virtual machines are stored. You should use what applies to your system and preferences (e.g. translate stuff to Windows speak).

See sample script

Configure VirtualBox PXE boot

Of course now you can install any OS that VirtualBox can run. In order to install Linux your can configure SysLinux to launch a Linux installation from PXE boot:
  • Get the required boot images
    • Select the nearest mirror for the distribution of choice (e.g. CentOS)
    • Navigate to the isolinux directory (7/os/x86_64/isolinux/)
  • Optionally create a nice boot splash to show during boot when not running headless, see example below.
  • Create a TFTP root directory in your VirtualBox configuration directory (where your VirtualBox.xml is, usually ~/.config/VirtualBox/VirtualBox.xml on Linux, ~/Library/VirtualBox/VirtualBox.xml on OS X)
    • If you are working with multiple OS-es, eventually rename the directory and create a symbolic link named TFTP to the flavour you are currently installing.
  • copy the (downloaded) required syslinux files, if you run Linux, you have them locally available (usually the exact version should not matter).

    mkdir ~/.config/VirtualBox/TFTP
    cd /usr/share/syslinux/
    cp pxelinux.0 ldlinux.c32 libcom32.c32 libutil.c32 
      vesamenu.c32 ~/.config/VirtualBox/TFTP/
  • create a directory “pxelinux,cfg

    mkdir ~/.config/VirtualBox/TFTP/pxelinux.cfg
  • create a SystLinux menu/boot configuration (eventually use the isolinux.cfg as an example:

    $ vi ~/.config/VirtualBox/TFTP/pxelinux.cfg/default
  • When you are installing any Enterprise Linux version, you can add a Kickstart configuration to load (make it the default for handsfree installation).
  • Copy the kernel and initrd, e.g. from the mounted installation media:

    $ cp ~/Downloads/vmlinuz ~/Downloads/initrd.img ~/.config/VirtualBox/TFTP/
To configure a virtual machine “testvm” to be able to PXE-boot:
cd ~/.config/VirtualBox/TFTP/
ln -s pxelinux.0 ${MyVM}.pxe
When your VM is not configured to boot from the network, change the NIC and boot order to have the network boot as the first option using the NAT interface to boot! E.g.:
vboxmanage modifyvm ${MyVM} --nic1 NAT --nictype1 virtio
vboxmanage modifyvm ${MyVM} --boot1 net --boot2 disk --boot3 none --boot4 none
If needed, configure the VirtualBox DHCP server (this should already be the default configured, so this should not be needed). Eventually you can change the PXE boot file (e.g. to pxelinux.0 instead of using the symbolic link above):
$ vboxmanage modifyvm ${MyVM} --nattftpfile1 ${MyVM}.pxe

Complete the process

When your are done configuring, you can launch the virtual machine. First use the normal start to see if it loads the boot files and will show your menu:
vboxmanage startvm ${MyVM}
As soon as the menu is displayed, you can e.g. start a Kickstart installation you have configured in there. If you added a boot splash that should now show up with the lines showing it is loading the kernel and initrd printed on top of your image at the bottom left:
When you are completely satisfied and everything installs without manual intervention, you should change your menu to automatically select the Kickstart without delay and then you can start the machine “headless”:
vboxheadless --startvm ${MyVM}
Note: The PXE boot seems to be very slow depending on the version of VirtualBox. The time needed for the installation itself mostly depends on your network speed and the mirror selected.  You can try to select a faster mirror yourself if needed (change the parameter
in your Kickstart configuration accordingly).
For more details see: