Search in sources :

Example 41 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class ManagedNfsStorageAdaptor method getPhysicalDisk.

/*
     * creates a disk based on the created nfs storage pool using libvirt
     */
@Override
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
    // now create the volume upon the given storage pool in kvm
    Connect conn;
    StoragePool virtPool = null;
    try {
        conn = LibvirtConnection.getConnection();
        virtPool = conn.storagePoolLookupByName("/" + volumeUuid);
    } catch (LibvirtException e1) {
        throw new CloudRuntimeException(e1.toString());
    }
    LibvirtStorageVolumeDef.VolumeFormat libvirtformat = null;
    long volCapacity = 0;
    // check whether the volume is present on the given pool
    StorageVol vol = getVolume(virtPool, volumeUuid);
    try {
        if (vol == null) {
            libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.QCOW2;
            StoragePoolInfo poolinfo = virtPool.getInfo();
            volCapacity = poolinfo.available;
            LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(volumeUuid, volCapacity, libvirtformat, null, null);
            s_logger.debug(volDef.toString());
            vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
        }
        KVMPhysicalDisk disk = new KVMPhysicalDisk(vol.getPath(), volumeUuid, pool);
        disk.setFormat(PhysicalDiskFormat.QCOW2);
        disk.setSize(vol.getInfo().allocation);
        disk.setVirtualSize(vol.getInfo().capacity);
        return disk;
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) LibvirtStorageVolumeDef(com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef) StorageVol(org.libvirt.StorageVol) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect) StoragePoolInfo(org.libvirt.StoragePoolInfo)

Example 42 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtStorageAdaptor method createStoragePool.

@Override
public KVMStoragePool createStoragePool(String name, String host, int port, String path, String userInfo, StoragePoolType type) {
    s_logger.info("Attempting to create storage pool " + name + " (" + type.toString() + ") in libvirt");
    StoragePool sp = null;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    try {
        sp = conn.storagePoolLookupByUUIDString(name);
        if (sp != null && sp.isActive() == 0) {
            sp.undefine();
            sp = null;
            s_logger.info("Found existing defined storage pool " + name + ". It wasn't running, so we undefined it.");
        }
        if (sp != null) {
            s_logger.info("Found existing defined storage pool " + name + ", using it.");
        }
    } catch (LibvirtException e) {
        sp = null;
        s_logger.warn("Storage pool " + name + " was not found running in libvirt. Need to create it.");
    }
    // existing paths
    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    if (sp == null) {
        // see if any existing pool by another name is using our storage path.
        // if anyone is, undefine the pool so we can define it as requested.
        // This should be safe since a pool in use can't be removed, and no
        // volumes are affected by unregistering the pool with libvirt.
        s_logger.info("Didn't find an existing storage pool " + name + " by UUID, checking for pools with duplicate paths");
        try {
            String[] poolnames = conn.listStoragePools();
            for (String poolname : poolnames) {
                s_logger.debug("Checking path of existing pool " + poolname + " against pool we want to create");
                StoragePool p = conn.storagePoolLookupByName(poolname);
                LibvirtStoragePoolDef pdef = getStoragePoolDef(conn, p);
                String targetPath = pdef.getTargetPath();
                if (targetPath != null && targetPath.equals(path)) {
                    s_logger.debug("Storage pool utilizing path '" + path + "' already exists as pool " + poolname + ", undefining so we can re-define with correct name " + name);
                    if (p.isPersistent() == 1) {
                        p.destroy();
                        p.undefine();
                    } else {
                        p.destroy();
                    }
                }
            }
        } catch (LibvirtException e) {
            s_logger.error("Failure in attempting to see if an existing storage pool might be using the path of the pool to be created:" + e);
        }
        s_logger.debug("Attempting to create storage pool " + name);
        if (type == StoragePoolType.NetworkFilesystem) {
            try {
                sp = createNetfsStoragePool(PoolType.NETFS, conn, name, host, path);
            } catch (LibvirtException e) {
                s_logger.error("Failed to create netfs mount: " + host + ":" + path, e);
                s_logger.error(e.getStackTrace());
                throw new CloudRuntimeException(e.toString());
            }
        } else if (type == StoragePoolType.Gluster) {
            try {
                sp = createNetfsStoragePool(PoolType.GLUSTERFS, conn, name, host, path);
            } catch (LibvirtException e) {
                s_logger.error("Failed to create glusterfs mount: " + host + ":" + path, e);
                s_logger.error(e.getStackTrace());
                throw new CloudRuntimeException(e.toString());
            }
        } else if (type == StoragePoolType.SharedMountPoint || type == StoragePoolType.Filesystem) {
            sp = createSharedStoragePool(conn, name, host, path);
        } else if (type == StoragePoolType.RBD) {
            sp = createRBDStoragePool(conn, name, host, port, userInfo, path);
        } else if (type == StoragePoolType.CLVM) {
            sp = createCLVMStoragePool(conn, name, host, path);
        }
    }
    if (sp == null) {
        throw new CloudRuntimeException("Failed to create storage pool: " + name);
    }
    try {
        if (sp.isActive() == 0) {
            s_logger.debug("Attempting to activate pool " + name);
            sp.create(0);
        }
        return getStoragePool(name);
    } catch (LibvirtException e) {
        String error = e.toString();
        if (error.contains("Storage source conflict")) {
            throw new CloudRuntimeException("A pool matching this location already exists in libvirt, " + " but has a different UUID/Name. Cannot create new pool without first " + " removing it. Check for inactive pools via 'virsh pool-list --all'. " + error);
        } else {
            throw new CloudRuntimeException(error);
        }
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect) LibvirtStoragePoolDef(com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef)

Example 43 with Connect

use of org.libvirt.Connect 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;
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect)

Example 44 with Connect

use of org.libvirt.Connect 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();
}
Also used : AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtException(org.libvirt.LibvirtException) PrepareForMigrationCommand(com.cloud.agent.api.PrepareForMigrationCommand) Connect(org.libvirt.Connect) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) NicTO(com.cloud.agent.api.to.NicTO) DiskTO(com.cloud.agent.api.to.DiskTO) Test(org.junit.Test)

Example 45 with Connect

use of org.libvirt.Connect 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());
    }
}
Also used : AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) CheckVirtualMachineCommand(com.cloud.agent.api.CheckVirtualMachineCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Aggregations

Connect (org.libvirt.Connect)113 LibvirtException (org.libvirt.LibvirtException)112 Answer (com.cloud.agent.api.Answer)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)42 Test (org.junit.Test)40 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)39 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)39 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)39 InternalErrorException (com.cloud.exception.InternalErrorException)33 Domain (org.libvirt.Domain)30 URISyntaxException (java.net.URISyntaxException)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 NicTO (com.cloud.agent.api.to.NicTO)23 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)22 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)19 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)16 ConfigurationException (javax.naming.ConfigurationException)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)11