VirtualBox Headless Administration
Server - Virtualization

VirtualBox is often used on the desktop through it's GUI interface. Sometimes, though, you'll need to run it on a headless server somewhere on the internet. Our demonstration setup today is on a Debian 7 (Wheezy) system, and the virtual machine we're creating will use a placeholder of $VM for it's name. We'll assume the guest OS is Debian, and that you've already installed VirtualBox itself to the host.

CentOS Online Course < Start Learning CentOS Now $120

VirtualBox has one of the best manuals, even if it is a hundred pages long. Chapter 8 is of particular interest to us. You can find the manual here: http://www.virtualbox.org/manual/ Installation instructions can be found here: https://www.virtualbox.org/wiki/Linux_Downloads We'll need the VirtualBox Extension Pack to be able to use RDP to initially set up the guest OS. Download it from https://www.virtualbox.org/wiki/Downloads, and install it like so. Root is required, however this only needs to be done once on the host.

sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.2*.vbox-extpack

To make it easy to copy & paste these commands, set $VM to the name of the guest virtual machine. If you are using the bash shell, it goes like this:

VM=spaceball

We'll start with creating a virtual machine. If you are running on a 32 bit host, drop the “_64” part. The setting for “ostypes” doesn't really matter, however it does set a few sane defaults, so it's best to use one if at all possible.

VBoxManage createvm --name $VM --ostype "Debian_64" --register

If you aren't using Debian, you can run this to get a list of OS types:

VBoxManage list ostypes

Next we need to give it a hard drive. Make sure to adjust the argument to --size as needed. In this example, it is set to 10GB. The first line creates the file that is the drive itself, the second line adds a SATA controller to the VM, and the last line plugs the hard drive in to the controller. You can add additional drives by incrementing the argument to --port.

VBoxManage createhd --filename $VM.vdi --size 10240 VBoxManage storagectl $VM --name "SATA Controller" --add sata --controller IntelAHCI VBoxManage storageattach $VM --storagectl "SATA Controller" --type hdd --medium $VM.vdi --port 0

We'll need to boot from an ISO image to be able to install our guest operating system, so lets add a DVD drive and attach an image to it. VirtualBox does not support SATA DVD drives, so we make an IDE controller to attach it to.

VBoxManage storagectl $VM --name "IDE Controller" --add ide VBoxManage storageattach $VM --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive  --medium debian-7.1.0-amd64-netinst.iso Now we need to tell it the boot order. After this, you can go ahead and start the VM if you really want to, but it is a bad idea, as it hasn't been tuned to run well yet, and we haven't set up networking. VBoxManage modifyvm $VM --boot1 dvd --boot2 disk --boot3 none --boot4 none

While at this point it is now possible to start it up, it's best to tune it a bit so it makes proper use of the host hardware. For a full explanation of these options, please see the VirtualBox manual.

VBoxManage modifyvm $VM --ioapic on --hwvirtex on --nestedpaging on --hwvirtexexcl on --pagefusion on --largepages on --acpi on

Now to set up networking. For this example, we'll assume that you want a VM that can function as a full fledged server on the Internet. This means we want bridged networking. The exact details of this will change based on your host OS, for example under FreeBSD you'll be using tun/tap interfaces.

VBoxManage modifyvm $VM --nic1 bridged --bridgeadapter1 eth0

We should probably give it more memory than the default. For the OS type of “Debian_64” it defaults to 384MB, so it would be a good idea to change it. Here we set it to 4GB.

VBoxManage modifyvm $VM --memory 4096

If your host has multiple CPU cores, we can take advantage of that. We can also tell VirtualBox to not use more than some percent of CPU time. This is a good idea on shared hosts. It defaults to 100%, so without setting it, VirtualBox will gladly allow a guest VM to use all the cycles it can.

VBoxManage modifyvm $VM --cpus 4 VBoxManage modifyvm $VM --cpuexecutioncap 80

Lastly, we should change the RDP port. Later, once the VM is pushed in to production, we will want to disable RDP entirely for security.

VBoxManage modifyvm $VM --vrdeport 12345

At this point we are now done setting up the new virtual machine. Time to start it up!

VBoxHeadless --startvm $VM

Connect to it with your favorite Remote Desktop viewer, and install however you want.

VirtualBox Headless Administration

We're done setting up a new virtual machine. There's a few common tasks left to go over, such as safely shutting down, suspending, resetting and cloning. Do not ctrl-C the VirtualBox process, run these from a different terminal. Here they are, in order.

VBoxManage controlvm $VM acpipowerbutton VBoxManage controlvm $VM savestate VBoxManage controlvm $VM reset VboxManage clonevm $VM --name “New VM Name” --register

If your VM locks up, you can “pull the plug” with this command (use this as a last resort):

VBoxManage controlvm $VM poweroff

Once your VM farm starts to grow a bit, you might need a refresher as to what is what. The first command lists all registered VMs, and the second command shows the configuration of the named VM.

VBoxManage list vms VBoxManage showvminfo $VM

Article written by Michael "mngrif" Griffith