Better Time Synchronization with VirtualBox Guest Additions

The time synchronization using the VirtualBox Guest Additions is too gradual by default. One of my machines drifted until there was a discrepancy of almost 20 minutes. Apparently, 20 minutes is the threshold at which a hard resync is performed. But this isn’t what I expected from the Guest Additions. I expected the guest system time to track the host system time precisely, and it didn’t seem like this was happening. I also don’t want gradual resynchronization, as it just takes too long to shift the clock back into a usable state.

When the clock is seriously skewed, building software goes awry, because the make command starts detecting filestamps from the future, and other weird things.

According to the VirtualBox Manual, you can tune the time synchronization parameters by either setting properties on the virtual machine configuration using vboxmanage, or by specifying the parameters to VBoxService itself. I chose the latter option, though I’m sure the former is probably easier.

The startup file that controls VBoxService on Ubuntu is /etc/init.d/vboxadd-service. It is possible to edit this file to force more frequent synchronization, by changing the line:

daemon $binary > /dev/null

to:

daemon $binary "--timesync-set-start --timesync-set-on-restore 1 --timesync-set-threshold 5000" > /dev/null

In addition, I pushed the niceness level of the task downwards, so that even under pressure, the VBoxService isn’t competing with normal tasks to do its job. In the same file, I changed the line:

start-stop-daemon --start --exec $1 -- $2

to:

start-stop-daemon --start --nicelevel "-5" --exec $1 -- $2

For the impatient: Running the VBoxService command by hand will also do an immediate clock-sync, along with generating some output you would rarely see:

/etc/init.d$ sudo /usr/sbin/VBoxService --timesync-set-start
VBoxService 4.3.6 r91406 (verbosity: 0) linux.amd64 (Dec 18 2013 16:45:21) release log
00:00:00.001752 main Log opened 2014-02-09T12:38:04.424360000Z
00:00:00.002533 main OS Product: Linux
00:00:00.002776 main OS Release: 3.8.0-35-generic
00:00:00.003015 main OS Version: #52~precise1-Ubuntu SMP Thu Jan 30 17:24:40 UTC 2014
00:00:00.003255 main OS Service Pack: #52~precise1-Ubuntu SMP Thu Jan 30 17:24:40 UTC 2014
00:00:00.003498 main Executable: /opt/VBoxGuestAdditions-4.3.6/sbin/VBoxService
00:00:00.003499 main Process ID: 1864
00:00:00.003500 main Package type: LINUX_64BITS_GENERIC
00:00:00.004203 main 4.3.6 r91406 started. Verbose level = 0

In my case, the guest clock will now drift no more than 5 seconds before a hard-resync with the host clock.