You need to sign in or sign up before continuing.
esx_storage_backend_iscsi.c 21.7 KB
Newer Older
1 2 3
/*
 * esx_storage_backend_iscsi.c: ESX storage backend for iSCSI handling
 *
4
 * Copyright (C) 2014 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
 * Copyright (C) 2012 Ata E Husain Bohra <ata.husain@hotmail.com>
 *
 * 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 <string.h>
#include <stdio.h>
#include <unistd.h>

#include "internal.h"
#include "md5.h"
31
#include "viralloc.h"
32
#include "viruuid.h"
33
#include "storage_conf.h"
34
#include "virstoragefile.h"
35 36 37 38 39
#include "esx_storage_backend_iscsi.h"
#include "esx_private.h"
#include "esx_vi.h"
#include "esx_vi_methods.h"
#include "esx_util.h"
40
#include "virstring.h"
41 42 43 44 45 46 47 48 49 50 51 52

#define VIR_FROM_THIS VIR_FROM_ESX

/*
 * The UUID of a storage pool is the MD5 sum of it's mount path. Therefore,
 * verify that UUID and MD5 sum match in size, because we rely on that.
 */
verify(MD5_DIGEST_SIZE == VIR_UUID_BUFLEN);



static int
53
esxConnectNumOfStoragePools(virConnectPtr conn)
54 55 56
{
    bool success = false;
    int count = 0;
57
    esxPrivate *priv = conn->privateData;
58 59 60 61 62 63 64 65 66 67 68
    esxVI_HostInternetScsiHba *hostInternetScsiHba = NULL;
    esxVI_HostInternetScsiHbaStaticTarget *target;

    if (esxVI_LookupHostInternetScsiHba(priv->primary,
                                        &hostInternetScsiHba) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to obtain iSCSI adapter"));
        goto cleanup;
    }

    /* FIXME: code looks for software iSCSI adapter only */
69
    if (!hostInternetScsiHba) {
70 71 72 73 74 75 76 77 78 79 80 81
        /* iSCSI adapter may not be enabled for this host */
        return 0;
    }

    /*
     * ESX has two kind of targets:
     * 1. staticIscsiTargets
     * 2. dynamicIscsiTargets
     * For each dynamic target if its reachable a static target is added.
     * return iSCSI names for all static targets to avoid duplicate names.
     */
    for (target = hostInternetScsiHba->configuredStaticTarget;
82
         target; target = target->_next) {
83 84 85 86 87
        ++count;
    }

    success = true;

88
 cleanup:
89 90 91 92 93 94 95 96
    esxVI_HostInternetScsiHba_Free(&hostInternetScsiHba);

    return success ? count : -1;
}



static int
97 98
esxConnectListStoragePools(virConnectPtr conn, char **const names,
                           const int maxnames)
99 100 101
{
    bool success = false;
    int count = 0;
102
    esxPrivate *priv = conn->privateData;
103 104
    esxVI_HostInternetScsiHba *hostInternetScsiHba = NULL;
    esxVI_HostInternetScsiHbaStaticTarget *target;
105
    size_t i;
106

107
    if (maxnames == 0)
108 109 110 111 112 113 114 115 116 117
        return 0;

    if (esxVI_LookupHostInternetScsiHba(priv->primary,
                                        &hostInternetScsiHba) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to obtain iSCSI adapter"));
        goto cleanup;
    }

    /* FIXME: code looks for software iSCSI adapter only */
118
    if (!hostInternetScsiHba) {
119 120 121 122 123 124 125 126 127 128 129 130
        /* iSCSI adapter may not be enabled for this host */
        return 0;
    }

    /*
     * ESX has two kind of targets:
     * 1. staticIscsiTargets
     * 2. dynamicIscsiTargets
     * For each dynamic target if its reachable a static target is added.
     * return iSCSI names for all static targets to avoid duplicate names.
     */
    for (target = hostInternetScsiHba->configuredStaticTarget;
131
         target && count < maxnames; target = target->_next) {
132
        if (VIR_STRDUP(names[count], target->iScsiName) < 0)
133 134 135 136 137 138 139
            goto cleanup;

        ++count;
    }

    success = true;

140
 cleanup:
141
    if (! success) {
142
        for (i = 0; i < count; ++i)
143 144 145 146 147 148 149 150 151 152 153
            VIR_FREE(names[i]);
    }

    esxVI_HostInternetScsiHba_Free(&hostInternetScsiHba);

    return success ? count : -1;
}



