Skip to content

Commit f0e9284

Browse files
mlsorensensudison
authored andcommitted
KVM: Storage Pool collisions on ISOs due to using random UUID on pool
create The code is unable to detect an existing pool, because we use a random UUID each time. New Libvirt doesn't allow multiple pools to be defined to the same storage. This patch generates a UUID based on the storage path, so that it can be detected as existing and reused. It also cleans up no-op code and adjusts the naming of a few things to clean up any confusion. Signed-off-by: Edison Su <sudison@gmail.com>
1 parent 45360b7 commit f0e9284

3 files changed

Lines changed: 6 additions & 106 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public KVMStoragePool getStoragePool(String uuid) {
4949
}
5050

5151
public KVMStoragePool getStoragePoolByURI(String uri) {
52-
return this._storageAdaptor.getStoragePoolByUri(uri);
52+
return this._storageAdaptor.getStoragePoolByURI(uri);
5353
}
5454

5555
public KVMStoragePool createStoragePool(String name, String host, int port, String path,

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

Lines changed: 4 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -101,81 +101,6 @@ public StorageVol createVolume(Connect conn, StoragePool pool, String uuid,
101101
return pool.storageVolCreateXML(volDef.toString(), 0);
102102
}
103103

104-
public StoragePool getStoragePoolbyURI(Connect conn, URI uri)
105-
throws LibvirtException {
106-
String sourcePath;
107-
String uuid;
108-
String sourceHost = "";
109-
String protocal;
110-
if (uri.getScheme().equalsIgnoreCase("local")) {
111-
sourcePath = _mountPoint + File.separator
112-
+ uri.toString().replace("local:///", "");
113-
sourcePath = sourcePath.replace("//", "/");
114-
uuid = UUID.nameUUIDFromBytes(new String(sourcePath).getBytes())
115-
.toString();
116-
protocal = "DIR";
117-
} else {
118-
sourcePath = uri.getPath();
119-
sourcePath = sourcePath.replace("//", "/");
120-
sourceHost = uri.getHost();
121-
uuid = UUID.nameUUIDFromBytes(
122-
new String(sourceHost + sourcePath).getBytes()).toString();
123-
protocal = "NFS";
124-
}
125-
126-
String targetPath = _mountPoint + File.separator + uuid;
127-
StoragePool sp = null;
128-
try {
129-
sp = conn.storagePoolLookupByUUIDString(uuid);
130-
} catch (LibvirtException e) {
131-
}
132-
133-
if (sp == null) {
134-
try {
135-
LibvirtStoragePoolDef spd = null;
136-
if (protocal.equalsIgnoreCase("NFS")) {
137-
_storageLayer.mkdir(targetPath);
138-
spd = new LibvirtStoragePoolDef(poolType.NETFS, uuid, uuid,
139-
sourceHost, sourcePath, targetPath);
140-
s_logger.debug(spd.toString());
141-
// addStoragePool(uuid);
142-
143-
} else if (protocal.equalsIgnoreCase("DIR")) {
144-
_storageLayer.mkdir(targetPath);
145-
spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid,
146-
null, null, sourcePath);
147-
}
148-
149-
synchronized (getStoragePool(uuid)) {
150-
sp = conn.storagePoolDefineXML(spd.toString(), 0);
151-
if (sp == null) {
152-
s_logger.debug("Failed to define storage pool");
153-
return null;
154-
}
155-
sp.create(0);
156-
}
157-
158-
return sp;
159-
} catch (LibvirtException e) {
160-
try {
161-
if (sp != null) {
162-
sp.undefine();
163-
sp.free();
164-
}
165-
} catch (LibvirtException l) {
166-
167-
}
168-
throw e;
169-
}
170-
} else {
171-
StoragePoolInfo spi = sp.getInfo();
172-
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
173-
sp.create(0);
174-
}
175-
return sp;
176-
}
177-
}
178-
179104
public void storagePoolRefresh(StoragePool pool) {
180105
try {
181106
synchronized (getStoragePool(pool.getUUIDString())) {
@@ -368,32 +293,6 @@ public LibvirtStorageVolumeDef getStorageVolumeDef(Connect conn,
368293
return parser.parseStorageVolumeXML(volDefXML);
369294
}
370295

371-
public StorageVol getVolumeFromURI(Connect conn, String volPath)
372-
throws LibvirtException, URISyntaxException {
373-
int index = volPath.lastIndexOf("/");
374-
URI volDir = null;
375-
StoragePool sp = null;
376-
StorageVol vol = null;
377-
try {
378-
volDir = new URI(volPath.substring(0, index));
379-
String volName = volPath.substring(index + 1);
380-
sp = getStoragePoolbyURI(conn, volDir);
381-
vol = sp.storageVolLookupByName(volName);
382-
return vol;
383-
} catch (LibvirtException e) {
384-
s_logger.debug("Faild to get vol path: " + e.toString());
385-
throw e;
386-
} finally {
387-
try {
388-
if (sp != null) {
389-
sp.free();
390-
}
391-
} catch (LibvirtException e) {
392-
393-
}
394-
}
395-
}
396-
397296
public StoragePool createFileBasedStoragePool(Connect conn,
398297
String localStoragePath, String uuid) {
399298
if (!(_storageLayer.exists(localStoragePath) && _storageLayer
@@ -821,7 +720,7 @@ public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name,
821720
}
822721

823722
@Override
824-
public KVMStoragePool getStoragePoolByUri(String uri) {
723+
public KVMStoragePool getStoragePoolByURI(String uri) {
825724
URI storageUri = null;
826725

827726
try {
@@ -838,11 +737,12 @@ public KVMStoragePool getStoragePoolByUri(String uri) {
838737
sourcePath = storageUri.getPath();
839738
sourcePath = sourcePath.replace("//", "/");
840739
sourceHost = storageUri.getHost();
841-
uuid = UUID.randomUUID().toString();
740+
uuid = UUID.nameUUIDFromBytes(
741+
new String(sourceHost + sourcePath).getBytes()).toString();
842742
protocal = StoragePoolType.NetworkFilesystem;
843743
}
844744

845-
return createStoragePool(uuid, sourceHost, 0, sourcePath, "", protocal);
745+
return createStoragePool(uuid, sourceHost, 0, sourcePath, "", protocal);
846746
}
847747

848748
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name,
5555
public KVMPhysicalDisk createDiskFromSnapshot(KVMPhysicalDisk snapshot,
5656
String snapshotName, String name, KVMStoragePool destPool);
5757

58-
public KVMStoragePool getStoragePoolByUri(String uri);
58+
public KVMStoragePool getStoragePoolByURI(String uri);
5959

6060
public KVMPhysicalDisk getPhysicalDiskFromURI(String uri);
6161

0 commit comments

Comments
 (0)