Search in sources :

Example 1 with DettachCommand

use of com.cloud.storage.command.DettachCommand in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method attachISOToVM.

private boolean attachISOToVM(final long vmId, final long isoId, final boolean attach) {
    final 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
    final TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId());
    final String vmName = vm.getInstanceName();
    final HostVO host = _hostDao.findById(vm.getHostId());
    if (host == null) {
        s_logger.warn("Host: " + vm.getHostId() + " does not exist");
        return false;
    }
    final DataTO isoTO = tmplt.getTO();
    final DiskTO disk = new DiskTO(isoTO, null, null, Volume.Type.ISO);
    final Command cmd;
    if (attach) {
        cmd = new AttachCommand(disk, vmName);
    } else {
        cmd = new DettachCommand(disk, vmName);
    }
    final Answer a = _agentMgr.easySend(vm.getHostId(), cmd);
    return a != null && a.getResult();
}
Also used : Answer(com.cloud.agent.api.Answer) UserVmVO(com.cloud.vm.UserVmVO) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) DataTO(com.cloud.agent.api.to.DataTO) DettachCommand(com.cloud.storage.command.DettachCommand) DestroyCommand(com.cloud.agent.api.storage.DestroyCommand) Command(com.cloud.agent.api.Command) TemplateOrVolumePostUploadCommand(com.cloud.storage.command.TemplateOrVolumePostUploadCommand) AttachCommand(com.cloud.storage.command.AttachCommand) ComputeChecksumCommand(com.cloud.agent.api.ComputeChecksumCommand) DettachCommand(com.cloud.storage.command.DettachCommand) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) VMTemplateHostVO(com.cloud.storage.VMTemplateHostVO) HostVO(com.cloud.host.HostVO) AttachCommand(com.cloud.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 2 with DettachCommand

use of com.cloud.storage.command.DettachCommand in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method handleManagedStorage.

private void handleManagedStorage(final UserVmVO vm, final VolumeVO root) {
    if (Volume.State.Allocated.equals(root.getState())) {
        return;
    }
    final StoragePoolVO storagePool = _storagePoolDao.findById(root.getPoolId());
    if (storagePool != null && storagePool.isManaged()) {
        final Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
        if (hostId != null) {
            final VolumeInfo volumeInfo = volFactory.getVolume(root.getId());
            final Host host = _hostDao.findById(hostId);
            final Command cmd;
            if (host.getHypervisorType() == HypervisorType.XenServer) {
                final 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());
                final DettachCommand detachCommand = (DettachCommand) cmd;
                detachCommand.setManaged(true);
                detachCommand.setStorageHost(storagePool.getHostAddress());
                detachCommand.setStoragePort(storagePool.getPort());
                detachCommand.set_iScsiName(root.get_iScsiName());
            } else {
                throw new CloudRuntimeException("This hypervisor type is not supported on managed storage for this command.");
            }
            final Commands cmds = new Commands(Command.OnError.Stop);
            cmds.addCommand(cmd);
            try {
                _agentMgr.send(hostId, cmds);
            } catch (final Exception ex) {
                throw new CloudRuntimeException(ex.getMessage());
            }
            if (!cmds.isSuccessful()) {
                for (final 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
            final DataStore dataStore = root.getPoolId() != null ? _dataStoreMgr.getDataStore(root.getPoolId(), DataStoreRole.Primary) : null;
            volumeMgr.revokeAccess(volFactory.getVolume(root.getId()), host, dataStore);
        }
    }
}
Also used : GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) 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) DettachCommand(com.cloud.storage.command.DettachCommand) Command(com.cloud.agent.api.Command) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) 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(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) DettachCommand(com.cloud.storage.command.DettachCommand) Commands(com.cloud.agent.manager.Commands) VolumeInfo(com.cloud.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) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) 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) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 3 with DettachCommand

use of com.cloud.storage.command.DettachCommand in project cosmic by MissionCriticalCloud.

the class XenServerGuru method finalizeExpungeVolumes.

@Override
public List<Command> finalizeExpungeVolumes(final VirtualMachine vm) {
    final List<Command> commands = new ArrayList<>();
    final List<VolumeVO> volumes = _volumeDao.findByInstance(vm.getId());
    // will simply lead to the SR that supports the root volume being removed
    if (volumes != null) {
        for (final VolumeVO volume : volumes) {
            final StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
            // so the volume was never assigned to a storage pool)
            if (storagePool != null && storagePool.isManaged()) {
                final DataTO volTO = _volFactory.getVolume(volume.getId()).getTO();
                final DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
                final 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(com.cloud.storage.command.StorageSubSystemCommand) DettachCommand(com.cloud.storage.command.DettachCommand) CopyCommand(com.cloud.storage.command.CopyCommand) Command(com.cloud.agent.api.Command) ArrayList(java.util.ArrayList) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) DettachCommand(com.cloud.storage.command.DettachCommand) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 4 with DettachCommand

use of com.cloud.storage.command.DettachCommand in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method orchestrateDetachVolumeFromVM.

private Volume orchestrateDetachVolumeFromVM(final long vmId, final long volumeId) {
    final Volume volume = _volsDao.findById(volumeId);
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    String errorMsg = "Failed to detach volume " + volume.getName() + " from VM " + vm.getHostName();
    boolean sendCommand = vm.getState() == State.Running;
    final Long hostId = vm.getHostId();
    HostVO host = null;
    final 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;
        }
    }
    Answer answer = null;
    if (sendCommand) {
        final DataTO volTO = volFactory.getVolume(volume.getId()).getTO();
        final DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
        final 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 (final 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());
        // volume.getPoolId() should be null if the VM we are detaching the disk from has never been started before
        final DataStore dataStore = volume.getPoolId() != null ? dataStoreMgr.getDataStore(volume.getPoolId(), DataStoreRole.Primary) : null;
        volService.revokeAccess(volFactory.getVolume(volume.getId()), host, dataStore);
        return _volsDao.findById(volumeId);
    } else {
        if (answer != null) {
            final String details = answer.getDetails();
            if (details != null && !details.isEmpty()) {
                errorMsg += "; " + details;
            }
        }
        throw new CloudRuntimeException(errorMsg);
    }
}
Also used : VMInstanceVO(com.cloud.vm.VMInstanceVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) CloudException(com.cloud.exception.CloudException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) Answer(com.cloud.agent.api.Answer) AttachAnswer(com.cloud.storage.command.AttachAnswer) 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(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) DettachCommand(com.cloud.storage.command.DettachCommand) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

DiskTO (com.cloud.agent.api.to.DiskTO)4 DettachCommand (com.cloud.storage.command.DettachCommand)4 Answer (com.cloud.agent.api.Answer)3 Command (com.cloud.agent.api.Command)3 DataTO (com.cloud.agent.api.to.DataTO)3 StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)3 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)2 CloudException (com.cloud.exception.CloudException)2 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)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 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)2 ComputeChecksumCommand (com.cloud.agent.api.ComputeChecksumCommand)1 GetVmDiskStatsAnswer (com.cloud.agent.api.GetVmDiskStatsAnswer)1 GetVmDiskStatsCommand (com.cloud.agent.api.GetVmDiskStatsCommand)1 GetVmIpAddressCommand (com.cloud.agent.api.GetVmIpAddressCommand)1 GetVmStatsAnswer (com.cloud.agent.api.GetVmStatsAnswer)1 GetVmStatsCommand (com.cloud.agent.api.GetVmStatsCommand)1