VMware {code} Community
andrea1975
Enthusiast
Enthusiast

How to get the configuration of a running VirtualMachine

Hello,

I'm a beginner with the SDK and I'm trying to get the configuration of a running VirtualMachine.

I'm able to connect to the virtual center and to the following:

Dim myVM As New VMware.Vim.VirtualMachine(vmClient, New VMware.Vim.ManagedObjectReference("VirtualMachine-vm-1404"))

I'm sure that the object myVM is correctly set coz I'm able to command the VirtualMachine (for example I can issue a MyVM.ShutdownGuest that works perfectly).

I wish to retrieve the configuration of the virtual machine, particularly I wish to get virtual disks configuration, but here is my problem, if I look into the managed objects that are supposed to store the infos I need they are empty!

VirtualMachineConfigInfo.png

since I'm a beginner with the SDK I think I'm missing something...

Please help Smiley Happy

Reply
0 Kudos
2 Replies
ViswanathAngaja
Contributor
Contributor

Hi Andrea,

Please have a look at the method attached in the text file to collect VM disks information from the VirtualMachineConfigInfo object.

 

Thanks.

Viswanath

 

 

 

private static List<VMDisk> collectVMDiskData(String datacenterName, VMWareVMSummary vmstate, VirtualMachine vm, Folder rootFolder) throws Exception 
   {
           List<VMDisk> vdiskList = new ArrayList<VMDisk>();
 
           VirtualDevice[] vds = null;
 
           VirtualMachineConfigInfo vmConfigInfo = vm.getConfig();
 
           if ((vmConfigInfo != null) && (vmConfigInfo.getHardware() != null)) {
               vds = vmConfigInfo.getHardware().getDevice();
           }
 
           if (vds != null) {
               for (int i = 0; i < vds.length; i++) {
                   if (vds[i] instanceof VirtualDisk) {
                       VMDisk disk = new VMDisk();
                       VirtualDisk vDisk = (VirtualDisk) vds[i];
                       disk.setVmwareDatacenterName(datacenterName);
                       disk.setVmName(vm.getName());
                       disk.setVmIPAddr(vmstate.getVmIPAddr());
                       disk.setHostNode(vmstate.getHostNode());
                       disk.setKey(vDisk.getKey());
                       disk.setLabel(vDisk.getDeviceInfo().getLabel());
                       disk.setProvisionedSizeKB(vDisk.getCapacityInKB());
                       disk.setUnitNumber(vDisk.getUnitNumber());
 
                       if (vDisk.getBacking() != null) {
                           if (vDisk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) {
                               VirtualDiskFlatVer2BackingInfo backing = (VirtualDiskFlatVer2BackingInfo) vDisk.getBacking();
                               disk.setVirtualDiskType(
                                   "VirtualDiskFlatVer2BackingInfo");
                               disk.setDiskMode(backing.getDiskMode());
                               disk.setFileName(backing.getFileName());
 
                               if (backing.getSplit() != null) {
                                   disk.setSplit(backing.getSplit());
                               }
 
                               disk.setThinProvision(backing.getThinProvisioned());
 
                               if (backing.getWriteThrough() != null) {
                                   disk.setWriteThrough(backing.getWriteThrough());
                               }
 
                               disk.setUuid(backing.getUuid());
 
                               try {
                                   ManagedObjectReference dsMOR = backing.getDatastore();
                                   Datastore ds = (Datastore) MorUtil.createExactManagedEntity(rootFolder.getServerConnection(),
                                           dsMOR);
                                   disk.setDatastore(ds.getName());
                                   if(ds.getSummary().getMultipleHostAccess()!=null)
                                    disk.setDataStoreMultiHost(ds.getSummary().getMultipleHostAccess().booleanValue());
                               } catch (Exception e) {
                                   logger.error(
                                       "Exception while getting datastore name for vmdisk " +
                                       disk.getLabel() + " on vm " + vm.getName());
                               }
 
                               if (backing.getEagerlyScrub() != null) {
                                   disk.setEagerlyScrub(backing.getEagerlyScrub());
                               }
                           } else if (vDisk.getBacking() instanceof VirtualDiskRawDiskMappingVer1BackingInfo) {
                               // instance of raw device mapping
                               VirtualDiskRawDiskMappingVer1BackingInfo rawBacking = (VirtualDiskRawDiskMappingVer1BackingInfo) vDisk.getBacking();
                               disk.setVirtualDiskType(
                                   "VirtualDiskRawDiskMappingVer1BackingInfo");
                               disk.setDiskMode(rawBacking.getDiskMode());
                               disk.setFileName(rawBacking.getFileName());
                               disk.setUuid(rawBacking.getUuid());
                               disk.setCompatabilityMode(rawBacking.getCompatibilityMode());
 
                               try {
                                   ManagedObjectReference dsMOR = rawBacking.getDatastore();
                                   Datastore ds = (Datastore) MorUtil.createExactManagedEntity(rootFolder.getServerConnection(),
                                           dsMOR);
                                   disk.setDatastore(ds.getName());
                                   if(ds.getSummary().getMultipleHostAccess()!=null)
                                    disk.setDataStoreMultiHost(ds.getSummary().getMultipleHostAccess().booleanValue());
                               } catch (Exception e) {
                                   logger.error(
                                       "Exception while getting datastore name for vmdisk " +
                                       disk.getLabel() + " on vm " + vm.getName());
                               }
                           } else if (vDisk.getBacking() instanceof VirtualDiskSparseVer2BackingInfo) {
                               VirtualDiskSparseVer2BackingInfo backing = (VirtualDiskSparseVer2BackingInfo) vDisk.getBacking();
                               disk.setVirtualDiskType(
                                   "VirtualDiskSparseVer2BackingInfo");
                               disk.setDiskMode(backing.getDiskMode());
                               disk.setFileName(backing.getFileName());
 
                               if (backing.getSplit() != null) {
                                   disk.setSplit(backing.getSplit());
                               }
 
                               if (backing.getWriteThrough() != null) {
                                   disk.setWriteThrough(backing.getWriteThrough());
                               }
 
                               disk.setUuid(backing.getUuid());
 
                               try {
                                   ManagedObjectReference dsMOR = backing.getDatastore();
                                   Datastore ds = (Datastore) MorUtil.createExactManagedEntity(rootFolder.getServerConnection(),
                                           dsMOR);
                                   disk.setDatastore(ds.getName());
                                   if(ds.getSummary().getMultipleHostAccess()!=null)
                                    disk.setDataStoreMultiHost(ds.getSummary().getMultipleHostAccess().booleanValue());
                               } catch (Exception e) {
                                   logger.error(
                                       "Exception while getting datastore name for vmdisk " +
                                       disk.getLabel() + " on vm " + vm.getName());
                               }
                           } else if (vDisk.getBacking() instanceof VirtualDiskRawDiskVer2BackingInfo) {
                               VirtualDiskRawDiskVer2BackingInfo backing = (VirtualDiskRawDiskVer2BackingInfo) vDisk.getBacking();
                               disk.setVirtualDiskType(
                                   "VirtualDiskRawDiskVer2BackingInfo");
                               disk.setFileName(backing.getDescriptorFileName());
                               disk.setUuid(backing.getUuid());
                           } else if (vDisk.getBacking() instanceof VirtualDiskSparseVer1BackingInfo) {
                               VirtualDiskSparseVer1BackingInfo backing = (VirtualDiskSparseVer1BackingInfo) vDisk.getBacking();
                               disk.setVirtualDiskType(
                                   "VirtualDiskSparseVer1BackingInfo");
                               disk.setDiskMode(backing.getDiskMode());
                               disk.setFileName(backing.getFileName());
 
                               if (backing.getSplit() != null) {
                                   disk.setSplit(backing.getSplit());
                               }
 
                               if (backing.getWriteThrough() != null) {
                                   disk.setWriteThrough(backing.getWriteThrough());
                               }
 
                               try {
                                   ManagedObjectReference dsMOR = backing.getDatastore();
                                   Datastore ds = (Datastore) MorUtil.createExactManagedEntity(rootFolder.getServerConnection(),
                                           dsMOR);
                                   disk.setDatastore(ds.getName());
                                   if(ds.getSummary().getMultipleHostAccess()!=null)
                                    disk.setDataStoreMultiHost(ds.getSummary().getMultipleHostAccess().booleanValue());
                               } catch (Exception e) {
                                   logger.error(
                                       "Exception while getting datastore name for vmdisk " +
                                       disk.getLabel() + " on vm " + vm.getName());
                               }
                           } else if (vDisk.getBacking() instanceof VirtualDiskPartitionedRawDiskVer2BackingInfo) {
                               VirtualDiskPartitionedRawDiskVer2BackingInfo backing =
                                   (VirtualDiskPartitionedRawDiskVer2BackingInfo) vDisk.getBacking();
                               disk.setVirtualDiskType(
                                   "VirtualDiskPartitionedRawDiskVer2BackingInfo");
                               disk.setFileName(backing.getDescriptorFileName());
                               disk.setUuid(backing.getUuid());
                           } else if (vDisk.getBacking() instanceof VirtualDiskFlatVer1BackingInfo) {
                               VirtualDiskFlatVer1BackingInfo backing = (VirtualDiskFlatVer1BackingInfo) vDisk.getBacking();
                               disk.setVirtualDiskType(
                                   "VirtualDiskFlatVer1BackingInfo");
                               disk.setDiskMode(backing.getDiskMode());
                               disk.setFileName(backing.getFileName());
 
                               if (backing.getSplit() != null) {
                                   disk.setSplit(backing.getSplit());
                               }
 
                               if (backing.getWriteThrough() != null) {
                                   disk.setWriteThrough(backing.getWriteThrough());
                               }
 
                               try {
                                   ManagedObjectReference dsMOR = backing.getDatastore();
                                   Datastore ds = (Datastore) MorUtil.createExactManagedEntity(rootFolder.getServerConnection(),
                                           dsMOR);
                                   disk.setDatastore(ds.getName());
                                   if(ds.getSummary().getMultipleHostAccess()!=null)
                                    disk.setDataStoreMultiHost(ds.getSummary().getMultipleHostAccess().booleanValue());
                               } catch (Exception e) {
                                   logger.error(
                                       "Exception while getting datastore name for vmdisk " +
                                       disk.getLabel() + " on vm " + vm.getName());
                               }
                           }
                       }
 
                       vdiskList.add(disk);
                   }
               }
           }
 
           return vdiskList;
       }
Reply
0 Kudos
joshames
Enthusiast
Enthusiast

To answer your question more succintly, the method you used does not return all the properties.  If I were doing it, I would use VirtualMachine = myVM = (VirtualMachine)VimClient.GetView(vimclient, moref);

This will return a complete virtual machine object and then you can get whatever properites you needed.  Note using the Datastore property will only return the MOREF's of the datastore(s).

 

Josh

Reply
0 Kudos