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();
}
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);
}
}
}
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;
}
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);
}
}
Aggregations