2011/07/17

Automatic start of Virtual Machine using VMware Player

I'm using VM with SVN server installed to store files, documents, and own projects. VMWare Server used so far to host this VM and start it automatically when my home PC started. But VMWare just retired its Server and suggested uisng either ESX or Player. Using ESX is not an option for me, and I have had a good experience with Player so far. At least NAT does not crash so often as with Server. So I decided to use Windows Player on my new home PC. Unfortunately, VMWare Player does not have the capabilities to start VM during startup. Note, that VMWare Workstation does not have such a feature too. Let's fix this using built-in Windows 7 features. I'm using a 64-bit host and 32-bit VM.

Setup automatic VM start

Windows Scheduler is a good tool to start VM. Simply add a new task to execute the application during system startup. Put full path to vmplayer.exe as application name (C:\Program Files (x86)\VMware\VMware Player\vmplayer.exe) and path to VM configuration file as application parameter (e.g. C:\VMServers\SVN\SVN.vmx).Disable all unnecessary execution options set by default then save created job. Unfortunately, the solution requires a second part. The player starts VM right after reboot well but does not power off the server during shutdown or reboot. So VM state could be inconsistent after reb oot.

Add automatic VM shutdown

VMWare player does not allow shutting down of existing VM using command line. But VMWare VIX does. Now shutdown event should be caught up. Windows scheduler does not allow to run any tasks on shutdown, however LocalSystem policy can be used to run specific script during system shutdown. Start Run dialog (Win+R) then type gpedit.msc then Enter. In opened local group policy editor, select System Configuration/Windows Settings/Scripts (Startup/Shudown). And add Shotdown.bat as shutdown script. Shudown.bat includes just a single line:

"C:\Program Files (x86)\VMware\VMware VIX\vmrun.exe" -T player suspend C:\VMServers\SVN\SVN.vmx

It should be saved as C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown\Shutdown.bat (note that GroupPolicy is a hidden folder and administrative rights are needed to write to this folder).

A reasonable question is why not use a local group policy startup script to start the VM instead of the scheduler. Unfortunately, this does not work. Most probably because not all necessary services started for VMWare Player.

Hints


Replace suspend with shutdown if you need to make a VM reboot for some reason.

VMWare Server allows attaching to console of auto-started VM. This is not the case for Player. The started instance is running on another desktop, so its window cannot be viewed. In case of any problem, VM can be suspended then started again using vmrun.exe. In this case, the restored VM should start on the current desktop:

"C:\Program Files (x86)\VMware\VMware VIX\vmrun.exe" -T player suspend C:\VMServers\SVN\SVN.vmx
"C:\Program Files (x86)\VMware\VMware VIX\vmrun.exe" -T player start C:\VMServers\SVN\SVN.vmx