vmware_driver.c 33.9 KB
Newer Older
1
/*---------------------------------------------------------------------------*/
E
Eric Blake 已提交
2
/*
3
 * Copyright (C) 2011-2015 Red Hat, Inc.
E
Eric Blake 已提交
4
 * Copyright 2010, diateam (www.diateam.net)
5
 * Copyright (C) 2013. Doug Goldstein <cardoe@cardoe.com>
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26
 */
/*---------------------------------------------------------------------------*/

#include <config.h>

#include <fcntl.h>

27
#include "internal.h"
28
#include "virerror.h"
29
#include "datatypes.h"
E
Eric Blake 已提交
30
#include "virfile.h"
31
#include "viralloc.h"
32
#include "viruuid.h"
33
#include "vircommand.h"
34
#include "vmx.h"
35 36
#include "vmware_conf.h"
#include "vmware_driver.h"
37
#include "virstring.h"
38

39 40 41 42 43
/* Various places we may find the "vmrun" binary,
 * without a leading / it will be searched in PATH
 */
static const char * const vmrun_candidates[] = {
    "vmrun",
44 45 46 47
#ifdef __APPLE__
    "/Applications/VMware Fusion.app/Contents/Library/vmrun",
    "/Library/Application Support/VMware Fusion/vmrun",
#endif /* __APPLE__ */
48 49
};

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static void
vmwareDriverLock(struct vmware_driver *driver)
{
    virMutexLock(&driver->lock);
}

static void
vmwareDriverUnlock(struct vmware_driver *driver)
{
    virMutexUnlock(&driver->lock);
}

static void *
vmwareDataAllocFunc(void)
{
    vmwareDomainPtr dom;

    if (VIR_ALLOC(dom) < 0)
        return NULL;

    dom->vmxPath = NULL;
    dom->gui = true;

    return dom;
}

static void
vmwareDataFreeFunc(void *data)
{
    vmwareDomainPtr dom = data;

    VIR_FREE(dom->vmxPath);
    VIR_FREE(dom);
}

85
static int
86
vmwareDomainDefPostParse(virDomainDefPtr def ATTRIBUTE_UNUSED,
87
                         virCapsPtr caps ATTRIBUTE_UNUSED,
88
                         unsigned int parseFlags ATTRIBUTE_UNUSED,
89 90 91 92 93 94 95 96 97
                         void *opaque ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
vmwareDomainDeviceDefPostParse(virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
                               const virDomainDef *def ATTRIBUTE_UNUSED,
                               virCapsPtr caps ATTRIBUTE_UNUSED,
98
                               unsigned int parseFlags ATTRIBUTE_UNUSED,
99 100 101 102 103 104 105 106 107 108
                               void *opaque ATTRIBUTE_UNUSED)
{
    return 0;
}

virDomainDefParserConfig vmwareDomainDefParserConfig = {
    .devicesPostParseCallback = vmwareDomainDeviceDefPostParse,
    .domainPostParseCallback = vmwareDomainDefPostParse,
};

109
static virDomainXMLOptionPtr
110 111 112 113 114
vmwareDomainXMLConfigInit(void)
{
    virDomainXMLPrivateDataCallbacks priv = { .alloc = vmwareDataAllocFunc,
                                              .free = vmwareDataFreeFunc };

115
    return virDomainXMLOptionNew(&vmwareDomainDefParserConfig, &priv, NULL);
116 117
}

118
static virDrvOpenStatus
119 120
vmwareConnectOpen(virConnectPtr conn,
                  virConnectAuthPtr auth ATTRIBUTE_UNUSED,
121
                  virConfPtr conf ATTRIBUTE_UNUSED,
122
                  unsigned int flags)
123 124
{
    struct vmware_driver *driver;
125
    size_t i;
126
    char *tmp;
127

E
Eric Blake 已提交
128 129
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

130 131 132 133 134 135
    if (conn->uri == NULL) {
        /* @TODO accept */
        return VIR_DRV_OPEN_DECLINED;
    } else {
        if (conn->uri->scheme == NULL ||
            (STRNEQ(conn->uri->scheme, "vmwareplayer") &&
136 137
             STRNEQ(conn->uri->scheme, "vmwarews") &&
             STRNEQ(conn->uri->scheme, "vmwarefusion")))
138 139 140 141 142 143 144 145
            return VIR_DRV_OPEN_DECLINED;

        /* If server name is given, its for remote driver */
        if (conn->uri->server != NULL)
            return VIR_DRV_OPEN_DECLINED;

        /* If path isn't /session, then they typoed, so tell them correct path */
        if (conn->uri->path == NULL || STRNEQ(conn->uri->path, "/session")) {
146
            virReportError(VIR_ERR_INTERNAL_ERROR,
147
                           _("unexpected VMware URI path '%s', try vmwareplayer:///session, vmwarews:///session or vmwarefusion:///session"),
148
                           NULLSTR(conn->uri->path));
149 150 151 152 153 154 155
            return VIR_DRV_OPEN_ERROR;
        }
    }

    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */

156
    if (VIR_ALLOC(driver) < 0)
157
        return VIR_DRV_OPEN_ERROR;
158 159 160 161 162 163 164 165 166 167

    /* Find vmrun, which is what this driver uses to communicate to
     * the VMware hypervisor. We look this up first since we use it
     * for auto detection of the backend
     */
    for (i = 0; i < ARRAY_CARDINALITY(vmrun_candidates); i++) {
        driver->vmrun = virFindFileInPath(vmrun_candidates[i]);
        /* If we found one, we can stop looking */
        if (driver->vmrun)
            break;
168 169
    }

170 171 172 173 174
    if (driver->vmrun == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("vmrun utility is missing"));
        goto cleanup;
    }
175 176 177 178

    if (virMutexInit(&driver->lock) < 0)
        goto cleanup;

179 180 181 182 183 184
    if ((tmp = STRSKIP(conn->uri->scheme, "vmware")) == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unable to parse URI "
                       "scheme '%s'"), conn->uri->scheme);
        goto cleanup;
    }

