Search in sources :

Example 6 with LibvirtStoragePoolDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method createStoragePool.

@Override
public KvmStoragePool createStoragePool(final String name, final String host, final int port, String path, final String userInfo, final StoragePoolType type) {
    this.logger.info("Attempting to create storage pool " + name + " (" + type.toString() + ") in libvirt");
    StoragePool sp;
    final Connect conn;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (final LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    try {
        sp = conn.storagePoolLookupByUUIDString(name);
        if (sp != null && sp.isActive() == 0) {
            sp.undefine();
            sp = null;
            this.logger.info("Found existing defined storage pool " + name + ". It wasn't running, so we undefined it.");
        }
        if (sp != null) {
            this.logger.info("Found existing defined storage pool " + name + ", using it.");
        }
    } catch (final LibvirtException e) {
        sp = null;
        this.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.
        this.logger.info("Didn't find an existing storage pool " + name + " by UUID, checking for pools with duplicate paths");
        try {
            final String[] poolnames = conn.listStoragePools();
            for (final String poolname : poolnames) {
                this.logger.debug("Checking path of existing pool " + poolname + " against pool we want to create");
                final StoragePool p = conn.storagePoolLookupByName(poolname);
                final LibvirtStoragePoolDef pdef = getStoragePoolDef(p);
                final String targetPath = pdef.getTargetPath();
                if (targetPath != null && targetPath.equals(path)) {
                    this.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 (final LibvirtException e) {
            this.logger.error("Failure in attempting to see if an existing storage pool might be using the path " + "of the pool to be created:" + e);
        }
        this.logger.debug("Attempting to create storage pool " + name);
        if (type == StoragePoolType.NetworkFilesystem) {
            try {
                sp = createNetfsStoragePool(PoolType.NETFS, conn, name, host, path);
            } catch (final LibvirtException e) {
                this.logger.error("Failed to create netfs mount: " + host + ":" + path, e);
                this.logger.error(Arrays.toString(e.getStackTrace()));
                throw new CloudRuntimeException(e.toString());
            }
        } else if (type == StoragePoolType.Gluster) {
            try {
                sp = createNetfsStoragePool(PoolType.GLUSTERFS, conn, name, host, path);
            } catch (final LibvirtException e) {
                this.logger.error("Failed to create glusterfs mount: " + host + ":" + path, e);
                this.logger.error(Arrays.toString(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);
        } else if (type == StoragePoolType.LVM) {
            sp = createLVMStoragePool(conn, name, path);
        }
    }
    if (sp == null) {
        throw new CloudRuntimeException("Failed to create storage pool: " + name);
    }
    try {
        if (sp.isActive() == 0) {
            this.logger.debug("Attempting to activate pool " + name);
            sp.create(0);
        }
        return getStoragePool(name);
    } catch (final LibvirtException e) {
        final 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.legacymodel.exceptions.CloudRuntimeException) Connect(org.libvirt.Connect) LibvirtStoragePoolDef(com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef)

Example 7 with LibvirtStoragePoolDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method createSharedStoragePool.

private StoragePool createSharedStoragePool(final Connect conn, final String uuid, final String host, final String path) {
    if (!this.storageLayer.exists(path)) {
        this.logger.error(path + " does not exists. Check local.storage.path in agent.properties.");
        return null;
    }
    final LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(PoolType.DIR, uuid, uuid, host, path, path);
    final StoragePool sp;
    try {
        this.logger.debug(spd.toString());
        sp = conn.storagePoolCreateXML(spd.toString(), 0);
        return sp;
    } catch (final LibvirtException e) {
        this.logger.error(e.toString());
        return null;
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) LibvirtStoragePoolDef(com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef)

Example 8 with LibvirtStoragePoolDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef in project cosmic by MissionCriticalCloud.

the class ManagedNfsStorageAdaptor method connectPhysicalDisk.

/*
     * creates a nfs storage pool using libvirt
     */
@Override
public boolean connectPhysicalDisk(final String volumeUuid, final KvmStoragePool pool, final Map<String, String> details) {
    StoragePool sp = null;
    Connect conn = null;
    String targetPath = null;
    LibvirtStoragePoolDef spd = null;
    try {
        conn = LibvirtConnection.getConnection();
        if (conn == null) {
            throw new CloudRuntimeException("Failed to create Libvrt Connection");
        }
        targetPath = "/mnt" + volumeUuid;
        spd = new LibvirtStoragePoolDef(PoolType.NETFS, volumeUuid, details.get(DiskTO.UUID), pool.getSourceHost(), details.get(DiskTO.MOUNT_POINT), targetPath);
        this.storageLayer.mkdir(targetPath);
        this.logger.debug(spd.toString());
        sp = conn.storagePoolCreateXML(spd.toString(), 0);
        if (sp == null) {
            throw new CloudRuntimeException("Failed to create storage pool:" + volumeUuid);
        }
        try {
            if (sp.isActive() == 0) {
                // s_logger.debug("attempting to activate pool " + name);
                sp.create(0);
            }
            // now add the storage pool
            final LibvirtStoragePool storagePool = (LibvirtStoragePool) getStoragePool(pool.getUuid());
            storagePool.setPool(sp);
            return true;
        } catch (final LibvirtException e) {
            final 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);
            }
        }
    } catch (final LibvirtException e) {
        this.logger.error(e.toString());
        // if error is that pool is mounted, try to handle it
        if (e.toString().contains("already mounted")) {
            this.logger.error("Attempting to unmount old mount libvirt is unaware of at " + targetPath);
            final String result = Script.runSimpleBashScript("umount -l " + targetPath);
            if (result == null) {
                this.logger.error("Succeeded in unmounting " + targetPath);
                try {
                    conn.storagePoolCreateXML(spd.toString(), 0);
                    this.logger.error("Succeeded in redefining storage");
                    return true;
                } catch (final LibvirtException l) {
                    this.logger.error("Target was already mounted, unmounted it but failed to redefine storage:" + l);
                }
            } else {
                this.logger.error("Failed in unmounting and redefining storage");
            }
        } else {
            this.logger.error("Internal error occurred when attempting to mount:" + e.getMessage());
            // stacktrace for agent.log
            e.printStackTrace();
            throw new CloudRuntimeException(e.toString());
        }
        return false;
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Connect(org.libvirt.Connect) LibvirtStoragePoolDef(com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef)

Example 9 with LibvirtStoragePoolDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method createRbdStoragePool.

private StoragePool createRbdStoragePool(final Connect conn, final String uuid, final String host, final int port, final String userInfo, final String path) {
    final LibvirtStoragePoolDef storagePoolDefinition;
    final StoragePool storagePool;
    Secret secretString = null;
    final String[] userInfoTemp = userInfo.split(":");
    if (userInfoTemp.length == 2) {
        final LibvirtSecretDef sd = new LibvirtSecretDef(Usage.CEPH, uuid);
        sd.setCephName(userInfoTemp[0] + "@" + host + ":" + port + "/" + path);
        try {
            this.logger.debug(sd.toString());
            secretString = conn.secretDefineXML(sd.toString());
            secretString.setValue(Base64.decodeBase64(userInfoTemp[1]));
        } catch (final LibvirtException e) {
            this.logger.error("Failed to define the libvirt secret: " + e.toString());
            if (secretString != null) {
                try {
                    secretString.undefine();
                    secretString.free();
                } catch (final LibvirtException l) {
                    this.logger.error("Failed to undefine the libvirt secret: " + l.toString());
                }
            }
            return null;
        }
        storagePoolDefinition = new LibvirtStoragePoolDef(PoolType.RBD, uuid, uuid, host, port, path, userInfoTemp[0], AuthenticationType.CEPH, uuid);
    } else {
        storagePoolDefinition = new LibvirtStoragePoolDef(PoolType.RBD, uuid, uuid, host, port, path, "");
    }
    try {
        this.logger.debug(storagePoolDefinition.toString());
        storagePool = conn.storagePoolCreateXML(storagePoolDefinition.toString(), 0);
        return storagePool;
    } catch (final LibvirtException e) {
        this.logger.error("Failed to create RBD storage pool: " + e.toString());
        if (secretString != null) {
            try {
                this.logger.error("Failed to create the RBD storage pool, cleaning up the libvirt secret");
                secretString.undefine();
                secretString.free();
            } catch (final LibvirtException se) {
                this.logger.error("Failed to remove the libvirt secret: " + se.toString());
            }
        }
        return null;
    }
}
Also used : Secret(org.libvirt.Secret) StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) LibvirtStoragePoolDef(com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef) LibvirtSecretDef(com.cloud.agent.resource.kvm.xml.LibvirtSecretDef)

Example 10 with LibvirtStoragePoolDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method createNetfsStoragePool.

private StoragePool createNetfsStoragePool(final PoolType fsType, final Connect conn, final String uuid, final String host, final String path) throws LibvirtException {
    final String targetPath = this.mountPoint + File.separator + uuid;
    final LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(fsType, uuid, uuid, host, path, targetPath);
    this.storageLayer.mkdir(targetPath);
    StoragePool sp;
    try {
        this.logger.debug(spd.toString());
        sp = conn.storagePoolCreateXML(spd.toString(), 0);
        return sp;
    } catch (final LibvirtException e) {
        this.logger.error(e.toString());
        // if error is that pool is mounted, try to handle it
        if (e.toString().contains("already mounted")) {
            this.logger.error("Attempting to unmount old mount libvirt is unaware of at " + targetPath);
            final String result = Script.runSimpleBashScript("umount -l " + targetPath);
            if (result == null) {
                this.logger.error("Succeeded in unmounting " + targetPath);
                try {
                    sp = conn.storagePoolCreateXML(spd.toString(), 0);
                    this.logger.error("Succeeded in redefining storage");
                    return sp;
                } catch (final LibvirtException l) {
                    this.logger.error("Target was already mounted, unmounted it but failed to redefine storage:" + l);
                }
            } else {
                this.logger.error("Failed in unmounting and redefining storage");
            }
        } else {
            this.logger.error("Internal error occurred when attempting to mount: specified path may be invalid");
            throw e;
        }
        return null;
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) LibvirtStoragePoolDef(com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef)

Aggregations

LibvirtStoragePoolDef (com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef)11 LibvirtException (org.libvirt.LibvirtException)8 StoragePool (org.libvirt.StoragePool)6 PoolType (com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef.PoolType)3 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)3 Connect (org.libvirt.Connect)3 Secret (org.libvirt.Secret)2 LibvirtSecretDef (com.cloud.agent.resource.kvm.xml.LibvirtSecretDef)1 AuthenticationType (com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef.AuthenticationType)1 StoragePoolType (com.cloud.model.enumeration.StoragePoolType)1