use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtStorageAdaptor method disconnectPhysicalDiskByPath.
@Override
public boolean disconnectPhysicalDiskByPath(String localPath) {
// we've only ever cleaned up ISOs that are NFS mounted
String poolUuid = null;
if (localPath != null && localPath.startsWith(_mountPoint) && localPath.endsWith(".iso")) {
String[] token = localPath.split("/");
if (token.length > 3) {
poolUuid = token[2];
}
} else {
return false;
}
if (poolUuid == null) {
return false;
}
try {
Connect conn = LibvirtConnection.getConnection();
conn.storagePoolLookupByUUIDString(poolUuid);
deleteStoragePool(poolUuid);
return true;
} catch (LibvirtException ex) {
return false;
} catch (CloudRuntimeException ex) {
return false;
}
}
use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtStorageAdaptor method createNetfsStoragePool.
private StoragePool createNetfsStoragePool(PoolType fsType, Connect conn, String uuid, String host, String path) throws LibvirtException {
String targetPath = _mountPoint + File.separator + uuid;
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(fsType, uuid, uuid, host, path, targetPath);
_storageLayer.mkdir(targetPath);
StoragePool sp = null;
try {
s_logger.debug(spd.toString());
sp = conn.storagePoolCreateXML(spd.toString(), 0);
return sp;
} catch (LibvirtException e) {
s_logger.error(e.toString());
// if error is that pool is mounted, try to handle it
if (e.toString().contains("already mounted")) {
s_logger.error("Attempting to unmount old mount libvirt is unaware of at " + targetPath);
String result = Script.runSimpleBashScript("umount -l " + targetPath);
if (result == null) {
s_logger.error("Succeeded in unmounting " + targetPath);
try {
sp = conn.storagePoolCreateXML(spd.toString(), 0);
s_logger.error("Succeeded in redefining storage");
return sp;
} catch (LibvirtException l) {
s_logger.error("Target was already mounted, unmounted it but failed to redefine storage:" + l);
}
} else {
s_logger.error("Failed in unmounting and redefining storage");
}
} else {
s_logger.error("Internal error occurred when attempting to mount: specified path may be invalid");
throw e;
}
if (sp != null) {
try {
if (sp.isPersistent() == 1) {
sp.destroy();
sp.undefine();
} else {
sp.destroy();
}
sp.free();
} catch (LibvirtException l) {
s_logger.debug("Failed to undefine " + fsType.toString() + " storage pool with: " + l.toString());
}
}
return null;
}
}
use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtComputingResourceTest method testPrepareForMigrationCommandMigration.
@Test
public void testPrepareForMigrationCommandMigration() {
final Connect conn = Mockito.mock(Connect.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final VirtualMachineTO vm = Mockito.mock(VirtualMachineTO.class);
final KVMStoragePoolManager storagePoolManager = Mockito.mock(KVMStoragePoolManager.class);
final NicTO nicTO = Mockito.mock(NicTO.class);
final DiskTO diskTO = Mockito.mock(DiskTO.class);
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final PrepareForMigrationCommand command = new PrepareForMigrationCommand(vm);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vm.getName())).thenReturn(conn);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(vm.getNics()).thenReturn(new NicTO[] { nicTO });
when(vm.getDisks()).thenReturn(new DiskTO[] { diskTO });
when(nicTO.getType()).thenReturn(TrafficType.Guest);
when(diskTO.getType()).thenReturn(Volume.Type.ISO);
when(libvirtComputingResource.getVifDriver(nicTO.getType())).thenReturn(vifDriver);
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolManager);
when(storagePoolManager.connectPhysicalDisksViaVmSpec(vm)).thenReturn(true);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertTrue(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vm.getName());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(vm, times(1)).getNics();
verify(vm, times(1)).getDisks();
verify(diskTO, times(1)).getType();
}
use of org.libvirt.LibvirtException in project cloudstack by apache.
the class LibvirtComputingResourceTest method testCheckVirtualMachineCommand.
@Test
public void testCheckVirtualMachineCommand() {
final Connect conn = Mockito.mock(Connect.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final String vmName = "Test";
final CheckVirtualMachineCommand command = new CheckVirtualMachineCommand(vmName);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(libvirtComputingResource.getVmState(conn, command.getVmName())).thenReturn(PowerState.PowerOn);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertTrue(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of org.libvirt.LibvirtException in project CloudStack-archive by CloudStack-extras.
the class KVMHABase method getMountPoint.
protected String getMountPoint(NfsStoragePool storagePool) {
StoragePool pool = null;
String poolName = null;
try {
pool = LibvirtConnection.getConnection().storagePoolLookupByUUIDString(storagePool._poolUUID);
if (pool != null) {
StoragePoolInfo spi = pool.getInfo();
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
pool.create(0);
} else {
/*
* Sometimes, the mount point is lost, even libvirt thinks
* the storage pool still running
*/
}
}
poolName = pool.getName();
} catch (LibvirtException e) {
} finally {
try {
if (pool != null) {
pool.free();
}
} catch (LibvirtException e) {
}
}
return checkingMountPoint(storagePool, poolName);
}
Aggregations