185 186
    /* Match the non-'vmware' part of the scheme as the driver backend */
    driver->type = vmwareDriverTypeFromString(tmp);
187 188 189 190 191 192

    if (driver->type == -1) {
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unable to find valid "
                       "requested VMware backend '%s'"), tmp);
        goto cleanup;
    }
193

194 195 196
    if (vmwareExtractVersion(driver) < 0)
        goto cleanup;

197
    if (!(driver->domains = virDomainObjListNew()))
198 199 200 201 202
        goto cleanup;

    if (!(driver->caps = vmwareCapsInit()))
        goto cleanup;

203
    if (!(driver->xmlopt = vmwareDomainXMLConfigInit()))
204
        goto cleanup;
205 206 207 208 209 210 211 212

    if (vmwareLoadDomains(driver) < 0)
        goto cleanup;

    conn->privateData = driver;

    return VIR_DRV_OPEN_SUCCESS;

213
 cleanup:
214 215 216 217 218
    vmwareFreeDriver(driver);
    return VIR_DRV_OPEN_ERROR;
};

static int
219
vmwareConnectClose(virConnectPtr conn)
220 221 222 223 224 225 226 227 228 229 230
{
    struct vmware_driver *driver = conn->privateData;

    vmwareFreeDriver(driver);

    conn->privateData = NULL;

    return 0;
}

static const char *
231
vmwareConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
232 233 234 235 236
{
    return "VMware";
}

static int
237
vmwareConnectGetVersion(virConnectPtr conn, unsigned long *version)
238 239 240 241 242 243 244 245 246
{
    struct vmware_driver *driver = conn->privateData;

    vmwareDriverLock(driver);
    *version = driver->version;
    vmwareDriverUnlock(driver);
    return 0;
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260
static int
vmwareUpdateVMStatus(struct vmware_driver *driver, virDomainObjPtr vm)
{
    virCommandPtr cmd;
    char *outbuf = NULL;
    char *vmxAbsolutePath = NULL;
    char *parsedVmxPath = NULL;
    char *str;
    char *saveptr = NULL;
    bool found = false;
    int oldState = virDomainObjGetState(vm, NULL);
    int newState;
    int ret = -1;

261
    cmd = virCommandNewArgList(driver->vmrun, "-T",
262
                               vmwareDriverTypeToString(driver->type),
263 264 265 266 267 268 269 270 271
                               "list", NULL);
    virCommandSetOutputBuffer(cmd, &outbuf);
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

    if (virFileResolveAllLinks(((vmwareDomainPtr) vm->privateData)->vmxPath,
                               &vmxAbsolutePath) < 0)
        goto cleanup;

272
    for (str = outbuf; (parsedVmxPath = strtok_r(str, "\n", &saveptr)) != NULL;
273
         str = NULL) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

        if (parsedVmxPath[0] != '/')
            continue;

        if (STREQ(parsedVmxPath, vmxAbsolutePath)) {
            found = true;
            /* If the vmx path is in the output, the domain is running or
             * is paused but we have no way to detect if it is paused or not. */
            if (oldState == VIR_DOMAIN_PAUSED)
                newState = oldState;
            else
                newState = VIR_DOMAIN_RUNNING;
            break;
        }
    }

    if (!found) {
        vm->def->id = -1;
        newState = VIR_DOMAIN_SHUTOFF;
    }

    virDomainObjSetState(vm, newState, 0);

    ret = 0;

299
 cleanup:
300 301 302 303 304 305
    virCommandFree(cmd);
    VIR_FREE(outbuf);
    VIR_FREE(vmxAbsolutePath);
    return ret;
}

306
static int
J
Jiri Denemark 已提交
307 308 309
vmwareStopVM(struct vmware_driver *driver,
             virDomainObjPtr vm,
             virDomainShutoffReason reason)
