esx_storage_driver.c 14.3 KB
Newer Older
1 2

/*
3
 * esx_storage_driver.c: storage driver functions for managing VMware ESX
4 5
 *                       host storage
 *
6
 * Copyright (C) 2010-2011 Red Hat, Inc.
7 8
 * Copyright (C) 2010-2012 Matthias Bolte <matthias.bolte@googlemail.com>
 * Copyright (C) 2012 Ata E Husain Bohra <ata.husain@hotmail.com>
9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * License along with this library.  If not, see
O
Osier Yang 已提交
22
 * <http://www.gnu.org/licenses/>.
23 24 25 26 27
 *
 */

#include <config.h>

28
#include "viruuid.h"
29
#include "viralloc.h"
30
#include "storage_conf.h"
31 32
#include "esx_private.h"
#include "esx_storage_driver.h"
33
#include "esx_storage_backend_vmfs.h"
34
#include "esx_storage_backend_iscsi.h"
35 36 37

#define VIR_FROM_THIS VIR_FROM_ESX

38
/*
39 40 41 42
 * ESX storage driver implements a facade pattern;
 * the driver exposes the routines supported by libvirt
 * public interface to manage ESX storage devices. Internally
 * it uses backend drivers to perform the required task.
43
 */
44 45
enum {
    VMFS = 0,
46
    ISCSI,
47 48
    LAST_BACKEND
};
49

50
static virStorageDriverPtr backends[] = {
51 52
    &esxStorageBackendVMFS,
    &esxStorageBackendISCSI
53
};
54 55 56



57 58 59
static virDrvOpenStatus
esxStorageOpen(virConnectPtr conn,
               virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
60
               unsigned int flags)
61
{
E
Eric Blake 已提交
62 63
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

64
    if (conn->driver->no != VIR_DRV_ESX) {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        return VIR_DRV_OPEN_DECLINED;
    }

    conn->storagePrivateData = conn->privateData;

    return VIR_DRV_OPEN_SUCCESS;
}



static int
esxStorageClose(virConnectPtr conn)
{
    conn->storagePrivateData = NULL;

    return 0;
}



85 86 87 88 89
static int
esxNumberOfStoragePools(virConnectPtr conn)
{
    int count = 0;
    esxPrivate *priv = conn->storagePrivateData;
90 91
    int i;
    int tmp;
92

93
    if (esxVI_EnsureSession(priv->primary) < 0) {
94 95 96
        return -1;
    }

97 98
    for (i = 0; i < LAST_BACKEND; ++i) {
        tmp = backends[i]->numOfPools(conn);
99

100 101 102
        if (tmp < 0) {
            return -1;
        }
103

104 105
        count += tmp;
    }
106 107 108 109 110 111 112 113 114 115 116 117 118

    return count;
}



static int
esxListStoragePools(virConnectPtr conn, char **const names, int maxnames)
{
    bool success = false;
    esxPrivate *priv = conn->storagePrivateData;
    int count = 0;
    int i;
119
    int tmp;
120 121 122 123 124

    if (maxnames == 0) {
        return 0;
    }

125
    if (esxVI_EnsureSession(priv->primary) < 0) {
126 127 128
        return -1;
    }

129 130
    for (i = 0; i < LAST_BACKEND; ++i) {
        tmp = backends[i]->listPools(conn, &names[count], maxnames - count);
131

132 133
        if (tmp < 0) {
            goto cleanup;
134
        }
135 136

        count += tmp;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    }

    success = true;

  cleanup:
    if (! success) {
        for (i = 0; i < count; ++i) {
            VIR_FREE(names[i]);
        }

        count = -1;
    }

    return count;
}



static int
esxNumberOfDefinedStoragePools(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* ESX storage pools are always active */
    return 0;
}



static int
esxListDefinedStoragePools(virConnectPtr conn ATTRIBUTE_UNUSED,
                           char **const names ATTRIBUTE_UNUSED,
                           int maxnames ATTRIBUTE_UNUSED)
{
    /* ESX storage pools are always active */
    return 0;
}