static virStoragePoolPtr
154 155
esxStoragePoolLookupByName(virConnectPtr conn,
                           const char *name)
156
{
157
    esxPrivate *priv = conn->privateData;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    esxVI_HostInternetScsiHbaStaticTarget *target = NULL;
    /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    unsigned char md5[MD5_DIGEST_SIZE];
    virStoragePoolPtr pool = NULL;

    /*
     * Lookup routine are used by the base driver to determine
     * appropriate backend driver, lookup targetName as optional
     * parameter
     */
    if (esxVI_LookupHostInternetScsiHbaStaticTargetByName
          (priv->primary, name, &target, esxVI_Occurrence_OptionalItem) < 0) {
        goto cleanup;
    }

173
    if (!target) {
174 175 176 177 178 179 180 181 182 183 184 185 186
        /* pool not found, error handling done by the base driver */
        goto cleanup;
    }

    /*
     * HostInternetScsiHbaStaticTarget does not provide a uuid field,
     * but iScsiName (or widely known as IQN) is unique across the multiple
     * hosts, using it to compute key
     */
    md5_buffer(target->iScsiName, strlen(target->iScsiName), md5);

    pool = virGetStoragePool(conn, name, md5, &esxStorageBackendISCSI, NULL);

187
 cleanup:
188 189 190 191 192 193 194 195
    esxVI_HostInternetScsiHbaStaticTarget_Free(&target);

    return pool;
}



static virStoragePoolPtr
196 197
esxStoragePoolLookupByUUID(virConnectPtr conn,
                           const unsigned char *uuid)
198 199
{
    virStoragePoolPtr pool = NULL;
200
    esxPrivate *priv = conn->privateData;
201 202 203 204 205 206 207 208 209 210 211 212 213
    esxVI_HostInternetScsiHba *hostInternetScsiHba = NULL;
    esxVI_HostInternetScsiHbaStaticTarget *target;
    /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    unsigned char md5[MD5_DIGEST_SIZE];

    if (esxVI_LookupHostInternetScsiHba(priv->primary,
                                        &hostInternetScsiHba) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to obtain iSCSI adapter"));
        goto cleanup;
    }

    /* FIXME: code just looks for software iSCSI adapter */
214
    if (!hostInternetScsiHba) {
215 216 217 218 219
        /* iSCSI adapter may not be enabled for this host */
        return NULL;
    }

    for (target = hostInternetScsiHba->configuredStaticTarget;
220
         target; target = target->_next) {
221 222
        md5_buffer(target->iScsiName, strlen(target->iScsiName), md5);

223
        if (memcmp(uuid, md5, VIR_UUID_STRING_BUFLEN) == 0)
224 225 226
            break;
    }

227
    if (!target) {
228 229 230 231 232 233 234
        /* pool not found, error handling done by the base driver */
        goto cleanup;
    }

    pool = virGetStoragePool(conn, target->iScsiName, md5,
                             &esxStorageBackendISCSI, NULL);

235
 cleanup:
236 237 238 239 240 241 242 243
    esxVI_HostInternetScsiHba_Free(&hostInternetScsiHba);

    return pool;
}



static int
244 245
esxStoragePoolRefresh(virStoragePoolPtr pool,
                      unsigned int flags)
