Search in sources :

Example 1 with VStorageObject

use of com.vmware.vim25.VStorageObject 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));
    }
}
Also used : VirtualStorageObjectManagerMO(com.cloud.hypervisor.vmware.mo.VirtualStorageObjectManagerMO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VStorageObject(com.vmware.vim25.VStorageObject)

Example 2 with VStorageObject

use of com.vmware.vim25.VStorageObject in project cloudstack by apache.

the class VirtualStorageObjectManagerMO method createDisk.

public VStorageObject createDisk(ManagedObjectReference morDS, Storage.ProvisioningType diskProvisioningType, long currentSizeInBytes, String datastoreFilepath, String filename) throws Exception {
    long currentSizeInMB = currentSizeInBytes / (1024 * 1024);
    VslmCreateSpecDiskFileBackingSpec diskFileBackingSpec = new VslmCreateSpecDiskFileBackingSpec();
    diskFileBackingSpec.setDatastore(morDS);
    if (diskProvisioningType != null) {
        if (diskProvisioningType == Storage.ProvisioningType.FAT) {
            diskFileBackingSpec.setProvisioningType(BaseConfigInfoDiskFileBackingInfoProvisioningType.EAGER_ZEROED_THICK.value());
        } else if (diskProvisioningType == Storage.ProvisioningType.THIN) {
            diskFileBackingSpec.setProvisioningType(BaseConfigInfoDiskFileBackingInfoProvisioningType.THIN.value());
        } else if (diskProvisioningType == Storage.ProvisioningType.SPARSE) {
            diskFileBackingSpec.setProvisioningType(BaseConfigInfoDiskFileBackingInfoProvisioningType.LAZY_ZEROED_THICK.value());
        }
    }
    // path should be just the folder name. For example, instead of '[datastore1] folder1/filename.vmdk' you would just do 'folder1'.
    // path is introduced from 6.7. In 6.5 disk will be created in the default folder "fcd"
    diskFileBackingSpec.setPath(null);
    VslmCreateSpec vslmCreateSpec = new VslmCreateSpec();
    vslmCreateSpec.setBackingSpec(diskFileBackingSpec);
    vslmCreateSpec.setCapacityInMB(currentSizeInMB);
    vslmCreateSpec.setName(filename);
    ManagedObjectReference morTask = _context.getService().createDiskTask(_mor, vslmCreateSpec);
    boolean result = _context.getVimClient().waitForTask(morTask);
    VStorageObject vStorageObject = null;
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        // _context.getService().reconcileDatastoreInventoryTask(_mor, morDS);
        TaskInfo taskInfo = TaskMO.getTaskInfo(_context, morTask);
        vStorageObject = (VStorageObject) taskInfo.getResult();
    } else {
        LOGGER.error("VMware CreateDisk_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return vStorageObject;
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) VslmCreateSpecDiskFileBackingSpec(com.vmware.vim25.VslmCreateSpecDiskFileBackingSpec) VslmCreateSpec(com.vmware.vim25.VslmCreateSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VStorageObject(com.vmware.vim25.VStorageObject)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 VStorageObject (com.vmware.vim25.VStorageObject)2 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)1 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)1 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)1 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1 VirtualStorageObjectManagerMO (com.cloud.hypervisor.vmware.mo.VirtualStorageObjectManagerMO)1 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)1 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 TaskInfo (com.vmware.vim25.TaskInfo)1 VslmCreateSpec (com.vmware.vim25.VslmCreateSpec)1 VslmCreateSpecDiskFileBackingSpec (com.vmware.vim25.VslmCreateSpecDiskFileBackingSpec)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 RemoteException (java.rmi.RemoteException)1 CreateObjectAnswer (org.apache.cloudstack.storage.command.CreateObjectAnswer)1 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)1 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)1