Monday, October 22, 2012

Disabling Time Sync in VirtualBox

Have you ever need to control the date or time on one of your VirtualBox Guests? Typically, the Guest OS gets the date and time from the Host.

Time Synced

Usually, this is fine. However, there are times when you may wish to control how the Guest operates. This could be for a variety of testing reasons, particularly with Edge-Case Testing.

For example, Active Directory Authentication, Mirroring and Witnesses, Replication, Clustering, etc. In each of these applications, if the servers involved get beyond five minutes from one another, they may cease to operate normally.

By disabling time synchronization between the Host and the Guest, you will be able to move each servers' time backwards and forwards while you observe how they behave. Will your application continue to function? Will you be able to trap error messages and inform the user?

VBoxManage Utility

VirtualBox includes a command-line utility, VBoxManage, that will allow you to control every aspect of your Guest VMs.

On a Windows host you will find this in the following directory: C:\Program Files\Oracle\VirtualBox

VirtualBox Manage List VMs

This example lists all the VMs that you have defined on your VirtualBox installation.

.vbox Configuration Files

Before we dive into this utility, another piece we need to look at are the VirtualBox Machine Definition Files. These are XML file which control how your Guest VMs operate. Basically, when you use the GUI to setup and control your VM, all the various settings are stored here.

On my Host, these files are stored in C:\Users\<USERNAME>VirtualBox VMs

You will see one directory for each VM that you have defined. Within that folder you will see a file with the .vbox extension. Before we go too much further, go ahead and make a copy of that file. This way, if something happens while you are working with it, you can restore your VM's configuration.

Now, use Notepad to open the .vbox file and poke around a bit. Just be careful not to modify anything yet, that comes later. You should see something like this...

VirtualBox Configuration File

This is an XML file that contains all the configuration information for your VM.

Let's go take a look at a couple of sections and see what they do.

ExtraData

As you might guess, this section contains 'extra' data about your Guest VM.

Extra Data Section Before

BIOS

And this section contains information on how the virtual BIOS operates.

BIOS Section Before

OK, now that we've seen that, it's time to get to work.

Disable Time Synchronization

First, let's turn off Time Synchronization.

http://www.virtualbox.org/manual/ch08.html

VBoxManage SetExtraData "YOURVMNAME" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "1"

Note, if your VM has spaces in its name, then surround it with quotes.

If you want to turn Time Synchronization back on, use a '0'.

VBoxManage SetExtraData "YOURVMNAME" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "0"

If you don't remember the names of all your VMs, try this first.

OK, let get down to business.  To turn off Time Synchronization, use this command:

Disable Time Sync Command

Now, the Extra Data section of your .vbox file should look like this.

Extra Data Section After

So now when you start your VM, you will be able to change the Date and Time. Presumably, one could simply edit the .vbox file to make this change.

Time Offset

OK, but what if you would like your VM to start with a different time. That's easy to do, as well. For this step, there isn't a command-line option, so we need to edit the .vbox file manually.

Find the BIOS section, then edit the TimeOffset line with something similar...

<TimeOffset value="-123456789"/>

It should look like this when done.

BIOS Section After

The number represents milliseconds so adjust as necessary for your needs. Also, the number can be positive or negative.

Et voila! Your Guest VM now has a completely different time from your Host.

17 comments:

  1. Thank you Steven, it does work, very usefull!

    ReplyDelete
  2. Awesome, I'm glad it was able to help.

    Thanks for reading!

    ReplyDelete
  3. Hi! I tried to make it work in Virtualbox 4.3, but it doesn't work :( What version did you use in this tutorial? Thanks!

    ReplyDelete
  4. I'm sorry to hear you're having trouble. I was using version 4.2 at the time.

    However, now I'm using VM Ware so I don't have Virtual Box installed at the moment.

    What sort of errors are you getting?

    ReplyDelete
  5. Thanks for your very good description ;).

    ReplyDelete
  6. Thank you, I'm glad you liked it.

    ReplyDelete
  7. This was very helpful.
    I removed the quotes - vboxmanage setextradata MyVM VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled 1
    - then it worked in 4.3
    Compared .vbox before and after the command and noticed some odd extra characters.

    ReplyDelete
  8. Josh T,

    I thought the same was happening with my 4.3.x version of Virtual Box, however, shutting down and restarting the image confirmed the time sync disable does work. A restart is not enough. You have to actually shut the machine down and start it again.

    ReplyDelete
  9. Guide is not compatible with VBox 4.3.6

    ReplyDelete
  10. […] 按照 http://stevenormrod.com/2012/10/disabling-time-sync-in-virtualbox/ […]

    ReplyDelete
  11. I can't enter a number higher than -4456000000 in .... Why??? :(
    I want my time to be two years back

    ReplyDelete
  12. I can’t enter a number higher than -4456000000 in TimeOffset value="-4456000000 Why??? :(
    I want my time to be two years back

    ReplyDelete
  13. Hello,

    My VBox is 4.3.2 and I disable Timesync by adjusting the .vbox config file and adding following:



    So value 1 did not work for me, but "yes" does!

    ReplyDelete
  14. That's great! I'm glad it worked.

    ReplyDelete
  15. To be honest, I'm not sure. It could be a data limit issue. For example, a 32-bit integer will max out at 2,147,483,647.

    ReplyDelete
  16. That's entirely possible. Over time all programs change with new version.

    ReplyDelete