Creating PXE bootable Virtualbox VMs

February 24, 2021 – Samuli Seppänen

We use Vagrant and Virtualbox heavily when developing Puppet code. We also do provisioning of baremetal servers with Foreman. While Vagrant is a really powerful tool, it is not designed for creating VMs that boot from the network (PXE boot). Fortunately Virtualbox itself provides a rich command-line with which you can create and configure virtual machines and configure them to boot from the network. It is assumed below that there is a PXE boot server (e.g. Foreman) created into a hostonly network with Vagrant. Here's an example script that create a VM with vboxmanage and adds it the same hostonly network interface (vboxnet15) as the PXE boot server:

vboxmanage createvm --name "pxetest" --ostype RedHat_64 --register
vboxmanage modifyvm pxetest --description "PXEboot test (CentOS 7)" --memory 2048 --cpus 1 --boot1 net --nic1 hostonly --hostonlyadapter1 vboxnet15 --macaddress1 080027fbad17
vboxmanage createmedium disk --filename pxeboot-disk.vdi --size 8192
vboxmanage storagectl pxetest --name "SATA" --add sata --bootable on
vboxmanage storageattach pxetest --storagectl "SATA" --port 1 --type hdd --medium pxeboot-disk.vdi
vboxmanage startvm pxetest

Using the same hostonly network allows the two machine, PXE boot server and PXE boot client, to talk to each other.

To tear down the PXE boot client VM:

vboxmanage controlvm pxetest poweroff
sleep 2
vboxmanage unregistervm pxetest --delete
sleep 2
vboxmanage closemedium pxeboot-disk.vdi --delete

The "sleep" command seem necessary because the vboxmanage commands exit before they have finished.

Want to talk to an expert?

If you want to reach us, just send us a message or book a free call!