246 247
{
    int result = -1;
248
    esxPrivate *priv = pool->conn->privateData;
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    esxVI_HostInternetScsiHba *hostInternetScsiHba = NULL;

    virCheckFlags(0, -1);

    if (esxVI_LookupHostInternetScsiHba(priv->primary,
                                        &hostInternetScsiHba) < 0) {
        goto cleanup;
    }

    /*
     * ESX does not allow rescan on a particular target,
     * rescan all the static targets
     */
    if (esxVI_RescanHba(priv->primary,
                        priv->primary->hostSystem->configManager->storageSystem,
                        hostInternetScsiHba->device) < 0) {
        goto cleanup;
    }

    result = 0;

270
 cleanup:
271 272 273 274 275 276 277 278
    esxVI_HostInternetScsiHba_Free(&hostInternetScsiHba);

    return result;
}



static int
279 280
esxStoragePoolGetInfo(virStoragePoolPtr pool ATTRIBUTE_UNUSED,
                      virStoragePoolInfoPtr info)
281 282 283 284 285 286 287 288 289 290 291
{
    /* These fields are not valid for iSCSI pool */
    info->allocation = info->capacity = info->available = 0;
    info->state = VIR_STORAGE_POOL_RUNNING;

    return 0;
}



static char *
292
esxStoragePoolGetXMLDesc(virStoragePoolPtr pool, unsigned int flags)
293 294
{
    char *xml = NULL;
295
    esxPrivate *priv = pool->conn->privateData;
296 297 298 299 300 301 302 303
    esxVI_HostInternetScsiHba *hostInternetScsiHba = NULL;
    esxVI_HostInternetScsiHbaStaticTarget *target;
    virStoragePoolDef def;

    virCheckFlags(0, NULL);

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

304
    if (esxVI_LookupHostInternetScsiHba(priv->primary, &hostInternetScsiHba))
305 306 307
        goto cleanup;

    for (target = hostInternetScsiHba->configuredStaticTarget;
308
         target; target = target->_next) {
309
        if (STREQ(target->iScsiName, pool->name))
310 311 312
            break;
    }

313
    if (!target) {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        /* pool not found */
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not find storage pool with name '%s'"),
                       pool->name);
        goto cleanup;
    }

    def.name = pool->name;

    memcpy(def.uuid, pool->uuid, VIR_UUID_BUFLEN);

    def.type = VIR_STORAGE_POOL_ISCSI;

    def.source.initiator.iqn = target->iScsiName;

    def.source.nhost = 1;

331
    if (VIR_ALLOC_N(def.source.hosts, def.source.nhost) < 0)
332 333 334 335
        goto cleanup;

    def.source.hosts[0].name = target->address;

336
    if (target->port)
337 338 339 340 341
        def.source.hosts[0].port = target->port->value;

    /* TODO: add CHAP authentication params */
    xml = virStoragePoolDefFormat(&def);

342
 cleanup:
343 344 345 346 347 348 349 350 351
    VIR_FREE(def.source.hosts);
    esxVI_HostInternetScsiHba_Free(&hostInternetScsiHba);

    return xml;
}



static int
352
esxStoragePoolNumOfVolumes(virStoragePoolPtr pool)
353 354
{
    int count = 0;
355
    esxPrivate *priv = pool->conn->privateData;
356 357 358 359 360 361 362 363 364
    esxVI_HostScsiTopologyLun *hostScsiTopologyLunList = NULL;
    esxVI_HostScsiTopologyLun *hostScsiTopologyLun;

    if (esxVI_LookupHostScsiTopologyLunListByTargetName
          (priv->primary, pool->name, &hostScsiTopologyLunList) < 0) {
        return -1;
    }

    for (hostScsiTopologyLun = hostScsiTopologyLunList;
365
         hostScsiTopologyLun;
366 367 368 369 370 371 372 373 374 375 376 377
         hostScsiTopologyLun = hostScsiTopologyLun->_next) {
        ++count;
    }

    esxVI_HostScsiTopologyLun_Free(&hostScsiTopologyLunList);

    return count;
}



