use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testCreateVolumeFromSnapshotCommandCloudException.
@Test
public void testCreateVolumeFromSnapshotCommandCloudException() {
final StoragePool pool = Mockito.mock(StoragePool.class);
final String secondaryStoragePoolURL = "/opt/storage/";
final Long dcId = 1l;
final Long accountId = 1l;
final Long volumeId = 1l;
final String backedUpSnapshotUuid = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/";
final String backedUpSnapshotName = "uuid:/8edb1156-a851-4914-afc6-468ee52ac862/";
final int wait = 0;
final CreateVolumeFromSnapshotCommand command = new CreateVolumeFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, wait);
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
final KvmPhysicalDisk snapshot = Mockito.mock(KvmPhysicalDisk.class);
String snapshotPath = command.getSnapshotUuid();
final int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
final String primaryUuid = command.getPrimaryStoragePoolNameLabel();
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(secondaryPool);
when(secondaryPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot);
when(storagePoolMgr.getStoragePool(command.getPool().getType(), primaryUuid)).thenThrow(CloudRuntimeException.class);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
verify(secondaryPool, times(1)).getPhysicalDisk(command.getSnapshotName());
verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), primaryUuid);
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtCopyVolumeCommandWrapper method execute.
@Override
public Answer execute(final CopyVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
final boolean copyToSecondary = command.toSecondaryStorage();
String volumePath = command.getVolumePath();
final StorageFilerTO pool = command.getPool();
final String secondaryStorageUrl = command.getSecondaryStorageURL();
KvmStoragePool secondaryStoragePool = null;
KvmStoragePool primaryPool = null;
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
try {
try {
primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
} catch (final CloudRuntimeException e) {
if (e.getMessage().contains("not found")) {
primaryPool = storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUserInfo(), pool.getType());
} else {
return new CopyVolumeAnswer(command, false, e.getMessage(), null, null);
}
}
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final String volumeName = libvirtUtilitiesHelper.generateUuidName();
if (copyToSecondary) {
final String destVolumeName = volumeName + ".qcow2";
final KvmPhysicalDisk volume = primaryPool.getPhysicalDisk(command.getVolumePath());
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
secondaryStoragePool.createFolder(volumeDestPath);
storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + volumeDestPath);
storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, 0);
return new CopyVolumeAnswer(command, true, null, null, volumeName);
} else {
volumePath = "/volumes/" + command.getVolumeId() + File.separator;
secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + volumePath);
final KvmPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(command.getVolumePath() + ".qcow2");
storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, 0);
return new CopyVolumeAnswer(command, true, null, null, volumeName);
}
} catch (final CloudRuntimeException e) {
return new CopyVolumeAnswer(command, false, e.toString(), null, null);
} finally {
if (secondaryStoragePool != null) {
storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
}
}
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtCreateVolumeFromSnapshotCommandWrapper method execute.
@Override
public Answer execute(final CreateVolumeFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
try {
String snapshotPath = command.getSnapshotUuid();
final int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
final KvmStoragePool secondaryPool = storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
final KvmPhysicalDisk snapshot = secondaryPool.getPhysicalDisk(command.getSnapshotName());
final String primaryUuid = command.getPrimaryStoragePoolNameLabel();
final StorageFilerTO pool = command.getPool();
final KvmStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), primaryUuid);
final String volUuid = UUID.randomUUID().toString();
final KvmPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool, 0);
if (disk == null) {
throw new NullPointerException("Disk was not successfully copied to the new storage.");
}
return new CreateVolumeFromSnapshotAnswer(command, true, "", disk.getName());
} catch (final CloudRuntimeException e) {
return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null);
} catch (final Exception e) {
return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null);
}
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method attachOrDetachIso.
public synchronized String attachOrDetachIso(final Connect conn, final String vmName, String isoPath, final boolean isAttach) throws LibvirtException, URISyntaxException, InternalErrorException {
String isoXml = null;
if (isoPath != null && isAttach) {
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);
isoPath = isoVol.getPath();
final LibvirtDiskDef iso = new LibvirtDiskDef();
iso.defIsoDisk(isoPath);
isoXml = iso.toString();
} else {
final LibvirtDiskDef iso = new LibvirtDiskDef();
iso.defIsoDisk(null);
isoXml = iso.toString();
}
final List<LibvirtDiskDef> disks = getDisks(conn, vmName);
final String result = attachOrDetachDevice(conn, true, vmName, isoXml);
if (result == null && !isAttach) {
for (final LibvirtDiskDef disk : disks) {
if (disk.getDeviceType() == LibvirtDiskDef.DeviceType.CDROM) {
cleanupDisk(disk);
}
}
}
return result;
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testCreatePrivateTemplateFromSnapshotCommandIOException.
@Test
public void testCreatePrivateTemplateFromSnapshotCommandIOException() {
final StoragePool pool = Mockito.mock(StoragePool.class);
final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary";
final Long dcId = 1l;
final Long accountId = 1l;
final Long volumeId = 1l;
final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/";
final String backedUpSnapshotName = "snap";
final String origTemplateInstallPath = "/install/path/";
final Long newTemplateId = 2l;
final String templateName = "templ";
final int wait = 0;
final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait);
final String templatePath = "/template/path";
final String localPath = "/mnt/local";
final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
final KvmStoragePool snapshotPool = Mockito.mock(KvmStoragePool.class);
final KvmPhysicalDisk snapshot = Mockito.mock(KvmPhysicalDisk.class);
final StorageLayer storage = Mockito.mock(StorageLayer.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final TemplateLocation location = Mockito.mock(TemplateLocation.class);
final Processor qcow2Processor = Mockito.mock(Processor.class);
final TemplateFormatInfo info = Mockito.mock(TemplateFormatInfo.class);
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
String snapshotPath = command.getSnapshotUuid();
final int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool);
when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl())).thenReturn(secondaryPool);
when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot);
when(secondaryPool.getLocalPath()).thenReturn(localPath);
when(this.libvirtComputingResource.getStorage()).thenReturn(storage);
when(this.libvirtComputingResource.createTmplPath()).thenReturn(templatePath);
when(this.libvirtComputingResource.getCmdsTimeout()).thenReturn(1);
final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
final String templateInstallFolder = "template/tmpl/" + templateFolder;
final String tmplPath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
when(libvirtUtilitiesHelper.generateUuidName()).thenReturn(tmplName);
try {
when(libvirtUtilitiesHelper.buildQcow2Processor(storage)).thenReturn(qcow2Processor);
when(qcow2Processor.process(tmplPath, null, tmplName)).thenReturn(info);
when(location.create(1, true, tmplName)).thenThrow(IOException.class);
} catch (final ConfigurationException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
} catch (final IOException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl());
}
Aggregations