/sbin/hwclock --utc --systohc not working on AWS EC2 under Ubuntu

I'm in the process of moving some servers to AWS. One problem I'm encountering is that my script to synchronize the time using ntpdate and then write that time to the hwclock is failing. When the server reboots, I'd like the time to be close to accurate. However, I can't seem to adjust the hardware clock.

My script reports that the system time and the hardware clock differ by about half a second:

systime: 2018-01-03 05:51:58.529746-0500
hwtime: 2018-01-03 05:51:59.023462996-05:00
diff (sec): 0.493716955184937

I'd expect that running sudo /sbin/hwclock --utc --systohc would correct the problem. It certainly does on my traditional non-virtualized servers. However, when I run that command, I still see the same half second difference between the system time and the hardware clock.

My instance was created using ubuntu/images/hvm-ssd/ubuntu-artful-17.10-amd64-server-20171208 (ami-d53b3eb5). I'm getting the system time using date --iso-8601=ns and the hardware time using sudo /sbin/hwclock --show. I'm comparing them after parsing them with Perl's Date::Parse.str2time.

Is setting the hardware clock not supported in virtualized environments such as AWS? If that is the case, is there any workaround to ensure that the time remains synchronized when AWS servers reboot?

0
задан 3 January 2018 в 13:09
1 ответ

Как Sum1sAdmin говорит в комментариях, виртуальные машины не имеют аппаратных часов. Вы можете определить источник часов, проверив / sys / devices / system / clocksource / clocksource0 / current_clocksource .

Я изменил свой скрипт, чтобы проверить этот файл и синхронизировать только аппаратные часы с системой время на машинах, которые не являются виртуальными машинами XEN.

my $clocksource=`cat /sys/devices/system/clocksource/clocksource0/current_clocksource`;
chomp $clocksource;
if ($clocksource !~ /^xen$/){  # Can't adjust hardware clock on virtual machines
    my $hwdate=`/sbin/hwclock --show`;
    chomp $hwdate;
    $hwdate=~s/T/ /g;
    $hwdate=~s/,/./g;
    my $hwtime=str2time($hwdate);

    my $diff=abs($systime-$hwtime);

    if ($diff > $report_change_threshold_seconds){
        print "diff (sec): $diff, hwtime: $sysdate, systime: $hwdate\n";
        my $msg="the hardware clock to the current system time";
        my $cmd="/sbin/hwclock --utc --systohc";
        if ($test){
            print "Set $msg using:\n";
            print "  sudo $cmd\n";
        } else {
            print "Setting $msg\n";
            print `$cmd`;
        }
    }
}
0
ответ дан 5 December 2019 в 06:56

Теги

Похожие вопросы