Tip: Install a service in a Windows VM

Previously I discussed how to get a script to run the first time a user logs in. This tip goes further and demonstrates how to install a service into a Windows VM using guestfish, virt-win-reg and a new open source program written by my colleague Yuval Kashtan called RHSrvAny1.

First, compile RHSrvAny from source. You can do this using our completely free Fedora Windows cross-compiler stack. Just:

# yum install mingw32-gcc

Clone the RHSrvAny git repo and compile it:

$ mingw32-configure
$ make

Second we’ll copy the files we need into the Windows guest. Note: The Windows VM must be shut off.

# guestfish -i Windows7x64
Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for a list of commands
      'man' to read the manual
      'quit' to quit the shell

><fs> upload RHSrvAny/rhsrvany.exe /rhsrvany.exe
><fs> upload test.exe /test.exe
><fs> exit

“test.exe” is a little program I wrote which writes the date into C:\TEST.LOG but you can also use the batch file from the last tip or any JScript or VBScript you happen to have (via cscript.exe).

Third we need to add some Windows Registry keys to tell Windows about the new service:

# cat service.reg
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\RHSrvAny]
"Type"=dword:00000010
"Start"=dword:00000002
"ErrorControl"=dword:00000001
"ImagePath"="c:\\rhsrvany.exe"
"DisplayName"="RHSrvAny"
"ObjectName"="LocalSystem"

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\RHSrvAny\Parameters]
"CommandLine"="c:\\test.exe"
"PWD"="c:\\Temp"
# virt-win-reg --merge Windows7x64 service.reg

The magic numbers in the registry entries let you do things like boot with the service disabled. See this MSDN article.

Edit: See Yuval’s comment about alternatives to using "ObjectName"="LocalSystem".

Now boot your Windows guest, and observe the log file to prove that test.exe was run, and/or look at the list of services in the control panel.

><fs> cat /TEST.LOG
Hello
Thu Apr 29 18:39:13 2010

1 Actually you could install any service, but I’m using RHSrvAny because it can turn ordinary Windows programs and scripts into services. It takes care of the Windows “Service Control Protocol” for us.

Advertisement

10 Comments

Filed under Uncategorized

10 responses to “Tip: Install a service in a Windows VM

  1. Yaniv

    ControlSet001? Not CurrentControlSet?

  2. Yuval Kashtan

    Yes this is nice trick.
    A few comments though:
    1) RHSrvAny is based on SrvAny by Microsoft. The only problem with SrvAny is that it’s license does not permit re-distribution. Since we wanted to be able to distribute or product, we developed our own open source version.
    2) “LocalSystem” is THE most privileged account in windows. and in most cases – unnecessary. for Services which does not require high permissions, please use “NetworkService”.
    3) If you’re still confident you absolutely must use “LocalSystem” make sure you have both the service and the script/executeable it is executing, located in a protected directory (Preferably one that only “Administrators” and “LocalSystem” can access.
    otherwise normal users might be able to utilize this to gain elevated privileges.

  3. Pingback: Tip: Install a device driver in a Windows VM « Richard WM Jones

  4. Pingback: New home for (RH) SrvAny « Richard WM Jones

  5. Hello, I know this is a very old post, but hoping that I still can get a solution for it. When I try to run `mingw32-configure` I am getting the following error:

    /usr/bin/mingw32-configure: line 77: ../configure: No such file or directory

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.