use of org.libvirt.StoragePool in project cloudstack by apache.
the class LibvirtStorageAdaptor method listPhysicalDisks.
@Override
public List<KVMPhysicalDisk> listPhysicalDisks(String storagePoolUuid, KVMStoragePool pool) {
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
StoragePool virtPool = libvirtPool.getPool();
List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
try {
String[] vols = virtPool.listVolumes();
for (String volName : vols) {
KVMPhysicalDisk disk = getPhysicalDisk(volName, pool);
disks.add(disk);
}
return disks;
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
}
use of org.libvirt.StoragePool in project cloudstack by apache.
the class LibvirtStorageAdaptor method deleteStoragePool.
@Override
public boolean deleteStoragePool(String uuid) {
s_logger.info("Attempting to remove storage pool " + uuid + " from libvirt");
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
StoragePool sp = null;
Secret s = null;
try {
sp = conn.storagePoolLookupByUUIDString(uuid);
} catch (LibvirtException e) {
s_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 {
s = conn.secretLookupByUUIDString(uuid);
} catch (LibvirtException e) {
s_logger.info("Storage pool " + uuid + " has no corresponding secret. Not removing any secret.");
}
try {
if (sp.isPersistent() == 1) {
sp.destroy();
sp.undefine();
} else {
sp.destroy();
}
sp.free();
if (s != null) {
s.undefine();
s.free();
}
s_logger.info("Storage pool " + uuid + " was succesfully removed from libvirt.");
return true;
} catch (LibvirtException e) {
// handle ebusy error when pool is quickly destroyed
if (e.toString().contains("exit status 16")) {
String targetPath = _mountPoint + File.separator + uuid;
s_logger.error("deleteStoragePool removed pool from libvirt, but libvirt had trouble unmounting the pool. Trying umount location " + targetPath + "again in a few seconds");
String result = Script.runSimpleBashScript("sleep 5 && umount " + targetPath);
if (result == null) {
s_logger.error("Succeeded in unmounting " + targetPath);
return true;
}
s_logger.error("Failed to unmount " + targetPath);
}
throw new CloudRuntimeException(e.toString(), e);
}
}
use of org.libvirt.StoragePool in project cloudstack by apache.
the class LibvirtStorageAdaptor method createPhysicalDiskByLibVirt.
private KVMPhysicalDisk createPhysicalDiskByLibVirt(String name, KVMStoragePool pool, PhysicalDiskFormat format, Storage.ProvisioningType provisioningType, long size) {
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
StoragePool virtPool = libvirtPool.getPool();
LibvirtStorageVolumeDef.VolumeFormat libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.getFormat(format);
String volPath = null;
String volName = null;
long volAllocation = 0;
long volCapacity = 0;
LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name, size, libvirtformat, null, null);
s_logger.debug(volDef.toString());
try {
StorageVol vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
volPath = vol.getPath();
volName = vol.getName();
volAllocation = vol.getInfo().allocation;
volCapacity = vol.getInfo().capacity;
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
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 cloudstack by apache.
the class LibvirtStorageAdaptor method createCLVMStoragePool.
private StoragePool createCLVMStoragePool(Connect conn, String uuid, String host, String path) {
String volgroupPath = "/dev/" + path;
String volgroupName = path;
volgroupName = volgroupName.replaceFirst("/", "");
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(PoolType.LOGICAL, volgroupName, uuid, host, volgroupPath, volgroupPath);
StoragePool sp = null;
try {
s_logger.debug(spd.toString());
sp = conn.storagePoolCreateXML(spd.toString(), 0);
return sp;
} catch (LibvirtException e) {
s_logger.error(e.toString());
if (sp != null) {
try {
if (sp.isPersistent() == 1) {
sp.destroy();
sp.undefine();
} else {
sp.destroy();
}
sp.free();
} catch (LibvirtException l) {
s_logger.debug("Failed to define clvm storage pool with: " + l.toString());
}
}
return null;
}
}
use of org.libvirt.StoragePool in project cloudstack by apache.
the class ManagedNfsStorageAdaptor method connectPhysicalDisk.
/*
* creates a nfs storage pool using libvirt
*/
@Override
public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<String, String> details) {
StoragePool sp = null;
Connect conn = null;
String targetPath = null;
LibvirtStoragePoolDef spd = null;
try {
conn = LibvirtConnection.getConnection();
if (conn == null) {
throw new CloudRuntimeException("Failed to create Libvrt Connection");
}
targetPath = "/mnt" + volumeUuid;
spd = new LibvirtStoragePoolDef(PoolType.NETFS, volumeUuid, details.get(DiskTO.UUID), pool.getSourceHost(), details.get(DiskTO.MOUNT_POINT), targetPath);
_storageLayer.mkdir(targetPath);
s_logger.debug(spd.toString());
sp = conn.storagePoolCreateXML(spd.toString(), 0);
if (sp == null) {
throw new CloudRuntimeException("Failed to create storage pool:" + volumeUuid);
}
try {
if (sp.isActive() == 0) {
// s_logger.debug("attempting to activate pool " + name);
sp.create(0);
}
// now add the storage pool
LibvirtStoragePool storagePool = (LibvirtStoragePool) getStoragePool(pool.getUuid());
storagePool.setPool(sp);
return true;
} catch (LibvirtException e) {
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);
}
}
} catch (LibvirtException e) {
s_logger.error(e.toString());
// if error is that pool is mounted, try to handle it
if (e.toString().contains("already mounted")) {
s_logger.error("Attempting to unmount old mount libvirt is unaware of at " + targetPath);
String result = Script.runSimpleBashScript("umount -l " + targetPath);
if (result == null) {
s_logger.error("Succeeded in unmounting " + targetPath);
try {
conn.storagePoolCreateXML(spd.toString(), 0);
s_logger.error("Succeeded in redefining storage");
return true;
} catch (LibvirtException l) {
s_logger.error("Target was already mounted, unmounted it but failed to redefine storage:" + l);
}
} else {
s_logger.error("Failed in unmounting and redefining storage");
}
} else {
s_logger.error("Internal error occurred when attempting to mount:" + e.getMessage());
// stacktrace for agent.log
e.printStackTrace();
throw new CloudRuntimeException(e.toString());
}
return false;
}
}
Aggregations