Vmware 如何在另一个vHost上克隆当前状态的虚拟机

Vmware 如何在另一个vHost上克隆当前状态的虚拟机,vmware,vsphere,Vmware,Vsphere,我的应用程序应该自动将虚拟机迁移到同一vCenter中可用的另一个vHost,以防其vHost出现故障 此外,我应该使用冷迁移,以及最新的快照。 我正在使用以下代码迁移VM,但它引发异常无法连接远程主机。我怎样才能做到这一点 这是我的密码: try { Folder rootFolder = Connect.getServiceInstance().getRootFolder(); String cloneName=vmname+"_clone"; //

我的应用程序应该自动将虚拟机迁移到同一vCenter中可用的另一个vHost,以防其vHost出现故障

此外,我应该使用冷迁移,以及最新的快照。 我正在使用以下代码迁移VM,但它引发异常
无法连接远程主机
。我怎样才能做到这一点

这是我的密码:

try {
        Folder rootFolder = Connect.getServiceInstance().getRootFolder();
        String cloneName=vmname+"_clone";
    //  String AlternateHostName="130.65.133.21";

        VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);
    //  ResourcePool rp = (ResourcePool) new InventoryNavigator(rootFolder).searchManagedEntity("ResourcePool", "Resources");
        HostSystem targetHost = (HostSystem) new InventoryNavigator(rootFolder).searchManagedEntity("HostSystem",alternateHost); 

        //  HostSystem targetHost = (HostSystem) new InventoryNavigator(rootFolder).searchManagedEntity("HostSystem",A);
        //  ResourcePool rp = new ResourcePool(Connect.getServiceInstance().getServerConnection(),);

        ManagedEntity[] rps = new InventoryNavigator(rootFolder).searchManagedEntities(new String[][] {{"ResourcePool", "name" }, }, true);  

        //needs a datastore
        Datastore[] dc = vm.getDatastores();
        //    System.out.println("DataStore"+dc[0].getName());

        //configuration setup to relocate clone file on another vHost
        VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
        relocateSpec.setDatastore(dc[0].getMOR());
        relocateSpec.setHost(targetHost.getMOR());
        relocateSpec.setPool(rps[1].getMOR());


        //config setup to clone this VM using relocation specification above.
        VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
        cloneSpec.setSnapshot(vm.getCurrentSnapShot().getMOR()); // should be working issue here
        cloneSpec.setPowerOn(true);         
        cloneSpec.setLocation(relocateSpec);

        //cloneSpec.setMemory(true);


        Task task = vm.cloneVM_Task((Folder) vm.getParent(),cloneName, cloneSpec);
        System.out.println("Launching the VM clone task. " +"Please wait ...");

        String status = task.waitForTask();  // shall be waitfortask ??
        if(status==Task.SUCCESS)
        {
            System.out.println("VM got cloned successfully.");
        }
        else
        {
            System.out.println("Failure -: VM cannot be cloned");
        }`