use of org.libvirt.StoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtStorageAdaptor method deleteStoragePool.
@Override
public boolean deleteStoragePool(final String uuid) {
logger.info("Attempting to remove storage pool " + uuid + " from libvirt");
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (final LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
StoragePool storagePool = null;
Secret secretString = null;
try {
storagePool = conn.storagePoolLookupByUUIDString(uuid);
} catch (final LibvirtException e) {
logger.warn("Storage pool " + uuid + " doesn't exist in libvirt. Assuming it is already removed");
return true;
}
/*
* Some storage pools, like RBD also have 'secret' information stored in libvirt Destroy them if they exist
*/
try {
secretString = conn.secretLookupByUUIDString(uuid);
} catch (final LibvirtException e) {
logger.info("Storage pool " + uuid + " has no corresponding secret. Not removing any secret.");
}
try {
if (storagePool.isPersistent() == 1) {
storagePool.destroy();
storagePool.undefine();
} else {
storagePool.destroy();
}
storagePool.free();
if (secretString != null) {
secretString.undefine();
secretString.free();
}
logger.info("Storage pool " + uuid + " was succesfully removed from libvirt.");
return true;
} catch (final LibvirtException e) {
// handle ebusy error when pool is quickly destroyed
if (e.toString().contains("exit status 16")) {
final String targetPath = mountPoint + File.separator + uuid;
logger.error("deleteStoragePool removed pool from libvirt, but libvirt had trouble unmounting the pool. " + "Trying umount location " + targetPath + " again in a few seconds");
final String result = Script.runSimpleBashScript("sleep 5 && umount " + targetPath);
if (result == null) {
logger.error("Succeeded in unmounting " + targetPath);
return true;
}
logger.error("Failed to unmount " + targetPath);
}
throw new CloudRuntimeException(e.toString(), e);
}
}
use of org.libvirt.StoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtStorageAdaptor method createStoragePool.
@Override
public KvmStoragePool createStoragePool(final String name, final String host, final int port, String path, final String userInfo, final StoragePoolType type) {
logger.info("Attempting to create storage pool " + name + " (" + type.toString() + ") in libvirt");
StoragePool sp = null;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (final LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
try {
sp = conn.storagePoolLookupByUUIDString(name);
if (sp != null && sp.isActive() == 0) {
sp.undefine();
sp = null;
logger.info("Found existing defined storage pool " + name + ". It wasn't running, so we undefined it.");
}
if (sp != null) {
logger.info("Found existing defined storage pool " + name + ", using it.");
}
} catch (final LibvirtException e) {
sp = null;
logger.warn("Storage pool " + name + " was not found running in libvirt. Need to create it.");
}
// 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.
logger.info("Didn't find an existing storage pool " + name + " by UUID, checking for pools with duplicate paths");
try {
final String[] poolnames = conn.listStoragePools();
for (final String poolname : poolnames) {
logger.debug("Checking path of existing pool " + poolname + " against pool we want to create");
final StoragePool p = conn.storagePoolLookupByName(poolname);
final LibvirtStoragePoolDef pdef = getStoragePoolDef(conn, p);
final String targetPath = pdef.getTargetPath();
if (targetPath != null && targetPath.equals(path)) {
logger.debug("Storage pool utilizing path '" + path + "' already exists as pool " + poolname + ", undefining so we can re-define with correct name " + name);
if (p.isPersistent() == 1) {
p.destroy();
p.undefine();
} else {
p.destroy();
}
}
}
} catch (final LibvirtException e) {
logger.error("Failure in attempting to see if an existing storage pool might be using the path " + "of the pool to be created:" + e);
}
logger.debug("Attempting to create storage pool " + name);
if (type == StoragePoolType.NetworkFilesystem) {
try {
sp = createNetfsStoragePool(PoolType.NETFS, conn, name, host, path);
} catch (final LibvirtException e) {
logger.error("Failed to create netfs mount: " + host + ":" + path, e);
logger.error(e.getStackTrace().toString());
throw new CloudRuntimeException(e.toString());
}
} else if (type == StoragePoolType.Gluster) {
try {
sp = createNetfsStoragePool(PoolType.GLUSTERFS, conn, name, host, path);
} catch (final LibvirtException e) {
logger.error("Failed to create glusterfs mount: " + host + ":" + path, e);
logger.error(e.getStackTrace().toString());
throw new CloudRuntimeException(e.toString());
}
} else if (type == StoragePoolType.SharedMountPoint || type == StoragePoolType.Filesystem) {
sp = createSharedStoragePool(conn, name, host, path);
} else if (type == StoragePoolType.RBD) {
sp = createRbdStoragePool(conn, name, host, port, userInfo, path);
} else if (type == StoragePoolType.CLVM) {
sp = createClvmStoragePool(conn, name, host, path);
}
}
if (sp == null) {
throw new CloudRuntimeException("Failed to create storage pool: " + name);
}
try {
if (sp.isActive() == 0) {
logger.debug("Attempting to activate pool " + name);
sp.create(0);
}
return getStoragePool(name);
} catch (final LibvirtException e) {
final String error = e.toString();
if (error.contains("Storage source conflict")) {
throw new CloudRuntimeException("A pool matching this location already exists in libvirt, " + " but has a different UUID/Name. Cannot create new pool without first " + " removing it. Check for inactive pools via 'virsh pool-list --all'. " + error);
} else {
throw new CloudRuntimeException(error);
}
}
}
use of org.libvirt.StoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtStorageAdaptor method refresh.
@Override
public boolean refresh(final KvmStoragePool pool) {
final LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
final StoragePool virtPool = libvirtPool.getPool();
try {
refreshPool(virtPool);
} catch (final LibvirtException e) {
return false;
}
return true;
}
use of org.libvirt.StoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtStorageAdaptor method createPhysicalDiskByLibVirt.
private KvmPhysicalDisk createPhysicalDiskByLibVirt(final String name, final KvmStoragePool pool, final PhysicalDiskFormat format, final Storage.ProvisioningType provisioningType, final long size) {
final LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
final StoragePool virtPool = libvirtPool.getPool();
final LibvirtStorageVolumeDef.VolumeFormat libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.getFormat(format);
String volPath = null;
String volName = null;
long volAllocation = 0;
long volCapacity = 0;
final LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name, size, libvirtformat, null, null);
logger.debug(volDef.toString());
try {
final StorageVol vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
volPath = vol.getPath();
volName = vol.getName();
volAllocation = vol.getInfo().allocation;
volCapacity = vol.getInfo().capacity;
} catch (final LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
final KvmPhysicalDisk disk = new KvmPhysicalDisk(volPath, volName, pool);
disk.setFormat(format);
disk.setSize(volAllocation);
disk.setVirtualSize(volCapacity);
return disk;
}
use of org.libvirt.StoragePool in project cosmic by MissionCriticalCloud.
the class ManagedNfsStorageAdaptor method disconnectPhysicalDisk.
/*
* disconnect the disk by destroying the sp pointer
*/
public boolean disconnectPhysicalDisk(final KvmStoragePool pool, final String mountpoint) throws LibvirtException {
final LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
final StoragePool sp = libvirtPool.getPool();
// destroy the pool
sp.destroy();
return true;
}
Aggregations