Time Synchronization in Hyper-V

Time Synchronization in Hyper-V

Rate This
  • Comments 41


There is a lot of confusion about how time synchronization works in Hyper-V – so I wanted to take the time to sit down and write up all the details. 

There are actually multiple problems that exist around keeping time inside of virtual machines – and Hyper-V tackles these problems in different ways.

Problem #1 – Running virtual machines lose track of time.

While all computers contain a hardware clock (called the RTC – or real-time clock) most operating systems do not rely on this clock.  Instead they read the time from this clock once (when they boot) and then they use their own internal routines to calculate how much time has passed.

The problem is that these internal routines make assumptions about how the underlying hardware behaves (how frequently interrupts are delivered, etc…) and these assumptions do not account for the fact that things are different inside a virtual machine.  The fact that multiple virtual machines need to be scheduled to run on the same physical hardware invariably results in minor differences in these underlying systems.  The net result of this is that time appears to drift inside of virtual machines.

UPDATE 11/22: One thing that you should be aware of here: the rate at which the time in a virtual machine drifts is affected by the total system load of the Hyper-V server.  More virtual machines doing more stuff means time drifts faster.

In order to deal with time drift in a virtual machine – you need to have some process that regularly gets the real time from a trusted source and updates the time in a virtual machine.

Hyper-V provides the time synchronization integration services to do this for you.  The way it does this is by getting time readings from the management operating system and sending them over to the guest operating system.  Once inside the guest operating system – these time readings are then delivered to the Windows time keeping infrastructure in the form of an Windows time provider (you can read more about this here: http://msdn.microsoft.com/en-us/library/bb608215.aspx).   These time samples are correctly adjusted for any time zone difference between the management operating system and the guest operating system.

Problem #2 – Saved virtual machines / snapshots have the wrong time when they are restored.

When we restore a virtual machines from a saved state or from a snapshot we put back together the memory and run state of the guest operating system to exactly match what it was when the saved state / snapshot was taken.  This includes the time calculated by the guest operating system.  So if the snapshot was taken one month ago – the time and date will report that it is still one month ago.

Interestingly enough, at this point in time we will be reporting the correct (with some caveats) time in the systems RTC.  But unfortunately the guest operating system has no idea that anything significant has happened – so it does not know to go and check the RTC and instead continues with its own internally calculated time.

To deal with this the Hyper-V time synchronization integration service detects whenever it has come back from a saved state or snapshot, and corrects the time.  It does this by issuing a time change request through the normal user mode interfaces provided by Windows.  The effect of this is that it looks just like the user sat down and changed the time manually.  This method also correctly adjusts for time zone differences between the management operating system and the guest operating system.

Problem #3 – There is no correct “RTC value” when a virtual machine is started

As I have mentioned – physical computers have a RTC that operating systems look at when they first boot to get the time.  This real-time clock is backed by a small battery (you have probably seen the battery yourself if you have every pulled apart a computer).  Unfortunately virtual machines do not have any “batteries”.  When a virtual machine is turned off there is no component that keeps track of time for it.  Instead – whenever you start a virtual machine we take the time from the management operating system and put this into the real-time clock of the virtual machine.

This is done without the use of the Hyper-V time synchronization integration servers (it happens long before the integration services have loaded). 

The downside of this approach is that this does not take into account any potential time zone differences between the management operating system and the guest operating system.  The reason for this is that “time zones” are a construct of the software that runs in a virtual machine – and is not communicated to the virtual hardware in any way.  So – in short – when we start a virtual machine there is no way for us to know what time zone the guest operating system believes it is in.

One partial mitigation we have for this issue is that when the Hyper-V time synchronization component loads for the first time – it does an initial user mode set of the time to ensure that the time gets corrected as quickly as possible (using the same technique as discussed in problem #2).

So now that you understand how this all works – let’s discuss some common issues and questions around virtual machines and time synchronization.

Question #1 – I have a virtual machine that is configured for a different time zone to the management operating system.  Should I disable the time synchronization component of Hyper-V?

No, no, no, no, no, no, no.  And I say again – no.  As I have mentioned above – all time synchronization that is done by the Hyper-V time synchronization integration service is time zone aware.  If you disable the Hyper-V time synchronization integration service you will disable all the time synchronization aspects of Hyper-V that are time zone aware – and only leave the initial RTC synchronization active – which is not time zone aware.

This means that your virtual machines will go from booting in the wrong time zone, and then being corrected as soon as the Hyper-V time synchronization integration service loads to booting in the wrong time zone and staying in the wrong time zone.

Question #2 – Is there any way that I can stop Hyper-V from putting the wrong time in the RTC at boot?

In short; no.  We need to put something in there – and that is the best thing that we have to work with.

Question #3 – Can’t you use UTC time in the RTC so that the correct time is established when the virtual machine boots?

UTC (which is the computer techy version of saying GMT) time would solve this problem nicely with only one problem.  Windows does not support UTC time in the BIOS (Linux does).  So while this would solve the problem for our Linux running user base – the fact of the matter is that most of our users run Windows – and this would not work for them.

Question #4 – What about if I am using a different time synchronization source (e.g. domain time or a remote time server)?

Hyper-V time synchronization was designed to “get along well” with other time synchronization sources.  You should not need to disable Hyper-V time synchronization in order to use a different time synchronization source – as long as it goes through the Windows time synchronization infrastructure.

In fact – if you are running a Domain Controller inside a virtual machine I would recommend that you leave Hyper-V time synchronization enabled but that you also setup an external time source.  You can do this by going to this KB article: http://support.microsoft.com/kb/816042 and following the steps outlined in the “Configuring the Windows Time service to use an external time source” section.

UPDATE 11/22: I should have mentioned: since virtual machines tend to lose time much faster than physical computer, you need to configure any external time source to be checked frequently.  Once every 15 minutes is a good place to start.

Question #5 – How can I check what time source is being used by Windows inside of a virtual machine?

This is easy to do.  Just open an administrative command prompt and run “w32tm /query /source”.  If you are synchronizing with a remote computer – its name should be listed.  If you are using the Hyper-V time synchronization integration service you should see the following output:

image

If you see this output:

image

It means that there is no time synchronization going on for this virtual machine.  This is a very bad thing – as time will drift inside of the virtual machine.

Question #6 – Wait a minute!  My virtual machine should be synchronizing to the domain (or an external server) – but when I run that command it tells me that the Hyper-V time synchronization provider is being used!  How do I fix this!

I do not know why this happens – but sometimes it happens.  The first thing that you should do is to check that your domain does have a correctly configured authoritative time source.  There have been a small number of times when I have seen this problem being caused by the lack of an authoritative time source.

Alternatively – you can “partially disable” Hyper-V time synchronization.  The reason why I say “partially disable” is that you do not want to turn off the aspects of Hyper-V time synchronization that fix the time after a virtual machine has booted for the first time, or after the virtual machine comes back from a saved state.  No other time synchronization source can address these scenarios elegantly.

Luckily – there is a way to leave this functionality intact but still ensure that the day to day time synchronization is conducted by an external time source.  The key thing trick here is that it is possible to disable the Hyper-V time synchronization provider in the Windows time synchronization infrastructure – while still leaving the service running and enabled under Hyper-V.

To do this you will need to log into the virtual machine, open an administrative command prompt and run the following commands:

reg add HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\VMICTimeProvider /v Enabled /t reg_dword /d 0

This command stops W32Time from using the Hyper-V time synchronization integration service for moment-to-moment synchronization.  Remember from earlier in this post that we do not go through the Windows time synchronization infrastructure to correct the time in the event of virtual machine boot / restore from saved state or snapshot.  So those operations are unaffected.

w32tm /config /syncfromflags:DOMHIER /update

This command tells Windows to go and look for the best time source in the domain hierarchy.  If you want to use an external time server instead you can use the commands found here: http://technet.microsoft.com/en-us/library/cc784553(WS.10).aspx

net stop w32time & net start w32time

w32tm /resync /force

These two commands just “kick the Windows time service” to make sure the settings changes take effect immediately.

w32tm /query /source

This final command should confirm that everything is working as expected.

When you run these commands you should see something like this:

image

Question #7 – I have a virtual machine that has gotten ahead of time, and it never gets corrected back to the correct time.  What is going on here?

As a general rule of thumb, when time drifts inside a virtual machine it runs slower than in the real world, and the time falls behind.  We will always detect and correct this.

However, in the past, we have had reports of software problems caused when the Hyper-V time synchronization integration service decides to adjust the time back – because it believes the virtual machine is ahead of time.  To deal with this (rare) issue – we put logic in our integration service that will not change the time if the virtual machine is more than 5 seconds ahead of the physical computer.

UPDATE 11/22: I was asked how having the virtual machine in a different time zone to the Hyper-V server would affect this.  The short answer is that it does not.  The 5 second check is done after we have done the necessary time zone translation.

Question #8 – When should I disable the Hyper-V time synchronization service (either in the virtual machine settings, or inside the guest operating system)?

Never.

There are definitely times when you will want to augment the functionality of the Hyper-V time integration services with a remote time source (be it a domain source or an external time server) but the only way to get the best experience around virtual machine boot / restore operations is to leave the Hyper-V time integration services enabled.

Hopefully you will all find this information useful Smile

Cheers,
Ben

Leave a Comment
  • Please add 4 and 1 and type the answer here:
  • Post
  • [Disclaimer: I am working for a hardware vendor selling computer clocks, e.g. for PCI / PCIe bus]

    I wonder why Hypervisors do not include the possibility to allow their VMs to access a hardware time source during runtime. The RTC is one choice, but since it only has a 1s resolution it could probably only be a last resort time source. Using a dedicated hardware clock [see disclaimer], offered by some vendors, and allowing VMs to access this device either directly or indirectly would resolve a lot of timing related issues that our customers report to us.

    Maybe redirecting the system calls to read the current time to a function that reads the hardware clock would already help, or integrating this possibility into the Hypervisor-specific utilities running inside the VMs (like the Hyper-V time sync service).

    Regarding the UTC/Localtime problem with the RTC: Why don't you allow people to specify in which time zone the RTC for a specific VM is operating? That way you could set the initial time correctly for each VM.

    Regards,

     Heiko

  • Ben,

     Premier Support tells us to just uncheck the box.  Your article says otherwise.

     We tried setting the registry key to '0' in the guests.  But, even after rebooting (or simply restarting the Time Services), the time still syncs to the Host.  We've checked more than once. No Group Policies, either. (Host: 2008 R2 SP1,  Guest: 2003 R2)

     Are you sure this key does what it is supposed to do? We see an immediate effect when unchecking the box.  No effect when changing the guest's "Enabled" key to 0.

    Advice? Comments?

    Thanks,

    - Ryan P

  • Have you any thoughts on what to do about the time source for a Domain Controller on an "Internal only" virtual network?  I can't sync to an external time source because there isn't one on the internal network.  I've tried turning off w32time sync'ing by setting HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\Type to NoSync but after a bit of indecision, it tells me Event ID 144: "The time service has stopped advertising as a good time source."

  • I can confirm the post of Ryan P.

    I have a Server 2008 R2 as Hyper-V host running a Server 2008R2 guest. Only disabling the time sync in the integration services stops the guest from sync-ing time with the host. Setting the VMICTimeProvider\Enabled to '0' doesn't seem to work (not even after a reboot of the guest).

    I saw the guest concerned sync-ing time with an external source every 80 seconds, only to be "corrected" back by the host.

    Maybe something changed in R2?

  • Correction to my previous post (Jun 30 11:15): Setting the "Enabled" registry value to 0 seems to work as explained for servers that are not time servers themselves (i.e. in my case Domain Controllers). It definitely doesn't work on servers that are supposed to sync with an external clock and that function as time servers themselves.

  • If we had a virtual server as a domain controller and we were to have small application running to sync to the time in the virtual server would we see something like the whole system cripping forward in time like 7 minutes over 2 weeks?

  • Hi Ben!

    Is there a way to disable time sync *for a single machine* and set the timer of this machine to a special date / time?

    I need to run an old server image and set it's time to 2009.

    Thank you!

    Regards

    Ingo

  • It seems to me that there are mixed messages from microsoft on this topic which is never good, as that tends to mean that the best solution depends on your environment. However if you read various MS and MVP articles you will certainly be thinking what is the correct answer

    support.microsoft.com/.../976924 , blogs.msdn.com/.../time-synchronization-and-domain-controller-vm-s.aspx etc etc

    So what is the current recommendation from Microsoft on this topic ? Can we have an update as I have found mixed success using Integration Services and External time sources, in fact at one site we had MS Support connected in for three days trying to get it to work.

  • Your link under Problem #1, update 11/22 includes the ending parenthesis, making it an invalid link.

  • What do you reccommend as the best way to configure a Linux VM on hyperv so that they time is in sync and correct. Our Linux VM time advances over time. We are a microsoft shop so we use hyperv however there are instances where we need to use a linux vm

    Any suggestions you have would be greatly appreciated.

    Thanks in advance

  • Hi Ben - reg key fix doesnt work. Instructions followed exactly.

    I've configured my time source externally to a list of pool.ntp.org servers. When using /resync /force it sets the time correctly, but then about a second later the host machine overwrites it.

    Hopefully I can find a fix as I dont want to disable the integration service, but looking like thats the only way to get this going.

  • Thank you for your article... We are getting constant barrage of system event logs about every 30 seconds that the time has been updated in our VMs .... Am I correct to assume that this is normal?

    Thanks!

  • Does Hyper V 2012 resolve the time issues? Specifically, using a virtual domain controller (windows 2008r2) on Hyper V we have had to disable Hyper V time synchronization services and resort to third party time keeping software. We were consistently dealing with wide variations in time and resulting in security policy failures and etc. We have a Hyper V host that gets time from an ntp server and the PDC does as well. Disabled HyperV time sync and installed third party time keeper on all hosts and VMs.

  • Ben, the Microsoft recommendation for time keeping of DC's on Hyper-V seems to be changed again?

    technet.microsoft.com/.../virtual_active_directory_domain_controller_virtualization_hyperv(WS.10).aspx updated October 24, 2012:

    Note: This guidance has been recently updated to reflect the current recommendation to synchronize time for the guest domain controller from only the domain hierarchy, rather than the previous recommendation to partially disable time synchronization between the host system and guest domain controller.

    There was always a lot of discussion around time keeping of DC's on Hyper-V and partially disabling time services seemed to be the solution, but now again several articles conflict with each other.

  • Hi Ben, great article, still proving useful over 2 years on!

    We have just moved 2 DCs to HyperV VMs and your Question 6 helped fix the issue we were experiencing but I have a question about the update you added

    "UPDATE 11/22: I should have mentioned: since virtual machines tend to lose time much faster than physical computer, you need to configure any external time source to be checked frequently.  Once every 15 minutes is a good place to start."

    Our PDC Emulator is still a physical server that checks with an external NTP source. Does that update just apply to clients with external sources configured or Domain clients too?

    Do you mean I should change the frequency on all VMs, or just virtual DCs, or just the PDC emulator? If so, how exactly do I check the existing frequency and how do I modify it?

    Many thanks

    @thommck

Page 2 of 3 (41 items) 123