Search in sources :

Example 1 with StoragePoolType

use of com.cloud.model.enumeration.StoragePoolType in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method getStoragePool.

@Override
public KvmStoragePool getStoragePool(final String uuid, final boolean refreshInfo) {
    this.logger.info("Trying to fetch storage pool " + uuid + " from libvirt");
    final StoragePool storage;
    try {
        final Connect conn = LibvirtConnection.getConnection();
        storage = conn.storagePoolLookupByUUIDString(uuid);
        if (storage.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
            this.logger.warn("Storage pool " + uuid + " is not in running state. Attempting to start it.");
            storage.create(0);
        }
        final LibvirtStoragePoolDef spd = getStoragePoolDef(storage);
        if (spd == null) {
            throw new CloudRuntimeException("Unable to parse the storage pool definition for storage pool " + uuid);
        }
        StoragePoolType type = null;
        if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.NETFS) {
            type = StoragePoolType.NetworkFilesystem;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.DIR) {
            type = StoragePoolType.Filesystem;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.RBD) {
            type = StoragePoolType.RBD;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.LOGICAL) {
            type = StoragePoolType.LVM;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.GLUSTERFS) {
            type = StoragePoolType.Gluster;
        }
        final LibvirtStoragePool pool = new LibvirtStoragePool(uuid, storage.getName(), type, this, storage);
        if (pool.getType() != StoragePoolType.RBD) {
            pool.setLocalPath(spd.getTargetPath());
        } else {
            pool.setLocalPath("");
        }
        if (pool.getType() == StoragePoolType.RBD || pool.getType() == StoragePoolType.Gluster) {
            pool.setSourceHost(spd.getSourceHost());
            pool.setSourcePort(spd.getSourcePort());
            pool.setSourceDir(spd.getSource());
            final String authUsername = spd.getAuthUsername();
            if (authUsername != null) {
                final Secret secret = conn.secretLookupByUUIDString(spd.getSecretUuid());
                final String secretValue = new String(Base64.encodeBase64(secret.getByteValue()), Charset.defaultCharset());
                pool.setAuthUsername(authUsername);
                pool.setAuthSecret(secretValue);
            }
        }
        if (refreshInfo) {
            this.logger.info("Asking libvirt to refresh storage pool " + uuid);
            pool.refresh();
        }
        pool.setCapacity(storage.getInfo().capacity);
        pool.setUsed(storage.getInfo().allocation);
        pool.setAvailable(storage.getInfo().available);
        this.logger.debug("Succesfully refreshed pool " + uuid + " Capacity: " + storage.getInfo().capacity + " Used: " + storage.getInfo().allocation + " Available: " + storage.getInfo().available);
        return pool;
    } catch (final LibvirtException e) {
        this.logger.debug("Could not find storage pool " + uuid + " in libvirt");
        throw new CloudRuntimeException(e.toString(), e);
    }
}
Also used : Secret(org.libvirt.Secret) StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) StoragePoolType(com.cloud.model.enumeration.StoragePoolType) Connect(org.libvirt.Connect) LibvirtStoragePoolDef(com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef)

Example 2 with StoragePoolType

use of com.cloud.model.enumeration.StoragePoolType in project cosmic by MissionCriticalCloud.

the class KvmStoragePoolManager method getStoragePoolByUri.