static int
378 379
esxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names,
                          int maxnames)
380 381 382
{
    bool success = false;
    int count = 0;
383
    esxPrivate *priv = pool->conn->privateData;
384 385 386 387
    esxVI_HostScsiTopologyLun *hostScsiTopologyLunList = NULL;
    esxVI_HostScsiTopologyLun *hostScsiTopologyLun;
    esxVI_ScsiLun *scsiLunList = NULL;
    esxVI_ScsiLun *scsiLun = NULL;
388
    size_t i;
389 390 391 392 393 394

    if (esxVI_LookupHostScsiTopologyLunListByTargetName
          (priv->primary, pool->name, &hostScsiTopologyLunList) < 0) {
        goto cleanup;
    }

395
    if (!hostScsiTopologyLunList) {
396 397 398 399
        /* iSCSI adapter may not be enabled on ESX host */
        return 0;
    }

400
    if (esxVI_LookupScsiLunList(priv->primary, &scsiLunList) < 0)
401 402
        goto cleanup;

403
    for (scsiLun = scsiLunList; scsiLun && count < maxnames;
404 405
         scsiLun = scsiLun->_next) {
        for (hostScsiTopologyLun = hostScsiTopologyLunList;
406
             hostScsiTopologyLun && count < maxnames;
407 408
             hostScsiTopologyLun = hostScsiTopologyLun->_next) {
            if (STREQ(hostScsiTopologyLun->scsiLun, scsiLun->key)) {
409
                if (VIR_STRDUP(names[count], scsiLun->deviceName) < 0)
410 411 412 413 414 415 416 417 418
                    goto cleanup;

                ++count;
            }
        }
    }

    success = true;

419
 cleanup:
420
    if (! success) {
421
        for (i = 0; i < count; ++i)
422 423 424 425 426 427 428 429 430 431 432 433 434 435
            VIR_FREE(names[i]);

        count = -1;
    }

    esxVI_HostScsiTopologyLun_Free(&hostScsiTopologyLunList);
    esxVI_ScsiLun_Free(&scsiLunList);

    return count;
}



static virStorageVolPtr
436 437
esxStorageVolLookupByName(virStoragePoolPtr pool,
                          const char *name)
438 439
{
    virStorageVolPtr volume = NULL;
440
    esxPrivate *priv = pool->conn->privateData;
441 442 443 444 445 446
    esxVI_ScsiLun *scsiLunList = NULL;
    esxVI_ScsiLun *scsiLun;
    /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    unsigned char md5[MD5_DIGEST_SIZE];
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";

447
    if (esxVI_LookupScsiLunList(priv->primary, &scsiLunList) < 0)
448 449
        goto cleanup;

450
    for (scsiLun = scsiLunList; scsiLun;
451 452 453
         scsiLun = scsiLun->_next) {
        if (STREQ(scsiLun->deviceName, name)) {
            /*
N
Nehal J Wani 已提交
454
             * ScsiLun provides a UUID field that is unique across
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
             * multiple servers. But this field length is ~55 characters
             * compute MD5 hash to transform it to an acceptable
             * libvirt format
             */
            md5_buffer(scsiLun->uuid, strlen(scsiLun->uuid), md5);
            virUUIDFormat(md5, uuid_string);

            /*
             * ScsiLun provides displayName and canonicalName but both are
             * optional and its observed that they can be NULL, using
             * deviceName to create volume.
             */
            volume = virGetStorageVol(pool->conn, pool->name, name, uuid_string,
                                      &esxStorageBackendISCSI, NULL);
            break;
        }
    }

473
 cleanup:
474 475 476 477 478 479 480 481
    esxVI_ScsiLun_Free(&scsiLunList);

    return volume;
}