static virStoragePoolPtr
esxStoragePoolLookupByName(virConnectPtr conn, const char *name)
{
    esxPrivate *priv = conn->storagePrivateData;
179 180 181 182
    int i;
    virStoragePoolPtr pool;

    virCheckNonNullArgReturn(name, NULL);
183

184
    if (esxVI_EnsureSession(priv->primary) < 0) {
185 186 187
        return NULL;
    }

188 189
    for (i = 0; i < LAST_BACKEND; ++i) {
        pool = backends[i]->poolLookupByName(conn, name);
190

191 192 193
        if (pool != NULL) {
            return pool;
        }
194 195
    }

196 197
    virReportError(VIR_ERR_NO_STORAGE_POOL,
                   _("Could not find storage pool with name '%s'"), name);
198

199
    return NULL;
200 201 202 203 204 205 206 207
}



static virStoragePoolPtr
esxStoragePoolLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    esxPrivate *priv = conn->storagePrivateData;
208 209
    int i;
    virStoragePoolPtr pool;
210 211
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";

212
    if (esxVI_EnsureSession(priv->primary) < 0) {
213 214 215
        return NULL;
    }

216 217 218
    /* invoke backend drive method to search all known pools */
    for (i = 0; i < LAST_BACKEND; ++i) {
        pool = backends[i]->poolLookupByUUID(conn, uuid);
219

220 221
        if (pool != NULL) {
            return pool;
222 223 224
        }
    }

225 226 227 228
    virUUIDFormat(uuid, uuid_string);
    virReportError(VIR_ERR_NO_STORAGE_POOL,
                   _("Could not find storage pool with uuid '%s'"),
                   uuid_string);
229

230
    return NULL;
231 232 233 234
}



235 236 237 238 239 240 241 242
static virStoragePoolPtr
esxStoragePoolLookupByVolume(virStorageVolPtr volume)
{
    return esxStoragePoolLookupByName(volume->conn, volume->pool);
}



243 244 245 246
static int
esxStoragePoolRefresh(virStoragePoolPtr pool, unsigned int flags)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
247
    virStorageDriverPtr backend = pool->privateData;
248

249
    virCheckNonNullArgReturn(pool->privateData, -1);
250

251
    if (esxVI_EnsureSession(priv->primary) < 0) {
252 253 254
        return -1;
    }

255
    return backend->poolRefresh(pool, flags);
256 257 258 259 260 261 262 263
}



static int
esxStoragePoolGetInfo(virStoragePoolPtr pool, virStoragePoolInfoPtr info)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
264 265 266
    virStorageDriverPtr backend = pool->privateData;

    virCheckNonNullArgReturn(pool->privateData, -1);
267

268
    memset(info, 0, sizeof(*info));
269

270
    if (esxVI_EnsureSession(priv->primary) < 0) {
271 272 273
        return -1;
    }

274
    return backend->poolGetInfo(pool, info);
275 276 277 278 279 280 281 282
}



static char *
esxStoragePoolGetXMLDesc(virStoragePoolPtr pool, unsigned int flags)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
283
    virStorageDriverPtr backend = pool->privateData;
284

285
    virCheckNonNullArgReturn(pool->privateData, NULL);
286

287
    if (esxVI_EnsureSession(priv->primary) < 0) {
288 289 290
        return NULL;
    }

291
    return backend->poolGetXMLDesc(pool, flags);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
}



static int
esxStoragePoolGetAutostart(virStoragePoolPtr pool ATTRIBUTE_UNUSED,
                           int *autostart)
{
    /* ESX storage pools are always active */
    *autostart = 1;

    return 0;
}



static int
esxStoragePoolSetAutostart(virStoragePoolPtr pool ATTRIBUTE_UNUSED,
                           int autostart)
{
    /* Just accept autostart activation, but fail on autostart deactivation */
    autostart = (autostart != 0);

    if (! autostart) {
316 317
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot deactivate storage pool autostart"));
318 319 320 321 322 323 324 325
        return -1;
    }

    return 0;
}



326 327 328 329
static int
esxStoragePoolNumberOfStorageVolumes(virStoragePoolPtr pool)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
330 331 332
    virStorageDriverPtr backend = pool->privateData;

    virCheckNonNullArgReturn(pool->privateData, -1);
333 334 335 336 337

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return -1;
    }

338
    return backend->poolNumOfVolumes(pool);
339 340 341 342 343 344 345 346 347
}



static int
esxStoragePoolListStorageVolumes(virStoragePoolPtr pool, char **const names,
                                 int maxnames)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
348
    virStorageDriverPtr backend = pool->privateData;
349

350
    virCheckNonNullArgReturn(pool->privateData, -1);
351 352 353 354 355

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return -1;
    }

356
    return backend->poolListVolumes(pool, names, maxnames);
