Monday, May 5, 2008

VMware Player Service

VMware Player Service
The Stowers Institute For Medical Research

Running VMware Player as a Windows Service

February 10, 2006
by Dan Thomasset (dct@stowers-institute.org)

Overview

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.

  1. Create a virtual machine
  2. Installing VMware Player
  3. Make VMware Player a service
  4. Running the virtual machine at startup

1.1 Create a virtual machine

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"
MemAllowAutoScaleDown = "TRUE"

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.

1.2 Install VMware Player

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.

The installation command was derived using Orca to find the checkbox names in the Player installer package. Check out Customonizing MSI installs from the command line for details.

1.3 Make VMWare Player a service

  1. 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.
  2. 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.

  1. Add the fields to the .vmx file so that VMware Player suppresses all "Ok" message boxes and doesn't show a user interface.

server.vmx

...
hints.hideAll = "TRUE"
msg.noOk = "TRUE"
...

The hints.hideAll parameter was mentioned in the VMware - At Your Service! article in the June 2004 issue of Windows IT Pro. The msg.noOk parameter was found in DaveP's comment the VMware forums.

  1. 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.

VirtualServer.reg

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\VirtualServer\Parameters]
"Application"="\"C:\\\\Program Files\\\\VMware\\\\VMware Player\\\\vmplayer.exe\""
"AppParameters"="\"C:\\\\VirtualServer\\\\server.vmx\""
"AppDirectory"="\"C:\\\\VirtualServre\\\\\""

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.

  1. 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.

::
:: start-vm.bat
::


:: If the server isn't installed, then don't start it
if not exist C:\VirtualServer goto noServer

:: If the service is already running, then skip starting it
net start | grep -q -i "VirtualServer"
if %ERRORLEVEL% == 0 goto noServer

:: To make sure that lingering processes are dead, force kill the process
taskkill /F /IM vmplayer.exe
taskkill /F /IM vmware-vmx.exe

:: Remove *.lck, *.vmss from the grid directory
del "C:\VirtualServer\*.lck"
del "C:\VirtualServer\*.vmss"
del "C:\VirtualServer\*.vmem"

:: Start the server service
net start VirtualServer

:noServer

  1. 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.

::
:: stop-vm.bat
::


:: If the server isn't installed, then don't start it

if not exist C:\VirtualServer goto noServer

:: Start the service
net stop VirtualServer

:: Force kill the processes
taskkill /F /IM vmplayer.exe
taskkill /F /IM vmware-vmx.exe

:noServer


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\"
copy /Y srvany.exe "%SYSTEMROOT%\system32\"

instsrv VirtualServer "%SYSTEMROOT%\System32\srvany.exe"
regedit /s VirtualServer.reg

1.4 Running the virtual machine at startup

  1. Place the startup script in %SYSTEMROOT%\System32\GroupPolicy\Machine\Scripts\Startup. For this example, start-vm.bat is used as the startup script.

  1. 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.

  1. 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.

  1. 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.

No comments: