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