310 311
{
    const char *cmd[] = {
312
        driver->vmrun, "-T", PROGRAM_SENTINEL, "stop",
E
Eric Blake 已提交
313
        PROGRAM_SENTINEL, "soft", NULL
314 315
    };

316
    vmwareSetSentinal(cmd, vmwareDriverTypeToString(driver->type));
317 318
    vmwareSetSentinal(cmd, ((vmwareDomainPtr) vm->privateData)->vmxPath);

319
    if (virRun(cmd, NULL) < 0)
320 321 322
        return -1;

    vm->def->id = -1;
J
Jiri Denemark 已提交
323
    virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
324 325 326 327 328 329 330 331

    return 0;
}

static int
vmwareStartVM(struct vmware_driver *driver, virDomainObjPtr vm)
{
    const char *cmd[] = {
332
        driver->vmrun, "-T", PROGRAM_SENTINEL, "start",
E
Eric Blake 已提交
333
        PROGRAM_SENTINEL, PROGRAM_SENTINEL, NULL
334 335 336
    };
    const char *vmxPath = ((vmwareDomainPtr) vm->privateData)->vmxPath;

J
Jiri Denemark 已提交
337
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_SHUTOFF) {
338 339
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("domain is not in shutoff state"));
340 341 342
        return -1;
    }

343
    vmwareSetSentinal(cmd, vmwareDriverTypeToString(driver->type));
344 345 346 347 348 349
    vmwareSetSentinal(cmd, vmxPath);
    if (!((vmwareDomainPtr) vm->privateData)->gui)
        vmwareSetSentinal(cmd, NOGUI);
    else
        vmwareSetSentinal(cmd, NULL);

350
    if (virRun(cmd, NULL) < 0)
351 352 353
        return -1;

    if ((vm->def->id = vmwareExtractPid(vmxPath)) < 0) {
J
Jiri Denemark 已提交
354
        vmwareStopVM(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED);
355 356 357
        return -1;
    }

J
Jiri Denemark 已提交
358
    virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_BOOTED);
359 360 361 362 363

    return 0;
}

static virDomainPtr
364
vmwareDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
365 366 367 368 369 370 371 372 373 374
{
    struct vmware_driver *driver = conn->privateData;
    virDomainDefPtr vmdef = NULL;
    virDomainObjPtr vm = NULL;
    virDomainPtr dom = NULL;
    char *vmx = NULL;
    char *directoryName = NULL;
    char *fileName = NULL;
    char *vmxPath = NULL;
    vmwareDomainPtr pDomain = NULL;
375
    virVMXContext ctx;
376
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
377

378 379 380
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
381
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
382

383
    ctx.parseFileName = NULL;
384
    ctx.formatFileName = vmwareCopyVMXFileName;
385 386
    ctx.autodetectSCSIControllerModel = NULL;
    ctx.datacenterPath = NULL;
387 388

    vmwareDriverLock(driver);
389
    if ((vmdef = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
390
                                         parse_flags)) == NULL)
391 392 393
        goto cleanup;

    /* generate vmx file */
394
    vmx = virVMXFormatConfig(&ctx, driver->xmlopt, vmdef, 7);
395 396 397 398 399 400 401 402
    if (vmx == NULL)
        goto cleanup;

    if (vmwareVmxPath(vmdef, &vmxPath) < 0)
        goto cleanup;

    /* create vmx file */
    if (virFileWriteStr(vmxPath, vmx, S_IRUSR|S_IWUSR) < 0) {
403 404
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to write vmx file '%s'"), vmxPath);
405 406 407 408
        goto cleanup;
    }

    /* assign def */
409
    if (!(vm = virDomainObjListAdd(driver->domains,
410
                                   vmdef,
411
                                   driver->xmlopt,
412 413
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
414 415 416
        goto cleanup;

    pDomain = vm->privateData;
417
    if (VIR_STRDUP(pDomain->vmxPath, vmxPath) < 0)
418 419 420 421 422 423 424 425 426 427 428
        goto cleanup;

    vmwareDomainConfigDisplay(pDomain, vmdef);

    vmdef = NULL;
    vm->persistent = 1;

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = -1;

429
 cleanup:
430 431 432 433 434 435
    virDomainDefFree(vmdef);
    VIR_FREE(vmx);
    VIR_FREE(directoryName);
    VIR_FREE(fileName);
    VIR_FREE(vmxPath);
    if (vm)
436
        virObjectUnlock(vm);
437 438 439 440
    vmwareDriverUnlock(driver);
    return dom;
}

441 442 443 444 445 446
static virDomainPtr
vmwareDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return vmwareDomainDefineXMLFlags(conn, xml, 0);
}

447
static int
448 449
vmwareDomainShutdownFlags(virDomainPtr dom,
                          unsigned int flags)
450 451 452 453 454
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

455 456
    virCheckFlags(0, -1);

457 458
    vmwareDriverLock(driver);

459
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
460 461

    if (!vm) {
462 463
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
464 465 466
        goto cleanup;
    }

467 468 469
    if (vmwareUpdateVMStatus(driver, vm) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
470
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_RUNNING) {
471 472
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not in running state"));
473 474 475
        goto cleanup;
    }

J
Jiri Denemark 已提交
476
    if (vmwareStopVM(driver, vm, VIR_DOMAIN_SHUTOFF_SHUTDOWN) < 0)
477 478 479
        goto cleanup;

    if (!vm->persistent) {
480
        virDomainObjListRemove(driver->domains, vm);
481 482 483 484
        vm = NULL;
    }

    ret = 0;
485
 cleanup:
486
    if (vm)
487
        virObjectUnlock(vm);
488 489 490 491
    vmwareDriverUnlock(driver);
    return ret;
}

