agent: Do not define domains persistent in libvirt

We used to define domains persistent in libvirt, which caused XML definitions
to stay there after a reboot of the hypervisor.

We however don't do anything with those already defined domains, actually, we wipe
all defined domains when starting the agent.

Some users however reported that libvirt started these domains after a reboot
before the CloudStack agent was started.

By starting domains from the XML description and not defining them we prevent
them from ever being stored in libvirt.
This commit is contained in:
Wido den Hollander 2013-02-07 22:58:20 +01:00
parent f6c4b22139
commit 5dfcd309f1

View File

@ -368,10 +368,6 @@ ServerResource {
NATIVE, OPENVSWITCH
}
protected enum defineOps {
UNDEFINE_VM, DEFINE_VM
}
protected BridgeType _bridgeType;
private String getEndIpFromStartIp(String startIp, int numIps) {
@ -981,75 +977,22 @@ ServerResource {
protected String startDomain(Connect conn, String vmName, String domainXML)
throws LibvirtException, InternalErrorException {
/* No duplicated vm, we will success, or failed */
boolean failed = false;
Domain dm = null;
try {
dm = conn.domainDefineXML(domainXML);
/*
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.
*/
dm = conn.domainCreateXML(domainXML, 0);
} catch (final LibvirtException e) {
/* Duplicated defined vm */
s_logger.warn("Failed to define domain " + vmName + ": "
s_logger.warn("Failed to start domain " + vmName + ": "
+ e.getMessage());
failed = true;
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
}
}
/* If failed, undefine the vm */
Domain dmOld = null;
Domain dmNew = null;
try {
if (failed) {
dmOld = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
.getBytes()));
dmOld.undefine();
dmNew = conn.domainDefineXML(domainXML);
}
} catch (final LibvirtException e) {
s_logger.warn("Failed to define domain (second time) " + vmName
+ ": " + e.getMessage());
throw e;
} catch (Exception e) {
s_logger.warn("Failed to define domain (second time) " + vmName
+ ": " + e.getMessage());
throw new InternalErrorException(e.toString());
} finally {
try {
if (dmOld != null) {
dmOld.free();
}
if (dmNew != null) {
dmNew.free();
}
} catch (final LibvirtException e) {
}
}
/* Start the VM */
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
.getBytes()));
dm.create();
} catch (LibvirtException e) {
s_logger.warn("Failed to start domain: " + vmName + ": "
+ e.getMessage());
throw e;
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
}
}
return null;
}
@ -2845,7 +2788,7 @@ ServerResource {
List<InterfaceDef> ifaces = getInterfaces(conn, vmName);
destroy_network_rules_for_vm(conn, vmName);
String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
String result = stopVM(conn, vmName);
if (result == null) {
for (DiskDef disk : disks) {
if (disk.getDeviceType() == DiskDef.deviceType.CDROM
@ -3888,7 +3831,7 @@ ServerResource {
.getBytes()));
String vmDef = dm.getXMLDesc(0);
s_logger.debug(vmDef);
msg = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
msg = stopVM(conn, vmName);
msg = startDomain(conn, vmName, vmDef);
return null;
} catch (LibvirtException e) {
@ -3910,7 +3853,7 @@ ServerResource {
return msg;
}
protected String stopVM(Connect conn, String vmName, defineOps df) {
protected String stopVM(Connect conn, String vmName) {
DomainInfo.DomainState state = null;
Domain dm = null;
@ -3960,23 +3903,6 @@ ServerResource {
}
}
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) {
}
}
}
return null;
}