357 358 359 360 361 362 363 364
}



static virStorageVolPtr
esxStorageVolumeLookupByName(virStoragePoolPtr pool, const char *name)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
365 366 367
    virStorageDriverPtr backend = pool->privateData;

    virCheckNonNullArgReturn(pool->privateData, NULL);
368 369 370 371 372

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return NULL;
    }

373
    return backend->volLookupByName(pool, name);
374 375 376 377 378
}



static virStorageVolPtr
379
esxStorageVolumeLookupByPath(virConnectPtr conn, const char *path)
380 381 382 383 384 385 386
{
    esxPrivate *priv = conn->storagePrivateData;

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return NULL;
    }

387 388 389 390 391
    /*
     * FIXME: calling backends blindly may set unwanted error codes
     *
     * VMFS Datastore path follows cannonical format i.e.:
     * [<datastore_name>] <file_path>
392 393
     *          WHEREAS
     * iSCSI LUNs device path follows normal linux path convention
394 395 396
     */
    if (STRPREFIX(path, "[")) {
        return backends[VMFS]->volLookupByPath(conn, path);
397 398
    } else if (STRPREFIX(path, "/")) {
        return backends[ISCSI]->volLookupByPath(conn, path);
399 400 401
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Unexpected volume path format: %s"), path);
402

403
        return NULL;
404
    }
405 406 407 408 409 410 411
}



static virStorageVolPtr
esxStorageVolumeLookupByKey(virConnectPtr conn, const char *key)
{
412
    virStorageVolPtr volume;
413
    esxPrivate *priv = conn->storagePrivateData;
414
    int i;
415

416 417 418 419
    if (esxVI_EnsureSession(priv->primary) < 0) {
        return NULL;
    }

420 421
    for (i = 0; i < LAST_BACKEND; ++i) {
        volume = backends[i]->volLookupByKey(conn, key);
422

423 424
        if (volume != NULL) {
            return volume;
425 426 427
        }
    }

428 429 430 431 432
    virReportError(VIR_ERR_NO_STORAGE_VOL,
                   _("Could not find storage volume with key '%s'"),
                   key);

    return NULL;
433 434 435 436
}



437 438 439 440 441
static virStorageVolPtr
esxStorageVolumeCreateXML(virStoragePoolPtr pool, const char *xmldesc,
                          unsigned int flags)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
442
    virStorageDriverPtr backend = pool->privateData;
443

444
    virCheckNonNullArgReturn(pool->privateData, NULL);
445

446
    if (esxVI_EnsureSession(priv->primary) < 0) {
447
        return NULL;
448 449
    }

450
    return backend->volCreateXML(pool, xmldesc, flags);
451 452 453 454
}



455 456 457 458 459
static virStorageVolPtr
esxStorageVolumeCreateXMLFrom(virStoragePoolPtr pool, const char *xmldesc,
                              virStorageVolPtr sourceVolume, unsigned int flags)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
460
    virStorageDriverPtr backend = pool->privateData;
461

462
    virCheckNonNullArgReturn(pool->privateData, NULL);
463

464
    if (esxVI_EnsureSession(priv->primary) < 0) {
465 466 467
        return NULL;
    }

468
    return backend->volCreateXMLFrom(pool, xmldesc, sourceVolume, flags);
469 470 471 472
}



473 474 475 476
static int
esxStorageVolumeDelete(virStorageVolPtr volume, unsigned int flags)
{
    esxPrivate *priv = volume->conn->storagePrivateData;
477
    virStorageDriverPtr backend = volume->privateData;
478

479
    virCheckNonNullArgReturn(volume->privateData, -1);
480 481 482 483 484

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return -1;
    }

485
    return backend->volDelete(volume, flags);
486 487 488 489
}



490 491 492 493
static int
esxStorageVolumeWipe(virStorageVolPtr volume, unsigned int flags)
{
    esxPrivate *priv = volume->conn->storagePrivateData;
494
    virStorageDriverPtr backend = volume->privateData;
495

496
    virCheckNonNullArgReturn(volume->privateData, -1);
497 498 499 500 501

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return -1;
    }

502
    return backend->volWipe(volume, flags);
503 504 505 506
}



507 508 509 510
static int
esxStorageVolumeGetInfo(virStorageVolPtr volume, virStorageVolInfoPtr info)
{
    esxPrivate *priv = volume->conn->storagePrivateData;
511
    virStorageDriverPtr backend = volume->privateData;
512

513
    virCheckNonNullArgReturn(volume->privateData, -1);
514 515 516 517 518

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return -1;
    }

