scriptbased XenServer guest OS customization

One feature that is really missing at Citrix XenServer compared to VMware vSphere is OS Customization. My dear co-blogger Birk (the VMware guy) will never stop to underline this. At vSphere settings like Hostname, Network configuration, Domain membership, Licensing can be defined in specifications and applied to templates and VM images.

How VMware does it, how we workaround with XenServer

VMware vSphere can inject a command from hypervisor into the VM. VMware Tools needs to be started for that.

So at vSphere they are cloning the VM, create a VM-specific sysprep file from specification and inject that file with help of VMware Tools. After that they start the sysprep process and reboot. The “invoke-vmscript” cmdlet from powercli is the powershell implementation of passing the command to the VM.

Easy, simple, reliable but useless for XenServer as there is no possibility to pass a command from the hypervisor to a VM. But even though there is no possibility to prepare and start a sysprep from the hypervisor we can trigger the process from “outside” – as long as there is a network connection available.

Planning the implementation.

Here is our workaround: we generate and trigger sysprep from a “helper machine”. This host needs to have direct network access and credentials to XenServer and to the target VM.

I created a PowerShell script which handles the sysprep from your helper machine. In detail it does the following steps:

  1. read XenServer configuration from a XML config file (url, username, passwort)
  2. connect to XenServer or Poolmaster
  3. read configuration for the VM you want to customize from XML file (template, network, hostname..)
  4. create a clone from template, connect to the correct network
  5. power up new created VM and wait for VM to be started
  6. the VM is assumed running when XenServer tools provide a valid IP address and we are able to connect to specified TCP port (configured in vm configfile). For most scenarios tcp/445 (cifs) or TCP 3389 are common tcp ports to check against.
  7. connecting with specified credentials configured in the VM config file
  8. read custom sysprep answer file configured in the VM config file and transform it in a sysprep file for the target VM
  9. copy the VM specific sysprep.xml file at c:\sysprep\ on the target VM
  10. set the autologin registry key and make sure sysprep is started at next login.
  11. restart the target VM

That’s it: your target VM will reboot now, will autologin with the credentials you supplied and start sysprep. Sysprep will trigger the next reboot and customize everything you prepared.

** Setup example **

Choose a windows machine running at least powershell 2 (could be your workstation). In addition you’ll need to have the XenServer PowerShell CmdLet installed.

  1. Create a script directory (c:\osc in this scenario) and extract the provided files to it.
  2. Adjust hypervisor-sample.xml to your environment: choose poolmaster in “url” parameter if you are running a pool and save it as hypervisor.xml.
  3. Now create a sysprep template file using a Windows WAIK (www.microsoft.com/de-ch/download/details.aspx?id=10333) or adjust my sample-sysprep-domainjoin.xml. Make sure there is the line “REPLACECOMPUTERNAME” in your sysprep.xml – the script is doing a simple string replacement for your hostname matching REPLACECOMPUTERNAME. There is still some room for improvements in the scripts.
  4. Create the target VM config file by renaming and adjusting examplevm01-sample.xml. Make sure you have updated the path to your sysprep file.
  5. Copy the x64 version of reg.exe to C:\osc – no joke! :) – the only reliable way to manipulate remote registry was reg.exe. As powershell binary needs to be started as x86 version a start-process would always start the x86 version and just update the HKLM\SOFTWARE\Wow6432Node registry key. As a workaround we use reg.exe in the C:\osc directory.

Now you can just start you powershell x86 and run the osc.ps1 passing parameters hypervisor.xml and examplevm01.xml to it.

C:\Windows\SysWOW64\WINDOWSPOWERSHELL\V1.0\powershell.exe -file .\osc.ps1 -hypervisorconfig xenserver01.xml -clonevm example01.xml

Summary

As you see it’s possible to add OS customizing functionality with XenServer – even if it’s some work. You’ll need such functionality if you want to create an automated VM deployment.

The mentioned osc.ps1 and configurationfiles are available for download.