[PATCH] storage: Linstor support

Peter Krempa pkrempa at redhat.com
Mon Jan 18 13:12:04 UTC 2021


On Fri, Jan 15, 2021 at 08:16:47 +0100, Rene Peinthor wrote:
> Implement a LINSTOR backend storage driver.
> The Linstor client needs to be installed and it needs to be configured
> on the nodes used by the controller.
> 
> It supports most pool/vol commands, except for pool-build/pool-delete
> and provides a block device in RAW file mode.
> Linstor supports more than just DRBD so it would also be possible to have
> it provide LVM, ZFS or NVME volumes, but the common case will be to provide
> DRBD volumes in a cluster.
> 
> Sample pool XML:
> <pool type='linstor'>
>   <name>linstor</name>
>   <source>
>     <host name='ubuntu-focal-60'/>
>     <name>libvirtgrp</name>
>   </source>
> </pool>
> 
> <pool/source/name> element must point to an already created LINSTOR
> resource-group, which is used to spawn resources/volumes.
> <pool/source/host at name> attribute should be the local linstor node name,
> if missing it will try to get the hosts uname and use that instead.
> 
> Result volume XML sample:
> <volume type='block'>
>   <name>alpine12</name>
>   <key>libvirtgrp/alpine12</key>
>   <capacity unit='bytes'>5368709120</capacity>
>   <allocation unit='bytes'>5540028416</allocation>
>   <target>
>     <path>/dev/drbd1000</path>
>     <format type='raw'/>
>   </target>
> </volume>
> 
> Signed-off-by: Rene Peinthor <rene.peinthor at linbit.com>
> ---

Firstly I'd like you to split the patch into more granular commits as
it's customary in our project ...

>  docs/schemas/storagepool.rng                  |  27 +
>  docs/storage.html.in                          |  39 +
>  include/libvirt/libvirt-storage.h             |   1 +
>  meson.build                                   |   6 +
>  meson_options.txt                             |   1 +
>  po/POTFILES.in                                |   1 +
>  src/conf/domain_conf.c                        |   1 +
>  src/conf/storage_conf.c                       |  14 +-
>  src/conf/storage_conf.h                       |   1 +
>  src/conf/virstorageobj.c                      |   4 +-a

Conf changes are usually separate

>  src/storage/meson.build                       |  25 +
>  src/storage/storage_backend.c                 |   6 +
>  src/storage/storage_backend_linstor.c         | 803 ++++++++++++++++++
>  src/storage/storage_backend_linstor.h         |  23 +
>  src/storage/storage_backend_linstor_priv.h    |  53 ++
>  src/storage/storage_driver.c                  |   1 +

Implementation should also be separate

>  src/test/test_driver.c                        |   1 +
>  tests/linstorjsondata/broken.json             |   1 +
>  tests/linstorjsondata/resource-group.json     |   1 +
>  .../linstorjsondata/resource-list-test2.json  | 332 ++++++++
>  .../storage-pools-ssdpool.json                |  72 ++
>  tests/linstorjsondata/storage-pools.json      | 192 +++++
>  tests/linstorjsondata/volume-def-list.json    | 158 ++++
>  .../volume-definition-test2.json              |   1 +
>  tests/meson.build                             |   6 +
>  tests/storagebackendlinstortest.c             | 371 ++++++++
>  .../storagepoolcapsschemadata/poolcaps-fs.xml |   7 +
>  .../poolcaps-full.xml                         |   7 +
>  tests/storagepoolxml2argvtest.c               |   1 +
>  tests/storagepoolxml2xmlin/pool-linstor.xml   |   8 +
>  tests/storagevolxml2xmlin/vol-linstor.xml     |  10 +

Placing tests depends. Usually XML related tests go with the commits
implementing the parser/formatter or follow that commit.

Other tests should be added separately.

>  tools/virsh-pool.c                            |   3 +

This is adding the support for the new type so it goes where the type is
declared

>  32 files changed, 2175 insertions(+), 2 deletions(-)

[...]

> diff --git a/docs/schemas/storagepool.rng b/docs/schemas/storagepool.rng
> index bd24b8b8d0..9b163e611d 100644
> --- a/docs/schemas/storagepool.rng
> +++ b/docs/schemas/storagepool.rng
> @@ -26,6 +26,7 @@
>          <ref name="poolgluster"/>
>          <ref name="poolzfs"/>
>          <ref name="poolvstorage"/>
> +        <ref name="poollinstor"/>
>        </choice>
>      </element>
>    </define>
> @@ -224,6 +225,21 @@
>      </interleave>
>    </define>
>  
> +  <define name="poollinstor">
> +    <attribute name="type">
> +      <value>linstor</value>
> +    </attribute>
> +    <interleave>
> +      <ref name="commonMetadataNameOptional"/>
> +      <ref name="sizing"/>
> +      <ref name="features"/>
> +      <ref name="sourcelinstor"/>
> +      <optional>
> +        <ref name="target"/>
> +      </optional>
> +    </interleave>
> +  </define>
> +
>    <define name="sourceinfovendor">
>      <interleave>
>        <optional>
> @@ -463,6 +479,17 @@
>      </element>
>    </define>
>  
> +  <define name="sourcelinstor">
> +    <element name="source">
> +      <interleave>
> +        <ref name="sourceinfoname"/>
> +        <optional>
> +          <ref name="sourceinfohost"/>
> +        </optional>
> +      </interleave>
> +    </element>
> +  </define>
> +
>    <define name="sourcefmtfs">
>      <optional>
>        <element name="format">
> diff --git a/docs/storage.html.in b/docs/storage.html.in
> index b2cf343933..9130fbd180 100644
> --- a/docs/storage.html.in
> +++ b/docs/storage.html.in
> @@ -829,5 +829,44 @@
>  
>      <h3>Valid volume format types</h3>
>      <p>The valid volume types are the same as for the directory pool.</p>
> +
> +
> +    <h2><a id="StorageBackendLINSTOR">LINSTOR pool</a></h2>
> +    <p>
> +      This provides a pool using the LINSTOR software-defined-storage.
> +      LINSTOR can provide block storage devices based on DRBD or basic
> +      LVM/ZFS volumes.
> +    </p>
> +
> +    <p>
> +      To use LINSTOR in libvirt, setup a working LINSTOR cluster, documentation
> +      for that is in the LINSTOR Users-guide.

