Folks, I am trying to write a script to create VM's in my cluster. Right now, I have a script, but it is crashing my vCenter server completely. I posted the script and asked question to vSphere Management SDK forum already( http://communities.vmware.com/message/2229982#2229982). I'm hoping the people in this forum can help identify the bug in the script below that causes the crash.
The requirements for building a host are that I define the hostname, folder, CPU, memory, disk space and network to use. The script specifies the storage cluster to use and should pick the correct datastore for me to put the guest and its vmdk on.
Any advice with the script would be helpful. Should I be using ApplyStorageDrsRecommendation_Task, or is there something more appropriate. Am I doing something wrong that would cause this to crash?
Thanks,
Scott
#!/usr/bin/perluse strict;use VMware::VIRuntime;use VMware::VILib;use Data::Dumper;# Connect to vSphere and collect informationOpts::parse();Opts::validate();Util::connect();my $dscluster = Vim::find_entity_view(view_type => 'StoragePod', filter => {'name' => "SANCLUST" }, properties => ['name']);if ( ! defined( $dscluster ) ) {print "Unable to locate datastore cluster: $dscluster!\n";Util::disconnect();exit 1;}my $folder = Vim::find_entity_view(view_type => 'Folder', filter => {'name' => "Folder-blah"}, properties => ['name']);if ( ! defined( $folder ) ) {print "Unable to find folder to use!\n";Util::disconnect();exit 1;}# Create some devicesmy @vm_devices;push( @vm_devices, create_controller_spec() );push( @vm_devices, create_virtual_disk( 60 ) );my $configSpec = VirtualMachineConfigSpec->new(name => "test-vm",memoryMB => 2048,numCPUs => 2,deviceChange => \@vm_devices);# We only have one resource pool. Use that one for the storageplacementspecmy $resourcePools = Vim::find_entity_views( view_type => "ResourcePool" );my $storageMgr = Vim::get_view(mo_ref => Vim::get_service_content()->storageResourceManager);my $podSpec = StorageDrsPodSelectionSpec->new(storagePod => $dscluster);my $storageSpec = StoragePlacementSpec->new( type => "create",podSelectionSpec => $podSpec,folder => $folder,configSpec => $configSpec,resourcePool => $resourcePools->[0]);# Figure out which datastore to put the guest onmy $result = $storageMgr->RecommendDatastores(storageSpec => $storageSpec);my $curRec;my $curRecFree = 100;foreach my $rec ( @{$result->recommendations} ) {#print Dumper( $rec->{action}->[0]->spaceUtilAfter ) ; exit 0;if ( $rec->action->[0]->spaceUtilAfter < $curRecFree ) {$curRecFree = $rec->action->[0]->spaceUtilAfter;$curRec = $rec->key;}}# Create the VM#$storageMgr->ApplyStorageDrsRecommendation_Task( key => [ $curRec ] );print $curRec . "\n";sub create_virtual_disk {my ( $space ) = @_;my $backing = new VirtualDiskFlatVer2BackingInfo( diskMode => "persistant",split => 0, thinProvisioned => 1,fileName => "[ test-vm ]" );my $device = new VirtualDisk( backing => $backing, controllerKey => 0,key => 0, unitNumber => 0,capacityInKB => ( $space * 1024 * 1024 ) );return new VirtualDeviceConfigSpec( device => $device,fileOperation => VirtualDeviceConfigSpecFileOperation->new('create'),operation => VirtualDeviceConfigSpecOperation->new('add'));}sub create_controller_spec {my $controller =VirtualBusLogicController->new(key => 0,device => [0],busNumber => 0,sharedBus => VirtualSCSISharing->new('noSharing'));my $controller_vm_dev_conf_spec =VirtualDeviceConfigSpec->new(device => $controller,operation => VirtualDeviceConfigSpecOperation->new('add'));return $controller_vm_dev_conf_spec;}