Create a new VirtualBox from command line

I am running my virtual machine host on a headless Ubuntu server. This means that all of my interacting with VirtualBox is done through the command line over SSH. Here is the commands that I use to create a new virtual machine. These command examples are specific to VirtualBox 3.1 and later. Any bold text in the example commands is something that you should probably change to be more specific to your own setup and requirements.

The first step is to create the new virtual machine and register it with VirtualBox:

VBoxManage createvm --name MyVM --ostype Linux --register

Now we need to define the virtual machines configuration, including memory and network interfaces. I prefer bridged networking so that the virtual machine feels like it’s part of the family or network or whatever. Depending on your desired setup, you might want to setup the network interfaces in either NAT or Host Only, Google is your friend. Here’s the commands to I use for 512MB memory and bridged networking:

VBoxManage modifyvm MyVM --memory 512 --acpi on --pae on --hwvirtex on
VBoxManage modifyvm MyVM --nic1 bridged
VBoxManage modifyvm MyVM --bridgeadapter1 eth1

Since every machine, even a virtual one, needs a hard drive we need to create the hard drive controller and then define a hard drive:

VBoxManage storagectl MyVM --name "IDE0" --add IDE
VBoxManage createhd --filename harddisk1.vdi --size 30000 --variant Standard
VBoxManage storageattach MyVM --storagectl "IDE0"
      --port 0 --device 0
      --type hdd --medium harddisk1.vdi

Despite discussions I have read about hard drive controllers, the virtual IDE controllers work better on my system than the SATA ones. YMMV. The port and device arguments define the port used on the virtual HDD controller. As we are using IDE each port will have a master and a slave device just as a physical IDE controller has. In my case I set up the hard drive as primary master, and the CD/DVD drive as secondary master. Since we are installing a system from scratch using a supplied installation ISO, the next step is to set up and attach a virtual CD/DVD drive and load the ISO image:

VBoxManage storageattach MyVM --storagectl "IDE0"
      --port 1 --device 0
      --type dvddrive --medium /path/to/cdimage.iso

So that the virtual machine can boot from the installation media we need to define the boot order as follows:

VBoxManage modifyvm MyVM --boot1 dvd --boot2 disk

Next we set up VRDP so that we are able to RDP in to the virtual machine, you can use any available port but you will have to remember to pass the same port to your RDP client:

VBoxManage modifyvm MyVM --vrdpport 33891

Finally it’s time to launch the virtual machine:

VBoxHeadless -startvm MyVM &

We are now able to connect to the virtual machine using our RDP client, such as Windows Remote Desktop, and work our way through the installation. Once installation is complete you will need to power off the virtual machine, remove or unregister the ISO image and alter the boot order so that the virtual machine boots from the virtual hard drive instead of the virtual CD/DVD drive:

VBoxManage controlvm MyVM poweroff
VBoxManage storageattach MyVM --storagectl "IDE0"
      --port 1 --device 0
      --medium none
VBoxManage modifyvm MyVM --boot1 disk --boot2 none

We are then able to start the virtual machine again using the VBoxHeadless command from before.

If at a later stage you no longer need the virtual machine you can unregister it and erase it’s associated disk image by running the following commands:

VBoxManage modifyvm MyVM --boot1 none
VBoxManage unregistervm --delete MyVM
VBoxManage unregisterimage disk "harddisk1.vdi"

Leave a comment

Your email address will not be published.