Archive for July, 2009

Running a virtual machine in virtualbox 3.0 from the command line

Posted in Linux, Ubuntu, VirtualBox on July 12th, 2009 by alex – Be the first to comment

VirtualBox has a great user interface but sometimes it’s just not necessary, especially if you want to create/start/stop virtual machines remotely or if you are just running a server without a windowing system.
Luckily VBox allows you to do everything from the command line interface (cli) that you can do from the user interface and it also allows you to remote desktop connect to the virtual machine.
In this post I’ll show you how to install VirtualBox on an ubuntu linux server, then set up and run a virtual machine from the cli.

Some terms in this post:
host – machine on which VirtualBox runs, your “real” server
guest – virtual machine running in VirtualBox, your virtual server

  1. Install VirtualBox:
  2. Create the virtual machine and register it with VBox. The name of the vm is vbuntu.
    user@host:~$ VBoxManage createvm -name vbuntu -register
  3. Change the boot adapter to the virtual dvd drive so we can boot the OS install disk.
    user@host:~$ VBoxManage modifyvm vbuntu --boot1 dvd
  4. Create and register the virtual disk image, basically the virtual disk from which the vm runs. Give it a filename and a size in MB, here my virtual disk image is name vbuntu.vdi and it has 30,000 MB.
    user@host:~$ VBoxManage createvdi -filename "vbuntu.vdi" -size 30000 -register
  5. Link the vdi to the vm, here hda is the hdd off which the vm boots.
    user@host:~$ VBoxManage modifyvm vbuntu -hda vbuntu.vdi
  6. Register the install disk image of ubuntu server with VBox. Then mount the install disk on your vm.
    user@host:~$ VBoxManage registerimage dvd /path/to/ubuntu-9.04-server-amd64.iso
    user@host:~$ VBoxManage modifyvm vbuntu -dvd /path/to/ubuntu-9.04-server-amd64.iso
  7. Modify the default options for the vm. I gave the vm 1GB of ram, turned on ACPI for power control, turned on PAE (ubuntu server requires it, it enabled 32bit hosts to use more than 4GB of ram) and I also turned on Enable VT-x/AMD-V which are the technologies Intel and AMD processors use for hardware virtualization extensions. To use these extensions your host must have a processor supporting them and then enable them from the BIOS.
    user@host:~$ VBoxManage modifyvm vbuntu --memory 1024MB --acpi on --pae on --hwvirtex on
  8. For networking I’m using bridged networking. First set the vm nic to bridged mode, then bridge it with one of the host’s nic. Here I bridged it over my host’s eth1 nic.
    user@host:~$ VBoxManage modifyvm vbuntu --nic1 bridged
    user@host:~$ VBoxManage modifyvm vbuntu --bridgeadapter1 eth1
  9. I also changed the port for the remote desktop protocol to 33891 because I’m running multiple vm’s, so each vm has its own port.
    user@host:~$ VBoxManage modifyvm vbuntu --vrdpport 33891
  10. Start the vm in the background, so remember to add the ampersand at the end of the command. After that you can RDP into the vm by connecting to the IP of your host system on the above set port. Now you can install the guest OS.
    user@host:~$ VBoxHeadless -startvm vbuntu &
  11. To shut off the vm use this command.
    user@host:~$ VBoxManage controlvm vbuntu poweroff
  12. Once done with the install, you can unregister the install disk and have the vm boot from its own hdd.
    user@host:~$ VBoxManage modifyvm vbuntu -dvd none
    user@host:~$ VBoxManage unregisterimage dvd /path/to/ubuntu-9.04-server-amd64.iso
    user@host:~$ VBoxManage modifyvm vbuntu -boot1 hda
  13. Optionally here are the steps to get rid of the vm and its disk image if you don’t need it anymore.
    user@host:~$ VBoxManage modifyvm vbuntu --hda none
    user@host:~$ VBoxManage unregistervm --delete vbuntu
    user@host:~$ VBoxManage unregisterimage disk "vbuntu.vdi"

Let me know if you have any suggestions or comments as to the process I just described.

Share