VMware {code} Community
srimini
Contributor
Contributor

how to get the ORPHANED vms from Virtual Center

HI

i am using VI SDK for my application.i provide a option for deleting vms from server.if i delete a vm from ESX (while connecting to ESX ) it gets deleted and those deleted vms appear as ORPHANED in the virtual center that is managing the server under which the VM was deleted.

can anyone help me with the code using which i can find out which vms are all orphaned(while connecting to VC) is there a property for getting this?

Thanks in advance

Regards,

Padmini

Reply
0 Kudos
13 Replies
Steve_Jin
Expert
Expert

How did you delete the VM? Unregister it or destroy it with the files deleted as well?

Steve JIN, VMware Engineering

Creator of VMware Infrastructure Java API. VI Java API 2.0 --- 15 times faster than AXIS in loading, 4+ faster in deserialization; only 1/4 of the size required by AXIS. More importantly, the freedom to redistribute your applications. (Download, Samples, DocWiki, RSS Feed)

Get Connected with Other Developers in the Community?

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
Reply
0 Kudos
srimini
Contributor
Contributor

Hi ,

this the code i use for deleting the vm

VimPortType vimPortType = ((ServiceConnection)serviceConnection).getService();

ManagedObjectReference deleteTask

= ((VimPortType)vimPortType).destroy_Task

((ManagedObjectReference)desktopRef); //this is the vm reference

String status = ((ServiceUtil)serviceUtil).waitForTask(deleteTask);

if(status.equalsIgnoreCase("failure")) {

//System.out.println("Failure -: Virtual Machine cannot be deleted");

taskSuccess = false;

}

if(status.equalsIgnoreCase("sucess")) {

taskSuccess = true;

}

i tried to unregister but it dint work so went for this ...please help me how to seggregate the orphaned vms or how to delete a vm completely so that it doesnt apper on the VC as orphaned.......

Reply
0 Kudos
gboskin
Enthusiast
Enthusiast

Theres a very good article on Yellow bricks

Reply
0 Kudos
srimini
Contributor
Contributor

Hi,

is there a way how to find orphaned vms using VI SDK..........please help........i am not sure whether the function i have mentioned in the prev post actually deletes the vmdk....

Reply
0 Kudos
njain
Expert
Expert

destroy_Task method destroys the managed entity and deletes all its contents, that is its associated files as well.

If you are deleting the virtual machines through the ESX directly and the ESX is being managed by the VirtualCenter, then the VMs will appear as orphaned. There is no way where the orphaned VMs can be identified. In this scenario, it is advised to delete the VMs through VC itself.

Reply
0 Kudos
lamw
Community Manager
Community Manager

You can basically use the datastore browser and HostDatastoreBrowserSearchSpec() to search for specific files, I've create a VMware Health Report script that provides information regarding orphaned snapshot files and also allowing you to specify the number of days passed for displaying this information at: http://communities.vmware.com/docs/DOC-9420

Here is the small snippet of code in Perl that does this, I'm sure you can easily modify this into the language you're looking to implement

#capture VM dir contents w/delta files
my $browser = Vim::get_view (mo_ref => $ds->browser);
my $ds_path = "";

my $file_query = FileQueryFlags->new(fileSize => 1,fileType => 0,modification => 1);

my $searchSpec = HostDatastoreBrowserSearchSpec->new(details => $file_query,matchPattern => );
my $search_res = $browser->SearchDatastoreSubFolders(datastorePath => $ds_path,searchSpec => $searchSpec);

if ($search_res) {
	foreach my $result (@$search_res) {
		my $files = $result->file;
		if ($files) {
    		foreach my $file (@$files) {
				if($file->path =~ /-delta.vmdk/ ) {
					print $file->path,"\n";
				}
			}
		}
	}
}

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

--William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Reply
0 Kudos
jrackliffe
Hot Shot
Hot Shot

I don't have any ophaned VMs to really test this theory or see if there is a better way, but I think there are a couple ways to approach this.

