Hi all,
Here my task is to create vmdk or rdm.
Can any one help me how to create a vmdk or rdm using vmware sdk call.Here i am calling this method to create vmdk 'CreateVirtualDisk_Task ' is this the method to call or any other method available to create vmdk?
i am bit confused with parameters for this method CreateVirtualDisk_Task .here i am hot coding the parameters to create vmdk like this "m_pVimSvc->CreateVirtualDisk_Task(imor,"[Dsc_0121] Hello/Hello.vmdk",nullptr,xx);" but it's failing to create the error message i got was "invalid parameter" afeter researching i came to know i need to fill "virtualdiskspec" can any know how to fill this spec?
Thanks in advance...
--karthik
Hi Karthik,
You are right in picking the correct method for the vmdk creation.
The sample syntax that you need to use is as follows:
my $name = "[Datastore] folder/myvmdffile.vmdk";
my $spec = FileBackedVirtualDiskSpec->new(
adapterType=>"busLogic",
diskType=>"preallocated",
capacityKb=>"2048"
);
eval {
$virtualDiskManager_view->CreateVirtualDisk(name => $name,
datacenter => $dc_mor, # mor of the datacenter
spec => $spec);};
if ($@) {
print "error : " . $@;
}
Please note that the minimum size of the disk is 1 MB. Thus try to give a higher value than 1 MB for the capacity. You can change the adapter type and disk type as per your choice and environment.
Hope the above helps.
~ Sidharth
Hi Sidharth,
Thanks for your reply, finally i created vmdk my question is the same procedure for creating RDM also? after creating vmdk i am trying to assign it to a vm but it is not successful.you have any idea or sample code you have to assigning it to a vm. it would be very helpul for me if you help me in scenario.
--Karthik
Hi Karthik,
Yes, you can create the RDM also with the same method, although I have not tried it myself, but it is possible.
As far as assigning the vmdk to the VMs you can easily do so using the VI Client. You can also use the below sample code snippet for the same to do it programmatically.
>.# $vm_view =====> Virtual Machine Managed Object
>.# $disk_file ===> Complete path of the vmdk file. like "[datasore-1] diskLibrary/disk.vmdk"
>my @device_config_specs = ();
>my $devices = $vm_view->config->hardware->device;
>foreach my $device (@$devices) {
if ($device->deviceInfo->label eq "SCSI Controller 0") {
$controller = $device;
last;
}
>}
>unless ($controller) {
Util::trace(0, "Controller not found.\n");
return;
>}
>my $disk_backing_info = VirtualDiskFlatVer2BackingInfo->new(diskMode => "persistent" ,
fileName => $disk_file);
>my $disk = VirtualDisk->new(controllerKey => $controller->key,
unitNumber => 2,
key => -1,
backing => $disk_backing_info,
capacityInKB => 2048); # capacityInKB doesn't really matter while adding an existing vmdk, but the parameter is required so you need to specify
>my $devspec = VirtualDeviceConfigSpec->new(operation => VirtualDeviceConfigSpecOperation->new('add'),
device => $disk);
>@device_config_specs = (@device_config_specs, $devspec);
>my $vmspec = VirtualMachineConfigSpec->new(deviceChange => \@device_config_specs);
>eval {
$vm_view->ReconfigVM( spec => $vmspec );
Util::trace(0,"\nVirtual machine '" . $vm_view->name
. "' is reconfigured successfully.\n");
>};
>if ($@) {
Util::trace(0, "\nReconfiguration failed: ");
if (ref($@) eq 'SoapFault') {
if (ref($@->detail) eq 'TooManyDevices') {
Util::trace(0, "\nNumber of virtual devices exceeds "
. "the maximum for a given controller.\n");
}
elsif (ref($@->detail) eq 'InvalidDeviceSpec') {
Util::trace(0, "The Device configuration is not valid\n");
Util::trace(0, "\nFollowing is the detailed error: \n\n$@");
}
elsif (ref($@->detail) eq 'FileAlreadyExists') {
Util::trace(0, "\nOperation failed because file already exists");
}
else {
Util::trace(0, "\n" . $@ . "\n");
}
}
else {
Util::trace(0, "\n" . $@ . "\n");
}
>}
Hope the above helps.
~ Sidharth
Hi Sidharth ,
Thanks for your reply, after seeing your code i modified my code actually i am working c++/CLi it's working fine but some times it's causing problem, this sample code helped me a lot.
thanks
--karthik
