Running VMware Player as a Windows Service
Ever wanted to run a VMware virtual machine as a background process on your Windows computer?
None of this is endorsed by VMware. This guide only documents my way of using VMware Player.
- Create a virtual machine
- Installing VMware Player
- Make VMware Player a service
- Running the virtual machine at startup
Use a VMware product to create an virtual machine. I used Workstation version 5.5, because it creates Generation 5 virtual machines. If using another VMware product, check VMware’s mobility guide to be sure that your product will create a virtual machine that is compatible with Player.
VMware Player will be used to run the virtual machines, because it can run Generation 5 virtual machines (and it’s free!). I have also run VMware ACE as a service using the same methods as VMware Player. If you require any security for your virtual machines, ACE may be a good fit for your installation.
Generation 5 virtual machines can automatically scale the virtual machine’s system RAM setting to fit available physical memory by adding a couple parameters to the .vmx file.
memsize = "2048"
In this case, the VM will have 2GB of RAM, if available. On machines with less than 2GB of RAM, Player will automatically adjust to fit available RAM. The memsize of 2048 is not the limit. If your environment has machines with more than 2GB of memory, then increase the number to suit.
As far as I know, this is an undocumented feature. I found it in the .vmx file for the Browser Appliance Virtual Machine provided by VMware.
On a the machine where the service will run (henceforth called the “target machine”), install VMware Player . This example uses version 1.0.1. Player can not co-exist with other VMware products on the same machine, so the target machine must be a different from the machine where the virtual machine was created. (This isn’t strictly true, because you could uninstall the creating VMware product and install Player, but then you wouldn’t be able to edit the machine and create new versions.)
It is possible to install VMware Player from the command line without all of the icons and Google Desktop integration. This makes automated deployment easy. Here’s the command:
VMware-player-1.0.1-19317.exe /s /v"/qn REBOOT=ReallySuppress ENABLE_GOOGLE_TOOLBAR=0 DISABLE_AUTORUN=0 STARTMENU_SHORTCUT=0 QUICKLAUNCH_SHORTCUT=0 DESKTOP_SHORTCUT=0
On first install, this installation command will have a return code 1303, indicating success with a reboot required to finish the install. Subsequent installs will have a return code of 0, indicating success. See Microsoft’s documentation on Windows Installer Error Codes for details.
- Obtain copies of instsrv.exe and srvany.exe from the appropriate Windows Resource Kit. For example, I used the Windows 2003 Resource Kit. Copy instsrv.exe and srvany.exe into %SYSTEMROOT% on the target machine.
- Copy the virtual machine files from the creating machine to a directory on the target machine. Not all files are needed; I only copied the files shown below.
- Add the fields to the .vmx file so that VMware Player suppresses all “Ok” message boxes and doesn’t show a user interface.
hints.hideAll = "TRUE"
msg.noOk = "TRUE"
- Create the VirtualServer service on the target machine using instsrv.exe.
|instsrv VirtualServer "%SYSTEMROOT%\System32\srvany.exe"|
Add the registry keys for srvany.exe so that it runs the vmplayer.exe with your virtual machine. For a detailed explaination of creating user-defined services in Windows, see the knowledge base article How to Create a User-Defined Service.
|Windows Registry Editor Version 5.00
To use a different VMware product such as ACE, change the “Application” value for this key. To use a different virtual machine, change the “AppParameters” value.
- Create a startup batch file for the virtual machine called start-vm.bat. This script first checks to see if the virtual machine’s files are installed and that the service isn’t already running. It then kills any old VMware processes and removes various state files from the last run of the virtual machine. To finish, the VirtualServer service is started.
:: If the server isn't installed, then don't start it
:: If the service is already running, then skip starting it
:: To make sure that lingering processes are dead, force kill the process
:: Remove *.lck, *.vmss from the grid directory
:: Start the server service
Create a stop batch file for the virtual machine called stop-vm.bat. The stop script stops the VirtualServer service and kills any VMware processes related to running the virtual machine.
:: If the server isn't installed, then don't start it
if not exist C:\VirtualServer goto noServer
:: Start the service
:: Force kill the processes
Important: The virtual machine must be OK with a hard shutdown. Killing the virtual machine’s processes is analagous to pulling the power cord on a physical computer. If virtual machine state doesn’t matter, then you could use the nonpersistent disk feature. Since the disk image is not modified while running the virtual machine, a hard shutdown wouldn’t have the chance of corrupting the virtual machine’s disk.
To automate creating the VirtualServr service, add the following to your grid installation batch file.
copy /Y instsrv.exe "%SYSTEMROOT%\system32\"
instsrv VirtualServer "%SYSTEMROOT%\System32\srvany.exe"
- Place the startup script in %SYSTEMROOT%\System32\GroupPolicy\Machine\Scripts\Startup. For this example, start-vm.bat is used as the startup script.
- On the target machine, make start-vm.bat run at system startup by adding it to the Startup group policy. gpedit.msc is the group policy editor. Run the command gpedit.msc to open the group policy editor. . Microsoft’s document How Core Group Policy Works has an extensive explanation of how machine startup and shutdown policies are applied.
- Within the “Scripts (Startup/Shutdown)” section of the group policy editor, open the Startup Properties window. Click “Add” and type the name of the virtual machine startup script.
- Do steps 1-3 for for the Shutdown group policy, but use stop-vm.bat as the script. Adding the Shutdown group policy may not be strictly necessary, but it could be important to run stop-vm.bat if it contained more commands than just killing the virtual machine.