Have you opened the MOB interface and taken a look at the MORs left over for an orphan VM on your VC? There are a number of config properties on a VM MOR like configStatus, configIssue, and overallStatus that may shed some light on how VC sees this orphan.

If everything looks good in the MOB you may need to get more creative with your signature detection...

One way I could think would be to check the VMX File status of all the VMs managed by your VC. Basically for each VM grab the summary.config.vmPathName which will give you the Datastore location of the VMX (which should not be there because you deleted it) and then do a simple HostDatastoreBrowser.SearchDatastore or FileManager call to see if the file exists. If the file doesn't exist than it is an orphan. Not a definative, but a VM MOR w/o a properly bound VMX seems suspect to me.

UPDATE: Can't leave the comment open for an hour or you mis duping others suggestions. See next comment. DOH

What you do with that result is up to you. Probably could just delete the VM, BUT I would run that "search" process a couple times before deteing the VM objects. And I would still follow the parents advice and manage in VC and not ESX as you will only get worse sync issues with other operations.

Good luck

J

Reply
0 Kudos
srimini
Contributor
Contributor

hi,

how to use the MOB interface to get the vm references and the vmx file details.......and also can someone help with the code for browsing the datastore seraching for a specific vmx file

thanks in advance

regards,

Padmini

Reply
0 Kudos
jrackliffe
Hot Shot
Hot Shot

Well the MOB (Managed Object Browser) is available on your VCs at https://<Your VC DNS>/mob. It is a web UI that will allow you to browse through your entire object model on that VC. It doesn't give you the objects specifically, but just allows you to see the properties in a simple view.

As for the Datastore browser code, what specifcally didn't you understand about the fragment that William posted? That was pretty straight forward Perl code on a datastore pattern?

BTW I would still make sure your process is aligned going forward on deleting from VC and not ESX as sync issues are just a pain.

J

Reply
0 Kudos
srimini
Contributor
Contributor

Hi

i dono pearl language but i need the code in java ......now i dont have much time to explore so it would be very helpful if i get the readymade code for how to browse the datastore for a specific vmx file

Thanks in advance

Regards,

Padmini

Reply
0 Kudos
jrackliffe
Hot Shot
Hot Shot

Sorry I am most familiar with the SOAP interfaces via C#, but did you try and use Steve Jin's VI Java library (linked early in the thread)?

There would be a good deal of code to do everything including property collector, etc. so I am unsure if you have the VMX path (from the VM's config) and all you need is an example of a HostDatastoreSpec? If so here is a really rough example that should be easy to port to Java.I hope it gets you part of the way there.

 
string file = vmx.Substring(vmx.LastIndexOf('/') + 1);
string path = vmx.Substring(0, vmx.LastIndexOf('/'));

HostDatastoreBrowserSearchSpec ss = new HostDatastoreBrowserSearchSpec();
ss.matchPattern = new string[] { file };
ss.searchCaseInsensitive = true;
ss.searchCaseInsensitiveSpecified = true;

ManagedObjectReference task = Conn.Service.SearchDatastore_Task(vmHost.datastoreBrowser, path, ss);

HostDatastoreBrowserSearchResults res = (HostDatastoreBrowserSearchResults)task.Result;
if (res.file != null)
  FileInfo[] filesfound = res.file

Reply
0 Kudos
srimini
Contributor
Contributor

Hi,

can i know where the vmHost is declared or how to get the vmHost.dataStoreBrowser ? because theSearchDatastore_Task takes datastoreBrowser as one of the argument rite? i dono how to create it

Thanks in advance

regards,

Padmini

Reply
0 Kudos
jrackliffe
Hot Shot
Hot Shot

The Vm's Host is linked via the runtime.host property of the VM. That should get you the HostSystem object. Then you will be able to gather the datastoreBrowser which is a property of the Host.

It is pretty straightforward. There are a number of samples in the API distribution that show how to use the PropertyCollector etc. to complete these requests.

J

Reply
0 Kudos