use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method getVolumePath.
public String getVolumePath(final Connect conn, final DiskTO volume) throws LibvirtException, URISyntaxException {
final DataTO data = volume.getData();
final DataStoreTO store = data.getDataStore();
if (volume.getType() == VolumeType.ISO && data.getPath() != null) {
final NfsTO nfsStore = (NfsTO) store;
final String isoPath = nfsStore.getUrl() + File.separator + data.getPath();
final int index = isoPath.lastIndexOf("/");
final String path = isoPath.substring(0, index);
final String name = isoPath.substring(index + 1);
final KvmStoragePool secondaryPool = this.storagePoolMgr.getStoragePoolByUri(path);
final KvmPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
return isoVol.getPath();
} else {
return data.getPath();
}
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method templateToPrimaryDownload.
// this is much like PrimaryStorageDownloadCommand, but keeping it separate
public KvmPhysicalDisk templateToPrimaryDownload(final String templateUrl, final KvmStoragePool primaryPool, final String volUuid) {
final int index = templateUrl.lastIndexOf("/");
final String mountpoint = templateUrl.substring(0, index);
String templateName = null;
if (index < templateUrl.length() - 1) {
templateName = templateUrl.substring(index + 1);
}
KvmPhysicalDisk templateVol = null;
KvmStoragePool secondaryPool = null;
try {
secondaryPool = this.storagePoolMgr.getStoragePoolByUri(mountpoint);
/* Get template vol */
if (templateName == null) {
secondaryPool.refresh();
final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
logger.error("Failed to get volumes from pool: " + secondaryPool.getUuid());
return null;
}
for (final KvmPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
templateVol = disk;
break;
}
}
if (templateVol == null) {
logger.error("Failed to get template from pool: " + secondaryPool.getUuid());
return null;
}
} else {
templateVol = secondaryPool.getPhysicalDisk(templateName);
}
/* Copy volume to primary storage */
final KvmPhysicalDisk primaryVol = this.storagePoolMgr.copyPhysicalDisk(templateVol, volUuid, primaryPool, 0);
return primaryVol;
} catch (final CloudRuntimeException e) {
logger.error("Failed to download template to primary storage", e);
return null;
} finally {
if (secondaryPool != null) {
this.storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
}
}
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method createVbd.
public void createVbd(final Connect conn, final VirtualMachineTO vmSpec, final String vmName, final LibvirtVmDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
final List<DiskTO> disks = Arrays.asList(vmSpec.getDisks());
Collections.sort(disks, new Comparator<DiskTO>() {
@Override
public int compare(final DiskTO arg0, final DiskTO arg1) {
return arg0.getDiskSeq() > arg1.getDiskSeq() ? 1 : -1;
}
});
for (final DiskTO volume : disks) {
KvmPhysicalDisk physicalDisk = null;
KvmStoragePool pool = null;
final DataTO data = volume.getData();
if (volume.getType() == VolumeType.ISO && data.getPath() != null) {
final NfsTO nfsStore = (NfsTO) data.getDataStore();
final String volPath = nfsStore.getUrl() + File.separator + data.getPath();
final int index = volPath.lastIndexOf("/");
final String volDir = volPath.substring(0, index);
final String volName = volPath.substring(index + 1);
final KvmStoragePool secondaryStorage = this.storagePoolMgr.getStoragePoolByUri(volDir);
physicalDisk = secondaryStorage.getPhysicalDisk(volName);
} else if (volume.getType() != VolumeType.ISO) {
final PrimaryDataStoreTO store = (PrimaryDataStoreTO) data.getDataStore();
physicalDisk = this.storagePoolMgr.getPhysicalDisk(store.getPoolType(), store.getUuid(), data.getPath());
pool = physicalDisk.getPool();
}
String volPath = null;
if (physicalDisk != null) {
volPath = physicalDisk.getPath();
}
// check for disk activity, if detected we should exit because vm is running elsewhere
if (this.diskActivityCheckEnabled && physicalDisk != null && physicalDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
logger.debug("Checking physical disk file at path " + volPath + " for disk activity to ensure vm is not running elsewhere");
try {
HypervisorUtils.checkVolumeFileForActivity(volPath, this.diskActivityCheckTimeoutSeconds, this.diskActivityInactiveThresholdMilliseconds, this.diskActivityCheckFileSizeMin);
} catch (final IOException ex) {
throw new CloudRuntimeException("Unable to check physical disk file for activity", ex);
}
logger.debug("Disk activity check cleared");
}
final LibvirtDiskDef disk = new LibvirtDiskDef();
if (volume.getType() == VolumeType.ISO) {
if (volPath == null) {
/* Add iso as placeholder */
disk.defIsoDisk(null);
} else {
disk.defIsoDisk(volPath);
}
} else {
final int devId = volume.getDiskSeq().intValue();
if (volume.getDiskController() == DiskControllerType.SCSI) {
disk.setQemuDriver(true);
disk.setDiscard(DiscardType.UNMAP);
}
disk.setImageFormat(volume.getDiskFormat());
if (pool.getType() == StoragePoolType.RBD) {
/*
* For RBD pools we use the secret mechanism in libvirt. We store the secret under the UUID of the pool,
* that's why we pass the pool's UUID as the authSecret
*/
disk.defNetworkBasedDisk(physicalDisk.getPath().replace("rbd:", ""), pool.getSourceHost(), pool.getSourcePort(), pool.getAuthUserName(), pool.getUuid(), devId, volume.getDiskController(), DiskProtocol.RBD, ImageFormat.RAW);
} else if (pool.getType() == StoragePoolType.Gluster) {
final String mountpoint = pool.getLocalPath();
final String path = physicalDisk.getPath();
final String glusterVolume = pool.getSourceDir().replace("/", "");
disk.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint, ""), pool.getSourceHost(), pool.getSourcePort(), null, null, devId, volume.getDiskController(), DiskProtocol.GLUSTER, ImageFormat.QCOW2);
} else if (pool.getType() == StoragePoolType.CLVM || pool.getType() == StoragePoolType.LVM) {
disk.defBlockBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController());
} else if (pool.getType() == StoragePoolType.NetworkFilesystem) {
disk.defFileBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController(), volume.getDiskFormat());
} else {
disk.defFileBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController(), volume.getDiskFormat());
}
}
if (data instanceof VolumeObjectTO) {
final VolumeObjectTO volumeObjectTo = (VolumeObjectTO) data;
disk.setSerial(volumeObjectTo.getDeviceId() + "-" + diskUuidToSerial(volumeObjectTo.getUuid()));
disk.setDeviceId(volumeObjectTo.getDeviceId().intValue());
if (volumeObjectTo.getBytesReadRate() != null && volumeObjectTo.getBytesReadRate() > 0) {
disk.setBytesReadRate(volumeObjectTo.getBytesReadRate());
}
if (volumeObjectTo.getBytesWriteRate() != null && volumeObjectTo.getBytesWriteRate() > 0) {
disk.setBytesWriteRate(volumeObjectTo.getBytesWriteRate());
}
if (volumeObjectTo.getIopsReadRate() != null && volumeObjectTo.getIopsReadRate() > 0) {
disk.setIopsReadRate(volumeObjectTo.getIopsReadRate());
}
if (volumeObjectTo.getIopsWriteRate() != null && volumeObjectTo.getIopsWriteRate() > 0) {
disk.setIopsWriteRate(volumeObjectTo.getIopsWriteRate());
}
if (volumeObjectTo.getIopsTotalRate() != null && volumeObjectTo.getIopsTotalRate() > 0) {
disk.setIopsTotalRate(volumeObjectTo.getIopsTotalRate());
}
if (volumeObjectTo.getCacheMode() != null) {
disk.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(volumeObjectTo.getCacheMode().toString().toUpperCase()));
}
if (volumeObjectTo.getFormat() != null) {
physicalDisk.setFormat(physicalDisk.getPhysicalDiskFormatFromImageFormat(volumeObjectTo.getFormat()));
}
}
logger.debug("Adding disk: " + disk.toString());
vm.getDevices().addDevice(disk);
}
if (vmSpec.getType() != VirtualMachineType.User) {
final String sysvmIsoPath = getSysvmIsoPath();
if (sysvmIsoPath != null) {
final LibvirtDiskDef iso = new LibvirtDiskDef();
iso.defIsoDisk(sysvmIsoPath);
vm.getDevices().addDevice(iso);
}
}
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtPrimaryStorageDownloadCommandWrapper method execute.
@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String tmplturl = command.getUrl();
final int index = tmplturl.lastIndexOf("/");
final String mountpoint = tmplturl.substring(0, index);
String tmpltname = null;
if (index < tmplturl.length() - 1) {
tmpltname = tmplturl.substring(index + 1);
}
KvmPhysicalDisk tmplVol = null;
KvmStoragePool secondaryPool = null;
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
try {
secondaryPool = storagePoolMgr.getStoragePoolByUri(mountpoint);
/* Get template vol */
if (tmpltname == null) {
secondaryPool.refresh();
final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
}
for (final KvmPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
tmplVol = disk;
break;
}
}
if (tmplVol == null) {
return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
}
} else {
tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
}
/* Copy volume to primary storage */
final KvmStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid());
final KvmPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0);
return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
} catch (final CloudRuntimeException e) {
return new PrimaryStorageDownloadAnswer(e.toString());
} finally {
if (secondaryPool != null) {
storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
}
}
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtDestroyCommandWrapper method execute.
@Override
public Answer execute(final DestroyCommand command, final LibvirtComputingResource libvirtComputingResource) {
final VolumeTO vol = command.getVolume();
try {
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
final KvmStoragePool pool = storagePoolMgr.getStoragePool(vol.getPoolType(), vol.getPoolUuid());
pool.deletePhysicalDisk(vol.getPath(), null);
return new Answer(command, true, "Success");
} catch (final CloudRuntimeException e) {
s_logger.debug("Failed to delete volume: " + e.toString());
return new Answer(command, false, e.toString());
}
}
Aggregations