Search in sources :

Example 1 with DettachCommand

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

the class TemplateManagerImpl method attachISOToVM.

private boolean attachISOToVM(long vmId, long isoId, boolean attach, boolean forced) {
    UserVmVO vm = _userVmDao.findById(vmId);
    if (vm == null) {
        return false;
    } else if (vm.getState() != State.Running) {
        return true;
    }
    // prepare ISO ready to mount on hypervisor resource level
    TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId(), vm.getHostId(), null);
    if (tmplt == null) {
        s_logger.error("Failed to prepare ISO ready to mount on hypervisor resource level");
        throw new CloudRuntimeException("Failed to prepare ISO ready to mount on hypervisor resource level");
    }
    String vmName = vm.getInstanceName();
    HostVO host = _hostDao.findById(vm.getHostId());
    if (host == null) {
        s_logger.warn("Host: " + vm.getHostId() + " does not exist");
        return false;
    }
    DataTO isoTO = tmplt.getTO();
    DiskTO disk = new DiskTO(isoTO, null, null, Volume.Type.ISO);
    HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
    VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    VirtualMachineTO vmTO = hvGuru.implement(profile);
    Command cmd = null;
    if (attach) {
        cmd = new AttachCommand(disk, vmName, vmTO.getDetails());
        ((AttachCommand) cmd).setForced(forced);
    } else {
        cmd = new DettachCommand(disk, vmName, vmTO.getDetails());
        ((DettachCommand) cmd).setForced(forced);
    }
    Answer a = _agentMgr.easySend(vm.getHostId(), cmd);
    return (a != null && a.getResult());
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) Answer(com.cloud.agent.api.Answer) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataTO(com.cloud.agent.api.to.DataTO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DestroyCommand(com.cloud.agent.api.storage.DestroyCommand) Command(com.cloud.agent.api.Command) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) ComputeChecksumCommand(com.cloud.agent.api.ComputeChecksumCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 2 with DettachCommand

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

the class UserVmManagerImpl method handleManagedStorage.

private void handleManagedStorage(UserVmVO vm, VolumeVO root) {
    if (Volume.State.Allocated.equals(root.getState())) {
        return;
    }
    StoragePoolVO storagePool = _storagePoolDao.findById(root.getPoolId());
    if (storagePool != null && storagePool.isManaged()) {
        Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
        if (hostId != null) {
            VolumeInfo volumeInfo = volFactory.getVolume(root.getId());
            Host host = _hostDao.findById(hostId);
            final Command cmd;
            if (host.getHypervisorType() == HypervisorType.XenServer) {
                DiskTO disk = new DiskTO(volumeInfo.getTO(), root.getDeviceId(), root.getPath(), root.getVolumeType());
                // it's OK in this case to send a detach command to the host for a root volume as this
                // will simply lead to the SR that supports the root volume being removed
                cmd = new DettachCommand(disk, vm.getInstanceName());
                DettachCommand detachCommand = (DettachCommand) cmd;
                detachCommand.setManaged(true);
                detachCommand.setStorageHost(storagePool.getHostAddress());
                detachCommand.setStoragePort(storagePool.getPort());
                detachCommand.set_iScsiName(root.get_iScsiName());
            } else if (host.getHypervisorType() == HypervisorType.VMware) {
                PrimaryDataStore primaryDataStore = (PrimaryDataStore) volumeInfo.getDataStore();
                Map<String, String> details = primaryDataStore.getDetails();
                if (details == null) {
                    details = new HashMap<>();
                    primaryDataStore.setDetails(details);
                }
                details.put(DiskTO.MANAGED, Boolean.TRUE.toString());
                cmd = new DeleteCommand(volumeInfo.getTO());
            } else if (host.getHypervisorType() == HypervisorType.KVM) {
                cmd = null;
            } else {
                throw new CloudRuntimeException("This hypervisor type is not supported on managed storage for this command.");
            }
            if (cmd != null) {
                Commands cmds = new Commands(Command.OnError.Stop);
                cmds.addCommand(cmd);
                try {
                    _agentMgr.send(hostId, cmds);
                } catch (Exception ex) {
                    throw new CloudRuntimeException(ex.getMessage());
                }
                if (!cmds.isSuccessful()) {
                    for (Answer answer : cmds.getAnswers()) {
                        if (!answer.getResult()) {
                            s_logger.warn("Failed to reset vm due to: " + answer.getDetails());
                            throw new CloudRuntimeException("Unable to reset " + vm + " due to " + answer.getDetails());
                        }
                    }
                }
            }
            // root.getPoolId() should be null if the VM we are detaching the disk from has never been started before
            DataStore dataStore = root.getPoolId() != null ? _dataStoreMgr.getDataStore(root.getPoolId(), DataStoreRole.Primary) : null;
            volumeMgr.revokeAccess(volFactory.getVolume(root.getId()), host, dataStore);
            if (dataStore != null) {
                handleTargetsForVMware(host.getId(), storagePool.getHostAddress(), storagePool.getPort(), root.get_iScsiName());
            }
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) Host(com.cloud.host.Host) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientServerCapacityException(com.cloud.exception.InsufficientServerCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) IOException(java.io.IOException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) AffinityConflictException(com.cloud.exception.AffinityConflictException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) GetVolumeStatsAnswer(com.cloud.agent.api.GetVolumeStatsAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) GetVmNetworkStatsAnswer(com.cloud.agent.api.GetVmNetworkStatsAnswer) Answer(com.cloud.agent.api.Answer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) ModifyTargetsCommand(com.cloud.agent.api.ModifyTargetsCommand) GetVolumeStatsCommand(com.cloud.agent.api.GetVolumeStatsCommand) GetVmNetworkStatsCommand(com.cloud.agent.api.GetVmNetworkStatsCommand) Command(com.cloud.agent.api.Command) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) GetVmIpAddressCommand(com.cloud.agent.api.GetVmIpAddressCommand) GetVmDiskStatsCommand(com.cloud.agent.api.GetVmDiskStatsCommand) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) Commands(com.cloud.agent.manager.Commands) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DiskTO(com.cloud.agent.api.to.DiskTO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 3 with DettachCommand

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

the class StorageManagerImpl method handleManagedStorage.

/**
 * This method only applies for managed storage.
 *
 * For XenServer and vSphere, see if we need to remove an SR or a datastore, then remove the underlying volume
 * from any applicable access control list (before other code attempts to delete the volume that supports it).
 *
 * For KVM, just tell the underlying storage plug-in to remove the volume from any applicable access control list
 * (before other code attempts to delete the volume that supports it).
 */
private void handleManagedStorage(Volume volume) {
    Long instanceId = volume.getInstanceId();
    if (instanceId != null) {
        StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
        if (storagePool != null && storagePool.isManaged()) {
            VMInstanceVO vmInstanceVO = _vmInstanceDao.findById(instanceId);
            if (vmInstanceVO == null) {
                return;
            }
            Long lastHostId = vmInstanceVO.getLastHostId();
            if (lastHostId != null) {
                HostVO host = _hostDao.findById(lastHostId);
                ClusterVO cluster = _clusterDao.findById(host.getClusterId());
                VolumeInfo volumeInfo = volFactory.getVolume(volume.getId());
                if (cluster.getHypervisorType() == HypervisorType.KVM) {
                    volService.revokeAccess(volumeInfo, host, volumeInfo.getDataStore());
                } else {
                    DataTO volTO = volFactory.getVolume(volume.getId()).getTO();
                    DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
                    DettachCommand cmd = new DettachCommand(disk, null);
                    cmd.setManaged(true);
                    cmd.setStorageHost(storagePool.getHostAddress());
                    cmd.setStoragePort(storagePool.getPort());
                    cmd.set_iScsiName(volume.get_iScsiName());
                    Answer answer = _agentMgr.easySend(lastHostId, cmd);
                    if (answer != null && answer.getResult()) {
                        volService.revokeAccess(volumeInfo, host, volumeInfo.getDataStore());
                    } else {
                        s_logger.warn("Unable to remove host-side clustered file system for the following volume: " + volume.getUuid());
                    }
                }
            }
        }
    }
}
Also used : ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) GetVolumeStatsAnswer(com.cloud.agent.api.GetVolumeStatsAnswer) Answer(com.cloud.agent.api.Answer) SyncVolumePathAnswer(org.apache.cloudstack.storage.command.SyncVolumePathAnswer) GetStoragePoolCapabilitiesAnswer(com.cloud.agent.api.GetStoragePoolCapabilitiesAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) ClusterVO(com.cloud.dc.ClusterVO) DataTO(com.cloud.agent.api.to.DataTO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) HostVO(com.cloud.host.HostVO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 4 with DettachCommand

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

the class VolumeApiServiceImpl method orchestrateDetachVolumeFromVM.

private Volume orchestrateDetachVolumeFromVM(long vmId, long volumeId) {
    Volume volume = _volsDao.findById(volumeId);
    VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    String errorMsg = "Failed to detach volume " + volume.getName() + " from VM " + vm.getHostName();
    boolean sendCommand = vm.getState() == State.Running;
    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 volumePool = _storagePoolDao.findByIdIncludingRemoved(volume.getPoolId());
    if (hostId != null) {
        host = _hostDao.findById(hostId);
        if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumePool != null && volumePool.isManaged()) {
            sendCommand = true;
        }
    }
    if (volumePool == null) {
        sendCommand = false;
    }
    Answer answer = null;
    if (sendCommand) {
        // collect vm disk statistics before detach a volume
        UserVmVO userVm = _userVmDao.findById(vmId);
        if (userVm != null && userVm.getType() == VirtualMachine.Type.User) {
            _userVmService.collectVmDiskStatistics(userVm);
        }
        DataTO volTO = volFactory.getVolume(volume.getId()).getTO();
        DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
        Map<String, String> details = new HashMap<String, String>();
        disk.setDetails(details);
        if (volume.getPoolId() != null) {
            StoragePoolVO poolVO = _storagePoolDao.findById(volume.getPoolId());
            if (poolVO.getParent() != 0L) {
                details.put(DiskTO.PROTOCOL_TYPE, Storage.StoragePoolType.DatastoreCluster.toString());
            }
        }
        DettachCommand cmd = new DettachCommand(disk, vm.getInstanceName());
        cmd.setManaged(volumePool.isManaged());
        cmd.setStorageHost(volumePool.getHostAddress());
        cmd.setStoragePort(volumePool.getPort());
        cmd.set_iScsiName(volume.get_iScsiName());
        try {
            answer = _agentMgr.send(hostId, cmd);
        } catch (Exception e) {
            throw new CloudRuntimeException(errorMsg + " due to: " + e.getMessage());
        }
    }
    if (!sendCommand || (answer != null && answer.getResult())) {
        // Mark the volume as detached
        _volsDao.detachVolume(volume.getId());
        if (answer != null) {
            String datastoreName = answer.getContextParam("datastoreName");
            if (datastoreName != null) {
                StoragePoolVO storagePoolVO = _storagePoolDao.findByUuid(datastoreName);
                if (storagePoolVO != null) {
                    VolumeVO volumeVO = _volsDao.findById(volumeId);
                    volumeVO.setPoolId(storagePoolVO.getId());
                    _volsDao.update(volumeVO.getId(), volumeVO);
                } else {
                    s_logger.warn(String.format("Unable to find datastore %s while updating the new datastore of the volume %d", datastoreName, volumeId));
                }
            }
            String volumePath = answer.getContextParam("volumePath");
            if (volumePath != null) {
                VolumeVO volumeVO = _volsDao.findById(volumeId);
                volumeVO.setPath(volumePath);
                _volsDao.update(volumeVO.getId(), volumeVO);
            }
            String chainInfo = answer.getContextParam("chainInfo");
            if (chainInfo != null) {
                VolumeVO volumeVO = _volsDao.findById(volumeId);
                volumeVO.setChainInfo(chainInfo);
                _volsDao.update(volumeVO.getId(), volumeVO);
            }
        }
        // volume.getPoolId() should be null if the VM we are detaching the disk from has never been started before
        if (volume.getPoolId() != null) {
            DataStore dataStore = dataStoreMgr.getDataStore(volume.getPoolId(), DataStoreRole.Primary);
            volService.revokeAccess(volFactory.getVolume(volume.getId()), host, dataStore);
        }
        if (volumePool != null && hostId != null) {
            handleTargetsForVMware(hostId, volumePool.getHostAddress(), volumePool.getPort(), volume.get_iScsiName());
        }
        return _volsDao.findById(volumeId);
    } else {
        if (answer != null) {
            String details = answer.getDetails();
            if (details != null && !details.isEmpty()) {
                errorMsg += "; " + details;
            }
        }
        throw new CloudRuntimeException(errorMsg);
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) HashMap(java.util.HashMap) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ServerApiException(org.apache.cloudstack.api.ServerApiException) 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) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) DataTO(com.cloud.agent.api.to.DataTO) 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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 5 with DettachCommand

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

