Brian Madden Logo
Your independent source for application and desktop virtualization.
Marketplace

advertisement
Brian Madden's Blog

How Applications use the Registry in Terminal Server Environments (Part 2 of 3)

Written on Aug 03 2004 16,750 views, 45 comments


by Brian Madden

In Part One of this series, we looked at the basics of how Terminal Server uses the registry. In this article, we’ll examine at how applications use the registry in Terminal Server environments, mostly focusing on application installation issues.

Installing an application on a Windows server causes it to write data and save settings in both the HKLM and HKCU registry hives. As we learned in the previous article, the HKLM hive stores the system-wide application settings such as file installation paths and installed options, and the HKCU hive stores the user-specific settings such as custom dictionaries, menu settings, and default fonts and colors.

As you’ve probably guessed, Terminal Servers behave a bit differently than regular Windows computers. To understand this, let’s look at an example. Imagine that you’re installing Microsoft Office onto a Windows XP workstation. Besides copying the files, the setup.exe program will write all the necessary Office settings to the registry. In this case, it will write the system-wide stuff to HKLM and it will write the default user settings to HKCU. On a single-user system such as Windows XP Professional, this works fine.

However, on multi-user Terminal Servers, this could cause a few problems. First is the fact that all the application’s new registry settings would be written to the specific HKCU of the actual user account that was used to install the application. This could cause a problem when a regular user logged onto the server and ran the application since their personal HKCU wouldn’t contain any of the application’s settings. (Whether this is a problem or not really depends on the application itself.)

So how does Terminal Server deal with these two problems?

Every user session on a Terminal Server operates in one of two modes: “execute” mode or “install” mode. Execute mode describes regular user session behavior when users are “executing” applications. This is the standard mode of operation and how your user sessions will operate 99.9% of the time.

