private Long getHostId(VolumeInfo volumeInfo) { VirtualMachine virtualMachine = volumeInfo.getAttachedVM(); if (virtualMachine == null) { return null; } Long hostId = virtualMachine.getHostId(); if (hostId == null) { hostId = virtualMachine.getLastHostId(); } return hostId; }
protected Long destroyVM(final HaWorkVO work) { final VirtualMachine vm = _itMgr.findById(work.getInstanceId()); s_logger.info("Destroying " + vm.toString()); try { if (vm.getState() != State.Destroyed) { s_logger.info("VM is no longer in Destroyed state " + vm.toString()); return null; } if (vm.getHostId() != null) { _itMgr.destroy(vm.getUuid(), false); s_logger.info("Successfully destroy " + vm); return null; } else { if (s_logger.isDebugEnabled()) { s_logger.debug(vm + " has already been stopped"); } return null; } } catch (final AgentUnavailableException e) { s_logger.debug("Agnet is not available" + e.getMessage()); } catch (OperationTimedoutException e) { s_logger.debug("operation timed out: " + e.getMessage()); } catch (ConcurrentOperationException e) { s_logger.debug("concurrent operation: " + e.getMessage()); } return (System.currentTimeMillis() >> 10) + _stopRetryInterval; }
private void handleAuthRequest(HttpServletRequest req, HttpServletResponse resp, long vmId) { // TODO authentication channel between console proxy VM and management server needs to be secured, // the data is now being sent through private network, but this is apparently not enough VirtualMachine vm = _vmMgr.findById(vmId); if (vm == null) { s_logger.warn("VM " + vmId + " does not exist, sending failed response for authentication request from console proxy"); sendResponse(resp, "failed"); return; } if (vm.getHostId() == null) { s_logger.warn("VM " + vmId + " lost host info, failed response for authentication request from console proxy"); sendResponse(resp, "failed"); return; } HostVO host = _ms.getHostBy(vm.getHostId()); if (host == null) { s_logger.warn("VM " + vmId + "'s host does not exist, sending failed response for authentication request from console proxy"); sendResponse(resp, "failed"); return; } String sid = req.getParameter("sid"); if (sid == null || !sid.equals(vm.getVncPassword())) { s_logger.warn("sid " + sid + " in url does not match stored sid."); sendResponse(resp, "failed"); return; } sendResponse(resp, "success"); }
/** * Set VM CPU quota percentage with respect to host CPU on 'to' if CPU limit option is set * @param to vm to * @param vmProfile vm profile */ protected void setVmQuotaPercentage(VirtualMachineTO to, VirtualMachineProfile vmProfile) { if (to.getLimitCpuUse()) { VirtualMachine vm = vmProfile.getVirtualMachine(); HostVO host = _hostDao.findById(vm.getHostId()); if (host == null) { throw new CloudRuntimeException("Host with id: " + vm.getHostId() + " not found"); } s_logger.debug("Limiting CPU usage for VM: " + vm.getUuid() + " on host: " + host.getUuid()); double hostMaxSpeed = getHostCPUSpeed(host); double maxSpeed = getVmSpeed(to); try { BigDecimal percent = new BigDecimal(maxSpeed / hostMaxSpeed); percent = percent.setScale(2, RoundingMode.HALF_DOWN); if (percent.compareTo(new BigDecimal(1)) == 1) { s_logger.debug("VM " + vm.getUuid() + " CPU MHz exceeded host " + host.getUuid() + " CPU MHz, limiting VM CPU to the host maximum"); percent = new BigDecimal(1); } to.setCpuQuotaPercentage(percent.doubleValue()); s_logger.debug("Host: " + host.getUuid() + " max CPU speed = " + hostMaxSpeed + "MHz, VM: " + vm.getUuid() + "max CPU speed = " + maxSpeed + "MHz. Setting CPU quota percentage as: " + percent.doubleValue()); } catch (NumberFormatException e) { s_logger.error("Error calculating VM: " + vm.getUuid() + " quota percentage, it wll not be set. Error: " + e.getMessage(), e); } } }
@Override public Pair<String, Integer> getVncPort(final VirtualMachine vm) { if (vm.getHostId() == null) { s_logger.warn("VM " + vm.getHostName() + " does not have host, return -1 for its VNC port"); return new Pair<String, Integer>(null, -1); } if (s_logger.isTraceEnabled()) { s_logger.trace("Trying to retrieve VNC port from agent about VM " + vm.getHostName()); } final GetVncPortAnswer answer = (GetVncPortAnswer)_agentMgr.easySend(vm.getHostId(), new GetVncPortCommand(vm.getId(), vm.getInstanceName())); if (answer != null && answer.getResult()) { return new Pair<String, Integer>(answer.getAddress(), answer.getPort()); } return new Pair<String, Integer>(null, -1); }
private Long findAgentId(VirtualMachineProfile profile, DeployDestination dest, DataStore dataStore) throws ResourceUnavailableException { Long agentId; if (dest.getHost() == null) { agentId = (profile.getVirtualMachine().getHostId() == null ? profile.getVirtualMachine().getLastHostId() : profile.getVirtualMachine().getHostId()); } else { agentId = dest.getHost().getId(); } if (!VirtualMachineManager.VmConfigDriveOnPrimaryPool.value()) { agentId = findAgentIdForImageStore(dataStore); } return agentId; }
@Override public boolean isVmAlive(VirtualMachine vm, Host host) throws UnknownVM { CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName()); try { CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer)_agentMgr.send(vm.getHostId(), cmd); if (!answer.getResult()) { s_logger.debug("Unable to get vm state on " + vm.toString()); throw new UnknownVM(); } s_logger.debug("Agent responded with state " + answer.getState().toString()); return answer.getState() == PowerState.PowerOn; } catch (AgentUnavailableException e) { s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage()); throw new UnknownVM(); } catch (OperationTimedoutException e) { s_logger.debug("Operation timed out for " + vm.toString() + ": " + e.getMessage()); throw new UnknownVM(); } } }
@Override public EndPoint select(DataObject srcData, DataObject destData, StorageAction action) { s_logger.error("IR24 select BACKUPSNAPSHOT from primary to secondary " + srcData.getId() + " dest=" + destData.getId()); if (action == StorageAction.BACKUPSNAPSHOT && srcData.getDataStore().getRole() == DataStoreRole.Primary) { SnapshotInfo srcSnapshot = (SnapshotInfo)srcData; VolumeInfo volumeInfo = srcSnapshot.getBaseVolume(); VirtualMachine vm = volumeInfo.getAttachedVM(); if (srcSnapshot.getHypervisorType() == Hypervisor.HypervisorType.KVM) { if (vm != null && vm.getState() == VirtualMachine.State.Running) { return getEndPointFromHostId(vm.getHostId()); } } if (srcSnapshot.getHypervisorType() == Hypervisor.HypervisorType.VMware) { if (vm != null) { Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId(); if (hostId != null) { return getEndPointFromHostId(hostId); } } } } return select(srcData, destData); }
VirtualMachine vm = volumeInfo.getAttachedVM(); if ((vm != null) && (vm.getState() == VirtualMachine.State.Running)) { Long hostId = vm.getHostId(); return getEndPointFromHostId(hostId); VirtualMachine vm = volume.getAttachedVM(); if ((vm != null) && (vm.getState() == VirtualMachine.State.Running)) { Long hostId = vm.getHostId(); return getEndPointFromHostId(hostId); VirtualMachine vm = volume.getAttachedVM(); if (vm != null) { Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId(); if (hostId != null) { return getEndPointFromHostId(hostId);
private boolean deleteConfigDriveIso(final VirtualMachine vm) throws ResourceUnavailableException { DataStore dataStore = _dataStoreMgr.getImageStore(vm.getDataCenterId()); Long agentId = findAgentIdForImageStore(dataStore); if (VirtualMachineManager.VmConfigDriveOnPrimaryPool.value()) { List<VolumeVO> volumes = _volumeDao.findByInstanceAndType(vm.getId(), Volume.Type.ROOT); if (volumes != null && volumes.size() > 0) { dataStore = _dataStoreMgr.getDataStore(volumes.get(0).getPoolId(), DataStoreRole.Primary); } agentId = (vm.getHostId() != null) ? vm.getHostId() : vm.getLastHostId(); } if (agentId == null || dataStore == null) { throw new ResourceUnavailableException("Config drive iso creation failed, agent or datastore not available", ConfigDriveNetworkElement.class, 0L); } LOG.debug("Deleting config drive ISO for vm: " + vm.getInstanceName()); final String isoPath = ConfigDrive.createConfigDrivePath(vm.getInstanceName()); final HandleConfigDriveIsoCommand configDriveIsoCommand = new HandleConfigDriveIsoCommand(isoPath, null, dataStore.getTO(), false); final Answer answer = agentManager.easySend(agentId, configDriveIsoCommand); if (!answer.getResult()) { LOG.error("Failed to remove config drive for instance: " + vm.getInstanceName()); return false; } return true; }
@Override public boolean release(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException { if (!canHandle(network, Service.Connectivity)) { return false; } if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vswitch) { return false; } if (nic.getTrafficType() != Networks.TrafficType.Guest) { return false; } final HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId()); _ovsTunnelMgr.checkAndRemoveHostFromTunnelNetwork(network, host); return true; }
@Override public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque) { State newState = transition.getToState(); State oldState = transition.getCurrentState(); if (newState != State.Starting && newState != State.Error && newState != State.Expunging) { return true; } if (vo.getHypervisorType() != HypervisorType.BareMetal) { return true; } HostVO host = _hostDao.findById(vo.getHostId()); if (host == null) { s_logger.debug("Skip oldState " + oldState + " to " + "newState " + newState + " transimtion"); return true; } _hostDao.loadDetails(host); if (newState == State.Starting) { host.setDetail("vmName", vo.getInstanceName()); s_logger.debug("Add vmName " + host.getDetail("vmName") + " to host " + host.getId() + " details"); } else { if (host.getDetail("vmName") != null && host.getDetail("vmName").equalsIgnoreCase(vo.getInstanceName())) { s_logger.debug("Remove vmName " + host.getDetail("vmName") + " from host " + host.getId() + " details"); host.getDetails().remove("vmName"); } } _hostDao.saveDetails(host); return true; }
@Override public SystemVmInstanceResponse createSystemVmInstanceResponse(VirtualMachine vm) { SystemVmInstanceResponse vmResponse = new SystemVmInstanceResponse(); vmResponse.setId(vm.getUuid()); vmResponse.setSystemVmType(vm.getType().toString().toLowerCase()); vmResponse.setName(vm.getHostName()); if (vm.getHostId() != null) { Host host = ApiDBUtils.findHostById(vm.getHostId()); if (host != null) { vmResponse.setHostId(host.getUuid()); } } if (vm.getState() != null) { vmResponse.setState(vm.getState().toString()); } if (vm.getType() == Type.DomainRouter) { VirtualRouter router = (VirtualRouter)vm; if (router.getRole() != null) { vmResponse.setRole(router.getRole().toString()); } } vmResponse.setObjectName("systemvminstance"); return vmResponse; }
@Override public void releaseVlan(Network nw, VirtualMachineProfile vm) { List<BaremetalRctVO> vos = rctDao.listAll(); if (vos.isEmpty()) { throw new CloudRuntimeException("no rack configuration found, please call addBaremetalRct to add one"); } BaremetalRctVO vo = vos.get(0); BaremetalRct rct = gson.fromJson(vo.getRct(), BaremetalRct.class); HostVO host = hostDao.findById(vm.getVirtualMachine().getHostId()); RackPair rp = findRack(rct, host.getPrivateMacAddress()); assert rp != null : String.format("where is my rack???"); int vlan = Integer.parseInt(Networks.BroadcastDomainType.getValue(nw.getBroadcastUri())); BaremetalVlanStruct struct = new BaremetalVlanStruct(); struct.setHostMac(rp.host.getMac()); struct.setPort(rp.host.getPort()); struct.setSwitchIp(rp.rack.getL2Switch().getIp()); struct.setSwitchPassword(rp.rack.getL2Switch().getPassword()); struct.setSwitchType(rp.rack.getL2Switch().getType()); struct.setSwitchUsername(rp.rack.getL2Switch().getUsername()); struct.setVlan(vlan); BaremetalSwitchBackend backend = getSwitchBackend(rp.rack.getL2Switch().getType()); backend.removePortFromVlan(struct); }
@Override public void finalizeStop(VirtualMachineProfile profile, Answer answer) { VirtualMachine vm = profile.getVirtualMachine(); // release elastic IP here IPAddressVO ip = _ipAddressDao.findByAssociatedVmId(profile.getId()); if (ip != null && ip.getSystem()) { CallContext ctx = CallContext.current(); try { long networkId = ip.getAssociatedWithNetworkId(); Network guestNetwork = _networkDao.findById(networkId); NetworkOffering offering = _entityMgr.findById(NetworkOffering.class, guestNetwork.getNetworkOfferingId()); assert (offering.isAssociatePublicIP() == true) : "User VM should not have system owned public IP associated with it when offering configured not to associate public IP."; _rulesMgr.disableStaticNat(ip.getId(), ctx.getCallingAccount(), ctx.getCallingUserId(), true); } catch (Exception ex) { s_logger.warn("Failed to disable static nat and release system ip " + ip + " as a part of vm " + profile.getVirtualMachine() + " stop due to exception ", ex); } } List<NicVO> nics = _nicDao.listByVmId(vm.getId()); for (NicVO nic : nics) { NetworkVO network = _networkDao.findById(nic.getNetworkId()); if (network.getTrafficType() == TrafficType.Guest) { if (nic.getBroadcastUri() != null && nic.getBroadcastUri().getScheme().equals("pvlan")) { NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), 0, false, "pvlan-nic"); setupVmForPvlan(false, vm.getHostId(), nicProfile); } } } }
final Host host = _hostDao.findById(vm.getHostId()); final DeployDestination dest = new DeployDestination(dc, null, null, host);
@Override public VirtualMachineTO implement(VirtualMachineProfile vm) { VirtualMachineTO to = toVirtualMachineTO(vm); // Determine the VM's OS description GuestOSVO guestOS = _guestOsDao.findByIdIncludingRemoved(vm.getVirtualMachine().getGuestOSId()); to.setOs(guestOS.getDisplayName()); HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId()); GuestOSHypervisorVO guestOsMapping = null; if (host != null) { guestOsMapping = _guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), getHypervisorType().toString(), host.getHypervisorVersion()); } if (guestOsMapping == null || host == null) { to.setPlatformEmulator("Other"); } else { to.setPlatformEmulator(guestOsMapping.getGuestOsName()); } return to; }
@Override public VirtualMachineTO implement(VirtualMachineProfile vm) { VirtualMachineTO to = toVirtualMachineTO(vm); setVmQuotaPercentage(to, vm); // Determine the VM's OS description GuestOSVO guestOS = _guestOsDao.findByIdIncludingRemoved(vm.getVirtualMachine().getGuestOSId()); to.setOs(guestOS.getDisplayName()); HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId()); GuestOSHypervisorVO guestOsMapping = null; if (host != null) { guestOsMapping = _guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), getHypervisorType().toString(), host.getHypervisorVersion()); } if (guestOsMapping == null || host == null) { to.setPlatformEmulator("Other"); } else { to.setPlatformEmulator(guestOsMapping.getGuestOsName()); } return to; }
final Host host = _hostDao.findById(vm.getHostId()); final DeployDestination dest = new DeployDestination(dc, null, null, host);
HostVO host = hostDao.findById(vm.getVirtualMachine().getHostId()); GuestOSHypervisorVO guestOsMapping = null; if (host != null) {