492 493 494 495 496 497
static int
vmwareDomainShutdown(virDomainPtr dom)
{
    return vmwareDomainShutdownFlags(dom, 0);
}

498 499 500 501 502 503 504 505 506 507 508 509 510
static int
vmwareDomainDestroy(virDomainPtr dom)
{
    return vmwareDomainShutdownFlags(dom, 0);
}

static int
vmwareDomainDestroyFlags(virDomainPtr dom,
                         unsigned int flags)
{
    return vmwareDomainShutdownFlags(dom, flags);
}

511 512 513 514 515 516 517
static int
vmwareDomainSuspend(virDomainPtr dom)
{
    struct vmware_driver *driver = dom->conn->privateData;

    virDomainObjPtr vm;
    const char *cmd[] = {
518
      driver->vmrun, "-T", PROGRAM_SENTINEL, "pause",
E
Eric Blake 已提交
519
      PROGRAM_SENTINEL, NULL
520 521 522
    };
    int ret = -1;

523
    if (driver->type == VMWARE_DRIVER_PLAYER) {
524 525 526
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("vmplayer does not support libvirt suspend/resume"
                         " (vmware pause/unpause) operation "));
527 528 529 530
        return ret;
    }

    vmwareDriverLock(driver);
531
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
532 533 534
    vmwareDriverUnlock(driver);

    if (!vm) {
535 536
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
537 538 539
        goto cleanup;
    }

540
    vmwareSetSentinal(cmd, vmwareDriverTypeToString(driver->type));
541
    vmwareSetSentinal(cmd, ((vmwareDomainPtr) vm->privateData)->vmxPath);
J
Jiri Denemark 已提交
542
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_RUNNING) {
543 544
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not in running state"));
545 546 547 548 549 550
        goto cleanup;
    }

    if (virRun(cmd, NULL) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
551
    virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
552 553
    ret = 0;

554
 cleanup:
555
    if (vm)
556
        virObjectUnlock(vm);
557 558 559 560 561 562 563 564 565 566
    return ret;
}

static int
vmwareDomainResume(virDomainPtr dom)
{
    struct vmware_driver *driver = dom->conn->privateData;

    virDomainObjPtr vm;
    const char *cmd[] = {
567
        driver->vmrun, "-T", PROGRAM_SENTINEL, "unpause", PROGRAM_SENTINEL,
568 569 570 571
        NULL
    };
    int ret = -1;

572
    if (driver->type == VMWARE_DRIVER_PLAYER) {
573
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
574
                       _("vmplayer does not support libvirt suspend/resume "
575
                         "(vmware pause/unpause) operation "));
576 577 578 579
        return ret;
    }

    vmwareDriverLock(driver);
580
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
581 582 583
    vmwareDriverUnlock(driver);

    if (!vm) {
584 585
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
586 587 588
        goto cleanup;
    }

589
    vmwareSetSentinal(cmd, vmwareDriverTypeToString(driver->type));
590
    vmwareSetSentinal(cmd, ((vmwareDomainPtr) vm->privateData)->vmxPath);
J
Jiri Denemark 已提交
591
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_PAUSED) {
592 593
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not in suspend state"));
594 595 596 597 598 599
        goto cleanup;
    }

    if (virRun(cmd, NULL) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
600
    virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_UNPAUSED);
601 602
    ret = 0;

603
 cleanup:
604
    if (vm)
605
        virObjectUnlock(vm);
606 607 608 609
    return ret;
}

static int
E
Eric Blake 已提交
610
vmwareDomainReboot(virDomainPtr dom, unsigned int flags)
611 612 613 614 615
{
    struct vmware_driver *driver = dom->conn->privateData;
    const char * vmxPath = NULL;
    virDomainObjPtr vm;
    const char *cmd[] = {
616
        driver->vmrun, "-T", PROGRAM_SENTINEL,
E
Eric Blake 已提交
617
        "reset", PROGRAM_SENTINEL, "soft", NULL
618 619 620
    };
    int ret = -1;

E
Eric Blake 已提交
621 622
    virCheckFlags(0, -1);

623
    vmwareDriverLock(driver);
624
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
625 626 627
    vmwareDriverUnlock(driver);

    if (!vm) {
628 629
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
630 631 632
        goto cleanup;
    }

633
    vmxPath = ((vmwareDomainPtr) vm->privateData)->vmxPath;
634
    vmwareSetSentinal(cmd, vmwareDriverTypeToString(driver->type));
C
Christophe Fergeau 已提交
635
    vmwareSetSentinal(cmd, vmxPath);
636

637 638
    if (vmwareUpdateVMStatus(driver, vm) < 0)
        goto cleanup;
639

J
Jiri Denemark 已提交
640
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_RUNNING) {
641 642
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not in running state"));
643 644 645 646 647 648 649 650
        goto cleanup;
    }

    if (virRun(cmd, NULL) < 0)
        goto cleanup;

    ret = 0;