static virStorageVolPtr
482
esxStorageVolLookupByPath(virConnectPtr conn, const char *path)
483 484
{
    virStorageVolPtr volume = NULL;
485
    esxPrivate *priv = conn->privateData;
486 487 488 489 490 491 492 493
    esxVI_ScsiLun *scsiLunList = NULL;
    esxVI_ScsiLun *scsiLun;
    esxVI_HostScsiDisk *hostScsiDisk = NULL;
    char *poolName = NULL;
    /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    unsigned char md5[MD5_DIGEST_SIZE];
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";

494
    if (esxVI_LookupScsiLunList(priv->primary, &scsiLunList) < 0)
495 496
        goto cleanup;

497
    for (scsiLun = scsiLunList; scsiLun; scsiLun = scsiLun->_next) {
498 499
        hostScsiDisk = esxVI_HostScsiDisk_DynamicCast(scsiLun);

500
        if (hostScsiDisk && STREQ(hostScsiDisk->devicePath, path)) {
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
            /* Found matching device */
            VIR_FREE(poolName);

            if (esxVI_LookupStoragePoolNameByScsiLunKey(priv->primary,
                                                        hostScsiDisk->key,
                                                        &poolName) < 0) {
                goto cleanup;
            }

            md5_buffer(scsiLun->uuid, strlen(scsiLun->uuid), md5);
            virUUIDFormat(md5, uuid_string);

            volume = virGetStorageVol(conn, poolName, path, uuid_string,
                                      &esxStorageBackendISCSI, NULL);
            break;
        }
    }

519
 cleanup:
520 521 522 523 524 525 526 527 528
    esxVI_ScsiLun_Free(&scsiLunList);
    VIR_FREE(poolName);

    return volume;
}



static virStorageVolPtr
529
esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
530 531
{
    virStorageVolPtr volume = NULL;
532
    esxPrivate *priv = conn->privateData;
533 534 535 536 537 538 539 540
    char *poolName = NULL;
    esxVI_ScsiLun *scsiLunList = NULL;
    esxVI_ScsiLun *scsiLun;
    /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    unsigned char md5[MD5_DIGEST_SIZE];
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";

    /* key may be LUN device path */
541
    if (STRPREFIX(key, "/"))
542
        return esxStorageVolLookupByPath(conn, key);
543

544
    if (esxVI_LookupScsiLunList(priv->primary, &scsiLunList) < 0)
545 546
        goto cleanup;

547
    for (scsiLun = scsiLunList; scsiLun;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
         scsiLun = scsiLun->_next) {
        memset(uuid_string, '\0', sizeof(uuid_string));
        memset(md5, '\0', sizeof(md5));

        md5_buffer(scsiLun->uuid, strlen(scsiLun->uuid), md5);
        virUUIDFormat(md5, uuid_string);

        if (STREQ(key, uuid_string)) {
            /* Found matching UUID */
            VIR_FREE(poolName);

            if (esxVI_LookupStoragePoolNameByScsiLunKey(priv->primary,
                                                        scsiLun->key,
                                                        &poolName) < 0) {
                goto cleanup;
            }

            volume = virGetStorageVol(conn, poolName, scsiLun->deviceName,
                                      uuid_string, &esxStorageBackendISCSI,
                                      NULL);
            break;
        }
    }

572
 cleanup:
573 574 575 576 577 578 579 580 581
    esxVI_ScsiLun_Free(&scsiLunList);
    VIR_FREE(poolName);

    return volume;
}



static virStorageVolPtr
582 583 584
esxStorageVolCreateXML(virStoragePoolPtr pool ATTRIBUTE_UNUSED,
                       const char *xmldesc ATTRIBUTE_UNUSED,
                       unsigned int flags)
585 586 587 588 589 590 591 592 593 594 595 596
{
    virCheckFlags(0, NULL);

    virReportError(VIR_ERR_NO_SUPPORT, "%s",
                   _("iSCSI storage pool does not support volume creation"));

    return NULL;
}



