Можно ли настроить NTP для загрузки времени из файла, если он не может загружаться из сети?

373
metasoarous

Я посмотрел на документацию, но не могу найти ничего подобного. Ситуация такова, что у меня есть BeagleBone Black, у которого нет настоящих часов. По умолчанию компьютер загружается, думая, что это 1999 год (что отлично подходит для вечеринок, но ...). NTP исправляет это, но только если сеть подключена. В противном случае, это снова 1999 год. И, к сожалению, я нуждаюсь в устройстве в месте, где оно не может получить доступ к Интернету, а также необходимо регистрировать время, которое имеет хоть какой-то смысл.

Мне пришло в голову, что для моих целей было бы хорошо, если бы был способ заставить NTP читать время из файла, если сеть была недоступна. Я предполагаю, что файл времени будет записан либо как задание cron, либо как нечто внутри самого NTP, которое поддерживает состояние этого файла.

Is there anything like this for NTP? Are there any alternative tools that would accomplish what I need? In particular that wouldn't be too much of a pain to set up on the BBB?

3
Надо было озаглавить это "Как я могу остановить мой Beaglebone от вечеринок, как будто это 1999 год?" Michael Frank 9 лет назад 2

2 ответа на вопрос

4
dfc

You want the fake-hwclock package. It is specifically designed for situations like this. Description from the Debian package page:

fake-hwclock: Save/restore system clock on machines without working RTC hardware

Some machines don't have a working realtime clock (RTC) unit, or no driver for the hardware that does exist. fake-hwclock is a simple set of scripts to save the kernel's current clock periodically (including at shutdown) and restore it at boot so that the system clock keeps at least close to realtime. This will stop some of the problems that may be caused by a system believing it has travelled in time back to 1970, such as needing to perform filesystem checks at every boot.

On top of this, use of NTP is still recommended to deal with the fake clock "drifting" while the hardware is halted or rebooting.

2
Paul

You could have a cronjob that ran every few minutes like this:

date +%s > /root/lasttime 

Then have another cronjob (assuming you have a cron that understands advanced time settings, otherwise in a bootup script):

@reboot date +%s -s `cat /root/lasttime` 

The first stores the time in seconds since epoch into /root/lasttime, then the reboot one reads the time from the file and sets the date.

So your time will be wrong by the amount of time the beagle was turned off. You may have a race condition where the first cron job might execute before the second, and so set the time to 1999, so it is best to make it infrequent or put a check in to make sure the time makes sense before storing it.

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