11

I am trying to use VB to set up a Windows 7 VM for use in a training course. I want the virtual machine always to start up from the same conditions, in particular:

  1. The VM always starts at the same date and time
  2. The disk remains unchanged as a result of the previous exercise

I have worked out how to configure a disk as "immutable" which should take care of (2), but I can't get the VM to start at the same time. After some looking around I found this:

Once installed and started, the Virtualbox Guest Additions will try to synchronize the guest time with the host time. This can be prevented by forbidding the guest service from reading the host clock:

VBoxManage setextradata "VM name" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 1

I tried this with the VM shut down, and with it started up, but it does not seem to do anything. On the same documentation page there is also a mention of a parameter "--timesync-set-start" however I cannot see any mention of the format for setting date and time. Help would be welcome.

3
  • With "it does not seem to do anything", do you mean "the system time&date gets synchronized anyway with the host"? Apr 17, 2014 at 18:56
  • Should it be a solution for you to set time&date after Windows boot (a few seconds later, I mean)? A simple .cmd script should do that, I think. Apr 17, 2014 at 18:57
  • Windows itself might also be configured to synchronize time against an internet time server. Oct 4, 2016 at 14:44

5 Answers 5

21

You need to perform the following actions to make the guest time keeps what it was:

1.Disable the time sync of your virtual machine:

1.1 Disable Host to Guest Timesync

VBoxManage setextradata <YOUR_VM_NAME> "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 1

VBoxManage setextradata <YOUR_VM_NAME> "VBoxInternal/TM/TSCTiedToExecution" 1

To revert back:

VBoxManage setextradata <YOUR_VM_NAME> "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 0

1.2 Disable GuestAddition Timesync

Use the regedit.exe to modify the registry.

Find this branch: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\VBoxService

Change the value in ImagePath from

system32\VBoxService.exe

to

system32\VBoxService.exe --disable-timesync

Restart your VM or restart the service "VirtualBox Guest Additions Service".

2.Disable the time sync of Windows.

Check the time settings. Disable internet time sync.

Source: http://www.ppq.ch/snippets/15-vobxtime

1
  • 1
    Thanks. Your "1.2 Disable GuestAddition Timesync" is the only option that seemed to work for me. The host changes kept getting reverted by VirtualBox. I would take a look at the file after I started up the VM and the new extradata xml entries were removed.
    – Graymatter
    Jul 8, 2019 at 23:15
9

Following steps will work, if host machine is Windows/Linux.

  1. If "target machine" is Windows: Open "target virtual machine" and apply "Control Panel -> Date and Time -> Internet Time -> Change Settings -> Disable "Synchronize with an Internet time server"".

  2. Copy lines below as file "yourfilename.ps1" (Windows) / "yourfilename.sh" (Linux) on host machine . Edit this file as described in step 3. 4. 5.


HOST: WINDOWS

$vbox_path="C:\Program Files\ORACLE\VirtualBox"
$vm_name="Win7_VB"
$start_utc=1444176000

#DO NOT CHANGE ANYTHING BELOW THIS LINE
$now_utc_ms=[int64](([datetime]::UtcNow)-(get-date "1/1/1970")).TotalMilliseconds
$dif_utc_ms=$start_utc * 1000 - $now_utc_ms
Write-Host "START UTC:" $start_utc*1000 " DIF UTC:" $dif_utc_ms " NEW UTC:" $now_utc_ms

cd $vbox_path
VBoxManage setextradata $vm_name "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "0"
VBoxManage setextradata $vm_name "VBoxInternal/TM/TSCTiedToExecution" 1
VBoxManage modifyvm     $vm_name --biossystemtimeoffset $dif_utc_ms
VBoxManage startvm      $vm_name
Start-Sleep -s 5

HOST: LINUX

#!/bin/sh
vbox_path="/usr/bin/"
vm_name="Win7_VB"
start_utc=1444176000

#DO NOT CHANGE ANYTHING BELOW THIS LINE
now_utc_ms=$(($(date +%s)*1000))
dif_utc_ms=$(($(($start_utc * 1000)) - $now_utc_ms))
echo "START UTC:" $(($start_utc * 1000)) " DIF UTC:" $dif_utc_ms "NOW UTC:" $now_utc_ms

