VMware {code} Community
IDPRESV
Contributor
Contributor

Which api is used to find the path to a VMs vmdk given only its name (or minimal information)

Hello,

I am searching for the proper API to find the absolute path to a Virtual Machines vmdk file.

Let’s say inputting the minimum amount of information (ideally just the Virtual machines

name) i would want to get the path like this:

"[ [storage1 ]] MailServer/SystemDisk.vmdk"

Can anyone point me to the right api or if they even know the methods/procedures that would be even better.

thank you

0 Kudos
7 Replies
admin
Immortal
Immortal

You might want to refer to the C# sample GetVMFiles.cs that is shipped with the vSphere SDK package. The sample accepts VM name and the path to store the VM files ( .vmdk, .vmx etc) as input arguements and downloads all the VM files to the path specified. Based on your requirement, you can filter out only the vmdk files. This sample makes use of Http files access mechanism to download the VM's files.

The command to run the above sample is: --url IP> --username <username> --password <password> --vmname <virtual machine name> --localpath <path where to store the files

IDPRESV
Contributor
Contributor

I am looking at that right now thank you Angela.

Is it possible to find all of the virtual machines that are managed by a vsphere machine or an ESX server. ie get all the virtual machines names <currently residing on an ESX server. Or one better get all the virtual machines names that the Vsphere is managing on its ESX servers?

0 Kudos
lamw
Community Manager
Community Manager

It might be easier to query by the VM's display name since VMware allows you to have a different display name than what the actual files are named, at least you can match what you're seeing using the vSphere client.

Here is a script using vSphere SDK for Perl that lists all VMs and their full path to their disks, you can modify it to display what you need but an example to get you going.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

Twitter: @lamw

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos
IDPRESV
Contributor
Contributor

Thank you for sharing that lamw

I am not very fluent in perl however without learning perl i want to say this line

$vm_view = Vim::find_entity_views(view_type => 'VirtualMachine');

is a key [art to finding all vms by their name.

i am assuming you dont use the same API reference as the Java and C#

This api

would you say this method FindAllByDnsName

is similar to what you are using, or might be the answer to my solution?

FindAllByDnsName

"Finds all virtual machines or hosts by DNS name. The DNS name for a virtual machine is the one returned from VMware tools, hostName."

I think it is unless this name could be different then the name of the vmdk in which the machine resides on.

0 Kudos
IDPRESV
Contributor
Contributor

You might want to refer to the C# sample GetVMFiles.cs that is shipped with the vSphere SDK package. The sample accepts VM name and the path to store the VM files ( .vmdk, .vmx etc) as input arguements and downloads all the VM files to the path specified. Based on your requirement, you can filter out only the vmdk files. This sample makes use of Http files access mechanism to download the VM's files.

The command to run the above sample is: --url IP> --username <username> --password <password> --vmname <virtual machine name> --localpath <path where to store the files

I think I should have said my intentions. I need the path to the VMDK to use in the vddk api. what i am trying to do is get the path for the vmdk in order to use it in with the vddk.

i believe the way they are doing it is with this line

vmDir[0] = vmConfigInfo.files.vmPathName;

where they have the stored path name

0 Kudos
yvsvmware
Contributor
Contributor

vmConfigInfo.files.vmPathName will give the path of VMX file but not the VMDK disk path.

For getting VMDK disk paths,

1. Get all virtual devices for a given VM...

2. Filter out the Virtual devices that are Virtual disks.

3. virtualdevice.backing.filename is the required VMDK disk path.

vmConfigInfo.hardware.device will give all the devices.

Filter them wrt properties VirtualDeviceFileBackingInfo or __VirtualDiskFlatVer2BackingInfo etc

virtualdevice.backing.filename will give the VMDK disk path

ManagedObjectReference vmMor = getDecendentMoRef(null,"VirtualMachine",vmname);

example: VirtualDevice vDevs = (VirtualDevice[])getDynamicProperty(vmMor, "config.hardware.device");

vDevs[i].getBacking().getClass().getCanonicalName().indexOf

("VirtualDiskFlatVer1BackingInfo")

{

//if (vdevice.backing != null && vdevice.backing is VirtualDeviceFileBackingInfo)

if ((vDevs[i].getBacking() != null)

&& (

(vDevs[i].getBacking().getClass().getCanonicalName().indexOf

("VirtualDiskFlatVer1BackingInfo") != -1) ||

(vDevs[i].getBacking().getClass().getCanonicalName().indexOf

("VirtualDiskFlatVer2BackingInfo")!= -1) ||

(vDevs[i].getBacking().getClass().getCanonicalName().indexOf

("VirtualDiskSparseVer1BackingInfo")!= -1) ||

(vDevs[i].getBacking().getClass().getCanonicalName().indexOf

("VirtualDiskSparseVer2BackingInfo")!= -1)

))

{

VirtualDeviceFileBackingInfo vBackInfo = (VirtualDeviceFileBackingInfo)vDevs[i].getBacking();

vmDiskURLs.add(vBackInfo.getFileName());

}

}

0 Kudos
netlib
Enthusiast
Enthusiast

I downloaded the SDK and tried building GetVMFiles.cs. However when I run it on the guest (Windows 2003 Server) on vSphere 4 it just crashes. Do I need any specific runtimes or other packages to be installed on the guest? Thanks.

0 Kudos