7

I am running a few Ubuntu VMs on a MacBook Pro host.

Whenever I pause a VM or save its state to disk, the next time I restart it, the system clock goes out of sync.

Is there a way for the VM to know it has just been resumed, and run a command? (for example, running ntpdate -u time.nist.gov as root would do the trick)

I tried modifying the scripts in /etc/pm/sleep.d, but these don't appear to be called on VM suspend/resume.

mpontillo
  • 941
  • 1
  • 8
  • 24
  • For this particular case, shouldn't the Guest Additions do this automatically? – Mechanical snail Sep 09 '11 at 21:54
  • @Mechanical snail, actually you might be right. I don't remember if I installed the guest additions on these VMs. (never thought to do that because they don't run a GUI) You should make this an answer. – mpontillo Sep 09 '11 at 23:03
  • @Mechanical snail, good sign: I did `mkdir /media/cdrom ; mount /dev/cdrom /media/cdrom ; cd /media/cdrom ; ./VBoxLinuxAdditions.run` and it updated the guest additions from 4.0.10 to 4.0.12 *and* synchronized the clock. Good so far, but I guess I *did* have them installed before. (maybe there's a bug...) Now I'll test a save/resume cycle. – mpontillo Sep 09 '11 at 23:08
  • @Mechanical snail, clock sync now works. Thanks! If you make this an answer I'll accept it. – mpontillo Sep 09 '11 at 23:10

3 Answers3

6

For this particular use, the Guest Additions is supposed to synchronize the VM's clock automatically. (See "Time synchronization" at http://www.virtualbox.org/manual/ch04.html, or if you want to customize it, http://www.virtualbox.org/manual/ch09.html#changetimesync.) It's possible that this updating might have stopped working when using an older version of the guest additions than the VirtualBox version on the host.

To install or update the Guest Additions, mount the Guest Additions CD image using the relevant menu command on the host, and run the script from the CD on the guest. For details see the directions at http://www.virtualbox.org/manual/ch04.html#idp11850096.

Mechanical snail
  • 7,583
  • 5
  • 45
  • 66
2

The easiest solution is to install DKMS on the server, which syncs time and does a lot of other useful things. This works on my OSX system, running VirtualBox with an Ubuntu VM Guest.

All I needed to do was run this on my VM:

sudo apt-get install virtualbox-guest-dkms
sudo reboot

People may need different packages specific to their linux flavor.

Ubuntu DKMS Reference: https://help.ubuntu.com/community/DKMS

0

You can use the crontab within linux if you want, it's not a full proof fix but you can set it so it runs every 5 minutes if you want which will execute the command you need, so updating the time with the server.

This blog post shows how to use the crontab. Also you can look at the CronHowTo @ Ubuntu Documentation

It might be useful for you.

Sandeep Bansal
  • 6,354
  • 1
  • 29
  • 34
  • Yeah, I actually thought about using `crontab`. I think that might be the workaround I'll have to use. But I don't want it to run constantly. Only when it wakes up from sleep. The laptop doesn't have consistent network access, and I don't want it constantly probing for the time server. – mpontillo Sep 01 '11 at 00:10
  • It shouldn't matter so much, it wouldn't really be slowing down using the machine, as it is a service right – Sandeep Bansal Sep 01 '11 at 00:18
  • Well, there's the problem that it will try and fail to get a connection sometimes. But there's also the problem that I would have to wait the 5 minutes (or whatever) after I started up the VM to continue working. The reason I care is that I am running programs that are sensitive to timestamps, so I'd rather it be event-driven than be the guy who annoys NIST by pounding their server with an NTP request every 5 seconds. ;-) – mpontillo Sep 01 '11 at 01:23
  • I get what you mean, You could try what's listed in this article, http://rickguyer.com/virtualbox-disable-time-sync-between-host-and-client/ but with setting that value as 0 instead of 1? – Sandeep Bansal Sep 01 '11 at 01:29
  • Thanks, I tried it and it didn't work. (maybe it doesn't work on OS X hosts?) – mpontillo Sep 01 '11 at 02:00
  • Oh VBoxManage is a Windows command, I'll research a bit more and see if I can find a solution – Sandeep Bansal Sep 01 '11 at 12:49
  • VBoxManage exists on the OS X version of VirtualBox. If I type `$ which vboxmanage` I get back `/usr/bin/vboxmanage`, and it works fine. It's just that time syncing settings don't appear to be fully supported. – mpontillo Sep 01 '11 at 15:27
  • Maybe use one of the [`anacron`](http://en.wikipedia.org/wiki/Anacron) variants instead. – Mechanical snail Sep 09 '11 at 21:56