From: Clementine Hayat <clem(a)lse.epita.fr>
The code to set the connection and connect using libiscsi will always be
the same. Add function to avoid code duplication.
Signed-off-by: Clementine Hayat <clem(a)lse.epita.fr>
---
src/storage/storage_backend_iscsi_direct.c | 38 +++++++++++++++-------
1 file changed, 26 insertions(+), 12 deletions(-)
diff --git a/src/storage/storage_backend_iscsi_direct.c
b/src/storage/storage_backend_iscsi_direct.c
index 62b7e0d8bc..7bdd39582b 100644
--- a/src/storage/storage_backend_iscsi_direct.c
+++ b/src/storage/storage_backend_iscsi_direct.c
@@ -557,23 +557,37 @@ virStorageBackendISCSIDirectFindPoolSources(const char *srcSpec,
return ret;
}
+struct iscsi_context *
+virStorageBackendISCSIDirectSetConnection(virStoragePoolObjPtr pool,
+ char **portal)
+{
+ virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
+ struct iscsi_context *iscsi = NULL;
+
+ if (!(*iscsi = virISCSIDirectCreateContext(def->source.initiator.iqn)))
+ return iscsi;
+ if (!(*portal = virStorageBackendISCSIDirectPortal(&def->source)))
+ goto error ;
+ if (virStorageBackendISCSIDirectSetAuth(*iscsi, &def->source) < 0)
+ goto error ;
+ if (virISCSIDirectSetContext(*iscsi, def->source.devices[0].path,
ISCSI_SESSION_NORMAL) < 0)
+ goto error ;
+ if (virISCSIDirectConnect(*iscsi, *portal) < 0)
+ goto error ;
+ return iscsi;
+
+ error:
+ iscsi_destroy_context(iscsi);
+ return iscsi;
+}
+
static int
virStorageBackendISCSIDirectRefreshPool(virStoragePoolObjPtr pool)
{
- virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
struct iscsi_context *iscsi = NULL;
char *portal = NULL;
int ret = -1;
-
- if (!(iscsi = virISCSIDirectCreateContext(def->source.initiator.iqn)))
- goto cleanup;
- if (!(portal = virStorageBackendISCSIDirectPortal(&def->source)))
- goto cleanup;
- if (virStorageBackendISCSIDirectSetAuth(iscsi, &def->source) < 0)
- goto cleanup;
- if (virISCSIDirectSetContext(iscsi, def->source.devices[0].path,
ISCSI_SESSION_NORMAL) < 0)
- goto cleanup;
- if (virISCSIDirectConnect(iscsi, portal) < 0)
+ if (!(iscsi = virStorageBackendISCSIDirectSetConnection(pool, &portal)))
goto cleanup;
if (virISCSIDirectReportLuns(pool, iscsi, portal) < 0)
goto disconect;
@@ -581,9 +595,9 @@ virStorageBackendISCSIDirectRefreshPool(virStoragePoolObjPtr pool)
ret = 0;
disconect:
virISCSIDirectDisconnect(iscsi);
+ iscsi_destroy_context(iscsi);
cleanup:
VIR_FREE(portal);
- iscsi_destroy_context(iscsi);
return ret;
}
--
2.18.0