storage_backend_iscsi.c 21.8 KB
Newer Older
1 2 3
/*
 * storage_backend_iscsi.c: storage backend for iSCSI handling
 *
4
 * Copyright (C) 2007-2008, 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * 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>
D
David Allan 已提交
35
#include <sys/stat.h>
36

37
#include "virterror_internal.h"
38
#include "storage_backend_scsi.h"
39 40
#include "storage_backend_iscsi.h"
#include "util.h"
41
#include "memory.h"
D
David Allan 已提交
42
#include "logging.h"
E
Eric Blake 已提交
43
#include "virfile.h"
44
#include "command.h"
45

46 47
#define VIR_FROM_THIS VIR_FROM_STORAGE

48
static int
49
virStorageBackendISCSITargetIP(const char *hostname,
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                               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) {
65
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
66 67 68 69 70 71
                              _("host lookup failed %s"),
                              gai_strerror(ret));
        return -1;
    }

    if (result == NULL) {
72
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
73 74 75 76 77 78 79 80
                              _("no IP address for target %s"),
                              hostname);
        return -1;
    }

    if (getnameinfo(result->ai_addr, result->ai_addrlen,
                    ipaddr, ipaddrlen, NULL, 0,
                    NI_NUMERICHOST) < 0) {
81
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
82 83 84 85 86 87 88 89 90 91
                              _("cannot format ip addr for %s"),
                              hostname);
        freeaddrinfo(result);
        return -1;
    }

    freeaddrinfo(result);
    return 0;
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
static char *
virStorageBackendISCSIPortal(virStoragePoolSourcePtr source)
{
    char ipaddr[NI_MAXHOST];
    char *portal;

    if (virStorageBackendISCSITargetIP(source->host.name,
                                       ipaddr, sizeof(ipaddr)) < 0)
        return NULL;

    if (virAsprintf(&portal, "%s:%d,1", ipaddr,
                    source->host.port ?
                    source->host.port : 3260) < 0) {
        virReportOOMError();
        return NULL;
    }

    return portal;
}


113
static int
114
virStorageBackendISCSIExtractSession(virStoragePoolObjPtr pool,
115 116 117 118 119 120 121
                                     char **const groups,
                                     void *data)
{
    char **session = data;

    if (STREQ(groups[1], pool->def->source.devices[0].path)) {
        if ((*session = strdup(groups[0])) == NULL) {
122
            virReportOOMError();
123 124 125 126 127 128 129 130
            return -1;
        }
    }

    return 0;
}

static char *
131
virStorageBackendISCSISession(virStoragePoolObjPtr pool,
132
                              int probe)
133 134
{
    /*
135
     * # iscsiadm --mode session
136 137 138 139 140 141 142 143 144 145 146
     * 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,
    };
147
    const char *const prog[] = {
148
        ISCSIADM, "--mode", "session", NULL
149 150 151
    };
    char *session = NULL;

152 153 154 155
    /* 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.
     */
156
    if (virStorageBackendRunProgRegex(pool,
157 158 159 160 161
                                      prog,
                                      1,
                                      regexes,
                                      vars,
                                      virStorageBackendISCSIExtractSession,
162
                                      &session, NULL) < 0)
163 164
        return NULL;

165 166
    if (session == NULL &&
        !probe) {
167
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
168
                              "%s", _("cannot find session"));
169 170 171 172 173 174
        return NULL;
    }

    return session;
}

D
David Allan 已提交
175 176 177 178

#define LINE_SIZE 4096

static int
179
virStorageBackendIQNFound(const char *initiatoriqn,
D
David Allan 已提交
180 181 182 183 184
                          char **ifacename)
{
    int ret = IQN_MISSING, fd = -1;
    char ebuf[64];
    FILE *fp = NULL;
E
Eric Blake 已提交
185
    char *line = NULL, *newline = NULL, *iqn = NULL, *token = NULL;
186 187
    virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
                                             "--mode", "iface", NULL);