651
 cleanup:
652
    if (vm)
653
        virObjectUnlock(vm);
654 655 656 657 658
    return ret;
}

static virDomainPtr
vmwareDomainCreateXML(virConnectPtr conn, const char *xml,
E
Eric Blake 已提交
659
                      unsigned int flags)
660 661 662 663 664 665 666 667
{
    struct vmware_driver *driver = conn->privateData;
    virDomainDefPtr vmdef = NULL;
    virDomainObjPtr vm = NULL;
    virDomainPtr dom = NULL;
    char *vmx = NULL;
    char *vmxPath = NULL;
    vmwareDomainPtr pDomain = NULL;
668
    virVMXContext ctx;
669 670 671
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;

    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);
672

673
    if (flags & VIR_DOMAIN_START_VALIDATE)
674
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
E
Eric Blake 已提交
675

676
    ctx.parseFileName = NULL;
677
    ctx.formatFileName = vmwareCopyVMXFileName;
678 679
    ctx.autodetectSCSIControllerModel = NULL;
    ctx.datacenterPath = NULL;
680 681 682

    vmwareDriverLock(driver);

683
    if ((vmdef = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
684
                                         parse_flags)) == NULL)
685 686 687
        goto cleanup;

    /* generate vmx file */
688
    vmx = virVMXFormatConfig(&ctx, driver->xmlopt, vmdef, 7);
689 690 691 692 693 694 695 696
    if (vmx == NULL)
        goto cleanup;

    if (vmwareVmxPath(vmdef, &vmxPath) < 0)
        goto cleanup;

    /* create vmx file */
    if (virFileWriteStr(vmxPath, vmx, S_IRUSR|S_IWUSR) < 0) {
697 698
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to write vmx file '%s'"), vmxPath);
699 700 701 702
        goto cleanup;
    }

    /* assign def */
703
    if (!(vm = virDomainObjListAdd(driver->domains,
704
                                   vmdef,
705
                                   driver->xmlopt,
706
                                   VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
707 708
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
709 710 711
        goto cleanup;

    pDomain = vm->privateData;
712 713
    if (VIR_STRDUP(pDomain->vmxPath, vmxPath) < 0)
        goto cleanup;
714 715 716 717 718

    vmwareDomainConfigDisplay(pDomain, vmdef);
    vmdef = NULL;

    if (vmwareStartVM(driver, vm) < 0) {
719 720 721 722
        if (!vm->persistent) {
            virDomainObjListRemove(driver->domains, vm);
            vm = NULL;
        }
723 724 725 726 727 728 729
        goto cleanup;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

730
 cleanup:
731 732 733
    virDomainDefFree(vmdef);
    VIR_FREE(vmx);
    VIR_FREE(vmxPath);
734
    if (vm)
735
        virObjectUnlock(vm);
736 737 738 739 740 741
    vmwareDriverUnlock(driver);
    return dom;
}

static int
vmwareDomainCreateWithFlags(virDomainPtr dom,
E
Eric Blake 已提交
742
                            unsigned int flags)
743 744 745 746 747
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

E
Eric Blake 已提交
748 749
    virCheckFlags(0, -1);

750
    vmwareDriverLock(driver);
751
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
752 753 754
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
755 756
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("No domain with matching uuid '%s'"), uuidstr);
757 758 759
        goto cleanup;
    }

760 761 762
    if (vmwareUpdateVMStatus(driver, vm) < 0)
        goto cleanup;

763
    if (virDomainObjIsActive(vm)) {
764 765
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is already running"));
766 767 768 769 770
        goto cleanup;
    }

    ret = vmwareStartVM(driver, vm);

771
 cleanup:
772
    if (vm)
773
        virObjectUnlock(vm);
774 775 776 777 778 779 780 781 782 783 784
    vmwareDriverUnlock(driver);
    return ret;
}

static int
vmwareDomainCreate(virDomainPtr dom)
{
    return vmwareDomainCreateWithFlags(dom, 0);
}

static int
785 786
vmwareDomainUndefineFlags(virDomainPtr dom,
                          unsigned int flags)
787 788 789 790 791
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

792 793
    virCheckFlags(0, -1);

794
    vmwareDriverLock(driver);
795
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
796 797 798 799 800

    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
801 802
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
803 804 805 806
        goto cleanup;
    }

    if (!vm->persistent) {
807 808
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot undefine transient domain"));
809 810 811
        goto cleanup;
    }

