use of org.libvirt.StorageVol in project cosmic by MissionCriticalCloud.
the class LibvirtStorageAdaptor method getVolume.
public StorageVol getVolume(final StoragePool pool, final String volName) {
StorageVol vol = null;
try {
vol = pool.storageVolLookupByName(volName);
} catch (final LibvirtException e) {
logger.debug("Could not find volume " + volName + ": " + e.getMessage());
}
/**
* The volume was not found in the storage pool This can happen when a volume has just been created on a different
* host and since then the libvirt storage pool has not been refreshed.
*/
if (vol == null) {
try {
logger.debug("Refreshing storage pool " + pool.getName());
refreshPool(pool);
} catch (final LibvirtException e) {
logger.debug("Failed to refresh storage pool: " + e.getMessage());
}
try {
vol = pool.storageVolLookupByName(volName);
logger.debug("Found volume " + volName + " in storage pool " + pool.getName() + " after refreshing the pool");
} catch (final LibvirtException e) {
throw new CloudRuntimeException("Could not find volume " + volName + ": " + e.getMessage());
}
}
return vol;
}
use of org.libvirt.StorageVol in project cosmic by MissionCriticalCloud.
the class ManagedNfsStorageAdaptor method getPhysicalDisk.
/*
* creates a disk based on the created nfs storage pool using libvirt
*/
@Override
public KvmPhysicalDisk getPhysicalDisk(final String volumeUuid, final KvmStoragePool pool) {
// now create the volume upon the given storage pool in kvm
final Connect conn;
StoragePool virtPool = null;
try {
conn = LibvirtConnection.getConnection();
virtPool = conn.storagePoolLookupByName("/" + volumeUuid);
} catch (final LibvirtException e1) {
throw new CloudRuntimeException(e1.toString());
}
LibvirtStorageVolumeDef.VolumeFormat libvirtformat = null;
long volCapacity = 0;
// check whether the volume is present on the given pool
StorageVol vol = getVolume(virtPool, volumeUuid);
try {
if (vol == null) {
libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.QCOW2;
final StoragePoolInfo poolinfo = virtPool.getInfo();
volCapacity = poolinfo.available;
final LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(volumeUuid, volCapacity, libvirtformat, null, null);
logger.debug(volDef.toString());
vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
}
final KvmPhysicalDisk disk = new KvmPhysicalDisk(vol.getPath(), volumeUuid, pool);
disk.setFormat(PhysicalDiskFormat.QCOW2);
disk.setSize(vol.getInfo().allocation);
disk.setVirtualSize(vol.getInfo().capacity);
return disk;
} catch (final LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
}
use of org.libvirt.StorageVol in project cloudstack by apache.
the class LibvirtMigrateCommandWrapper method deleteLocalVolume.
/**
* Deletes the local volume from the storage pool.
*/
protected void deleteLocalVolume(String localPath) {
try {
Connect conn = LibvirtConnection.getConnection();
StorageVol storageVolLookupByPath = conn.storageVolLookupByPath(localPath);
storageVolLookupByPath.delete(0);
} catch (LibvirtException e) {
s_logger.error(String.format("Cannot delete local volume [%s] due to: %s", localPath, e));
}
}
use of org.libvirt.StorageVol in project cloudstack by apache.
the class LibvirtStorageAdaptor method getPhysicalDisk.
@Override
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
try {
StorageVol vol = getVolume(libvirtPool.getPool(), volumeUuid);
KVMPhysicalDisk disk;
LibvirtStorageVolumeDef voldef = getStorageVolumeDef(libvirtPool.getPool().getConnect(), vol);
disk = new KVMPhysicalDisk(vol.getPath(), vol.getName(), pool);
disk.setSize(vol.getInfo().allocation);
disk.setVirtualSize(vol.getInfo().capacity);
/**
* libvirt returns format = 'unknow', so we have to force
* the format to RAW for RBD storage volumes
*/
if (pool.getType() == StoragePoolType.RBD) {
disk.setFormat(PhysicalDiskFormat.RAW);
} else if (voldef.getFormat() == null) {
File diskDir = new File(disk.getPath());
if (diskDir.exists() && diskDir.isDirectory()) {
disk.setFormat(PhysicalDiskFormat.DIR);
} else if (volumeUuid.endsWith("tar") || volumeUuid.endsWith(("TAR"))) {
disk.setFormat(PhysicalDiskFormat.TAR);
} else if (volumeUuid.endsWith("raw") || volumeUuid.endsWith(("RAW"))) {
disk.setFormat(PhysicalDiskFormat.RAW);
} else {
disk.setFormat(pool.getDefaultFormat());
}
} else if (voldef.getFormat() == LibvirtStorageVolumeDef.VolumeFormat.QCOW2) {
disk.setFormat(PhysicalDiskFormat.QCOW2);
} else if (voldef.getFormat() == LibvirtStorageVolumeDef.VolumeFormat.RAW) {
disk.setFormat(PhysicalDiskFormat.RAW);
}
return disk;
} catch (LibvirtException e) {
s_logger.debug("Failed to get physical disk:", e);
throw new CloudRuntimeException(e.toString());
}
}
use of org.libvirt.StorageVol in project cloudstack by apache.
the class LibvirtStorageAdaptor method deletePhysicalDisk.
@Override
public boolean deletePhysicalDisk(String uuid, KVMStoragePool pool, Storage.ImageFormat format) {
s_logger.info("Attempting to remove volume " + uuid + " from pool " + pool.getUuid());
/**
* RBD volume can have snapshots and while they exist libvirt
* can't remove the RBD volume
*
* We have to remove those snapshots first
*/
if (pool.getType() == StoragePoolType.RBD) {
try {
s_logger.info("Unprotecting and Removing RBD snapshots of image " + pool.getSourceDir() + "/" + uuid + " prior to removing the image");
Rados r = new Rados(pool.getAuthUserName());
r.confSet("mon_host", pool.getSourceHost() + ":" + pool.getSourcePort());
r.confSet("key", pool.getAuthSecret());
r.confSet("client_mount_timeout", "30");
r.connect();
s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
IoCTX io = r.ioCtxCreate(pool.getSourceDir());
Rbd rbd = new Rbd(io);
RbdImage image = rbd.open(uuid);
s_logger.debug("Fetching list of snapshots of RBD image " + pool.getSourceDir() + "/" + uuid);
List<RbdSnapInfo> snaps = image.snapList();
try {
for (RbdSnapInfo snap : snaps) {
if (image.snapIsProtected(snap.name)) {
s_logger.debug("Unprotecting snapshot " + pool.getSourceDir() + "/" + uuid + "@" + snap.name);
image.snapUnprotect(snap.name);
} else {
s_logger.debug("Snapshot " + pool.getSourceDir() + "/" + uuid + "@" + snap.name + " is not protected.");
}
s_logger.debug("Removing snapshot " + pool.getSourceDir() + "/" + uuid + "@" + snap.name);
image.snapRemove(snap.name);
}
s_logger.info("Succesfully unprotected and removed any remaining snapshots (" + snaps.size() + ") of " + pool.getSourceDir() + "/" + uuid + " Continuing to remove the RBD image");
} catch (RbdException e) {
s_logger.error("Failed to remove snapshot with exception: " + e.toString() + ", RBD error: " + ErrorCode.getErrorMessage(e.getReturnValue()));
throw new CloudRuntimeException(e.toString() + " - " + ErrorCode.getErrorMessage(e.getReturnValue()));
} finally {
s_logger.debug("Closing image and destroying context");
rbd.close(image);
r.ioCtxDestroy(io);
}
} catch (RadosException e) {
s_logger.error("Failed to remove snapshot with exception: " + e.toString() + ", RBD error: " + ErrorCode.getErrorMessage(e.getReturnValue()));
throw new CloudRuntimeException(e.toString() + " - " + ErrorCode.getErrorMessage(e.getReturnValue()));
} catch (RbdException e) {
s_logger.error("Failed to remove snapshot with exception: " + e.toString() + ", RBD error: " + ErrorCode.getErrorMessage(e.getReturnValue()));
throw new CloudRuntimeException(e.toString() + " - " + ErrorCode.getErrorMessage(e.getReturnValue()));
}
}
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
try {
StorageVol vol = getVolume(libvirtPool.getPool(), uuid);
s_logger.debug("Instructing libvirt to remove volume " + uuid + " from pool " + pool.getUuid());
if (Storage.ImageFormat.DIR.equals(format)) {
deleteDirVol(libvirtPool, vol);
} else {
deleteVol(libvirtPool, vol);
}
vol.free();
return true;
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
}
Aggregations