519
    return backend->volGetInfo(volume, info);
520 521 522 523 524
}



static char *
525
esxStorageVolumeGetXMLDesc(virStorageVolPtr volume, unsigned int flags)
526 527
{
    esxPrivate *priv = volume->conn->storagePrivateData;
528
    virStorageDriverPtr backend = volume->privateData;
529

530
    virCheckNonNullArgReturn(volume->privateData, NULL);
531 532 533 534 535

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return NULL;
    }

536
    return backend->volGetXMLDesc(volume, flags);
537 538 539 540 541 542 543
}



static char *
esxStorageVolumeGetPath(virStorageVolPtr volume)
{
544 545 546 547
    esxPrivate *priv = volume->conn->storagePrivateData;
    virStorageDriverPtr backend = volume->privateData;

    virCheckNonNullArgReturn(volume->privateData, NULL);
548

549
    if (esxVI_EnsureSession(priv->primary) < 0) {
550 551 552
        return NULL;
    }

553
    return backend->volGetPath(volume);
554 555 556 557
}



558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
static int
esxStoragePoolIsActive(virStoragePoolPtr pool ATTRIBUTE_UNUSED)
{
    /* ESX storage pools are always active */
    return 1;
}



static int
esxStoragePoolIsPersistent(virStoragePoolPtr pool ATTRIBUTE_UNUSED)
{
    /* ESX has no concept of transient pools, so all of them are persistent */
    return 1;
}


575

576
static virStorageDriver esxStorageDriver = {
577
    .name = "ESX",
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    .open = esxStorageOpen, /* 0.7.6 */
    .close = esxStorageClose, /* 0.7.6 */
    .numOfPools = esxNumberOfStoragePools, /* 0.8.2 */
    .listPools = esxListStoragePools, /* 0.8.2 */
    .numOfDefinedPools = esxNumberOfDefinedStoragePools, /* 0.8.2 */
    .listDefinedPools = esxListDefinedStoragePools, /* 0.8.2 */
    .poolLookupByName = esxStoragePoolLookupByName, /* 0.8.2 */
    .poolLookupByUUID = esxStoragePoolLookupByUUID, /* 0.8.2 */
    .poolLookupByVolume = esxStoragePoolLookupByVolume, /* 0.8.4 */
    .poolRefresh = esxStoragePoolRefresh, /* 0.8.2 */
    .poolGetInfo = esxStoragePoolGetInfo, /* 0.8.2 */
    .poolGetXMLDesc = esxStoragePoolGetXMLDesc, /* 0.8.2 */
    .poolGetAutostart = esxStoragePoolGetAutostart, /* 0.8.2 */
    .poolSetAutostart = esxStoragePoolSetAutostart, /* 0.8.2 */
    .poolNumOfVolumes = esxStoragePoolNumberOfStorageVolumes, /* 0.8.4 */
    .poolListVolumes = esxStoragePoolListStorageVolumes, /* 0.8.4 */
    .volLookupByName = esxStorageVolumeLookupByName, /* 0.8.4 */
    .volLookupByPath = esxStorageVolumeLookupByPath, /* 0.8.4 */
596
    .volLookupByKey = esxStorageVolumeLookupByKey, /* 0.8.4 */
597 598 599 600 601 602 603 604 605
    .volCreateXML = esxStorageVolumeCreateXML, /* 0.8.4 */
    .volCreateXMLFrom = esxStorageVolumeCreateXMLFrom, /* 0.8.7 */
    .volDelete = esxStorageVolumeDelete, /* 0.8.7 */
    .volWipe = esxStorageVolumeWipe, /* 0.8.7 */
    .volGetInfo = esxStorageVolumeGetInfo, /* 0.8.4 */
    .volGetXMLDesc = esxStorageVolumeGetXMLDesc, /* 0.8.4 */
    .volGetPath = esxStorageVolumeGetPath, /* 0.8.4 */
    .poolIsActive = esxStoragePoolIsActive, /* 0.8.2 */
    .poolIsPersistent = esxStoragePoolIsPersistent, /* 0.8.2 */
606 607 608 609 610 611 612 613 614
};



int
esxStorageRegister(void)
{
    return virRegisterStorageDriver(&esxStorageDriver);
}