pdhami1
Contributor
Contributor

Enumerate Virtual Machines and list the attached RDM devices.

I'm trying to list out each Virtual Machine (which works), and the associated RDM path (Backinginfo?) I've had little luck so I tried to list all the devices on the system and thier associated Virtual Machine.

Heres the code I'm working with (its perl). I need to reference the Virtual machines associated with a scsilun.

This doesnt produce the data I want. Any ideas?

Just pass the server username and password params.

    1. Start

#!/usr/bin/perl -w

use strict;

use warnings;

use VMware::VIM2Runtime;

use VMware::VILib;

use VMware::VIRuntime

Opts::parse();

Opts::validate();

Util::connect();

my $hostview = Vim::find_entity_view (view_type => 'HostSystem');

display_scsi_luns($hostview);

Util::disconnect();

sub display_scsi_luns {

my ($hostview) = @_;

my $lun_counter = 0;

my @logicalunitlist;

$, = ' ';

if(defined $hostview->config->storageDevice->scsiLun) {

@logicalunitlist = @{$hostview->config->storageDevice->scsiLun};

}

foreach(@logicalunitlist) {

my $lunname = $_->canonicalName;

get_vms($hostview, $lunname);

}

}

sub get_vms {

my ($hostview, $canonicalname) = @_;

my $vms = $hostview->vm;

my $exist_flag = 0;

print $canonicalname . "\n";

foreach(@$vms) {

my $vms_view = Vim::get_view(mo_ref => $_);

print $vms_view->config->name . "\n";

}

if($exist_flag eq 0) {

}

}

Util::disconnect();

    1. End

0 Kudos
7 Replies
Courtney
Enthusiast
Enthusiast

Have you tried looking at the VirtualMachine -> VirtualMachineDiskDeviceInfo or FindByDatastorePath option? I haven't tried using these but they can be found in the VIPERL toolkit documentation.

Good luck!

pdhami1
Contributor
Contributor

the FindByDatastorePath wont do it. I am curious how I'd go about using the VirtualMachineDiskDeviceInfo. any ideas?

0 Kudos
ssurana
VMware Employee
VMware Employee

Hi Ken,

Below is the altered version of your code snippet that illustrates the usage of VirtualMachineDiskDeviceInfo. Currently, I do not have the setup for the RDM thus I could not verify the execution of the code. But theoretically this code should work for you. The code would list down the Device Path for the RDM and the VMs that might be using the device for the Backing.

#!/usr/bin/perl -w

use strict;

use warnings;

use VMware::VIM2Runtime;

use VMware::VILib;

use VMware::VIRuntime

Opts::parse();

Opts::validate();

Util::connect();

my $hostview = Vim::find_entity_view(view_type => 'HostSystem');

display_scsi_luns($hostview);

Util::disconnect();

sub display_scsi_luns {

my ($hostview) = @_;

my $lun_counter = 0;

my @logicalunitlist;

if(defined $hostview->config->storageDevice->scsiLun) {

@logicalunitlist = @{$hostview->config->storageDevice->scsiLun};

}

foreach(@logicalunitlist) {

my $lunname = $_->canonicalName;

get_vms($hostview, $lunname);

}

}

sub get_vms {

my ($hostview, $canonicalname) = @_;

my $vms = $hostview->vm;

my $exist_flag = 0;

my $parent = Vim::get_view(mo_ref =>$hostview->parent);

if (defined $parent->environmentBrowser) {

my $envbrw = Vim::get_view(mo_ref =>$parent->environmentBrowser);

if (defined $envbrw) {

print "\n Retrieving information for " . $hostview->name . " host.\n";

my $confTgt = $envbrw->QueryConfigTarget(host=>$hostview);

if (defined $confTgt) {

if (defined $confTgt->scsiDisk) {

my $scsi = $confTgt->scsiDisk;

foreach (@$scsi) {

if (defined $_->disk) {

my $devicePath = $_->disk->devicePath;

print "Backing device Info: Device Path = " . $devicePath . "\n ";

}

if (defined $_->vm) {

print "Virtual Machines that are using this device are:\n";

foreach my $vm_list(@$_->vm) {

my $vms_view = Vim::get_view(mo_ref => $vm_list);

print $vms_view->config->name . "\n";

}

}

}

}

}

}

}

print "\nPrinting List of VMs on this host\n";

foreach(@$vms) {

my $vms_view = Vim::get_view(mo_ref => $_);

print $vms_view->config->name . "\n";

}

if($exist_flag eq 0) {

}

}

Hope the above helps.

~ Sidharth

0 Kudos
pdhami1
Contributor
Contributor

Thanks for the response! I played around with your code a bit but I still can't seem to get the associated VM(s) to a particular RDM.

This particular peice of code is never executed...

if (defined $_->vm) {

print "Virtual Machines that are using this device are:\n";

foreach my $vm_list(@$_->vm) {

my $vms_view = Vim::get_view(mo_ref => $vm_list);

print $vms_view->config->name . "\n";

}

}

0 Kudos
ssurana
VMware Employee
VMware Employee

Well the possible reason could be that there are no virtual machines that are using the physical disk as a backing. So you might want to verify the Virtual Machines of your environment and then compare it with the results.

0 Kudos
pdhami1
Contributor
Contributor

I am using RDM luns from a Netapp storage array. The physical disks are definally being used by the virtual machine.

0 Kudos
sunvmman
Enthusiast
Enthusiast

do you have an example for this. very interested to see all devices that my vm is using.

0 Kudos