812 813 814
    if (vmwareUpdateVMStatus(driver, vm) < 0)
        goto cleanup;

815 816 817
    if (virDomainObjIsActive(vm)) {
        vm->persistent = 0;
    } else {
818
        virDomainObjListRemove(driver->domains, vm);
819 820 821
        vm = NULL;
    }

822 823
    ret = 0;

824
 cleanup:
825
    if (vm)
826
        virObjectUnlock(vm);
827 828 829 830
    vmwareDriverUnlock(driver);
    return ret;
}

831 832 833 834 835 836
static int
vmwareDomainUndefine(virDomainPtr dom)
{
    return vmwareDomainUndefineFlags(dom, 0);
}

837 838 839 840 841 842 843 844
static virDomainPtr
vmwareDomainLookupByID(virConnectPtr conn, int id)
{
    struct vmware_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

    vmwareDriverLock(driver);
845
    vm = virDomainObjListFindByID(driver->domains, id);
846 847 848
    vmwareDriverUnlock(driver);

    if (!vm) {
849
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
850 851 852 853 854 855 856
        goto cleanup;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

857
 cleanup:
858
    if (vm)
859
        virObjectUnlock(vm);
860 861 862 863
    return dom;
}

static char *
864
vmwareDomainGetOSType(virDomainPtr dom)
865 866 867 868 869 870
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;

    vmwareDriverLock(driver);
871
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
872 873 874
    vmwareDriverUnlock(driver);

    if (!vm) {
875
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
876 877 878
        goto cleanup;
    }

879
    ignore_value(VIR_STRDUP(ret, virDomainOSTypeToString(vm->def->os.type)));
880

881
 cleanup:
882
    if (vm)
883
        virObjectUnlock(vm);
884 885 886 887 888 889 890 891 892 893 894 895
    return ret;
}


static virDomainPtr
vmwareDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    struct vmware_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

    vmwareDriverLock(driver);
896
    vm = virDomainObjListFindByUUID(driver->domains, uuid);
897 898 899
    vmwareDriverUnlock(driver);

    if (!vm) {
900
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
901 902 903 904 905 906 907
        goto cleanup;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

908
 cleanup:
909
    if (vm)
910
        virObjectUnlock(vm);
911 912 913 914 915 916 917 918 919 920 921
    return dom;
}

static virDomainPtr
vmwareDomainLookupByName(virConnectPtr conn, const char *name)
{
    struct vmware_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

    vmwareDriverLock(driver);
922
    vm = virDomainObjListFindByName(driver->domains, name);
923 924 925
    vmwareDriverUnlock(driver);

    if (!vm) {
926
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
927 928 929 930 931 932 933
        goto cleanup;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

934
 cleanup:
935
    if (vm)
936
        virObjectUnlock(vm);
937 938 939 940 941 942 943 944 945 946 947
    return dom;
}

static int
vmwareDomainIsActive(virDomainPtr dom)
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    vmwareDriverLock(driver);
948
    obj = virDomainObjListFindByUUID(driver->domains, dom->uuid);
949 950
    vmwareDriverUnlock(driver);
    if (!obj) {
951
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
952 953 954 955
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

956
 cleanup:
957
    if (obj)
958
        virObjectUnlock(obj);
959 960 961 962 963 964 965 966 967 968 969 970
    return ret;
}


static int
vmwareDomainIsPersistent(virDomainPtr dom)
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    vmwareDriverLock(driver);
971
    obj = virDomainObjListFindByUUID(driver->domains, dom->uuid);
972 973
    vmwareDriverUnlock(driver);
    if (!obj) {
974
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
975 976 977 978
        goto cleanup;
    }
    ret = obj->persistent;

979
 cleanup:
980
    if (obj)
981
        virObjectUnlock(obj);
982 983 984 985 986
    return ret;
}


static char *
987
vmwareDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
988 989 990 991 992
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;

993 994
    /* Flags checked by virDomainDefFormat */

995
    vmwareDriverLock(driver);
996
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
997 998 999
    vmwareDriverUnlock(driver);

    if (!vm) {
1000 1001
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1002 1003 1004
        goto cleanup;
    }

1005
    ret = virDomainDefFormat(vm->def, driver->caps,
1006
                             virDomainDefFormatConvertXMLFlags(flags));
1007

1008
 cleanup:
1009
    if (vm)
1010
        virObjectUnlock(vm);
1011 1012 1013
    return ret;
}

1014
static char *
1015 1016 1017
vmwareConnectDomainXMLFromNative(virConnectPtr conn, const char *nativeFormat,
                                 const char *nativeConfig,
                                 unsigned int flags)
