2014-03-19 15:03:11 +00:00
|
|
|
/*
|
|
|
|
* viriscsi.c: helper APIs for managing iSCSI
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2014 Red Hat, Inc.
|
|
|
|
* 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
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "viriscsi.h"
|
|
|
|
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "vircommand.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virrandom.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("util.iscsi");
|
|
|
|
|
|
|
|
|
2018-06-29 14:05:50 +00:00
|
|
|
static int
|
|
|
|
virISCSIScanTargetsInternal(const char *portal,
|
|
|
|
const char *ifacename,
|
2018-06-29 14:18:23 +00:00
|
|
|
bool persist,
|
2018-06-29 14:05:50 +00:00
|
|
|
size_t *ntargetsret,
|
|
|
|
char ***targetsret);
|
|
|
|
|
|
|
|
|
2014-03-19 15:03:11 +00:00
|
|
|
struct virISCSISessionData {
|
|
|
|
char *session;
|
|
|
|
const char *devpath;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virISCSIExtractSession(char **const groups,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
struct virISCSISessionData *data = opaque;
|
|
|
|
|
2017-04-05 08:48:10 +00:00
|
|
|
if (!data->session &&
|
2019-10-20 10:55:05 +00:00
|
|
|
STREQ(groups[1], data->devpath)) {
|
|
|
|
data->session = g_strdup(groups[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-19 15:03:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
virISCSIGetSession(const char *devpath,
|
|
|
|
bool probe)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* # iscsiadm --mode session
|
|
|
|
* 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[] = {
|
|
|
|
"^tcp:\\s+\\[(\\S+)\\]\\s+\\S+\\s+(\\S+).*$"
|
|
|
|
};
|
|
|
|
int vars[] = {
|
|
|
|
2,
|
|
|
|
};
|
|
|
|
struct virISCSISessionData cbdata = {
|
|
|
|
.session = NULL,
|
|
|
|
.devpath = devpath,
|
|
|
|
};
|
2016-05-13 17:25:30 +00:00
|
|
|
int exitstatus = 0;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *error = NULL;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM, "--mode",
|
2018-09-19 08:38:14 +00:00
|
|
|
"session", NULL);
|
2016-05-13 17:25:30 +00:00
|
|
|
virCommandSetErrorBuffer(cmd, &error);
|
2014-03-19 15:03:11 +00:00
|
|
|
|
|
|
|
if (virCommandRunRegex(cmd,
|
|
|
|
1,
|
|
|
|
regexes,
|
|
|
|
vars,
|
|
|
|
virISCSIExtractSession,
|
2016-05-13 17:25:30 +00:00
|
|
|
&cbdata, NULL, &exitstatus) < 0)
|
2018-07-28 18:01:17 +00:00
|
|
|
return NULL;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2016-05-13 17:25:30 +00:00
|
|
|
if (cbdata.session == NULL && !probe)
|
2014-03-19 15:03:11 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2016-05-13 17:25:30 +00:00
|
|
|
_("cannot find iscsiadm session: %s"),
|
|
|
|
NULLSTR(error));
|
2014-03-19 15:03:11 +00:00
|
|
|
|
|
|
|
return cbdata.session;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define IQN_FOUND 1
|
|
|
|
#define IQN_MISSING 0
|
|
|
|
#define IQN_ERROR -1
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageBackendIQNFound(const char *initiatoriqn,
|
|
|
|
char **ifacename)
|
|
|
|
{
|
2018-06-29 09:34:23 +00:00
|
|
|
int ret = IQN_ERROR;
|
|
|
|
char *line = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *outbuf = NULL;
|
|
|
|
g_autofree char *iface = NULL;
|
|
|
|
g_autofree char *iqn = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM,
|
2018-09-19 08:38:14 +00:00
|
|
|
"--mode", "iface", NULL);
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-06-29 08:12:58 +00:00
|
|
|
*ifacename = NULL;
|
|
|
|
|
2018-06-29 09:34:23 +00:00
|
|
|
virCommandSetOutputBuffer(cmd, &outbuf);
|
|
|
|
if (virCommandRun(cmd, NULL) < 0)
|
2018-06-29 09:43:54 +00:00
|
|
|
goto cleanup;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-06-29 09:34:23 +00:00
|
|
|
/* Example of data we are dealing with:
|
|
|
|
* default tcp,<empty>,<empty>,<empty>,<empty>
|
|
|
|
* iser iser,<empty>,<empty>,<empty>,<empty>
|
|
|
|
* libvirt-iface-253db048 tcp,<empty>,<empty>,<empty>,iqn.2017-03.com.user:client
|
|
|
|
*/
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-06-29 09:34:23 +00:00
|
|
|
line = outbuf;
|
|
|
|
while (line && *line) {
|
2018-07-25 09:18:39 +00:00
|
|
|
char *current = line;
|
2018-06-29 09:34:23 +00:00
|
|
|
char *newline;
|
2018-07-25 09:18:39 +00:00
|
|
|
char *next;
|
|
|
|
size_t i;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-06-29 09:34:23 +00:00
|
|
|
if (!(newline = strchr(line, '\n')))
|
|
|
|
break;
|
|
|
|
|
|
|
|
*newline = '\0';
|
|
|
|
|
|
|
|
VIR_FREE(iface);
|
|
|
|
VIR_FREE(iqn);
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-07-25 09:18:39 +00:00
|
|
|
/* Find the first space, copy everything up to that point into
|
|
|
|
* iface and move past it to continue processing */
|
|
|
|
if (!(next = strchr(current, ' ')))
|
|
|
|
goto error;
|
|
|
|
|
2019-10-24 17:41:34 +00:00
|
|
|
iface = g_strndup(current, next - current);
|
2018-07-25 09:18:39 +00:00
|
|
|
|
|
|
|
current = next + 1;
|
|
|
|
|
|
|
|
/* There are five comma separated fields after iface and we only
|
|
|
|
* care about the last one, so we need to skip four commas and
|
|
|
|
* copy whatever's left into iqn */
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
if (!(next = strchr(current, ',')))
|
|
|
|
goto error;
|
|
|
|
current = next + 1;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
iqn = g_strdup(current);
|
2018-07-25 09:18:39 +00:00
|
|
|
|
2014-03-19 15:03:11 +00:00
|
|
|
if (STREQ(iqn, initiatoriqn)) {
|
2019-10-16 11:43:52 +00:00
|
|
|
*ifacename = g_steal_pointer(&iface);
|
2018-06-29 08:12:58 +00:00
|
|
|
|
2014-03-19 15:03:11 +00:00
|
|
|
VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-29 09:34:23 +00:00
|
|
|
line = newline + 1;
|
|
|
|
}
|
2018-06-29 08:12:58 +00:00
|
|
|
|
|
|
|
ret = *ifacename ? IQN_FOUND : IQN_MISSING;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-06-29 09:43:54 +00:00
|
|
|
cleanup:
|
2014-11-13 14:28:18 +00:00
|
|
|
if (ret == IQN_MISSING)
|
2014-03-19 15:03:11 +00:00
|
|
|
VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
|
|
|
|
|
|
|
|
return ret;
|
2018-07-25 09:18:39 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("malformed output of %s: %s"),
|
|
|
|
ISCSIADM, line);
|
|
|
|
goto cleanup;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
|
|
|
|
char **ifacename)
|
|
|
|
{
|
2018-08-09 04:12:09 +00:00
|
|
|
int exitstatus = -1;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *iface_name = NULL;
|
|
|
|
g_autofree char *temp_ifacename = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
temp_ifacename = g_strdup_printf("libvirt-iface-%08llx",
|
|
|
|
(unsigned long long)virRandomBits(32));
|
2014-03-19 15:03:11 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'",
|
|
|
|
temp_ifacename, initiatoriqn);
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "iface",
|
|
|
|
"--interface", temp_ifacename,
|
|
|
|
"--op", "new",
|
|
|
|
NULL);
|
|
|
|
/* 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. */
|
|
|
|
if (virCommandRun(cmd, &exitstatus) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to run command '%s' to create new iscsi interface"),
|
|
|
|
ISCSIADM);
|
2018-08-09 04:12:09 +00:00
|
|
|
return -1;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "iface",
|
|
|
|
"--interface", temp_ifacename,
|
|
|
|
"--op", "update",
|
|
|
|
"--name", "iface.initiatorname",
|
|
|
|
"--value",
|
|
|
|
initiatoriqn,
|
|
|
|
NULL);
|
|
|
|
/* 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. */
|
|
|
|
if (virCommandRun(cmd, &exitstatus) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to run command '%s' to update iscsi interface with IQN '%s'"),
|
|
|
|
ISCSIADM, initiatoriqn);
|
2018-08-09 04:12:09 +00:00
|
|
|
return -1;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check again to make sure the interface was created. */
|
2018-08-09 04:12:09 +00:00
|
|
|
if (virStorageBackendIQNFound(initiatoriqn, &iface_name) != IQN_FOUND) {
|
2014-03-19 15:03:11 +00:00
|
|
|
VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
|
|
|
|
"after attempting to create it",
|
|
|
|
&temp_ifacename[0], initiatoriqn);
|
2018-08-09 04:12:09 +00:00
|
|
|
return -1;
|
2014-03-19 15:03:11 +00:00
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
|
2018-08-09 04:12:09 +00:00
|
|
|
iface_name, initiatoriqn);
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*ifacename = g_steal_pointer(&iface_name);
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-08-09 04:12:09 +00:00
|
|
|
return 0;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virISCSIConnection(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
const char *target,
|
|
|
|
const char **extraargv)
|
|
|
|
{
|
|
|
|
const char *const baseargv[] = {
|
|
|
|
ISCSIADM,
|
|
|
|
"--mode", "node",
|
|
|
|
"--portal", portal,
|
|
|
|
"--targetname", target,
|
|
|
|
NULL
|
|
|
|
};
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *ifacename = NULL;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
|
|
|
cmd = virCommandNewArgs(baseargv);
|
|
|
|
virCommandAddArgSet(cmd, extraargv);
|
|
|
|
|
|
|
|
if (initiatoriqn) {
|
|
|
|
switch (virStorageBackendIQNFound(initiatoriqn, &ifacename)) {
|
|
|
|
case IQN_FOUND:
|
|
|
|
VIR_DEBUG("ifacename: '%s'", ifacename);
|
|
|
|
break;
|
|
|
|
case IQN_MISSING:
|
2014-12-09 23:34:32 +00:00
|
|
|
if (virStorageBackendCreateIfaceIQN(initiatoriqn, &ifacename) != 0)
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2014-12-09 23:34:32 +00:00
|
|
|
/*
|
|
|
|
* iscsiadm doesn't let you send commands to the Interface IQN,
|
|
|
|
* unless you've first issued a 'sendtargets' command to the
|
|
|
|
* portal. Without the sendtargets all that is received is a
|
2018-06-29 14:05:50 +00:00
|
|
|
* "iscsiadm: No records found". However, we must ensure that
|
2018-06-29 14:18:23 +00:00
|
|
|
* the command is issued over interface name we invented above
|
|
|
|
* and that targets are made persistent.
|
2014-12-09 23:34:32 +00:00
|
|
|
*/
|
2018-06-29 14:18:23 +00:00
|
|
|
if (virISCSIScanTargetsInternal(portal, ifacename,
|
|
|
|
true, NULL, NULL) < 0)
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2014-12-09 23:34:32 +00:00
|
|
|
|
2014-03-19 15:03:11 +00:00
|
|
|
break;
|
|
|
|
case IQN_ERROR:
|
|
|
|
default:
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
virCommandAddArgList(cmd, "--interface", ifacename, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0)
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-07-28 18:01:17 +00:00
|
|
|
return 0;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virISCSIConnectionLogin(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
const char *target)
|
|
|
|
{
|
|
|
|
const char *extraargv[] = { "--login", NULL };
|
|
|
|
return virISCSIConnection(portal, initiatoriqn, target, extraargv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virISCSIConnectionLogout(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
|
|
|
const char *target)
|
|
|
|
{
|
|
|
|
const char *extraargv[] = { "--logout", NULL };
|
|
|
|
return virISCSIConnection(portal, initiatoriqn, target, extraargv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virISCSIRescanLUNs(const char *session)
|
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM,
|
2018-09-19 08:38:14 +00:00
|
|
|
"--mode", "session",
|
|
|
|
"-r", session,
|
|
|
|
"-R",
|
|
|
|
NULL);
|
2018-07-28 18:01:17 +00:00
|
|
|
return virCommandRun(cmd, NULL);
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct virISCSITargetList {
|
|
|
|
size_t ntargets;
|
|
|
|
char **targets;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virISCSIGetTargets(char **const groups,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct virISCSITargetList *list = data;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *target = NULL;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
target = g_strdup(groups[1]);
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-07-28 18:01:16 +00:00
|
|
|
if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0)
|
2014-03-19 15:03:11 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-29 14:05:50 +00:00
|
|
|
static int
|
|
|
|
virISCSIScanTargetsInternal(const char *portal,
|
|
|
|
const char *ifacename,
|
2018-06-29 14:18:23 +00:00
|
|
|
bool persist,
|
2018-06-29 14:05:50 +00:00
|
|
|
size_t *ntargetsret,
|
|
|
|
char ***targetsret)
|
2014-03-19 15:03:11 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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*$"
|
|
|
|
};
|
|
|
|
int vars[] = { 2 };
|
|
|
|
struct virISCSITargetList list;
|
|
|
|
size_t i;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = virCommandNewArgList(ISCSIADM,
|
2018-09-19 08:38:14 +00:00
|
|
|
"--mode", "discovery",
|
|
|
|
"--type", "sendtargets",
|
|
|
|
"--portal", portal,
|
|
|
|
NULL);
|
2014-03-19 15:03:11 +00:00
|
|
|
|
2018-06-29 14:18:23 +00:00
|
|
|
if (!persist) {
|
|
|
|
virCommandAddArgList(cmd,
|
|
|
|
"--op", "nonpersistent",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2018-06-29 14:05:50 +00:00
|
|
|
if (ifacename) {
|
|
|
|
virCommandAddArgList(cmd,
|
|
|
|
"--interface", ifacename,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2014-03-19 15:03:11 +00:00
|
|
|
memset(&list, 0, sizeof(list));
|
|
|
|
|
|
|
|
if (virCommandRunRegex(cmd,
|
|
|
|
1,
|
|
|
|
regexes,
|
|
|
|
vars,
|
|
|
|
virISCSIGetTargets,
|
2016-05-13 16:36:39 +00:00
|
|
|
&list, NULL, NULL) < 0)
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2014-03-19 15:03:11 +00:00
|
|
|
|
|
|
|
if (ntargetsret && targetsret) {
|
|
|
|
*ntargetsret = list.ntargets;
|
|
|
|
*targetsret = list.targets;
|
|
|
|
} else {
|
2014-11-13 14:28:18 +00:00
|
|
|
for (i = 0; i < list.ntargets; i++)
|
2014-03-19 15:03:11 +00:00
|
|
|
VIR_FREE(list.targets[i]);
|
|
|
|
VIR_FREE(list.targets);
|
|
|
|
}
|
|
|
|
|
2018-07-28 18:01:17 +00:00
|
|
|
return 0;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 14:05:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virISCSIScanTargets:
|
|
|
|
* @portal: iSCSI portal
|
|
|
|
* @initiatoriqn: Initiator IQN
|
2018-06-29 14:18:23 +00:00
|
|
|
* @persists: whether scanned targets should be saved
|
2018-06-29 14:05:50 +00:00
|
|
|
* @ntargets: number of items in @targetsret array
|
|
|
|
* @targets: array of targets
|
|
|
|
*
|
|
|
|
* For given @portal issue sendtargets command. Optionally,
|
|
|
|
* @initiatoriqn can be set to override default configuration.
|
|
|
|
* The targets are stored into @targets array and the size of
|
|
|
|
* the array is stored into @ntargets.
|
|
|
|
*
|
2018-06-29 14:18:23 +00:00
|
|
|
* If @persist is true, then targets returned by iSCSI portal are
|
|
|
|
* made persistent on the host (their config is saved).
|
|
|
|
*
|
2018-06-29 14:05:50 +00:00
|
|
|
* Returns: 0 on success,
|
|
|
|
* -1 otherwise (with error reported)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virISCSIScanTargets(const char *portal,
|
|
|
|
const char *initiatoriqn,
|
2018-06-29 14:18:23 +00:00
|
|
|
bool persist,
|
2018-06-29 14:05:50 +00:00
|
|
|
size_t *ntargets,
|
|
|
|
char ***targets)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *ifacename = NULL;
|
2018-06-29 14:05:50 +00:00
|
|
|
|
|
|
|
if (ntargets)
|
|
|
|
*ntargets = 0;
|
|
|
|
if (targets)
|
|
|
|
*targets = NULL;
|
|
|
|
|
|
|
|
if (initiatoriqn) {
|
|
|
|
switch ((virStorageBackendIQNFound(initiatoriqn, &ifacename))) {
|
|
|
|
case IQN_FOUND:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IQN_MISSING:
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("no iSCSI interface defined for IQN %s"),
|
|
|
|
initiatoriqn);
|
2019-10-15 11:38:21 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2018-06-29 14:05:50 +00:00
|
|
|
case IQN_ERROR:
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 18:01:16 +00:00
|
|
|
return virISCSIScanTargetsInternal(portal, ifacename,
|
2018-09-19 08:38:14 +00:00
|
|
|
persist, ntargets, targets);
|
2018-06-29 14:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-16 12:10:30 +00:00
|
|
|
/*
|
|
|
|
* virISCSINodeNew:
|
|
|
|
* @portal: address for iSCSI target
|
|
|
|
* @target: IQN and specific LUN target
|
|
|
|
*
|
|
|
|
* Usage of nonpersistent discovery in virISCSIScanTargets is useful primarily
|
|
|
|
* only when the target IQN is not known; however, since we have the target IQN
|
|
|
|
* usage of the "--op new" can be done. This avoids problems if "--op delete"
|
|
|
|
* had been used wiping out the static nodes determined by the scanning of
|
|
|
|
* all targets.
|
|
|
|
*
|
|
|
|
* NB: If an iSCSI node record is already created for this portal and
|
|
|
|
* target, subsequent "--op new" commands do not return an error.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 w/ error message on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virISCSINodeNew(const char *portal,
|
|
|
|
const char *target)
|
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2016-07-16 12:10:30 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "node",
|
|
|
|
"--portal", portal,
|
|
|
|
"--targetname", target,
|
|
|
|
"--op", "new",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, &status) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed new node mode for target '%s'"),
|
|
|
|
target);
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2016-07-16 12:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("%s failed new mode for target '%s' with status '%d'"),
|
|
|
|
ISCSIADM, target, status);
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2016-07-16 12:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 18:01:17 +00:00
|
|
|
return 0;
|
2016-07-16 12:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 15:03:11 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virISCSINodeUpdate(const char *portal,
|
|
|
|
const char *target,
|
|
|
|
const char *name,
|
|
|
|
const char *value)
|
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2014-03-19 15:03:11 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(ISCSIADM,
|
|
|
|
"--mode", "node",
|
|
|
|
"--portal", portal,
|
|
|
|
"--target", target,
|
|
|
|
"--op", "update",
|
|
|
|
"--name", name,
|
|
|
|
"--value", value,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Ignore non-zero status. */
|
|
|
|
if (virCommandRun(cmd, &status) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to update '%s' of node mode for target '%s'"),
|
|
|
|
name, target);
|
2018-07-28 18:01:17 +00:00
|
|
|
return -1;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 18:01:17 +00:00
|
|
|
return 0;
|
2014-03-19 15:03:11 +00:00
|
|
|
}
|