Skip to content

Commit a94ff36

Browse files
author
Marcus Sorensen
committed
CLOUDSTACK-1648 - KVM - make storage pools non-persistent in libvirt. Persisting
cloud-defined resources on the host has caused various problems. As a backward compatible fix, if an existing pool with a different name collides with a pool being created (by path), the pool will be redefined with the name cloudstack knows about. This is actually what brought up the bug, a persisted storage pool cloudstack wasn't managing. Signed-off-by: Marcus Sorensen <marcus@betterservers.com> 1363210149 -0600
1 parent 7bb8ed4 commit a94ff36

2 files changed

Lines changed: 91 additions & 20 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3496,7 +3496,7 @@ public StartupCommand[] initialize() {
34963496
sscmd.setDataCenter(_dcId);
34973497
sscmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
34983498
} catch (CloudRuntimeException e) {
3499-
3499+
s_logger.debug("Unable to initialize local storage pool: " + e);
35003500
}
35013501

35023502
if (sscmd != null) {

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java‎

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,18 @@ private StoragePool createNfsStoragePool(Connect conn, String uuid,
120120
StoragePool sp = null;
121121
try {
122122
s_logger.debug(spd.toString());
123-
sp = conn.storagePoolDefineXML(spd.toString(), 0);
124-
sp.create(0);
123+
sp = conn.storagePoolCreateXML(spd.toString(), 0);
125124
return sp;
126125
} catch (LibvirtException e) {
127126
s_logger.error(e.toString());
128127
if (sp != null) {
129128
try {
130-
sp.undefine();
129+
if (sp.isPersistent() == 1) {
130+
sp.destroy();
131+
sp.undefine();
132+
} else {
133+
sp.destroy();
134+
}
131135
sp.free();
132136
} catch (LibvirtException l) {
133137
s_logger.debug("Failed to define nfs storage pool with: "
@@ -150,15 +154,18 @@ private StoragePool createSharedStoragePool(Connect conn, String uuid,
150154
StoragePool sp = null;
151155
try {
152156
s_logger.debug(spd.toString());
153-
sp = conn.storagePoolDefineXML(spd.toString(), 0);
154-
sp.create(0);
155-
157+
sp = conn.storagePoolCreateXML(spd.toString(), 0);
156158
return sp;
157159
} catch (LibvirtException e) {
158160
s_logger.error(e.toString());
159161
if (sp != null) {
160162
try {
161-
sp.undefine();
163+
if (sp.isPersistent() == 1) {
164+
sp.destroy();
165+
sp.undefine();
166+
} else {
167+
sp.destroy();
168+
}
162169
sp.free();
163170
} catch (LibvirtException l) {
164171
s_logger.debug("Failed to define shared mount point storage pool with: "
@@ -181,14 +188,18 @@ private StoragePool createCLVMStoragePool(Connect conn, String uuid,
181188
StoragePool sp = null;
182189
try {
183190
s_logger.debug(spd.toString());
184-
sp = conn.storagePoolDefineXML(spd.toString(), 0);
185-
sp.create(0);
191+
sp = conn.storagePoolCreateXML(spd.toString(), 0);
186192
return sp;
187193
} catch (LibvirtException e) {
188194
s_logger.error(e.toString());
189195
if (sp != null) {
190196
try {
191-
sp.undefine();
197+
if (sp.isPersistent() == 1) {
198+
sp.destroy();
199+
sp.undefine();
200+
} else {
201+
sp.destroy();
202+
}
192203
sp.free();
193204
} catch (LibvirtException l) {
194205
s_logger.debug("Failed to define clvm storage pool with: "
@@ -236,14 +247,18 @@ private StoragePool createRBDStoragePool(Connect conn, String uuid,
236247

237248
try {
238249
s_logger.debug(spd.toString());
239-
sp = conn.storagePoolDefineXML(spd.toString(), 0);
240-
sp.create(0);
250+
sp = conn.storagePoolCreateXML(spd.toString(), 0);
241251
return sp;
242252
} catch (LibvirtException e) {
243253
s_logger.debug(e.toString());
244254
if (sp != null) {
245255
try {
246-
sp.undefine();
256+
if (sp.isPersistent() == 1) {
257+
sp.destroy();
258+
sp.undefine();
259+
} else {
260+
sp.destroy();
261+
}
247262
sp.free();
248263
} catch (LibvirtException l) {
249264
s_logger.debug("Failed to define RBD storage pool with: " + l.toString());
@@ -385,15 +400,59 @@ public KVMStoragePool createStoragePool(String name, String host, int port,
385400

386401
try {
387402
sp = conn.storagePoolLookupByUUIDString(name);
388-
if (sp.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
403+
if (sp != null && sp.isActive() == 0) {
389404
sp.undefine();
390405
sp = null;
406+
s_logger.debug("Found existing defined storage pool " + name + ". It wasn't running, so we undefined it.");
407+
}
408+
if (sp != null) {
409+
s_logger.debug("Found existing defined storage pool " + name + ", using it.");
391410
}
392411
} catch (LibvirtException e) {
412+
sp = null;
413+
s_logger.debug("createStoragePool didn't find existing running pool: " + e + ", need to create it");
414+
}
393415

416+
// libvirt strips trailing slashes off of path, we will too in order to match
417+
// existing paths
418+
if (path.endsWith("/")) {
419+
path = path.substring(0, path.length() - 1);
394420
}
395421

396422
if (sp == null) {
423+
// see if any existing pool by another name is using our storage path.
424+
// if anyone is, undefine the pool so we can define it as requested.
425+
// This should be safe since a pool in use can't be removed, and no
426+
// volumes are affected by unregistering the pool with libvirt.
427+
s_logger.debug("Didn't find an existing storage pool " + name
428+
+ " by UUID, checking for pools with duplicate paths");
429+
430+
try {
431+
String[] poolnames = conn.listStoragePools();
432+
for (String poolname : poolnames) {
433+
s_logger.debug("Checking path of existing pool " + poolname
434+
+ " against pool we want to create");
435+
StoragePool p = conn.storagePoolLookupByName(poolname);
436+
LibvirtStoragePoolDef pdef = getStoragePoolDef(conn, p);
437+
438+
if (pdef.getTargetPath().equals(path)) {
439+
s_logger.debug("Storage pool utilizing path '" + path + "' already exists as pool "
440+
+ poolname + ", undefining so we can re-define with correct name " + name);
441+
if (p.isPersistent() == 1) {
442+
p.destroy();
443+
p.undefine();
444+
} else {
445+
p.destroy();
446+
}
447+
}
448+
}
449+
} catch (LibvirtException e) {
450+
s_logger.error("Failure in attempting to see if an existing storage pool might "
451+
+ "be using the path of the pool to be created:" + e);
452+
}
453+
454+
s_logger.debug("Attempting to create storage pool " + name);
455+
397456
if (type == StoragePoolType.NetworkFilesystem) {
398457
sp = createNfsStoragePool(conn, name, host, path);
399458
} else if (type == StoragePoolType.SharedMountPoint
@@ -407,8 +466,8 @@ public KVMStoragePool createStoragePool(String name, String host, int port,
407466
}
408467

409468
try {
410-
StoragePoolInfo spi = sp.getInfo();
411-
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
469+
if (sp.isActive() == 0) {
470+
s_logger.debug("attempting to activate pool " + name);
412471
sp.create(0);
413472
}
414473

@@ -427,7 +486,15 @@ public KVMStoragePool createStoragePool(String name, String host, int port,
427486

428487
return pool;
429488
} catch (LibvirtException e) {
430-
throw new CloudRuntimeException(e.toString());
489+
String error = e.toString();
490+
if (error.contains("Storage source conflict")) {
491+
throw new CloudRuntimeException("A pool matching this location already exists in libvirt, "
492+
+ " but has a different UUID/Name. Cannot create new pool without first "
493+
+ " removing it. Check for inactive pools via 'virsh pool-list --all'. "
494+
+ error);
495+
} else {
496+
throw new CloudRuntimeException(error);
497+
}
431498
}
432499
}
433500

@@ -459,8 +526,12 @@ public boolean deleteStoragePool(String uuid) {
459526
}
460527

461528
try {
462-
sp.destroy();
463-
sp.undefine();
529+
if (sp.isPersistent() == 1) {
530+
sp.destroy();
531+
sp.undefine();
532+
} else {
533+
sp.destroy();
534+
}
464535
sp.free();
465536
if (s != null) {
466537
s.undefine();

0 commit comments

Comments
 (0)