D
David Allan 已提交
188 189 190

    if (VIR_ALLOC_N(line, LINE_SIZE) != 0) {
        ret = IQN_ERROR;
191
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
D
David Allan 已提交
192
                              _("Could not allocate memory for output of '%s'"),
193
                              ISCSIADM);
D
David Allan 已提交
194 195 196 197 198
        goto out;
    }

    memset(line, 0, LINE_SIZE);

199 200
    virCommandSetOutputFD(cmd, &fd);
    if (virCommandRunAsync(cmd, NULL) < 0) {
D
David Allan 已提交
201 202 203 204
        ret = IQN_ERROR;
        goto out;
    }

205
    if ((fp = VIR_FDOPEN(fd, "r")) == NULL) {
206
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
D
David Allan 已提交
207 208
                              _("Failed to open stream for file descriptor "
                                "when reading output from '%s': '%s'"),
209
                              ISCSIADM, virStrerror(errno, ebuf, sizeof ebuf));
D
David Allan 已提交
210 211 212 213 214 215 216 217
        ret = IQN_ERROR;
        goto out;
    }

    while (fgets(line, LINE_SIZE, fp) != NULL) {
        newline = strrchr(line, '\n');
        if (newline == NULL) {
            ret = IQN_ERROR;
218
            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
D
David Allan 已提交
219 220
                                  _("Unexpected line > %d characters "
                                    "when parsing output of '%s'"),
221
                                  LINE_SIZE, ISCSIADM);
D
David Allan 已提交
222 223 224 225 226 227 228 229 230 231
            goto out;
        }
        *newline = '\0';

        iqn = strrchr(line, ',');
        if (iqn == NULL) {
            continue;
        }
        iqn++;

232
        if (STREQ(iqn, initiatoriqn)) {
E
Eric Blake 已提交
233 234 235 236 237 238 239 240 241
            token = strchr(line, ' ');
            if (!token) {
                ret = IQN_ERROR;
                virStorageReportError(VIR_ERR_INTERNAL_ERROR,
                                      _("Missing space when parsing output "
                                        "of '%s'"), ISCSIADM);
                goto out;
            }
            *ifacename = strndup(line, token - line);
D
David Allan 已提交
242 243
            if (*ifacename == NULL) {
                ret = IQN_ERROR;
244
                virReportOOMError();
D
David Allan 已提交
245 246 247 248 249 250 251 252
                goto out;
            }
            VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn);
            ret = IQN_FOUND;
            break;
        }
    }

253 254 255
    if (virCommandWait(cmd, NULL) < 0)
        ret = IQN_ERROR;

D
David Allan 已提交
256 257
out:
    if (ret == IQN_MISSING) {
258
        VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
D
David Allan 已提交
259 260 261
    }

    VIR_FREE(line);
262 263
    VIR_FORCE_FCLOSE(fp);
    VIR_FORCE_CLOSE(fd);
264
    virCommandFree(cmd);
D
David Allan 已提交
265 266 267 268 269 270

    return ret;
}


static int
271
virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
272
                                char **ifacename)
D
David Allan 已提交
273 274 275
{
    int ret = -1, exitstatus = -1;
    char temp_ifacename[32];
276 277 278 279 280 281 282 283 284
    const char *const cmdargv1[] = {
        ISCSIADM, "--mode", "iface", "--interface",
        temp_ifacename, "--op", "new", NULL
    };
    const char *const cmdargv2[] = {
        ISCSIADM, "--mode", "iface", "--interface", temp_ifacename,
        "--op", "update", "--name", "iface.initiatorname", "--value",
        initiatoriqn, NULL
    };
D
David Allan 已提交
285 286

    if (virRandomInitialize(time(NULL) ^ getpid()) == -1) {
287
        virStorageReportError(VIR_ERR_INTERNAL_ERROR, "%s",
D
David Allan 已提交
288 289 290 291 292
                              _("Failed to initialize random generator "
                                "when creating iscsi interface"));
        goto out;
    }

293 294
    snprintf(temp_ifacename, sizeof(temp_ifacename), "libvirt-iface-%08x",
             virRandom(1024 * 1024 * 1024));
D
David Allan 已提交
295 296

    VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'",
297
              &temp_ifacename[0], initiatoriqn);