cd $vbox_path
VBoxManage setextradata $vm_name "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "0"
VBoxManage setextradata $vm_name "VBoxInternal/TM/TSCTiedToExecution" 1
VBoxManage modifyvm     $vm_name --biossystemtimeoffset $dif_utc_ms
VBoxManage startvm      $vm_name
sleep 5
  1. Update "vbox_path" with VirtualBox installation path. (Host: Windows)
  2. Update "vm_name" with the name of VirtualBox Image which will be started.
  3. Update "start_utc" using with http://www.epochconverter.com/. Target machine will start always with "start_utc" time. Epoch Converter
  4. Open "Windows PowerShell" from Start Menu as Administrator. (Host: Windows)
  5. Run following command and answer question as [Y]. (Host: Windows)

    Set-ExecutionPolicy RemoteSigned
    
  6. Run with right click on "yourfilename.ps1" and select "Run with PowerShell" to start target machine. (Host: Windows)

  7. Start a Linux Terminal execute command belows to start target machine (Host: Linux)

    chmod +x yourfilename.sh && sh yourfilename.sh
    

Note: Always use step 8 (Host: Windows) / step 9 (Host: Linux) to run target virtual machine.

1
  • 1
    For users who have a linux host, I've created a bash script based on this answer which you can find here: gist.github.com/breautek/be83414034b973d53691645d6e178bb4 Just save it as startvm.sh, chmod +x startvm.sh and use ./startvm.sh to run it. Will probably work on a mac host as well, but I haven't tested it. Dec 20, 2018 at 14:12
3

Addendum to the answer of Onur Turhan:

With Windows 10 host, I had to add .\ for the four VBoxManage in the powershell script to make it work:

.\VBoxManage setextradata $vm_name "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "0"
.\VBoxManage setextradata $vm_name "VBoxInternal/TM/TSCTiedToExecution" 1
.\VBoxManage modifyvm     $vm_name --biossystemtimeoffset $dif_utc_ms
.\VBoxManage startvm      $vm_name
1

To answer the OP's (MartinK) original Question: Is it possible to get a Virtualbox VM always start from same time, if so how?

It is possible simply by

(a) Setting the "GetHostTimeDisabled" property =1, using VBoxManage command at command line (in offline mode, i.e before starting the VM)

(b) Start a VM, from a Saved SnapShot taken before. (at the desired start time)

No more commands is required, every time this works. (tested for Windows Guest in Windows Hosts)

This answer is closer to the earlier answer by Oliver Salzburg, except he missed the "GetHostTimeDisabled" parametersetting. OfCourse Internet Time Sync (NTP) should be disabled, in the saved snapshot.

0

Use snapshots.

  1. To create a new snapshot, just use the Take Snapshot button in the Snapshots overview:

    enter image description here

  2. Name the snapshot and save it.

  3. When you want to boot the fixed state at a later time, revert to your previous snapshot with the Restore Snapshot button:

    enter image description here

If you want to make sure that your snapshot always has the same date, you'd probably want to disable the internet time synchronization in Windows by unchecking the Synchronize with an Internet time server checkbox in the clock configuration dialog.

4
  • That is an excellent solution and fixes the problem for me, I think. It seems then that if I use a snapshot I don't need to bother about making the disk "immutable" - am I right? Also, although I don't need this to solve my problem, I am curious as to the purpose of the VBoxManage commands that I mentioned in my previous post (and how to get them to work)
    – Martin K
    Apr 23, 2014 at 8:32
  • Please refer to the documentation about that command: virtualbox.org/manual/ch09.html#disabletimesync I don't see what benefit an immutable disk would have or what it actually means in this context. Apr 23, 2014 at 10:20
  • As per my original post, I have already looked at the documentation you refer to and tried to get it to work - however it did not do anything. I must be missing something - but what?
    – Martin K
    Apr 24, 2014 at 15:53
  • 1
    Misleading answer, this is not how snapshots work. They restore the state of the VM, yes, but then the OS in the VM starts running, and one of its task is to check if the time is right. Which won't be. Then the OS will try to sync the time. But before all of these, in the startup phase, Virtualbox will check if there is a time drift within the VM compared to the Host (don't forget, VM has BIOS. which has time). Then it will sync the time with the Guest and by the time the VM starts, it has the proper time. So for a VM remain at the same time, both time syncing features have to be disabled.
    – karatedog
    Apr 10, 2021 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .