esx_storage_driver.c 14.0 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 28
 *
 */

#include <config.h>

#include "uuid.h"
29
#include "memory.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 35 36

#define VIR_FROM_THIS VIR_FROM_ESX

37
/*
38 39 40 41
 * 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.
42
 */
43 44 45 46
enum {
    VMFS = 0,
    LAST_BACKEND
};
47

48 49 50
static virStorageDriverPtr backends[] = {
    &esxStorageBackendVMFS
};
51 52 53



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

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

    conn->storagePrivateData = conn->privateData;

    return VIR_DRV_OPEN_SUCCESS;
}



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

    return 0;
}



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

90
    if (esxVI_EnsureSession(priv->primary) < 0) {
91 92 93
        return -1;
    }

94 95
    for (i = 0; i < LAST_BACKEND; ++i) {
        tmp = backends[i]->numOfPools(conn);
96

97 98 99
        if (tmp < 0) {
            return -1;
        }
100

101 102
        count += tmp;
    }
103 104 105 106 107 108 109 110 111 112 113 114 115

    return count;
}



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

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

122
    if (esxVI_EnsureSession(priv->primary) < 0) {
123 124 125
        return -1;
    }

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

129 130
        if (tmp < 0) {
            goto cleanup;
131
        }
132 133

        count += tmp;
134 135 136 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
    }

    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;
176 177 178 179
    int i;
    virStoragePoolPtr pool;

    virCheckNonNullArgReturn(name, NULL);
180

181
    if (esxVI_EnsureSession(priv->primary) < 0) {
182 183 184
        return NULL;
    }

185 186
    for (i = 0; i < LAST_BACKEND; ++i) {
        pool = backends[i]->poolLookupByName(conn, name);
187

188 189 190
        if (pool != NULL) {
            return pool;
        }
191 192
    }

193 194
    virReportError(VIR_ERR_NO_STORAGE_POOL,
                   _("Could not find storage pool with name '%s'"), name);
195

196
    return NULL;
197 198 199 200 201 202 203 204
}



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

209
    if (esxVI_EnsureSession(priv->primary) < 0) {
210 211 212
        return NULL;
    }

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

217 218
        if (pool != NULL) {
            return pool;
219 220 221
        }
    }

222 223 224 225
    virUUIDFormat(uuid, uuid_string);
    virReportError(VIR_ERR_NO_STORAGE_POOL,
                   _("Could not find storage pool with uuid '%s'"),
                   uuid_string);
226

227
    return NULL;
228 229 230 231
}



232 233 234 235 236 237 238 239
static virStoragePoolPtr
esxStoragePoolLookupByVolume(virStorageVolPtr volume)
{
    return esxStoragePoolLookupByName(volume->conn, volume->pool);
}



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

246
    virCheckNonNullArgReturn(pool->privateData, -1);
247

248
    if (esxVI_EnsureSession(priv->primary) < 0) {
249 250 251
        return -1;
    }

252
    return backend->poolRefresh(pool, flags);
253 254 255 256 257 258 259 260
}



static int
esxStoragePoolGetInfo(virStoragePoolPtr pool, virStoragePoolInfoPtr info)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
261 262 263
    virStorageDriverPtr backend = pool->privateData;

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

265
    memset(info, 0, sizeof(*info));
266

267
    if (esxVI_EnsureSession(priv->primary) < 0) {
268 269 270
        return -1;
    }

271
    return backend->poolGetInfo(pool, info);
272 273 274 275 276 277 278 279
}



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

282
    virCheckNonNullArgReturn(pool->privateData, NULL);
283

284
    if (esxVI_EnsureSession(priv->primary) < 0) {
285 286 287
        return NULL;
    }

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



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) {
313 314
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot deactivate storage pool autostart"));
315 316 317 318 319 320 321 322
        return -1;
    }

    return 0;
}



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

    virCheckNonNullArgReturn(pool->privateData, -1);
330 331 332 333 334

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

335
    return backend->poolNumOfVolumes(pool);
336 337 338 339 340 341 342 343 344
}



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

347
    virCheckNonNullArgReturn(pool->privateData, -1);
348 349 350 351 352

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

353
    return backend->poolListVolumes(pool, names, maxnames);
354 355 356 357 358 359 360 361
}



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

    virCheckNonNullArgReturn(pool->privateData, NULL);
365 366 367 368 369

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

370
    return backend->volLookupByName(pool, name);
371 372 373 374 375
}



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

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

384 385 386 387 388 389 390 391 392 393 394
    /*
     * FIXME: calling backends blindly may set unwanted error codes
     *
     * VMFS Datastore path follows cannonical format i.e.:
     * [<datastore_name>] <file_path>
     */
    if (STRPREFIX(path, "[")) {
        return backends[VMFS]->volLookupByPath(conn, path);
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Unexpected volume path format: %s"), path);
395

396
        return NULL;
397
    }
398 399 400 401 402 403 404
}



static virStorageVolPtr
esxStorageVolumeLookupByKey(virConnectPtr conn, const char *key)
{
405
    virStorageVolPtr volume;
406
    esxPrivate *priv = conn->storagePrivateData;
407
    int i;
408

409 410 411 412
    if (esxVI_EnsureSession(priv->primary) < 0) {
        return NULL;
    }

413 414
    for (i = 0; i < LAST_BACKEND; ++i) {
        volume = backends[i]->volLookupByKey(conn, key);
415

416 417
        if (volume != NULL) {
            return volume;
418 419 420
        }
    }

421 422 423 424 425
    virReportError(VIR_ERR_NO_STORAGE_VOL,
                   _("Could not find storage volume with key '%s'"),
                   key);

    return NULL;
426 427 428 429
}



430 431 432 433 434
static virStorageVolPtr
esxStorageVolumeCreateXML(virStoragePoolPtr pool, const char *xmldesc,
                          unsigned int flags)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
435
    virStorageDriverPtr backend = pool->privateData;
436

437
    virCheckNonNullArgReturn(pool->privateData, NULL);
438

439
    if (esxVI_EnsureSession(priv->primary) < 0) {
440
        return NULL;
441 442
    }

443
    return backend->volCreateXML(pool, xmldesc, flags);
444 445 446 447
}



448 449 450 451 452
static virStorageVolPtr
esxStorageVolumeCreateXMLFrom(virStoragePoolPtr pool, const char *xmldesc,
                              virStorageVolPtr sourceVolume, unsigned int flags)
{
    esxPrivate *priv = pool->conn->storagePrivateData;
453
    virStorageDriverPtr backend = pool->privateData;
454

455
    virCheckNonNullArgReturn(pool->privateData, NULL);
456

457
    if (esxVI_EnsureSession(priv->primary) < 0) {
458 459 460
        return NULL;
    }

461
    return backend->volCreateXMLFrom(pool, xmldesc, sourceVolume, flags);
462 463 464 465
}



466 467 468 469
static int
esxStorageVolumeDelete(virStorageVolPtr volume, unsigned int flags)
{
    esxPrivate *priv = volume->conn->storagePrivateData;
470
    virStorageDriverPtr backend = volume->privateData;
471

472
    virCheckNonNullArgReturn(volume->privateData, -1);
473 474 475 476 477

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

478
    return backend->volDelete(volume, flags);
479 480 481 482
}



483 484 485 486
static int
esxStorageVolumeWipe(virStorageVolPtr volume, unsigned int flags)
{
    esxPrivate *priv = volume->conn->storagePrivateData;
487
    virStorageDriverPtr backend = volume->privateData;
488

489
    virCheckNonNullArgReturn(volume->privateData, -1);
490 491 492 493 494

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

495
    return backend->volWipe(volume, flags);
496 497 498 499
}



500 501 502 503
static int
esxStorageVolumeGetInfo(virStorageVolPtr volume, virStorageVolInfoPtr info)
{
    esxPrivate *priv = volume->conn->storagePrivateData;
504
    virStorageDriverPtr backend = volume->privateData;
505

506
    virCheckNonNullArgReturn(volume->privateData, -1);
507 508 509 510 511

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

512
    return backend->volGetInfo(volume, info);
513 514 515 516 517
}



static char *
518
esxStorageVolumeGetXMLDesc(virStorageVolPtr volume, unsigned int flags)
519 520
{
    esxPrivate *priv = volume->conn->storagePrivateData;
521
    virStorageDriverPtr backend = volume->privateData;
522

523
    virCheckNonNullArgReturn(volume->privateData, NULL);
524 525 526 527 528

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

529
    return backend->volGetXMLDesc(volume, flags);
530 531 532 533 534 535 536
}



static char *
esxStorageVolumeGetPath(virStorageVolPtr volume)
{
537 538 539 540
    esxPrivate *priv = volume->conn->storagePrivateData;
    virStorageDriverPtr backend = volume->privateData;

    virCheckNonNullArgReturn(volume->privateData, NULL);
541

542
    if (esxVI_EnsureSession(priv->primary) < 0) {
543 544 545
        return NULL;
    }

546
    return backend->volGetPath(volume);
547 548 549 550
}



551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
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;
}


568

569
static virStorageDriver esxStorageDriver = {
570
    .name = "ESX",
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    .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 */
589
    .volLookupByKey = esxStorageVolumeLookupByKey, /* 0.8.4 */
590 591 592 593 594 595 596 597 598
    .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 */
599 600 601 602 603 604 605 606 607
};



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