Is it possible to get a Virtualbox VM always start from same time, if so how?

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.


Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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