Using VirtualBox as a Cloud Computing Server

Sandeep Kattepogu
6 min readDec 1, 2020

For when you really just don’t want to pay for vSphere

Outline

  • Introduction
  • Requirements
  • Part 0: Installing VirtualBox and Extension Pack on the Command Line
  • Part 1: Creating and Deleting a VirtualBox VM using “VBoxManage”
  • Part 2: Enabling RDP access for a VirtualBox VM
  • Part 3: Additional Considerations

Introduction

When I was pursuing my undergraduate degree in IT, one of my favorite classes taught me about how cloud computing services worked behind the scenes. The best part was the group project I spent far too much time on. The class was split up into teams and given computers and networking equipment. Each team had to use these to create an AWS-like service on the local network to allow the creation and use of virtual machines over the network.

We had to make THIS with a Dell Optiplex and old Cisco networking equipment…

Because I didn’t want to be a burden to the folks on my team, I decided to start the project a little early and found out that Oracle’s VM VirtualBox had all the features we needed in the backend to our virtualization server: the automated creation of VMs with scripting, easy networking of virtual machines to allow SSH connections, and the ability to display VMs over the network using RDP.

This became my entire life for a while…

In the following weeks, I created a prototype to show my team including a simple web interface. From then on, I became the main programming/web design guy and wasted hours and hours on features such as: web authentication, copying VMs, getting SSH to work with port forwarding, automating Windows 10 installation, and many more ideas (and dead ends). However, I learned so much about programming, scripting, and Oracle VM VirtualBox that I treasure the experience to this day. It is this experience that I wish to discuss today.

In this blog/tutorial, we will go over the commands needed to automate the creation of a VirtualBox VM using basic shell scripting.

Requirements

A non-virtual computer with, ideally,:

  • Several cores
  • More than 8 GB of memory
  • Plenty of hard drive space
  • Ubuntu 16 or later installed
  • VirtualBox installed with the appropriate version Extension Pack

Part 0: Installing VirtualBox and Extension Pack on the Command Line

If you don’t have VirtualBox installed, here is how to do it on the command line in Ubuntu/any distro that use the apt package manager.

sudo apt-get update -ysudo apt-get install virtualbox -yvboxver=`vboxmanage --version | awk -F '_' '{print $1}'`sudo curl https://download.virtualbox.org/virtualbox/$vboxver/Oracle_VM_VirtualBox_Extension_Pack-$vboxver.vbox-extpack > Oracle_VM_VirtualBox_Extension_Pack-$vboxver.vbox-extpack                       echo "y" | sudo vboxmanage extpack install Oracle_VM_VirtualBox_Extension_Pack-$vboxver.vbox-extpack                       sudo vboxmanage extpack uninstall VNC

We first update then install VirtualBox which is in the basic Ubuntu repositories. Then we download the right version of the Extension Pack and install it, automating the license agreement. Then, we uninstall an extension that will cause issues in allowing RDP access later unless it is removed.

Part 1: Creating and Deleting a VirtualBox VM using “VBoxManage”

There are many parts to creating a virtual machine and much more than is here can be learned at the official Oracle VM VirtualBox manual, particularly Chapter 8.

  • The first step is creating an XML virtual machine definition file.
VBoxManage createvm --basefolder "{absolute path}" --name VM_NAME --uuid=`uuidgen` --register

This command will create a new directory called “VM_NAME” with the chosen basefolder used as the parent and will create the “*.vbox” settings file. Remember to use an absolute path for the “basefolder” argument or else VirtualBox might use a folder in the ~/.config directory.

  • The second step is setting a few basic options on the VM such as CPU, RAM, VRAM, and setting time to UTC.
VBoxManage modifyvm VM_NAME --cpus 1 --memory 1024 --vram 16 --acpi on --ioapic on --pae off --x2apic on --rtcuseutc on

The few settings set here can be changed easily and chained into the same command. View more settings you may want to set here at Chapter 8 of the manual.

  • The third step is to create a virtual hard drive and storage contoller and then attach the storage to the VM.
VBoxManage createhd --filename "{absolute path}/VM_NAME.vdi" --size 10000 --variant StandardVBoxManage storagectl VM_NAME --name "SATA Controller" --add sata --bootable onVBoxManage storageattach VM_NAME --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "{absolute path}/VM_NAME.vdi"

The absolute path can be anywhere where you want the the virtual hard drives to be stored. Additionally, the size here of 10 GB (10000 MB) is customizable. Learn more here with the “createmedium” command which “createhd” internally maps to.

  • The fourth step, techincally optional, is to add an IDE controller to the virtual machine and attach an ISO file to be booted at startup.
VBoxManage storagectl VM_NAME --name "IDE Controller" --add ideVBoxManage storageattach VM_NAME --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium "{absolute path}/operatingsystem.iso"
  • Finally, you can start the VM.
VBoxManage startvm VM_NAME

The image below shows a black screen, but this is because I am running my server inside a VM and it showed me an error. I blacked out the error by hand, but it will definitely work for you.

  • To delete the VM, power it off, unregister, and delete all its files.
VBoxManage controlvm VM_NAME poweroff
VBoxManage unregistervm VM_NAME --delete
rm -rf "{absolute path}/VM_NAME"

Part 2: Enabling RDP access for a VirtualBox VM

To create a RDP-ready VM, start by following Part 1 but DO NOT power on or delete the VM.

NOTE: Remember to disable firewall rules that block access to the port through which you wish to RDP to your virtual machine.

  • With the VM created and ready to be powered on, modify the VM settings to to enable RDP access.
VBoxManage modifyvm VM_NAME --vrde on --vrdeport $3 --vrdeaddress {IP address}
  • Now start the VM headless (without a window).
VBoxManage startvm VM_NAME --type headless
  • Now, from a different computer on the same network, attempt an RDP connection with software like Remmina Remote Desktop Client. Again, I blacked out an error on the image below, but as you can see, the RDP server is working and shows what you would see on a booted VM.

Part 3: Additional Considerations

Some firewall rules will need to be created to allow RDP connections and security may become an issue for you. Additionally, SSH security will need to be taken into account if you plan on installing an SSH server on any of your VMs (this will require port forwarding on the host machine to work and is easy to set up using “VBoxManage”, check here at Chapter 7). Finally, you may want a web interface to pull everything together and allow easy access to people on the network. This can be done with software like phpvirtualbox (https://github.com/phpvirtualbox/phpvirtualbox) or, with my very own creation, virtualflask (https://github.com/skatt621/virtualflask). Virtualflask requires nothing except an Ubuntu 16+ machine to be set up and comes with an easy setup script and easily editable web pages and code. Development is still active and the “Create New VM from Template” feature still needs testing.

--

--