D
David Allan 已提交
298 299 300 301 302

    /* 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. */
303
    if (virRun(cmdargv1, &exitstatus) < 0) {
304
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
D
David Allan 已提交
305 306 307 308 309 310 311 312
                              _("Failed to run command '%s' to create new iscsi interface"),
                              cmdargv1[0]);
        goto out;
    }

    /* 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. */
313
    if (virRun(cmdargv2, &exitstatus) < 0) {
314
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
D
David Allan 已提交
315
                              _("Failed to run command '%s' to update iscsi interface with IQN '%s'"),
316
                              cmdargv2[0], initiatoriqn);
D
David Allan 已提交
317 318 319 320
        goto out;
    }

    /* Check again to make sure the interface was created. */
321
    if (virStorageBackendIQNFound(initiatoriqn, ifacename) != IQN_FOUND) {
D
David Allan 已提交
322 323
        VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
                  "after attempting to create it",
324
                  &temp_ifacename[0], initiatoriqn);
D
David Allan 已提交
325 326 327
        goto out;
    } else {
        VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
328
                  *ifacename, initiatoriqn);
D
David Allan 已提交
329 330 331 332 333 334 335 336 337 338 339
    }

    ret = 0;

out:
    if (ret != 0)
        VIR_FREE(*ifacename);
    return ret;
}


340

D
David Allan 已提交
341
static int
342 343 344 345
virStorageBackendISCSIConnection(const char *portal,
                                 const char *initiatoriqn,
                                 const char *target,
                                 const char **extraargv)
D
David Allan 已提交
346 347
{
    int ret = -1;
348 349 350 351 352 353 354
    const char *const baseargv[] = {
        ISCSIADM,
        "--mode", "node",
        "--portal", portal,
        "--targetname", target,
        NULL
    };
355
    virCommandPtr cmd;
D
David Allan 已提交
356 357
    char *ifacename = NULL;

358 359
    cmd = virCommandNewArgs(baseargv);
    virCommandAddArgSet(cmd, extraargv);
D
David Allan 已提交
360

361 362 363 364 365 366
    if (initiatoriqn) {
        switch (virStorageBackendIQNFound(initiatoriqn, &ifacename)) {
        case IQN_FOUND:
            VIR_DEBUG("ifacename: '%s'", ifacename);
            break;
        case IQN_MISSING:
367 368
            if (virStorageBackendCreateIfaceIQN(initiatoriqn,
                                                &ifacename) != 0) {
369 370 371 372 373 374 375
                goto cleanup;
            }
            break;
        case IQN_ERROR:
        default:
            goto cleanup;
        }
376
        virCommandAddArgList(cmd, "--interface", ifacename, NULL);
D
David Allan 已提交
377
    }
378

379
    if (virCommandRun(cmd, NULL) < 0)
380
        goto cleanup;
D
David Allan 已提交
381 382 383

    ret = 0;

384
cleanup:
385
    virCommandFree(cmd);
D
David Allan 已提交
386 387 388
    VIR_FREE(ifacename);

    return ret;
389 390
}

391

392
static int
393
virStorageBackendISCSIFindLUs(virStoragePoolObjPtr pool,
394
                              const char *session)
395
{
396
    char *sysfs_path;
397 398
    int retval = 0;
    uint32_t host;
399

400 401 402 403 404
    if (virAsprintf(&sysfs_path,
                    "/sys/class/iscsi_session/session%s/device", session) < 0) {
        virReportOOMError();
        return -1;
    }
405

406
    if (virStorageBackendSCSIGetHostNumber(sysfs_path, &host) < 0) {
407
        virReportSystemError(errno,
408 409
                             _("Failed to get host number for iSCSI session "
                               "with path '%s'"),
410
                             sysfs_path);
411
        retval = -1;
412 413
    }

414
    if (virStorageBackendSCSIFindLUs(pool, host) < 0) {
415
        virReportSystemError(errno,
416 417
                             _("Failed to find LUs on host %u"), host);
        retval = -1;
418 419
    }

420 421
    VIR_FREE(sysfs_path);

422 423
    return retval;
}
424 425

