我正在把一些服务器转移到AWS。我遇到的一个问题是,我的脚本使用ntpdate同步时间,然后将时间写入hwclock是失败的。当服务器重新启动时,我希望时间接近准确。然而,我似乎无法调整硬件时钟。
我的脚本报告说,系统时间和硬件时钟相差大约半秒钟:
systime: 2018-01-03 05:51:58.529746-0500
hwtime: 2018-01-03 05:51:59.023462996-05:00
diff (sec): 0.493716955184937我预计运行sudo /sbin/hwclock --utc --systohc会纠正这个问题。当然,在我的传统非虚拟化服务器上也是如此。但是,当我运行该命令时,我仍然可以看到系统时间和硬件时钟之间的相同的半秒差。
我的实例是使用ubuntu/images/hvm-ssd/ubuntu-artful-17.10-amd64-server-20171208 (ami-d53b3eb5)创建的。我使用date --iso-8601=ns获取系统时间,使用sudo /sbin/hwclock --show获取硬件时间。我在将它们与Perl的Date::Parse.str2time解析后进行比较。
在诸如AWS这样的虚拟环境中,设置硬件时钟不受支持吗?如果是这样的话,有什么解决办法来确保AWS服务器重新启动时时间保持同步吗?
发布于 2018-01-07 10:37:18
正如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`;
}
}
}https://serverfault.com/questions/890498
复制相似问题