Sunday, November 28, 2010

MACOSX - Pausing your VirtualBox instance (Creating an app)

Synopsis
In this article I will demonstrate how to automate placing an application, namely a VirtualBox Instance, in "suspend" mode to reduce system load when not in use.

Before I begin, yes I know some of you are thinking: "Umm why not just use the VirtualBox pause feature".   Well I wrote this app only because I am too lazy to open the VirtualBox app to pause my machine.  I kind of like having a simple one click app on my dock.  Besides this was my first app and I wanted to give it a try.

Now that we have that out of the way, lets begin.

Overview
I need to run Windows apps on my Mac frequently but do not want to boot into bootcamp mode into Windows.  For such a situation I use Oracle VirtualBox (AKA SUN VirtualBox) to run a virtual instance of Windows under OSX.
I like having this virtual instance constantly running in the background for quick access when ever I need to run my Windows apps.  Unfortunately this does come with a price.  Not only does it utilize physical memory but the virtual OS will use CPU cycles, hence may slow your Mac down a bit.  Having plenty of memory, my only concern is CPU time. 
Typically users would just pause or "save state" for the virtual instance, but if you are impatient like me and don't want to have to deal with the time needed to save the state and come out of a saved state then this solution may be for you.
Being an advanced UNIX user I know that one can pause or suspend a process using the kill command. 
MacOSX has a similar kill functionality known as "kill -SIGSTOP" and "kill -SITCONT".  The trick now is to attempt to automate this into an "Automator" app so that you don't have to type this in a command window every time.
NOTE: I'm sure their are more elegant methods of achieving this with AppleScript, but I wanted to create something quick.

Steps

  • Open the "Oracle VM VirtualBox" app and retrieve the name of the Virtual instance you want to create the app for.
  • Download the "PauseVirtualBox.zip", uncompress and save to a desired location.  I chose my Application Folder.
  • Now open the "Automator App"   . (See icon to left).
  • Ignore the "Choose Template" dialog window and proceed to next step.
  • In the Automator "File" menu option, select "Open" and navigate to the location where you saved the "PauseVirtualBox" app.
  • Modify the "VM=" variable, indicated by the red rectangle, in the screenshot below.  The new value should be the name you retrieved from the Virtual instance in the first step.  In my example it is called "Windows 7".
  • Save the app.
  • Now launch the VirtualBox instance.  
  • Once the virtualbox instance has stabilized, launch the "PauseVirtualBox" app.  If you receive an error then you either do not have the Virtual instance name correct or the virtual instance is not started.
  • If no errors occur you should see a message stating that the instance was either paused or resumed.
  • At this point, if you have paused the instance, you will notice that the VirtualBox instance now appears to be "Hung" or you cannot restore it from the dock.  Have no fear. 
  • To resume the instance, click the PauseVirtualBox app again.   When your done with the instance, just pause again.
  • REMINDER: Before you reboot you Mac you will need to "Unpause" the virtualbox instance and properly shutdown or save the state.