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>
This commit is contained in:
Marcus Sorensen 2012-09-26 16:17:23 -07:00 committed by Edison Su
parent 45360b7a57
commit f0e928461a
3 changed files with 6 additions and 106 deletions

View File

@ -49,7 +49,7 @@ public class KVMStoragePoolManager {
}
public KVMStoragePool getStoragePoolByURI(String uri) {
return this._storageAdaptor.getStoragePoolByUri(uri);
return this._storageAdaptor.getStoragePoolByURI(uri);
}
public KVMStoragePool createStoragePool(String name, String host, int port, String path,

View File

@ -101,81 +101,6 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
return pool.storageVolCreateXML(volDef.toString(), 0);
}
public StoragePool getStoragePoolbyURI(Connect conn, URI uri)
throws LibvirtException {
String sourcePath;
String uuid;
String sourceHost = "";
String protocal;
if (uri.getScheme().equalsIgnoreCase("local")) {
sourcePath = _mountPoint + File.separator
+ uri.toString().replace("local:///", "");
sourcePath = sourcePath.replace("//", "/");
uuid = UUID.nameUUIDFromBytes(new String(sourcePath).getBytes())
.toString();
protocal = "DIR";
} else {
sourcePath = uri.getPath();
sourcePath = sourcePath.replace("//", "/");
sourceHost = uri.getHost();
uuid = UUID.nameUUIDFromBytes(
new String(sourceHost + sourcePath).getBytes()).toString();
protocal = "NFS";
}
String targetPath = _mountPoint + File.separator + uuid;
StoragePool sp = null;
try {
sp = conn.storagePoolLookupByUUIDString(uuid);
} catch (LibvirtException e) {
}
if (sp == null) {
try {
LibvirtStoragePoolDef spd = null;
if (protocal.equalsIgnoreCase("NFS")) {
_storageLayer.mkdir(targetPath);
spd = new LibvirtStoragePoolDef(poolType.NETFS, uuid, uuid,
sourceHost, sourcePath, targetPath);
s_logger.debug(spd.toString());
// addStoragePool(uuid);
} else if (protocal.equalsIgnoreCase("DIR")) {
_storageLayer.mkdir(targetPath);
spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid,
null, null, sourcePath);
}
synchronized (getStoragePool(uuid)) {
sp = conn.storagePoolDefineXML(spd.toString(), 0);
if (sp == null) {
s_logger.debug("Failed to define storage pool");
return null;
}
sp.create(0);
}
return sp;
} catch (LibvirtException e) {
try {
if (sp != null) {
sp.undefine();
sp.free();
}
} catch (LibvirtException l) {
}
throw e;
}
} else {
StoragePoolInfo spi = sp.getInfo();
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
sp.create(0);
}
return sp;
}
}
public void storagePoolRefresh(StoragePool pool) {
try {
synchronized (getStoragePool(pool.getUUIDString())) {
@ -368,32 +293,6 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
return parser.parseStorageVolumeXML(volDefXML);
}
public StorageVol getVolumeFromURI(Connect conn, String volPath)
throws LibvirtException, URISyntaxException {
int index = volPath.lastIndexOf("/");
URI volDir = null;
StoragePool sp = null;
StorageVol vol = null;
try {
volDir = new URI(volPath.substring(0, index));
String volName = volPath.substring(index + 1);
sp = getStoragePoolbyURI(conn, volDir);
vol = sp.storageVolLookupByName(volName);
return vol;
} catch (LibvirtException e) {
s_logger.debug("Faild to get vol path: " + e.toString());
throw e;
} finally {
try {
if (sp != null) {
sp.free();
}
} catch (LibvirtException e) {
}
}
}
public StoragePool createFileBasedStoragePool(Connect conn,
String localStoragePath, String uuid) {
if (!(_storageLayer.exists(localStoragePath) && _storageLayer
@ -821,7 +720,7 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
}
@Override
public KVMStoragePool getStoragePoolByUri(String uri) {
public KVMStoragePool getStoragePoolByURI(String uri) {
URI storageUri = null;
try {
@ -838,11 +737,12 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
sourcePath = storageUri.getPath();
sourcePath = sourcePath.replace("//", "/");
sourceHost = storageUri.getHost();
uuid = UUID.randomUUID().toString();
uuid = UUID.nameUUIDFromBytes(
new String(sourceHost + sourcePath).getBytes()).toString();
protocal = StoragePoolType.NetworkFilesystem;
}
return createStoragePool(uuid, sourceHost, 0, sourcePath, "", protocal);
return createStoragePool(uuid, sourceHost, 0, sourcePath, "", protocal);
}
@Override

View File

@ -55,7 +55,7 @@ public interface StorageAdaptor {
public KVMPhysicalDisk createDiskFromSnapshot(KVMPhysicalDisk snapshot,
String snapshotName, String name, KVMStoragePool destPool);
public KVMStoragePool getStoragePoolByUri(String uri);
public KVMStoragePool getStoragePoolByURI(String uri);
public KVMPhysicalDisk getPhysicalDiskFromURI(String uri);