Home > Hyper-V R2, Linux, Microsoft, None-MS Operating Systems, Virtualisation and Storage > Timedrift with Linux Guest VM under Hyper-V R2

Timedrift with Linux Guest VM under Hyper-V R2


Recently had a Linux guest VM timedrifting considerably under Hyper-V R2. I contacted the Linux IC team, who responded stating there was a known issue and they had written a script to “work around” it 🙂

Below is the Linux Clock Sync script.

DISCLAIMER: I did not write this script, nor will I be able to support you on the use of it. Any issues should be sent to the support email address at the end of the script.

Linux Clock Sync

Version 0.5

Why do I need this script?

This script is targeted at users of Linux in Hyper-V virtual machines that are experiencing clock drift in the virtual machine.

Script Overview:

This script will sync the software clock within the Linux VM with the hardware clock that’s projected into the virtual BIOS every 15 seconds.

Supported Linux Distributions:

The following distributions are supported:

· SUSE Linux Enterprise Server 10 SP2

· Red Hat Enterprise Linux 5.2, 5.3, and 5.4

Requires:

This package requires the ntp package be installed.

Installation Instructions:

1. Confirm ntp package is installed. As root, execute the following command:

[root@linux ~]# which ntpdate

/usr/sbin/ntpdate

2. Create a directory to store the script:

[root@linux ~]# mkdir /etc/clocksync

3. Create the script in the directory:

[root@linux ~]# vi /etc/clocksync/clocksync.sh

4. Copy and paste this script into the VM. Replace the NTPSERVER variable with the fully qualified domain name or IP address of the NTP server.

#!/bin/bash

#######################################

# SIMPLE BASH SCRIPT TO CONTROL DRIFT

#######################################

# Copyright (c) Microsoft Corporation.

# All Rights Reserved.

#######################################

# Stop the NTP Daemon

if [ -f /etc/redhat-release ]

then

service ntpd stop >> /dev/null 2>&1

else

service ntp stop >> /dev/null 2>&1

fi

# Force Sync first time with NTP server

ntpdate -s -b NTPSERVER

ntpdate -s -b NTPSERVER

ntpdate -s -b NTPSERVER

# Sync hw clock with System clock

hwclock –systohc

i=0

while true

do

# Keep Syncing system clock with h/w clock

hwclock –hctosys

# The time interval for which this script sleeps and force updates time

sleep 15

i=`expr $i + 1`

# Sync Hwclock against NTP after 60 sec

if [ "$i" -eq 4 ]; then

ntpdate -s -b redmond.corp.microsoft.com

hwclock –systohc

i=0

fi

done

5. Save the file.

6. Change the permissions on the file:

[root@linux ~]# chmod 755 /etc/clocksync/clocksync.sh

7. Add a reference to the script in /etc/rc.d/rc.local, to ensure the syncing occurs when the VM reboots. Open the file, and add the following line to the end of the file:

sh /etc/clocksync/clocksync.sh &

8. Start the script:

[root@linux ~]# sh /etc/clocksync/clocksync.sh &

Uninstallation Instructions

To stop the clock syncing, issue the following command:

[root@linux ~]# ps ax | grep clock*

root 26243 0.0 0.0 4480 1032 pts/2 S 09:45 0:00 /bin/bash ./clocksync.sh

This will return the process ID of the script. Then, issue the following command:

[root@linux ~]# kill -10 <PROCESSID>

To remove this functionality, remove the script (/etc/clocksync/clocksync.sh) and the reference from /etc/rc.d/rc.local.

Support

Any issues with this script should be sent to lnxisfb@microsoft.com.

Advertisements
  1. Roy Chan
    February 9, 2010 at 3:35 pm
    • February 17, 2010 at 6:24 pm

      Would depend on how often the NTP would run a refresh.

      What the script technically does is call upon NTP to force a refresh every 15 seconds, to stop the drift from happening.

      Not too far from what VMware use in Linux from what I have been told 🙂

  2. Matt
    March 29, 2010 at 12:17 pm

    Ugh, what an ugly hack. This means that time is no longer monotonic increasing and you can’t rely on your logs being accurate. KVM and Xen are able to emulate a hardware clock properly (tsc, acpi, and cmos rtc). What is stopping MS from doing the job properly? The jitter and drift on HyperV is a woe to behold.

    • March 29, 2010 at 2:21 pm

      Telll me about it – I laughed so hard when I was given it. And before that I laughed so hard at Hyper-V not supporting SMP kernels for their IC tools.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: