How to correctly sync time in linux running as virtual guest after host resumes from hibernation?

19

5

I use multiple virtual boxes for development and correct time and date is important for me when I push git commits. I do not shut down my host - it runs for months without restarting. I only hibernate it for nights and weekends. This introduces weird issue to my guest operating systems (all debians from turnkeylinux.org) - after some time, the date of guest operating systems is off by few days.

Although all guests use NTP it does not work if it detects that date is off by some large values (few hours seems to be enough). Is there any way to force NTP sync even for large time differences? Or any other solution that would restore correct date and time on guest boxes after host resumes from hibernation?

SiliconMind

Posted 2013-12-13T20:49:06.030

Reputation: 601

1

See this answer for details - basically the guest additions provide for direct clock sync without the need for NTP: http://superuser.com/questions/463106/virtualbox-how-to-sync-host-and-guest-time

– Pierz – 2016-06-30T10:25:54.200

Answers

17

In brief: Install (the ntp package and) the VirtualBox Guest Additions :)

  • Host: Windows 6.1 SP1, x64, 3GB
  • VirtualBox: v4.3.6
  • Guest: Debian 7.3, 32-bits, 768 KB

From VirtualBox, attach the file Oracle\VirtualBox\VBoxGuestAdditions.iso to the guest system (Settings/Storage) and as root

cd /media/cdrom0
sh ./VBoxLinuxAdditions.run

The VirtualBox Help at "9.4. Advanced configuration for Linux and Solaris guests" describes the steps but I had to install the package linux-headers-3.2.0-4-486, first.
Then, as root,

/usr/lib/VBoxGuestAdditions/vboxadd setup
/usr/lib/VBoxGuestAdditions/vboxadd-service setup
/usr/lib/VBoxGuestAdditions/vboxadd setup

Afterwards, reboot the guest.

I've checked by quitting the guest system (Alt+F4, then order VirtualBox to save the state of the guest), waiting 5 minutes, then restarting the guest system
At first, the clock was restored from its previous value, but after a few seconds all went fine (clock synced)

Pierre

Posted 2013-12-13T20:49:06.030

Reputation: 309

NTP is not required for guest additions clock sync - it is performed by the guest additions daemon (VBoxService). – Pierz – 2016-06-30T10:28:45.497

2Why do you suggest to repeat the first command twice? Is that really necessary? – RDP – 2016-10-08T14:35:16.063

@RDP <quote>Is it really necessary to repeat the first command twice?</quote> I don't remember, and documentation has changed see [https://www.virtualbox.org/manual/ch09.html#idm7495] – Pierre – 2016-10-16T13:19:13.717

You have to install gcc and make, too. Not having them manifests as an error starting the vboxadd service. But you don't get the helpful suggestion to install those packages unless you run vboxadd explicitly -- VBoxLinuxAdditions.run doesn't expose it. – Harry Tsai – 2017-03-16T12:49:11.517

As a matter of fact, that's what I ended up doing :) Just didn't had time to post the answer, so the credit goes to you. – SiliconMind – 2014-01-24T10:58:14.650

1

NTP server was not designed to run inside of a virtual machine. It requires a high resolution system clock, with response times to clock interrupts that are serviced with a high level of accuracy. NTP client is ok to run in some virtualization solutions. Run NTP on the base OS of the machine, and then have your various guest OSes take advantage of the good clock that is created on the system. Even that may not be enough, as there may be additional tools or kernel options that you need to enable so that virtual machine clients can adequately synchronize their virtual clocks to the physical system clock.

Source: Support NTP Known Issues

From that statement, and reading that your important part is git commit times, then it would be trivial to write a pre-commit hook to gather the time from your host machine, and you can get that simply by asking your host machine:

ssh hostmachine "date +%Y%m%d%H%M%s"

vgoff

Posted 2013-12-13T20:49:06.030

Reputation: 286

Thanks but that's not quite an answer :) I'm using VirtualBox and there are no options for time sync. So if not NTP then what? – SiliconMind – 2013-12-17T15:26:56.727

Sorry, I took the leap... but now with an edit clarified my thought. – vgoff – 2013-12-17T20:05:37.040