Search in sources :

Example 6 with DiskControllerType

use of com.cloud.model.enumeration.DiskControllerType in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method allocVolume.

/*
     * Just allocate a volume in the database, don't send the createvolume cmd
     * to hypervisor. The volume will be finally created only when it's attached
     * to a VM.
     */
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_CREATE, eventDescription = "creating volume", create = true)
public VolumeVO allocVolume(final CreateVolumeCmd cmd) throws ResourceAllocationException {
    // FIXME: some of the scheduled event stuff might be missing here...
    final Account caller = CallContext.current().getCallingAccount();
    final long ownerId = cmd.getEntityOwnerId();
    final Account owner = this._accountMgr.getActiveAccountById(ownerId);
    Boolean displayVolume = cmd.getDisplayVolume();
    // permission check
    this._accountMgr.checkAccess(caller, null, true, this._accountMgr.getActiveAccountById(ownerId));
    if (displayVolume == null) {
        displayVolume = true;
    } else {
        if (!this._accountMgr.isRootAdmin(caller.getId())) {
            throw new PermissionDeniedException("Cannot update parameter displayvolume, only admin permitted ");
        }
    }
    // Check that the resource limit for volumes won't be exceeded
    this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.volume, displayVolume);
    Long zoneId = cmd.getZoneId();
    final Long diskOfferingId;
    final DiskOfferingVO diskOffering;
    final StorageProvisioningType provisioningType;
    Long size;
    Long minIops = null;
    Long maxIops = null;
    // Volume VO used for extracting the source template id
    VolumeVO parentVolume = null;
    // validate input parameters before creating the volume
    if (cmd.getSnapshotId() == null && cmd.getDiskOfferingId() == null || cmd.getSnapshotId() != null && cmd.getDiskOfferingId() != null) {
        throw new InvalidParameterValueException("Either disk Offering Id or snapshot Id must be passed whilst creating volume");
    }
    if (cmd.getSnapshotId() == null) {
        // create a new volume
        diskOfferingId = cmd.getDiskOfferingId();
        size = cmd.getSize();
        final Long sizeInGB = size;
        if (size != null) {
            if (size > 0) {
                // user specify size in GB
                size = size * 1024 * 1024 * 1024;
            } else {
                throw new InvalidParameterValueException("Disk size must be larger than 0");
            }
        }
        // Check that the the disk offering is specified
        diskOffering = this._diskOfferingDao.findById(diskOfferingId);
        if (diskOffering == null || diskOffering.getRemoved() != null || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
            throw new InvalidParameterValueException("Please specify a valid disk offering.");
        }
        if (diskOffering.isCustomized()) {
            if (size == null) {
                throw new InvalidParameterValueException("This disk offering requires a custom size specified");
            }
            final Long customDiskOfferingMaxSize = this._volumeMgr.CustomDiskOfferingMaxSize.value();
            final Long customDiskOfferingMinSize = this._volumeMgr.CustomDiskOfferingMinSize.value();
            if (sizeInGB < customDiskOfferingMinSize || sizeInGB > customDiskOfferingMaxSize) {
                throw new InvalidParameterValueException("Volume size: " + sizeInGB + "GB is out of allowed range. Max: " + customDiskOfferingMaxSize + " Min:" + customDiskOfferingMinSize);
            }
        }
        if (!diskOffering.isCustomized() && size != null) {
            throw new InvalidParameterValueException("This disk offering does not allow custom size");
        }
        if (diskOffering.getDomainId() == null) {
        // do nothing as offering is public
        } else {
            this._configMgr.checkDiskOfferingAccess(caller, diskOffering);
        }
        if (diskOffering.getDiskSize() > 0) {
            size = diskOffering.getDiskSize();
        }
        final Boolean isCustomizedIops = diskOffering.isCustomizedIops();
        if (isCustomizedIops != null) {
            if (isCustomizedIops) {
                minIops = cmd.getMinIops();
                maxIops = cmd.getMaxIops();
                if (minIops == null && maxIops == null) {
                    minIops = 0L;
                    maxIops = 0L;
                } else {
                    if (minIops == null || minIops <= 0) {
                        throw new InvalidParameterValueException("The min IOPS must be greater than 0.");
                    }
                    if (maxIops == null) {
                        maxIops = 0L;
                    }
                    if (minIops > maxIops) {
                        throw new InvalidParameterValueException("The min IOPS must be less than or equal to the max IOPS.");
                    }
                }
            } else {
                minIops = diskOffering.getMinIops();
                maxIops = diskOffering.getMaxIops();
            }
        }
        provisioningType = diskOffering.getProvisioningType();
        if (!validateVolumeSizeRange(size)) {
            // for validation
            throw new InvalidParameterValueException("Invalid size for custom volume creation: " + size + " ,max volume size is:" + this._maxVolumeSizeInGb);
        }
    } else {
        // create volume from snapshot
        final Long snapshotId = cmd.getSnapshotId();
        final SnapshotVO snapshotCheck = this._snapshotDao.findById(snapshotId);
        if (snapshotCheck == null) {
            throw new InvalidParameterValueException("unable to find a snapshot with id " + snapshotId);
        }
        if (snapshotCheck.getState() != Snapshot.State.BackedUp) {
            throw new InvalidParameterValueException("Snapshot id=" + snapshotId + " is not in " + Snapshot.State.BackedUp + " state yet and can't be used for volume " + "creation");
        }
        parentVolume = this._volsDao.findByIdIncludingRemoved(snapshotCheck.getVolumeId());
        diskOfferingId = snapshotCheck.getDiskOfferingId();
        diskOffering = this._diskOfferingDao.findById(diskOfferingId);
        if (zoneId == null) {
            // if zoneId is not provided, we default to create volume in the same zone as the snapshot zone.
            zoneId = snapshotCheck.getDataCenterId();
        }
        // ; disk offering is used for tags
        size = snapshotCheck.getSize();
        // purposes
        minIops = snapshotCheck.getMinIops();
        maxIops = snapshotCheck.getMaxIops();
        provisioningType = diskOffering.getProvisioningType();
        // check snapshot permissions
        this._accountMgr.checkAccess(caller, null, true, snapshotCheck);
        // one step operation - create volume in VM's cluster and attach it
        // to the VM
        final Long vmId = cmd.getVirtualMachineId();
        if (vmId != null) {
            // Check that the virtual machine ID is valid and it's a user vm
            final UserVmVO vm = this._userVmDao.findById(vmId);
            if (vm == null || vm.getType() != VirtualMachineType.User) {
                throw new InvalidParameterValueException("Please specify a valid User VM.");
            }
            // Check that the VM is in the correct state
            if (vm.getState() != State.Running && vm.getState() != State.Stopped) {
                throw new InvalidParameterValueException("Please specify a VM that is either running or stopped.");
            }
            // permission check
            this._accountMgr.checkAccess(caller, null, false, vm);
        }
    }
    // Check that the resource limit for primary storage won't be exceeded
    this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.primary_storage, displayVolume, new Long(size));
    // Verify that zone exists
    final DataCenterVO zone = this._dcDao.findById(zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("Unable to find zone by id " + zoneId);
    }
    // Check if zone is disabled
    if (AllocationState.Disabled == zone.getAllocationState() && !this._accountMgr.isRootAdmin(caller.getId())) {
        throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zoneId);
    }
    final String userSpecifiedName = getVolumeNameFromCommand(cmd);
    DiskControllerType diskControllerType = getDiskControllerType();
    if (cmd.getDiskController() != null) {
        diskControllerType = DiskControllerType.valueOf(cmd.getDiskController().toUpperCase());
    }
    ImageFormat fileFormat = ImageFormat.QCOW2;
    if (cmd.getFileFormat() != null) {
        fileFormat = ImageFormat.valueOf(cmd.getFileFormat().toUpperCase());
    }
    return commitVolume(cmd, caller, owner, displayVolume, zoneId, diskOfferingId, provisioningType, size, minIops, maxIops, parentVolume, userSpecifiedName, this._uuidMgr.generateUuid(Volume.class, cmd.getCustomId()), diskControllerType, fileFormat);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.legacymodel.user.Account) UserVmVO(com.cloud.vm.UserVmVO) StorageProvisioningType(com.cloud.model.enumeration.StorageProvisioningType) ImageFormat(com.cloud.model.enumeration.ImageFormat) DiskControllerType(com.cloud.model.enumeration.DiskControllerType) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Volume(com.cloud.legacymodel.storage.Volume) VmWorkDetachVolume(com.cloud.vm.VmWorkDetachVolume) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkResizeVolume(com.cloud.vm.VmWorkResizeVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) VmWorkExtractVolume(com.cloud.vm.VmWorkExtractVolume) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

DiskControllerType (com.cloud.model.enumeration.DiskControllerType)6 ImageFormat (com.cloud.model.enumeration.ImageFormat)4 LibvirtDiskDef (com.cloud.agent.resource.kvm.xml.LibvirtDiskDef)3 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)3 LibvirtDomainXmlParser (com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser)2 ActionEvent (com.cloud.event.ActionEvent)2 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)2 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)2 DB (com.cloud.utils.db.DB)2 IoCTX (com.ceph.rados.IoCTX)1 Rados (com.ceph.rados.Rados)1 Rbd (com.ceph.rbd.Rbd)1 RbdImage (com.ceph.rbd.RbdImage)1 LibvirtComputingResource (com.cloud.agent.resource.kvm.LibvirtComputingResource)1 LibvirtConnection (com.cloud.agent.resource.kvm.LibvirtConnection)1 QemuImg (com.cloud.agent.resource.kvm.storage.utils.QemuImg)1 QemuImgException (com.cloud.agent.resource.kvm.storage.utils.QemuImgException)1 QemuImgFile (com.cloud.agent.resource.kvm.storage.utils.QemuImgFile)1 InterfaceDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef)1 RngDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.RngDef)1