2008-02-20 15:49:25 +00:00
|
|
|
/*
|
|
|
|
* storage_backend_iscsi.c: storage backend for iSCSI handling
|
|
|
|
*
|
2014-02-20 00:32:19 +00:00
|
|
|
* Copyright (C) 2007-2014 Red Hat, Inc.
|
2008-02-20 15:49:25 +00:00
|
|
|
* Copyright (C) 2007-2008 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2008-02-20 15:49:25 +00:00
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-03-25 16:43:38 +00:00
|
|
|
#include <dirent.h>
|
2008-02-20 15:49:25 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2010-01-21 11:50:52 +00:00
|
|
|
#include <sys/stat.h>
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2013-07-15 17:23:45 +00:00
|
|
|
#include "datatypes.h"
|
|
|
|
#include "driver.h"
|
2009-04-01 16:03:22 +00:00
|
|
|
#include "storage_backend_scsi.h"
|
2008-02-20 15:49:25 +00:00
|
|
|
#include "storage_backend_iscsi.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-12 16:27:01 +00:00
|
|
|
#include "vircommand.h"
|
2014-03-18 15:20:01 +00:00
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virlog.h"
|
2013-07-15 17:23:45 +00:00
|
|
|
#include "virobject.h"
|
2012-01-25 15:17:46 +00:00
|
|
|
#include "virrandom.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2013-10-15 08:29:18 +00:00
|
|
|
#include "viruuid.h"
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2009-01-20 17:13:33 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("storage.storage_backend_iscsi");
|
|
|
|
|
2013-07-18 16:30:29 +00:00
|
|
|
#define ISCSI_DEFAULT_TARGET_PORT 3260
|
|
|
|
|
2010-11-02 11:40:46 +00:00
|
|
|
static char *
|
|
|
|
virStorageBackendISCSIPortal(virStoragePoolSourcePtr source)
|
|
|
|
{
|
2013-06-06 12:00:13 +00:00
|
|
|
char *portal = NULL;
|
2010-11-02 11:40:46 +00:00
|
|
|
|
2012-04-25 10:43:09 +00:00
|
|
|
if (source->nhost != 1) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Expected exactly 1 host for the storage pool"));
|
2012-04-25 10:43:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:30:29 +00:00
|
|
|
if (source->hosts[0].port == 0)
|
|
|
|
source->hosts[0].port = ISCSI_DEFAULT_TARGET_PORT;
|
2010-11-02 11:40:46 +00:00
|
|
|
|
2013-07-18 16:30:29 +00:00
|
|
|
if (strchr(source->hosts[0].name, ':')) {
|
|
|
|
ignore_value(virAsprintf(&portal, "[%s]:%d,1",
|
|
|
|
source->hosts[0].name,
|
|
|
|
source->hosts[0].port));
|
2013-06-06 12:00:13 +00:00
|
|
|
} else {
|
2013-07-18 16:30:29 +00:00
|
|
|
ignore_value(virAsprintf(&portal, "%s:%d,1",
|
|
|
|
source->hosts[0].name,
|
|
|
|
source->hosts[0].port));
|
2010-11-02 11:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return portal;
|
|
|
|
}
|
|
|
|
|
2014-03-18 14:35:01 +00:00
|
|
|
struct virStorageBackendISCSISessionData {
|
|
|
|
char *session;
|
|
|
|
const char *devpath;
|
|
|
|
};
|
2010-11-02 11:40:46 +00:00
|
|
|
|
2008-02-20 15:49:25 +00:00
|
|
|
static int
|
2014-03-18 14:35:01 +00:00
|
|
|
virStorageBackendISCSIExtractSession(char **const groups,
|
|
|
|
void *opaque)
|
2008-02-20 15:49:25 +00:00
|
|
|
{
|
2014-03-18 14:35:01 +00:00
|
|
|
struct virStorageBackendISCSISessionData *data = opaque;
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2014-03-18 14:35:01 +00:00
|
|
|
if (STREQ(groups[1], data->devpath))
|
|
|
|
return VIR_STRDUP(data->session, groups[0]);
|
2008-02-20 15:49:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSISession(virStoragePoolObjPtr pool,
|
2014-03-11 11:58:35 +00:00
|
|
|
bool probe)
|
2008-02-20 15:49:25 +00:00
|
|
|
{
|
|
|
|
/*
|
2008-06-17 12:46:38 +00:00
|
|
|
* # iscsiadm --mode session
|
2008-02-20 15:49:25 +00:00
|
|
|
* tcp: [1] 192.168.122.170:3260,1 demo-tgt-b
|
|
|
|
* tcp: [2] 192.168.122.170:3260,1 demo-tgt-a
|
|
|
|
*
|
|
|
|
* Pull out 2nd and 4th fields
|
|
|
|
*/
|
|
|
|
const char *regexes[] = {
|
2014-02-21 09:22:22 +00:00
|
|
|
"^tcp:\\s+\\[(\\S+)\\]\\s+\\S+\\s+(\\S+).*$"
|
2008-02-20 15:49:25 +00:00
|
|
|
};
|
|
|
|
int vars[] = {
|
|
|
|
2,
|
|
|
|
};
|
2014-03-18 14:35:01 +00:00
|
|
|
struct virStorageBackendISCSISessionData cbdata = {
|
|
|
|
.session = NULL,
|
|
|
|
.devpath = pool->def->source.devices[0].path
|
|
|
|
};
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2012-07-11 14:56:55 +00:00
|
|
|
virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode", "session", NULL);
|
|
|
|
|
2014-03-18 14:35:01 +00:00
|
|
|
if (virCommandRunRegex(cmd,
|
|
|
|
1,
|
|
|
|
regexes,
|
|
|
|
vars,
|
|
|
|
virStorageBackendISCSIExtractSession,
|
|
|
|
&cbdata, NULL) < 0)
|
2012-07-11 14:56:55 +00:00
|
|
|
goto cleanup;
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2014-03-18 14:35:01 +00:00
|
|
|
if (cbdata.session == NULL && !probe) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("cannot find session"));
|
2012-07-11 14:56:55 +00:00
|
|
|
goto cleanup;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 14:56:55 +00:00
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
2014-03-18 14:35:01 +00:00
|
|
|
return cbdata.session;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 11:50:52 +00:00
|
|
|
|
|
|
|
#define LINE_SIZE 4096
|
|
|
|
|
|
|
|
static int
|
2010-11-02 11:40:46 +00:00
|
|
|
virStorageBackendIQNFound(const char *initiatoriqn,
|
2010-01-21 11:50:52 +00:00
|
|
|
char **ifacename)
|
|
|
|
{
|
|
|
|
int ret = IQN_MISSING, fd = -1;
|
|
|
|
char ebuf[64];
|
|
|
|
FILE *fp = NULL;
|
2011-07-04 02:41:38 +00:00
|
|
|
char *line = NULL, *newline = NULL, *iqn = NULL, *token = NULL;
|
2011-05-06 20:00:23 +00:00
|
|
|
virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "iface", NULL);
|
2010-01-21 11:50:52 +00:00
|
|
|
|
|
|
|
if (VIR_ALLOC_N(line, LINE_SIZE) != 0) {
|
|
|
|
ret = IQN_ERROR;
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not allocate memory for output of '%s'"),
|
|
|
|
ISCSIADM);
|
2010-01-21 11:50:52 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(line, 0, LINE_SIZE);
|
|
|
|
|
2011-05-06 20:00:23 +00:00
|
|
|
virCommandSetOutputFD(cmd, &fd);
|
|
|
|
if (virCommandRunAsync(cmd, NULL) < 0) {
|
2010-01-21 11:50:52 +00:00
|
|
|
ret = IQN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-11-17 02:13:29 +00:00
|
|
|
if ((fp = VIR_FDOPEN(fd, "r")) == NULL) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to open stream for file descriptor "
|
|
|
|
"when reading output from '%s': '%s'"),
|
|
|
|
ISCSIADM, virStrerror(errno, ebuf, sizeof(ebuf)));
|
2010-01-21 11:50:52 +00:00
|
|
|
ret = IQN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(line, LINE_SIZE, fp) != NULL) {
|
|
|
|
newline = strrchr(line, '\n');
|
|
|
|
if (newline == NULL) {
|
|
|
|
ret = IQN_ERROR;
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unexpected line > %d characters "
|
|
|
|
"when parsing output of '%s'"),
|
|
|
|
LINE_SIZE, ISCSIADM);
|
2010-01-21 11:50:52 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*newline = '\0';
|
|
|
|
|
|
|
|
iqn = strrchr(line, ',');
|
|
|
|
if (iqn == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
iqn++;
|
|
|
|
|
2010-11-02 11:40:46 +00:00
|
|
|
if (STREQ(iqn, initiatoriqn)) {
|
2011-07-04 02:41:38 +00:00
|
|
|
token = strchr(line, ' ');
|
|
|
|
if (!token) {
|
|
|
|
ret = IQN_ERROR;
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Missing space when parsing output "
|
|
|
|
"of '%s'"), ISCSIADM);
|
2011-07-04 02:41:38 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2013-05-03 12:49:08 +00:00
|
|
|
if (VIR_STRNDUP(*ifacename, line, token - line) < 0) {
|
2010-01-21 11:50:52 +00:00
|
|
|
ret = IQN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn);
|
|
|
|
ret = IQN_FOUND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-06 20:00:23 +00:00
|
|
|
if (virCommandWait(cmd, NULL) < 0)
|
|
|
|
ret = IQN_ERROR;
|
|
|
|
|
2010-01-21 11:50:52 +00:00
|
|
|
out:
|
|
|
|
if (ret == IQN_MISSING) {
|
2010-08-02 19:52:02 +00:00
|
|
|
VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
|
2010-01-21 11:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(line);
|
2010-11-17 02:13:29 +00:00
|
|
|
VIR_FORCE_FCLOSE(fp);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2011-05-06 20:00:23 +00:00
|
|
|
virCommandFree(cmd);
|
2010-01-21 11:50:52 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-11-02 11:40:46 +00:00
|
|
|
virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
|
2010-02-10 11:42:56 +00:00
|
|
|
char **ifacename)
|
2010-01-21 11:50:52 +00:00
|
|
|
{
|
|
|
|
int ret = -1, exitstatus = -1;
|
2012-07-11 14:56:55 +00:00
|
|
|
char *temp_ifacename;
|
|
|
|
virCommandPtr cmd = NULL;
|
2010-01-21 11:50:52 +00:00
|
|
|
|
2012-07-11 14:56:55 +00:00
|
|
|
if (virAsprintf(&temp_ifacename,
|
|
|
|
"libvirt-iface-%08llx",
|
2013-07-04 10:16:29 +00:00
|
|
|
(unsigned long long)virRandomBits(30)) < 0)
|
2012-07-11 14:56:55 +00:00
|
|
|
return -1;
|
2010-01-21 11:50:52 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'",
|
2012-07-11 14:56:55 +00:00
|
|
|
temp_ifacename, initiatoriqn);
|
2010-01-21 11:50:52 +00:00
|
|
|
|
2012-07-11 14:56:55 +00:00
|
|
|
cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "iface",
|
|
|
|
"--interface", temp_ifacename,
|
|
|
|
"--op", "new",
|
|
|
|
NULL);
|
2010-01-21 11:50:52 +00:00
|
|
|
/* Note that we ignore the exitstatus. Older versions of iscsiadm
|
|
|
|
* tools returned an exit status of > 0, even if they succeeded.
|
|
|
|
* We will just rely on whether the interface got created
|
|
|
|
* properly. */
|
2012-07-11 14:56:55 +00:00
|
|
|
if (virCommandRun(cmd, &exitstatus) < 0) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to run command '%s' to create new iscsi interface"),
|
|
|
|
ISCSIADM);
|
2012-07-11 14:56:55 +00:00
|
|
|
goto cleanup;
|
2010-01-21 11:50:52 +00:00
|
|
|
}
|
2012-07-11 14:56:55 +00:00
|
|
|
virCommandFree(cmd);
|
2010-01-21 11:50:52 +00:00
|
|
|
|
2012-07-11 14:56:55 +00:00
|
|
|
cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "iface",
|
|
|
|
"--interface", temp_ifacename,
|
|
|
|
"--op", "update",
|
|
|
|
"--name", "iface.initiatorname",
|
|
|
|
"--value",
|
|
|
|
initiatoriqn,
|
|
|
|
NULL);
|
2010-01-21 11:50:52 +00:00
|
|
|
/* Note that we ignore the exitstatus. Older versions of iscsiadm tools
|
|
|
|
* returned an exit status of > 0, even if they succeeded. We will just
|
|
|
|
* rely on whether iface file got updated properly. */
|
2012-07-11 14:56:55 +00:00
|
|
|
if (virCommandRun(cmd, &exitstatus) < 0) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to run command '%s' to update iscsi interface with IQN '%s'"),
|
|
|
|
ISCSIADM, initiatoriqn);
|
2012-07-11 14:56:55 +00:00
|
|
|
goto cleanup;
|
2010-01-21 11:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check again to make sure the interface was created. */
|
2010-11-02 11:40:46 +00:00
|
|
|
if (virStorageBackendIQNFound(initiatoriqn, ifacename) != IQN_FOUND) {
|
2010-01-21 11:50:52 +00:00
|
|
|
VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
|
|
|
|
"after attempting to create it",
|
2010-11-02 11:40:46 +00:00
|
|
|
&temp_ifacename[0], initiatoriqn);
|
2012-07-11 14:56:55 +00:00
|
|
|
goto cleanup;
|
2010-01-21 11:50:52 +00:00
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
|
2010-11-02 11:40:46 +00:00
|
|
|
*ifacename, initiatoriqn);
|
2010-01-21 11:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2012-07-11 14:56:55 +00:00
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
VIR_FREE(temp_ifacename);
|
2010-01-21 11:50:52 +00:00
|
|
|
if (ret != 0)
|
|
|
|
VIR_FREE(*ifacename);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-02 11:40:46 +00:00
|
|
|
|
2010-01-21 11:50:52 +00:00
|
|
|
static int
|
2010-11-02 11:40:46 +00:00
|
|
|
virStorageBackendISCSIConnection(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
const char *target,
|
|
|
|
const char **extraargv)
|
2010-01-21 11:50:52 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2010-11-02 11:40:46 +00:00
|
|
|
const char *const baseargv[] = {
|
|
|
|
ISCSIADM,
|
|
|
|
"--mode", "node",
|
|
|
|
"--portal", portal,
|
|
|
|
"--targetname", target,
|
|
|
|
NULL
|
|
|
|
};
|
2011-05-03 22:14:00 +00:00
|
|
|
virCommandPtr cmd;
|
2010-01-21 11:50:52 +00:00
|
|
|
char *ifacename = NULL;
|
|
|
|
|
2011-05-03 22:14:00 +00:00
|
|
|
cmd = virCommandNewArgs(baseargv);
|
|
|
|
virCommandAddArgSet(cmd, extraargv);
|
2010-01-21 11:50:52 +00:00
|
|
|
|
2010-11-02 11:40:46 +00:00
|
|
|
if (initiatoriqn) {
|
|
|
|
switch (virStorageBackendIQNFound(initiatoriqn, &ifacename)) {
|
|
|
|
case IQN_FOUND:
|
|
|
|
VIR_DEBUG("ifacename: '%s'", ifacename);
|
|
|
|
break;
|
|
|
|
case IQN_MISSING:
|
2011-05-03 22:14:00 +00:00
|
|
|
if (virStorageBackendCreateIfaceIQN(initiatoriqn,
|
|
|
|
&ifacename) != 0) {
|
2010-11-02 11:40:46 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IQN_ERROR:
|
|
|
|
default:
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-05-03 22:14:00 +00:00
|
|
|
virCommandAddArgList(cmd, "--interface", ifacename, NULL);
|
2010-01-21 11:50:52 +00:00
|
|
|
}
|
2010-11-02 11:40:46 +00:00
|
|
|
|
2011-05-03 22:14:00 +00:00
|
|
|
if (virCommandRun(cmd, NULL) < 0)
|
2010-11-02 11:40:46 +00:00
|
|
|
goto cleanup;
|
2010-01-21 11:50:52 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2010-11-02 11:40:46 +00:00
|
|
|
cleanup:
|
2011-05-03 22:14:00 +00:00
|
|
|
virCommandFree(cmd);
|
2010-01-21 11:50:52 +00:00
|
|
|
VIR_FREE(ifacename);
|
|
|
|
|
|
|
|
return ret;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 14:52:51 +00:00
|
|
|
static int
|
|
|
|
virStorageBackendISCSIConnectionLogin(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
const char *target)
|
|
|
|
{
|
|
|
|
const char *extraargv[] = { "--login", NULL };
|
|
|
|
return virStorageBackendISCSIConnection(portal, initiatoriqn, target, extraargv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageBackendISCSIConnectionLogout(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
const char *target)
|
|
|
|
{
|
|
|
|
const char *extraargv[] = { "--logout", NULL };
|
|
|
|
return virStorageBackendISCSIConnection(portal, initiatoriqn, target, extraargv);
|
|
|
|
}
|
|
|
|
|
2013-03-25 16:43:38 +00:00
|
|
|
static int
|
|
|
|
virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
|
|
|
|
uint32_t *host)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
DIR *sysdir = NULL;
|
|
|
|
struct dirent *dirent = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Finding host number from '%s'", sysfs_path);
|
|
|
|
|
|
|
|
virFileWaitForDevices();
|
|
|
|
|
|
|
|
sysdir = opendir(sysfs_path);
|
|
|
|
|
|
|
|
if (sysdir == NULL) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to opendir path '%s'"), sysfs_path);
|
|
|
|
retval = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dirent = readdir(sysdir))) {
|
|
|
|
if (STREQLEN(dirent->d_name, "target", strlen("target"))) {
|
|
|
|
if (sscanf(dirent->d_name,
|
|
|
|
"target%u:", host) != 1) {
|
|
|
|
VIR_DEBUG("Failed to parse target '%s'", dirent->d_name);
|
|
|
|
retval = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(sysdir);
|
|
|
|
out:
|
|
|
|
return retval;
|
|
|
|
}
|
2008-06-17 12:49:37 +00:00
|
|
|
|
2008-02-20 15:49:25 +00:00
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIFindLUs(virStoragePoolObjPtr pool,
|
2009-04-01 16:03:22 +00:00
|
|
|
const char *session)
|
2008-02-20 15:49:25 +00:00
|
|
|
{
|
2011-04-03 09:21:30 +00:00
|
|
|
char *sysfs_path;
|
2009-04-01 16:03:22 +00:00
|
|
|
int retval = 0;
|
|
|
|
uint32_t host;
|
2008-06-17 12:49:37 +00:00
|
|
|
|
2011-04-03 09:21:30 +00:00
|
|
|
if (virAsprintf(&sysfs_path,
|
2013-07-04 10:16:29 +00:00
|
|
|
"/sys/class/iscsi_session/session%s/device", session) < 0)
|
2011-04-03 09:21:30 +00:00
|
|
|
return -1;
|
2008-06-17 12:49:37 +00:00
|
|
|
|
2013-03-25 16:43:38 +00:00
|
|
|
if (virStorageBackendISCSIGetHostNumber(sysfs_path, &host) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-04-01 16:03:22 +00:00
|
|
|
_("Failed to get host number for iSCSI session "
|
|
|
|
"with path '%s'"),
|
2009-01-20 17:13:33 +00:00
|
|
|
sysfs_path);
|
2009-04-01 16:03:22 +00:00
|
|
|
retval = -1;
|
2008-06-17 12:49:37 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendSCSIFindLUs(pool, host) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-04-01 16:03:22 +00:00
|
|
|
_("Failed to find LUs on host %u"), host);
|
|
|
|
retval = -1;
|
2008-06-17 12:49:37 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 09:21:30 +00:00
|
|
|
VIR_FREE(sysfs_path);
|
|
|
|
|
2008-06-17 12:49:37 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2008-02-20 15:49:25 +00:00
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIRescanLUNs(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
|
2008-02-20 15:49:25 +00:00
|
|
|
const char *session)
|
|
|
|
{
|
2012-07-11 14:56:55 +00:00
|
|
|
virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "session",
|
|
|
|
"-r", session,
|
|
|
|
"-R",
|
|
|
|
NULL);
|
|
|
|
int ret = virCommandRun(cmd, NULL);
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 15:21:48 +00:00
|
|
|
struct virStorageBackendISCSITargetList {
|
|
|
|
size_t ntargets;
|
|
|
|
char **targets;
|
|
|
|
};
|
2008-02-20 15:49:25 +00:00
|
|
|
|
|
|
|
static int
|
2014-03-18 14:35:01 +00:00
|
|
|
virStorageBackendISCSIGetTargets(char **const groups,
|
2010-11-12 15:21:48 +00:00
|
|
|
void *data)
|
2008-02-20 15:49:25 +00:00
|
|
|
{
|
2010-11-12 15:21:48 +00:00
|
|
|
struct virStorageBackendISCSITargetList *list = data;
|
|
|
|
char *target;
|
|
|
|
|
2013-05-03 12:49:08 +00:00
|
|
|
if (VIR_STRDUP(target, groups[1]) < 0)
|
2010-11-12 15:21:48 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-03-07 08:33:31 +00:00
|
|
|
if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0) {
|
2010-11-12 15:21:48 +00:00
|
|
|
VIR_FREE(target);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageBackendISCSITargetAutologin(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
const char *target,
|
|
|
|
bool enable)
|
|
|
|
{
|
|
|
|
const char *extraargv[] = { "--op", "update",
|
|
|
|
"--name", "node.startup",
|
|
|
|
"--value", enable ? "automatic" : "manual",
|
|
|
|
NULL };
|
|
|
|
|
|
|
|
return virStorageBackendISCSIConnection(portal, initiatoriqn, target, extraargv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageBackendISCSIScanTargets(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
size_t *ntargetsret,
|
|
|
|
char ***targetsret)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* The output of sendtargets is very simple, just two columns,
|
|
|
|
* portal then target name
|
|
|
|
*
|
|
|
|
* 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo0.bf6d84
|
|
|
|
* 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo1.bf6d84
|
|
|
|
* 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo2.bf6d84
|
|
|
|
* 192.168.122.185:3260,1 iqn.2004-04.com:fedora14:iscsi.demo3.bf6d84
|
|
|
|
*/
|
|
|
|
const char *regexes[] = {
|
|
|
|
"^\\s*(\\S+)\\s+(\\S+)\\s*$"
|
2008-06-17 12:47:10 +00:00
|
|
|
};
|
2010-11-12 15:21:48 +00:00
|
|
|
int vars[] = { 2 };
|
|
|
|
struct virStorageBackendISCSITargetList list;
|
2012-07-11 14:56:55 +00:00
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
|
|
|
virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "discovery",
|
|
|
|
"--type", "sendtargets",
|
|
|
|
"--portal", portal,
|
|
|
|
NULL);
|
2010-11-12 15:21:48 +00:00
|
|
|
|
|
|
|
memset(&list, 0, sizeof(list));
|
|
|
|
|
2014-03-18 14:35:01 +00:00
|
|
|
if (virCommandRunRegex(cmd,
|
|
|
|
1,
|
|
|
|
regexes,
|
|
|
|
vars,
|
|
|
|
virStorageBackendISCSIGetTargets,
|
|
|
|
&list, NULL) < 0)
|
2012-07-11 14:56:55 +00:00
|
|
|
goto cleanup;
|
2010-11-12 15:21:48 +00:00
|
|
|
|
2013-05-21 07:21:21 +00:00
|
|
|
for (i = 0; i < list.ntargets; i++) {
|
2010-11-12 15:21:48 +00:00
|
|
|
/* We have to ignore failure, because we can't undo
|
|
|
|
* the results of 'sendtargets', unless we go scrubbing
|
|
|
|
* around in the dirt in /var/lib/iscsi.
|
|
|
|
*/
|
|
|
|
if (virStorageBackendISCSITargetAutologin(portal,
|
|
|
|
initiatoriqn,
|
|
|
|
list.targets[i], false) < 0)
|
|
|
|
VIR_WARN("Unable to disable auto-login on iSCSI target %s: %s",
|
|
|
|
portal, list.targets[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ntargetsret && targetsret) {
|
|
|
|
*ntargetsret = list.ntargets;
|
|
|
|
*targetsret = list.targets;
|
|
|
|
} else {
|
2013-05-21 07:21:21 +00:00
|
|
|
for (i = 0; i < list.ntargets; i++) {
|
2010-11-12 15:21:48 +00:00
|
|
|
VIR_FREE(list.targets[i]);
|
|
|
|
}
|
|
|
|
VIR_FREE(list.targets);
|
|
|
|
}
|
|
|
|
|
2012-07-11 14:56:55 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 15:41:16 +00:00
|
|
|
static char *
|
|
|
|
virStorageBackendISCSIFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|
|
|
const char *srcSpec,
|
2011-07-06 22:51:23 +00:00
|
|
|
unsigned int flags)
|
2010-11-12 15:41:16 +00:00
|
|
|
{
|
|
|
|
virStoragePoolSourcePtr source = NULL;
|
|
|
|
size_t ntargets = 0;
|
|
|
|
char **targets = NULL;
|
|
|
|
char *ret = NULL;
|
Convert 'int i' to 'size_t i' in src/storage/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-11-12 15:41:16 +00:00
|
|
|
virStoragePoolSourceList list = {
|
|
|
|
.type = VIR_STORAGE_POOL_ISCSI,
|
|
|
|
.nsources = 0,
|
|
|
|
.sources = NULL
|
|
|
|
};
|
|
|
|
char *portal = NULL;
|
|
|
|
|
2011-07-06 22:51:23 +00:00
|
|
|
virCheckFlags(0, NULL);
|
|
|
|
|
2012-07-31 08:56:41 +00:00
|
|
|
if (!srcSpec) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
"%s", _("hostname and device path "
|
|
|
|
"must be specified for iscsi sources"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-12 15:41:16 +00:00
|
|
|
if (!(source = virStoragePoolDefParseSourceString(srcSpec,
|
|
|
|
list.type)))
|
|
|
|
return NULL;
|
|
|
|
|
2012-04-25 10:43:09 +00:00
|
|
|
if (source->nhost != 1) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Expected exactly 1 host for the storage pool"));
|
2012-04-25 10:43:09 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-11-12 15:41:16 +00:00
|
|
|
if (!(portal = virStorageBackendISCSIPortal(source)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virStorageBackendISCSIScanTargets(portal,
|
|
|
|
source->initiator.iqn,
|
|
|
|
&ntargets, &targets) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-07-04 10:16:29 +00:00
|
|
|
if (VIR_ALLOC_N(list.sources, ntargets) < 0)
|
2010-11-12 15:41:16 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-05-21 07:21:21 +00:00
|
|
|
for (i = 0; i < ntargets; i++) {
|
2012-04-30 16:36:44 +00:00
|
|
|
if (VIR_ALLOC_N(list.sources[i].devices, 1) < 0 ||
|
2013-07-04 10:16:29 +00:00
|
|
|
VIR_ALLOC_N(list.sources[i].hosts, 1) < 0)
|
2010-11-12 15:41:16 +00:00
|
|
|
goto cleanup;
|
2012-04-30 16:36:44 +00:00
|
|
|
list.sources[i].nhost = 1;
|
|
|
|
list.sources[i].hosts[0] = source->hosts[0];
|
2010-11-12 15:41:16 +00:00
|
|
|
list.sources[i].initiator = source->initiator;
|
|
|
|
list.sources[i].ndevice = 1;
|
|
|
|
list.sources[i].devices[0].path = targets[i];
|
|
|
|
list.nsources++;
|
|
|
|
}
|
|
|
|
|
2013-07-04 10:16:29 +00:00
|
|
|
if (!(ret = virStoragePoolSourceListFormat(&list)))
|
2010-11-12 15:41:16 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (list.sources) {
|
2013-05-21 07:21:21 +00:00
|
|
|
for (i = 0; i < ntargets; i++) {
|
2012-04-30 16:36:44 +00:00
|
|
|
VIR_FREE(list.sources[i].hosts);
|
2010-11-12 15:41:16 +00:00
|
|
|
VIR_FREE(list.sources[i].devices);
|
2012-04-30 16:36:44 +00:00
|
|
|
}
|
2010-11-12 15:41:16 +00:00
|
|
|
VIR_FREE(list.sources);
|
|
|
|
}
|
2013-05-21 07:21:21 +00:00
|
|
|
for (i = 0; i < ntargets; i++)
|
2010-11-12 15:41:16 +00:00
|
|
|
VIR_FREE(targets[i]);
|
|
|
|
VIR_FREE(targets);
|
|
|
|
VIR_FREE(portal);
|
|
|
|
virStoragePoolSourceFree(source);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-11 20:09:20 +00:00
|
|
|
static int
|
|
|
|
virStorageBackendISCSICheckPool(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|
|
|
virStoragePoolObjPtr pool,
|
|
|
|
bool *isActive)
|
|
|
|
{
|
|
|
|
char *session = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
*isActive = false;
|
|
|
|
|
2012-04-25 10:43:09 +00:00
|
|
|
if (pool->def->source.nhost != 1) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Expected exactly 1 host for the storage pool"));
|
2012-04-25 10:43:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->def->source.hosts[0].name == NULL) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("missing source host"));
|
2010-11-11 20:09:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->def->source.ndevice != 1 ||
|
|
|
|
pool->def->source.devices[0].path == NULL) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("missing source device"));
|
2010-11-11 20:09:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-11 11:58:35 +00:00
|
|
|
if ((session = virStorageBackendISCSISession(pool, true)) != NULL) {
|
2010-11-11 20:09:20 +00:00
|
|
|
*isActive = true;
|
|
|
|
VIR_FREE(session);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-15 17:23:45 +00:00
|
|
|
static int
|
|
|
|
virStorageBackendISCSINodeUpdate(const char *portal,
|
|
|
|
const char *target,
|
|
|
|
const char *name,
|
|
|
|
const char *value)
|
|
|
|
{
|
|
|
|
virCommandPtr cmd = NULL;
|
|
|
|
int status;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "node",
|
|
|
|
"--portal", portal,
|
|
|
|
"--target", target,
|
|
|
|
"--op", "update",
|
|
|
|
"--name", name,
|
|
|
|
"--value", value,
|
|
|
|
NULL);
|
|
|
|
|
2014-02-20 00:32:19 +00:00
|
|
|
/* Ignore non-zero status. */
|
2013-07-15 17:23:45 +00:00
|
|
|
if (virCommandRun(cmd, &status) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to update '%s' of node mode for target '%s'"),
|
|
|
|
name, target);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-11-11 20:09:20 +00:00
|
|
|
|
2008-02-20 15:49:25 +00:00
|
|
|
static int
|
2013-07-15 17:23:45 +00:00
|
|
|
virStorageBackendISCSISetAuth(const char *portal,
|
|
|
|
virConnectPtr conn,
|
|
|
|
virStoragePoolDefPtr def)
|
|
|
|
{
|
|
|
|
virSecretPtr secret = NULL;
|
|
|
|
unsigned char *secret_value = NULL;
|
|
|
|
virStoragePoolAuthChap chap;
|
|
|
|
int ret = -1;
|
2013-10-15 08:29:18 +00:00
|
|
|
char uuidStr[VIR_UUID_STRING_BUFLEN];
|
2013-07-15 17:23:45 +00:00
|
|
|
|
|
|
|
if (def->source.authType == VIR_STORAGE_POOL_AUTH_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (def->source.authType != VIR_STORAGE_POOL_AUTH_CHAP) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("iscsi pool only supports 'chap' auth type"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("iscsi 'chap' authentication not supported "
|
|
|
|
"for autostarted pools"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
chap = def->source.auth.chap;
|
|
|
|
if (chap.secret.uuidUsable)
|
|
|
|
secret = virSecretLookupByUUID(conn, chap.secret.uuid);
|
|
|
|
else
|
|
|
|
secret = virSecretLookupByUsage(conn, VIR_SECRET_USAGE_TYPE_ISCSI,
|
|
|
|
chap.secret.usage);
|
|
|
|
|
|
|
|
if (secret) {
|
|
|
|
size_t secret_size;
|
|
|
|
secret_value =
|
|
|
|
conn->secretDriver->secretGetValue(secret, &secret_size, 0,
|
|
|
|
VIR_SECRET_GET_VALUE_INTERNAL_CALL);
|
|
|
|
if (!secret_value) {
|
2013-08-06 11:52:46 +00:00
|
|
|
if (chap.secret.uuidUsable) {
|
2013-10-15 08:29:18 +00:00
|
|
|
virUUIDFormat(chap.secret.uuid, uuidStr);
|
2013-08-06 11:52:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("could not get the value of the secret "
|
|
|
|
"for username %s using uuid '%s'"),
|
2013-10-15 08:29:18 +00:00
|
|
|
chap.username, uuidStr);
|
2013-08-06 11:52:46 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("could not get the value of the secret "
|
|
|
|
"for username %s using usage value '%s'"),
|
|
|
|
chap.username, chap.secret.usage);
|
|
|
|
}
|
2013-07-15 17:23:45 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else {
|
2013-08-06 11:52:46 +00:00
|
|
|
if (chap.secret.uuidUsable) {
|
2013-10-15 08:29:18 +00:00
|
|
|
virUUIDFormat(chap.secret.uuid, uuidStr);
|
2013-08-06 11:52:46 +00:00
|
|
|
virReportError(VIR_ERR_NO_SECRET,
|
|
|
|
_("no secret matches uuid '%s'"),
|
2013-10-15 08:29:18 +00:00
|
|
|
uuidStr);
|
2013-08-06 11:52:46 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_NO_SECRET,
|
|
|
|
_("no secret matches usage value '%s'"),
|
|
|
|
chap.secret.usage);
|
|
|
|
}
|
2013-07-15 17:23:45 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStorageBackendISCSINodeUpdate(portal,
|
|
|
|
def->source.devices[0].path,
|
|
|
|
"node.session.auth.authmethod",
|
|
|
|
"CHAP") < 0 ||
|
|
|
|
virStorageBackendISCSINodeUpdate(portal,
|
|
|
|
def->source.devices[0].path,
|
|
|
|
"node.session.auth.username",
|
|
|
|
chap.username) < 0 ||
|
|
|
|
virStorageBackendISCSINodeUpdate(portal,
|
|
|
|
def->source.devices[0].path,
|
|
|
|
"node.session.auth.password",
|
|
|
|
(const char *)secret_value) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnref(secret);
|
|
|
|
VIR_FREE(secret_value);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageBackendISCSIStartPool(virConnectPtr conn,
|
2008-02-20 15:49:25 +00:00
|
|
|
virStoragePoolObjPtr pool)
|
|
|
|
{
|
|
|
|
char *portal = NULL;
|
2010-11-02 11:40:46 +00:00
|
|
|
char *session = NULL;
|
|
|
|
int ret = -1;
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2012-04-25 10:43:09 +00:00
|
|
|
if (pool->def->source.nhost != 1) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Expected exactly 1 host for the storage pool"));
|
2012-04-25 10:43:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->def->source.hosts[0].name == NULL) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("missing source host"));
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->def->source.ndevice != 1 ||
|
|
|
|
pool->def->source.devices[0].path == NULL) {
|
2012-07-18 11:38:29 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("missing source device"));
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-11 11:58:35 +00:00
|
|
|
if ((session = virStorageBackendISCSISession(pool, true)) == NULL) {
|
2010-11-02 11:40:46 +00:00
|
|
|
if ((portal = virStorageBackendISCSIPortal(&pool->def->source)) == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
/*
|
|
|
|
* iscsiadm doesn't let you login to a target, unless you've
|
|
|
|
* first issued a 'sendtargets' command to the portal :-(
|
|
|
|
*/
|
2010-11-12 15:21:48 +00:00
|
|
|
if (virStorageBackendISCSIScanTargets(portal,
|
|
|
|
pool->def->source.initiator.iqn,
|
|
|
|
NULL, NULL) < 0)
|
2010-11-02 11:40:46 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-07-15 17:23:45 +00:00
|
|
|
if (virStorageBackendISCSISetAuth(portal, conn, pool->def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-03-19 14:52:51 +00:00
|
|
|
if (virStorageBackendISCSIConnectionLogin(portal,
|
|
|
|
pool->def->source.initiator.iqn,
|
|
|
|
pool->def->source.devices[0].path) < 0)
|
2010-11-02 11:40:46 +00:00
|
|
|
goto cleanup;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
2010-11-02 11:40:46 +00:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2013-05-06 12:36:23 +00:00
|
|
|
VIR_FREE(portal);
|
2010-11-02 11:40:46 +00:00
|
|
|
VIR_FREE(session);
|
|
|
|
return ret;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
|
2008-02-20 15:49:25 +00:00
|
|
|
virStoragePoolObjPtr pool)
|
|
|
|
{
|
|
|
|
char *session = NULL;
|
|
|
|
|
|
|
|
pool->def->allocation = pool->def->capacity = pool->def->available = 0;
|
|
|
|
|
2014-03-11 11:58:35 +00:00
|
|
|
if ((session = virStorageBackendISCSISession(pool, false)) == NULL)
|
2008-02-20 15:49:25 +00:00
|
|
|
goto cleanup;
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendISCSIRescanLUNs(pool, session) < 0)
|
2008-02-20 15:49:25 +00:00
|
|
|
goto cleanup;
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendISCSIFindLUs(pool, session) < 0)
|
2008-02-20 15:49:25 +00:00
|
|
|
goto cleanup;
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(session);
|
2008-02-20 15:49:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup:
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(session);
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIStopPool(virConnectPtr conn ATTRIBUTE_UNUSED,
|
2008-02-20 15:49:25 +00:00
|
|
|
virStoragePoolObjPtr pool)
|
|
|
|
{
|
|
|
|
char *portal;
|
2010-11-02 11:40:46 +00:00
|
|
|
int ret = -1;
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2010-11-02 11:40:46 +00:00
|
|
|
if ((portal = virStorageBackendISCSIPortal(&pool->def->source)) == NULL)
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-03-19 14:52:51 +00:00
|
|
|
if (virStorageBackendISCSIConnectionLogout(portal,
|
|
|
|
pool->def->source.initiator.iqn,
|
|
|
|
pool->def->source.devices[0].path) < 0)
|
2010-11-02 11:40:46 +00:00
|
|
|
goto cleanup;
|
|
|
|
ret = 0;
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2010-11-02 11:40:46 +00:00
|
|
|
cleanup:
|
|
|
|
VIR_FREE(portal);
|
|
|
|
return ret;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virStorageBackend virStorageBackendISCSI = {
|
2008-10-16 15:06:03 +00:00
|
|
|
.type = VIR_STORAGE_POOL_ISCSI,
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2010-11-11 20:09:20 +00:00
|
|
|
.checkPool = virStorageBackendISCSICheckPool,
|
2008-10-16 15:06:03 +00:00
|
|
|
.startPool = virStorageBackendISCSIStartPool,
|
|
|
|
.refreshPool = virStorageBackendISCSIRefreshPool,
|
|
|
|
.stopPool = virStorageBackendISCSIStopPool,
|
2010-11-12 15:41:16 +00:00
|
|
|
.findPoolSources = virStorageBackendISCSIFindPoolSources,
|
2008-02-20 15:49:25 +00:00
|
|
|
};
|