Skip to content

Commit 5dfcd30

Browse files
committed
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.
1 parent f6c4b22 commit 5dfcd30

1 file changed

Lines changed: 13 additions & 87 deletions

File tree

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,6 @@ protected enum BridgeType {
368368
NATIVE, OPENVSWITCH
369369
}
370370

371-
protected enum defineOps {
372-
UNDEFINE_VM, DEFINE_VM
373-
}
374-
375371
protected BridgeType _bridgeType;
376372

377373
private String getEndIpFromStartIp(String startIp, int numIps) {
@@ -981,75 +977,22 @@ boolean isDirectAttachedNetwork(String type) {
981977

982978
protected String startDomain(Connect conn, String vmName, String domainXML)
983979
throws LibvirtException, InternalErrorException {
984-
/* No duplicated vm, we will success, or failed */
985-
boolean failed = false;
986980
Domain dm = null;
987981
try {
988-
dm = conn.domainDefineXML(domainXML);
982+
/*
983+
We create a transient domain here. When this method gets
984+
called we receive a full XML specification of the guest,
985+
so no need to define it persistent.
986+
987+
This also makes sure we never have any old "garbage" defined
988+
in libvirt which might haunt us.
989+
*/
990+
dm = conn.domainCreateXML(domainXML, 0);
989991
} catch (final LibvirtException e) {
990-
/* Duplicated defined vm */
991-
s_logger.warn("Failed to define domain " + vmName + ": "
992+
s_logger.warn("Failed to start domain " + vmName + ": "
992993
+ e.getMessage());
993-
failed = true;
994-
} finally {
995-
try {
996-
if (dm != null) {
997-
dm.free();
998-
}
999-
} catch (final LibvirtException e) {
1000-
1001-
}
1002994
}
1003995

1004-
/* If failed, undefine the vm */
1005-
Domain dmOld = null;
1006-
Domain dmNew = null;
1007-
try {
1008-
if (failed) {
1009-
dmOld = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
1010-
.getBytes()));
1011-
dmOld.undefine();
1012-
dmNew = conn.domainDefineXML(domainXML);
1013-
}
1014-
} catch (final LibvirtException e) {
1015-
s_logger.warn("Failed to define domain (second time) " + vmName
1016-
+ ": " + e.getMessage());
1017-
throw e;
1018-
} catch (Exception e) {
1019-
s_logger.warn("Failed to define domain (second time) " + vmName
1020-
+ ": " + e.getMessage());
1021-
throw new InternalErrorException(e.toString());
1022-
} finally {
1023-
try {
1024-
if (dmOld != null) {
1025-
dmOld.free();
1026-
}
1027-
if (dmNew != null) {
1028-
dmNew.free();
1029-
}
1030-
} catch (final LibvirtException e) {
1031-
1032-
}
1033-
}
1034-
1035-
/* Start the VM */
1036-
try {
1037-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
1038-
.getBytes()));
1039-
dm.create();
1040-
} catch (LibvirtException e) {
1041-
s_logger.warn("Failed to start domain: " + vmName + ": "
1042-
+ e.getMessage());
1043-
throw e;
1044-
} finally {
1045-
try {
1046-
if (dm != null) {
1047-
dm.free();
1048-
}
1049-
} catch (final LibvirtException e) {
1050-
1051-
}
1052-
}
1053996
return null;
1054997
}
1055998

@@ -2845,7 +2788,7 @@ protected Answer execute(StopCommand cmd) {
28452788
List<InterfaceDef> ifaces = getInterfaces(conn, vmName);
28462789

28472790
destroy_network_rules_for_vm(conn, vmName);
2848-
String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
2791+
String result = stopVM(conn, vmName);
28492792
if (result == null) {
28502793
for (DiskDef disk : disks) {
28512794
if (disk.getDeviceType() == DiskDef.deviceType.CDROM
@@ -3888,7 +3831,7 @@ protected String rebootVM(Connect conn, String vmName) {
38883831
.getBytes()));
38893832
String vmDef = dm.getXMLDesc(0);
38903833
s_logger.debug(vmDef);
3891-
msg = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
3834+
msg = stopVM(conn, vmName);
38923835
msg = startDomain(conn, vmName, vmDef);
38933836
return null;
38943837
} catch (LibvirtException e) {
@@ -3910,7 +3853,7 @@ protected String rebootVM(Connect conn, String vmName) {
39103853
return msg;
39113854
}
39123855

3913-
protected String stopVM(Connect conn, String vmName, defineOps df) {
3856+
protected String stopVM(Connect conn, String vmName) {
39143857
DomainInfo.DomainState state = null;
39153858
Domain dm = null;
39163859

@@ -3960,23 +3903,6 @@ protected String stopVM(Connect conn, String vmName, defineOps df) {
39603903
}
39613904
}
39623905

3963-
if (df == defineOps.UNDEFINE_VM) {
3964-
try {
3965-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
3966-
.getBytes()));
3967-
dm.undefine();
3968-
} catch (LibvirtException e) {
3969-
3970-
} finally {
3971-
try {
3972-
if (dm != null) {
3973-
dm.free();
3974-
}
3975-
} catch (LibvirtException l) {
3976-
3977-
}
3978-
}
3979-
}
39803906
return null;
39813907
}
39823908

0 commit comments

Comments
 (0)