VirtualBox Script to Batch Save and Start Multiple Guests

I use Windows 7 as my laptop host OS and this limits me from using Hyper-V natively to handle VM guests.   Yes, I could do the dual-boot thing, but I haven’t done it yet and I do like the simplicity of not having to switch host OS contexts.  Windows 8 will, thankfully, be adding Hyper-V hosting support to the client, but in the meantime I’ve been using VirtualBox for my guest OS needs.

Jonathan Kehayias blogged about it extensively in his “Building a Completely Free Playground” series – and once I moved to SQLskills I felt less skittish about running a product that happened to have the big “O” word listed prominently on the application toolbar header.

I actually use VirtualBox for a variety of purposes – not just for SQL Server test environments. For example, if I need to connect to an environment that has conflicting (or unique) remote connectivity requirements, rather than risk clobbering my host VPN settings, I provision a clean, isolated guest environment that I use for connectivity and configuration instead.

Sometimes I’ll have a group of servers that I want to launch at the same time or I’ll want to put a group of them in a saved state at the same time (for example – when running a guest Failover Cluster).  In one case I have six different guests that I want to save or start at (nearly) the same time and I don’t like having to right-click save on each one – especially since my guests will quickly think something is wrong and when I bring them back from a saved state, it takes a few seconds to get them de-frazzled.

By the way – I’m using these scripts for test environments.  These are test environments that if I lost them, I’d be annoyed – but not devastated. 

Rather than rely on the GUI, I created a BAT file to save the guests in a batch:

cd /D "C:\Program Files\Oracle\VirtualBox\"

START /b VBoxManage.exe  controlvm "Node4_D2"  savestate

START /b VBoxManage.exe  controlvm "Node3_D2"  savestate

START /b VBoxManage.exe  controlvm "Node2_D1"  savestate

START /b VBoxManage.exe  controlvm "Node1_D1"  savestate

START /b VBoxManage.exe  controlvm "SAN_D1" savestate

START /b VBoxManage.exe  controlvm "Node_DC"  savestate

I use the START option so that I’m not waiting for the save operation in a serial fashion.  I use the “/b” so that six windows don’t pop up.  As for the Virtualbox command syntax – it’s pretty straightforward – I call VBoxManage.exe with controlvm followed by the name of the VM and the option to save it (savestate).

As for starting them up from the saved state, I do stagger things a bit.  I use startvm instead of controlvm, followed by the VM name and I don’t use the START keyword for anything I want to ensure is executed in order (I also use the TIMEOUT option to wait a few seconds before launching other VMs):

cd /D "C:\Program Files\Oracle\VirtualBox\"

VBoxManage.exe startvm "Node_DC" 

VBoxManage.exe startvm "SAN_D1"


START /b VBoxManage.exe startvm "Node1_D1" 

START /b VBoxManage.exe startvm "Node2_D1" 

START /b VBoxManage.exe startvm "Node3_D2" 

START /b VBoxManage.exe startvm "Node4_D2"

Even with these scripts I wouldn’t call this an orderly and synchronized save and start.  A Windows guest cluster may sometimes catch on to something being amiss – but often it does not.  Sometimes I’ll have to wait a few seconds for the nodes to start talking to each other again.

Anyhow – if you also use VirtualBox and have any tips you’d like to share when it comes to coordinating the guest launches, please post here as I’m definitely curious to learn about them.

Other articles

Imagine feeling confident enough to handle whatever your database throws at you.

With training and consulting from SQLskills, you’ll be able to solve big problems, elevate your team’s capacity, and take control of your data career.