use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.
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.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.
the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNODisk.
@Test
public void testPrimaryStorageDownloadCommandNOTemplateNODisk() {
final StoragePool pool = Mockito.mock(StoragePool.class);
final List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
final String name = "Test";
final String url = "http://template/";
final ImageFormat format = ImageFormat.QCOW2;
final long accountId = 1l;
final int wait = 0;
final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class);
final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class);
final int index = url.lastIndexOf("/");
final String mountpoint = url.substring(0, index);
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool);
when(secondaryPool.listPhysicalDisks()).thenReturn(disks);
when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
}
use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.
the class LibvirtComputingResourceTest method testCreatePrivateTemplateFromSnapshotCommandConfigurationException.
@SuppressWarnings("unchecked")
@Test
public void testCreatePrivateTemplateFromSnapshotCommandConfigurationException() {
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 FormatInfo info = Mockito.mock(FormatInfo.class);
when(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(libvirtComputingResource.getStorage()).thenReturn(storage);
when(libvirtComputingResource.createTmplPath()).thenReturn(templatePath);
when(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(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
try {
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenThrow(ConfigurationException.class);
when(qcow2Processor.process(tmplPath, null, tmplName)).thenReturn(info);
} catch (final ConfigurationException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl());
}
use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.
the class LibvirtComputingResourceTest method testCreateVolumeFromSnapshotCommandCloudException.
@SuppressWarnings("unchecked")
@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(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, libvirtComputingResource);
assertFalse(answer.getResult());
verify(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.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.
the class LibvirtComputingResourceTest method testCopyVolumeCommand.
@Test
public void testCopyVolumeCommand() {
final StoragePool storagePool = Mockito.mock(StoragePool.class);
final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary";
final Long volumeId = 1l;
final int wait = 0;
final String volumePath = "/vol/path";
final boolean toSecondaryStorage = true;
final boolean executeInSequence = false;
final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence);
final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
final KVMStoragePool secondary = Mockito.mock(KVMStoragePool.class);
final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class);
final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final StorageFilerTO pool = command.getPool();
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
when(primary.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL)).thenReturn(secondary);
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
when(secondary.createFolder(volumeDestPath)).thenReturn(true);
when(storagePoolMgr.deleteStoragePool(secondary.getType(), secondary.getUuid())).thenReturn(true);
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, secondary, 0)).thenReturn(disk);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertTrue(answer.getResult());
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Aggregations