1018 1019 1020 1021 1022 1023 1024 1025 1026
{
    struct vmware_driver *driver = conn->privateData;
    virVMXContext ctx;
    virDomainDefPtr def = NULL;
    char *xml = NULL;

    virCheckFlags(0, NULL);

    if (STRNEQ(nativeFormat, "vmware-vmx")) {
1027 1028
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Unsupported config format '%s'"), nativeFormat);
1029 1030 1031 1032
        return NULL;
    }

    ctx.parseFileName = vmwareCopyVMXFileName;
1033 1034 1035
    ctx.formatFileName = NULL;
    ctx.autodetectSCSIControllerModel = NULL;
    ctx.datacenterPath = NULL;
1036

1037
    def = virVMXParseConfig(&ctx, driver->xmlopt, driver->caps, nativeConfig);
1038 1039

    if (def != NULL)
1040 1041
        xml = virDomainDefFormat(def, driver->caps,
                                 VIR_DOMAIN_DEF_FORMAT_INACTIVE);
1042 1043 1044 1045 1046 1047

    virDomainDefFree(def);

    return xml;
}

1048
static int vmwareDomainObjListUpdateDomain(virDomainObjPtr dom, void *data)
1049 1050
{
    struct vmware_driver *driver = data;
1051 1052 1053 1054
    virObjectLock(dom);
    ignore_value(vmwareUpdateVMStatus(driver, dom));
    virObjectUnlock(dom);
    return 0;
1055 1056 1057 1058 1059
}

static void
vmwareDomainObjListUpdateAll(virDomainObjListPtr doms, struct vmware_driver *driver)
{
1060
    virDomainObjListForEach(doms, vmwareDomainObjListUpdateDomain, driver);
1061 1062
}

1063
static int
1064
vmwareConnectNumOfDefinedDomains(virConnectPtr conn)
1065 1066 1067 1068 1069
{
    struct vmware_driver *driver = conn->privateData;
    int n;

    vmwareDriverLock(driver);
1070
    vmwareDomainObjListUpdateAll(driver->domains, driver);
1071
    n = virDomainObjListNumOfDomains(driver->domains, false, NULL, NULL);
1072 1073 1074 1075 1076 1077
    vmwareDriverUnlock(driver);

    return n;
}

static int
1078
vmwareConnectNumOfDomains(virConnectPtr conn)
1079 1080 1081 1082 1083
{
    struct vmware_driver *driver = conn->privateData;
    int n;

    vmwareDriverLock(driver);
1084
    vmwareDomainObjListUpdateAll(driver->domains, driver);
1085
    n = virDomainObjListNumOfDomains(driver->domains, true, NULL, NULL);
1086 1087 1088 1089 1090 1091 1092
    vmwareDriverUnlock(driver);

    return n;
}


static int
1093
vmwareConnectListDomains(virConnectPtr conn, int *ids, int nids)
1094 1095 1096 1097 1098
{
    struct vmware_driver *driver = conn->privateData;
    int n;

    vmwareDriverLock(driver);
1099
    vmwareDomainObjListUpdateAll(driver->domains, driver);
1100
    n = virDomainObjListGetActiveIDs(driver->domains, ids, nids, NULL, NULL);
1101 1102 1103 1104 1105 1106
    vmwareDriverUnlock(driver);

    return n;
}

static int
1107 1108
vmwareConnectListDefinedDomains(virConnectPtr conn,
                                char **const names, int nnames)
1109 1110 1111 1112 1113
{
    struct vmware_driver *driver = conn->privateData;
    int n;

    vmwareDriverLock(driver);
1114
    vmwareDomainObjListUpdateAll(driver->domains, driver);
1115 1116
    n = virDomainObjListGetInactiveNames(driver->domains, names, nnames,
                                         NULL, NULL);
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    vmwareDriverUnlock(driver);
    return n;
}

static int
vmwareDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    vmwareDriverLock(driver);
1129
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
1130 1131 1132
    vmwareDriverUnlock(driver);

    if (!vm) {
1133 1134
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1135 1136 1137
        goto cleanup;
    }

1138 1139 1140
    if (vmwareUpdateVMStatus(driver, vm) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
1141
    info->state = virDomainObjGetState(vm, NULL);
1142
    info->cpuTime = 0;
1143
    info->maxMem = virDomainDefGetMemoryTotal(vm->def);
1144
    info->memory = vm->def->mem.cur_balloon;
1145
    info->nrVirtCpu = virDomainDefGetVcpus(vm->def);
1146 1147
    ret = 0;

1148
 cleanup:
1149
    if (vm)
1150
        virObjectUnlock(vm);
1151 1152 1153
    return ret;
}

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
static int
vmwareDomainGetState(virDomainPtr dom,
                     int *state,
                     int *reason,
                     unsigned int flags)
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

    vmwareDriverLock(driver);
1167
    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
1168 1169 1170
    vmwareDriverUnlock(driver);

    if (!vm) {
1171 1172
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1173 1174 1175
        goto cleanup;
    }

1176 1177 1178
    if (vmwareUpdateVMStatus(driver, vm) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
1179
    *state = virDomainObjGetState(vm, reason);
1180 1181
    ret = 0;

1182
 cleanup:
1183
    if (vm)
1184
        virObjectUnlock(vm);
1185 1186 1187
    return ret;
}

1188
static int
1189
vmwareConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1190 1191 1192 1193
{
    return 1;
}

1194
static int
1195 1196 1197
vmwareConnectListAllDomains(virConnectPtr conn,
                            virDomainPtr **domains,
                            unsigned int flags)
1198 1199 1200 1201
{
    struct vmware_driver *driver = conn->privateData;
    int ret = -1;

O
Osier Yang 已提交
1202
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
1203 1204

    vmwareDriverLock(driver);
1205
    vmwareDomainObjListUpdateAll(driver->domains, driver);
1206 1207
    ret = virDomainObjListExport(driver->domains, conn, domains,
                                 NULL, flags);
1208 1209 1210 1211
    vmwareDriverUnlock(driver);
    return ret;
}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
static int
vmwareDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    struct vmware_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    virCheckFlags(0, -1);

    vmwareDriverLock(driver);
    obj = virDomainObjListFindByUUID(driver->domains, dom->uuid);
    vmwareDriverUnlock(driver);
    if (!obj) {
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }
    ret = 0;

 cleanup:
    if (obj)
        virObjectUnlock(obj);
    return ret;
}

1236 1237


1238
static virHypervisorDriver vmwareHypervisorDriver = {
1239
    .name = "VMWARE",
1240 1241 1242 1243 1244 1245 1246
    .connectOpen = vmwareConnectOpen, /* 0.8.7 */
    .connectClose = vmwareConnectClose, /* 0.8.7 */
    .connectGetType = vmwareConnectGetType, /* 0.8.7 */
    .connectGetVersion = vmwareConnectGetVersion, /* 0.8.7 */
    .connectListDomains = vmwareConnectListDomains, /* 0.8.7 */
    .connectNumOfDomains = vmwareConnectNumOfDomains, /* 0.8.7 */
    .connectListAllDomains = vmwareConnectListAllDomains, /* 0.9.13 */
1247 1248 1249 1250 1251 1252 1253
    .domainCreateXML = vmwareDomainCreateXML, /* 0.8.7 */
    .domainLookupByID = vmwareDomainLookupByID, /* 0.8.7 */
    .domainLookupByUUID = vmwareDomainLookupByUUID, /* 0.8.7 */
    .domainLookupByName = vmwareDomainLookupByName, /* 0.8.7 */
    .domainSuspend = vmwareDomainSuspend, /* 0.8.7 */
    .domainResume = vmwareDomainResume, /* 0.8.7 */
    .domainShutdown = vmwareDomainShutdown, /* 0.8.7 */
1254
    .domainShutdownFlags = vmwareDomainShutdownFlags, /* 0.9.10 */
1255
    .domainReboot = vmwareDomainReboot, /* 0.8.7 */
1256 1257 1258
    .domainDestroy = vmwareDomainDestroy, /* 0.8.7 */
    .domainDestroyFlags = vmwareDomainDestroyFlags, /* 0.9.4 */
    .domainGetOSType = vmwareDomainGetOSType, /* 0.8.7 */
1259 1260 1261
    .domainGetInfo = vmwareDomainGetInfo, /* 0.8.7 */
    .domainGetState = vmwareDomainGetState, /* 0.9.2 */
    .domainGetXMLDesc = vmwareDomainGetXMLDesc, /* 0.8.7 */
1262 1263 1264
    .connectDomainXMLFromNative = vmwareConnectDomainXMLFromNative, /* 0.9.11 */
    .connectListDefinedDomains = vmwareConnectListDefinedDomains, /* 0.8.7 */
    .connectNumOfDefinedDomains = vmwareConnectNumOfDefinedDomains, /* 0.8.7 */
1265 1266 1267
    .domainCreate = vmwareDomainCreate, /* 0.8.7 */
    .domainCreateWithFlags = vmwareDomainCreateWithFlags, /* 0.8.7 */
    .domainDefineXML = vmwareDomainDefineXML, /* 0.8.7 */
1268
    .domainDefineXMLFlags = vmwareDomainDefineXMLFlags, /* 1.2.12 */
1269
    .domainUndefine = vmwareDomainUndefine, /* 0.8.7 */
1270
    .domainUndefineFlags = vmwareDomainUndefineFlags, /* 0.9.4 */
1271 1272
    .domainIsActive = vmwareDomainIsActive, /* 0.8.7 */
    .domainIsPersistent = vmwareDomainIsPersistent, /* 0.8.7 */
1273
    .connectIsAlive = vmwareConnectIsAlive, /* 0.9.8 */
1274
    .domainHasManagedSaveImage = vmwareDomainHasManagedSaveImage, /* 1.2.13 */
1275 1276
};

1277 1278 1279 1280
static virConnectDriver vmwareConnectDriver = {
    .hypervisorDriver = &vmwareHypervisorDriver,
};

1281 1282 1283
int
vmwareRegister(void)
{
1284 1285
    return virRegisterConnectDriver(&vmwareConnectDriver,
                                    false);
1286
}