public KvmStoragePool getStoragePoolByUri(final String uri) {
    URI storageUri = null;
    try {
        storageUri = new URI(uri);
    } catch (final URISyntaxException e) {
        throw new CloudRuntimeException(e.toString());
    }
    String sourcePath = null;
    String uuid = null;
    String sourceHost = "";
    StoragePoolType protocol = null;
    if (storageUri.getScheme().equalsIgnoreCase("nfs")) {
        sourcePath = storageUri.getPath();
        sourcePath = sourcePath.replace("//", "/");
        sourceHost = storageUri.getHost();
        uuid = UUID.nameUUIDFromBytes(new String(sourceHost + sourcePath).getBytes()).toString();
        protocol = StoragePoolType.NetworkFilesystem;
    }
    // secondary storage registers itself through here
    return createStoragePool(uuid, sourceHost, 0, sourcePath, "", protocol, false);
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) StoragePoolType(com.cloud.model.enumeration.StoragePoolType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 3 with StoragePoolType

use of com.cloud.model.enumeration.StoragePoolType in project cosmic by MissionCriticalCloud.

the class LibvirtStoragePoolTest method testAttributes.

public void testAttributes() {
    final String uuid = "4c4fb08b-373e-4f30-a120-3aa3a43f31da";
    final String name = "myfirstpool";
    final StoragePoolType type = StoragePoolType.NetworkFilesystem;
    final StorageAdaptor adapter = Mockito.mock(LibvirtStorageAdaptor.class);
    final StoragePool storage = Mockito.mock(StoragePool.class);
    final LibvirtStoragePool pool = new LibvirtStoragePool(uuid, name, type, adapter, storage);
    assertEquals(pool.getCapacity(), 0);
    assertEquals(pool.getUsed(), 0);
    assertEquals(pool.getName(), name);
    assertEquals(pool.getUuid(), uuid);
    assertEquals(pool.getAvailable(), 0);
    assertEquals(pool.getStoragePoolType(), type);
    pool.setCapacity(2048);
    pool.setUsed(1024);
    pool.setAvailable(1023);
    assertEquals(pool.getCapacity(), 2048);
    assertEquals(pool.getUsed(), 1024);
    assertEquals(pool.getAvailable(), 1023);
}
Also used : StoragePool(org.libvirt.StoragePool) StoragePoolType(com.cloud.model.enumeration.StoragePoolType)

Example 4 with StoragePoolType

use of com.cloud.model.enumeration.StoragePoolType in project cosmic by MissionCriticalCloud.

the class CloudStackPrimaryDataStoreLifeCycleImpl method initialize.

@Override
public DataStore initialize(final Map<String, Object> dsInfos) {
    final Long clusterId = (Long) dsInfos.get("clusterId");
    final Long podId = (Long) dsInfos.get("podId");
    final Long zoneId = (Long) dsInfos.get("zoneId");
    final String url = (String) dsInfos.get("url");
    final String providerName = (String) dsInfos.get("providerName");
    if (clusterId != null && podId == null) {
        throw new InvalidParameterValueException("Cluster id requires pod id");
    }
    final PrimaryDataStoreParameters parameters = new PrimaryDataStoreParameters();
    URI uri = null;
    try {
        uri = new URI(UriUtils.encodeURIComponent(url));
        if (uri.getScheme() == null) {
            throw new InvalidParameterValueException("scheme is null " + url + ", add nfs:// (or cifs://) as a prefix");
        } else if (uri.getScheme().equalsIgnoreCase("nfs")) {
            final String uriHost = uri.getHost();
            final String uriPath = uri.getPath();
            if (uriHost == null || uriPath == null || uriHost.trim().isEmpty() || uriPath.trim().isEmpty()) {
                throw new InvalidParameterValueException("host or path is null, should be nfs://hostname/path");
            }
        } else if (uri.getScheme().equalsIgnoreCase("cifs")) {
            // Don't validate against a URI encoded URI.
            final URI cifsUri = new URI(url);
            final String warnMsg = UriUtils.getCifsUriParametersProblems(cifsUri);
            if (warnMsg != null) {
                throw new InvalidParameterValueException(warnMsg);
            }
        } else if (uri.getScheme().equalsIgnoreCase("sharedMountPoint")) {
            final String uriPath = uri.getPath();
            if (uriPath == null) {
                throw new InvalidParameterValueException("host or path is null, should be sharedmountpoint://localhost/path");
            }
        } else if (uri.getScheme().equalsIgnoreCase("rbd")) {
            final String uriPath = uri.getPath();
            if (uriPath == null) {
                throw new InvalidParameterValueException("host or path is null, should be rbd://hostname/pool");
            }
        } else if (uri.getScheme().equalsIgnoreCase("gluster")) {
            final String uriHost = uri.getHost();
            final String uriPath = uri.getPath();
            if (uriHost == null || uriPath == null || uriHost.trim().isEmpty() || uriPath.trim().isEmpty()) {
                throw new InvalidParameterValueException("host or path is null, should be gluster://hostname/volume");
            }
        }
    } catch (final URISyntaxException e) {
        throw new InvalidParameterValueException(url + " is not a valid uri");
    }
    final String tags = (String) dsInfos.get("tags");
    final Map<String, String> details = (Map<String, String>) dsInfos.get("details");
    parameters.setTags(tags);
    parameters.setDetails(details);
    final String scheme = uri.getScheme();
    final String storageHost = uri.getHost();
    String hostPath = null;
    try {
        hostPath = URLDecoder.decode(uri.getPath(), "UTF-8");
    } catch (final UnsupportedEncodingException e) {
        s_logger.error("[ignored] we are on a platform not supporting \"UTF-8\"!?!", e);
    }
    if (hostPath == null) {
        // if decoding fails, use getPath() anyway
        hostPath = uri.getPath();
    }
    final Object localStorage = dsInfos.get("localStorage");
    if (localStorage != null) {
        hostPath = hostPath.replaceFirst("/", "");
        hostPath = hostPath.replace("+", " ");
    }
    final String userInfo = uri.getUserInfo();
    int port = uri.getPort();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("createPool Params @ scheme - " + scheme + " storageHost - " + storageHost + " hostPath - " + hostPath + " port - " + port);
    }
    if (scheme.equalsIgnoreCase("nfs")) {
        if (port == -1) {
            port = 2049;
        }
        parameters.setType(StoragePoolType.NetworkFilesystem);
        parameters.setHost(storageHost);
        parameters.setPort(port);
        parameters.setPath(hostPath);
    } else if (scheme.equalsIgnoreCase("cifs")) {
        if (port == -1) {
            port = 445;
        }
        parameters.setType(StoragePoolType.SMB);
        parameters.setHost(storageHost);
        parameters.setPort(port);
        parameters.setPath(hostPath);
    } else if (scheme.equalsIgnoreCase("file")) {
        if (port == -1) {
            port = 0;
        }
        parameters.setType(StoragePoolType.Filesystem);
        parameters.setHost("localhost");
        parameters.setPort(0);
        parameters.setPath(hostPath);
    } else if (scheme.equalsIgnoreCase("sharedMountPoint")) {
        parameters.setType(StoragePoolType.SharedMountPoint);
        parameters.setHost(storageHost);
        parameters.setPort(0);
        parameters.setPath(hostPath);
    } else if (scheme.equalsIgnoreCase("clvm")) {
        parameters.setType(StoragePoolType.CLVM);
        parameters.setHost(storageHost);
        parameters.setPort(0);
        parameters.setPath(hostPath.replaceFirst("/", ""));
    } else if (scheme.equalsIgnoreCase("rbd")) {
        if (port == -1) {
            port = 6789;
        }
        parameters.setType(StoragePoolType.RBD);
        parameters.setHost(storageHost);
        parameters.setPort(port);
        parameters.setPath(hostPath.replaceFirst("/", ""));
        parameters.setUserInfo(userInfo);
    } else if (scheme.equalsIgnoreCase("PreSetup")) {
        parameters.setType(StoragePoolType.PreSetup);
        parameters.setHost(storageHost);
        parameters.setPort(0);
        parameters.setPath(hostPath);
    } else if (scheme.equalsIgnoreCase("iscsi")) {
        final String[] tokens = hostPath.split("/");
        final int lun = NumbersUtil.parseInt(tokens[tokens.length - 1], -1);
        if (port == -1) {
            port = 3260;
        }
        if (lun != -1) {
            if (clusterId == null) {
                throw new IllegalArgumentException("IscsiLUN need to have clusters specified");
            }
            parameters.setType(StoragePoolType.IscsiLUN);
            parameters.setHost(storageHost);
            parameters.setPort(port);
            parameters.setPath(hostPath);
        } else {
            throw new IllegalArgumentException("iSCSI needs to have LUN number");
        }
    } else if (scheme.equalsIgnoreCase("iso")) {
        if (port == -1) {
            port = 2049;
        }
        parameters.setType(StoragePoolType.ISO);
        parameters.setHost(storageHost);
        parameters.setPort(port);
        parameters.setPath(hostPath);
    } else if (scheme.equalsIgnoreCase("ocfs2")) {
        port = 7777;
        parameters.setType(StoragePoolType.OCFS2);
        parameters.setHost("clustered");
        parameters.setPort(port);
        parameters.setPath(hostPath);
    } else if (scheme.equalsIgnoreCase("gluster")) {
        if (port == -1) {
            port = 24007;
        }
        parameters.setType(StoragePoolType.Gluster);
        parameters.setHost(storageHost);
        parameters.setPort(port);
        parameters.setPath(hostPath);
    } else {
        final StoragePoolType type = Enum.valueOf(StoragePoolType.class, scheme);
        if (type != null) {
            parameters.setType(type);
            parameters.setHost(storageHost);
            parameters.setPort(0);
            parameters.setPath(hostPath);
        } else {
            s_logger.warn("Unable to figure out the scheme for URI: " + uri);
            throw new IllegalArgumentException("Unable to figure out the scheme for URI: " + uri);
        }
    }
    if (localStorage == null) {
        final List<StoragePoolVO> pools = primaryDataStoreDao.listPoolByHostPath(storageHost, hostPath);
        if (!pools.isEmpty() && !scheme.equalsIgnoreCase("sharedmountpoint")) {
            final Long oldPodId = pools.get(0).getPodId();
            throw new CloudRuntimeException("Storage pool " + uri + " already in use by another pod (id=" + oldPodId + ")");
        }
    }
    final Object existingUuid = dsInfos.get("uuid");
    String uuid = null;
    if (existingUuid != null) {
        uuid = (String) existingUuid;
    } else if (scheme.equalsIgnoreCase("sharedmountpoint") || scheme.equalsIgnoreCase("clvm")) {
        uuid = UUID.randomUUID().toString();
    } else if (scheme.equalsIgnoreCase("PreSetup")) {
        uuid = hostPath.replace("/", "");
    } else {
        uuid = UUID.nameUUIDFromBytes((storageHost + hostPath).getBytes()).toString();
    }
    final List<StoragePoolVO> spHandles = primaryDataStoreDao.findIfDuplicatePoolsExistByUUID(uuid);
    if (spHandles != null && spHandles.size() > 0) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Another active pool with the same uuid already exists");
        }
        throw new CloudRuntimeException("Another active pool with the same uuid already exists");
    }
    final String poolName = (String) dsInfos.get("name");
    parameters.setUuid(uuid);
    parameters.setZoneId(zoneId);
    parameters.setPodId(podId);
    parameters.setName(poolName);
    parameters.setClusterId(clusterId);
    parameters.setProviderName(providerName);
    return dataStoreHelper.createPrimaryDataStore(parameters);
}
Also used : StoragePoolType(com.cloud.model.enumeration.StoragePoolType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PrimaryDataStoreParameters(com.cloud.engine.subsystem.api.storage.PrimaryDataStoreParameters) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) Map(java.util.Map)

Aggregations

StoragePoolType (com.cloud.model.enumeration.StoragePoolType)4 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 StoragePool (org.libvirt.StoragePool)2 LibvirtStoragePoolDef (com.cloud.agent.resource.kvm.xml.LibvirtStoragePoolDef)1 PrimaryDataStoreParameters (com.cloud.engine.subsystem.api.storage.PrimaryDataStoreParameters)1 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)1 StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 Connect (org.libvirt.Connect)1 LibvirtException (org.libvirt.LibvirtException)1 Secret (org.libvirt.Secret)1