Thursday, July 8, 2010

How to Run a Headless Virtual Machine

When running a virtual machine (using VirtualBox) on top of your primary system sometimes you might wish for the window to just go away. In the event that you want to log into the machine you could just use Remote Desktop instead of having a window open at all times. I searched the web looking for bits and pieces of information on how to "close VirtualBox but keep [the] machine running." Eventually I came up with a solution that I thought was acceptable.

My solution ended up being a special VB script. One that could even be launched on startup via a simple shortcut. Before you create your script you may want some background on starting VirtualBox in headless mode. VBoxHeadless.exe is a command line application that allows you to start a virtual machine that you have already created without a graphical interface. In a default installation VBoxHeadless.exe is located in "C:\Program Files\Oracle\VirtualBox\". Below I have shown you the proper usage for version 3.2.2. You may also download this information from my box.net account.


Oracle VM VirtualBox Headless Interface 3.2.2
(C) 2008-2010 Oracle Corporation
All rights reserved.

Usage:
   -s, -startvm, --startvm [name|uuid]   Start given VM (required argument)
   -v, -vrdp, --vrdp on|off|config       Enable (default) or disable the VRDP
                                         server or don't change the setting
   -p, -vrdpport, --vrdpport [ports]     Comma-separated list of ports the VRDP
                                         server can bind to. Use a dash between
                                         two port numbers to specify a range
   -a, -vrdpaddress, --vrdpaddress [ip]  Interface IP the VRDP will bind to
   -c, -capture, --capture               Record the VM screen output to a file
   -w, --width                           Frame width when recording
   -h, --height                          Frame height when recording
   -r, --bitrate                         Recording bit rate when recording
   -f, --filename                        File name when recording.  The codec
                                         used will be chosen based on the
                                         file extension


So in my case I am launching a virtual machine I have named "Debain", without quotes of course. My command would be:

C:\Progra~1\Oracle\VirtualBox\VBoxHeadless.exe -s Debian

Now if I were to launch that from a regular "Run" box in windows it would show me a black DOS-like box. If your goal is to get rid of that black box then you should continue. Still reading? Glad to hear it. Below I have copied some code from a Yahoo Answers post.

'=====================================
Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("H:\test.bat", 0)
set WshShell = Nothing
'=====================================

Copy that code into a blank notepad document and save it as "HIDECMDWINDOW.vbs". The file extension must be ".VBS". Replace the "H:\test.bat" with your command to launch "VBoxHeadless.exe". My code looks like this:

'=====================================
Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("C:\Progra~1\Oracle\VirtualBox\VBoxHeadless.exe -s Debian", 0)
set WshShell = Nothing
'=====================================

When you open the VB script, VBoxHeadless.exe will open in the background (you will not receive feedback). Your computer may ask you to allow port 3389 to open. If it does this is OK, because it is your remote desktop port.

Now if you want to log into the virtual machine using remote desktop all you have to do is open Remote Desktop Connection and type in local host as your server. (127.0.0.1 or localhost)

If you have any questions or comments please use the comment feature.

Sources/useful links:
http://johnmee.com/2008/10/start-headless-vbox-on-windows/
http://answers.yahoo.com/question/index?qid=20071011212557AAofTy6

3 comments:

  1. Thanks, that was extremely helpful.

    ReplyDelete
  2. Thanks. This just helped me.

    ReplyDelete
  3. Thanks for this tip. However, it only works if your VM name has no spaces. To fix this, a small change is necessary:
    '=====================================
    Set WshShell = WScript.CreateObject("WScript.Shell")
    obj = WshShell.Run("C:\Progra~1\Oracle\VirtualBox\VBoxHeadless.exe -s " & chr(34) & "VM Name Here" & chr(34), 0)
    'set WshShell = Nothing
    '=====================================

    The only change is that I added chr(34) (double quote) to either side of the VM name.

    ReplyDelete