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

时间:2014-03-28 18:31:41

标签: vmware vsphere

我的应用程序应该自动迁移虚拟机,以防vHost失败,可能转移到同一个vCentre中的另一个vHost。

此外,我应该使用最近的快照进行冷迁移。 我使用以下代码迁移VM,但它抛出异常Remote Host can not be connected。我怎么能实现这个???

这是我的代码:

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");
        }`

0 个答案:

没有答案