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.
ControlSet001? Not CurrentControlSet?
Yes that’s correct. “CurrentControlSet” is a fiction invented by Windows when it’s running. We’re modifying the hive files of a shut down virtual machine, and that key does not exist.
That not entirely true.
There are a few ControlSet (001, 002,003, sometimes even more).
CurrentControlSet only point to the correct one, when Windows is running.
Since you are editing this when windows is offline, you need to look first at:
HKEY_LOCAL_MACHINE\SYSTEM\Select
and see what’s controlset is listed as default (which is what NTLDR chooses when booting windows).
It is usually 001, but it might change if there where, for example, previous boot errors, or use of LastKnownGood, and a few other.
We should note this in the documentation.
Documentation updated:
http://git.annexia.org/?p=libguestfs.git;a=commitdiff;h=25c15261253bbd2fb140e5ca1c902acf714f76a9
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.
Pingback: Tip: Install a device driver in a Windows VM « Richard WM Jones
Pingback: New home for (RH) SrvAny « Richard WM Jones
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
https://github.com/rwmjones/rhsrvany/issues/2