Could you link to it?

Also I'd like to ask you to provide a way to setup this storage on our
CI system so that we can compile-test the new driver.

We'd also like to know if you are willing to continue maintaining this
storage driver, so that it doesn't become abandonware right at commit
time.

...

Non-exhaustive review follows. Please note that some comments apply on
multiple places, so please make sure to fix all of the issues.

> +      And create a resource-group that will be used by libvirt, also make sure
> +      the resource-group is setup in a way so that all nodes you want to use with libvirt
> +      will create a resource. So either use diskless-on-remaining or make sure
> +      replica-count is the same as you have nodes in your cluster.
> +    </p>
> +
> +    <p><span class="since">Since 7.1.0</span></p>.
> +
> +    <h3>Example pool input</h3>
> +    <pre>
> +    <pool type="linstor">
> +    <name>linstorpool</name>
> +    <source>
> +    <name>libvirtrscgrp</name>
> +    <host name="linstornode">/>
> +    </source>
> +    </pool></pre>
> +
> +    <h3>Valid pool format types</h3> +    <p>
> +      The LINSTOR volume pool does not use the pool format type element.
> +    </p>
> +
> +    <h3>Valid volume format types</h3>
> +    <p>
> +      The LINSTOR volume pool does not use the volume format type element.
> +    </p>
>    </body>
>  </html>
> diff --git a/include/libvirt/libvirt-storage.h b/include/libvirt/libvirt-storage.h
> index 089e1e0bd1..6876ce6c5a 100644
> --- a/include/libvirt/libvirt-storage.h
> +++ b/include/libvirt/libvirt-storage.h
> @@ -245,6 +245,7 @@ typedef enum {
>      VIR_CONNECT_LIST_STORAGE_POOLS_ZFS           = 1 << 17,
>      VIR_CONNECT_LIST_STORAGE_POOLS_VSTORAGE      = 1 << 18,
>      VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI_DIRECT  = 1 << 19,
> +    VIR_CONNECT_LIST_STORAGE_POOLS_LINSTOR       = 1 << 20,
>  } virConnectListAllStoragePoolsFlags;
>  
>  int                     virConnectListAllStoragePools(virConnectPtr conn,



> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index 01b7187637..cdf1da81c8 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -31333,6 +31333,7 @@ virDomainStorageSourceTranslateSourcePool(virStorageSourcePtr src,
>      case VIR_STORAGE_POOL_SCSI:
>      case VIR_STORAGE_POOL_ZFS:
>      case VIR_STORAGE_POOL_VSTORAGE:
> +    case VIR_STORAGE_POOL_LINSTOR:
>          if (!(src->path = virStorageVolGetPath(vol)))
>              return -1;
>

So if you use it like this, it means that the storage must be accessible
via regular open() on the host system, is that so? (Asking because the
pool XML hints that the pool is accessed via a network protocol)

[...]


> diff --git a/src/conf/virstorageobj.c b/src/conf/virstorageobj.c
> index 9fe8b3f28e..4a2a924eb2 100644
> --- a/src/conf/virstorageobj.c
> +++ b/src/conf/virstorageobj.c
> @@ -1461,13 +1461,15 @@ virStoragePoolObjSourceFindDuplicateCb(const void *payload,
>      case VIR_STORAGE_POOL_FS:
>      case VIR_STORAGE_POOL_LOGICAL:
>      case VIR_STORAGE_POOL_DISK:
> +    case VIR_STORAGE_POOL_LINSTOR:
>      case VIR_STORAGE_POOL_ZFS:
>          if ((data->def->type == VIR_STORAGE_POOL_ISCSI ||
>               data->def->type == VIR_STORAGE_POOL_ISCSI_DIRECT ||
>               data->def->type == VIR_STORAGE_POOL_FS ||
>               data->def->type == VIR_STORAGE_POOL_LOGICAL ||
>               data->def->type == VIR_STORAGE_POOL_DISK ||
> -             data->def->type == VIR_STORAGE_POOL_ZFS) &&
> +             data->def->type == VIR_STORAGE_POOL_ZFS ||
> +             data->def->type == VIR_STORAGE_POOL_LINSTOR) &&
>              virStoragePoolObjSourceMatchTypeDEVICE(obj, data->def))
>              return 1;
>          break;

[...]


> diff --git a/src/storage/storage_backend_linstor.c b/src/storage/storage_backend_linstor.c
> new file mode 100644
> index 0000000000..65463eb26b
> --- /dev/null
> +++ b/src/storage/storage_backend_linstor.c
> @@ -0,0 +1,803 @@
> +/*
> + * storage_backend_linstor.c: storage backend for linstor volume handling
> + *
> + * Copyright (C) 2020-2021 Rene Peinthor
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library.  If not, see
> + * <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <config.h>
> +
> +#include "storage_backend_linstor.h"
> +#define LIBVIRT_STORAGE_BACKEND_LINSTOR_PRIV_H_ALLOW
> +#include "storage_backend_linstor_priv.h"
> +#include "virerror.h"
> +#include "virjson.h"
> +#include "virstring.h"
> +#include "virlog.h"
> +#include "viralloc.h"
> +#include "storage_conf.h"
> +#include "storage_util.h"
> +
> +#include <sys/utsname.h>
> +
> +#define VIR_FROM_THIS VIR_FROM_STORAGE
> +
> +VIR_LOG_INIT("storage.storage_backend_linstor");
> +
> +
> +#define LINSTORCLI "linstor"

This should be defined via the build system once you detect where the
program is located, to prevent PATH env variable from playing a role in
which program is actually used.

> +
> +
> +/**
> + * @brief virStorageBackendLinstorGetNodeName
> + *        Get the configured linstor node name, checks pool host[0]
> + *        if node isn't set there, it will try to get hostname and use that.
> + * @param pool Pool configuration
> + * @param nodenameOut Retrieved nodename will be copied here, caller is responsible to free.
> + * @return -1 on error, otherwise 0
> + */
> +static int
> +virStorageBackendLinstorGetNodeName(virStoragePoolObjPtr pool, char **nodenameOut)
> +{
> +    int ret = 0;
> +    struct utsname host;
> +    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
> +    if (def->source.nhost > 0 && def->source.hosts[0].name != NULL)
> +         *nodenameOut = g_strdup(def->source.hosts[0].name);
> +    else if (uname(&host) == 0)
> +        *nodenameOut = g_strdup(host.nodename);

We have virGetHostname which is made more robust.

> +    else
> +        ret = -1;
> +
> +    return ret;
> +}
> +
> +
> +static virCommandPtr
> +virStorageBackendLinstorPrepLinstorCmd(bool machineout)
> +{
> +    if (machineout)
> +        return virCommandNewArgList(LINSTORCLI, "-m", "--output-version", "v1", NULL);



> +    else
> +        return virCommandNewArgList(LINSTORCLI, NULL);
> +}
> +
> +
> +/**
> + * @brief virStorageBackendLinstorUnpackLinstorJSON
> + *        Linstor client results are packed into an array, as results usually contain
> + *        a list of apicallrcs. But lists usually only have 1 entry.
> + * @param replyArr linstor reply array json
> + * @return Pointer to the first array element or NULL if no array or empty
> + */
> +static virJSONValuePtr
> +virStorageBackendLinstorUnpackLinstorJSON(virJSONValuePtr replyArr)
> +{
> +    if (replyArr == NULL) {
> +        return NULL;
> +    }
> +
> +    if (!virJSONValueIsArray(replyArr)) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("Root Linstor list result is expected to be an array"));
> +        return NULL;
> +    }
> +
> +    if (virJSONValueArraySize(replyArr) == 0) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("Empty reply from Linstor client"));
> +        return NULL;
> +    }
> +
> +    return virJSONValueArrayGet(replyArr, 0);
> +}
> +
> +
> +int
> +virStorageBackendLinstorFilterRscDefsForRscGroup(const char *resourceGroup,
> +                                                 const char *output,
> +                                                 virJSONValuePtr rscDefArrayOut)
> +{
> +    int ret = -1;
> +    virJSONValuePtr replyArr = NULL;
> +    virJSONValuePtr rscDefArr = NULL;
> +    size_t i;
> +
> +    replyArr = virJSONValueFromString(output);
> +
> +    rscDefArr = virStorageBackendLinstorUnpackLinstorJSON(replyArr);
> +    if (rscDefArr == NULL) {
> +        goto cleanup;
> +    }
> +
> +    for (i = 0; i < virJSONValueArraySize(rscDefArr); i++) {
> +        virJSONValuePtr rscDefObj = virJSONValueArrayGet(rscDefArr, i);
> +
> +        if (g_ascii_strcasecmp(virJSONValueObjectGetString(rscDefObj, "resource_group_name"),
> +                               resourceGroup) == 0) {
> +
> +            virJSONValueArrayAppendString(rscDefArrayOut,
> +                                          g_strdup(virJSONValueObjectGetString(rscDefObj, "name")));

Return value should be checked

> +        }
> +    }
> +
> +    ret = 0;
> + cleanup:
> +    virJSONValueFree(replyArr);
> +    return ret;
> +}
> +
> +
> +int
> +virStorageBackendLinstorParseResourceGroupList(const char *resourceGroup,
> +                                               const char *output,
> +                                               virJSONValuePtr *storPoolArrayOut)
> +{
> +    int ret = -1;
> +    bool rscGrpFound = false;
> +    virJSONValuePtr replyArr = NULL;
> +    virJSONValuePtr rscGrpArr = NULL;
> +    virJSONValuePtr rscGrpSelFilterObj = NULL;
> +    virJSONValuePtr storPoolsArr = NULL;
> +    size_t i;
> +
> +    replyArr = virJSONValueFromString(output);
> +
> +    rscGrpArr = virStorageBackendLinstorUnpackLinstorJSON(replyArr);
> +    if (rscGrpArr == NULL) {
> +        goto cleanup;
> +    }
> +
> +    for (i = 0; i < virJSONValueArraySize(rscGrpArr); i++) {
> +        virJSONValuePtr rscGrpObj = virJSONValueArrayGet(rscGrpArr, i);
> +
> +        if (g_ascii_strcasecmp(virJSONValueObjectGetString(rscGrpObj, "name"),
> +                               resourceGroup) == 0) {
> +            rscGrpFound = true;
> +
> +            rscGrpSelFilterObj = virJSONValueObjectGetObject(rscGrpObj, "select_filter");
> +            if (rscGrpSelFilterObj != NULL) {
> +                storPoolsArr = virJSONValueObjectGetArray(rscGrpSelFilterObj, "storage_pool_list");
> +
> +                *storPoolArrayOut = virJSONValueCopy(storPoolsArr);
> +            }
> +            break;
> +        }
> +    }
> +
> +    if (!rscGrpFound) {
> +        virReportError(VIR_ERR_INVALID_STORAGE_POOL,
> +                      _("Specified resource group '%s' not found in linstor"), resourceGroup);
> +        goto cleanup;
> +    }
> +
> +    ret = 0;
> + cleanup:
> +    virJSONValueFree(replyArr);
> +    return ret;
> +}
> +
> +
> +/**
> + * @brief virStorageBackendLinstorParseStoragePoolList
> + *        Parses a storage pool list result and updates the pools capacity, allocation numbers,
> + *        for the given node.
> + * @param pool Pool object to update
> + * @param nodename Node name of which storage pools are taken for the update.
> + * @param output JSON output content from the `linstor storage-pool list` command
> + * @return -1 on error, 0 on success
> + */
> +int
> +virStorageBackendLinstorParseStoragePoolList(virStoragePoolDefPtr pool,
> +                                             const char* nodename,
> +                                             const char *output)
> +{
> +    int ret = -1;
> +    virJSONValuePtr replyArr = NULL;
> +    virJSONValuePtr storpoolArr = NULL;
> +    unsigned long long capacity = 0;
> +    unsigned long long freeCapacity = 0;
> +    size_t i;
> +
> +    replyArr = virJSONValueFromString(output);
> +
> +    storpoolArr = virStorageBackendLinstorUnpackLinstorJSON(replyArr);
> +    if (storpoolArr == NULL) {
> +        goto cleanup;
> +    }
> +
> +    if (!virJSONValueIsArray(storpoolArr)) {
> +        // probably an ApiCallRc then, with an error

We don't use the '//' style of comments

> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("Storage pool list not recieved"));
> +        goto cleanup;
> +    }
> +
> +    for (i = 0; i < virJSONValueArraySize(storpoolArr); i++) {
> +        unsigned long long storCapacity = 0;
> +        unsigned long long storFree = 0;
> +        virJSONValuePtr storPoolObj = virJSONValueArrayGet(storpoolArr, i);
> +
> +        if (!virJSONValueIsObject(storPoolObj)) {
> +            virReportError(VIR_ERR_INTERNAL_ERROR,
> +                           _("Unable to parse storage pool object for pool '%s'"),
> +                           pool->name);
> +            goto cleanup;
> +        }
> +
> +        if (g_ascii_strcasecmp(virJSONValueObjectGetString(storPoolObj, "node_name"), nodename) == 0) {
> +            if (g_str_equal(virJSONValueObjectGetString(storPoolObj, "provider_kind"), "DISKLESS")) {
> +                /* ignore diskless pools, as they have no capacity */
> +                continue;
> +            }
> +
> +            if (virJSONValueObjectGetNumberUlong(storPoolObj, "total_capacity", &storCapacity)) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("Unable to parse storage pool '%s' capacity"),
> +                               virJSONValueObjectGetString(storPoolObj, "storage_pool_name"));
> +                goto cleanup;
> +            }
> +            if (virJSONValueObjectGetNumberUlong(storPoolObj, "free_capacity", &storFree)) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("Unable to parse storage pool '%s' free capacity"),
> +                               virJSONValueObjectGetString(storPoolObj, "storage_pool_name"));
> +                goto cleanup;
> +            }
> +            capacity += storCapacity * 1024; // linstor reports in KiB
> +            freeCapacity += storFree * 1024; // linstor reports in KiB
> +        }
> +    }
> +
> +    pool->capacity = capacity;
> +    pool->available = freeCapacity;
> +    pool->allocation = capacity - freeCapacity;
> +
> +    ret = 0;
> +
> + cleanup:
> +    virJSONValueFree(replyArr);
> +    return ret;
> +}
> +
> +
> +/**
> + * @brief virStorageBackendLinstorParseVolumeDefinition
> + *        Parses the machine output of `linstor volume-definition list` and updates
> + *        the virStorageVolDef capacity.
> + * @param vol Volume to update the capacity
> + * @param output JSON output of `linstor volume-definition list -r ...`
> + * @return -1 on error, 0 on success
> + */
> +int
> +virStorageBackendLinstorParseVolumeDefinition(virStorageVolDefPtr vol,
> +                                              const char *output)
> +{
> +    int ret = -1;
> +    virJSONValuePtr replyArr = NULL;
> +    virJSONValuePtr resourceDefArr = NULL;
> +    size_t i;
> +
> +    replyArr = virJSONValueFromString(output);
> +
> +    resourceDefArr = virStorageBackendLinstorUnpackLinstorJSON(replyArr);
> +    if (resourceDefArr == NULL) {
> +        goto cleanup;
> +    }
> +
> +    if (!virJSONValueIsArray(resourceDefArr)) {
> +        /* probably an ApiCallRc then, with an error */
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("Volume definition list not recieved"));
> +        goto cleanup;
> +    }
> +
> +    for (i = 0; i < virJSONValueArraySize(resourceDefArr); i++) {
> +        unsigned long long volDefCapacityKiB = 0;
> +        virJSONValuePtr resourceDefObj = virJSONValueArrayGet(resourceDefArr, i);
> +
> +        if (resourceDefObj == NULL || !virJSONValueIsObject(resourceDefObj)) {

virJSONValueIsObject returns false if the argument is NULL

> +            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                           _("Unable to parse resource definition object"));
> +            goto cleanup;
> +        }
> +
> +        if (g_ascii_strcasecmp(virJSONValueObjectGetString(resourceDefObj, "name"), vol->name) == 0) {
> +            virJSONValuePtr volumeDefArr = virJSONValueObjectGet(resourceDefObj, "volume_definitions");
> +            virJSONValuePtr volumeDefObj = NULL;
> +
> +            if (volumeDefArr == NULL || !virJSONValueIsArray(volumeDefArr)
> +                    || virJSONValueArraySize(volumeDefArr) == 0) {

Eww, one condition per line if you must split a long line and the
boolean operators at the end of the line.

> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("Volume definition list incorrect for resource definition '%s'"),
> +                               vol->name);
> +                goto cleanup;
> +            }
> +
> +            volumeDefObj = virJSONValueArrayGet(volumeDefArr, 0);
> +            if (virJSONValueObjectGetNumberUlong(volumeDefObj, "size_kib", &volDefCapacityKiB)) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("Unable to parse volume definition size for resource '%s'"),
> +                               vol->name);
> +                goto cleanup;
> +            }
> +
> +            /* linstor reports in KiB */
> +            vol->target.capacity = volDefCapacityKiB * 1024;
> +            break;
> +        }
> +    }
> +
> +    ret = 0;
> +
> + cleanup:
> +    virJSONValueFree(replyArr);
> +    return ret;
> +}
> +
> +
> +static int
> +virStorageBackendLinstorRefreshVolFromJSON(const char *sourceName,
> +                                           virStorageVolDefPtr vol,
> +                                           virJSONValuePtr linstorResObj,
> +                                           const char *volumeDefListOutput)
> +{
> +    virJSONValuePtr volumesArr = NULL;
> +    virJSONValuePtr volumeObj = NULL;
> +    long long alloc_kib = 0;
> +
> +    volumesArr = virJSONValueObjectGet(linstorResObj, "volumes");
> +
> +    if (volumesArr != NULL && !virJSONValueIsArray(volumesArr)) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("'volumes' not found in resource object JSON"));
> +        return -1;
> +    }
> +
> +    volumeObj = virJSONValueArrayGet(volumesArr, 0);