the class XenServerGuru method finalizeExpungeVolumes.

@Override
public List<Command> finalizeExpungeVolumes(VirtualMachine vm) {
    List<Command> commands = new ArrayList<Command>();
    List<VolumeVO> volumes = volumeDao.findByInstance(vm.getId());
    // will simply lead to the SR that supports the root volume being removed
    if (volumes != null) {
        for (VolumeVO volume : volumes) {
            StoragePoolVO storagePool = storagePoolDao.findById(volume.getPoolId());
            // so the volume was never assigned to a storage pool)
            if (storagePool != null && storagePool.isManaged()) {
                DataTO volTO = volFactory.getVolume(volume.getId()).getTO();
                DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
                DettachCommand cmd = new DettachCommand(disk, vm.getInstanceName());
                cmd.setManaged(true);
                cmd.setStorageHost(storagePool.getHostAddress());
                cmd.setStoragePort(storagePool.getPort());
                cmd.set_iScsiName(volume.get_iScsiName());
                commands.add(cmd);
            }
        }
    }
    return commands;
}
Also used : DataTO(com.cloud.agent.api.to.DataTO) VolumeVO(com.cloud.storage.VolumeVO) StorageSubSystemCommand(org.apache.cloudstack.storage.command.StorageSubSystemCommand) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) Command(com.cloud.agent.api.Command) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) ArrayList(java.util.ArrayList) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

DiskTO (com.cloud.agent.api.to.DiskTO)5 DettachCommand (org.apache.cloudstack.storage.command.DettachCommand)5 Answer (com.cloud.agent.api.Answer)4 DataTO (com.cloud.agent.api.to.DataTO)4 Command (com.cloud.agent.api.Command)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)3 GetVolumeStatsAnswer (com.cloud.agent.api.GetVolumeStatsAnswer)2 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)2 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)2 HostVO (com.cloud.host.HostVO)2 VMInstanceVO (com.cloud.vm.VMInstanceVO)2 ComputeChecksumCommand (com.cloud.agent.api.ComputeChecksumCommand)1 GetStoragePoolCapabilitiesAnswer (com.cloud.agent.api.GetStoragePoolCapabilitiesAnswer)1 GetStorageStatsAnswer (com.cloud.agent.api.GetStorageStatsAnswer)1 GetVmDiskStatsAnswer (com.cloud.agent.api.GetVmDiskStatsAnswer)1 GetVmDiskStatsCommand (com.cloud.agent.api.GetVmDiskStatsCommand)1