VMware API code to mount NFS Datastore on ESX host.

Since I posted this to the VMware user group today I figured I might as well post this to my blog as well:

Lets say you have a datastore 10.3.0.101 that has a mount point /install/vm directory. You want to mount that datastore onto host vhost04 so you can start creating VMs. Here is how it is done:

[cc lang=’perl’]
use Data::Dumper;
require VMware::VIRuntime;
VMware::VIRuntime->import();
use strict;

# log into a node:
my $conn;
my $esx = shift || ‘vhost04’;
my $server = ‘10.3.0.101’;
my $serverpath = ‘/install/vm’;
my $location = ‘nfs_10.3.0.101_install_vm’;

eval {
$conn = Vim->new(service_url=>”https://$esx/sdk”);
$conn->login(user_name=>’root’,password=>’c1ust3r’);
};
if($@){
print Dumper($@);
print $@->;fault_string . “\n”;
}

my $hv = $conn->find_entity_view(view_type => ‘HostSystem’);

my $nds = HostNasVolumeSpec->new(accessMode=>’readWrite’,
remoteHost=>$server,
localPath=>$location,
remotePath=>$serverpath);
my $dsmv = $hv->{vim}->get_view(mo_ref=>$hv->configManager->datastoreSystem);

eval {
$dsmv->CreateNasDatastore(spec=>$nds);
};

if ($@) {
print $@->fault_string . “\n”;
print Dumper($@);
}

$conn->logout;
[/cc]

Comments are closed.