This should be checked, empty array will return NULL

> +
> +    vol->type = VIR_STORAGE_VOL_BLOCK;
> +    VIR_FREE(vol->key);
> +    vol->key = g_strdup_printf("%s/%s", sourceName, vol->name);
> +    VIR_FREE(vol->target.path);
> +    vol->target.path = g_strdup(virJSONValueObjectGetString(volumeObj, "device_path"));
> +    vol->target.format = VIR_STORAGE_FILE_RAW;
> +
> +    virJSONValueObjectGetNumberLong(volumeObj, "allocated_size_kib", &alloc_kib);

and this doesn't NULL-check

> +
> +    if (alloc_kib >= 0)
> +        vol->target.allocation = alloc_kib * 1024;
> +    else
> +        vol->target.allocation = 0;
> +
> +    if (volumeDefListOutput != NULL) {
> +        return virStorageBackendLinstorParseVolumeDefinition(vol, volumeDefListOutput);
> +    }
> +
> +    return 0;
> +}
> +
> +
> +static int
> +virStorageBackendLinstorRefreshVol(virStoragePoolObjPtr pool,
> +                                   virStorageVolDefPtr vol)
> +{
> +    int ret = -1;
> +    g_autofree char *output = NULL;
> +    g_autofree char *outputVolDef = NULL;
> +    g_autofree char *nodename = NULL;
> +    g_autoptr(virCommand) cmdResList = NULL;
> +    g_autoptr(virCommand) cmdVolDefList = NULL;
> +    virJSONValuePtr replyArr = NULL;
> +    virJSONValuePtr rscArr = NULL;
> +    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
> +
> +    if (virStorageBackendLinstorGetNodeName(pool, &nodename))
> +        return -1;
> +
> +    cmdResList = virStorageBackendLinstorPrepLinstorCmd(true);
> +    virCommandAddArgList(cmdResList, "resource", "list", "-n", nodename, "-r", vol->name, NULL);
> +    virCommandSetOutputBuffer(cmdResList, &output);
> +    if (virCommandRun(cmdResList, NULL) < 0)
> +        return -1;
> +
> +    cmdVolDefList = virStorageBackendLinstorPrepLinstorCmd(true);
> +    virCommandAddArgList(cmdVolDefList, "volume-definition", "list", "-r", vol->name, NULL);
> +    virCommandSetOutputBuffer(cmdVolDefList, &outputVolDef);
> +    if (virCommandRun(cmdVolDefList, NULL) < 0)
> +        return -1;
> +
> +    replyArr = virJSONValueFromString(output);
> +
> +    rscArr = virStorageBackendLinstorUnpackLinstorJSON(replyArr);
> +    if (rscArr == NULL) {
> +        goto cleanup;
> +    }
> +
> +    if (!virJSONValueIsArray(rscArr)) {
> +        // probably an ApiCallRc then, with an error
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("Resource list not recieved"));
> +        goto cleanup;
> +    }
> +
> +    if (virJSONValueArraySize(rscArr) != 1) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR,
> +                       _("Couldn't find resource '%s' in Linstor resource list JSON"), vol->name);
> +        goto cleanup;
> +    }
> +
> +    ret = virStorageBackendLinstorRefreshVolFromJSON(
> +                def->source.name, vol, virJSONValueArrayGet(rscArr, 0), outputVolDef);
> +
> + cleanup:
> +    virJSONValueFree(rscArr);
> +    return ret;
> +}
> +
> +
> +static int
> +virStorageBackendLinstorAddVolume(virStoragePoolObjPtr pool,
> +                                  virJSONValuePtr resourceObj,
> +                                  const char *outputVolDef)
> +{
> +    g_autoptr(virStorageVolDef) vol = NULL;
> +    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
> +
> +    if (resourceObj == NULL) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("Missing disk info when adding volume"));
> +        return -1;
> +    }
> +
> +    vol = g_new0(virStorageVolDef, 1);
> +
> +    vol->name = g_strdup(virJSONValueObjectGetString(resourceObj, "name"));
> +
> +    if (virStorageBackendLinstorRefreshVolFromJSON(def->source.name,
> +                                                   vol, resourceObj, outputVolDef) < 0) {
> +        virStorageVolDefFree(vol);
> +        return -1;
> +    }
> +
> +    if (virStoragePoolObjAddVol(pool, vol) < 0) {
> +        virStorageVolDefFree(vol);
> +        return -1;
> +    }
> +    vol = NULL;
> +
> +    return 0;
> +}
> +
> +
> +static bool
> +virStorageBackendLinstorStringInJSONArray(virJSONValuePtr arr, const char *string)
> +{
> +    size_t i;
> +    for (i = 0; i < virJSONValueArraySize(arr); i++) {
> +        if (g_ascii_strcasecmp(virJSONValueGetString(virJSONValueArrayGet(arr, i)), string) == 0) {
> +            return true;
> +        }
> +    }
> +    return false;
> +}
> +
> +
> +int
> +virStorageBackendLinstorParseResourceList(virStoragePoolObjPtr pool,
> +                                          const char *nodeName,
> +                                          virJSONValuePtr rscDefFilterArr,
> +                                          const char *outputRscList,
> +                                          const char *outputVolDef)
> +{
> +    int ret = -1;
> +    virJSONValuePtr replyArr = NULL;
> +    virJSONValuePtr rscListArr = NULL;
> +    size_t i;
> +
> +    replyArr = virJSONValueFromString(outputRscList);
> +
> +    rscListArr = virStorageBackendLinstorUnpackLinstorJSON(replyArr);
> +    if (rscListArr == NULL) {
> +        goto cleanup;
> +    }
> +
> +    if (!virJSONValueIsArray(rscListArr)) {
> +        // probably an ApiCallRc then, with an error
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("Storage pool list not recieved"));
> +        goto cleanup;
> +    }
> +
> +    for (i = 0; i < virJSONValueArraySize(rscListArr); i++) {
> +        virJSONValuePtr rscObj = virJSONValueArrayGet(rscListArr, i);
> +
> +        if (g_ascii_strcasecmp(virJSONValueObjectGetString(rscObj, "node_name"), nodeName) == 0 &&
> +                virStorageBackendLinstorStringInJSONArray(rscDefFilterArr,
> +                                                          virJSONValueObjectGetString(rscObj, "name"))) {
> +            if (virStorageBackendLinstorAddVolume(pool, rscObj, outputVolDef)) {
> +                goto cleanup;
> +            }
> +        }
> +    }
> +
> +    ret = 0;
> +
> + cleanup:
> +    virJSONValueFree(rscListArr);
> +    return ret;
> +}
> +
> +static int
> +virStorageBackendLinstorRefreshAllVol(virStoragePoolObjPtr pool)
> +{
> +    int ret = -1;
> +    g_autofree char *output = NULL;
> +    g_autofree char *outputVolDef = NULL;
> +    g_autofree char *nodename = NULL;
> +    g_autoptr(virCommand) cmdRscList = NULL;
> +    g_autoptr(virCommand) cmdVolDefList = NULL;
> +    virJSONValuePtr rscDefFilterArr = virJSONValueNewArray();
> +    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
> +
> +    /* Get all resources usable on that node */
> +    if (virStorageBackendLinstorGetNodeName(pool, &nodename)) {
> +        goto cleanup;
> +    }
> +
> +    cmdRscList = virStorageBackendLinstorPrepLinstorCmd(true);
> +    virCommandAddArgList(cmdRscList, "resource", "list", "-n", nodename, NULL);
> +    virCommandSetOutputBuffer(cmdRscList, &output);
> +    if (virCommandRun(cmdRscList, NULL) < 0)
> +        goto cleanup;
> +
> +    /* Get a list of resources that belong to the rsc group for filtering */
> +    cmdVolDefList = virStorageBackendLinstorPrepLinstorCmd(true);
> +    virCommandAddArgList(cmdVolDefList, "volume-definition", "list", NULL);
> +    virCommandSetOutputBuffer(cmdVolDefList, &outputVolDef);
> +    if (virCommandRun(cmdVolDefList, NULL) < 0) {
> +        goto cleanup;
> +    }
> +
> +    /* resource belonging to the resource group will be stored in rscDefFilterArr */
> +    if (virStorageBackendLinstorFilterRscDefsForRscGroup(def->source.name,
> +                                                         outputVolDef,
> +                                                         rscDefFilterArr)) {
> +        goto cleanup;
> +    }
> +
> +    ret = virStorageBackendLinstorParseResourceList(pool,
> +                                                    nodename,
> +                                                    rscDefFilterArr,
> +                                                    output,
> +                                                    outputVolDef);
> +
> + cleanup:
> +    virJSONValueFree(rscDefFilterArr);
> +    return ret;
> +}
> +
> +
> +/**
> + * @brief virStorageBackendLinstorGetRscGrpPools
> + *        Retrieves the set storage pools used in resource group.
> + *        On success caller is responsible to free the virJSONValuePtr.
> + * @param rscgrpname resource group name to get the storage pools
> + * @param storagePoolsOut virJSONArray with used storage pools
> + * @return -1 on error, 0 on success
> + */
> +static int
> +virStorageBackendLinstorGetRscGrpPools(const char* rscgrpname, virJSONValuePtr *storagePoolsOut)
> +{
> +    g_autofree char *outputRscGrp = NULL;
> +    g_autoptr(virCommand) cmdRscGrpList = NULL;
> +
> +    cmdRscGrpList = virStorageBackendLinstorPrepLinstorCmd(true);
> +    virCommandAddArgList(cmdRscGrpList, "resource-group", "list", "-r", rscgrpname, NULL);
> +    virCommandSetOutputBuffer(cmdRscGrpList, &outputRscGrp);
> +    if (virCommandRun(cmdRscGrpList, NULL) < 0)
> +        return -1;
> +
> +    if (virStorageBackendLinstorParseResourceGroupList(rscgrpname,
> +                                                       outputRscGrp,
> +                                                       storagePoolsOut)) {
> +        return -1;
> +    }
> +
> +    return 0;
> +}
> +
> +
> +static int
> +virStorageBackendLinstorRefreshPool(virStoragePoolObjPtr pool)
> +{
> +    size_t i;
> +    g_autofree char *outputStorPoolList = NULL;
> +    g_autofree char *nodename = NULL;
> +    g_autoptr(virCommand) cmdStorPoolList = NULL;
> +    virJSONValuePtr storagePoolArr = NULL;
> +    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
> +
> +    if (virStorageBackendLinstorGetNodeName(pool, &nodename))
> +        return -1;
> +
> +    if (virStorageBackendLinstorGetRscGrpPools(def->source.name, &storagePoolArr))
> +        return -1;
> +
> +    /* Get storage pools used in the used resource group */
> +    cmdStorPoolList = virStorageBackendLinstorPrepLinstorCmd(true);
> +    virCommandAddArgList(cmdStorPoolList, "storage-pool", "list", "-n", nodename, NULL);
> +
> +    if (storagePoolArr != NULL && virJSONValueArraySize(storagePoolArr) > 0) {
> +        virCommandAddArgList(cmdStorPoolList, "-s", NULL);
> +        for (i = 0; i < virJSONValueArraySize(storagePoolArr); i++) {
> +            virCommandAddArg(cmdStorPoolList,
> +                             virJSONValueGetString(virJSONValueArrayGet(storagePoolArr, i)));
> +        }
> +
> +        virJSONValueFree(storagePoolArr);
> +    }
> +
> +    virCommandSetOutputBuffer(cmdStorPoolList, &outputStorPoolList);
> +    if (virCommandRun(cmdStorPoolList, NULL) < 0)
> +        return -1;
> +
> +    /* update capacity and allocated from used storage pools */
> +    if (virStorageBackendLinstorParseStoragePoolList(virStoragePoolObjGetDef(pool),
> +                                                     nodename,
> +                                                     outputStorPoolList) < 0)
> +        return -1;
> +
> +    /* Get volumes used in the resource group and add */
> +    return virStorageBackendLinstorRefreshAllVol(pool);
> +}
> +
> +static int
> +virStorageBackendLinstorCreateVol(virStoragePoolObjPtr pool,
> +                                  virStorageVolDefPtr vol)
> +{
> +    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
> +    g_autoptr(virCommand) cmdRscGrp = NULL;
> +
> +    VIR_DEBUG("Creating Linstor image %s/%s with size %llu",
> +              def->source.name, vol->name, vol->target.capacity);
> +
> +    if (!vol->target.capacity) {
> +        virReportError(VIR_ERR_NO_SUPPORT, "%s",
> +                       _("volume capacity required for this storage pool"));
> +        return -1;
> +    }
> +
> +    if (vol->target.format != VIR_STORAGE_FILE_RAW) {
> +        virReportError(VIR_ERR_NO_SUPPORT, "%s",
> +                       _("only RAW volumes are supported by this storage pool"));
> +        return -1;
> +    }
> +
> +    if (vol->target.encryption != NULL) {
> +        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
> +                       _("storage pool does not support encrypted volumes"));
> +        return -1;
> +    }
> +
> +    /* spawn resource */
> +    cmdRscGrp = virStorageBackendLinstorPrepLinstorCmd(false);
> +    virCommandAddArgList(cmdRscGrp, "resource-group", "spawn",
> +                         "--partial", def->source.name, vol->name, NULL);
> +    virCommandAddArgFormat(cmdRscGrp, "%lluKiB", vol->target.capacity / 1024);
> +    if (virCommandRun(cmdRscGrp, NULL) < 0)
> +        return -1;
> +
> +    /* set volume path and key */
> +    /* we could skip getting the capacity as we already know it */
> +    return virStorageBackendLinstorRefreshVol(pool, vol);
> +}
> +
> +
> +static int
> +virStorageBackendLinstorBuildVolFrom(virStoragePoolObjPtr pool,
> +                                     virStorageVolDefPtr vol,
> +                                     virStorageVolDefPtr inputvol,
> +                                     unsigned int flags)
> +{
> +    virStorageBackendBuildVolFrom build_func;
> +
> +    build_func = virStorageBackendGetBuildVolFromFunction(vol, inputvol);
> +    if (!build_func)
> +        return -1;
> +
> +    return build_func(pool, vol, inputvol, flags);
> +}
> +
> +
> +static int
> +virStorageBackendLinstorDeleteVol(virStoragePoolObjPtr pool,
> +                                  virStorageVolDefPtr vol,
> +                                  unsigned int flags)
> +{
> +    g_autoptr(virCommand) cmd = NULL;
> +
> +    (void)pool;
> +    virCheckFlags(0, -1);
> +
> +    cmd = virStorageBackendLinstorPrepLinstorCmd(false);
> +    virCommandAddArgList(cmd, "resource-definition", "delete", vol->name, NULL);
> +    return virCommandRun(cmd, NULL);
> +}
> +
> +
> +static int
> +virStorageBackendLinstorResizeVol(virStoragePoolObjPtr pool,
> +                                  virStorageVolDefPtr vol,
> +                                  unsigned long long capacity,
> +                                  unsigned int flags)
> +{
> +    g_autoptr(virCommand) cmd = NULL;
> +
> +    (void)pool;
> +    virCheckFlags(0, -1);
> +
> +    cmd = virStorageBackendLinstorPrepLinstorCmd(false);
> +    virCommandAddArgList(cmd, "volume-definition", "set-size", vol->name, "0", NULL);
> +    virCommandAddArgFormat(cmd, "%lluKiB", capacity / 1024);
> +    return virCommandRun(cmd, NULL);
> +}
> +
> +
> +/**
> + * @brief virStorageBackendVzCheck
> + *        Check if we can connect to a Linstor-Controller
> + */
> +static int
> +virStorageBackendLinstorCheck(virStoragePoolObjPtr pool,
> +                         bool *isActive)
> +{
> +    g_autoptr(virCommand) cmd = NULL;
> +
> +    (void)pool;

Use G_GNUC_UNUSED in the parameter declaration if you don't want to use
the parameter.

> +
> +    /* This command gets the controller version */
> +    cmd = virStorageBackendLinstorPrepLinstorCmd(false);
> +    virCommandAddArgList(cmd, "controller", "version", NULL);
> +    if (virCommandRun(cmd, NULL)) {
> +        *isActive = false;
> +    }
> +
> +    *isActive = true;
> +    return 0;
> +}
> +
> +virStorageBackend virStorageBackendLinstor = {
> +    .type = VIR_STORAGE_POOL_LINSTOR,
> +
> +    .refreshPool = virStorageBackendLinstorRefreshPool,
> +    .checkPool = virStorageBackendLinstorCheck,
> +    .createVol = virStorageBackendLinstorCreateVol,
> +    .buildVol = NULL,
> +    .buildVolFrom = virStorageBackendLinstorBuildVolFrom,
> +    .refreshVol = virStorageBackendLinstorRefreshVol,
> +    .deleteVol = virStorageBackendLinstorDeleteVol,
> +    .resizeVol = virStorageBackendLinstorResizeVol,
> +    .uploadVol = virStorageBackendVolUploadLocal,
> +    .downloadVol = virStorageBackendVolDownloadLocal,
> +    .wipeVol = virStorageBackendVolWipeLocal,
> +};
> +
> +
> +int
> +virStorageBackendLinstorRegister(void)
> +{
> +    return virStorageBackendRegister(&virStorageBackendLinstor);
> +}
> diff --git a/src/storage/storage_backend_linstor.h b/src/storage/storage_backend_linstor.h
> new file mode 100644
> index 0000000000..72750fd278
> --- /dev/null
> +++ b/src/storage/storage_backend_linstor.h
> @@ -0,0 +1,23 @@
> +/*
> + * storage_backend_linstor.h: storage backend for Sheepdog handling
> + *
> + * Copyright (C) 2020-2021 Rene Peinthor
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library.  If not, see
> + * <http://www.gnu.org/licenses/>.
> + */
> +
> +#pragma once
> +
> +int virStorageBackendLinstorRegister(void);
[...]

Most cleanup sections just call virJSONValueFree, which can be replaced
by a g_autoptr(virJSONValue) varname = NULL;




More information about the libvir-list mailing list