A couple months ago I wrote a post about creating light weight windows vagrant boxes. For those unfamiliar with vagrant, a "Vagrant Box" is essentially a VM image and vagrant provides a rich plugin ecosystem that allows one to consume a "box" from different clouds and hypervisors and also use a variety of provisioning technologies to build out the final instance. My post covered how a windows image is prepared for vagrant and also discussed several techniques for making the image as small as possible. Last week I set about updating a windows vmware template using many of those same optimizations but when it came time to sysprep the image, alas it was not a tear free process.
This post will cover:
- gotchas when it comes to sysprepping windows images
- Troubleshooting sysprep failures
- public mourning of the loss of our good friend, cleanmgr.exe, on server 2012 R2
What is sysprep?
Sysprep is a command line tool that prepares a windows instance to be "reconsumed." It can take different command line arguments which will produce different flavors of output. My use of the tool and the one covered by this post is to prepare a base windows image to be deployed from VMWare infrastructure. This often involves the use of the /generalize switch which strips a windows OS of its individuality. It removes things like hostname, IP, user SIDs and even geographical association. You can also provide sysprep a path to an unattend file, also known as an answer file, that can contain all sorts of setup metadata such as administrator credentials, startup script, windows product key and more. Here is an example:
<?xml version="1.0" encoding="utf-8" ?> <Unattend> <UserData> <!--This section contains elements for pre-populating user information and personalizing the user experience--> <AdminPassword Value="TG33hY" StrongPassword="No" EncryptedPassword="No"/> <FullName Value="Cookie Jones" /> <ProductKey Value="12345-ABCDE-12345-ABCDE-12345" /> </UserData> <DiskConfig> <!--This section contains elements for pre-populating information about disk configuration settings--> <Disk ID="0"> <CreatePartition /> </Disk> </DiskConfig> <SystemData> <RegionalSettings> <!--This section contains elements for selecting regional and language settings for the user interface--> <UserInterface Value="12" /> </RegionalSettings> </SystemData> </Unattend>
This has the advantage of preparing a fresh install that does not require the user to manually input a bunch of information before logging on and being productive.
You might prep the os with this file by running:
C:\windows\system32\sysprep\sysprep.exe /generalize /oobe /shutdown /unattend:myAnswerFile.xml
Sysprep without running sysprep
I dont often have the need to directly interact with sysprep.exe. Almost all of my dealings with it have been through VMWare's customization tooling and API which allow me to provision windows machines from ruby code that instruct VMWare how to perform sysprep and assemble the answer file. Here is an example of working with the ruby based vmware API, rbvmomi, to programatically construct the answer file:
def windows_prep_for(options, vm_name) cust_runonce = RbVmomi::VIM::CustomizationGuiRunOnce.new( :commandList => [ 'winrm set winrm/config/client/auth @{Basic="true"}', 'winrm set winrm/config/service/auth @{Basic="true"}', 'winrm set winrm/config/service @{AllowUnencrypted="true"}', 'shutdown -l']) cust_login_password = RbVmomi::VIM::CustomizationPassword( :plainText => true, :value => options[:password]) if options.has_key?(:domain) cust_domain_password = RbVmomi::VIM::CustomizationPassword( :plainText => true, :value => options[:domainAdminPassword]) cust_id = RbVmomi::VIM::CustomizationIdentification.new( :joinDomain => options[:domain], :domainAdmin => options[:domainAdmin], :domainAdminPassword => cust_domain_password) else cust_id = RbVmomi::VIM::CustomizationIdentification.new( :joinWorkgroup => 'WORKGROUP') end cust_gui_unattended = RbVmomi::VIM::CustomizationGuiUnattended.new( :autoLogon => true, :autoLogonCount => 1, :password => cust_login_password, :timeZone => options[:win_time_zone]) cust_userdata = RbVmomi::VIM::CustomizationUserData.new( :computerName => RbVmomi::VIM::CustomizationFixedName.new( :name => options[:hostname] ), :fullName => options[:org_name], :orgName => options[:org_name], :productId => options[:product_id]) RbVmomi::VIM::CustomizationSysprep.new( :guiRunOnce => cust_runonce, :identification => cust_id, :guiUnattended => cust_gui_unattended, :userData => cust_userdata) end
VMWare calls sysprep.exe for me on the base vm template image and can pass in a file like the one above to enable winrm, register the product key, setup the local administrator and domain join the final vm. This all works great except for when it doesn't.
When things go wrong either by calling sysprep.exe directly or via VMWare, its not immediately obvious what the error is. In fact I would say that it is immediately very confusing...and even worse sometimes it is not immediate at all. I wrote a post six months ago about how to troubleshoot unattended windows provisioning gone wrong. Here I want to look specifically at issues concerning disk cleanup.
Preparing for sysprep
Often the point of running sysprep is to be able to take a golden image and deploy that for use in many virtual instances.So you want to make sure that the image you are capturing is...well...golden. That might also mean, especially for windows, as small as possible. Since windows images are much larger than their linux counter parts and orders of magnitude larger than containers, its important to me that they be as small as possible at the outset so that an already drawn out provisioning time does not go even longer.
There are a few techniques that can be applied here and which ones will depend on the version of windows you are running. I'm focusing here on the latest released server version 2012 R2. I'd definitely encourage you to read my vagrant post that talks about some of the new features of component cleanup and features on demand that can shave many gigabytes off of your base image. Another tool that many use to purge useless files from their windows os is cleanmgr.exe. Many know this better as the little app that is launched from the "disk cleanup" button when viewing a disk's properties.
Enabling Disk Cleanup on windows server
Windows clients have this feature enabled by default but out of the box it is not present on server SKUs. The way to enable it is by adding the Desktop Experience feature. This would be done in powershell by running:
Add-WindowsFeature Desktop-Experience
The problem with this is that the Desktop-Experience brings alot of baggage with it that you do not typically need or want on a server. In fact, it will automatically enable two additional features:
- Media Services
- Ink and Handwriting Services
All around in files and registry size, this makes your OS footprint larger so there are typically two ways to deal with this.
Install, Cleanup, Uninstall
You want to have this be the last step of your image preparation process. Once everything is as it should be, you install the Desktop Experience, perform a required reboot, invoke cleanmgr.exe and dump as much as you can and then uninstall the feature along with the above two features it installed. Then finally, of course, reboot again.
Install cleanmgr.exe ala carte style
You dont need this feature just to run cleanmgr. While this is certainly not obvious, it is buried deep inside your windows folder even when the desktop experience is not enabled. This is even documented on Technet. Search for cleanmgr.exe and cleanmgr.exe.mui inside of c:\windows\winSXS:
Get-ChildItem -Path c:\windows\winsxs -Recursive -Filter cleanmgr.exe Get-ChildItem -Path c:\windows\winsxs -Recursive -Filter cleanmgr.exe.mui
This may return two or three versions of the same file. You'll probably want whichever has the highest versions. According to the above referenced Technet article, on server 2008 R2 these will be in:
C:\Windows\winsxs\amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.1.7600.16385_none_c9392808773cd7da\cleanmgr.exe C:\Windows\winsxs\amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.1.7600.16385_en-us_b9cb6194b257cc63\cleanmgr.exe.mui
They can simply be copied to c:\windows\system32 and c:\windows\system32\en-US respectively. While they wont be visible from the disk properties, you can still access them from the command line.
Two steps forward one step back on server 2012 R2
Server 2012 R2 has delivered some major enhancements for reducing the size of a windows os footprint. It provides commands for cleaning out installed updates and you can completely remove unused features from disk. Further, those parts of winSXS that are not in use are compressed. This is all great stuff but the problem is that because cleanmgr.exe is compressed, it cannot simply be copied out and run as is. Further, neither I nor anyone else on the internet can seem to extract it.
Its clearly compressed. While disabled, its about 82k and 213k afterwards. I tried using the compact commandline tool as well as winrar without luck.
One option is to do a mix of the above two approaches: Enable the feature. Once enabled, those two files are both expanded and moved to system32. Then copy them somewhere safe before disabling the feature. Now you could use these files either on this machine or another server 2012 R2 box...or not.
I have tried this and it works in so far as I can get cleanmgr.exe to pop its GUI dialog, but it appears crippled. Only a hand full of the usually available options are present:
Where are the error reports, the setup files, etc?
So what does this have to do with sysprep?
Go ahead and perform a sysprep after disabling the desktop experience feature.
A fatal error...hmm.
Troubleshooting sysprep
When things go wrong during a sysprep cycle, the place to look is:
c:\windows\system32\sysprep\panther\setupact.log
This file will almost always include a more instructive error as well as information as to what it was doing just before the failure which can help debug the issue. The error we get here is:
Package winstore_1.0.0.0_neutral_neutral_cw5n1h2txyewy was installed for a user, but not provisioned for all users. This package will not function properly in the sysprep image.
Sysprep will attempt to uninstall all windows store apps and here it is complaining that it cannot and one is still installed.
Lets just check to see what store apps are currently installed:
PS C:\Users\Administrator.WIN-DKAJ9Q1JK5N> Get-AppxPackage Name : winstore Publisher : CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US Architecture : Neutral ResourceId : neutral Version : 1.0.0.0 PackageFullName : winstore_1.0.0.0_neutral_neutral_cw5n1h2txyewy InstallLocation : C:\Windows\WinStore IsFramework : False PackageFamilyName : winstore_cw5n1h2txyewy PublisherId : cw5n1h2txyewy IsResourcePackage : False IsBundle : False IsDevelopmentMode : False Name : windows.immersivecontrolpanel Publisher : CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US Architecture : Neutral ResourceId : neutral Version : 6.2.0.0 PackageFullName : windows.immersivecontrolpanel_6.2.0.0_neutral_neutral_cw5n1h2txyewy InstallLocation : C:\Windows\ImmersiveControlPanel IsFramework : False PackageFamilyName : windows.immersivecontrolpanel_cw5n1h2txyewy PublisherId : cw5n1h2txyewy IsResourcePackage : False IsBundle : False IsDevelopmentMode : False
Ok. fine. We'll just delete them ourselves.
PS C:\Users\Administrator.WIN-DKAJ9Q1JK5N> Get-AppxPackage | Remove-AppxPackage Remove-AppxPackage : Deployment failed with HRESULT: 0x80073CFA, Removal failed. Please contact your software vendor. (Exception from HRESULT: 0x80073CFA) error 0x80070032: AppX Deployment Remove operation on package winstore_1.0.0.0_neutral_neutral_cw5n1h2txyewy from: C:\Windows\WinStore failed. This app is part of Windows and cannot be uninstalled on a per-user basis. An administrator can attempt to remove the app from the computer using Turn Windows Features on or off. However, it may not be possible to uninstall the app. NOTE: For additional information, look for [ActivityId] cc6d4139-3ae8-0000-0447-6dcce83ad001 in the Event Log or use the command line Get-AppxLog -ActivityID cc6d4139-3ae8-0000-0447-6dcce83ad001 At line:1 char:19 + Get-AppxPackage | Remove-AppxPackage + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (winstore_1.0.0....l_cw5n1h2txyewy:String) [Remove-AppxPackage], IOException + FullyQualifiedErrorId : DeploymentError,Microsoft.Windows.Appx.PackageManager.Commands.RemoveAppxPackageCommand Remove-AppxPackage : Deployment failed with HRESULT: 0x80073CFA, Removal failed. Please contact your software vendor. (Exception from HRESULT: 0x80073CFA) error 0x80070032: AppX Deployment Remove operation on package windows.immersivecontrolpanel_6.2.0.0_neutral_neutral_cw5n1h2txyewy from: C:\Windows\ImmersiveControlPanel failed. This app is part of Windows and cannot be uninstalled on a per-user basis. An administrator can attempt to remove the app from the computer using Turn Windows Features on or off. However, it may not be possible to uninstall the app. NOTE: For additional information, look for [ActivityId] cc6d4139-3ae8-0000-0f47-6dcce83ad001 in the Event Log or use the command line Get-AppxLog -ActivityID cc6d4139-3ae8-0000-0f47-6dcce83ad001 At line:1 char:19 + Get-AppxPackage | Remove-AppxPackage + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (windows.immersi...l_cw5n1h2txyewy:String) [Remove-AppxPackage], IOException + FullyQualifiedErrorId : DeploymentError,Microsoft.Windows.Appx.PackageManager.Commands.RemoveAppxPackageCommand
Ugh. We cant uninstall these? Nope. You cannot. So once you install the desktop experience feature, it cannot be fully uninstalled. The only way to sysprep this machine is to keep the desktop experience feature enabled.
Whether you sysprep via the VMWare tools or directly, you can now no longer run a successful sysprep without the desktop experience unless you start over with a new OS. I have scowered the internet for a work around have not found any. There are lots of folks complaining about this.
Its not as bad as it might seem
The fact of the matter is that I do not see this as being a horrendous show stopper at least not for my use case. By the time I run disk cleanup, there really is not that much to be purged. Far less than a gigabyte. This is because I am preparing a fresh os so there has not been much accumulation of cruft. The vast majority of disposable content I can now purge very thoroughly with the new DISM.exe command:
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
Worse case, I manually delete temp files and some of the other random junk lying around. Its unfortunate that we have lost cleanmgr.exe but all is not lost.
Exceeding the 3 sysprep limit
Another issue I hit with sysprep that threw me and prompted a fair amount of research was the limit of 3 sysprep runs from a single os install. It is true that you are limited to three but there is an easy workaround I found here. The limit manifests itself with another fatal error during sysprep and the following message in the log file:
RunExternalDlls:Not running DLLs; either the machine is in an invalid state or we couldn't update the recorded state, dwRet = 0x1f
According to the post mentioned above, the work around is to set the following reg keys:
HKEY_LOCAL_MACHINE\SYSTEM\Setup\Status\SysprepStatus\GeneralizationState\ CleanupState:2 GeneralizationState:7
Then run:
msdtc -uninstall msdtc -install
and then reboot. I was able to get by by just setting the GeneralizationState property of HKEY_LOCAL_MACHINE\SYSTEM\Setup\Status\SysprepStatus\GeneralizationState to 7, but your mileage may vary.