VMware {code} Community
Nigel_Stuart
Contributor
Contributor
Jump to solution

Creating a VMFS Datastore using Vi Perl Toolkit Question

Hello,

I am trying to use the VI PerlToolkit to create a VMFS datastore on an ESX Sever. I found a method called CreateVmfsDatastore which is a managed object of type HostDatastoreSystem. (See below for a link to the documentation for this method). To make the call to CreateVmfsDatastore, I get the datastoreSystem object, which is of type "HostDatastoreSystem'. When I try to call CreateVmfsDatastore in the below code, I get the following error. Does anyone have any idea what I may be doing wrong?

Error Message:

Can't locate object method "CreateVmfsDatastore" via package "ManagedObjectReference"

sub _createDataStore

{

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

print "\nThe name of this host is ", $host_view->name . "\n\n";

my $hostDiskPartitionSpec = new HostDiskPartitionSpec();

my $hostScsiDiskPartition = new HostScsiDiskPartition( diskName => 'vmhba3:0:0', partition => 3 );

my $hostVmfsSpec = new HostVmfsSpec( blockSizeMb => 262144, extent => $hostScsiDiskPartition, majorVersion => 3, volumeName => 'datastore2');

my $vmfsDatastoreCreateSpec = new VmfsDatastoreCreateSpec( extent => $hostScsiDiskPartition, partition => $hostDiskPartitionSpec, vmfs => $hostVmfsSpec );

my $dataStoreSystem = $host_view->configManager->datastoreSystem;

$dataStoreSystem->CreateVmfsDatastore( spec => $vmfsDatastoreCreateSpec );

}

Tags (4)
Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

The reason for the problem is that when you say $host_view->configManager, you are only getting the reference to the ManagedObject as opposed to the MO itself. So you have to use a get_view() to get the actual MO from the reference. Then you must do the same thing with the datastoreSystem and get the Managed Object. Once you have the MO, you will be able to call createVmfsDatastore on it. What the error message is saying is that the method is not available on the MORef - which is correct since it is available on the MO.

Hope this helps.

View solution in original post

Reply
0 Kudos
3 Replies
admin
Immortal
Immortal
Jump to solution

The reason for the problem is that when you say $host_view->configManager, you are only getting the reference to the ManagedObject as opposed to the MO itself. So you have to use a get_view() to get the actual MO from the reference. Then you must do the same thing with the datastoreSystem and get the Managed Object. Once you have the MO, you will be able to call createVmfsDatastore on it. What the error message is saying is that the method is not available on the MORef - which is correct since it is available on the MO.

Hope this helps.

Reply
0 Kudos
Mystec
Contributor
Contributor
Jump to solution

The Vim::get_view() method can be used like this to create a new VMFS Datastore in Perl:

# Get HostDatastoreSystem MO to call CreateVMFSDatastore() method

my $hdSystem_mo_ref = $host_view -> configManager -> datastoreSystem;

# Get Managed Object so we can run methods on it

my $hdSystem_view = Vim::get_view (mo_ref => $hdSystem_mo_ref ) ;

my $vmfs_datastore_options = $hdSystem_view -> QueryVmfsDatastoreCreateOptions (

            devicePath => $esxLun -> devicePath,

            vmfsMajorVersion => 5 );

my $vmfs_spec = @$vmfs_datastore_options[0] -> spec;

# Here you can modify $vmfs_spec to what you need, such as the volume name:

$vmfs_spec->{vmfs}->{volumeName} = $lun->lun();

# Create the Datastore!

my $datastore = $hdSystem_view -> CreateVmfsDatastore( spec => $vmfs_spec );

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

If you use get_view() and just need it to call a method, I recommend you use properties => ['name'] to minimize the SDK overhead:

$thisDatastore = Vim::get_view(mo_ref => $dsRef, properties => ['name']);

You can also 'hack' out an entity object view as well, if you really wanted to avoid another SDK call to the API (though I'd recommend the get_view() process):

$thisDatastore = new Datastore();

$thisDatastore->{'mo_ref'} = $dsRef;

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos