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