01 June 2011

Virtualisation with VirtualBox

Sometimes you just want to try something on a different operating system. I used to use Microsoft’s Virtual Server and latterly VirtualPC on Windows and Xenserver is an excellent host but some time ago I wanted to use something that I could have more granular control over and do things like have it run as a service in the background or shutdown virtual clients to backup etc. I looked at a few options but I’ve been using Sun’s VirtualBox now for over a year and I really like it.

The advantages of VirtualBox for me are:
  • Supports Open Source standards virtual hard disk (VMDK)
  • Import/export machines in open OPL format
  • Able to run as a service
  • Lightweight: when it’s not running it isn’t lurking in the background hogging resources
During my time with it I’ve kept some notes on various bits which weren’t so clear to me when attempting for the first time so I’m posting them here as much for myself as everyone else. Happy virtualising!

Notes on VirtualBox
Client: Virtual machine running inside VirtualBox
Host: Computer running VirtualBox
VB: VirtualBox

VM: Virtual Machine

LAN Access/Remote Desktop

Default Network adapter is VirtualBox internal/NAT, for a server (e.g. for IIS) you need a bridged network.
By default machines are setup to be connected to a special 'VirtualBox network'. To connect remotely enable remote connections in the client machine’s OS then connect using Remote Desktop using the HOST’s IP with the assigned port number. It defaults to 3386 but on windows host you probably need 5000-5050. So for VB network AND LAN access a client should have two network adapters e.g.:
Intel PRO/1000 MT Desktop (Host only) - for connection via [host ip].1:[port]
Intel PRO/1000 MT Desktop (Bridged adapter, Intel(R) PRO/1000 MT Server Connection) - for outside world connections
Also enable Display/Remote Display as port e.g. 5000
NB: even when LAN enabled I leave the VB network as sometimes I have found it can’t connect to the LAN, so you can still get in on the VB one.

When Installing a Machine Created Using VirtualServerCheck the disk drive type: VirtualBox uses SATA by default but VirtualMachine might be setup as IDE: change the type corresponding to the original server configuration.
Creating VMDK disk (Open source VHD)
NB:  immutable = differencing
  1. Create parent differencing disk (20GB):
    Use following commands at command prompt:
    C:\Program Files\Oracle\VirtualBox\VBoxManage.exe createhd --filename "[VHD path and filename].vmdk" --size 20480 --format VMDK --variant standard --remember
  2. Install OS, apply any updates, defrag drive, zero space, stop VM
  3. If using differencing disk: Create a 'child' differencing disk by setting the parent as immutable, VB will auto create a child:
    vboxmanage modifyhd "[VHD path and filename].vmdk" --type immutable
  4. Set parent hard disk file as readonly just to be sure.
  5. Start up your VM and VB will auto create a differencing disk.
  6. If you need to move the child, it's created by default in
    C:\Users\[Username]\.VirtualBox\Machines\[Machine name]\Snapshots\{[GUID]}.vmdk
    In order to move it:
    1. Close VirtualBox
    2. Edit two files (create copies as backups):
      C:\Users\[Username]\.VirtualBox\VirtualBox.xml and search for the GUID (do this first so you have the GUID)
      C:\Users\[Username]\.VirtualBox\[Machine Name]\[Machine Name].xml
      • In VirtualBox.xml look for <MediaRegistry>
        then the uuid(s) you’re interested in and alter their path accordingly.
      • In [Machine Name].xml look for the <Machine tag property stateFile which will be a path to the ‘snapshot’ aka child disk. Alter the path accordingly.
    3. Move the VMDK file(s) and rename it/them.
    4. Restart VirtualBox
Notes on Creating Virtual Disk Drives

To be cross compatible with VirtualPC you will have to use VHD drives.
Also bear in mind that as of writing the maximum size for VirtualPC is 127GB so keep them to 126GB to be cross compatible

Running VirtualBox Machine as a Service
This had me foxed for a bit but I found a handy tutorial at:

