冻结一系列VM的时间和日期

时间:2014-06-07 09:24:43

标签: virtual-machine virtualbox

有没有办法冻结VM的时间和日期,以便它不与来宾BIOS和/或互联网同步?我已经看到一些解决方案,讨论在VM内部杀死服务,但我希望避免这种情况,因为它改变了VM的“干净状态”(用于测试目的)。

澄清一下:我不想设置VM的时间偏移量,我想设置在启动时传递给操作系统的确切时间。

从那里,有没有办法在大量虚拟机上执行此操作?

2 个答案:

答案 0 :(得分:1)

结束用python脚本解决问题。

要使用它,您可以编辑VM_NAMES列表以包含VirtualBox中显示的VM的名称,然后根据您希望发送VM的日期和时间设置RESET_TIME_VALUE。

如果您已在非默认位置安装VirtualBox,请同时修改VIRTUAL_BOX_MANAGE_PATH变量。

要运行,请调用main方法。

import datetime
import subprocess

VIRTUAL_BOX_MANAGE_PATH         = r"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe"

SET_EXTRA_DATA_COMMAND          = r"setextradata"
GET_HOST_TIME_DISABLE_COMMAND   = "\"VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled\" \"1\""

MODIFY_VM_COMMAND               = r"modifyvm"
BIOS_SYSTEM_TIME_OFFSET         = r"--biossystemtimeoffset"

# Edit this list to add more Virtual Machines
VM_NAMES                        = ("xxx",
                                   "yyy",
                                   "zzz") 

RESET_TIME_VALUE                = datetime.datetime(2014, 6, 7, 13, 0, 0, 0)                                

def main():
    for vm in VM_NAMES:
        reset_time(vm)
        disable_time_sync(vm)

def reset_time(vm_name):
    """
        Resets the VM to the clean install time
    """

    args = get_subprocess_args_set_bios_time(vm_name, RESET_TIME_VALUE)
    print("Resetting time on VM [" + vm_name + "] to " + str(RESET_TIME_VALUE) + " ...")

    subprocess.call(args)
    print("\tDone.")    

def disable_time_sync(vm_name):
    """
        Disables the time synchronization of a VM with the BIOS
    """

    args = [
                VIRTUAL_BOX_MANAGE_PATH,
                SET_EXTRA_DATA_COMMAND,
                vm_name,
                GET_HOST_TIME_DISABLE_COMMAND
           ]

    print("Disabling time synchronization on VM [" + vm_name + "] ...")

    subprocess.call(args)
    print("\tDone.")

def get_subprocess_args_set_bios_time(vm_name, datetime_to_set):
    """
        Returns a list containing the arguments to pass to the subprocess method
        to start the Virtual Box Manage program and set the BIOS time to the supplied value
    """

    return [
            VIRTUAL_BOX_MANAGE_PATH, 
            MODIFY_VM_COMMAND, 
            vm_name, 
            BIOS_SYSTEM_TIME_OFFSET, 
            str(get_msec_time_difference(datetime.datetime.now(), datetime_to_set))
           ]

def get_msec_time_difference(reference_point, check_point):
    """
        Computes the offset in msec from the reference point to the check point
    """

    return int(round( (check_point - reference_point).total_seconds() * 1000 ))

答案 1 :(得分:-1)

“要运行,请调用main方法。”

..或者只是在文件的末尾添加

if __name__ == '__main__':
    main()