Search in sources :

Example 6 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

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());
    } catch (final URISyntaxException e) {
        return new Answer(cmd, false, e.toString());
    } catch (final InternalErrorException e) {
        return new Answer(cmd, false, e.toString());
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) URISyntaxException(java.net.URISyntaxException) InternalErrorException(com.cloud.exception.InternalErrorException) NfsTO(com.cloud.agent.api.to.NfsTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 7 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class VolumeApiServiceImpl method sendAttachVolumeCommand.

private VolumeVO sendAttachVolumeCommand(UserVmVO vm, VolumeVO volumeToAttach, Long deviceId) {
    String errorMsg = "Failed to attach volume " + volumeToAttach.getName() + " to VM " + vm.getHostName();
    boolean sendCommand = vm.getState() == State.Running;
    AttachAnswer answer = null;
    Long hostId = vm.getHostId();
    if (hostId == null) {
        hostId = vm.getLastHostId();
        HostVO host = _hostDao.findById(hostId);
        if (host != null && host.getHypervisorType() == HypervisorType.VMware) {
            sendCommand = true;
        }
    }
    HostVO host = null;
    StoragePoolVO volumeToAttachStoragePool = _storagePoolDao.findById(volumeToAttach.getPoolId());
    if (hostId != null) {
        host = _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
    DataStore dataStore = volumeToAttachStoragePool != null ? 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 {
            volService.grantAccess(volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
        } catch (Exception e) {
            volService.revokeAccess(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());
            _volsDao.update(volumeToAttach.getId(), volumeToAttach);
        }
        DataTO volTO = volFactory.getVolume(volumeToAttach.getId()).getTO();
        deviceId = getDeviceId(vm, deviceId);
        DiskTO disk = storageMgr.getDiskWithThrottling(volTO, volumeToAttach.getVolumeType(), deviceId, volumeToAttach.getPath(), vm.getServiceOfferingId(), volumeToAttach.getDiskOfferingId());
        AttachCommand cmd = new AttachCommand(disk, vm.getInstanceName());
        ChapInfo chapInfo = volService.getChapInfo(volFactory.getVolume(volumeToAttach.getId()), dataStore);
        Map<String, String> details = new HashMap<String, String>();
        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());
        }
        _userVmDao.loadDetails(vm);
        Map<String, String> controllerInfo = new HashMap<String, String>();
        controllerInfo.put(VmDetailConstants.ROOT_DISK_CONTROLLER, vm.getDetail(VmDetailConstants.ROOT_DISK_CONTROLLER));
        controllerInfo.put(VmDetailConstants.DATA_DISK_CONTROLLER, vm.getDetail(VmDetailConstants.DATA_DISK_CONTROLLER));
        cmd.setControllerInfo(controllerInfo);
        s_logger.debug("Attach volume id:" + volumeToAttach.getId() + " on VM id:" + vm.getId() + " has controller info:" + controllerInfo);
        try {
            answer = (AttachAnswer) _agentMgr.send(hostId, cmd);
        } catch (Exception e) {
            if (host != null) {
                volService.revokeAccess(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) {
            DiskTO disk = answer.getDisk();
            _volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), disk.getDiskSeq());
            volumeToAttach = _volsDao.findById(volumeToAttach.getId());
            if (volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
                volumeToAttach.setPath(answer.getDisk().getPath());
                _volsDao.update(volumeToAttach.getId(), volumeToAttach);
            }
        } else {
            deviceId = getDeviceId(vm, deviceId);
            _volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), deviceId);
        }
        // insert record for disk I/O statistics
        VmDiskStatisticsVO diskstats = _vmDiskStatsDao.findBy(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
        if (diskstats == null) {
            diskstats = new VmDiskStatisticsVO(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
            _vmDiskStatsDao.persist(diskstats);
        }
        return _volsDao.findById(volumeToAttach.getId());
    } else {
        if (answer != null) {
            String details = answer.getDetails();
            if (details != null && !details.isEmpty()) {
                errorMsg += "; " + details;
            }
        }
        if (host != null) {
            volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
        }
        throw new CloudRuntimeException(errorMsg);
    }
}
Also used : HashMap(java.util.HashMap) ChapInfo(org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo) VmDiskStatisticsVO(com.cloud.user.VmDiskStatisticsVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) JsonParseException(com.google.gson.JsonParseException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 8 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class KVMStorageProcessor method dettachIso.

@Override
public Answer dettachIso(final DettachCommand 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(), false);
    } catch (final LibvirtException e) {
        return new Answer(cmd, false, e.toString());
    } catch (final URISyntaxException e) {
        return new Answer(cmd, false, e.toString());
    } catch (final InternalErrorException e) {
        return new Answer(cmd, false, e.toString());
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) URISyntaxException(java.net.URISyntaxException) InternalErrorException(com.cloud.exception.InternalErrorException) NfsTO(com.cloud.agent.api.to.NfsTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 9 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class VmwareStorageProcessor method attachVolume.

private Answer attachVolume(Command cmd, DiskTO disk, boolean isAttach, boolean isManaged, String vmName, String iScsiName, String storageHost, int storagePort, Map<String, String> controllerInfo) {
    VolumeObjectTO volumeTO = (VolumeObjectTO) disk.getData();
    DataStoreTO primaryStore = volumeTO.getDataStore();
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            String msg = "Unable to find the VM to execute AttachCommand, vmName: " + vmName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        vmName = vmMo.getName();
        ManagedObjectReference morDs = null;
        String diskUuid = volumeTO.getUuid().replace("-", "");
        if (isAttach && isManaged) {
            Map<String, String> details = disk.getDetails();
            morDs = prepareManagedStorage(context, hyperHost, diskUuid, iScsiName, storageHost, storagePort, null, details.get(DiskTO.CHAP_INITIATOR_USERNAME), details.get(DiskTO.CHAP_INITIATOR_SECRET), details.get(DiskTO.CHAP_TARGET_USERNAME), details.get(DiskTO.CHAP_TARGET_SECRET), volumeTO.getSize(), cmd);
        } else {
            if (storagePort == DEFAULT_NFS_PORT) {
                morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(diskUuid) : primaryStore.getUuid());
            } else {
                morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(iScsiName) : primaryStore.getUuid());
            }
        }
        if (morDs == null) {
            String msg = "Unable to find the mounted datastore to execute AttachCommand, vmName: " + vmName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        DatastoreMO dsMo = new DatastoreMO(context, morDs);
        String datastoreVolumePath;
        if (isAttach) {
            if (isManaged) {
                datastoreVolumePath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
            } else {
                datastoreVolumePath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dsMo.getOwnerDatacenter().first(), vmName, dsMo, volumeTO.getPath());
            }
        } else {
            if (isManaged) {
                datastoreVolumePath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
            } else {
                datastoreVolumePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumeTO.getPath() + ".vmdk");
                if (!dsMo.fileExists(datastoreVolumePath)) {
                    datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, vmName, volumeTO.getPath() + ".vmdk");
                }
            }
        }
        disk.setPath(datastoreVolumePath);
        AttachAnswer answer = new AttachAnswer(disk);
        if (isAttach) {
            String diskController = getLegacyVmDataDiskController();
            if (controllerInfo != null && !Strings.isNullOrEmpty(controllerInfo.get(VmDetailConstants.DATA_DISK_CONTROLLER))) {
                diskController = controllerInfo.get(VmDetailConstants.DATA_DISK_CONTROLLER);
            }
            if (DiskControllerType.getType(diskController) == DiskControllerType.osdefault) {
                diskController = vmMo.getRecommendedDiskController(null);
            }
            vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs, diskController);
        } else {
            vmMo.removeAllSnapshots();
            vmMo.detachDisk(datastoreVolumePath, false);
            if (isManaged) {
                handleDatastoreAndVmdkDetachManaged(diskUuid, iScsiName, storageHost, storagePort);
            } else {
                VmwareStorageLayoutHelper.syncVolumeToRootFolder(dsMo.getOwnerDatacenter().first(), dsMo, volumeTO.getPath(), vmName);
            }
        }
        return answer;
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        String msg = "";
        if (isAttach)
            msg += "Failed to attach volume: " + e.getMessage();
        else
            msg += "Failed to detach volume: " + e.getMessage();
        s_logger.error(msg, e);
        return new AttachAnswer(msg);
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 10 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class SimulatorStorageProcessor method dettachIso.

@Override
public Answer dettachIso(DettachCommand cmd) {
    DiskTO disk = cmd.getDisk();
    TemplateObjectTO isoTO = (TemplateObjectTO) disk.getData();
    DataStoreTO store = isoTO.getDataStore();
    if (!(store instanceof NfsTO)) {
        return new AttachAnswer("unsupported protocol");
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Aggregations

AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)14 DiskTO (com.cloud.agent.api.to.DiskTO)11 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)7 NfsTO (com.cloud.agent.api.to.NfsTO)7 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)7 Answer (com.cloud.agent.api.Answer)6 InternalErrorException (com.cloud.exception.InternalErrorException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)6 DettachAnswer (org.apache.cloudstack.storage.command.DettachAnswer)5 DataTO (com.cloud.agent.api.to.DataTO)4 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)4 CreateObjectAnswer (org.apache.cloudstack.storage.command.CreateObjectAnswer)4 ResignatureAnswer (org.apache.cloudstack.storage.command.ResignatureAnswer)4 SnapshotAndCopyAnswer (org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer)4 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