I am able to add the disks linearly like below:
my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[0]]);
$vmView->ReconfigVM(spec => $vmSpec);
my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[1]]);
$vmView->ReconfigVM(spec => $vmSpec);
my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[2]]);
$vmView->ReconfigVM(spec => $vmSpec);
However, when I attempt to add all the disks at once I get a SOAP fault
my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => \@devSpecs);
$vmView->ReconfigVM(spec => $vmSpec);
The Error:
SOAP Fault:
-----------
Fault string: Cannot complete the operation because the file or folder /vmfs/volumes/ba5e81fe-201a6c4e/paul-vm1/paul-vm1_0_3.vmdk already exists
Fault detail: FileAlreadyExistsPAULWORK>clear
Is there anyone out there that knows what is going on?
I did a dump like you said and it was very helpful. I needed to add the code below to make it work
Turns out that I need a unique key for every disk that I am adding. Thank you for your help
my $disk = VirtualDisk->new(
controllerKey => $devSpec->device->controllerKey,
unitNumber => $devSpec->device->unitNumber,
key => -1 *($i + 1),
backing => $devSpec->device->backing,
capacityInKB => $devSpec->device->capacityInKB,
);
$devSpec = VirtualDeviceConfigSpec->new(
operation => $devSpec->operation,
fileOperation => $devSpec->fileOperation,
device => $disk,
);
Is there a better way to do it than this
I've created new disks before in bulk. Are you adding or creating disks? Got more of your code to show?
I did not want to do a code dump, but here you go
Since you're adding an existing VMDK, any chance you're adding it twice or it's already attached to that VM?
I dont think so, If that was true, why could I add the three individual machines by array index as apposed to passing it the array of three items
Do this, add "print Dumper($vmSpec)" before line 150. We can eyeball the spec sent to the API.
I did a dump like you said and it was very helpful. I needed to add the code below to make it work
Turns out that I need a unique key for every disk that I am adding. Thank you for your help
my $disk = VirtualDisk->new(
controllerKey => $devSpec->device->controllerKey,
unitNumber => $devSpec->device->unitNumber,
key => -1 *($i + 1),
backing => $devSpec->device->backing,
capacityInKB => $devSpec->device->capacityInKB,
);
$devSpec = VirtualDeviceConfigSpec->new(
operation => $devSpec->operation,
fileOperation => $devSpec->fileOperation,
device => $disk,
);
Is there a better way to do it than this
"key => -1" works fine for me:
Since I am adding a bunch of pre-existing disks, what I do is this:
- locate the disks based on the datastore and folder location using datastore browser (SearchDatastoreSubFolders call).
- do an inventory of the controllers so that I can reference the controller keys when needed
- do an inventory of the virtual disks currently attached to the vm (so that I know if adding more disk to a particular virtual node makes sense).
then loop through the diks that you have found from the datastore and are supposedly attaching disks to.
my $backing = @$disks[$i]->diskType->new (
diskMode => 'independent_persistent',
fileName => $result->folderPath . @$disks[$i]->path,
);
my $connectInfo = VirtualDeviceConnectInfo->new (
allowGuestControl => 0,
connected => 1,
startConnected => 1,
);
my $devInfo = VirtualDisk->new (
backing => $backing,
controllerKey => $ctrlKey,
unitNumber => $unit,
connectable => $connectInfo,
key => -1,
capacityInKB => @$disks[$i]->capacityKb,
);
$devspec[$i] = VirtualDeviceConfigSpec->new(
operation => VirtualDeviceConfigSpecOperation->new('add'),
device => $devInfo,
);
Once finish the loop, construct the $spec and run reconfigVM (or reconfigVM_task):
my $spec = VirtualMachineConfigSpec->new(deviceChange => [@devspec] );
if (@devspec) {
print "Reconfig VM with disk changes...\n";
# Asynchronous vs Synchronous execution
#eval { $vm->ReconfigVM(spec => $spec); };
eval { $vm->ReconfigVM_Task(spec => $spec); };
if ($@) { print "Reconfiguration failed.\n $@";}
else {print "Disks added successfully.\n"; }
} else { print "Nothing to add!\n"};
