use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtCheckVirtualMachineCommandWrapper method execute.
@Override
public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) {
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName());
Integer vncPort = null;
if (state == PowerState.PowerOn) {
vncPort = libvirtComputingResource.getVncPort(conn, command.getVmName());
}
return new CheckVirtualMachineAnswer(command, state, vncPort);
} catch (final LibvirtException e) {
return new CheckVirtualMachineAnswer(command, e.getMessage());
}
}
use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtGetVmDiskStatsCommandWrapper method execute.
@Override
public Answer execute(final GetVmDiskStatsCommand command, final LibvirtComputingResource libvirtComputingResource) {
final List<String> vmNames = command.getVmNames();
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
try {
final HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsNameMap = new HashMap<String, List<VmDiskStatsEntry>>();
final Connect conn = libvirtUtilitiesHelper.getConnection();
for (final String vmName : vmNames) {
try {
final List<VmDiskStatsEntry> statEntry = libvirtComputingResource.getVmDiskStat(conn, vmName);
if (statEntry == null) {
continue;
}
vmDiskStatsNameMap.put(vmName, statEntry);
} catch (LibvirtException e) {
s_logger.warn("Can't get vm disk stats: " + e.toString() + ", continue");
}
}
return new GetVmDiskStatsAnswer(command, "", command.getHostName(), vmDiskStatsNameMap);
} catch (final LibvirtException e) {
s_logger.debug("Can't get vm disk stats: " + e.toString());
return new GetVmDiskStatsAnswer(command, null, null, null);
}
}
use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtComputingResource method stopVM.
protected String stopVM(final Connect conn, final String vmName, final boolean force) {
Domain dm = null;
try {
dm = conn.domainLookupByName(vmName);
final int persist = dm.isPersistent();
if (force) {
if (dm.isActive() == 1) {
dm.destroy();
if (persist == 1) {
dm.undefine();
}
}
} else {
if (dm.isActive() == 0) {
return null;
}
dm.shutdown();
int retry = _stopTimeout / 2000;
/* Wait for the domain gets into shutoff state. When it does
the dm object will no longer work, so we need to catch it. */
try {
while (dm.isActive() == 1 && retry >= 0) {
Thread.sleep(2000);
retry--;
}
} catch (final LibvirtException e) {
final String error = e.toString();
if (error.contains("Domain not found")) {
s_logger.debug("successfully shut down vm " + vmName);
} else {
s_logger.debug("Error in waiting for vm shutdown:" + error);
}
}
if (retry < 0) {
s_logger.warn("Timed out waiting for domain " + vmName + " to shutdown gracefully");
return Script.ERR_TIMEOUT;
} else {
if (persist == 1) {
dm.undefine();
}
}
}
} catch (final LibvirtException e) {
if (e.getMessage().contains("Domain not found")) {
s_logger.debug("VM " + vmName + " doesn't exist, no need to stop it");
return null;
}
s_logger.debug("Failed to stop VM :" + vmName + " :", e);
return e.getMessage();
} catch (final InterruptedException ie) {
s_logger.debug("Interrupted sleep");
return ie.getMessage();
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
s_logger.trace("Ignoring libvirt error.", e);
}
}
return null;
}
use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtComputingResource method cleanupNetworkElementCommand.
protected ExecutionResult cleanupNetworkElementCommand(final IpAssocCommand cmd) {
final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
final String lastIp = cmd.getAccessDetail(NetworkElementCommand.NETWORK_PUB_LAST_IP);
Connect conn;
try {
conn = LibvirtConnection.getConnectionByVmName(routerName);
final List<InterfaceDef> nics = getInterfaces(conn, routerName);
final Map<String, Integer> broadcastUriAllocatedToVM = new HashMap<String, Integer>();
Integer nicPos = 0;
for (final InterfaceDef nic : nics) {
if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
broadcastUriAllocatedToVM.put("LinkLocal", nicPos);
} else {
if (nic.getBrName().equalsIgnoreCase(_publicBridgeName) || nic.getBrName().equalsIgnoreCase(_privBridgeName) || nic.getBrName().equalsIgnoreCase(_guestBridgeName)) {
broadcastUriAllocatedToVM.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), nicPos);
} else {
final String broadcastUri = getBroadcastUriFromBridge(nic.getBrName());
broadcastUriAllocatedToVM.put(broadcastUri, nicPos);
}
}
nicPos++;
}
final IpAddressTO[] ips = cmd.getIpAddresses();
final int numOfIps = ips.length;
int nicNum = 0;
for (final IpAddressTO ip : ips) {
if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
/* plug a vif into router */
VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress());
broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++);
}
nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri());
if (org.apache.commons.lang.StringUtils.equalsIgnoreCase(lastIp, "true") && !ip.isAdd()) {
// in isolated network eth2 is the default public interface. We don't want to delete it.
if (nicNum != 2) {
vifHotUnPlug(conn, routerName, ip.getVifMacAddress());
networkUsage(routerIp, "deleteVif", "eth" + nicNum);
}
}
}
} catch (final LibvirtException e) {
s_logger.error("ipassoccmd failed", e);
return new ExecutionResult(false, e.getMessage());
} catch (final InternalErrorException e) {
s_logger.error("ipassoccmd failed", e);
return new ExecutionResult(false, e.getMessage());
}
return new ExecutionResult(true, null);
}
use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtGetVncPortCommandWrapper method execute.
@Override
public Answer execute(final GetVncPortCommand command, final LibvirtComputingResource libvirtComputingResource) {
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getName());
final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName());
return new GetVncPortAnswer(command, libvirtComputingResource.getPrivateIp(), 5900 + vncPort);
} catch (final LibvirtException e) {
return new GetVncPortAnswer(command, e.toString());
}
}
Aggregations