return msg;
}
protected String stopVM(Connect conn, String vmName, defineOps df) {
DomainInfo.DomainState state = null;
Domain dm = null;
s_logger.debug("Try to stop the vm at first");
String ret = stopVM(conn, vmName, false);
if (ret == Script.ERR_TIMEOUT) {
ret = stopVM(conn, vmName, true);
} else if (ret != null) {
/*
* There is a race condition between libvirt and qemu: libvirt
* listens on qemu's monitor fd. If qemu is shutdown, while libvirt
* is reading on the fd, then libvirt will report an error.
*/
/* Retry 3 times, to make sure we can get the vm's status */
for (int i = 0; i < 3; i++) {
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
.getBytes()));
state = dm.getInfo().state;
break;
} catch (LibvirtException e) {
s_logger.debug("Failed to get vm status:" + e.getMessage());
} catch (Exception e) {
s_logger.debug("Failed to get vm status:" + e.getMessage());
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException l) {
}
}
}
if (state == null) {
s_logger.debug("Can't get vm's status, assume it's dead already");
return null;
}
if (state != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) {
s_logger.debug("Try to destroy the vm");
ret = stopVM(conn, vmName, true);
if (ret != null) {
return ret;
}
}
}
if (df == defineOps.UNDEFINE_VM) {
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
.getBytes()));
dm.undefine();
} catch (LibvirtException e) {
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException l) {
}
}