2008-02-20 15:49:25 +00:00
|
|
|
/*
|
|
|
|
* storage_backend_iscsi.c: storage backend for iSCSI handling
|
|
|
|
*
|
2010-02-01 17:25:23 +00:00
|
|
|
* Copyright (C) 2007-2008, 2010 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
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2010-01-21 11:50:52 +00:00
|
|
|
#include <sys/stat.h>
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2008-11-04 22:30:33 +00:00
|
|
|
#include "virterror_internal.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"
|
|
|
|
#include "util.h"
|
2008-06-06 11:09:57 +00:00
|
|
|
#include "memory.h"
|
2010-01-21 11:50:52 +00:00
|
|
|
#include "logging.h"
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2009-01-20 17:13:33 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
2008-02-20 15:49:25 +00:00
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSITargetIP(const char *hostname,
|
2008-02-20 15:49:25 +00:00
|
|
|
char *ipaddr,
|
|
|
|
size_t ipaddrlen)
|
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *result = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_flags = AI_ADDRCONFIG;
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
|
|
|
|
ret = getaddrinfo(hostname, NULL, &hints, &result);
|
|
|
|
if (ret != 0) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2008-02-20 15:49:25 +00:00
|
|
|
_("host lookup failed %s"),
|
|
|
|
gai_strerror(ret));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == NULL) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2008-02-20 15:49:25 +00:00
|
|
|
_("no IP address for target %s"),
|
|
|
|
hostname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getnameinfo(result->ai_addr, result->ai_addrlen,
|
|
|
|
ipaddr, ipaddrlen, NULL, 0,
|
|
|
|
NI_NUMERICHOST) < 0) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2008-02-20 15:49:25 +00:00
|
|
|
_("cannot format ip addr for %s"),
|
|
|
|
hostname);
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIExtractSession(virStoragePoolObjPtr pool,
|
2008-02-20 15:49:25 +00:00
|
|
|
char **const groups,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
char **session = data;
|
|
|
|
|
|
|
|
if (STREQ(groups[1], pool->def->source.devices[0].path)) {
|
|
|
|
if ((*session = strdup(groups[0])) == NULL) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSISession(virStoragePoolObjPtr pool,
|
2009-01-20 22:43:07 +00:00
|
|
|
int 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[] = {
|
|
|
|
"^tcp:\\s+\\[(\\S+)\\]\\s+\\S+\\s+(\\S+)\\s*$"
|
|
|
|
};
|
|
|
|
int vars[] = {
|
|
|
|
2,
|
|
|
|
};
|
2008-06-17 12:49:37 +00:00
|
|
|
const char *const prog[] = {
|
2008-06-17 12:46:38 +00:00
|
|
|
ISCSIADM, "--mode", "session", NULL
|
2008-02-20 15:49:25 +00:00
|
|
|
};
|
|
|
|
char *session = NULL;
|
|
|
|
|
2008-06-17 12:45:24 +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 session got filled in properly.
|
|
|
|
*/
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendRunProgRegex(pool,
|
2008-02-20 15:49:25 +00:00
|
|
|
prog,
|
|
|
|
1,
|
|
|
|
regexes,
|
|
|
|
vars,
|
|
|
|
virStorageBackendISCSIExtractSession,
|
2008-06-17 12:45:24 +00:00
|
|
|
&session,
|
|
|
|
NULL) < 0)
|
2008-02-20 15:49:25 +00:00
|
|
|
return NULL;
|
|
|
|
|
2009-01-20 22:43:07 +00:00
|
|
|
if (session == NULL &&
|
|
|
|
!probe) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("cannot find session"));
|
2008-02-20 15:49:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2010-01-21 11:50:52 +00:00
|
|
|
|
|
|
|
#define LINE_SIZE 4096
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendIQNFound(virStoragePoolObjPtr pool,
|
2010-01-21 11:50:52 +00:00
|
|
|
char **ifacename)
|
|
|
|
{
|
|
|
|
int ret = IQN_MISSING, fd = -1;
|
|
|
|
char ebuf[64];
|
|
|
|
FILE *fp = NULL;
|
|
|
|
pid_t child = 0;
|
|
|
|
char *line = NULL, *newline = NULL, *iqn = NULL, *token = NULL,
|
|
|
|
*saveptr = NULL;
|
|
|
|
const char *const prog[] = {
|
|
|
|
ISCSIADM, "--mode", "iface", NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(line, LINE_SIZE) != 0) {
|
|
|
|
ret = IQN_ERROR;
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Could not allocate memory for output of '%s'"),
|
|
|
|
prog[0]);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(line, 0, LINE_SIZE);
|
|
|
|
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virExec(prog, NULL, NULL, &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Failed to run '%s' when looking for existing interface with IQN '%s'"),
|
|
|
|
prog[0], pool->def->source.initiator.iqn);
|
|
|
|
|
|
|
|
ret = IQN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fp = fdopen(fd, "r")) == NULL) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Failed to open stream for file descriptor "
|
|
|
|
"when reading output from '%s': '%s'"),
|
|
|
|
prog[0], virStrerror(errno, ebuf, sizeof ebuf));
|
|
|
|
ret = IQN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(line, LINE_SIZE, fp) != NULL) {
|
|
|
|
newline = strrchr(line, '\n');
|
|
|
|
if (newline == NULL) {
|
|
|
|
ret = IQN_ERROR;
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Unexpected line > %d characters "
|
|
|
|
"when parsing output of '%s'"),
|
|
|
|
LINE_SIZE, prog[0]);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*newline = '\0';
|
|
|
|
|
|
|
|
iqn = strrchr(line, ',');
|
|
|
|
if (iqn == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
iqn++;
|
|
|
|
|
|
|
|
if (STREQ(iqn, pool->def->source.initiator.iqn)) {
|
|
|
|
token = strtok_r(line, " ", &saveptr);
|
|
|
|
*ifacename = strdup(token);
|
|
|
|
if (*ifacename == NULL) {
|
|
|
|
ret = IQN_ERROR;
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2010-01-21 11:50:52 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn);
|
|
|
|
ret = IQN_FOUND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (fp != NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
} else {
|
|
|
|
if (fd != -1) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendCreateIfaceIQN(virStoragePoolObjPtr pool,
|
|
|
|
char **ifacename)
|
2010-01-21 11:50:52 +00:00
|
|
|
{
|
|
|
|
int ret = -1, exitstatus = -1;
|
|
|
|
char temp_ifacename[32];
|
|
|
|
|
|
|
|
if (virRandomInitialize(time(NULL) ^ getpid()) == -1) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Failed to initialize random generator "
|
|
|
|
"when creating iscsi interface"));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(temp_ifacename, sizeof(temp_ifacename), "libvirt-iface-%08x", virRandom(1024 * 1024 * 1024));
|
|
|
|
|
|
|
|
const char *const cmdargv1[] = {
|
|
|
|
ISCSIADM, "--mode", "iface", "--interface",
|
|
|
|
&temp_ifacename[0], "--op", "new", NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'",
|
|
|
|
&temp_ifacename[0], pool->def->source.initiator.iqn);
|
|
|
|
|
|
|
|
/* 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. */
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virRun(cmdargv1, &exitstatus) < 0) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Failed to run command '%s' to create new iscsi interface"),
|
|
|
|
cmdargv1[0]);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *const cmdargv2[] = {
|
|
|
|
ISCSIADM, "--mode", "iface", "--interface", &temp_ifacename[0],
|
|
|
|
"--op", "update", "--name", "iface.initiatorname", "--value",
|
|
|
|
pool->def->source.initiator.iqn, 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. */
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virRun(cmdargv2, &exitstatus) < 0) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Failed to run command '%s' to update iscsi interface with IQN '%s'"),
|
2010-08-02 19:52:02 +00:00
|
|
|
cmdargv2[0], pool->def->source.initiator.iqn);
|
2010-01-21 11:50:52 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check again to make sure the interface was created. */
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendIQNFound(pool, 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",
|
|
|
|
&temp_ifacename[0], pool->def->source.initiator.iqn);
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
|
|
|
|
*ifacename, pool->def->source.initiator.iqn);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (ret != 0)
|
|
|
|
VIR_FREE(*ifacename);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIConnectionIQN(virStoragePoolObjPtr pool,
|
2010-01-21 11:50:52 +00:00
|
|
|
const char *portal,
|
|
|
|
const char *action)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *ifacename = NULL;
|
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
switch (virStorageBackendIQNFound(pool, &ifacename)) {
|
2010-01-21 11:50:52 +00:00
|
|
|
case IQN_FOUND:
|
|
|
|
VIR_DEBUG("ifacename: '%s'", ifacename);
|
|
|
|
break;
|
|
|
|
case IQN_MISSING:
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendCreateIfaceIQN(pool, &ifacename) != 0) {
|
2010-01-21 11:50:52 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IQN_ERROR:
|
|
|
|
default:
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *const sendtargets[] = {
|
|
|
|
ISCSIADM, "--mode", "discovery", "--type", "sendtargets", "--portal", portal, NULL
|
|
|
|
};
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virRun(sendtargets, NULL) < 0) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Failed to run %s to get target list"),
|
|
|
|
sendtargets[0]);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *const cmdargv[] = {
|
|
|
|
ISCSIADM, "--mode", "node", "--portal", portal,
|
|
|
|
"--targetname", pool->def->source.devices[0].path, "--interface",
|
|
|
|
ifacename, action, NULL
|
|
|
|
};
|
|
|
|
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virRun(cmdargv, NULL) < 0) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-21 11:50:52 +00:00
|
|
|
_("Failed to run command '%s' with action '%s'"),
|
|
|
|
cmdargv[0], action);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
VIR_FREE(ifacename);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 15:49:25 +00:00
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIConnection(virStoragePoolObjPtr pool,
|
2008-02-20 15:49:25 +00:00
|
|
|
const char *portal,
|
|
|
|
const char *action)
|
|
|
|
{
|
2010-01-21 11:50:52 +00:00
|
|
|
int ret = 0;
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2010-01-21 11:50:52 +00:00
|
|
|
if (pool->def->source.initiator.iqn != NULL) {
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
ret = virStorageBackendISCSIConnectionIQN(pool, portal, action);
|
2010-01-21 11:50:52 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const char *const cmdargv[] = {
|
|
|
|
ISCSIADM, "--mode", "node", "--portal", portal,
|
|
|
|
"--targetname", pool->def->source.devices[0].path, action, NULL
|
|
|
|
};
|
|
|
|
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virRun(cmdargv, NULL) < 0) {
|
2010-01-21 11:50:52 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2008-06-17 12:49:37 +00:00
|
|
|
char sysfs_path[PATH_MAX];
|
2009-04-01 16:03:22 +00:00
|
|
|
int retval = 0;
|
|
|
|
uint32_t host;
|
2008-06-17 12:49:37 +00:00
|
|
|
|
|
|
|
snprintf(sysfs_path, PATH_MAX,
|
|
|
|
"/sys/class/iscsi_session/session%s/device", session);
|
|
|
|
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virStorageBackendSCSIGetHostNumber(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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-06-17 12:49:37 +00:00
|
|
|
const char *const cmdargv[] = {
|
2008-02-20 15:49:25 +00:00
|
|
|
ISCSIADM, "--mode", "session", "-r", session, "-R", NULL,
|
|
|
|
};
|
|
|
|
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virRun(cmdargv, NULL) < 0)
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSILogin(virStoragePoolObjPtr pool,
|
2008-02-20 15:49:25 +00:00
|
|
|
const char *portal)
|
|
|
|
{
|
2008-06-17 12:47:10 +00:00
|
|
|
const char *const cmdsendtarget[] = {
|
|
|
|
ISCSIADM, "--mode", "discovery", "--type", "sendtargets",
|
|
|
|
"--portal", portal, NULL
|
|
|
|
};
|
|
|
|
|
2010-02-04 22:41:52 +00:00
|
|
|
if (virRun(cmdsendtarget, NULL) < 0)
|
2008-06-17 12:47:10 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
return virStorageBackendISCSIConnection(pool, portal, "--login");
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSILogout(virStoragePoolObjPtr pool,
|
2008-02-20 15:49:25 +00:00
|
|
|
const char *portal)
|
|
|
|
{
|
2010-02-10 11:42:56 +00:00
|
|
|
return virStorageBackendISCSIConnection(pool, portal, "--logout");
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIPortal(virStoragePoolObjPtr pool)
|
2008-02-20 15:49:25 +00:00
|
|
|
{
|
|
|
|
char ipaddr[NI_MAXHOST];
|
|
|
|
char *portal;
|
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendISCSITargetIP(pool->def->source.host.name,
|
2008-02-20 15:49:25 +00:00
|
|
|
ipaddr, sizeof(ipaddr)) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
if (VIR_ALLOC_N(portal, strlen(ipaddr) + 1 + 4 + 2 + 1) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2008-02-20 15:49:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(portal, ipaddr);
|
|
|
|
strcat(portal, ":3260,1");
|
|
|
|
|
|
|
|
return portal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageBackendISCSIStartPool(virConnectPtr conn ATTRIBUTE_UNUSED,
|
2008-02-20 15:49:25 +00:00
|
|
|
virStoragePoolObjPtr pool)
|
|
|
|
{
|
|
|
|
char *portal = NULL;
|
2009-01-20 22:43:07 +00:00
|
|
|
char *session;
|
2008-02-20 15:49:25 +00:00
|
|
|
|
|
|
|
if (pool->def->source.host.name == NULL) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%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) {
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("missing source device"));
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
if ((session = virStorageBackendISCSISession(pool, 1)) == NULL) {
|
|
|
|
if ((portal = virStorageBackendISCSIPortal(pool)) == NULL)
|
2009-01-20 22:43:07 +00:00
|
|
|
return -1;
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendISCSILogin(pool, portal) < 0) {
|
2009-01-20 22:43:07 +00:00
|
|
|
VIR_FREE(portal);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(portal);
|
2009-01-20 22:43:07 +00:00
|
|
|
} else {
|
|
|
|
VIR_FREE(session);
|
2008-02-20 15:49:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
if ((session = virStorageBackendISCSISession(pool, 0)) == 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-02-10 11:42:56 +00:00
|
|
|
if ((portal = virStorageBackendISCSIPortal(pool)) == NULL)
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-02-10 11:42:56 +00:00
|
|
|
if (virStorageBackendISCSILogout(pool, portal) < 0) {
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(portal);
|
2008-02-20 15:49:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(portal);
|
2008-02-20 15:49:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virStorageBackend virStorageBackendISCSI = {
|
2008-10-16 15:06:03 +00:00
|
|
|
.type = VIR_STORAGE_POOL_ISCSI,
|
2008-02-20 15:49:25 +00:00
|
|
|
|
2008-10-16 15:06:03 +00:00
|
|
|
.startPool = virStorageBackendISCSIStartPool,
|
|
|
|
.refreshPool = virStorageBackendISCSIRefreshPool,
|
|
|
|
.stopPool = virStorageBackendISCSIStopPool,
|
2008-02-20 15:49:25 +00:00
|
|
|
};
|