Search in sources :

Example 6 with AttachAnswer

use of com.cloud.legacymodel.communication.answer.AttachAnswer in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method attachVolume.

@Override
public Answer attachVolume(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
    final String vmName = cmd.getVmName();
    final String serial = disk.getDiskSeq() + "-" + this.resource.diskUuidToSerial(vol.getUuid());
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        this.storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails());
        final KvmPhysicalDisk phyDisk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue(), disk.getDiskController(), disk.getDiskFormat(), serial, vol);
        return new AttachAnswer(disk);
    } catch (final LibvirtException e) {
        this.logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        this.storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new AttachAnswer(e.getMessage());
    } catch (final InternalErrorException | CloudRuntimeException e) {
        this.logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new AttachAnswer(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Connect(org.libvirt.Connect) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) DiskTO(com.cloud.legacymodel.to.DiskTO) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer)

Example 7 with AttachAnswer

use of com.cloud.legacymodel.communication.answer.AttachAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testStorageSubSystemCommand.

@Test
public void testStorageSubSystemCommand() {
    final DiskTO disk = Mockito.mock(DiskTO.class);
    final String vmName = "Test";
    final AttachCommand command = new AttachCommand(disk, vmName);
    final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class);
    when(this.libvirtComputingResource.getStorageHandler()).thenReturn(handler);
    when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk));
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) StorageSubsystemCommandHandler(com.cloud.common.storageprocessor.resource.StorageSubsystemCommandHandler) AttachCommand(com.cloud.legacymodel.communication.command.AttachCommand) DiskTO(com.cloud.legacymodel.to.DiskTO) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) Test(org.junit.Test)

Example 8 with AttachAnswer

use of com.cloud.legacymodel.communication.answer.AttachAnswer in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method attachIso.

@Override
public Answer attachIso(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final TemplateObjectTO isoTo = (TemplateObjectTO) disk.getData();
    final DataStoreTO store = isoTo.getDataStore();
    if (!(store instanceof NfsTO)) {
        return new AttachAnswer("unsupported protocol");
    }
    final NfsTO nfsStore = (NfsTO) store;
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
        attachOrDetachIso(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTo.getPath(), true);
    } catch (final LibvirtException e) {
        return new Answer(cmd, false, e.toString());
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(com.cloud.legacymodel.communication.answer.CreateObjectAnswer) PrimaryStorageDownloadAnswer(com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) DettachAnswer(com.cloud.legacymodel.communication.answer.DettachAnswer) SnapshotAndCopyAnswer(com.cloud.legacymodel.communication.answer.SnapshotAndCopyAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) NfsTO(com.cloud.legacymodel.to.NfsTO) DiskTO(com.cloud.legacymodel.to.DiskTO) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer)

Example 9 with AttachAnswer

use of com.cloud.legacymodel.communication.answer.AttachAnswer in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method sendAttachVolumeCommand.

