use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testResizeVolumeCommandShrink.
@Test
@Ignore
public void testResizeVolumeCommandShrink() {
final String path = "nfs:/127.0.0.1/storage/secondary";
final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
final Long currentSize = 100l;
final Long newSize = 200l;
final boolean shrinkOk = true;
final String vmInstance = "Test";
final ResizeVolumeCommand command = new ResizeVolumeCommand(path, pool, currentSize, newSize, shrinkOk, vmInstance);
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool storagePool = Mockito.mock(KvmStoragePool.class);
final KvmPhysicalDisk vol = Mockito.mock(KvmPhysicalDisk.class);
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(storagePool);
when(storagePool.getPhysicalDisk(path)).thenReturn(vol);
when(vol.getPath()).thenReturn(path);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testResizeVolumeCommandException2.
@Test
public void testResizeVolumeCommandException2() {
final String path = "nfs:/127.0.0.1/storage/secondary";
final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
final Long currentSize = 100l;
final Long newSize = 200l;
final boolean shrinkOk = false;
final String vmInstance = "Test";
final ResizeVolumeCommand command = new ResizeVolumeCommand(path, pool, currentSize, newSize, shrinkOk, vmInstance);
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool storagePool = Mockito.mock(KvmStoragePool.class);
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(storagePool);
when(storagePool.getPhysicalDisk(path)).thenThrow(CloudRuntimeException.class);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testCreatePrivateTemplateFromSnapshotCommandCloudRuntime.
@Test
public void testCreatePrivateTemplateFromSnapshotCommandCloudRuntime() {
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 KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
final KvmStoragePool snapshotPool = Mockito.mock(KvmStoragePool.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
String snapshotPath = command.getSnapshotUuid();
final int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generateUuidName()).thenReturn(tmplName);
when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool);
when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl())).thenReturn(secondaryPool);
when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).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(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl());
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNOQcow2.
@Test
public void testPrimaryStorageDownloadCommandNOTemplateNOQcow2() {
final StoragePool pool = Mockito.mock(StoragePool.class);
final List<KvmPhysicalDisk> disks = new ArrayList<>();
final List<KvmPhysicalDisk> spiedDisks = Mockito.spy(disks);
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(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
when(secondaryPool.listPhysicalDisks()).thenReturn(spiedDisks);
when(spiedDisks.isEmpty()).thenReturn(false);
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, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
}
use of com.cloud.agent.resource.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.
the class LibvirtResizeVolumeCommandWrapper method execute.
@Override
public Answer execute(final ResizeVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String volid = command.getPath();
final long newSize = command.getNewSize();
final long currentSize = command.getCurrentSize();
final String vmInstanceName = command.getInstanceName();
final boolean shrinkOk = command.getShrinkOk();
final StorageFilerTO spool = command.getPool();
if (currentSize == newSize) {
s_logger.info("No need to resize volume: current size " + currentSize + " is same as new size " + newSize);
return new ResizeVolumeAnswer(command, true, "success", currentSize);
}
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
final KvmStoragePool pool = storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
final KvmPhysicalDisk vol = pool.getPhysicalDisk(volid);
final String path = vol.getPath();
s_logger.debug("Resizing volume: " + path + "," + currentSize + "," + newSize + "," + vol.getFormat() + "," + vmInstanceName + "," + shrinkOk);
if (pool.getType() == StoragePoolType.RBD) {
s_logger.debug("Volume " + path + " is on a RBD storage pool. No need to query for additional information.");
} else if (pool.getType() == StoragePoolType.LVM || pool.getType() == StoragePoolType.CLVM) {
s_logger.debug("Volume " + path + " can be resized by libvirt. Asking libvirt to resize the volume.");
final LVM lvm = new LVM(command.getWait());
try {
// 1. Resize the logical volume
lvm.resize(newSize, vol.getPath());
// 2. If the volume is attached to a virtualmachine, notify libvirt domain of the size change
libvirtBlockResize(libvirtComputingResource, newSize, vmInstanceName, vol);
} catch (final LVMException e) {
// First step went wrong, nothing to clean up. Just return that it didn't work out.
return new ResizeVolumeAnswer(command, false, e.toString());
} catch (final LibvirtException e) {
// Second step went wrong, we should resize the volume back to how it was!
try {
lvm.resize(currentSize, vol.getPath());
} catch (final LVMException e1) {
s_logger.error("Unable to reverse lv resize: " + e1);
}
return new ResizeVolumeAnswer(command, false, e.toString());
}
} else if (pool.getType() == StoragePoolType.NetworkFilesystem) {
if (vol.getFormat() == PhysicalDiskFormat.QCOW2 && shrinkOk) {
return new ResizeVolumeAnswer(command, false, "Unable to shrink volumes of type " + PhysicalDiskFormat.QCOW2);
}
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect connection = libvirtUtilitiesHelper.getConnection();
final StorageVol storageVol = connection.storageVolLookupByPath(vol.getPath());
final VirtualMachine.PowerState state = libvirtComputingResource.getVmState(libvirtUtilitiesHelper.getConnection(), vmInstanceName);
if (state == VirtualMachine.PowerState.PowerOn) {
libvirtBlockResize(libvirtComputingResource, newSize, vmInstanceName, vol);
} else {
final int flags = shrinkOk ? StorageVol.ResizeFlags.SHRINK : 0;
storageVol.resize(newSize, flags);
}
} catch (final LibvirtException e) {
return new ResizeVolumeAnswer(command, false, e.toString());
}
}
/* fetch new size as seen from libvirt, don't want to assume anything */
pool.refresh();
final long finalSize = pool.getPhysicalDisk(volid).getVirtualSize();
s_logger.debug("after resize, size reports as " + finalSize + ", requested " + newSize);
return new ResizeVolumeAnswer(command, true, "success", finalSize);
}
Aggregations