Skip to content

Commit fce59b8

Browse files
author
Marcus Sorensen
committed
Summary: Code cleanup for KVM - look up domains by name rather than coversion
Detail: We do two strange things, apache#1, when a vm is created, we create the uuid for the domain by converting the name into a uuid, then subsequently, any time we look for a domain, we convert its name to a uuid and domainLookupByUUID. This is an unnecessary obfuscation, so instead we just do a domainLookupByName. As a bonus, we are now setting the domain's uuid to be the cloudstack uuid. It is no longer used anywhere, but will be less confusing to admins. This shouldn't affect upgrades, since we can always lookup existing VMs by name. Signed-off-by: Marcus Sorensen <marcus@betterservers.com> 1369287049 -0600
1 parent 795fd80 commit fce59b8

2 files changed

Lines changed: 18 additions & 31 deletions

File tree

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

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,8 +2619,7 @@ protected State getVmState(Connect conn, final String vmName) {
26192619
Domain vms = null;
26202620
while (retry-- > 0) {
26212621
try {
2622-
vms = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
2623-
.getBytes()));
2622+
vms = conn.domainLookupByName(vmName);
26242623
State s = convertToState(vms.getInfo().state);
26252624
return s;
26262625
} catch (final LibvirtException e) {
@@ -2712,8 +2711,7 @@ private synchronized Answer execute(MigrateCommand cmd) {
27122711
try {
27132712
conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
27142713
ifaces = getInterfaces(conn, vmName);
2715-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
2716-
.getBytes()));
2714+
dm = conn.domainLookupByName(vmName);
27172715
dconn = new Connect("qemu+tcp://" + cmd.getDestinationIp()
27182716
+ "/system");
27192717
/*
@@ -3162,8 +3160,7 @@ protected void handleVmStartFailure(Connect conn, String vmName,
31623160
protected LibvirtVMDef createVMFromSpec(VirtualMachineTO vmTO) {
31633161
LibvirtVMDef vm = new LibvirtVMDef();
31643162
vm.setDomainName(vmTO.getName());
3165-
vm.setDomUUID(UUID.nameUUIDFromBytes(vmTO.getName().getBytes())
3166-
.toString());
3163+
vm.setDomUUID(vmTO.getUuid());
31673164
vm.setDomDescription(vmTO.getOs());
31683165

31693166
GuestDef guest = new GuestDef();
@@ -3587,8 +3584,7 @@ protected synchronized String attachOrDetachDisk(Connect conn,
35873584
KVMStoragePool attachingPool = attachingDisk.getPool();
35883585
try {
35893586
if (!attach) {
3590-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
3591-
.getBytes()));
3587+
dm = conn.domainLookupByName(vmName);
35923588
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
35933589
String xml = dm.getXMLDesc(0);
35943590
parser.parseDomainXML(xml);
@@ -3637,9 +3633,7 @@ protected synchronized String attachOrDetachDevice(Connect conn,
36373633
InternalErrorException {
36383634
Domain dm = null;
36393635
try {
3640-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes((vmName
3641-
.getBytes())));
3642-
3636+
dm = conn.domainLookupByName(vmName);
36433637
if (attach) {
36443638
s_logger.debug("Attaching device: " + xml);
36453639
dm.attachDevice(xml);
@@ -3870,8 +3864,7 @@ protected State getRealPowerState(String vm) {
38703864
for (; i < 5; i++) {
38713865
try {
38723866
Connect conn = LibvirtConnection.getConnectionByVmName(vm);
3873-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vm
3874-
.getBytes()));
3867+
dm = conn.domainLookupByName(vm);
38753868
DomainInfo.DomainState vps = dm.getInfo().state;
38763869
if (vps != null
38773870
&& vps != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF
@@ -4008,8 +4001,7 @@ private HashMap<String, State> getAllVms(Connect conn) {
40084001
for (int i = 0; i < vms.length; i++) {
40094002
try {
40104003

4011-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vms[i]
4012-
.getBytes()));
4004+
dm = conn.domainLookupByName(vms[i]);
40134005

40144006
DomainInfo.DomainState ps = dm.getInfo().state;
40154007
final State state = convertToState(ps);
@@ -4114,8 +4106,7 @@ protected String rebootVM(Connect conn, String vmName) {
41144106
Domain dm = null;
41154107
String msg = null;
41164108
try {
4117-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
4118-
.getBytes()));
4109+
dm = conn.domainLookupByName(vmName);
41194110
String vmDef = dm.getXMLDesc(0);
41204111
s_logger.debug(vmDef);
41214112
msg = stopVM(conn, vmName);
@@ -4157,8 +4148,7 @@ protected String stopVM(Connect conn, String vmName) {
41574148
/* Retry 3 times, to make sure we can get the vm's status */
41584149
for (int i = 0; i < 3; i++) {
41594150
try {
4160-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
4161-
.getBytes()));
4151+
dm = conn.domainLookupByName(vmName);
41624152
state = dm.getInfo().state;
41634153
break;
41644154
} catch (LibvirtException e) {
@@ -4194,8 +4184,7 @@ protected String stopVM(Connect conn, String vmName) {
41944184
protected String stopVM(Connect conn, String vmName, boolean force) {
41954185
Domain dm = null;
41964186
try {
4197-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
4198-
.getBytes()));
4187+
dm = conn.domainLookupByName(vmName);
41994188
int persist = dm.isPersistent();
42004189
if (force) {
42014190
if (dm.isActive() == 1) {
@@ -4282,8 +4271,7 @@ protected Integer getVncPort(Connect conn, String vmName)
42824271
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
42834272
Domain dm = null;
42844273
try {
4285-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
4286-
.getBytes()));
4274+
dm = conn.domainLookupByName(vmName);
42874275
String xmlDesc = dm.getXMLDesc(0);
42884276
parser.parseDomainXML(xmlDesc);
42894277
return parser.getVncPort();
@@ -4328,8 +4316,7 @@ private String getGuestType(Connect conn, String vmName) {
43284316
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
43294317
Domain dm = null;
43304318
try {
4331-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
4332-
.getBytes()));
4319+
dm = conn.domainLookupByName(vmName);
43334320
String xmlDesc = dm.getXMLDesc(0);
43344321
parser.parseDomainXML(xmlDesc);
43354322
return parser.getDescription();
@@ -4427,15 +4414,14 @@ private void cleanupVMNetworks(Connect conn, List<InterfaceDef> nics) {
44274414
private Domain getDomain(Connect conn, String vmName)
44284415
throws LibvirtException {
44294416
return conn
4430-
.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
4417+
.domainLookupByName(vmName);
44314418
}
44324419

44334420
protected List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
44344421
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
44354422
Domain dm = null;
44364423
try {
4437-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
4438-
.getBytes()));
4424+
dm = conn.domainLookupByName(vmName);
44394425
parser.parseDomainXML(dm.getXMLDesc(0));
44404426
return parser.getInterfaces();
44414427

@@ -4457,8 +4443,7 @@ protected List<DiskDef> getDisks(Connect conn, String vmName) {
44574443
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
44584444
Domain dm = null;
44594445
try {
4460-
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
4461-
.getBytes()));
4446+
dm = conn.domainLookupByName(vmName);
44624447
parser.parseDomainXML(dm.getXMLDesc(0));
44634448
return parser.getDisks();
44644449

‎plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void testCreateVMFromSpecLegacy() {
6060
LibvirtComputingResource lcr = new LibvirtComputingResource();
6161
VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, speed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword);
6262
to.setVncAddr(vncAddr);
63+
to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9");
6364

6465
LibvirtVMDef vm = lcr.createVMFromSpec(to);
6566
vm.setHvsType(_hyperVisorType);
@@ -135,6 +136,7 @@ public void testCreateVMFromSpec() {
135136
LibvirtComputingResource lcr = new LibvirtComputingResource();
136137
VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, minSpeed, maxSpeed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword);
137138
to.setVncAddr(vncAddr);
139+
to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9");
138140

139141
LibvirtVMDef vm = lcr.createVMFromSpec(to);
140142
vm.setHvsType(_hyperVisorType);
@@ -181,4 +183,4 @@ public void testCreateVMFromSpec() {
181183

182184
assertEquals(vmStr, vm.toString());
183185
}
184-
}
186+
}

0 commit comments

Comments
 (0)