VMware Cloud Community
itianyi
Contributor
Contributor
Jump to solution

A component of the virtual machine is not accessible on the host !

My vmware version 5.1,.

when I  Create a virtual machine with cloneVm_task  by the Java API interface, suggest the following error :“A component of the virtual machine is not accessible on the host ”,。

who can tell me how to solve, thank you!

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
vThinkBeyondVM
VMware Employee
VMware Employee
Jump to solution

I could figure out this issue.

This is happening because of below reason:

1. The datastore that you have specified is shared across the datacenters (i.e. there are some host in both datacenter with shared datastore)

2. When this line gets executed "Datastore ds = (Datastore) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("Datastore",dsname ); " . It populates the managed entity "Datastore" with first occurrence of that datastore. It does not check the another entry of the same datastore in next datacenter.

3. Note that as per the current design, managed object reference that is is being created for the same shared "datastore"  are separate. Hence, when Clone operation tries to find the datastore ID based on Managed object reference in next datacenter, it could not find. Hence vCenter generates the error message.

4. Same code works fine if there is NO shared datastore.

How to solve this issue:

1. You will have to pass the datacenter managed entity object as well when you want to create clone in another datacenter on shared datastore.

2. Please modify below lines (Green font) in existing code :

VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine",snapshot);

  if (vm == null) {

  System.out.println("No VM " + snapshot + " found");

  } else {

  ManagedEntity datacenter=(Datacenter) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("Datacenter","DC-4");   //Pasing datacenter managed object

  Datastore ds = (Datastore) new InventoryNavigator(datacenter).searchManagedEntity("Datastore",dsname );

 

Now it should work fine in any case. Let me know if you need any help.


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

View solution in original post

Reply
0 Kudos
6 Replies
vThinkBeyondVM
VMware Employee
VMware Employee
Jump to solution

are you using VI JAVA?

Can you post complete code here?


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

Reply
0 Kudos
itianyi
Contributor
Contributor
Jump to solution

yes, I used VI JAVA!

My code is as follows:


public static void main(String[] args) throws Exception
{
System.out.println("time taken:" );
long start = System.currentTimeMillis();
ServiceInstance si = new ServiceInstance(new URL("https://10.24.5.10/sdk"), "administrator", "paddsasa", true);
long end = System.currentTimeMillis();
System.out.println("time taken:" + (end-start));
try {
String snapshot = "Temp_CentOS6.5-64";
String cpu = "2";
String memory = "2048";
String vncport = "5901";
String hcName = "IBM3850M2";
String dsname="ds47_2t_02";
VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine",snapshot);
if (vm == null) {
System.out.println("No VM " + snapshot + " found");
} else {

Datastore ds = (Datastore) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("Datastore",dsname );
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
// vmrs
VirtualMachineRelocateSpec vmrs = new VirtualMachineRelocateSpec();
ClusterComputeResource theHc = (ClusterComputeResource) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("ClusterComputeResource",hcName );
com.vmware.vim25.mo.Folder fder=getVmFolder(theHc.getParent());
System.out.println("aaa:" + fder.getName());
//vmrs.host = host.getMOR();
vmrs.pool = theHc.getResourcePool().getMOR();
vmrs.datastore = ds.getMOR();
vmrs.transform = VirtualMachineRelocateTransformation.sparse;
cloneSpec.setLocation(vmrs);
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
// cloneSpec.setCustomization(localCustomizationSpec);
VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
vmSpec.setMemoryMB(Long.parseLong(memory));
vmSpec.setNumCPUs(Integer.parseInt(cpu));
cloneSpec.setConfig(vmSpec);
Task task = vm.cloneVM_Task(fder, "gf-test"+StringUtil.getRandomStr(3),cloneSpec);
}

} catch (Exception e) {

e.printStackTrace();
}
si.getServerConnection().logout();
}

private static Folder getVmFolder(ManagedEntity parent) {
if (parent instanceof Datacenter){
try {
return ((Datacenter)parent).getVmFolder();
} catch (Exception e) {
return null;
}
} else {
return getVmFolder(parent.getParent());

}
}

Reply
0 Kudos
vThinkBeyondVM
VMware Employee
VMware Employee
Jump to solution

I tried the same code with 2-3 modification  in my environment and It worked fine: I could see VM clone is successful.

On below line, I have stored cluster name in "hcName" variable.

ClusterComputeResource theHc = (ClusterComputeResource) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("ClusterComputeResource",hcName );

Here is the code: You can take as it is and run , Please consider highlighted lines & modify.

package com.vmware.vim25.mo.Samples.cluster;  <You can use any package>

import java.net.URL;

import com.vmware.vim25.VirtualMachineCloneSpec;

import com.vmware.vim25.VirtualMachineConfigSpec;

import com.vmware.vim25.VirtualMachineRelocateSpec;

import com.vmware.vim25.VirtualMachineRelocateTransformation;

import com.vmware.vim25.mo.ClusterComputeResource;

import com.vmware.vim25.mo.Datacenter;

import com.vmware.vim25.mo.Datastore;

import com.vmware.vim25.mo.Folder;

import com.vmware.vim25.mo.InventoryNavigator;

import com.vmware.vim25.mo.ManagedEntity;

import com.vmware.vim25.mo.ServiceInstance;

import com.vmware.vim25.mo.Task;

import com.vmware.vim25.mo.VirtualMachine;

public class CreateVM {

  public static void main(String[] args) throws Exception

  {

  System.out.println("time taken:" );

  long start = System.currentTimeMillis();

  ServiceInstance si = new ServiceInstance(new URL("https://<VC IP>/sdk"), "<USERNAME>", "<PASSWORD>", true);

  long end = System.currentTimeMillis();

  System.out.println("time taken:" + (end-start));

  try {

String snapshot = "iorm-vm1"; ///This was my VM NAme

  String cpu = "2";

  String memory = "2048";

  String vncport = "5901";

String hcName = "Cluster-3"; //My cluster name

  String dsname="SSD1";

  VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine",snapshot);

  if (vm == null) {

  System.out.println("No VM " + snapshot + " found");

  } else {

  Datastore ds = (Datastore) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("Datastore",dsname );

  VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();

  // vmrs

  VirtualMachineRelocateSpec vmrs = new VirtualMachineRelocateSpec();

  ClusterComputeResource theHc = (ClusterComputeResource) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("ClusterComputeResource",hcName );

  Folder fder=getVmFolder(theHc.getParent());

  System.out.println("aaa:" + fder.getName());

  //vmrs.host = host.getMOR();

  vmrs.pool = theHc.getResourcePool().getMOR();

  vmrs.datastore = ds.getMOR();

  vmrs.transform = VirtualMachineRelocateTransformation.sparse;

  cloneSpec.setLocation(vmrs);

  cloneSpec.setPowerOn(false);

  cloneSpec.setTemplate(false);

  // cloneSpec.setCustomization(localCustomizationSpec);

  VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();

  vmSpec.setMemoryMB(Long.parseLong(memory));

  vmSpec.setNumCPUs(Integer.parseInt(cpu));

  cloneSpec.setConfig(vmSpec);

  Task task = vm.cloneVM_Task(fder, "gf-test"+"TestVM",cloneSpec);  //You can pass any random string as Name

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  si.getServerConnection().logout();

  }

  private static Folder getVmFolder(ManagedEntity parent) {

  if (parent instanceof Datacenter){

  try {

  return ((Datacenter)parent).getVmFolder();

  } catch (Exception e) {

  return null;

  }

  } else {

  return getVmFolder(parent.getParent());

  }

  }

}

Let me know if you need any other help


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

Reply
0 Kudos
itianyi
Contributor
Contributor
Jump to solution

My environment is: two data centers, one data center A with a cluster of A1, another data center B has three clusters: B1, B2, B3,.

using vSphere client 5.1, the virtual machine can create success in any cluster, but call the Java API, can only be created successfully virtual machine on A1.

Of course, the B data center under the complex environment,  with  virtual machine of version 5.1、5.0, and virtual desktop.

Reply
0 Kudos
vThinkBeyondVM
VMware Employee
VMware Employee
Jump to solution

I could figure out this issue.

This is happening because of below reason:

1. The datastore that you have specified is shared across the datacenters (i.e. there are some host in both datacenter with shared datastore)

2. When this line gets executed "Datastore ds = (Datastore) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("Datastore",dsname ); " . It populates the managed entity "Datastore" with first occurrence of that datastore. It does not check the another entry of the same datastore in next datacenter.

3. Note that as per the current design, managed object reference that is is being created for the same shared "datastore"  are separate. Hence, when Clone operation tries to find the datastore ID based on Managed object reference in next datacenter, it could not find. Hence vCenter generates the error message.

4. Same code works fine if there is NO shared datastore.

How to solve this issue:

1. You will have to pass the datacenter managed entity object as well when you want to create clone in another datacenter on shared datastore.

2. Please modify below lines (Green font) in existing code :

VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine",snapshot);

  if (vm == null) {

  System.out.println("No VM " + snapshot + " found");

  } else {

  ManagedEntity datacenter=(Datacenter) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("Datacenter","DC-4");   //Pasing datacenter managed object

  Datastore ds = (Datastore) new InventoryNavigator(datacenter).searchManagedEntity("Datastore",dsname );

 

Now it should work fine in any case. Let me know if you need any help.


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

Reply
0 Kudos
itianyi
Contributor
Contributor
Jump to solution

The problems have been solved, thank you very much!!

Reply
0 Kudos