KISS

Keep It Simple Stupid

Sync time in VirtualBox OS X guest

| comments

Virtualization is great! It allows me to mess up with my Jenkins setup trying to figure out how to configure iOS jobs with tests (jumping through a number of hoops on the way, hi Apple!). One point though is that the guest OS X in VirtualBox has terribly slow graphics, and also freezes after some time. Only restart helps. Hopefully, I usually need ssh and jenkins web interface only. Read about how to set that up here: Jenkins in OSX guest in VirtualBox for iOS jobs – full setup guide.

Anyway, due to missing VBox additions on OSX guest, time doesn’t update when the machine is suspended and then resumed. Plus, there is a constant time drift. Explanations here: https://forums.virtualbox.org/viewtopic.php?f=22&t=48233.

My solution is to synchronize with NTP time server repeatedly. Linux distros have the famous cron daemon, and so does OS X, however it’s deprecated in favor of launchd. Create file at /Library/LaunchDaemons/org.virtualbox.updatetime.plist with the following contents:

(org.virtualbox.updatetime.plist) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>org.virtualbox.updatetime</string>
  <key>ProgramArguments</key>
  <array>
      <string>/usr/sbin/ntpdate</string>
      <string>-u</string>
      <string>pool.ntp.org</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>StartInterval</key>
  <integer>180</integer>
  <key>StandardOutPath</key>
  <string>/tmp/updatetime.out</string>
  <key>StandardErrorPath</key>
  <string>/tmp/updatetime.err</string>
</dict>
</plist>

The owner and group should be root:wheel, so you can run

1
sudo chown root:wheel /Library/LaunchDaemons/org.virtualbox.updatetime.plist

if you downloaded the file. It has to be a system daemon (indicated by its location in /Library/LaunchDaemons/) because updating system time requires the root privileges.

The daemon will run at system startup, and you can launch it right away:

1
sudo launchctl load /Library/LaunchDaemons/org.virtualbox.updatetime.plist

Now, the time is updated every three minutes automagically, which is especially important for Jenkins builds.

Comments