Re: [libvirt] [PATCH 3/3] storage: Implement iscsi_direct pool backend

2018-07-20 Thread Gabriel Laskar
On Sat, 14 Jul 2018 00:06:15 +0200
c...@lse.epita.fr wrote:

> From: Clementine Hayat 
> 
> We need here libiscsi for the storgae pool backend.
> For the iscsi-direct storage pool, only checkPool and refreshPool should
> be necessary.
> The pool is state-less and just need the informations within the volume
> to work.
> 
> Signed-off-by: Clementine Hayat 
> ---
>  m4/virt-storage-iscsi-direct.m4|   3 +
>  src/storage/Makefile.inc.am|   3 +
>  src/storage/storage_backend_iscsi_direct.c | 407 -
>  3 files changed, 410 insertions(+), 3 deletions(-)
> 
> diff --git a/m4/virt-storage-iscsi-direct.m4 b/m4/virt-storage-iscsi-direct.m4
> index cc2d490352..dab4414169 100644
> --- a/m4/virt-storage-iscsi-direct.m4
> +++ b/m4/virt-storage-iscsi-direct.m4
> @@ -29,6 +29,9 @@ AC_DEFUN([LIBVIRT_STORAGE_CHECK_ISCSI_DIRECT], [
>  with_storage_iscsi_direct=$with_libiscsi
>fi
>if test "$with_storage_iscsi_direct" = "yes"; then
> +if test "$with_libiscsi" = "no"; then
> +  AC_MSG_ERROR([Need libiscsi for iscsi-direct storage driver])
> +fi
>  AC_DEFINE_UNQUOTED([WITH_STORAGE_ISCSI_DIRECT], [1],
> [whether iSCSI backend for storage driver is enabled])
>fi
> diff --git a/src/storage/Makefile.inc.am b/src/storage/Makefile.inc.am
> index d81864f5b9..bd5ea06f8b 100644
> --- a/src/storage/Makefile.inc.am
> +++ b/src/storage/Makefile.inc.am
> @@ -202,6 +202,8 @@ endif WITH_STORAGE_ISCSI
>  if WITH_STORAGE_ISCSI_DIRECT
>  libvirt_storage_backend_iscsi_direct_la_SOURCES = 
> $(STORAGE_DRIVER_ISCSI_DIRECT_SOURCES)
>  libvirt_storage_backend_iscsi_direct_la_CFLAGS = \
> + -I$(srcdir)/conf \
> + -I$(srcdir)/secret \
>   $(LIBISCSI_CFLAGS) \
>   $(AM_CFLAGS) \
>   $(NULL)
> @@ -210,6 +212,7 @@ storagebackend_LTLIBRARIES += 
> libvirt_storage_backend_iscsi-direct.la
>  libvirt_storage_backend_iscsi_direct_la_LDFLAGS = $(AM_LDFLAGS_MOD)
>  libvirt_storage_backend_iscsi_direct_la_LIBADD = \
>   libvirt.la \
> + $(LIBISCSI_LIBS) \
>   ../gnulib/lib/libgnu.la \
>   $(NULL)
>  endif WITH_STORAGE_ISCSI_DIRECT
> diff --git a/src/storage/storage_backend_iscsi_direct.c 
> b/src/storage/storage_backend_iscsi_direct.c
> index e3c1f75b42..f93c8e1e67 100644
> --- a/src/storage/storage_backend_iscsi_direct.c
> +++ b/src/storage/storage_backend_iscsi_direct.c
> @@ -1,34 +1,435 @@
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
>  #include 
>  #include 
>  #include 
>  
>  #include "datatypes.h"
>  #include "driver.h"
> +#include "secret_util.h"
>  #include "storage_backend_iscsi_direct.h"
>  #include "storage_util.h"
> +#include "viralloc.h"
> +#include "vircommand.h"
> +#include "virerror.h"
> +#include "virfile.h"
>  #include "virlog.h"
>  #include "virobject.h"
> +#include "virstring.h"
> +#include "virtime.h"
> +#include "viruuid.h"


>From what I see there is a lot of unused headers here:

* fcntl.h
* string.h
* sys/stat.h
* sys/wait.h
* unistd.h
 
* driver.h
* vircommand.h
* virfile.h

>  
>  #define VIR_FROM_THIS VIR_FROM_STORAGE
>  
> +#define ISCSI_DEFAULT_TARGET_PORT 3260
> +#define VIR_ISCSI_TEST_UNIT_TIMEOUT 30 * 1000
> +
>  VIR_LOG_INIT("storage.storage_backend_iscsi_direct");
>  
> +static struct iscsi_context *
> +virISCSIDirectCreateContext(const char* initiator_iqn)
> +{
> +struct iscsi_context *iscsi = NULL;
> +
> +iscsi = iscsi_create_context(initiator_iqn);
> +if (!iscsi)
> +virReportError(VIR_ERR_INTERNAL_ERROR,
> +   _("Failed to create iscsi context for %s"),
> +   initiator_iqn);
> +return iscsi;
> +}
> +
> +static char *
> +virStorageBackendISCSIDirectPortal(virStoragePoolSourcePtr source)
> +{
> +char *portal = NULL;
> +
> +if (source->nhost != 1) {
> +virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
> +   _("Expected exactly 1 host for the storage pool"));
> +return NULL;
> +}
> +if (source->hosts[0].port == 0) {
> +ignore_value(virAsprintf(, "%s:%d",
> + source->hosts[0].name,
> + ISCSI_DEFAULT_TARGET_PORT));
> +} else if (strchr(source->hosts[0].name, ':')) {
> +ignore_value(virAsprintf(, "[%s]:%d",
> + source->hosts[0].name,
> + source->hosts[0].port));
> +} else {
> +ignore_value(virAsprintf(, "%s:%d",
> + source->hosts[0].name,
> + source->hosts[0].port));
> +}
> +return portal;
> +}
> +
> +static int
> +virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,
> +virStoragePoolSourcePtr source)
> +{
> +unsigned char *secret_value = NULL;
> +size_t secret_size;
> +virStorageAuthDefPtr authdef = source->auth;
> +int ret = -1;
> +virConnectPtr conn = NULL;
> +
> +   

[libvirt] [PATCH 3/3] storage: Implement iscsi_direct pool backend

2018-07-13 Thread clem
From: Clementine Hayat 

We need here libiscsi for the storgae pool backend.
For the iscsi-direct storage pool, only checkPool and refreshPool should
be necessary.
The pool is state-less and just need the informations within the volume
to work.

Signed-off-by: Clementine Hayat 
---
 m4/virt-storage-iscsi-direct.m4|   3 +
 src/storage/Makefile.inc.am|   3 +
 src/storage/storage_backend_iscsi_direct.c | 407 -
 3 files changed, 410 insertions(+), 3 deletions(-)

diff --git a/m4/virt-storage-iscsi-direct.m4 b/m4/virt-storage-iscsi-direct.m4
index cc2d490352..dab4414169 100644
--- a/m4/virt-storage-iscsi-direct.m4
+++ b/m4/virt-storage-iscsi-direct.m4
@@ -29,6 +29,9 @@ AC_DEFUN([LIBVIRT_STORAGE_CHECK_ISCSI_DIRECT], [
 with_storage_iscsi_direct=$with_libiscsi
   fi
   if test "$with_storage_iscsi_direct" = "yes"; then
+if test "$with_libiscsi" = "no"; then
+  AC_MSG_ERROR([Need libiscsi for iscsi-direct storage driver])
+fi
 AC_DEFINE_UNQUOTED([WITH_STORAGE_ISCSI_DIRECT], [1],
[whether iSCSI backend for storage driver is enabled])
   fi
diff --git a/src/storage/Makefile.inc.am b/src/storage/Makefile.inc.am
index d81864f5b9..bd5ea06f8b 100644
--- a/src/storage/Makefile.inc.am
+++ b/src/storage/Makefile.inc.am
@@ -202,6 +202,8 @@ endif WITH_STORAGE_ISCSI
 if WITH_STORAGE_ISCSI_DIRECT
 libvirt_storage_backend_iscsi_direct_la_SOURCES = 
$(STORAGE_DRIVER_ISCSI_DIRECT_SOURCES)
 libvirt_storage_backend_iscsi_direct_la_CFLAGS = \
+   -I$(srcdir)/conf \
+   -I$(srcdir)/secret \
$(LIBISCSI_CFLAGS) \
$(AM_CFLAGS) \
$(NULL)
@@ -210,6 +212,7 @@ storagebackend_LTLIBRARIES += 
libvirt_storage_backend_iscsi-direct.la
 libvirt_storage_backend_iscsi_direct_la_LDFLAGS = $(AM_LDFLAGS_MOD)
 libvirt_storage_backend_iscsi_direct_la_LIBADD = \
libvirt.la \
+   $(LIBISCSI_LIBS) \
../gnulib/lib/libgnu.la \
$(NULL)
 endif WITH_STORAGE_ISCSI_DIRECT
diff --git a/src/storage/storage_backend_iscsi_direct.c 
b/src/storage/storage_backend_iscsi_direct.c
index e3c1f75b42..f93c8e1e67 100644
--- a/src/storage/storage_backend_iscsi_direct.c
+++ b/src/storage/storage_backend_iscsi_direct.c
@@ -1,34 +1,435 @@
 #include 
 #include 
+#include 
+#include 
+#include 
 #include 
 #include 
 #include 
 
 #include "datatypes.h"
 #include "driver.h"
+#include "secret_util.h"
 #include "storage_backend_iscsi_direct.h"
 #include "storage_util.h"
+#include "viralloc.h"
+#include "vircommand.h"
+#include "virerror.h"
+#include "virfile.h"
 #include "virlog.h"
 #include "virobject.h"
+#include "virstring.h"
+#include "virtime.h"
+#include "viruuid.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
+#define ISCSI_DEFAULT_TARGET_PORT 3260
+#define VIR_ISCSI_TEST_UNIT_TIMEOUT 30 * 1000
+
 VIR_LOG_INIT("storage.storage_backend_iscsi_direct");
 
+static struct iscsi_context *
+virISCSIDirectCreateContext(const char* initiator_iqn)
+{
+struct iscsi_context *iscsi = NULL;
+
+iscsi = iscsi_create_context(initiator_iqn);
+if (!iscsi)
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _("Failed to create iscsi context for %s"),
+   initiator_iqn);
+return iscsi;
+}
+
+static char *
+virStorageBackendISCSIDirectPortal(virStoragePoolSourcePtr source)
+{
+char *portal = NULL;
+
+if (source->nhost != 1) {
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+   _("Expected exactly 1 host for the storage pool"));
+return NULL;
+}
+if (source->hosts[0].port == 0) {
+ignore_value(virAsprintf(, "%s:%d",
+ source->hosts[0].name,
+ ISCSI_DEFAULT_TARGET_PORT));
+} else if (strchr(source->hosts[0].name, ':')) {
+ignore_value(virAsprintf(, "[%s]:%d",
+ source->hosts[0].name,
+ source->hosts[0].port));
+} else {
+ignore_value(virAsprintf(, "%s:%d",
+ source->hosts[0].name,
+ source->hosts[0].port));
+}
+return portal;
+}
+
+static int
+virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,
+virStoragePoolSourcePtr source)
+{
+unsigned char *secret_value = NULL;
+size_t secret_size;
+virStorageAuthDefPtr authdef = source->auth;
+int ret = -1;
+virConnectPtr conn = NULL;
+
+if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
+return 0;
+
+VIR_DEBUG("username='%s' authType=%d seclookupdef.type=%d",
+  authdef->username, authdef->authType, 
authdef->seclookupdef.type);
+
+if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
+virReportError(VIR_ERR_XML_ERROR, "%s",
+   _("iscsi-direct pool only supports 'chap' auth type"));
+return ret;
+}
+
+if (!(conn =