static int
426
virStorageBackendISCSIRescanLUNs(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
427 428
                                 const char *session)
{
429
    const char *const cmdargv[] = {
430 431 432
        ISCSIADM, "--mode", "session", "-r", session, "-R", NULL,
    };

433
    if (virRun(cmdargv, NULL) < 0)
434 435 436 437 438
        return -1;

    return 0;
}

439 440 441 442
struct virStorageBackendISCSITargetList {
    size_t ntargets;
    char **targets;
};
443 444

static int
445 446 447
virStorageBackendISCSIGetTargets(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
                                 char **const groups,
                                 void *data)
448
{
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    struct virStorageBackendISCSITargetList *list = data;
    char *target;

    if (!(target = strdup(groups[1]))) {
        virReportOOMError();
        return -1;
    }

    if (VIR_REALLOC_N(list->targets, list->ntargets + 1) < 0) {
        VIR_FREE(target);
        virReportOOMError();
        return -1;
    }

    list->targets[list->ntargets] = target;
    list->ntargets++;

    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*$"
502
    };
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    int vars[] = { 2 };
    const char *const cmdsendtarget[] = {
        ISCSIADM, "--mode", "discovery", "--type", "sendtargets",
        "--portal", portal, NULL
    };
    struct virStorageBackendISCSITargetList list;
    int i;

    memset(&list, 0, sizeof(list));

    if (virStorageBackendRunProgRegex(NULL, /* No pool for callback */
                                      cmdsendtarget,
                                      1,
                                      regexes,
                                      vars,
                                      virStorageBackendISCSIGetTargets,
519
                                      &list, NULL) < 0) {
520
        return -1;
521
    }
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

    for (i = 0 ; i < list.ntargets ; i++) {
        /* 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 {
        for (i = 0 ; i < list.ntargets ; i++) {
            VIR_FREE(list.targets[i]);
        }
        VIR_FREE(list.targets);
    }

545
    return 0;
546 547 548
}


549 550 551
static char *
virStorageBackendISCSIFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                      const char *srcSpec,
E
Eric Blake 已提交
552
                                      unsigned int flags)
553 554 555 556 557 558 559 560 561 562 563 564 565
{
    virStoragePoolSourcePtr source = NULL;
    size_t ntargets = 0;
    char **targets = NULL;
    char *ret = NULL;
    int i;
    virStoragePoolSourceList list = {
        .type = VIR_STORAGE_POOL_ISCSI,
        .nsources = 0,
        .sources = NULL
    };
    char *portal = NULL;

E
Eric Blake 已提交
566 567
    virCheckFlags(0, NULL);

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    if (!(source = virStoragePoolDefParseSourceString(srcSpec,
                                                      list.type)))
        return NULL;

    if (!(portal = virStorageBackendISCSIPortal(source)))
        goto cleanup;

    if (virStorageBackendISCSIScanTargets(portal,
                                          source->initiator.iqn,
                                          &ntargets, &targets) < 0)
        goto cleanup;

    if (VIR_ALLOC_N(list.sources, ntargets) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    for (i = 0 ; i < ntargets ; i++) {
        if (VIR_ALLOC_N(list.sources[i].devices, 1) < 0) {
            virReportOOMError();
            goto cleanup;
        }
        list.sources[i].host = source->host;
        list.sources[i].initiator = source->initiator;
        list.sources[i].ndevice = 1;
        list.sources[i].devices[0].path = targets[i];
        list.nsources++;
    }

    if (!(ret = virStoragePoolSourceListFormat(&list))) {
        virReportOOMError();
        goto cleanup;
    }

cleanup:
    if (list.sources) {
        for (i = 0 ; i < ntargets ; i++)
            VIR_FREE(list.sources[i].devices);
        VIR_FREE(list.sources);
    }
    for (i = 0 ; i < ntargets ; i++)
        VIR_FREE(targets[i]);
    VIR_FREE(targets);
    VIR_FREE(portal);
    virStoragePoolSourceFree(source);
    return ret;
}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
static int
virStorageBackendISCSICheckPool(virConnectPtr conn ATTRIBUTE_UNUSED,
                                virStoragePoolObjPtr pool,
                                bool *isActive)
{
    char *session = NULL;
    int ret = -1;

    *isActive = false;

    if (pool->def->source.host.name == NULL) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
                              "%s", _("missing source host"));
        return -1;
    }

    if (pool->def->source.ndevice != 1 ||
        pool->def->source.devices[0].path == NULL) {
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
                              "%s", _("missing source device"));
        return -1;
    }

    if ((session = virStorageBackendISCSISession(pool, 1)) != NULL) {
        *isActive = true;
        VIR_FREE(session);
    }
    ret = 0;

    return ret;
}


649
static int
650
virStorageBackendISCSIStartPool(virConnectPtr conn ATTRIBUTE_UNUSED,
651 652 653
                                virStoragePoolObjPtr pool)
{
    char *portal = NULL;
654 655 656
    char *session = NULL;
    int ret = -1;
    const char *loginargv[] = { "--login", NULL };
657 658

    if (pool->def->source.host.name == NULL) {
659
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
660
                              "%s", _("missing source host"));
661 662 663 664 665
        return -1;
    }

    if (pool->def->source.ndevice != 1 ||
        pool->def->source.devices[0].path == NULL) {
666
        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
667
                              "%s", _("missing source device"));
668 669 670
        return -1;
    }

671
    if ((session = virStorageBackendISCSISession(pool, 1)) == NULL) {
672 673 674 675 676 677
        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 :-(
         */
678 679 680
        if (virStorageBackendISCSIScanTargets(portal,
                                              pool->def->source.initiator.iqn,
                                              NULL, NULL) < 0)
681 682 683 684 685 686 687
            goto cleanup;

        if (virStorageBackendISCSIConnection(portal,
                                             pool->def->source.initiator.iqn,
                                             pool->def->source.devices[0].path,
                                             loginargv) < 0)
            goto cleanup;
688
    }