static virStorageVolPtr
597 598 599 600
esxStorageVolCreateXMLFrom(virStoragePoolPtr pool ATTRIBUTE_UNUSED,
                           const char *xmldesc ATTRIBUTE_UNUSED,
                           virStorageVolPtr sourceVolume ATTRIBUTE_UNUSED,
                           unsigned int flags)
601 602 603 604 605 606 607 608 609 610 611
{
    virCheckFlags(0, NULL);

    virReportError(VIR_ERR_NO_SUPPORT, "%s",
                   _("iSCSI storage pool does not support volume creation"));

    return NULL;
}



612 613 614 615 616
static int
esxStorageVolGetInfo(virStorageVolPtr volume,
                     virStorageVolInfoPtr info)
{
    int result = -1;
617
    esxPrivate *priv = volume->conn->privateData;
618 619 620 621
    esxVI_ScsiLun *scsiLunList = NULL;
    esxVI_ScsiLun *scsiLun;
    esxVI_HostScsiDisk *hostScsiDisk = NULL;

622
    if (esxVI_LookupScsiLunList(priv->primary, &scsiLunList) < 0)
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 649 650 651 652 653 654 655 656
        goto cleanup;

    for (scsiLun = scsiLunList; scsiLun;
         scsiLun = scsiLun->_next) {
        hostScsiDisk = esxVI_HostScsiDisk_DynamicCast(scsiLun);

        if (hostScsiDisk &&
            STREQ(hostScsiDisk->deviceName, volume->name)) {
            break;
        }
    }

    if (!hostScsiDisk) {
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("Could not find volume with name: %s"),
                       volume->name);
        goto cleanup;
    }

    info->type = VIR_STORAGE_VOL_BLOCK;
    info->capacity = hostScsiDisk->capacity->block->value *
                     hostScsiDisk->capacity->blockSize->value;
    info->allocation = info->capacity;

    result = 0;

 cleanup:
    esxVI_ScsiLun_Free(&scsiLunList);

    return result;
}



657
static char *
658 659
esxStorageVolGetXMLDesc(virStorageVolPtr volume,
                        unsigned int flags)
660 661
{
    char *xml = NULL;
662
    esxPrivate *priv = volume->conn->privateData;
663 664 665 666 667 668 669 670 671 672 673 674 675 676
    virStoragePoolDef pool;
    esxVI_ScsiLun *scsiLunList = NULL;
    esxVI_ScsiLun *scsiLun;
    esxVI_HostScsiDisk *hostScsiDisk = NULL;
    virStorageVolDef def;
    /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    unsigned char md5[MD5_DIGEST_SIZE];
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";

    virCheckFlags(0, NULL);

    memset(&pool, 0, sizeof(pool));
    memset(&def, 0, sizeof(def));

677
    if (esxVI_LookupScsiLunList(priv->primary, &scsiLunList) < 0)
678 679
        goto cleanup;

680
    for (scsiLun = scsiLunList; scsiLun;
681 682 683
         scsiLun = scsiLun->_next) {
        hostScsiDisk = esxVI_HostScsiDisk_DynamicCast(scsiLun);

684
        if (hostScsiDisk &&
685 686 687 688 689
            STREQ(hostScsiDisk->deviceName, volume->name)) {
            break;
        }
    }

690
    if (!hostScsiDisk) {
691 692 693 694 695 696 697 698 699 700 701 702 703
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could find volume with name: %s"), volume->name);
        goto cleanup;
    }

    pool.type = VIR_STORAGE_POOL_ISCSI;

    def.name = volume->name;

    md5_buffer(scsiLun->uuid, strlen(hostScsiDisk->uuid), md5);

    virUUIDFormat(md5, uuid_string);

704
    if (VIR_STRDUP(def.key, uuid_string) < 0)
705 706 707 708 709 710 711
        goto cleanup;

    /* iSCSI LUN exposes a block device */
    def.type = VIR_STORAGE_VOL_BLOCK;

    def.target.path = hostScsiDisk->devicePath;

712
    def.target.capacity = hostScsiDisk->capacity->block->value *
713 714
                   hostScsiDisk->capacity->blockSize->value;

715
    def.target.allocation = def.target.capacity;
716 717 718 719 720 721

    /* iSCSI LUN(s) hosting a datastore will be auto-mounted by ESX host */
    def.target.format = VIR_STORAGE_FILE_RAW;

    xml = virStorageVolDefFormat(&pool, &def);

722
 cleanup:
723 724 725 726 727 728 729 730 731
    esxVI_ScsiLun_Free(&scsiLunList);
    VIR_FREE(def.key);

    return xml;
}



