SiKing

September 19, 2008

Time warpin’ with Sun xVM VirtualBox

Filed under: virtualization — SiKing @ 3:42 pm
Tags:

Got a new job, got a new machine, got a new toy! 😉 I am currently working on testing something that requires me messing around with system time; specifically what will happen during the time changes due to summer daylight saving time. Since I have a history of messing around with things I should not be messing with and irrevocably screwing up machines, I thought it would be best to do my messing in a virtual machine. It’s the first time I really had a chance to play around with one of these.

Sun VirtualBox, while a VM is running, holds on to everything. If you change the system time, normally within a minute the time will return back to what it should be. Not only that, I actually needed to change the BIOS time as well as the system time. Although the VirtualBox has a pretty GUI, it is rather limited when it comes to the advanced functions. The BIOS time can only be modified from the command line.

VBoxManage modifyvm <uuid|name> -biossystemtimeoffset <msec>

The above one-liner is the entire documentation of this particular function! You have to do this while the VM is not running; in my case that means shutdown my system, shutdown the VM, modify the time, bring up the VM, bring up my system – all in all, about 20 minutes each time I do this. I immediately put in a request for more RAM. However, figuring out the milliseconds that you want to offset the time is a bit of a pain. ➡ At least it will take negative values if you need to go backwards.

I immediately came up with a few shortcuts. In case you’re wondering, this is all bash.

 vboxHtime () {
	# Advance VM BIOS time X hours
	msecs=$(( ${1:-0} * 60 * 60 * 1000 ))
	echo Advancing time ${msecs}ms
	VBoxManage modifyvm Vista -biossystemtimeoffset ${msecs};
 }

 vboxDtime () {
	# Advance VM BIOS time X days
	msecs=$(( ${1:-0} * 60 * 60 * 1000 * 24 ))
	echo Advancing time ${msecs}ms
	VBoxManage modifyvm Vista -biossystemtimeoffset ${msecs};
 }

 vboxWtime () {
	# Advance VM BIOS time X weeks
	msecs=$(( ${1:-0} * 60 * 60 * 1000 * 24 * 7 ))
	echo Advancing time ${msecs}ms
	VBoxManage modifyvm Vista -biossystemtimeoffset ${msecs};
 }

😐 So far, nothing spectacular. If you need to reset the time back to normal, just run any of these with no arguments; this is taken care of by the ${1:-0} bit. But I quickly found out that all these are not additive.

 vboxWDtime () {
	# Advance VM BIOS time x weeks plus y days
	msecs=$(( $(( ${1:-0} * 60 * 60 * 1000 * 24 * 7 )) + $(( ${2:-0} * 60 * 60 * 1000 * 24 )) ))
	echo Advancing time ${msecs}ms
	VBoxManage modifyvm Vista -biossystemtimeoffset ${msecs};
 }

Still a pain in the ass to figure out how many milliseconds are between now and any given point in time.

A big Thank You goes out to ripat for inspiring the following.

 vboxdate () {
	# Adjust VM BIOS time to a given date
	secs=$(date --date "$*" +%s)    # convert supplied date to secs, since 1970-01-01
	let secs-=$(date +%s)           # subtract the date NOW
	msecs=$(( $secs * 1000 ))       # convert to millis
	echo Adjusting time ${msecs}ms
	VBoxManage modifyvm Vista -biossystemtimeoffset ${msecs};
 }
Advertisements

4 Comments »

  1. I guess I did not make myself clear. My initial problem was: programmable way to calculate the number of ms between now – “now” is different every day that I go to work – and a particular date that I am interested in – “particular date” is different depending on what I am testing at the moment. When I am doing this I can’t be bothered, in fact having a computer sitting in front of me, I should not have to be bothered to figure out how many days, minutes, seconds and whatever is between those two points.

    Comment by siking — November 8, 2008 @ 2:54 pm | Reply

  2. My solution for this problem would be Google.
    Just type in “2 years 2 months 3 days in ms” and it will calculate it for you!

    Comment by Florian — November 8, 2008 @ 1:49 pm | Reply

  3. Yea right, in my old age I’m gonna learn how to code. The last time I did that was in school, more than a decade ago. 😆

    Comment by siking — September 21, 2008 @ 8:43 pm | Reply

  4. As I read, VirtualBox is opensource, so why don’t you hack it in source code ? 😀

    Comment by pavrda — September 19, 2008 @ 11:15 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: