use of com.cloud.hypervisor.vmware.mo.VirtualStorageObjectManagerMO in project cloudstack by apache.
the class VmwareStorageProcessor method createVolume.
@Override
public Answer createVolume(CreateObjectCommand cmd) {
VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
DataStoreTO primaryStore = volume.getDataStore();
String vSphereStoragePolicyId = volume.getvSphereStoragePolicyId();
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
if (morDatastore == null) {
throw new Exception("Unable to find datastore in vSphere");
}
DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
// create data volume
VirtualMachineMO vmMo = null;
String volumeUuid = UUID.randomUUID().toString().replace("-", "");
String volumeDatastorePath = VmwareStorageLayoutHelper.getDatastorePathBaseFolderFromVmdkFileName(dsMo, volumeUuid + ".vmdk");
VolumeObjectTO newVol = new VolumeObjectTO();
try {
VirtualStorageObjectManagerMO vStorageObjectManagerMO = new VirtualStorageObjectManagerMO(context);
VStorageObject virtualDisk = vStorageObjectManagerMO.createDisk(morDatastore, volume.getProvisioningType(), volume.getSize(), volumeDatastorePath, volumeUuid);
DatastoreFile file = new DatastoreFile(((BaseConfigInfoDiskFileBackingInfo) virtualDisk.getConfig().getBacking()).getFilePath());
newVol.setPath(file.getFileBaseName());
newVol.setSize(volume.getSize());
} catch (Exception e) {
s_logger.debug("Create disk using vStorageObject manager failed due to exception " + e.getMessage() + ", retying using worker VM");
String dummyVmName = hostService.getWorkerName(context, cmd, 0, dsMo);
try {
s_logger.info("Create worker VM " + dummyVmName);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName, null);
if (vmMo == null) {
throw new Exception("Unable to create a dummy VM for volume creation");
}
synchronized (this) {
try {
vmMo.createDisk(volumeDatastorePath, (int) (volume.getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey(), vSphereStoragePolicyId);
vmMo.detachDisk(volumeDatastorePath, false);
} catch (Exception e1) {
s_logger.error("Deleting file " + volumeDatastorePath + " due to error: " + e1.getMessage());
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid, dcMo, VmwareManager.s_vmwareSearchExcludeFolder.value());
throw new CloudRuntimeException("Unable to create volume due to: " + e1.getMessage());
}
}
newVol = new VolumeObjectTO();
newVol.setPath(volumeUuid);
newVol.setSize(volume.getSize());
return new CreateObjectAnswer(newVol);
} finally {
s_logger.info("Destroy dummy VM after volume creation");
if (vmMo != null) {
vmMo.detachAllDisksAndDestroy();
}
}
}
return new CreateObjectAnswer(newVol);
} catch (Throwable e) {
return new CreateObjectAnswer(hostService.createLogMessageException(e, cmd));
}
}
Aggregations