However, when you install an application onto a Terminal Server, the session you’re installing it from switches to “install” mode. (In the old days, you had to manually put a session into install mode via the command-line switch “change user /install.” You can still do this in Windows 2003, although the server now is smart enough to detect when you’re installing applications and it automatically puts the session into install mode as needed.

So what’s the point of install mode?

Let’s think back to the application installation problem we outlined earlier. When an application is installed on a server, the application usually writes a bunch of settings to the active user’s HKCU hive. In a Terminal Server environment, there needs to be some way to ensure that any application settings written to HKCU when the application is installed get copied to every single user’s HKCU hive when they log in. Otherwise, applications might behave strangely for the users since they don’t have any of the proper registry keys configured.

Therefore, when a user session goes into install mode, the server monitors all the activity of that user’s HKCU hive. Then, any changes to the user’s HKCU\Software key (i.e. from the application installation) are also written to a special Terminal Server HKLM key at HKLM\Software\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\Software. This key is informally known as the “shadow” key. As such, this shadow key contains all the most recent application installation information that’s needed in each user’s personal HKCU\Software key.

So far, so good, right? The only thing missing is how a Terminal Server knows whether a user who’s logging on needs to receive some settings from the Terminal Server’s shadow area. It does this by comparing timestamps between the two.

When a session is in install mode, each time a “real” HKCU\Software value is echoed to the shadow key location the server updates a timestamp called “LatestRegistryKey” stored in the HKLM\ SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\IniFile Times. This value of this registry entry is the number of seconds since January 1, 1970.

(As a side note, you’ll also notice a listing of INI files with timestamps in this same registry location since install mode also affects them. However, in today’s world, INI files aren’t really used and we only really care about the registry operations.)

All these timestamp values live in that “IniFile Times” registry key mentioned earlier. One of the lesser-known facts about the Windows registry is that every registry key has its own timestamp that shows the last time it was updated, similar to standard file timestamps. (These timestamps apply to each key, not to each value.)

Anyway, whenever a user logs on to any Windows computer, a process called userinit.exe is used to run programs before the shell loads. In Terminal Server environments, this executable compares the hidden timestamp of the server’s IniFile Times key to a timestamp in the user’s HCKU hive that holds the last synchronization time. (This “per user” timestamp is stored in the user’s HKCU\Software\Microsoft\Windows NT\CurrentVersion\Terminal Server\LastUserIniSyncTime.)

In doing so, the system can figure out whether the registry settings in the user’s HKCU are newer or whether the “shadow” settings in the server’s HKLM are newer. If the hidden timestamp of the IniFile Times key in the shadow area is more recent, the system knows that new software must have been installed on the server. Therefore, userinit.exe enumerates all the keys in the Terminal Server compatibility shadow are and compares each one’s hidden timestamp to the corresponding key in the user’s personal HKCU\Software key. If the shadow key is newer, the user’s corresponding key is deleted, and they receive the new key from the shadow area once their sessions starts. (This is known as “Terminal Server registry mapping.”

On the surface, it seems like this architecture is rather ingenious and that you don’t have to worry about anything. However, there is one major problem that can manifest itself in the real world.

Imagine that you have multiple Terminal Servers or Citrix servers that are all running the same application, such as Microsoft Office. If you’re like most companies, you probably installed Office on those servers a several months or even years ago. Since Office was originally installed via a user session operating in install mode, the server’s shadow key was created and timestamped with the original install time. Any users who did not have the appropriate Office settings in their own personal HKCU key received them when they logged on to the server for the first time.

Now, fast forward to today. What happens if you want to add more capacity to your server farm? Most likely you’d build a few more servers, install Office on them, and load-balance them with your existing servers.

Can you see the problem here? The timestamp on the shadow key area of the new Terminal Servers will be current, and in fact much newer than each user’s individual HKCU last update timestamp. Therefore, upon logging on to one of the new servers, userinit.exe will start comparing the key-by-key timestamps of all the HCKU\Software keys to the server’s shadow area software keys. Since the server just got a fresh install of Microsoft Office, those timestamps will be much newer than any of the user’s personalized settings in their own HKCU. The result? Mass deletion of all the Microsoft Office-specific keys in the user’s HKCU\Software hive.

From the user’s standpoint, it will appear as if all of their Office settings reverted back to the defaults, all because they were randomly load-balanced to a new Terminal Server.

Bummer dude!

So what can we do to change this behavior? Unfortunately, there’s no way to disable the shadow key functionality, and Windows 2003 does a pretty good job of ensuring that applications are installed with the user session configured for install mode. Therefore, if we can’t disable it, we’ll have to outsmart it.

This essentially means that we’ll have to ensure that the timestamps on the servers’ shadow area are older than the timestamps of the user’s HKCU hive. There are two ways to do this:

  • Before installing an application, set the server’s system clock back to before the time you installed the application on one of the original servers.
  • Deploy the new server from an image of the old server. (Ghost, sysprep, breaking a hardware mirror, etc.) If you do this, remember that you must use some kind of imaging technology. Unattended installs will not work in this case since they’ll write the current dates to the shadow area.

Alternately, if you don’t want to do either of these, you can simply delete the keys out of the new server’s shadow area. That will ensure that the users’ versions of those keys are not deleted when they log on to a new server. However, if you do this, you’ll have to use a logon script or some other method to ensure that new users get the appropriate keys populated into their own HKCU hive.

That pretty much sums up what you need to know about how the registry is used when you install applications. In the final installment of this three-part series on the registry, I’ll look at all the specific registry keys that are important in Terminal Server environments.



Comments

Guest wrote Microsoft patches
on 12-12-2004 1:30 PM
This message was originally posted by Bluff Rooster on August 3, 2004
Interesting stuff. I'm hoping to find out how important it is to install MS patches in install mode and whether it's best to do the patching when users are off the system. It's a bit of a pain to download each required patch then install each using Add or Remove Programs (for each Win2003 server) first ensuring users are logged off - is this necessary or can we automate the roll out of patches using 3rd party tools while users are active?

How do admins of large farms roll out MS patches (eg MS04-025) - I've been taught that Citrix Installation Manager is unreliable (at least in XP FR2)?

Thanks in advance!
Guest wrote Ooops, forgot to mention...
on 12-12-2004 1:31 PM
This message was originally posted by Bluff Rooster on August 3, 2004
Page 339 of the official Citrix Windows Server 2003 handbook advises that, when building the system, you should install "updates" (I assume those found on the Windows Update site) using install mode. I guess the same would need to be done after the system has been up some time and needs repatching but doubt this is common practice - maybe 'cos its unnecessary?
Guest wrote Installing Patches
on 12-12-2004 1:31 PM
This message was originally posted by Brian Madden on August 3, 2004
If a system goes into install mode to install patches, that will only update the timestamp on the IniFile Time reg key if the patch writes to HKCU\Software, which is unlikely. Besides, even if it did, a newer IniFile Times hidden timestamp would only trigger the sync process. In this case the users would be okay because the system will check each key's hidden timestamp one-by-one, and presumably Office keys would not be updated, and therefore not deleted.
Guest wrote Could not have said it better my self...
on 12-12-2004 1:31 PM
This message was originally posted by Freddie larsson on August 4, 2004
Very good article, very easy to understand.
But even if you feel like there seldom is problems whit the .ini-files mapping now a days I rely think it would bee very interesting if you explained that phenomenon anyway. I think many people would appreciate that.

See ya

/Freddie
Guest wrote Excellent!!!!
on 12-12-2004 1:31 PM
This message was originally posted by French Reader on August 4, 2004
Thanks a lot for this article. I work on TSE and Citrix servers for 4 years, and even if a understand that some "hidden" process were doing some configuration on users's hive, i have never really know exactly how. Now, it's perfect.
Thanks a lot again. Go further, it's always good to learn or re-learn (ex: part 1 of this article). :-)
Guest wrote Well Said...
on 12-12-2004 1:31 PM
This message was originally posted by MTB on August 4, 2004
Brian - excellent article. I know a lot about the Windows registry, and how it relates to Terminal Services, but this is new to me... Great stuff! Keep it coming...
Guest wrote Can't wait for part 3
on 12-12-2004 1:31 PM
This message was originally posted by an anonymous visitor on August 4, 2004
Very interesting article eventhough I've already known what you have said in the article. Setting the server system clock back was exactly what I did to ensure user's setting didn't get revert back. I can't wait for the more detailed explanation in part 3. I bought your book and think your Citrix book is the best out there.
Guest wrote question...
on 12-12-2004 1:31 PM
This message was originally posted by an anonymous visitor on August 4, 2004
I'm assuming the example of "out-smarting shadow key functionality" is only applicable if you are using roaming profiles, correct?
Guest wrote Old mechanism?
on 12-12-2004 1:31 PM
This message was originally posted by Marc Smit on August 4, 2004
Brian, nice to discus this part of SBC technology. This part of terminal server is always difficult to manage, because this whole mechanism is actually meant for 1 terminal server design, but that's another story. I do not totally agree with your story. If I install an application on a windows XP professional (single-user system) the default settings are written to HKLM & HKCU. That works fine you say. But what happens if another person logs on to this system. There is no “Shadow key” present, so where does this person gets the HKCU settings from? I think this mechanism could also do his job on single-user systems. Besides if you look in the registry of a Windows XP Professional system you can see that there is already a HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\Software key . I don’t know what the purpose of this key is.

Actually we don’t have any problem with this on any given workstation, because we mostly install the application via a msi package. The msi technology has attempted to overcome this shortcoming and uses self healing to configure current user settings for all users of a given workstation. After the application is installed on the system a part of the msi (no binary) is copied to the systemroot\install directory (it’s a system dir, so to see this dir you have to edit the folder view options.)

If we only use msi’s for terminal server, why do we still use this old mechanism?
Guest wrote Alternative timestamp solution
on 12-12-2004 1:31 PM
This message was originally posted by Kristoffer Johansson on August 6, 2004
There is another solution for the timestamp/multiple server and installation time problem: by using native NT API calls, you can (re)set the timestamp on individual keys. Donald Fens provides a helper dll for this purpose, se http://www.d-fens.net/?idMenu=3&idSub=1 for more information.
Guest wrote Group Policies
on 12-12-2004 1:31 PM
This message was originally posted by Drew Dubber on August 11, 2004
I would have thought you could do a simple policy addition to set the timestamp key back to what you want? Put all those Office SBC servers into one OU. As we are using a Hybrid profile solution, this would work across the estate. Thanks for the info!
Guest wrote triCerat Simplify Profiles
on 12-12-2004 1:31 PM
This message was originally posted by John Byrne on August 12, 2004
We've done a ton of work to make this stuff easy and far more powerful. Shameless plug I know, but we really have simplified this!
Guest wrote Install mode per session???
on 12-12-2004 1:31 PM
This message was originally posted by Rich Brumpton on August 13, 2004
First off GREAT ARTICLE!!!!! this is one area where ALOT of citrix/TS admins are weak. It's nice to see someone covering such an important fundamental concept.

I do have a question on install mode however. You state that the SESSION is in install mode. It was my impression that the entire server entered install mode. I remember having instances where someone would be logged on while installing software and settings that they changed in their session would affect future users. I have not seen this since TSE, but then again I NEVER install software with users logged on anymore :)

Could we get a clarification on this?
Guest wrote Yes, Install mode is per-session, not per-server
on 12-12-2004 1:31 PM
This message was originally posted by Brian Madden on August 13, 2004
You can confirm this by running "change user /query" from each user session.
Guest wrote great article. where is "1 of 3?"
on 12-12-2004 1:49 PM
This message was originally posted by stuart eggerton on September 9, 2004
Excellent article! Where is (Part 1 of 3) of
"How Applications use the Registry in Terminal Server Environments"
I couldn't find it in the list of previous articles
Guest wrote PerUser specific files.
on 12-12-2004 1:49 PM
This message was originally posted by Naren on September 13, 2004
Excellent article in understanding the registry in TS environment. This article deals with the information of updating the HKCU registry hives for the users who logins to server for using the application through terminal services. How the same works incase of files going to users profiles folders (ex.App data folder, My documents??)
Guest wrote Alternative timestamp solution
on 12-12-2004 1:50 PM
This message was originally posted by an anonymous visitor on September 30, 2004
Hi, there is another method to set a Timestamp
Stop Timeservice W32Time
Set Time
Set Date
Export Registry "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server" to a File e.g. shadow.reg
Delete Registry "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server"
Import shadow.reg (regedit.exe /i /s shadow.reg)

then Set all IniFileTimes to the Same Time

E.G. Use This Skript

XXXX BEGIN OF SCRIPT XXXX
Option Explicit
On Error Resume next
Dim objWSHShell, objRegistry
Dim strKey, strTemp, arySubKey
Const HKEY_LOCAL_MACHINE = &H80000002

Set objWSHShell = WScript.CreateObject("WScript.Shell")
Set objRegistry = GetObject("winmgmts:root/default:StdRegProv")
strKey = "Software\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\IniFile Times"
strTemp = objRegistry.EnumValues(HKEY_LOCAL_MACHINE, strKey, arySubKey)
For Each strKey In arySubKey
WScript.Echo strKey
objWSHShell.RegWrite "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\IniFile Times\"+ strKey,1000000000, "REG_DWORD"
Next

XXXX END OF SCRIPT XXXX

Restart Time Service W32Time or doe reboot.

....

hope that helps ;-)

bye bye
Guest wrote Strange behaviour that...
on 12-12-2004 1:50 PM
This message was originally posted by TSUser on September 30, 2004
... some people uses these comments to post questions (see first comment) while there are tons of good forums available...
My comment of the article is: if you do not use roaming profile you will not have the issue. This seems obvious but important to say though. Thanks for this article!!! Where is part 1 please?
Guest wrote What about TermSvrCopyKeyOnce
on 12-12-2004 1:52 PM
This message was originally posted by Phil Bowman on October 8, 2004
The registry key value TermSvrCopyKeyOnce would seem to be a key issue in rerspect of shadow key replication, having been conceived to deal with aspects of the problem, but surprisingly is not mentioned in your article. This has been mentioned in various places in Microsoft literature, but I have never seen an explicit detailed explanation of how it works.

It was dealt with in a Microsoft document produced by Frank Kim, but when I sent an email asking for more information, he replied that this issue seems to be undocumented!