use of org.libvirt.Domain in project cloudstack by apache.
the class LibvirtComputingResource method getVmState.
public PowerState getVmState(final Connect conn, final String vmName) {
int retry = 3;
Domain vms = null;
while (retry-- > 0) {
try {
vms = conn.domainLookupByName(vmName);
final PowerState s = convertToPowerState(vms.getInfo().state);
return s;
} catch (final LibvirtException e) {
s_logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
} finally {
try {
if (vms != null) {
vms.free();
}
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
return PowerState.PowerOff;
}
use of org.libvirt.Domain in project cloudstack by apache.
the class LibvirtComputingResource method attachOrDetachDevice.
protected synchronized String attachOrDetachDevice(final Connect conn, final boolean attach, final String vmName, final String xml) throws LibvirtException, InternalErrorException {
Domain dm = null;
try {
dm = conn.domainLookupByName(vmName);
if (attach) {
s_logger.debug("Attaching device: " + xml);
dm.attachDevice(xml);
} else {
s_logger.debug("Detaching device: " + xml);
dm.detachDevice(xml);
}
} catch (final LibvirtException e) {
if (attach) {
s_logger.warn("Failed to attach device to " + vmName + ": " + e.getMessage());
} else {
s_logger.warn("Failed to detach device from " + vmName + ": " + e.getMessage());
}
throw e;
} finally {
if (dm != null) {
try {
dm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
return null;
}
use of org.libvirt.Domain in project cloudstack by apache.
the class LibvirtComputingResource method getAllVmNames.
protected List<String> getAllVmNames(final Connect conn) {
final ArrayList<String> la = new ArrayList<String>();
try {
final String[] names = conn.listDefinedDomains();
for (int i = 0; i < names.length; i++) {
la.add(names[i]);
}
} catch (final LibvirtException e) {
s_logger.warn("Failed to list Defined domains", e);
}
int[] ids = null;
try {
ids = conn.listDomains();
} catch (final LibvirtException e) {
s_logger.warn("Failed to list domains", e);
return la;
}
Domain dm = null;
for (int i = 0; i < ids.length; i++) {
try {
dm = conn.domainLookupByID(ids[i]);
la.add(dm.getName());
} catch (final LibvirtException e) {
s_logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
s_logger.trace("Ignoring libvirt error.", e);
}
}
}
return la;
}
use of org.libvirt.Domain in project cloudstack by apache.
the class LibvirtComputingResource method getInterfaces.
public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
Domain dm = null;
try {
dm = conn.domainLookupByName(vmName);
parser.parseDomainXML(dm.getXMLDesc(0));
return parser.getInterfaces();
} catch (final LibvirtException e) {
s_logger.debug("Failed to get dom xml: " + e.toString());
return new ArrayList<InterfaceDef>();
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
s_logger.trace("Ignoring libvirt error.", e);
}
}
}
use of org.libvirt.Domain in project cloudstack by apache.
the class LibvirtComputingResource method startVM.
public String startVM(final Connect conn, final String vmName, final String domainXML) throws LibvirtException, InternalErrorException {
try {
/*
We create a transient domain here. When this method gets
called we receive a full XML specification of the guest,
so no need to define it persistent.
This also makes sure we never have any old "garbage" defined
in libvirt which might haunt us.
*/
// check for existing inactive vm definition and remove it
// this can sometimes happen during crashes, etc
Domain dm = null;
try {
dm = conn.domainLookupByName(vmName);
if (dm != null && dm.isPersistent() == 1) {
// this is safe because it doesn't stop running VMs
dm.undefine();
}
} catch (final LibvirtException e) {
// this is what we want, no domain found
} finally {
if (dm != null) {
dm.free();
}
}
conn.domainCreateXML(domainXML, 0);
} catch (final LibvirtException e) {
throw e;
}
return null;
}
Aggregations