static int
732 733
esxStorageVolDelete(virStorageVolPtr volume ATTRIBUTE_UNUSED,
                    unsigned int flags)
734 735 736 737 738 739 740 741 742 743 744 745
{
    virCheckFlags(0, -1);

    virReportError(VIR_ERR_NO_SUPPORT, "%s",
                   _("iSCSI storage pool does not support volume deletion"));

    return -1;
}



static int
746 747
esxStorageVolWipe(virStorageVolPtr volume ATTRIBUTE_UNUSED,
                  unsigned int flags)
748 749 750 751 752 753 754 755 756 757 758 759 760
{
    virCheckFlags(0, -1);


    virReportError(VIR_ERR_NO_SUPPORT, "%s",
                   _("iSCSI storage pool does not support volume wiping"));

    return -1;
}



static char *
761
esxStorageVolGetPath(virStorageVolPtr volume)
762
{
763
    char *path;
764

765
    ignore_value(VIR_STRDUP(path, volume->name));
766 767 768 769 770 771
    return path;
}



virStorageDriver esxStorageBackendISCSI = {
772 773 774 775 776 777 778 779 780 781 782 783 784 785
    .connectNumOfStoragePools = esxConnectNumOfStoragePools, /* 1.0.1 */
    .connectListStoragePools = esxConnectListStoragePools, /* 1.0.1 */
    .storagePoolLookupByName = esxStoragePoolLookupByName, /* 1.0.1 */
    .storagePoolLookupByUUID = esxStoragePoolLookupByUUID, /* 1.0.1 */
    .storagePoolRefresh = esxStoragePoolRefresh, /* 1.0.1 */
    .storagePoolGetInfo = esxStoragePoolGetInfo, /* 1.0.1 */
    .storagePoolGetXMLDesc = esxStoragePoolGetXMLDesc, /* 1.0.1 */
    .storagePoolNumOfVolumes = esxStoragePoolNumOfVolumes, /* 1.0.1 */
    .storagePoolListVolumes = esxStoragePoolListVolumes, /* 1.0.1 */
    .storageVolLookupByName = esxStorageVolLookupByName, /* 1.0.1 */
    .storageVolLookupByPath = esxStorageVolLookupByPath, /* 1.0.1 */
    .storageVolLookupByKey = esxStorageVolLookupByKey, /* 1.0.1 */
    .storageVolCreateXML = esxStorageVolCreateXML, /* 1.0.1 */
    .storageVolCreateXMLFrom = esxStorageVolCreateXMLFrom, /* 1.0.1 */
786
    .storageVolGetInfo = esxStorageVolGetInfo, /* 1.2.5 */
787 788 789 790
    .storageVolGetXMLDesc = esxStorageVolGetXMLDesc, /* 1.0.1 */
    .storageVolDelete = esxStorageVolDelete, /* 1.0.1 */
    .storageVolWipe = esxStorageVolWipe, /* 1.0.1 */
    .storageVolGetPath = esxStorageVolGetPath, /* 1.0.1 */
791
};