@Override
public KVMStoragePool createStoragePool(String name, String host, int port,
String path, String userInfo, StoragePoolType type) {
StoragePool sp = null;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
try {
sp = conn.storagePoolLookupByUUIDString(name);
if (sp != null && sp.isActive() == 0) {
sp.undefine();
sp = null;
s_logger.debug("Found existing defined storage pool " + name + ". It wasn't running, so we undefined it.");
}
if (sp != null) {
s_logger.debug("Found existing defined storage pool " + name + ", using it.");
}
} catch (LibvirtException e) {
sp = null;
s_logger.debug("createStoragePool didn't find existing running pool: " + e + ", need to create it");
}
// libvirt strips trailing slashes off of path, we will too in order to match
// existing paths
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
if (sp == null) {
// see if any existing pool by another name is using our storage path.
// if anyone is, undefine the pool so we can define it as requested.
// This should be safe since a pool in use can't be removed, and no
// volumes are affected by unregistering the pool with libvirt.
s_logger.debug("Didn't find an existing storage pool " + name
+ " by UUID, checking for pools with duplicate paths");
try {
String[] poolnames = conn.listStoragePools();
for (String poolname : poolnames) {
s_logger.debug("Checking path of existing pool " + poolname
+ " against pool we want to create");
StoragePool p = conn.storagePoolLookupByName(poolname);
LibvirtStoragePoolDef pdef = getStoragePoolDef(conn, p);
String targetPath = pdef.getTargetPath();
if (targetPath != null && targetPath.equals(path)) {
s_logger.debug("Storage pool utilizing path '" + path + "' already exists as pool "