689 690 691 692 693
    ret = 0;

cleanup:
    VIR_FREE(session);
    return ret;
694 695 696
}

static int
697
virStorageBackendISCSIRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
698 699 700 701 702 703
                                  virStoragePoolObjPtr pool)
{
    char *session = NULL;

    pool->def->allocation = pool->def->capacity = pool->def->available = 0;

704
    if ((session = virStorageBackendISCSISession(pool, 0)) == NULL)
705
        goto cleanup;
706
    if (virStorageBackendISCSIRescanLUNs(pool, session) < 0)
707
        goto cleanup;
708
    if (virStorageBackendISCSIFindLUs(pool, session) < 0)
709
        goto cleanup;
710
    VIR_FREE(session);
711 712 713 714

    return 0;

 cleanup:
715
    VIR_FREE(session);
716 717 718 719 720
    return -1;
}


static int
721
virStorageBackendISCSIStopPool(virConnectPtr conn ATTRIBUTE_UNUSED,
722 723
                               virStoragePoolObjPtr pool)
{
724
    const char *logoutargv[] = { "--logout", NULL };
725
    char *portal;
726
    int ret = -1;
727

728
    if ((portal = virStorageBackendISCSIPortal(&pool->def->source)) == NULL)
729 730
        return -1;

731 732 733 734 735 736
    if (virStorageBackendISCSIConnection(portal,
                                         pool->def->source.initiator.iqn,
                                         pool->def->source.devices[0].path,
                                         logoutargv) < 0)
        goto cleanup;
    ret = 0;
737

738 739 740
cleanup:
    VIR_FREE(portal);
    return ret;
741 742 743
}

virStorageBackend virStorageBackendISCSI = {
744
    .type = VIR_STORAGE_POOL_ISCSI,
745

746
    .checkPool = virStorageBackendISCSICheckPool,
747 748 749
    .startPool = virStorageBackendISCSIStartPool,
    .refreshPool = virStorageBackendISCSIRefreshPool,
    .stopPool = virStorageBackendISCSIStopPool,
750
    .findPoolSources = virStorageBackendISCSIFindPoolSources,
751
};