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

9

7

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.

Martin K

Posted 2014-04-17T13:26:56.720

Reputation: 191

Windows itself might also be configured to synchronize time against an internet time server. – Thorbjørn Ravn Andersen – 2016-10-04T14:44:48.067

With "it does not seem to do anything", do you mean "the system time&date gets synchronized anyway with the host"? – Sopalajo de Arrierez – 2014-04-17T18:56:25.563

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. – Sopalajo de Arrierez – 2014-04-17T18:57:27.830

Answers

14

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

Madwyn

Posted 2014-04-17T13:26:56.720

Reputation: 241

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 – 2019-07-08T23:15:59.703

5

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.

Onur Turhan

Posted 2014-04-17T13:26:56.720

Reputation: 151

For users who have a linux host, I've created a bash script based on this answer which you can find here: https://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.

– Norman Breau – 2018-12-20T14:12:07.170

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.

Bhishmar

Posted 2014-04-17T13:26:56.720

Reputation: 19

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.

Der Hochstapler

Posted 2014-04-17T13:26:56.720

Reputation: 77 228

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 – 2014-04-23T08:32:05.267

Please refer to the documentation about that command: http://www.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.

– Der Hochstapler – 2014-04-23T10:20:20.840

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 – 2014-04-24T15:53:50.303