So to summarise:
  1. Download:
  2. Install service commander
  3. Setup an ini file for the service you’re going to create:
    1. Copy [user]\.VirtualBox\VirtualBox.xml to Users\Default\VirtualBox (as there is no LocalService folder in Win7)
    2. Replace paths to virtual hosts in the file with absolute ones (Run Wordpad as administrator first to edit the file). ie prefix the src with the full path in <MachineEntry uuid="{[GUID]}" src="Machines\[Machine Name]\…
    3. Create folder c:\vm and extract srvstart_run.zip to it
    4. Create c:\vm\srvstart.ini with the following contents (I have shutdown as savestate (hibernate) rather than shutdown for Windows clients as I found the shutdown proper didn’t work reliably):
      env=VBOXGUI="C:\Program Files\Oracle\VirtualBox\virtualbox.exe"
      env=VBOXHEADLESS="C:\Program Files\Oracle\VirtualBox\vboxheadless.exe"
      env=VBOXWEBSRV="C:\Program Files\Oracle\VirtualBox\vboxwebsrv.exe"
      env=VBOXMANAGE="C:\Program Files\Oracle\VirtualBox\VBoxManage.exe"
      env=VBOX_PROG="C:\Program Files\Oracle\VirtualBox\"
      env=VBOX_VDI="F:\Virtual Machines\"

      [Machine name in square brackets]
      startup=%VBOXHEADLESS% -startvm [Machine name]
      shutdown=%VBOXMANAGE% controlvm [Machine name] savestate
      debug_out=>C:\vm\[Machine Name].log

      [Second Machine name in square brackets]
      startup=%VBOXHEADLESS% -startvm [Second Machine name]
      shutdown=%VBOXMANAGE% controlvm [Second Machine name] savestate
      debug_out=>C:\vm\[Second Machine Name].log
  4. Install the Service:
    1. Run service commander as admin: C:\Program Files (x86)\Null Logics Software\Windows Service Commander\wscommander.exe
    2. Select Tools/Install new service
    3. Enter:
      c:\VM\srvstart.exe svc [Machine name] -c "c:\vm\srvstart.ini"
      (The machine name corresponds to the server name in srvstart.ini)
    4. Name it as VirtualMachine [Machine name]
    5. Set as Local System Account (so it will run whether a user is logged on or not) and Enable interaction with Desktop.
    6. Set to Startup Auto; Error: Normal
    7. Repeat for other virtual machines e.g. Second Machine name
  5. Start service and check you can remote in or server is running.

check the VirtualBox.xml file; the syntax of ini file; the command entered when installing the service; and the log file at C:\vm\[Machine Name].log

If like me you have had trouble getting the srvstart.exe to read the VM’s from a different user’s .VirtualBox directory then you could use a symblic link to get around the problem. It’s not a fix, but a workaround. Using cmd prompt as admin:
>mklink /D [Link e.g. C:\Users\[olduser]\.VirtualBox] [Target e.g. C:\Users\[new user]\.VirtualBox]


To run a VM headless i.e. with no window use the command:
VBoxHeadless --startvm [VM name]
You can create a .bat file to do this on Windows or a .command file on Mac:
open -a "/Applications/VirtualBox.app/Contents/MacOS/VBoxHeadless" --args -startvm=[VM name] &
(Don't forget to change permissions to allow it to run: chmod +x [filename].command
To shut it down from command:
VBoxManage controlvm [VM name] acpipowerbutton
VBoxManage controlvm [VM name] savestate
VBoxManage controlvm [VM name] poweroff

Shared Folders
  • Ensure client is shutdown and off.
  • Change settings in VirtualBox to add shared folder with name
  • Boot up VM and ensure VB additions installed
  • Add share via Dos cmd:
    net use x: \\vboxsvr\[share name defined above]
    where x is the drive letter to assign

1 comment:

  1. A key advantage of a virtual drive is that it usually saves time. A virtual CD drive, for example, can be read much faster than a physical CD drive because it exists on the hard drive.