private VolumeVO sendAttachVolumeCommand(final UserVmVO vm, VolumeVO volumeToAttach, Long deviceId, final DiskControllerType diskController) {
    String errorMsg = "Failed to attach volume " + volumeToAttach.getName() + " to VM " + vm.getHostName();
    boolean sendCommand = vm.getState() == State.Running;
    AttachAnswer answer = null;
    final Long hostId = vm.getHostId();
    HostVO host = null;
    final StoragePoolVO volumeToAttachStoragePool = this._storagePoolDao.findById(volumeToAttach.getPoolId());
    if (hostId != null) {
        host = this._hostDao.findById(hostId);
        if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumeToAttachStoragePool != null && volumeToAttachStoragePool.isManaged()) {
            sendCommand = true;
        }
    }
    // volumeToAttachStoragePool should be null if the VM we are attaching the disk to has never been started before
    final DataStore dataStore = volumeToAttachStoragePool != null ? this.dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(), DataStoreRole.Primary) : null;
    // if we don't have a host, the VM we are attaching the disk to has never been started before
    if (host != null) {
        try {
            this.volService.grantAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
        } catch (final Exception e) {
            this.volService.revokeAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
            throw new CloudRuntimeException(e.getMessage());
        }
    }
    if (sendCommand) {
        if (host != null && host.getHypervisorType() == HypervisorType.KVM && volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
            volumeToAttach.setPath(volumeToAttach.get_iScsiName());
            this._volsDao.update(volumeToAttach.getId(), volumeToAttach);
        }
        final DataTO volTO = this.volFactory.getVolume(volumeToAttach.getId()).getTO();
        deviceId = getDeviceId(vm, deviceId);
        if (diskController != null) {
            volumeToAttach.setDiskController(diskController);
        }
        final DiskTO disk = new DiskTO(volTO, deviceId, volumeToAttach.getPath(), volumeToAttach.getVolumeType(), volumeToAttach.getDiskController(), volumeToAttach.getFormat());
        final AttachCommand cmd = new AttachCommand(disk, vm.getInstanceName());
        final ChapInfo chapInfo = this.volService.getChapInfo(this.volFactory.getVolume(volumeToAttach.getId()), dataStore);
        final Map<String, String> details = new HashMap<>();
        disk.setDetails(details);
        details.put(DiskTO.MANAGED, String.valueOf(volumeToAttachStoragePool.isManaged()));
        details.put(DiskTO.STORAGE_HOST, volumeToAttachStoragePool.getHostAddress());
        details.put(DiskTO.STORAGE_PORT, String.valueOf(volumeToAttachStoragePool.getPort()));
        details.put(DiskTO.VOLUME_SIZE, String.valueOf(volumeToAttach.getSize()));
        details.put(DiskTO.IQN, volumeToAttach.get_iScsiName());
        details.put(DiskTO.MOUNT_POINT, volumeToAttach.get_iScsiName());
        details.put(DiskTO.PROTOCOL_TYPE, volumeToAttach.getPoolType() != null ? volumeToAttach.getPoolType().toString() : null);
        if (chapInfo != null) {
            details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInfo.getInitiatorUsername());
            details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInfo.getInitiatorSecret());
            details.put(DiskTO.CHAP_TARGET_USERNAME, chapInfo.getTargetUsername());
            details.put(DiskTO.CHAP_TARGET_SECRET, chapInfo.getTargetSecret());
        }
        this._userVmDao.loadDetails(vm);
        try {
            answer = (AttachAnswer) this._agentMgr.send(hostId, cmd);
        } catch (final Exception e) {
            if (host != null) {
                this.volService.revokeAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
            }
            throw new CloudRuntimeException(errorMsg + " due to: " + e.getMessage());
        }
    }
    if (!sendCommand || answer != null && answer.getResult()) {
        // Mark the volume as attached
        if (sendCommand) {
            final DiskTO disk = answer.getDisk();
            this._volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), disk.getDiskSeq(), diskController);
            volumeToAttach = this._volsDao.findById(volumeToAttach.getId());
            if (volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
                volumeToAttach.setPath(answer.getDisk().getPath());
                if (diskController != null) {
                    volumeToAttach.setDiskController(diskController);
                }
                this._volsDao.update(volumeToAttach.getId(), volumeToAttach);
            }
        } else {
            deviceId = getDeviceId(vm, deviceId);
            this._volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), deviceId, diskController);
        }
        // insert record for disk I/O statistics
        VmDiskStatisticsVO diskstats = this._vmDiskStatsDao.findBy(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
        if (diskstats == null) {
            diskstats = new VmDiskStatisticsVO(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
            this._vmDiskStatsDao.persist(diskstats);
        }
        // Change MaintenancePolicy when adding disk with HOST scope
        if (volumeToAttachStoragePool != null && ScopeType.HOST.equals(volumeToAttachStoragePool.getScope())) {
            vm.setMaintenancePolicy(MaintenancePolicy.ShutdownAndStart);
            s_logger.debug("Setting MaintenancePolicy to '" + MaintenancePolicy.ShutdownAndStart.toString() + "' for VM " + vm.getInstanceName());
            _vmInstanceDao.persist(vm);
        }
        return this._volsDao.findById(volumeToAttach.getId());
    } else {
        if (answer != null) {
            final String details = answer.getDetails();
            if (details != null && !details.isEmpty()) {
                errorMsg += "; " + details;
            }
        }
        if (host != null) {
            this.volService.revokeAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
        }
        throw new CloudRuntimeException(errorMsg);
    }
}
Also used : HashMap(java.util.HashMap) ChapInfo(com.cloud.engine.subsystem.api.storage.ChapInfo) VmDiskStatisticsVO(com.cloud.user.VmDiskStatisticsVO) HostVO(com.cloud.host.HostVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) CloudException(com.cloud.legacymodel.exceptions.CloudException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) MalformedURLException(java.net.MalformedURLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AttachCommand(com.cloud.legacymodel.communication.command.AttachCommand) DataTO(com.cloud.legacymodel.to.DataTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) DiskTO(com.cloud.legacymodel.to.DiskTO)

Aggregations

AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)9 DiskTO (com.cloud.legacymodel.to.DiskTO)9 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)5 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)5 Answer (com.cloud.legacymodel.communication.answer.Answer)4 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)4 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)4 DataTO (com.cloud.legacymodel.to.DataTO)4 NfsTO (com.cloud.legacymodel.to.NfsTO)4 TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)4 DettachAnswer (com.cloud.legacymodel.communication.answer.DettachAnswer)3 AttachCommand (com.cloud.legacymodel.communication.command.AttachCommand)3 Connection (com.xensource.xenapi.Connection)3 XenAPIException (com.xensource.xenapi.Types.XenAPIException)3 VBD (com.xensource.xenapi.VBD)3 VDI (com.xensource.xenapi.VDI)3 VM (com.xensource.xenapi.VM)3 XmlRpcException (org.apache.xmlrpc.XmlRpcException)3 Connect (org.libvirt.Connect)3 LibvirtException (org.libvirt.LibvirtException)3