esx_driver.c 168.4 KB
Newer Older
1 2

/*
3
 * esx_driver.c: core driver functions for managing VMware ESX hosts
4
 *
5
 * Copyright (C) 2010-2013 Red Hat, Inc.
6
 * Copyright (C) 2009-2013 Matthias Bolte <matthias.bolte@googlemail.com>
7 8 9 10 11 12 13 14 15 16 17 18 19
 * Copyright (C) 2009 Maximilian Wilhelm <max@rfc2324.org>
 *
 * 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
20
 * License along with this library.  If not, see
O
Osier Yang 已提交
21
 * <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28
 *
 */

#include <config.h>

#include "internal.h"
#include "domain_conf.h"
29
#include "snapshot_conf.h"
30
#include "virauth.h"
31
#include "viralloc.h"
32
#include "virfile.h"
33
#include "virlog.h"
34
#include "viruuid.h"
35
#include "vmx.h"
36
#include "virtypedparam.h"
37
#include "esx_driver.h"
38 39 40 41 42
#include "esx_interface_driver.h"
#include "esx_network_driver.h"
#include "esx_storage_driver.h"
#include "esx_device_monitor.h"
#include "esx_secret_driver.h"
M
Matthias Bolte 已提交
43
#include "esx_nwfilter_driver.h"
44
#include "esx_private.h"
45 46 47
#include "esx_vi.h"
#include "esx_vi_methods.h"
#include "esx_util.h"
48
#include "virstring.h"
49
#include "viruri.h"
50 51 52 53 54

#define VIR_FROM_THIS VIR_FROM_ESX

static int esxDomainGetMaxVcpus(virDomainPtr domain);

55 56 57 58
typedef struct _esxVMX_Data esxVMX_Data;

struct _esxVMX_Data {
    esxVI_Context *ctx;
59
    char *datastorePathWithoutFileName;
60 61 62 63
};



64 65 66
static void
esxFreePrivate(esxPrivate **priv)
{
67
    if (!priv || !(*priv)) {
68 69 70 71 72 73
        return;
    }

    esxVI_Context_Free(&(*priv)->host);
    esxVI_Context_Free(&(*priv)->vCenter);
    esxUtil_FreeParsedUri(&(*priv)->parsedUri);
74
    virObjectUnref((*priv)->caps);
75
    virObjectUnref((*priv)->xmlopt);
76 77 78 79 80
    VIR_FREE(*priv);
}



81
/*
82 83
 * Parse a file name from a .vmx file and convert it to datastore path format
 * if possbile. A .vmx file can contain file names in various formats:
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
 *
 * - A single name referencing a file in the same directory as the .vmx file:
 *
 *     test1.vmdk
 *
 * - An absolute file name referencing a file in a datastore that is mounted at
 *   /vmfs/volumes/<datastore>:
 *
 *     /vmfs/volumes/b24b7a78-9d82b4f5/test1/test1.vmdk
 *     /vmfs/volumes/datastore1/test1/test1.vmdk
 *
 *   The actual mount directory is /vmfs/volumes/b24b7a78-9d82b4f5, the second
 *   form is a symlink to it using the datastore name. This is the typical
 *   setup on an ESX(i) server.
 *
 * - With GSX installed on Windows there are also Windows style file names
 *   including UNC file names:
 *
 *     C:\Virtual Machines\test1\test1.vmdk
 *     \\nas1\storage1\test1\test1.vmdk
 *
105 106 107 108 109 110 111 112
 * - There might also be absolute file names referencing files outside of a
 *   datastore:
 *
 *     /usr/lib/vmware/isoimages/linux.iso
 *
 *   Such file names are left as is and are not converted to datastore path
 *   format because this is not possible.
 *
113 114 115 116 117 118 119
 * The datastore path format typically looks like this:
 *
 *  [datastore1] test1/test1.vmdk
 *
 * Firstly this functions checks if the given file name contains a separator.
 * If it doesn't then the referenced file is in the same directory as the .vmx
 * file. The datastore name and directory of the .vmx file are passed to this
120
 * function via the opaque parameter by the caller of virVMXParseConfig.
121 122 123 124 125 126 127 128 129
 *
 * Otherwise query for all known datastores and their mount directories. Then
 * try to find a datastore with a mount directory that is a prefix to the given
 * file name. This mechanism covers the Windows style file names too.
 *
 * The symlinks using the datastore name (/vmfs/volumes/datastore1) are an
 * exception and need special handling. Parse the datastore name and use it
 * to lookup the datastore by name to verify that it exists.
 */
130
static char *
131
esxParseVMXFileName(const char *fileName, void *opaque)
132
{
133
    char *result = NULL;
134
    esxVMX_Data *data = opaque;
135
    esxVI_String *propertyNameList = NULL;
136
    esxVI_ObjectContent *datastoreList = NULL;
137
    esxVI_ObjectContent *datastore = NULL;
138 139 140 141 142 143 144 145
    esxVI_DatastoreHostMount *hostMount = NULL;
    char *datastoreName;
    char *tmp;
    char *saveptr;
    char *strippedFileName = NULL;
    char *copyOfFileName = NULL;
    char *directoryAndFileName;

146
    if (!strchr(fileName, '/') && !strchr(fileName, '\\')) {
147
        /* Plain file name, use same directory as for the .vmx file */
148
        if (virAsprintf(&result, "%s/%s",
149
                        data->datastorePathWithoutFileName, fileName) < 0)
150 151 152 153 154 155 156 157
            goto cleanup;
    } else {
        if (esxVI_String_AppendValueToList(&propertyNameList,
                                           "summary.name") < 0 ||
            esxVI_LookupDatastoreList(data->ctx, propertyNameList,
                                      &datastoreList) < 0) {
            return NULL;
        }
158

159
        /* Search for datastore by mount path */
160
        for (datastore = datastoreList; datastore;
161 162 163
             datastore = datastore->_next) {
            esxVI_DatastoreHostMount_Free(&hostMount);
            datastoreName = NULL;
164

165
            if (esxVI_LookupDatastoreHostMount(data->ctx, datastore->obj,
166 167
                                               &hostMount,
                                               esxVI_Occurrence_RequiredItem) < 0 ||
168 169 170 171
                esxVI_GetStringValue(datastore, "summary.name", &datastoreName,
                                     esxVI_Occurrence_RequiredItem) < 0) {
                goto cleanup;
            }
172

173
            tmp = (char *)STRSKIP(fileName, hostMount->mountInfo->path);
174

175
            if (!tmp) {
176 177
                continue;
            }
178

179 180 181 182
            /* Found a match. Strip leading separators */
            while (*tmp == '/' || *tmp == '\\') {
                ++tmp;
            }
183

184
            if (VIR_STRDUP(strippedFileName, tmp) < 0) {
185 186
                goto cleanup;
            }
187

188
            tmp = strippedFileName;
189

190 191 192 193 194
            /* Convert \ to / */
            while (*tmp != '\0') {
                if (*tmp == '\\') {
                    *tmp = '/';
                }
195

196 197
                ++tmp;
            }
198

199
            if (virAsprintf(&result, "[%s] %s", datastoreName,
200
                            strippedFileName) < 0)
201
                goto cleanup;
202

203 204
            break;
        }
205

206
        /* Fallback to direct datastore name match */
207
        if (!result && STRPREFIX(fileName, "/vmfs/volumes/")) {
208
            if (VIR_STRDUP(copyOfFileName, fileName) < 0) {
209 210
                goto cleanup;
            }
211

212
            /* Expected format: '/vmfs/volumes/<datastore>/<path>' */
213 214 215
            if (!(tmp = STRSKIP(copyOfFileName, "/vmfs/volumes/")) ||
                !(datastoreName = strtok_r(tmp, "/", &saveptr))    ||
                !(directoryAndFileName = strtok_r(NULL, "", &saveptr))) {
216 217 218
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("File name '%s' doesn't have expected format "
                                 "'/vmfs/volumes/<datastore>/<path>'"), fileName);
219 220
                goto cleanup;
            }
221

222
            esxVI_ObjectContent_Free(&datastoreList);
223

224 225 226 227 228
            if (esxVI_LookupDatastoreByName(data->ctx, datastoreName,
                                            NULL, &datastoreList,
                                            esxVI_Occurrence_OptionalItem) < 0) {
                goto cleanup;
            }
229

230
            if (!datastoreList) {
231 232 233
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("File name '%s' refers to non-existing datastore '%s'"),
                               fileName, datastoreName);
234 235
                goto cleanup;
            }
236

237
            if (virAsprintf(&result, "[%s] %s", datastoreName,
238
                            directoryAndFileName) < 0)
239
                goto cleanup;
240 241
        }

242
        /* If it's an absolute path outside of a datastore just use it as is */
243
        if (!result && *fileName == '/') {
244
            /* FIXME: need to deal with Windows paths here too */
245
            if (VIR_STRDUP(result, fileName) < 0) {
246 247 248 249
                goto cleanup;
            }
        }

250
        if (!result) {
251 252
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not handle file name '%s'"), fileName);
253
            goto cleanup;
254
        }
255
    }
256

257 258 259 260 261 262
  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&datastoreList);
    esxVI_DatastoreHostMount_Free(&hostMount);
    VIR_FREE(strippedFileName);
    VIR_FREE(copyOfFileName);
263

264
    return result;
265 266 267 268
}



269
/*
E
Eric Blake 已提交
270
 * This function does the inverse of esxParseVMXFileName. It takes a file name
271 272
 * in datastore path format or in absolute format and converts it to a file
 * name that can be used in a .vmx file.
273 274 275 276 277 278
 *
 * The datastore path format and the formats found in a .vmx file are described
 * in the documentation of esxParseVMXFileName.
 *
 * Firstly parse the datastore path. Then use the datastore name to lookup the
 * datastore and it's mount path. Finally concatenate the mount path, directory
E
Eric Blake 已提交
279
 * and file name to an absolute path and return it. Detect the separator type
280 281
 * based on the mount path.
 */
282
static char *
283
esxFormatVMXFileName(const char *fileName, void *opaque)
284 285
{
    bool success = false;
286
    char *result = NULL;
287
    esxVMX_Data *data = opaque;
288
    char *datastoreName = NULL;
289
    char *directoryAndFileName = NULL;
290 291 292 293 294
    esxVI_ObjectContent *datastore = NULL;
    esxVI_DatastoreHostMount *hostMount = NULL;
    char separator = '/';
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *tmp;
295
    size_t length;
296

297 298 299 300 301 302
    if (*fileName == '[') {
        /* Parse datastore path and lookup datastore */
        if (esxUtil_ParseDatastorePath(fileName, &datastoreName, NULL,
                                       &directoryAndFileName) < 0) {
            goto cleanup;
        }
303

304 305 306
        if (esxVI_LookupDatastoreByName(data->ctx, datastoreName, NULL, &datastore,
                                        esxVI_Occurrence_RequiredItem) < 0 ||
            esxVI_LookupDatastoreHostMount(data->ctx, datastore->obj,
307 308
                                           &hostMount,
                                           esxVI_Occurrence_RequiredItem) < 0) {
309 310
            goto cleanup;
        }
311

312
        /* Detect separator type */
313
        if (strchr(hostMount->mountInfo->path, '\\')) {
314 315
            separator = '\\';
        }
316

317 318
        /* Strip trailing separators */
        length = strlen(hostMount->mountInfo->path);
319

320 321 322
        while (length > 0 && hostMount->mountInfo->path[length - 1] == separator) {
            --length;
        }
323

324 325
        /* Format as <mount>[/<directory>]/<file>, convert / to \ when necessary */
        virBufferAdd(&buffer, hostMount->mountInfo->path, length);
326

327 328
        if (separator != '/') {
            tmp = directoryAndFileName;
329

330 331 332 333
            while (*tmp != '\0') {
                if (*tmp == '/') {
                    *tmp = separator;
                }
334

335 336
                ++tmp;
            }
337
        }
338

339 340
        virBufferAddChar(&buffer, separator);
        virBufferAdd(&buffer, directoryAndFileName, -1);
341

342 343 344 345 346 347 348 349
        if (virBufferError(&buffer)) {
            virReportOOMError();
            goto cleanup;
        }

        result = virBufferContentAndReset(&buffer);
    } else if (*fileName == '/') {
        /* FIXME: need to deal with Windows paths here too */
350
        if (VIR_STRDUP(result, fileName) < 0) {
351 352 353
            goto cleanup;
        }
    } else {
354 355
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not handle file name '%s'"), fileName);
356 357 358 359 360 361 362 363 364
        goto cleanup;
    }

    /* FIXME: Check if referenced path/file really exists */

    success = true;

  cleanup:
    if (! success) {
365
        virBufferFreeAndReset(&buffer);
366
        VIR_FREE(result);
367 368 369
    }

    VIR_FREE(datastoreName);
370
    VIR_FREE(directoryAndFileName);
371 372
    esxVI_ObjectContent_Free(&datastore);
    esxVI_DatastoreHostMount_Free(&hostMount);
373

374
    return result;
375 376 377 378 379 380 381 382 383 384
}



static int
esxAutodetectSCSIControllerModel(virDomainDiskDefPtr def, int *model,
                                 void *opaque)
{
    int result = -1;
    esxVMX_Data *data = opaque;
385
    esxVI_FileInfo *fileInfo = NULL;
386 387 388 389 390
    esxVI_VmDiskFileInfo *vmDiskFileInfo = NULL;

    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK ||
        def->bus != VIR_DOMAIN_DISK_BUS_SCSI ||
        def->type != VIR_DOMAIN_DISK_TYPE_FILE ||
391
        !def->src ||
392 393 394 395 396 397 398 399
        ! STRPREFIX(def->src, "[")) {
        /*
         * This isn't a file-based SCSI disk device with a datastore related
         * source path => do nothing.
         */
        return 0;
    }

400 401
    if (esxVI_LookupFileInfoByDatastorePath(data->ctx, def->src,
                                            false, &fileInfo,
402
                                            esxVI_Occurrence_RequiredItem) < 0) {
403 404 405
        goto cleanup;
    }

406
    vmDiskFileInfo = esxVI_VmDiskFileInfo_DynamicCast(fileInfo);
407

408
    if (!vmDiskFileInfo || !vmDiskFileInfo->controllerType) {
409 410
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not lookup controller model for '%s'"), def->src);
411 412 413 414 415
        goto cleanup;
    }

    if (STRCASEEQ(vmDiskFileInfo->controllerType,
                  "VirtualBusLogicController")) {
416
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_BUSLOGIC;
417 418
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "VirtualLsiLogicController")) {
419
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC;
420 421
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "VirtualLsiLogicSASController")) {
422
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSISAS1068;
423 424
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "ParaVirtualSCSIController")) {
425
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VMPVSCSI;
426
    } else {
427 428 429
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Found unexpected controller model '%s' for disk '%s'"),
                       vmDiskFileInfo->controllerType, def->src);
430 431 432 433 434 435
        goto cleanup;
    }

    result = 0;

  cleanup:
436
    esxVI_FileInfo_Free(&fileInfo);
437 438 439 440

    return result;
}

441 442


443
static esxVI_Boolean
444
esxSupportsLongMode(esxPrivate *priv)
445 446 447 448 449 450
{
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_HostCpuIdInfo *hostCpuIdInfoList = NULL;
    esxVI_HostCpuIdInfo *hostCpuIdInfo = NULL;
451
    esxVI_ParsedHostCpuIdInfo parsedHostCpuIdInfo;
452 453 454 455 456 457
    char edxLongModeBit = '?';

    if (priv->supportsLongMode != esxVI_Boolean_Undefined) {
        return priv->supportsLongMode;
    }

458
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
459
        return esxVI_Boolean_Undefined;
460 461
    }

462
    if (esxVI_String_AppendValueToList(&propertyNameList,
463
                                       "hardware.cpuFeature") < 0 ||
464 465
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
466
        goto cleanup;
467 468
    }

469
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
470 471 472
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "hardware.cpuFeature")) {
            if (esxVI_HostCpuIdInfo_CastListFromAnyType
473
                  (dynamicProperty->val, &hostCpuIdInfoList) < 0) {
M
Matthias Bolte 已提交
474
                goto cleanup;
475 476
            }

477
            for (hostCpuIdInfo = hostCpuIdInfoList; hostCpuIdInfo;
478 479
                 hostCpuIdInfo = hostCpuIdInfo->_next) {
                if (hostCpuIdInfo->level->value == -2147483647) { /* 0x80000001 */
480 481
                    if (esxVI_ParseHostCpuIdInfo(&parsedHostCpuIdInfo,
                                                 hostCpuIdInfo) < 0) {
M
Matthias Bolte 已提交
482
                        goto cleanup;
483 484
                    }

485
                    edxLongModeBit = parsedHostCpuIdInfo.edx[29];
486 487 488 489 490 491

                    if (edxLongModeBit == '1') {
                        priv->supportsLongMode = esxVI_Boolean_True;
                    } else if (edxLongModeBit == '0') {
                        priv->supportsLongMode = esxVI_Boolean_False;
                    } else {
492 493 494 495 496
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Bit 29 (Long Mode) of HostSystem property "
                                         "'hardware.cpuFeature[].edx' with value '%s' "
                                         "has unexpected value '%c', expecting '0' "
                                         "or '1'"), hostCpuIdInfo->edx, edxLongModeBit);
M
Matthias Bolte 已提交
497
                        goto cleanup;
498 499 500 501 502 503 504 505 506 507 508 509 510
                    }

                    break;
                }
            }

            break;
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

  cleanup:
M
Matthias Bolte 已提交
511 512 513 514
    /*
     * If we goto cleanup in case of an error then priv->supportsLongMode
     * is still esxVI_Boolean_Undefined, therefore we don't need to set it.
     */
515 516 517 518 519 520 521 522 523
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);
    esxVI_HostCpuIdInfo_Free(&hostCpuIdInfoList);

    return priv->supportsLongMode;
}



524 525 526 527 528 529
static int
esxLookupHostSystemBiosUuid(esxPrivate *priv, unsigned char *uuid)
{
    int result = -1;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
530
    char *uuid_string = NULL;
531

532
    if (esxVI_EnsureSession(priv->primary) < 0) {
533 534 535 536 537
        return -1;
    }

    if (esxVI_String_AppendValueToList(&propertyNameList,
                                       "hardware.systemInfo.uuid") < 0 ||
538
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
539 540 541
                                         &hostSystem) < 0 ||
        esxVI_GetStringValue(hostSystem, "hardware.systemInfo.uuid",
                             &uuid_string, esxVI_Occurrence_RequiredItem) < 0) {
542 543 544
        goto cleanup;
    }

545 546 547
    if (strlen(uuid_string) > 0) {
        if (virUUIDParse(uuid_string, uuid) < 0) {
            VIR_WARN("Could not parse host UUID from string '%s'", uuid_string);
548

549 550
            /* HostSystem has an invalid UUID, ignore it */
            memset(uuid, 0, VIR_UUID_BUFLEN);
551
        }
552 553 554
    } else {
        /* HostSystem has an empty UUID */
        memset(uuid, 0, VIR_UUID_BUFLEN);
555 556 557 558 559 560 561 562 563 564 565 566
    }

    result = 0;

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return result;
}


567
static virCapsPtr
568
esxCapsInit(esxPrivate *priv)
569
{
570
    esxVI_Boolean supportsLongMode = esxSupportsLongMode(priv);
571 572 573
    virCapsPtr caps = NULL;
    virCapsGuestPtr guest = NULL;

574 575 576 577 578
    if (supportsLongMode == esxVI_Boolean_Undefined) {
        return NULL;
    }

    if (supportsLongMode == esxVI_Boolean_True) {
579
        caps = virCapabilitiesNew(VIR_ARCH_X86_64, 1, 1);
580
    } else {
581
        caps = virCapabilitiesNew(VIR_ARCH_I686, 1, 1);
582
    }
583

584
    if (!caps)
585 586
        return NULL;

587
    virCapabilitiesAddHostMigrateTransport(caps, "vpxmigr");
588

589

590 591 592 593
    if (esxLookupHostSystemBiosUuid(priv, caps->host.host_uuid) < 0) {
        goto failure;
    }

594
    /* i686 */
595 596 597
    guest = virCapabilitiesAddGuest(caps, "hvm",
                                    VIR_ARCH_I686,
                                    NULL, NULL, 0,
598
                                    NULL);
599

600
    if (!guest) {
601 602 603
        goto failure;
    }

604
    if (!virCapabilitiesAddGuestDomain(guest, "vmware", NULL, NULL, 0, NULL)) {
605 606 607
        goto failure;
    }

608 609
    /* x86_64 */
    if (supportsLongMode == esxVI_Boolean_True) {
610 611 612
        guest = virCapabilitiesAddGuest(caps, "hvm",
                                        VIR_ARCH_X86_64,
                                        NULL, NULL,
613 614
                                        0, NULL);

615
        if (!guest) {
616 617 618
            goto failure;
        }

619
        if (!virCapabilitiesAddGuestDomain(guest, "vmware", NULL, NULL, 0, NULL)) {
620 621 622 623
            goto failure;
        }
    }

624 625 626
    return caps;

  failure:
627
    virObjectUnref(caps);
628 629 630 631 632 633

    return NULL;
}



634
static int
635 636
esxConnectToHost(esxPrivate *priv,
                 virConnectPtr conn,
637
                 virConnectAuthPtr auth,
638 639 640 641 642
                 char **vCenterIpAddress)
{
    int result = -1;
    char ipAddress[NI_MAXHOST] = "";
    char *username = NULL;
M
Matthias Bolte 已提交
643
    char *unescapedPassword = NULL;
644 645 646 647 648
    char *password = NULL;
    char *url = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_Boolean inMaintenanceMode = esxVI_Boolean_Undefined;
649 650 651
    esxVI_ProductVersion expectedProductVersion = STRCASEEQ(conn->uri->scheme, "esx")
        ? esxVI_ProductVersion_ESX
        : esxVI_ProductVersion_GSX;
652

653
    if (!vCenterIpAddress || *vCenterIpAddress) {
654
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
655 656 657
        return -1;
    }

658
    if (esxUtil_ResolveHostname(conn->uri->server, ipAddress, NI_MAXHOST) < 0) {
659 660 661
        return -1;
    }

662
    if (conn->uri->user) {
663
        if (VIR_STRDUP(username, conn->uri->user) < 0)
664 665
            goto cleanup;
    } else {
666
        username = virAuthGetUsername(conn, auth, "esx", "root", conn->uri->server);
667

668
        if (!username) {
669
            virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed"));
670 671 672 673
            goto cleanup;
        }
    }

674
    unescapedPassword = virAuthGetPassword(conn, auth, "esx", username, conn->uri->server);
675

676
    if (!unescapedPassword) {
677
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed"));
678 679 680
        goto cleanup;
    }

M
Matthias Bolte 已提交
681 682
    password = esxUtil_EscapeForXml(unescapedPassword);

683
    if (!password) {
M
Matthias Bolte 已提交
684 685 686
        goto cleanup;
    }

687
    if (virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport,
688
                    conn->uri->server, conn->uri->port) < 0)
689 690 691 692
        goto cleanup;

    if (esxVI_Context_Alloc(&priv->host) < 0 ||
        esxVI_Context_Connect(priv->host, url, ipAddress, username, password,
693
                              priv->parsedUri) < 0 ||
694
        esxVI_Context_LookupManagedObjects(priv->host) < 0) {
695 696 697 698 699
        goto cleanup;
    }

    if (expectedProductVersion == esxVI_ProductVersion_ESX) {
        if (priv->host->productVersion != esxVI_ProductVersion_ESX35 &&
M
Matthias Bolte 已提交
700 701
            priv->host->productVersion != esxVI_ProductVersion_ESX40 &&
            priv->host->productVersion != esxVI_ProductVersion_ESX41 &&
P
Patrice LACHANCE 已提交
702 703
            priv->host->productVersion != esxVI_ProductVersion_ESX4x &&
            priv->host->productVersion != esxVI_ProductVersion_ESX50 &&
M
Martin Kletzander 已提交
704
            priv->host->productVersion != esxVI_ProductVersion_ESX51 &&
P
Patrice LACHANCE 已提交
705
            priv->host->productVersion != esxVI_ProductVersion_ESX5x) {
706 707 708
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("%s is neither an ESX 3.5, 4.x nor 5.x host"),
                           conn->uri->server);
709 710 711 712
            goto cleanup;
        }
    } else { /* GSX */
        if (priv->host->productVersion != esxVI_ProductVersion_GSX20) {
713 714
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("%s isn't a GSX 2.0 host"), conn->uri->server);
715 716 717 718 719 720 721 722
            goto cleanup;
        }
    }

    /* Query the host for maintenance mode and vCenter IP address */
    if (esxVI_String_AppendValueListToList(&propertyNameList,
                                           "runtime.inMaintenanceMode\0"
                                           "summary.managementServerIp\0") < 0 ||
723 724
        esxVI_LookupHostSystemProperties(priv->host, propertyNameList,
                                         &hostSystem) < 0 ||
725 726 727 728 729 730 731 732 733 734 735
        esxVI_GetBoolean(hostSystem, "runtime.inMaintenanceMode",
                         &inMaintenanceMode,
                         esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_GetStringValue(hostSystem, "summary.managementServerIp",
                             vCenterIpAddress,
                             esxVI_Occurrence_OptionalItem) < 0) {
        goto cleanup;
    }

    /* Warn if host is in maintenance mode */
    if (inMaintenanceMode == esxVI_Boolean_True) {
736
        VIR_WARN("The server is in maintenance mode");
737 738
    }

739 740
    if (VIR_STRDUP(*vCenterIpAddress, *vCenterIpAddress) < 0)
        goto cleanup;
741 742 743 744 745

    result = 0;

  cleanup:
    VIR_FREE(username);
M
Matthias Bolte 已提交
746 747
    VIR_FREE(unescapedPassword);
    VIR_FREE(password);
748 749 750 751 752 753 754 755 756 757
    VIR_FREE(url);
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return result;
}



static int
758 759
esxConnectToVCenter(esxPrivate *priv,
                    virConnectPtr conn,
760 761
                    virConnectAuthPtr auth,
                    const char *hostname,
762
                    const char *hostSystemIpAddress)
763 764 765 766
{
    int result = -1;
    char ipAddress[NI_MAXHOST] = "";
    char *username = NULL;
M
Matthias Bolte 已提交
767
    char *unescapedPassword = NULL;
768 769 770
    char *password = NULL;
    char *url = NULL;

771 772
    if (!hostSystemIpAddress &&
        (!priv->parsedUri->path || STREQ(priv->parsedUri->path, "/"))) {
773 774
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Path has to specify the datacenter and compute resource"));
775 776 777
        return -1;
    }

778 779 780 781
    if (esxUtil_ResolveHostname(hostname, ipAddress, NI_MAXHOST) < 0) {
        return -1;
    }

782
    if (conn->uri->user) {
783
        if (VIR_STRDUP(username, conn->uri->user) < 0) {
784 785 786
            goto cleanup;
        }
    } else {
787
        username = virAuthGetUsername(conn, auth, "esx", "administrator", hostname);
788

789
        if (!username) {
790
            virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed"));
791 792 793 794
            goto cleanup;
        }
    }

795
    unescapedPassword = virAuthGetPassword(conn, auth, "esx", username, hostname);
796

797
    if (!unescapedPassword) {
798
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed"));
799 800 801
        goto cleanup;
    }

M
Matthias Bolte 已提交
802 803
    password = esxUtil_EscapeForXml(unescapedPassword);

804
    if (!password) {
M
Matthias Bolte 已提交
805 806 807
        goto cleanup;
    }

808
    if (virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport,
809
                    hostname, conn->uri->port) < 0)
810 811 812 813
        goto cleanup;

    if (esxVI_Context_Alloc(&priv->vCenter) < 0 ||
        esxVI_Context_Connect(priv->vCenter, url, ipAddress, username,
814
                              password, priv->parsedUri) < 0) {
815 816 817 818
        goto cleanup;
    }

    if (priv->vCenter->productVersion != esxVI_ProductVersion_VPX25 &&
M
Matthias Bolte 已提交
819 820
        priv->vCenter->productVersion != esxVI_ProductVersion_VPX40 &&
        priv->vCenter->productVersion != esxVI_ProductVersion_VPX41 &&
P
Patrice LACHANCE 已提交
821 822
        priv->vCenter->productVersion != esxVI_ProductVersion_VPX4x &&
        priv->vCenter->productVersion != esxVI_ProductVersion_VPX50 &&
M
Martin Kletzander 已提交
823
        priv->vCenter->productVersion != esxVI_ProductVersion_VPX51 &&
P
Patrice LACHANCE 已提交
824
        priv->vCenter->productVersion != esxVI_ProductVersion_VPX5x) {
825 826 827
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("%s is neither a vCenter 2.5, 4.x nor 5.x server"),
                       hostname);
828 829 830
        goto cleanup;
    }

831
    if (hostSystemIpAddress) {
832 833
        if (esxVI_Context_LookupManagedObjectsByHostSystemIp
              (priv->vCenter, hostSystemIpAddress) < 0) {
834 835 836
            goto cleanup;
        }
    } else {
837 838
        if (esxVI_Context_LookupManagedObjectsByPath(priv->vCenter,
                                                     priv->parsedUri->path) < 0) {
839 840 841 842
            goto cleanup;
        }
    }

843 844 845 846
    result = 0;

  cleanup:
    VIR_FREE(username);
M
Matthias Bolte 已提交
847 848
    VIR_FREE(unescapedPassword);
    VIR_FREE(password);
849 850 851 852 853 854 855
    VIR_FREE(url);

    return result;
}



856
/*
857 858
 * URI format: {vpx|esx|gsx}://[<username>@]<hostname>[:<port>]/[<path>][?<query parameter>...]
 *             <path> = [<folder>/...]<datacenter>/[<folder>/...]<computeresource>[/<hostsystem>]
859
 *
860 861
 * If no port is specified the default port is set dependent on the scheme and
 * transport parameter:
862 863
 * - vpx+http  80
 * - vpx+https 443
864
 * - esx+http  80
865
 * - esx+https 443
866 867 868
 * - gsx+http  8222
 * - gsx+https 8333
 *
869 870 871
 * For a vpx:// connection <path> references a host managed by the vCenter.
 * In case the host is part of a cluster then <computeresource> is the cluster
 * name. Otherwise <computeresource> and <hostsystem> are equal and the later
872 873
 * can be omitted. As datacenters and computeresources can be organized in
 * folders those have to be included in <path>.
874
 *
875 876
 * Optional query parameters:
 * - transport={http|https}
877
 * - vcenter={<vcenter>|*}             only useful for an esx:// connection
878 879
 * - no_verify={0|1}
 * - auto_answer={0|1}
M
Matthias Bolte 已提交
880
 * - proxy=[{http|socks|socks4|socks4a|socks5}://]<hostname>[:<port>]
881
 *
882 883 884
 * If no transport parameter is specified https is used.
 *
 * The vcenter parameter is only necessary for migration, because the vCenter
885
 * server is in charge to initiate a migration between two ESX hosts. The
886
 * vcenter parameter can be set to an explicitly hostname or to *. If set to *,
887 888
 * the driver will check if the ESX host is managed by a vCenter and connect to
 * it. If the ESX host is not managed by a vCenter an error is reported.
889 890
 *
 * If the no_verify parameter is set to 1, this disables libcurl client checks
891
 * of the server's certificate. The default value it 0.
892 893 894 895
 *
 * If the auto_answer parameter is set to 1, the driver will respond to all
 * virtual machine questions with the default answer, otherwise virtual machine
 * questions will be reported as errors. The default value it 0.
M
Matthias Bolte 已提交
896 897 898 899
 *
 * The proxy parameter allows to specify a proxy for to be used by libcurl.
 * The default for the optional <type> part is http and socks is synonymous for
 * socks5. The optional <port> part allows to override the default port 1080.
900 901
 */
static virDrvOpenStatus
902 903
esxConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
               unsigned int flags)
904
{
M
Matthias Bolte 已提交
905
    virDrvOpenStatus result = VIR_DRV_OPEN_ERROR;
906
    char *plus;
907
    esxPrivate *priv = NULL;
908
    char *potentialVCenterIpAddress = NULL;
M
Matthias Bolte 已提交
909
    char vCenterIpAddress[NI_MAXHOST] = "";
910

E
Eric Blake 已提交
911 912
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

913
    /* Decline if the URI is NULL or the scheme is NULL */
914
    if (!conn->uri || !conn->uri->scheme) {
915 916 917
        return VIR_DRV_OPEN_DECLINED;
    }

918 919 920
    /* Decline if the scheme is not one of {vpx|esx|gsx} */
    plus = strchr(conn->uri->scheme, '+');

921
    if (!plus) {
922 923 924 925 926 927 928 929 930 931 932 933 934
        if (STRCASENEQ(conn->uri->scheme, "vpx") &&
            STRCASENEQ(conn->uri->scheme, "esx") &&
            STRCASENEQ(conn->uri->scheme, "gsx")) {
            return VIR_DRV_OPEN_DECLINED;
        }
    } else {
        if (plus - conn->uri->scheme != 3 ||
            (STRCASENEQLEN(conn->uri->scheme, "vpx", 3) &&
             STRCASENEQLEN(conn->uri->scheme, "esx", 3) &&
             STRCASENEQLEN(conn->uri->scheme, "gsx", 3))) {
            return VIR_DRV_OPEN_DECLINED;
        }

935 936 937
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Transport '%s' in URI scheme is not supported, try again "
                         "without the transport part"), plus + 1);
938 939 940
        return VIR_DRV_OPEN_ERROR;
    }

941
    if (STRCASENEQ(conn->uri->scheme, "vpx") &&
942
        conn->uri->path && STRNEQ(conn->uri->path, "/")) {
943 944 945 946
        VIR_WARN("Ignoring unexpected path '%s' for non-vpx scheme '%s'",
                 conn->uri->path, conn->uri->scheme);
    }

947
    /* Require server part */
948
    if (!conn->uri->server) {
949 950
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("URI is missing the server part"));
951 952 953 954
        return VIR_DRV_OPEN_ERROR;
    }

    /* Require auth */
955
    if (!auth || !auth->cb) {
956 957
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Missing or invalid auth pointer"));
958
        return VIR_DRV_OPEN_ERROR;
959 960 961
    }

    /* Allocate per-connection private data */
962
    if (VIR_ALLOC(priv) < 0)
M
Matthias Bolte 已提交
963
        goto cleanup;
964

965
    if (esxUtil_ParseUri(&priv->parsedUri, conn->uri) < 0) {
966 967 968
        goto cleanup;
    }

M
Matthias Bolte 已提交
969 970
    priv->maxVcpus = -1;
    priv->supportsVMotion = esxVI_Boolean_Undefined;
971
    priv->supportsLongMode = esxVI_Boolean_Undefined;
972 973
    priv->usedCpuTimeCounterId = -1;

M
Matthias Bolte 已提交
974 975 976 977 978 979 980
    /*
     * Set the port dependent on the transport protocol if no port is
     * specified. This allows us to rely on the port parameter being
     * correctly set when building URIs later on, without the need to
     * distinguish between the situations port == 0 and port != 0
     */
    if (conn->uri->port == 0) {
981 982
        if (STRCASEEQ(conn->uri->scheme, "vpx") ||
            STRCASEEQ(conn->uri->scheme, "esx")) {
983
            if (STRCASEEQ(priv->parsedUri->transport, "https")) {
M
Matthias Bolte 已提交
984 985 986 987 988
                conn->uri->port = 443;
            } else {
                conn->uri->port = 80;
            }
        } else { /* GSX */
989
            if (STRCASEEQ(priv->parsedUri->transport, "https")) {
M
Matthias Bolte 已提交
990 991 992 993
                conn->uri->port = 8333;
            } else {
                conn->uri->port = 8222;
            }
994
        }
M
Matthias Bolte 已提交
995
    }
996

997 998 999
    if (STRCASEEQ(conn->uri->scheme, "esx") ||
        STRCASEEQ(conn->uri->scheme, "gsx")) {
        /* Connect to host */
1000
        if (esxConnectToHost(priv, conn, auth,
1001
                             &potentialVCenterIpAddress) < 0) {
M
Matthias Bolte 已提交
1002
            goto cleanup;
1003
        }
1004

1005
        /* Connect to vCenter */
1006
        if (priv->parsedUri->vCenter) {
1007
            if (STREQ(priv->parsedUri->vCenter, "*")) {
1008
                if (!potentialVCenterIpAddress) {
1009 1010
                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                   _("This host is not managed by a vCenter"));
M
Matthias Bolte 已提交
1011
                    goto cleanup;
1012 1013
                }

1014 1015
                if (!virStrcpyStatic(vCenterIpAddress,
                                     potentialVCenterIpAddress)) {
1016 1017 1018
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("vCenter IP address %s too big for destination"),
                                   potentialVCenterIpAddress);
1019 1020 1021
                    goto cleanup;
                }
            } else {
1022
                if (esxUtil_ResolveHostname(priv->parsedUri->vCenter,
1023 1024 1025
                                            vCenterIpAddress, NI_MAXHOST) < 0) {
                    goto cleanup;
                }
1026

1027
                if (potentialVCenterIpAddress &&
1028
                    STRNEQ(vCenterIpAddress, potentialVCenterIpAddress)) {
1029 1030 1031 1032 1033 1034
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("This host is managed by a vCenter with IP "
                                     "address %s, but a mismachting vCenter '%s' "
                                     "(%s) has been specified"),
                                   potentialVCenterIpAddress, priv->parsedUri->vCenter,
                                   vCenterIpAddress);
M
Matthias Bolte 已提交
1035
                    goto cleanup;
1036 1037
                }
            }
1038

1039
            if (esxConnectToVCenter(priv, conn, auth,
1040
                                    vCenterIpAddress,
1041
                                    priv->host->ipAddress) < 0) {
1042 1043
                goto cleanup;
            }
1044 1045
        }

1046 1047 1048
        priv->primary = priv->host;
    } else { /* VPX */
        /* Connect to vCenter */
1049
        if (esxConnectToVCenter(priv, conn, auth,
1050 1051
                                conn->uri->server,
                                NULL) < 0) {
M
Matthias Bolte 已提交
1052
            goto cleanup;
1053 1054
        }

1055
        priv->primary = priv->vCenter;
1056 1057
    }

M
Matthias Bolte 已提交
1058
    /* Setup capabilities */
1059
    priv->caps = esxCapsInit(priv);
1060

1061
    if (!priv->caps) {
M
Matthias Bolte 已提交
1062
        goto cleanup;
1063 1064
    }

1065
    if (!(priv->xmlopt = virVMXDomainXMLConfInit()))
1066 1067
        goto cleanup;

1068 1069
    conn->privateData = priv;
    priv = NULL;
M
Matthias Bolte 已提交
1070
    result = VIR_DRV_OPEN_SUCCESS;
1071

M
Matthias Bolte 已提交
1072
  cleanup:
1073
    esxFreePrivate(&priv);
1074
    VIR_FREE(potentialVCenterIpAddress);
1075

M
Matthias Bolte 已提交
1076
    return result;
1077 1078 1079 1080 1081
}



static int
1082
esxConnectClose(virConnectPtr conn)
1083
{
M
Matthias Bolte 已提交
1084
    esxPrivate *priv = conn->privateData;
E
Eric Blake 已提交
1085
    int result = 0;
1086

1087
    if (priv->host) {
1088 1089 1090 1091 1092
        if (esxVI_EnsureSession(priv->host) < 0 ||
            esxVI_Logout(priv->host) < 0) {
            result = -1;
        }
    }
1093

1094
    if (priv->vCenter) {
E
Eric Blake 已提交
1095 1096 1097 1098
        if (esxVI_EnsureSession(priv->vCenter) < 0 ||
            esxVI_Logout(priv->vCenter) < 0) {
            result = -1;
        }
1099 1100
    }

1101
    esxFreePrivate(&priv);
1102 1103 1104

    conn->privateData = NULL;

E
Eric Blake 已提交
1105
    return result;
1106 1107 1108 1109 1110
}



static esxVI_Boolean
1111
esxSupportsVMotion(esxPrivate *priv)
1112 1113 1114 1115
{
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;

M
Matthias Bolte 已提交
1116 1117
    if (priv->supportsVMotion != esxVI_Boolean_Undefined) {
        return priv->supportsVMotion;
1118 1119
    }

1120
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1121
        return esxVI_Boolean_Undefined;
1122 1123
    }

1124
    if (esxVI_String_AppendValueToList(&propertyNameList,
1125
                                       "capability.vmotionSupported") < 0 ||
1126
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
1127 1128
                                         &hostSystem) < 0 ||
        esxVI_GetBoolean(hostSystem, "capability.vmotionSupported",
1129 1130 1131
                         &priv->supportsVMotion,
                         esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
1132 1133 1134
    }

  cleanup:
M
Matthias Bolte 已提交
1135 1136 1137 1138
    /*
     * If we goto cleanup in case of an error then priv->supportsVMotion is
     * still esxVI_Boolean_Undefined, therefore we don't need to set it.
     */
1139 1140 1141
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

M
Matthias Bolte 已提交
1142
    return priv->supportsVMotion;
1143 1144 1145 1146 1147
}



static int
1148
esxConnectSupportsFeature(virConnectPtr conn, int feature)
1149
{
M
Matthias Bolte 已提交
1150
    esxPrivate *priv = conn->privateData;
M
Matthias Bolte 已提交
1151
    esxVI_Boolean supportsVMotion = esxVI_Boolean_Undefined;
1152 1153 1154

    switch (feature) {
      case VIR_DRV_FEATURE_MIGRATION_V1:
1155
        supportsVMotion = esxSupportsVMotion(priv);
1156

M
Matthias Bolte 已提交
1157
        if (supportsVMotion == esxVI_Boolean_Undefined) {
1158 1159 1160
            return -1;
        }

M
Matthias Bolte 已提交
1161
        /* Migration is only possible via a vCenter and if VMotion is enabled */
1162
        return priv->vCenter &&
M
Matthias Bolte 已提交
1163
               supportsVMotion == esxVI_Boolean_True ? 1 : 0;
1164 1165 1166 1167 1168 1169 1170 1171 1172

      default:
        return 0;
    }
}



static const char *
1173
esxConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
1174 1175 1176 1177 1178 1179 1180
{
    return "ESX";
}



static int
1181
esxConnectGetVersion(virConnectPtr conn, unsigned long *version)
1182
{
M
Matthias Bolte 已提交
1183
    esxPrivate *priv = conn->privateData;
1184

1185
    if (virParseVersionString(priv->primary->service->about->version,
1186
                              version, false) < 0) {
1187 1188 1189
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse version number from '%s'"),
                       priv->primary->service->about->version);
1190

1191
        return -1;
1192 1193 1194 1195 1196 1197 1198 1199
    }

    return 0;
}



static char *
1200
esxConnectGetHostname(virConnectPtr conn)
1201
{
M
Matthias Bolte 已提交
1202
    esxPrivate *priv = conn->privateData;
1203 1204 1205 1206 1207 1208 1209
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    const char *hostName = NULL;
    const char *domainName = NULL;
    char *complete = NULL;

1210
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1211
        return NULL;
1212 1213 1214
    }

    if (esxVI_String_AppendValueListToList
1215
          (&propertyNameList,
1216 1217
           "config.network.dnsConfig.hostName\0"
           "config.network.dnsConfig.domainName\0") < 0 ||
1218 1219
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
1220
        goto cleanup;
1221 1222
    }

1223
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
1224 1225 1226
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name,
                  "config.network.dnsConfig.hostName")) {
1227
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1228
                                         esxVI_Type_String) < 0) {
M
Matthias Bolte 已提交
1229
                goto cleanup;
1230 1231 1232 1233 1234
            }

            hostName = dynamicProperty->val->string;
        } else if (STREQ(dynamicProperty->name,
                         "config.network.dnsConfig.domainName")) {
1235
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1236
                                         esxVI_Type_String) < 0) {
M
Matthias Bolte 已提交
1237
                goto cleanup;
1238 1239 1240 1241 1242 1243 1244 1245
            }

            domainName = dynamicProperty->val->string;
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

1246
    if (!hostName || strlen(hostName) < 1) {
1247 1248
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing or empty 'hostName' property"));
M
Matthias Bolte 已提交
1249
        goto cleanup;
1250 1251
    }

1252
    if (!domainName || strlen(domainName) < 1) {
1253
        if (VIR_STRDUP(complete, hostName) < 0)
M
Matthias Bolte 已提交
1254
            goto cleanup;
1255
    } else {
1256
        if (virAsprintf(&complete, "%s.%s", hostName, domainName) < 0)
M
Matthias Bolte 已提交
1257
            goto cleanup;
1258 1259 1260
    }

  cleanup:
M
Matthias Bolte 已提交
1261 1262
    /*
     * If we goto cleanup in case of an error then complete is still NULL,
1263
     * either VIR_STRDUP returned -1 or virAsprintf failed. When virAsprintf
M
Matthias Bolte 已提交
1264 1265
     * fails it guarantees setting complete to NULL
     */
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return complete;
}



static int
esxNodeGetInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo)
{
M
Matthias Bolte 已提交
1277
    int result = -1;
M
Matthias Bolte 已提交
1278
    esxPrivate *priv = conn->privateData;
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    int64_t cpuInfo_hz = 0;
    int16_t cpuInfo_numCpuCores = 0;
    int16_t cpuInfo_numCpuPackages = 0;
    int16_t cpuInfo_numCpuThreads = 0;
    int64_t memorySize = 0;
    int32_t numaInfo_numNodes = 0;
    char *ptr = NULL;

1290
    memset(nodeinfo, 0, sizeof(*nodeinfo));
1291

1292
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1293
        return -1;
1294 1295
    }

1296
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1297 1298 1299 1300 1301 1302 1303
                                           "hardware.cpuInfo.hz\0"
                                           "hardware.cpuInfo.numCpuCores\0"
                                           "hardware.cpuInfo.numCpuPackages\0"
                                           "hardware.cpuInfo.numCpuThreads\0"
                                           "hardware.memorySize\0"
                                           "hardware.numaInfo.numNodes\0"
                                           "summary.hardware.cpuModel\0") < 0 ||
1304 1305
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
1306
        goto cleanup;
1307 1308
    }

1309
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
1310 1311
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "hardware.cpuInfo.hz")) {
1312
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1313
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
1314
                goto cleanup;
1315 1316 1317 1318 1319
            }

            cpuInfo_hz = dynamicProperty->val->int64;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.cpuInfo.numCpuCores")) {
1320
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1321
                                         esxVI_Type_Short) < 0) {
M
Matthias Bolte 已提交
1322
                goto cleanup;
1323 1324 1325 1326 1327
            }

            cpuInfo_numCpuCores = dynamicProperty->val->int16;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.cpuInfo.numCpuPackages")) {
1328
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1329
                                         esxVI_Type_Short) < 0) {
M
Matthias Bolte 已提交
1330
                goto cleanup;
1331 1332 1333 1334 1335
            }

            cpuInfo_numCpuPackages = dynamicProperty->val->int16;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.cpuInfo.numCpuThreads")) {
1336
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1337
                                         esxVI_Type_Short) < 0) {
M
Matthias Bolte 已提交
1338
                goto cleanup;
1339 1340 1341 1342
            }

            cpuInfo_numCpuThreads = dynamicProperty->val->int16;
        } else if (STREQ(dynamicProperty->name, "hardware.memorySize")) {
1343
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1344
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
1345
                goto cleanup;
1346 1347 1348 1349 1350
            }

            memorySize = dynamicProperty->val->int64;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.numaInfo.numNodes")) {
1351
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1352
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
1353
                goto cleanup;
1354 1355 1356 1357 1358
            }

            numaInfo_numNodes = dynamicProperty->val->int32;
        } else if (STREQ(dynamicProperty->name,
                         "summary.hardware.cpuModel")) {
1359
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1360
                                         esxVI_Type_String) < 0) {
M
Matthias Bolte 已提交
1361
                goto cleanup;
1362 1363 1364 1365 1366 1367
            }

            ptr = dynamicProperty->val->string;

            /* Strip the string to fit more relevant information in 32 chars */
            while (*ptr != '\0') {
M
Matthias Bolte 已提交
1368 1369
                if (STRPREFIX(ptr, "  ")) {
                    memmove(ptr, ptr + 1, strlen(ptr + 1) + 1);
1370
                    continue;
1371
                } else if (STRPREFIX(ptr, "(R)") || STRPREFIX(ptr, "(C)")) {
M
Matthias Bolte 已提交
1372
                    memmove(ptr, ptr + 3, strlen(ptr + 3) + 1);
1373
                    continue;
1374 1375 1376
                } else if (STRPREFIX(ptr, "(TM)")) {
                    memmove(ptr, ptr + 4, strlen(ptr + 4) + 1);
                    continue;
1377 1378 1379 1380 1381
                }

                ++ptr;
            }

1382 1383 1384
            if (!virStrncpy(nodeinfo->model, dynamicProperty->val->string,
                            sizeof(nodeinfo->model) - 1,
                            sizeof(nodeinfo->model))) {
1385 1386 1387
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("CPU Model %s too long for destination"),
                               dynamicProperty->val->string);
M
Matthias Bolte 已提交
1388
                goto cleanup;
C
Chris Lalancette 已提交
1389
            }
1390 1391 1392 1393 1394 1395 1396
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

    nodeinfo->memory = memorySize / 1024; /* Scale from bytes to kilobytes */
    nodeinfo->cpus = cpuInfo_numCpuCores;
1397
    nodeinfo->mhz = cpuInfo_hz / (1000 * 1000); /* Scale from hz to mhz */
1398 1399 1400 1401 1402 1403 1404 1405 1406
    nodeinfo->nodes = numaInfo_numNodes;
    nodeinfo->sockets = cpuInfo_numCpuPackages;
    nodeinfo->cores = cpuInfo_numCpuPackages > 0
                        ? cpuInfo_numCpuCores / cpuInfo_numCpuPackages
                        : 0;
    nodeinfo->threads = cpuInfo_numCpuCores > 0
                          ? cpuInfo_numCpuThreads / cpuInfo_numCpuCores
                          : 0;

M
Matthias Bolte 已提交
1407 1408
    result = 0;

1409 1410 1411 1412 1413 1414 1415 1416 1417
  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return result;
}



1418
static char *
1419
esxConnectGetCapabilities(virConnectPtr conn)
1420
{
M
Matthias Bolte 已提交
1421
    esxPrivate *priv = conn->privateData;
M
Matthias Bolte 已提交
1422
    char *xml = virCapabilitiesFormatXML(priv->caps);
1423

1424
    if (!xml) {
1425
        virReportOOMError();
1426 1427 1428 1429 1430 1431 1432 1433
        return NULL;
    }

    return xml;
}



1434
static int
1435
esxConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1436
{
M
Matthias Bolte 已提交
1437
    bool success = false;
M
Matthias Bolte 已提交
1438
    esxPrivate *priv = conn->privateData;
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    int count = 0;

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

1449
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1450
        return -1;
1451 1452
    }

1453
    if (esxVI_String_AppendValueToList(&propertyNameList,
1454
                                       "runtime.powerState") < 0 ||
1455 1456
        esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
                                       &virtualMachineList) < 0) {
M
Matthias Bolte 已提交
1457
        goto cleanup;
1458 1459
    }

1460
    for (virtualMachine = virtualMachineList; virtualMachine;
1461
         virtualMachine = virtualMachine->_next) {
1462
        if (esxVI_GetVirtualMachinePowerState(virtualMachine,
1463
                                              &powerState) < 0) {
M
Matthias Bolte 已提交
1464
            goto cleanup;
1465 1466 1467 1468 1469 1470 1471 1472 1473
        }

        if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
            continue;
        }

        if (esxUtil_ParseVirtualMachineIDString(virtualMachine->obj->value,
                                                &ids[count]) < 0 ||
            ids[count] <= 0) {
1474 1475 1476
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to parse positive integer from '%s'"),
                           virtualMachine->obj->value);
M
Matthias Bolte 已提交
1477
            goto cleanup;
1478 1479 1480 1481 1482 1483 1484 1485 1486
        }

        count++;

        if (count >= maxids) {
            break;
        }
    }

M
Matthias Bolte 已提交
1487 1488
    success = true;

1489 1490 1491 1492
  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);

M
Matthias Bolte 已提交
1493
    return success ? count : -1;
1494 1495 1496 1497 1498
}



static int
1499
esxConnectNumOfDomains(virConnectPtr conn)
1500
{
M
Matthias Bolte 已提交
1501
    esxPrivate *priv = conn->privateData;
1502

1503
    if (esxVI_EnsureSession(priv->primary) < 0) {
1504 1505 1506
        return -1;
    }

1507
    return esxVI_LookupNumberOfDomainsByPowerState
1508
             (priv->primary, esxVI_VirtualMachinePowerState_PoweredOn, false);
1509 1510 1511 1512 1513 1514 1515
}



static virDomainPtr
esxDomainLookupByID(virConnectPtr conn, int id)
{
M
Matthias Bolte 已提交
1516
    esxPrivate *priv = conn->privateData;
1517 1518 1519 1520
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
M
Matthias Bolte 已提交
1521 1522 1523
    int id_candidate = -1;
    char *name_candidate = NULL;
    unsigned char uuid_candidate[VIR_UUID_BUFLEN];
1524 1525
    virDomainPtr domain = NULL;

1526
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1527
        return NULL;
1528 1529
    }

1530
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1531
                                           "configStatus\0"
1532 1533
                                           "name\0"
                                           "runtime.powerState\0"
1534
                                           "config.uuid\0") < 0 ||
1535 1536
        esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
                                       &virtualMachineList) < 0) {
M
Matthias Bolte 已提交
1537
        goto cleanup;
1538 1539
    }

1540
    for (virtualMachine = virtualMachineList; virtualMachine;
1541
         virtualMachine = virtualMachine->_next) {
1542
        if (esxVI_GetVirtualMachinePowerState(virtualMachine,
1543
                                              &powerState) < 0) {
M
Matthias Bolte 已提交
1544
            goto cleanup;
1545 1546 1547 1548 1549 1550 1551
        }

        /* Only running/suspended domains have an ID != -1 */
        if (powerState == esxVI_VirtualMachinePowerState_PoweredOff) {
            continue;
        }

M
Matthias Bolte 已提交
1552
        VIR_FREE(name_candidate);
1553

1554
        if (esxVI_GetVirtualMachineIdentity(virtualMachine,
M
Matthias Bolte 已提交
1555 1556
                                            &id_candidate, &name_candidate,
                                            uuid_candidate) < 0) {
M
Matthias Bolte 已提交
1557
            goto cleanup;
1558 1559
        }

M
Matthias Bolte 已提交
1560
        if (id != id_candidate) {
1561 1562 1563
            continue;
        }

M
Matthias Bolte 已提交
1564
        domain = virGetDomain(conn, name_candidate, uuid_candidate);
1565

1566
        if (!domain) {
M
Matthias Bolte 已提交
1567
            goto cleanup;
1568 1569 1570 1571 1572 1573 1574
        }

        domain->id = id;

        break;
    }

1575
    if (!domain) {
1576
        virReportError(VIR_ERR_NO_DOMAIN, _("No domain with ID %d"), id);
1577 1578 1579 1580 1581
    }

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);
M
Matthias Bolte 已提交
1582
    VIR_FREE(name_candidate);
1583 1584 1585 1586 1587 1588 1589 1590 1591

    return domain;
}



static virDomainPtr
esxDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
M
Matthias Bolte 已提交
1592
    esxPrivate *priv = conn->privateData;
1593 1594 1595
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
1596 1597
    int id = -1;
    char *name = NULL;
1598 1599
    virDomainPtr domain = NULL;

1600
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1601
        return NULL;
1602 1603
    }

1604
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1605
                                           "name\0"
1606
                                           "runtime.powerState\0") < 0 ||
1607
        esxVI_LookupVirtualMachineByUuid(priv->primary, uuid, propertyNameList,
1608
                                         &virtualMachine,
M
Matthias Bolte 已提交
1609
                                         esxVI_Occurrence_RequiredItem) < 0 ||
1610 1611
        esxVI_GetVirtualMachineIdentity(virtualMachine, &id, &name, NULL) < 0 ||
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1612
        goto cleanup;
1613 1614
    }

1615
    domain = virGetDomain(conn, name, uuid);
1616

1617
    if (!domain) {
M
Matthias Bolte 已提交
1618
        goto cleanup;
1619
    }
1620

1621 1622 1623 1624 1625
    /* Only running/suspended virtual machines have an ID != -1 */
    if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
        domain->id = id;
    } else {
        domain->id = -1;
1626 1627 1628 1629
    }

  cleanup:
    esxVI_String_Free(&propertyNameList);
1630 1631
    esxVI_ObjectContent_Free(&virtualMachine);
    VIR_FREE(name);
1632 1633 1634 1635 1636 1637 1638 1639 1640

    return domain;
}



static virDomainPtr
esxDomainLookupByName(virConnectPtr conn, const char *name)
{
M
Matthias Bolte 已提交
1641
    esxPrivate *priv = conn->privateData;
1642 1643 1644
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
1645 1646
    int id = -1;
    unsigned char uuid[VIR_UUID_BUFLEN];
1647 1648
    virDomainPtr domain = NULL;

1649
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1650
        return NULL;
1651 1652
    }

1653
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1654
                                           "configStatus\0"
1655
                                           "runtime.powerState\0"
1656
                                           "config.uuid\0") < 0 ||
1657
        esxVI_LookupVirtualMachineByName(priv->primary, name, propertyNameList,
1658 1659
                                         &virtualMachine,
                                         esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
1660
        goto cleanup;
1661 1662
    }

1663
    if (!virtualMachine) {
1664
        virReportError(VIR_ERR_NO_DOMAIN, _("No domain with name '%s'"), name);
M
Matthias Bolte 已提交
1665
        goto cleanup;
1666
    }
1667

M
Matthias Bolte 已提交
1668 1669 1670
    if (esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, uuid) < 0 ||
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
        goto cleanup;
1671
    }
1672

1673
    domain = virGetDomain(conn, name, uuid);
1674

1675
    if (!domain) {
M
Matthias Bolte 已提交
1676
        goto cleanup;
1677 1678
    }

1679 1680 1681 1682 1683
    /* Only running/suspended virtual machines have an ID != -1 */
    if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
        domain->id = id;
    } else {
        domain->id = -1;
1684 1685 1686 1687
    }

  cleanup:
    esxVI_String_Free(&propertyNameList);
1688
    esxVI_ObjectContent_Free(&virtualMachine);
1689 1690 1691 1692 1693 1694 1695 1696 1697

    return domain;
}



static int
esxDomainSuspend(virDomainPtr domain)
{
M
Matthias Bolte 已提交
1698
    int result = -1;
M
Matthias Bolte 已提交
1699
    esxPrivate *priv = domain->conn->privateData;
1700 1701 1702 1703 1704
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
1705
    char *taskInfoErrorMessage = NULL;
1706

1707
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1708
        return -1;
1709 1710
    }

1711
    if (esxVI_String_AppendValueToList(&propertyNameList,
1712
                                       "runtime.powerState") < 0 ||
1713
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
1714
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
1715
           priv->parsedUri->autoAnswer) < 0 ||
1716
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1717
        goto cleanup;
1718 1719 1720
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1721 1722
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1723
        goto cleanup;
1724 1725
    }

1726 1727
    if (esxVI_SuspendVM_Task(priv->primary, virtualMachine->obj, &task) < 0 ||
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
1728
                                    esxVI_Occurrence_RequiredItem,
1729
                                    priv->parsedUri->autoAnswer, &taskInfoState,
1730
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
1731
        goto cleanup;
1732 1733 1734
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
1735 1736
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not suspend domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
1737
        goto cleanup;
1738 1739
    }

M
Matthias Bolte 已提交
1740 1741
    result = 0;

1742 1743 1744 1745
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
1746
    VIR_FREE(taskInfoErrorMessage);
1747 1748 1749 1750 1751 1752 1753 1754 1755

    return result;
}



static int
esxDomainResume(virDomainPtr domain)
{
M
Matthias Bolte 已提交
1756
    int result = -1;
M
Matthias Bolte 已提交
1757
    esxPrivate *priv = domain->conn->privateData;
1758 1759 1760 1761 1762
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
1763
    char *taskInfoErrorMessage = NULL;
1764

1765
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1766
        return -1;
1767 1768
    }

1769
    if (esxVI_String_AppendValueToList(&propertyNameList,
1770
                                       "runtime.powerState") < 0 ||
1771
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
1772
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
1773
           priv->parsedUri->autoAnswer) < 0 ||
1774
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1775
        goto cleanup;
1776 1777 1778
    }

    if (powerState != esxVI_VirtualMachinePowerState_Suspended) {
1779
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not suspended"));
M
Matthias Bolte 已提交
1780
        goto cleanup;
1781 1782
    }

1783
    if (esxVI_PowerOnVM_Task(priv->primary, virtualMachine->obj, NULL,
1784
                             &task) < 0 ||
1785
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
1786
                                    esxVI_Occurrence_RequiredItem,
1787
                                    priv->parsedUri->autoAnswer, &taskInfoState,
1788
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
1789
        goto cleanup;
1790 1791 1792
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
1793 1794
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not resume domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
1795
        goto cleanup;
1796 1797
    }

M
Matthias Bolte 已提交
1798 1799
    result = 0;

1800 1801 1802 1803
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
1804
    VIR_FREE(taskInfoErrorMessage);
1805 1806 1807 1808 1809 1810 1811

    return result;
}



static int
1812
esxDomainShutdownFlags(virDomainPtr domain, unsigned int flags)
1813
{
M
Matthias Bolte 已提交
1814
    int result = -1;
M
Matthias Bolte 已提交
1815
    esxPrivate *priv = domain->conn->privateData;
1816 1817 1818 1819
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

1820 1821
    virCheckFlags(0, -1);

1822
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1823
        return -1;
1824 1825
    }

1826
    if (esxVI_String_AppendValueToList(&propertyNameList,
1827
                                       "runtime.powerState") < 0 ||
1828
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
1829
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
1830
                                         esxVI_Occurrence_RequiredItem) < 0 ||
1831
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1832
        goto cleanup;
1833 1834 1835
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1836 1837
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1838
        goto cleanup;
1839 1840
    }

1841
    if (esxVI_ShutdownGuest(priv->primary, virtualMachine->obj) < 0) {
M
Matthias Bolte 已提交
1842
        goto cleanup;
1843 1844
    }

M
Matthias Bolte 已提交
1845 1846
    result = 0;

1847 1848 1849 1850 1851 1852 1853 1854
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}


1855 1856 1857 1858 1859 1860
static int
esxDomainShutdown(virDomainPtr domain)
{
    return esxDomainShutdownFlags(domain, 0);
}

1861 1862

static int
E
Eric Blake 已提交
1863
esxDomainReboot(virDomainPtr domain, unsigned int flags)
1864
{
M
Matthias Bolte 已提交
1865
    int result = -1;
M
Matthias Bolte 已提交
1866
    esxPrivate *priv = domain->conn->privateData;
1867 1868 1869 1870
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

E
Eric Blake 已提交
1871 1872
    virCheckFlags(0, -1);

1873
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
1874
        return -1;
1875 1876
    }

1877
    if (esxVI_String_AppendValueToList(&propertyNameList,
1878
                                       "runtime.powerState") < 0 ||
1879
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
1880
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
1881
                                         esxVI_Occurrence_RequiredItem) < 0 ||
1882
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1883
        goto cleanup;
1884 1885 1886
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1887 1888
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1889
        goto cleanup;
1890 1891
    }

1892
    if (esxVI_RebootGuest(priv->primary, virtualMachine->obj) < 0) {
M
Matthias Bolte 已提交
1893
        goto cleanup;
1894 1895
    }

M
Matthias Bolte 已提交
1896 1897
    result = 0;

1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}



static int
1908 1909
esxDomainDestroyFlags(virDomainPtr domain,
                      unsigned int flags)
1910
{
M
Matthias Bolte 已提交
1911
    int result = -1;
M
Matthias Bolte 已提交
1912
    esxPrivate *priv = domain->conn->privateData;
1913
    esxVI_Context *ctx = NULL;
1914 1915 1916 1917 1918
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
1919
    char *taskInfoErrorMessage = NULL;
1920

1921 1922
    virCheckFlags(0, -1);

1923
    if (priv->vCenter) {
1924 1925 1926 1927 1928
        ctx = priv->vCenter;
    } else {
        ctx = priv->host;
    }

1929
    if (esxVI_EnsureSession(ctx) < 0) {
M
Matthias Bolte 已提交
1930
        return -1;
1931 1932
    }

1933
    if (esxVI_String_AppendValueToList(&propertyNameList,
1934
                                       "runtime.powerState") < 0 ||
1935
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
1936
          (ctx, domain->uuid, propertyNameList, &virtualMachine,
1937
           priv->parsedUri->autoAnswer) < 0 ||
1938
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1939
        goto cleanup;
1940 1941 1942
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1943 1944
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1945
        goto cleanup;
1946 1947
    }

1948
    if (esxVI_PowerOffVM_Task(ctx, virtualMachine->obj, &task) < 0 ||
1949 1950
        esxVI_WaitForTaskCompletion(ctx, task, domain->uuid,
                                    esxVI_Occurrence_RequiredItem,
1951
                                    priv->parsedUri->autoAnswer, &taskInfoState,
1952
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
1953
        goto cleanup;
1954 1955 1956
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
1957 1958
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not destroy domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
1959
        goto cleanup;
1960 1961
    }

1962
    domain->id = -1;
M
Matthias Bolte 已提交
1963 1964
    result = 0;

1965 1966 1967 1968
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
1969
    VIR_FREE(taskInfoErrorMessage);
1970 1971 1972 1973 1974

    return result;
}


1975 1976 1977 1978 1979 1980
static int
esxDomainDestroy(virDomainPtr dom)
{
    return esxDomainDestroyFlags(dom, 0);
}

1981 1982

static char *
1983
esxDomainGetOSType(virDomainPtr domain ATTRIBUTE_UNUSED)
1984
{
1985
    char *osType;
1986

1987
    ignore_value(VIR_STRDUP(osType, "hvm"));
1988
    return osType;
1989 1990 1991 1992
}



1993
static unsigned long long
1994 1995
esxDomainGetMaxMemory(virDomainPtr domain)
{
M
Matthias Bolte 已提交
1996
    esxPrivate *priv = domain->conn->privateData;
1997 1998 1999 2000 2001
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    unsigned long memoryMB = 0;

2002
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2003
        return 0;
2004 2005
    }

2006
    if (esxVI_String_AppendValueToList(&propertyNameList,
2007
                                       "config.hardware.memoryMB") < 0 ||
2008
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
2009
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
2010
                                         esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
2011
        goto cleanup;
2012 2013
    }

2014
    for (dynamicProperty = virtualMachine->propSet; dynamicProperty;
2015 2016
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "config.hardware.memoryMB")) {
2017
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2018
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
2019
                goto cleanup;
2020 2021 2022
            }

            if (dynamicProperty->val->int32 < 0) {
2023 2024 2025
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Got invalid memory size %d"),
                               dynamicProperty->val->int32);
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
            } else {
                memoryMB = dynamicProperty->val->int32;
            }

            break;
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);

    return memoryMB * 1024; /* Scale from megabyte to kilobyte */
}



static int
esxDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
M
Matthias Bolte 已提交
2048
    int result = -1;
M
Matthias Bolte 已提交
2049
    esxPrivate *priv = domain->conn->privateData;
2050
    esxVI_String *propertyNameList = NULL;
2051
    esxVI_ObjectContent *virtualMachine = NULL;
2052
    esxVI_VirtualMachinePowerState powerState;
2053 2054 2055
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
2056
    char *taskInfoErrorMessage = NULL;
2057

2058
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2059
        return -1;
2060 2061
    }

2062 2063 2064 2065
    if (esxVI_String_AppendValueToList(&propertyNameList,
                                       "runtime.powerState") < 0 ||
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
2066
           priv->parsedUri->autoAnswer) < 0 ||
2067 2068 2069 2070 2071
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
        goto cleanup;
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
2072 2073
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered off"));
2074 2075 2076 2077
        goto cleanup;
    }

    if (esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
2078
        esxVI_Long_Alloc(&spec->memoryMB) < 0) {
M
Matthias Bolte 已提交
2079
        goto cleanup;
2080 2081
    }

2082
    /* max-memory must be a multiple of 4096 kilobyte */
2083
    spec->memoryMB->value =
2084
      VIR_DIV_UP(memory, 4096) * 4; /* Scale from kilobytes to megabytes */
2085

2086
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
2087
                              &task) < 0 ||
2088
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
2089
                                    esxVI_Occurrence_RequiredItem,
2090
                                    priv->parsedUri->autoAnswer, &taskInfoState,
2091
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
2092
        goto cleanup;
2093 2094 2095
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2096 2097 2098
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not set max-memory to %lu kilobytes: %s"), memory,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2099
        goto cleanup;
2100 2101
    }

M
Matthias Bolte 已提交
2102 2103
    result = 0;

2104
  cleanup:
2105
    esxVI_String_Free(&propertyNameList);
2106 2107 2108
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
2109
    VIR_FREE(taskInfoErrorMessage);
2110 2111 2112 2113 2114 2115 2116 2117 2118

    return result;
}



static int
esxDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
M
Matthias Bolte 已提交
2119
    int result = -1;
M
Matthias Bolte 已提交
2120
    esxPrivate *priv = domain->conn->privateData;
2121 2122 2123 2124
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
2125
    char *taskInfoErrorMessage = NULL;
2126

2127
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2128
        return -1;
2129 2130
    }

2131
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
2132
          (priv->primary, domain->uuid, NULL, &virtualMachine,
2133
           priv->parsedUri->autoAnswer) < 0 ||
2134 2135 2136
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_ResourceAllocationInfo_Alloc(&spec->memoryAllocation) < 0 ||
        esxVI_Long_Alloc(&spec->memoryAllocation->limit) < 0) {
M
Matthias Bolte 已提交
2137
        goto cleanup;
2138 2139 2140
    }

    spec->memoryAllocation->limit->value =
2141
      VIR_DIV_UP(memory, 1024); /* Scale from kilobytes to megabytes */
2142

2143
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
2144
                              &task) < 0 ||
2145
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
2146
                                    esxVI_Occurrence_RequiredItem,
2147
                                    priv->parsedUri->autoAnswer, &taskInfoState,
2148
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
2149
        goto cleanup;
2150 2151 2152
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2153 2154 2155
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not set memory to %lu kilobytes: %s"), memory,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2156
        goto cleanup;
2157 2158
    }

M
Matthias Bolte 已提交
2159 2160
    result = 0;

2161 2162 2163 2164
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
2165
    VIR_FREE(taskInfoErrorMessage);
2166 2167 2168 2169 2170 2171

    return result;
}



2172 2173 2174 2175 2176 2177 2178 2179 2180
/*
 * libvirt exposed virtual CPU usage in absolute time, ESX doesn't provide this
 * information in this format. It exposes it in 20 seconds slots, but it's hard
 * to get a reliable absolute time from this. Therefore, disable the code that
 * queries the performance counters here for now, but keep it as example for how
 * to query a selected performance counter for its values.
 */
#define ESX_QUERY_FOR_USED_CPU_TIME 0

2181 2182 2183
static int
esxDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
M
Matthias Bolte 已提交
2184
    int result = -1;
M
Matthias Bolte 已提交
2185
    esxPrivate *priv = domain->conn->privateData;
2186 2187 2188 2189 2190
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_VirtualMachinePowerState powerState;
    int64_t memory_limit = -1;
2191
#if ESX_QUERY_FOR_USED_CPU_TIME
2192 2193 2194 2195 2196 2197 2198
    esxVI_PerfMetricId *perfMetricId = NULL;
    esxVI_PerfMetricId *perfMetricIdList = NULL;
    esxVI_Int *counterId = NULL;
    esxVI_Int *counterIdList = NULL;
    esxVI_PerfCounterInfo *perfCounterInfo = NULL;
    esxVI_PerfCounterInfo *perfCounterInfoList = NULL;
    esxVI_PerfQuerySpec *querySpec = NULL;
2199 2200
    esxVI_PerfEntityMetricBase *perfEntityMetricBase = NULL;
    esxVI_PerfEntityMetricBase *perfEntityMetricBaseList = NULL;
2201 2202 2203
    esxVI_PerfEntityMetric *perfEntityMetric = NULL;
    esxVI_PerfMetricIntSeries *perfMetricIntSeries = NULL;
    esxVI_Long *value = NULL;
2204
#endif
2205

2206
    memset(info, 0, sizeof(*info));
M
Matthias Bolte 已提交
2207

2208
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2209
        return -1;
2210 2211
    }

2212
    if (esxVI_String_AppendValueListToList(&propertyNameList,
2213 2214 2215 2216
                                           "runtime.powerState\0"
                                           "config.hardware.memoryMB\0"
                                           "config.hardware.numCPU\0"
                                           "config.memoryAllocation.limit\0") < 0 ||
2217
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
2218
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
2219
                                         esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
2220
        goto cleanup;
2221 2222 2223 2224
    }

    info->state = VIR_DOMAIN_NOSTATE;

2225
    for (dynamicProperty = virtualMachine->propSet; dynamicProperty;
2226 2227 2228
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "runtime.powerState")) {
            if (esxVI_VirtualMachinePowerState_CastFromAnyType
2229
                  (dynamicProperty->val, &powerState) < 0) {
M
Matthias Bolte 已提交
2230
                goto cleanup;
2231 2232
            }

2233 2234
            info->state = esxVI_VirtualMachinePowerState_ConvertToLibvirt
                            (powerState);
2235
        } else if (STREQ(dynamicProperty->name, "config.hardware.memoryMB")) {
2236
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2237
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
2238
                goto cleanup;
2239 2240 2241 2242
            }

            info->maxMem = dynamicProperty->val->int32 * 1024; /* Scale from megabyte to kilobyte */
        } else if (STREQ(dynamicProperty->name, "config.hardware.numCPU")) {
2243
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2244
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
2245
                goto cleanup;
2246 2247 2248 2249 2250
            }

            info->nrVirtCpu = dynamicProperty->val->int32;
        } else if (STREQ(dynamicProperty->name,
                         "config.memoryAllocation.limit")) {
2251
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2252
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
2253
                goto cleanup;
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
            }

            memory_limit = dynamicProperty->val->int64;

            if (memory_limit > 0) {
                memory_limit *= 1024; /* Scale from megabyte to kilobyte */
            }
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

    /* memory_limit < 0 means no memory limit is set */
    info->memory = memory_limit < 0 ? info->maxMem : memory_limit;

2269
#if ESX_QUERY_FOR_USED_CPU_TIME
2270
    /* Verify the cached 'used CPU time' performance counter ID */
2271
    /* FIXME: Currently no host for a vpx:// connection */
2272
    if (priv->host) {
2273 2274 2275 2276
        if (info->state == VIR_DOMAIN_RUNNING && priv->usedCpuTimeCounterId >= 0) {
            if (esxVI_Int_Alloc(&counterId) < 0) {
                goto cleanup;
            }
2277

2278
            counterId->value = priv->usedCpuTimeCounterId;
2279

2280 2281 2282
            if (esxVI_Int_AppendToList(&counterIdList, counterId) < 0) {
                goto cleanup;
            }
2283

2284 2285 2286 2287
            if (esxVI_QueryPerfCounter(priv->host, counterIdList,
                                       &perfCounterInfo) < 0) {
                goto cleanup;
            }
2288

2289 2290 2291 2292 2293
            if (STRNEQ(perfCounterInfo->groupInfo->key, "cpu") ||
                STRNEQ(perfCounterInfo->nameInfo->key, "used") ||
                STRNEQ(perfCounterInfo->unitInfo->key, "millisecond")) {
                VIR_DEBUG("Cached usedCpuTimeCounterId %d is invalid",
                          priv->usedCpuTimeCounterId);
2294

2295 2296 2297 2298 2299
                priv->usedCpuTimeCounterId = -1;
            }

            esxVI_Int_Free(&counterIdList);
            esxVI_PerfCounterInfo_Free(&perfCounterInfo);
2300 2301
        }

2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
        /*
         * Query the PerformanceManager for the 'used CPU time' performance
         * counter ID and cache it, if it's not already cached.
         */
        if (info->state == VIR_DOMAIN_RUNNING && priv->usedCpuTimeCounterId < 0) {
            if (esxVI_QueryAvailablePerfMetric(priv->host, virtualMachine->obj,
                                               NULL, NULL, NULL,
                                               &perfMetricIdList) < 0) {
                goto cleanup;
            }
2312

2313
            for (perfMetricId = perfMetricIdList; perfMetricId;
2314 2315 2316
                 perfMetricId = perfMetricId->_next) {
                VIR_DEBUG("perfMetricId counterId %d, instance '%s'",
                          perfMetricId->counterId->value, perfMetricId->instance);
2317

2318
                counterId = NULL;
2319

2320 2321 2322 2323 2324
                if (esxVI_Int_DeepCopy(&counterId, perfMetricId->counterId) < 0 ||
                    esxVI_Int_AppendToList(&counterIdList, counterId) < 0) {
                    goto cleanup;
                }
            }
2325

2326 2327
            if (esxVI_QueryPerfCounter(priv->host, counterIdList,
                                       &perfCounterInfoList) < 0) {
M
Matthias Bolte 已提交
2328
                goto cleanup;
2329 2330
            }

2331
            for (perfCounterInfo = perfCounterInfoList; perfCounterInfo;
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
                 perfCounterInfo = perfCounterInfo->_next) {
                VIR_DEBUG("perfCounterInfo key %d, nameInfo '%s', groupInfo '%s', "
                          "unitInfo '%s', rollupType %d, statsType %d",
                          perfCounterInfo->key->value,
                          perfCounterInfo->nameInfo->key,
                          perfCounterInfo->groupInfo->key,
                          perfCounterInfo->unitInfo->key,
                          perfCounterInfo->rollupType,
                          perfCounterInfo->statsType);

                if (STREQ(perfCounterInfo->groupInfo->key, "cpu") &&
                    STREQ(perfCounterInfo->nameInfo->key, "used") &&
                    STREQ(perfCounterInfo->unitInfo->key, "millisecond")) {
                    priv->usedCpuTimeCounterId = perfCounterInfo->key->value;
                    break;
                }
2348 2349
            }

2350
            if (priv->usedCpuTimeCounterId < 0) {
2351
                VIR_WARN("Could not find 'used CPU time' performance counter");
2352
            }
2353 2354
        }

2355 2356 2357 2358 2359 2360
        /*
         * Query the PerformanceManager for the 'used CPU time' performance
         * counter value.
         */
        if (info->state == VIR_DOMAIN_RUNNING && priv->usedCpuTimeCounterId >= 0) {
            VIR_DEBUG("usedCpuTimeCounterId %d BEGIN", priv->usedCpuTimeCounterId);
2361

2362 2363 2364 2365 2366 2367
            if (esxVI_PerfQuerySpec_Alloc(&querySpec) < 0 ||
                esxVI_Int_Alloc(&querySpec->maxSample) < 0 ||
                esxVI_PerfMetricId_Alloc(&querySpec->metricId) < 0 ||
                esxVI_Int_Alloc(&querySpec->metricId->counterId) < 0) {
                goto cleanup;
            }
2368

2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
            querySpec->entity = virtualMachine->obj;
            querySpec->maxSample->value = 1;
            querySpec->metricId->counterId->value = priv->usedCpuTimeCounterId;
            querySpec->metricId->instance = (char *)"";
            querySpec->format = (char *)"normal";

            if (esxVI_QueryPerf(priv->host, querySpec,
                                &perfEntityMetricBaseList) < 0) {
                goto cleanup;
            }
2379

2380
            for (perfEntityMetricBase = perfEntityMetricBaseList;
2381
                 perfEntityMetricBase;
2382
                 perfEntityMetricBase = perfEntityMetricBase->_next) {
2383
                VIR_DEBUG("perfEntityMetric ...");
2384

2385 2386
                perfEntityMetric =
                  esxVI_PerfEntityMetric_DynamicCast(perfEntityMetricBase);
2387

2388
                if (!perfEntityMetric) {
2389 2390 2391
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("QueryPerf returned object with unexpected type '%s'"),
                                   esxVI_Type_ToString(perfEntityMetricBase->_type));
2392
                    goto cleanup;
2393
                }
2394

2395 2396
                perfMetricIntSeries =
                  esxVI_PerfMetricIntSeries_DynamicCast(perfEntityMetric->value);
2397

2398
                if (!perfMetricIntSeries) {
2399 2400 2401
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("QueryPerf returned object with unexpected type '%s'"),
                                   esxVI_Type_ToString(perfEntityMetric->value->_type));
2402
                    goto cleanup;
2403
                }
2404

2405
                for (; perfMetricIntSeries;
2406
                     perfMetricIntSeries = perfMetricIntSeries->_next) {
2407
                    VIR_DEBUG("perfMetricIntSeries ...");
2408

2409
                    for (value = perfMetricIntSeries->value;
2410
                         value;
2411 2412 2413
                         value = value->_next) {
                        VIR_DEBUG("value %lld", (long long int)value->value);
                    }
2414 2415 2416
                }
            }

2417
            VIR_DEBUG("usedCpuTimeCounterId %d END", priv->usedCpuTimeCounterId);
M
Matthias Bolte 已提交
2418

2419
            /*
E
Eric Blake 已提交
2420
             * FIXME: Cannot map between relative used-cpu-time and absolute
2421 2422 2423
             *        info->cpuTime
             */
        }
2424
    }
2425
#endif
2426

M
Matthias Bolte 已提交
2427 2428
    result = 0;

2429
  cleanup:
2430
#if ESX_QUERY_FOR_USED_CPU_TIME
2431 2432 2433 2434
    /*
     * Remove values owned by data structures to prevent them from being freed
     * by the call to esxVI_PerfQuerySpec_Free().
     */
2435
    if (querySpec) {
2436 2437 2438
        querySpec->entity = NULL;
        querySpec->format = NULL;

2439
        if (querySpec->metricId) {
2440 2441 2442
            querySpec->metricId->instance = NULL;
        }
    }
2443
#endif
2444

2445 2446
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);
2447
#if ESX_QUERY_FOR_USED_CPU_TIME
2448 2449 2450 2451
    esxVI_PerfMetricId_Free(&perfMetricIdList);
    esxVI_Int_Free(&counterIdList);
    esxVI_PerfCounterInfo_Free(&perfCounterInfoList);
    esxVI_PerfQuerySpec_Free(&querySpec);
2452
    esxVI_PerfEntityMetricBase_Free(&perfEntityMetricBaseList);
2453
#endif
2454 2455 2456 2457 2458 2459

    return result;
}



2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
static int
esxDomainGetState(virDomainPtr domain,
                  int *state,
                  int *reason,
                  unsigned int flags)
{
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;

    virCheckFlags(0, -1);

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

    if (esxVI_String_AppendValueToList(&propertyNameList,
                                       "runtime.powerState") < 0 ||
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
                                         propertyNameList, &virtualMachine,
                                         esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
        goto cleanup;
    }

    *state = esxVI_VirtualMachinePowerState_ConvertToLibvirt(powerState);

    if (reason)
        *reason = 0;

    result = 0;

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
}



2503
static int
2504 2505
esxDomainSetVcpusFlags(virDomainPtr domain, unsigned int nvcpus,
                       unsigned int flags)
2506
{
M
Matthias Bolte 已提交
2507
    int result = -1;
M
Matthias Bolte 已提交
2508
    esxPrivate *priv = domain->conn->privateData;
M
Matthias Bolte 已提交
2509
    int maxVcpus;
2510 2511 2512 2513
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
2514
    char *taskInfoErrorMessage = NULL;
2515

2516
    if (flags != VIR_DOMAIN_AFFECT_LIVE) {
2517
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
2518 2519 2520
        return -1;
    }

2521
    if (nvcpus < 1) {
2522 2523
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Requested number of virtual CPUs must at least be 1"));
M
Matthias Bolte 已提交
2524
        return -1;
2525 2526
    }

2527
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2528
        return -1;
2529 2530
    }

M
Matthias Bolte 已提交
2531
    maxVcpus = esxDomainGetMaxVcpus(domain);
2532

M
Matthias Bolte 已提交
2533
    if (maxVcpus < 0) {
M
Matthias Bolte 已提交
2534
        return -1;
2535 2536
    }

M
Matthias Bolte 已提交
2537
    if (nvcpus > maxVcpus) {
2538 2539 2540 2541
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Requested number of virtual CPUs is greater than max "
                         "allowable number of virtual CPUs for the domain: %d > %d"),
                       nvcpus, maxVcpus);
M
Matthias Bolte 已提交
2542
        return -1;
2543 2544
    }

2545
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
2546
          (priv->primary, domain->uuid, NULL, &virtualMachine,
2547
           priv->parsedUri->autoAnswer) < 0 ||
2548 2549
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_Int_Alloc(&spec->numCPUs) < 0) {
M
Matthias Bolte 已提交
2550
        goto cleanup;
2551 2552 2553 2554
    }

    spec->numCPUs->value = nvcpus;

2555
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
2556
                              &task) < 0 ||
2557
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
2558
                                    esxVI_Occurrence_RequiredItem,
2559
                                    priv->parsedUri->autoAnswer, &taskInfoState,
2560
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
2561
        goto cleanup;
2562 2563 2564
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2565 2566 2567
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not set number of virtual CPUs to %d: %s"), nvcpus,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2568
        goto cleanup;
2569 2570
    }

M
Matthias Bolte 已提交
2571 2572
    result = 0;

2573 2574 2575 2576
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
2577
    VIR_FREE(taskInfoErrorMessage);
2578 2579 2580 2581 2582

    return result;
}


M
Matthias Bolte 已提交
2583

2584 2585 2586
static int
esxDomainSetVcpus(virDomainPtr domain, unsigned int nvcpus)
{
2587
    return esxDomainSetVcpusFlags(domain, nvcpus, VIR_DOMAIN_AFFECT_LIVE);
2588 2589
}

2590

M
Matthias Bolte 已提交
2591

2592
static int
2593
esxDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
2594
{
M
Matthias Bolte 已提交
2595
    esxPrivate *priv = domain->conn->privateData;
2596 2597 2598 2599
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;

2600
    if (flags != (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
2601
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
2602 2603 2604
        return -1;
    }

M
Matthias Bolte 已提交
2605 2606
    if (priv->maxVcpus > 0) {
        return priv->maxVcpus;
2607 2608
    }

M
Matthias Bolte 已提交
2609 2610
    priv->maxVcpus = -1;

2611
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2612
        return -1;
2613 2614
    }

2615
    if (esxVI_String_AppendValueToList(&propertyNameList,
2616
                                       "capability.maxSupportedVcpus") < 0 ||
2617 2618
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
2619
        goto cleanup;
2620 2621
    }

2622
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
2623 2624
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "capability.maxSupportedVcpus")) {
2625
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2626
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
2627
                goto cleanup;
2628 2629
            }

M
Matthias Bolte 已提交
2630
            priv->maxVcpus = dynamicProperty->val->int32;
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
            break;
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

M
Matthias Bolte 已提交
2641
    return priv->maxVcpus;
2642 2643
}

M
Matthias Bolte 已提交
2644 2645


2646 2647 2648
static int
esxDomainGetMaxVcpus(virDomainPtr domain)
{
2649
    return esxDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2650 2651
                                           VIR_DOMAIN_VCPU_MAXIMUM));
}
2652

M
Matthias Bolte 已提交
2653 2654


2655
static char *
2656
esxDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2657
{
M
Matthias Bolte 已提交
2658
    esxPrivate *priv = domain->conn->privateData;
2659 2660
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
2661 2662
    esxVI_VirtualMachinePowerState powerState;
    int id;
2663
    char *vmPathName = NULL;
2664
    char *datastoreName = NULL;
M
Matthias Bolte 已提交
2665
    char *directoryName = NULL;
2666
    char *directoryAndFileName = NULL;
2667
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
2668 2669
    char *url = NULL;
    char *vmx = NULL;
2670
    virVMXContext ctx;
2671
    esxVMX_Data data;
2672 2673 2674
    virDomainDefPtr def = NULL;
    char *xml = NULL;

E
Eric Blake 已提交
2675 2676
    /* Flags checked by virDomainDefFormat */

2677
    memset(&data, 0, sizeof(data));
2678

2679
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2680
        return NULL;
2681 2682
    }

2683 2684 2685
    if (esxVI_String_AppendValueListToList(&propertyNameList,
                                           "config.files.vmPathName\0"
                                           "runtime.powerState\0") < 0 ||
2686
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
2687
                                         propertyNameList, &virtualMachine,
2688
                                         esxVI_Occurrence_RequiredItem) < 0 ||
2689 2690
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0 ||
        esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, NULL) < 0 ||
2691 2692
        esxVI_GetStringValue(virtualMachine, "config.files.vmPathName",
                             &vmPathName, esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
2693
        goto cleanup;
2694 2695
    }

2696
    if (esxUtil_ParseDatastorePath(vmPathName, &datastoreName, &directoryName,
2697
                                   &directoryAndFileName) < 0) {
M
Matthias Bolte 已提交
2698
        goto cleanup;
2699 2700
    }

2701
    virBufferAsprintf(&buffer, "%s://%s:%d/folder/", priv->parsedUri->transport,
2702
                      domain->conn->uri->server, domain->conn->uri->port);
2703
    virBufferURIEncodeString(&buffer, directoryAndFileName);
2704
    virBufferAddLit(&buffer, "?dcPath=");
2705
    virBufferURIEncodeString(&buffer, priv->primary->datacenterPath);
2706 2707 2708 2709
    virBufferAddLit(&buffer, "&dsName=");
    virBufferURIEncodeString(&buffer, datastoreName);

    if (virBufferError(&buffer)) {
2710
        virReportOOMError();
M
Matthias Bolte 已提交
2711
        goto cleanup;
2712 2713
    }

2714 2715
    url = virBufferContentAndReset(&buffer);

2716
    if (esxVI_CURL_Download(priv->primary->curl, url, &vmx, 0, NULL) < 0) {
M
Matthias Bolte 已提交
2717
        goto cleanup;
2718 2719
    }

2720
    data.ctx = priv->primary;
2721

2722
    if (!directoryName) {
2723
        if (virAsprintf(&data.datastorePathWithoutFileName, "[%s]",
2724
                        datastoreName) < 0)
2725 2726 2727
            goto cleanup;
    } else {
        if (virAsprintf(&data.datastorePathWithoutFileName, "[%s] %s",
2728
                        datastoreName, directoryName) < 0)
2729 2730
            goto cleanup;
    }
2731 2732 2733 2734 2735 2736

    ctx.opaque = &data;
    ctx.parseFileName = esxParseVMXFileName;
    ctx.formatFileName = NULL;
    ctx.autodetectSCSIControllerModel = NULL;

2737
    def = virVMXParseConfig(&ctx, priv->xmlopt, vmx);
2738

2739
    if (def) {
2740 2741 2742 2743
        if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
            def->id = id;
        }

2744
        xml = virDomainDefFormat(def, flags);
2745 2746 2747
    }

  cleanup:
2748
    if (!url) {
M
Matthias Bolte 已提交
2749 2750 2751
        virBufferFreeAndReset(&buffer);
    }

2752 2753
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);
2754
    VIR_FREE(datastoreName);
M
Matthias Bolte 已提交
2755
    VIR_FREE(directoryName);
2756
    VIR_FREE(directoryAndFileName);
2757
    VIR_FREE(url);
2758
    VIR_FREE(data.datastorePathWithoutFileName);
2759
    VIR_FREE(vmx);
2760
    virDomainDefFree(def);
2761 2762 2763 2764 2765 2766 2767

    return xml;
}



static char *
2768 2769 2770
esxConnectDomainXMLFromNative(virConnectPtr conn, const char *nativeFormat,
                              const char *nativeConfig,
                              unsigned int flags)
2771
{
M
Matthias Bolte 已提交
2772
    esxPrivate *priv = conn->privateData;
2773
    virVMXContext ctx;
2774
    esxVMX_Data data;
2775 2776 2777
    virDomainDefPtr def = NULL;
    char *xml = NULL;

E
Eric Blake 已提交
2778 2779
    virCheckFlags(0, NULL);

2780
    memset(&data, 0, sizeof(data));
2781

2782
    if (STRNEQ(nativeFormat, "vmware-vmx")) {
2783 2784
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Unsupported config format '%s'"), nativeFormat);
2785
        return NULL;
2786 2787
    }

2788
    data.ctx = priv->primary;
2789
    data.datastorePathWithoutFileName = (char *)"[?] ?";
2790 2791 2792 2793 2794 2795

    ctx.opaque = &data;
    ctx.parseFileName = esxParseVMXFileName;
    ctx.formatFileName = NULL;
    ctx.autodetectSCSIControllerModel = NULL;

2796
    def = virVMXParseConfig(&ctx, priv->xmlopt, nativeConfig);
2797

2798
    if (def) {
2799
        xml = virDomainDefFormat(def, VIR_DOMAIN_XML_INACTIVE);
2800 2801 2802 2803 2804 2805 2806 2807 2808
    }

    virDomainDefFree(def);

    return xml;
}



M
Matthias Bolte 已提交
2809
static char *
2810 2811 2812
esxConnectDomainXMLToNative(virConnectPtr conn, const char *nativeFormat,
                            const char *domainXml,
                            unsigned int flags)
M
Matthias Bolte 已提交
2813
{
M
Matthias Bolte 已提交
2814
    esxPrivate *priv = conn->privateData;
2815 2816
    int virtualHW_version;
    virVMXContext ctx;
2817
    esxVMX_Data data;
M
Matthias Bolte 已提交
2818 2819 2820
    virDomainDefPtr def = NULL;
    char *vmx = NULL;

E
Eric Blake 已提交
2821 2822
    virCheckFlags(0, NULL);

2823
    memset(&data, 0, sizeof(data));
2824

M
Matthias Bolte 已提交
2825
    if (STRNEQ(nativeFormat, "vmware-vmx")) {
2826 2827
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Unsupported config format '%s'"), nativeFormat);
M
Matthias Bolte 已提交
2828 2829 2830
        return NULL;
    }

2831 2832 2833 2834 2835 2836 2837
    virtualHW_version = esxVI_ProductVersionToDefaultVirtualHWVersion
                          (priv->primary->productVersion);

    if (virtualHW_version < 0) {
        return NULL;
    }

2838 2839
    def = virDomainDefParseString(domainXml, priv->caps, priv->xmlopt,
                                  1 << VIR_DOMAIN_VIRT_VMWARE, 0);
M
Matthias Bolte 已提交
2840

2841
    if (!def) {
M
Matthias Bolte 已提交
2842 2843 2844
        return NULL;
    }

2845
    data.ctx = priv->primary;
2846
    data.datastorePathWithoutFileName = NULL;
2847 2848 2849 2850 2851 2852

    ctx.opaque = &data;
    ctx.parseFileName = NULL;
    ctx.formatFileName = esxFormatVMXFileName;
    ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;

2853
    vmx = virVMXFormatConfig(&ctx, priv->xmlopt, def, virtualHW_version);
M
Matthias Bolte 已提交
2854 2855 2856 2857 2858 2859 2860 2861

    virDomainDefFree(def);

    return vmx;
}



2862
static int
2863
esxConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
2864
{
M
Matthias Bolte 已提交
2865
    bool success = false;
M
Matthias Bolte 已提交
2866
    esxPrivate *priv = conn->privateData;
2867 2868 2869 2870 2871
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
    int count = 0;
2872
    size_t i;
2873 2874 2875 2876 2877

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

2878
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2879
        return -1;
2880 2881
    }

2882
    if (esxVI_String_AppendValueListToList(&propertyNameList,
2883 2884
                                           "name\0"
                                           "runtime.powerState\0") < 0 ||
2885 2886
        esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
                                       &virtualMachineList) < 0) {
M
Matthias Bolte 已提交
2887
        goto cleanup;
2888 2889
    }

2890
    for (virtualMachine = virtualMachineList; virtualMachine;
2891
         virtualMachine = virtualMachine->_next) {
2892
        if (esxVI_GetVirtualMachinePowerState(virtualMachine,
2893
                                              &powerState) < 0) {
M
Matthias Bolte 已提交
2894
            goto cleanup;
2895 2896 2897 2898 2899 2900
        }

        if (powerState == esxVI_VirtualMachinePowerState_PoweredOn) {
            continue;
        }

2901
        names[count] = NULL;
2902

2903 2904 2905
        if (esxVI_GetVirtualMachineIdentity(virtualMachine, NULL, &names[count],
                                            NULL) < 0) {
            goto cleanup;
2906 2907
        }

2908 2909
        ++count;

2910 2911 2912 2913 2914
        if (count >= maxnames) {
            break;
        }
    }

M
Matthias Bolte 已提交
2915
    success = true;
2916

M
Matthias Bolte 已提交
2917 2918 2919 2920 2921
  cleanup:
    if (! success) {
        for (i = 0; i < count; ++i) {
            VIR_FREE(names[i]);
        }
2922

M
Matthias Bolte 已提交
2923
        count = -1;
2924 2925
    }

M
Matthias Bolte 已提交
2926 2927
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);
2928

M
Matthias Bolte 已提交
2929
    return count;
2930 2931 2932 2933 2934
}



static int
2935
esxConnectNumOfDefinedDomains(virConnectPtr conn)
2936
{
M
Matthias Bolte 已提交
2937
    esxPrivate *priv = conn->privateData;
2938

2939
    if (esxVI_EnsureSession(priv->primary) < 0) {
2940 2941 2942
        return -1;
    }

2943
    return esxVI_LookupNumberOfDomainsByPowerState
2944
             (priv->primary, esxVI_VirtualMachinePowerState_PoweredOn, true);
2945 2946 2947 2948 2949
}



static int
2950
esxDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
2951
{
M
Matthias Bolte 已提交
2952
    int result = -1;
M
Matthias Bolte 已提交
2953
    esxPrivate *priv = domain->conn->privateData;
2954 2955 2956
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
2957
    int id = -1;
2958 2959
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
2960
    char *taskInfoErrorMessage = NULL;
2961

2962 2963
    virCheckFlags(0, -1);

2964
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
2965
        return -1;
2966 2967
    }

2968
    if (esxVI_String_AppendValueToList(&propertyNameList,
2969
                                       "runtime.powerState") < 0 ||
2970
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
2971
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
2972
           priv->parsedUri->autoAnswer) < 0 ||
2973 2974
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0 ||
        esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, NULL) < 0) {
M
Matthias Bolte 已提交
2975
        goto cleanup;
2976 2977 2978
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
2979 2980
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered off"));
M
Matthias Bolte 已提交
2981
        goto cleanup;
2982 2983
    }

2984
    if (esxVI_PowerOnVM_Task(priv->primary, virtualMachine->obj, NULL,
2985
                             &task) < 0 ||
2986
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
2987
                                    esxVI_Occurrence_RequiredItem,
2988
                                    priv->parsedUri->autoAnswer, &taskInfoState,
2989
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
2990
        goto cleanup;
2991 2992 2993
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2994 2995
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not start domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2996
        goto cleanup;
2997 2998
    }

2999
    domain->id = id;
M
Matthias Bolte 已提交
3000 3001
    result = 0;

3002 3003 3004 3005
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
3006
    VIR_FREE(taskInfoErrorMessage);
3007 3008 3009 3010

    return result;
}

3011 3012


3013 3014 3015 3016 3017
static int
esxDomainCreate(virDomainPtr domain)
{
    return esxDomainCreateWithFlags(domain, 0);
}
3018

3019 3020


M
Matthias Bolte 已提交
3021
static virDomainPtr
3022
esxDomainDefineXML(virConnectPtr conn, const char *xml)
M
Matthias Bolte 已提交
3023
{
M
Matthias Bolte 已提交
3024
    esxPrivate *priv = conn->privateData;
M
Matthias Bolte 已提交
3025 3026
    virDomainDefPtr def = NULL;
    char *vmx = NULL;
3027
    size_t i;
3028
    virDomainDiskDefPtr disk = NULL;
M
Matthias Bolte 已提交
3029
    esxVI_ObjectContent *virtualMachine = NULL;
3030 3031
    int virtualHW_version;
    virVMXContext ctx;
3032
    esxVMX_Data data;
M
Matthias Bolte 已提交
3033 3034
    char *datastoreName = NULL;
    char *directoryName = NULL;
3035
    char *escapedName = NULL;
M
Matthias Bolte 已提交
3036 3037 3038 3039 3040 3041 3042 3043
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *url = NULL;
    char *datastoreRelatedPath = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_ManagedObjectReference *resourcePool = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
3044
    char *taskInfoErrorMessage = NULL;
M
Matthias Bolte 已提交
3045 3046
    virDomainPtr domain = NULL;

3047
    memset(&data, 0, sizeof(data));
3048

3049
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
3050
        return NULL;
M
Matthias Bolte 已提交
3051 3052 3053
    }

    /* Parse domain XML */
3054 3055
    def = virDomainDefParseString(xml, priv->caps, priv->xmlopt,
                                  1 << VIR_DOMAIN_VIRT_VMWARE,
M
Matthias Bolte 已提交
3056 3057
                                  VIR_DOMAIN_XML_INACTIVE);

3058
    if (!def) {
M
Matthias Bolte 已提交
3059
        return NULL;
M
Matthias Bolte 已提交
3060 3061 3062
    }

    /* Check if an existing domain should be edited */
3063
    if (esxVI_LookupVirtualMachineByUuid(priv->primary, def->uuid, NULL,
M
Matthias Bolte 已提交
3064
                                         &virtualMachine,
M
Matthias Bolte 已提交
3065
                                         esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
3066
        goto cleanup;
M
Matthias Bolte 已提交
3067 3068
    }

3069
    if (!virtualMachine &&
3070 3071 3072 3073 3074 3075
        esxVI_LookupVirtualMachineByName(priv->primary, def->name, NULL,
                                         &virtualMachine,
                                         esxVI_Occurrence_OptionalItem) < 0) {
        goto cleanup;
    }

3076
    if (virtualMachine) {
M
Matthias Bolte 已提交
3077
        /* FIXME */
3078 3079 3080
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain already exists, editing existing domains is not "
                         "supported yet"));
M
Matthias Bolte 已提交
3081
        goto cleanup;
M
Matthias Bolte 已提交
3082 3083 3084
    }

    /* Build VMX from domain XML */
3085 3086 3087 3088 3089 3090 3091
    virtualHW_version = esxVI_ProductVersionToDefaultVirtualHWVersion
                          (priv->primary->productVersion);

    if (virtualHW_version < 0) {
        goto cleanup;
    }

3092
    data.ctx = priv->primary;
3093
    data.datastorePathWithoutFileName = NULL;
3094 3095 3096 3097 3098 3099

    ctx.opaque = &data;
    ctx.parseFileName = NULL;
    ctx.formatFileName = esxFormatVMXFileName;
    ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;

3100
    vmx = virVMXFormatConfig(&ctx, priv->xmlopt, def, virtualHW_version);
M
Matthias Bolte 已提交
3101

3102
    if (!vmx) {
M
Matthias Bolte 已提交
3103
        goto cleanup;
M
Matthias Bolte 已提交
3104 3105
    }

3106 3107 3108 3109 3110 3111 3112
    /*
     * Build VMX datastore URL. Use the source of the first file-based harddisk
     * to deduce the datastore and path for the VMX file. Don't just use the
     * first disk, because it may be CDROM disk and ISO images are normaly not
     * located in the virtual machine's directory. This approach to deduce the
     * datastore isn't perfect but should work in the majority of cases.
     */
M
Matthias Bolte 已提交
3113
    if (def->ndisks < 1) {
3114 3115 3116
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain XML doesn't contain any disks, cannot deduce "
                         "datastore and path for VMX file"));
M
Matthias Bolte 已提交
3117
        goto cleanup;
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
    }

    for (i = 0; i < def->ndisks; ++i) {
        if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
            def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
            disk = def->disks[i];
            break;
        }
    }

3128
    if (!disk) {
3129 3130 3131
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain XML doesn't contain any file-based harddisks, "
                         "cannot deduce datastore and path for VMX file"));
M
Matthias Bolte 已提交
3132
        goto cleanup;
M
Matthias Bolte 已提交
3133 3134
    }

3135
    if (!disk->src) {
3136 3137 3138
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("First file-based harddisk has no source, cannot deduce "
                         "datastore and path for VMX file"));
M
Matthias Bolte 已提交
3139
        goto cleanup;
M
Matthias Bolte 已提交
3140 3141
    }

3142
    if (esxUtil_ParseDatastorePath(disk->src, &datastoreName, &directoryName,
3143
                                   NULL) < 0) {
M
Matthias Bolte 已提交
3144
        goto cleanup;
M
Matthias Bolte 已提交
3145 3146
    }

3147
    if (! virFileHasSuffix(disk->src, ".vmdk")) {
3148 3149 3150
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting source '%s' of first file-based harddisk to "
                         "be a VMDK image"), disk->src);
M
Matthias Bolte 已提交
3151
        goto cleanup;
M
Matthias Bolte 已提交
3152 3153
    }

3154
    virBufferAsprintf(&buffer, "%s://%s:%d/folder/", priv->parsedUri->transport,
M
Matthias Bolte 已提交
3155 3156
                      conn->uri->server, conn->uri->port);

3157
    if (directoryName) {
M
Matthias Bolte 已提交
3158 3159 3160 3161
        virBufferURIEncodeString(&buffer, directoryName);
        virBufferAddChar(&buffer, '/');
    }

3162 3163
    escapedName = esxUtil_EscapeDatastoreItem(def->name);

3164
    if (!escapedName) {
3165 3166 3167 3168
        goto cleanup;
    }

    virBufferURIEncodeString(&buffer, escapedName);
M
Matthias Bolte 已提交
3169
    virBufferAddLit(&buffer, ".vmx?dcPath=");
3170
    virBufferURIEncodeString(&buffer, priv->primary->datacenterPath);
M
Matthias Bolte 已提交
3171 3172 3173 3174
    virBufferAddLit(&buffer, "&dsName=");
    virBufferURIEncodeString(&buffer, datastoreName);

    if (virBufferError(&buffer)) {
3175
        virReportOOMError();
M
Matthias Bolte 已提交
3176
        goto cleanup;
M
Matthias Bolte 已提交
3177 3178 3179 3180
    }

    url = virBufferContentAndReset(&buffer);

3181 3182 3183 3184 3185 3186
    /* Check, if VMX file already exists */
    /* FIXME */

    /* Upload VMX file */
    VIR_DEBUG("Uploading .vmx config, url='%s' vmx='%s'", url, vmx);

3187
    if (esxVI_CURL_Upload(priv->primary->curl, url, vmx) < 0) {
3188 3189 3190 3191
        goto cleanup;
    }

    /* Register the domain */
3192
    if (directoryName) {
M
Matthias Bolte 已提交
3193
        if (virAsprintf(&datastoreRelatedPath, "[%s] %s/%s.vmx", datastoreName,
3194
                        directoryName, escapedName) < 0)
M
Matthias Bolte 已提交
3195
            goto cleanup;
M
Matthias Bolte 已提交
3196 3197
    } else {
        if (virAsprintf(&datastoreRelatedPath, "[%s] %s.vmx", datastoreName,
3198
                        escapedName) < 0)
M
Matthias Bolte 已提交
3199
            goto cleanup;
M
Matthias Bolte 已提交
3200 3201
    }

3202
    if (esxVI_RegisterVM_Task(priv->primary, priv->primary->datacenter->vmFolder,
M
Matthias Bolte 已提交
3203
                              datastoreRelatedPath, NULL, esxVI_Boolean_False,
3204 3205 3206 3207
                              priv->primary->computeResource->resourcePool,
                              priv->primary->hostSystem->_reference,
                              &task) < 0 ||
        esxVI_WaitForTaskCompletion(priv->primary, task, def->uuid,
3208
                                    esxVI_Occurrence_OptionalItem,
3209
                                    priv->parsedUri->autoAnswer, &taskInfoState,
3210
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
3211
        goto cleanup;
M
Matthias Bolte 已提交
3212 3213 3214
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
3215 3216
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not define domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
3217
        goto cleanup;
M
Matthias Bolte 已提交
3218 3219 3220 3221
    }

    domain = virGetDomain(conn, def->name, def->uuid);

3222
    if (domain) {
M
Matthias Bolte 已提交
3223 3224 3225 3226 3227 3228
        domain->id = -1;
    }

    /* FIXME: Add proper rollback in case of an error */

  cleanup:
3229
    if (!url) {
M
Matthias Bolte 已提交
3230 3231 3232
        virBufferFreeAndReset(&buffer);
    }

M
Matthias Bolte 已提交
3233 3234 3235 3236
    virDomainDefFree(def);
    VIR_FREE(vmx);
    VIR_FREE(datastoreName);
    VIR_FREE(directoryName);
3237
    VIR_FREE(escapedName);
M
Matthias Bolte 已提交
3238 3239 3240 3241 3242 3243 3244
    VIR_FREE(url);
    VIR_FREE(datastoreRelatedPath);
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);
    esxVI_ManagedObjectReference_Free(&resourcePool);
    esxVI_ManagedObjectReference_Free(&task);
3245
    VIR_FREE(taskInfoErrorMessage);
M
Matthias Bolte 已提交
3246 3247 3248 3249 3250 3251

    return domain;
}



3252
static int
3253 3254
esxDomainUndefineFlags(virDomainPtr domain,
                       unsigned int flags)
3255
{
M
Matthias Bolte 已提交
3256
    int result = -1;
M
Matthias Bolte 已提交
3257
    esxPrivate *priv = domain->conn->privateData;
3258
    esxVI_Context *ctx = NULL;
3259 3260 3261 3262
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

3263 3264 3265 3266
    /* No managed save, so we explicitly reject
     * VIR_DOMAIN_UNDEFINE_MANAGED_SAVE.  No snapshot metadata for
     * ESX, so we can trivially ignore that flag.  */
    virCheckFlags(VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
3267

3268
    if (priv->vCenter) {
3269 3270 3271 3272 3273
        ctx = priv->vCenter;
    } else {
        ctx = priv->host;
    }

3274
    if (esxVI_EnsureSession(ctx) < 0) {
M
Matthias Bolte 已提交
3275
        return -1;
3276 3277
    }

3278
    if (esxVI_String_AppendValueToList(&propertyNameList,
3279
                                       "runtime.powerState") < 0 ||
3280 3281
        esxVI_LookupVirtualMachineByUuid(ctx, domain->uuid, propertyNameList,
                                         &virtualMachine,
M
Matthias Bolte 已提交
3282
                                         esxVI_Occurrence_RequiredItem) < 0 ||
3283
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
3284
        goto cleanup;
3285 3286 3287 3288
    }

    if (powerState != esxVI_VirtualMachinePowerState_Suspended &&
        powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
3289 3290
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not suspended or powered off"));
M
Matthias Bolte 已提交
3291
        goto cleanup;
3292 3293
    }

3294
    if (esxVI_UnregisterVM(ctx, virtualMachine->obj) < 0) {
M
Matthias Bolte 已提交
3295
        goto cleanup;
3296 3297
    }

M
Matthias Bolte 已提交
3298 3299
    result = 0;

3300 3301 3302 3303 3304 3305 3306 3307
  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}


3308 3309 3310 3311 3312
static int
esxDomainUndefine(virDomainPtr domain)
{
    return esxDomainUndefineFlags(domain, 0);
}
3313

3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
static int
esxDomainGetAutostart(virDomainPtr domain, int *autostart)
{
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_AutoStartDefaults *defaults = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_AutoStartPowerInfo *powerInfo = NULL;
    esxVI_AutoStartPowerInfo *powerInfoList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;

    *autostart = 0;

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

    /* Check general autostart config */
    if (esxVI_LookupAutoStartDefaults(priv->primary, &defaults) < 0) {
        goto cleanup;
    }

    if (defaults->enabled != esxVI_Boolean_True) {
        /* Autostart is disabled in general, exit early here */
        result = 0;
        goto cleanup;
    }

    /* Check specific autostart config */
    if (esxVI_LookupAutoStartPowerInfoList(priv->primary, &powerInfoList) < 0) {
        goto cleanup;
    }

3347
    if (!powerInfoList) {
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358
        /* powerInfo list is empty, exit early here */
        result = 0;
        goto cleanup;
    }

    if (esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
                                         NULL, &virtualMachine,
                                         esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

3359
    for (powerInfo = powerInfoList; powerInfo;
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
         powerInfo = powerInfo->_next) {
        if (STREQ(powerInfo->key->value, virtualMachine->obj->value)) {
            if (STRCASEEQ(powerInfo->startAction, "powerOn")) {
                *autostart = 1;
            }

            break;
        }
    }

    result = 0;

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_AutoStartDefaults_Free(&defaults);
    esxVI_AutoStartPowerInfo_Free(&powerInfoList);
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
}



static int
esxDomainSetAutostart(virDomainPtr domain, int autostart)
{
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_HostAutoStartManagerConfig *spec = NULL;
    esxVI_AutoStartDefaults *defaults = NULL;
    esxVI_AutoStartPowerInfo *powerInfoList = NULL;
    esxVI_AutoStartPowerInfo *powerInfo = NULL;
    esxVI_AutoStartPowerInfo *newPowerInfo = NULL;

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

    if (esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
                                         NULL, &virtualMachine,
                                         esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_HostAutoStartManagerConfig_Alloc(&spec) < 0) {
        goto cleanup;
    }

    if (autostart) {
        /*
         * There is a general autostart option that affects the autostart
         * behavior of all domains. If it's disabled then no domain does
         * autostart. If it's enabled then the autostart behavior depends on
         * the per-domain autostart config.
         */
        if (esxVI_LookupAutoStartDefaults(priv->primary, &defaults) < 0) {
            goto cleanup;
        }

        if (defaults->enabled != esxVI_Boolean_True) {
            /*
             * Autostart is disabled in general. Check if no other domain is
             * in the list of autostarted domains, so it's safe to enable the
             * general autostart option without affecting the autostart
             * behavior of other domains.
             */
            if (esxVI_LookupAutoStartPowerInfoList(priv->primary,
                                                   &powerInfoList) < 0) {
                goto cleanup;
            }

3429
            for (powerInfo = powerInfoList; powerInfo;
3430 3431
                 powerInfo = powerInfo->_next) {
                if (STRNEQ(powerInfo->key->value, virtualMachine->obj->value)) {
3432 3433 3434
                    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                                   _("Cannot enable general autostart option "
                                     "without affecting other domains"));
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450
                    goto cleanup;
                }
            }

            /* Enable autostart in general */
            if (esxVI_AutoStartDefaults_Alloc(&spec->defaults) < 0) {
                goto cleanup;
            }

            spec->defaults->enabled = esxVI_Boolean_True;
        }
    }

    if (esxVI_AutoStartPowerInfo_Alloc(&newPowerInfo) < 0 ||
        esxVI_Int_Alloc(&newPowerInfo->startOrder) < 0 ||
        esxVI_Int_Alloc(&newPowerInfo->startDelay) < 0 ||
3451
        esxVI_Int_Alloc(&newPowerInfo->stopDelay) < 0) {
3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462
        goto cleanup;
    }

    newPowerInfo->key = virtualMachine->obj;
    newPowerInfo->startOrder->value = -1; /* no specific start order */
    newPowerInfo->startDelay->value = -1; /* use system default */
    newPowerInfo->waitForHeartbeat = esxVI_AutoStartWaitHeartbeatSetting_SystemDefault;
    newPowerInfo->startAction = autostart ? (char *)"powerOn" : (char *)"none";
    newPowerInfo->stopDelay->value = -1; /* use system default */
    newPowerInfo->stopAction = (char *)"none";

3463 3464 3465 3466 3467
    if (esxVI_AutoStartPowerInfo_AppendToList(&spec->powerInfo,
                                              newPowerInfo) < 0) {
        goto cleanup;
    }

3468
    newPowerInfo = NULL;
3469

3470 3471 3472 3473 3474 3475 3476 3477 3478 3479
    if (esxVI_ReconfigureAutostart
          (priv->primary,
           priv->primary->hostSystem->configManager->autoStartManager,
           spec) < 0) {
        goto cleanup;
    }

    result = 0;

  cleanup:
3480
    if (newPowerInfo) {
3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
        newPowerInfo->key = NULL;
        newPowerInfo->startAction = NULL;
        newPowerInfo->stopAction = NULL;
    }

    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_HostAutoStartManagerConfig_Free(&spec);
    esxVI_AutoStartDefaults_Free(&defaults);
    esxVI_AutoStartPowerInfo_Free(&powerInfoList);

3491
    esxVI_AutoStartPowerInfo_Free(&newPowerInfo);
3492

3493 3494 3495 3496 3497
    return result;
}



3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
/*
 * The scheduler interface exposes basically the CPU ResourceAllocationInfo:
 *
 * - http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.ResourceAllocationInfo.html
 * - http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.SharesInfo.html
 * - http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.SharesInfo.Level.html
 *
 *
 * Available parameters:
 *
3508
 * - reservation (VIR_TYPED_PARAM_LLONG >= 0, in megaherz)
3509
 *
3510
 *   The amount of CPU resource that is guaranteed to be available to the domain.
3511 3512
 *
 *
3513
 * - limit (VIR_TYPED_PARAM_LLONG >= 0, or -1, in megaherz)
3514
 *
3515 3516
 *   The CPU utilization of the domain will be limited to this value, even if
 *   more CPU resources are available. If the limit is set to -1, the CPU
3517 3518 3519 3520
 *   utilization of the domain is unlimited. If the limit is not set to -1, it
 *   must be greater than or equal to the reservation.
 *
 *
3521
 * - shares (VIR_TYPED_PARAM_INT >= 0, or in {-1, -2, -3}, no unit)
3522 3523 3524 3525 3526 3527
 *
 *   Shares are used to determine relative CPU allocation between domains. In
 *   general, a domain with more shares gets proportionally more of the CPU
 *   resource. The special values -1, -2 and -3 represent the predefined
 *   SharesLevel 'low', 'normal' and 'high'.
 */
3528
static char *
3529
esxDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED, int *nparams)
3530
{
3531
    char *type;
3532

3533
    if (VIR_STRDUP(type, "allocation") < 0)
3534
        return NULL;
3535

3536
    if (nparams) {
3537 3538
        *nparams = 3; /* reservation, limit, shares */
    }
3539 3540 3541 3542 3543 3544 3545

    return type;
}



static int
3546 3547 3548
esxDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                     virTypedParameterPtr params, int *nparams,
                                     unsigned int flags)
3549
{
M
Matthias Bolte 已提交
3550
    int result = -1;
M
Matthias Bolte 已提交
3551
    esxPrivate *priv = domain->conn->privateData;
3552 3553 3554 3555 3556
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_SharesInfo *sharesInfo = NULL;
    unsigned int mask = 0;
3557
    size_t i = 0;
3558

3559 3560
    virCheckFlags(0, -1);

3561
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
3562
        return -1;
3563 3564
    }

3565
    if (esxVI_String_AppendValueListToList(&propertyNameList,
3566 3567 3568
                                           "config.cpuAllocation.reservation\0"
                                           "config.cpuAllocation.limit\0"
                                           "config.cpuAllocation.shares\0") < 0 ||
3569
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
3570
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
3571
                                         esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
3572
        goto cleanup;
3573 3574 3575
    }

    for (dynamicProperty = virtualMachine->propSet;
3576
         dynamicProperty && mask != 7 && i < 3 && i < *nparams;
3577 3578
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "config.cpuAllocation.reservation") &&
M
Matthias Bolte 已提交
3579
            ! (mask & (1 << 0))) {
3580
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
3581
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
3582
                goto cleanup;
3583
            }
3584 3585 3586 3587 3588
            if (virTypedParameterAssign(&params[i],
                                        VIR_DOMAIN_SCHEDULER_RESERVATION,
                                        VIR_TYPED_PARAM_LLONG,
                                        dynamicProperty->val->int64) < 0)
                goto cleanup;
3589 3590 3591 3592
            mask |= 1 << 0;
            ++i;
        } else if (STREQ(dynamicProperty->name,
                         "config.cpuAllocation.limit") &&
M
Matthias Bolte 已提交
3593
                   ! (mask & (1 << 1))) {
3594
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
3595
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
3596
                goto cleanup;
3597
            }
3598 3599 3600 3601 3602
            if (virTypedParameterAssign(&params[i],
                                        VIR_DOMAIN_SCHEDULER_LIMIT,
                                        VIR_TYPED_PARAM_LLONG,
                                        dynamicProperty->val->int64) < 0)
                goto cleanup;
3603 3604 3605 3606
            mask |= 1 << 1;
            ++i;
        } else if (STREQ(dynamicProperty->name,
                         "config.cpuAllocation.shares") &&
M
Matthias Bolte 已提交
3607
                   ! (mask & (1 << 2))) {
3608 3609 3610 3611
            if (virTypedParameterAssign(&params[i],
                                        VIR_DOMAIN_SCHEDULER_SHARES,
                                        VIR_TYPED_PARAM_INT, 0) < 0)
                goto cleanup;
3612
            if (esxVI_SharesInfo_CastFromAnyType(dynamicProperty->val,
3613
                                                 &sharesInfo) < 0) {
M
Matthias Bolte 已提交
3614
                goto cleanup;
3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
            }

            switch (sharesInfo->level) {
              case esxVI_SharesLevel_Custom:
                params[i].value.i = sharesInfo->shares->value;
                break;

              case esxVI_SharesLevel_Low:
                params[i].value.i = -1;
                break;

              case esxVI_SharesLevel_Normal:
                params[i].value.i = -2;
                break;

              case esxVI_SharesLevel_High:
                params[i].value.i = -3;
                break;

              default:
3635 3636 3637
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Shares level has unknown value %d"),
                               (int)sharesInfo->level);
3638
                esxVI_SharesInfo_Free(&sharesInfo);
M
Matthias Bolte 已提交
3639
                goto cleanup;
3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651
            }

            esxVI_SharesInfo_Free(&sharesInfo);

            mask |= 1 << 2;
            ++i;
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

    *nparams = i;
M
Matthias Bolte 已提交
3652
    result = 0;
3653 3654 3655 3656 3657 3658 3659 3660

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
}

3661 3662 3663 3664 3665 3666
static int
esxDomainGetSchedulerParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int *nparams)
{
    return esxDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
}
3667 3668 3669


static int
3670 3671 3672
esxDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                     virTypedParameterPtr params, int nparams,
                                     unsigned int flags)
3673
{
M
Matthias Bolte 已提交
3674
    int result = -1;
M
Matthias Bolte 已提交
3675
    esxPrivate *priv = domain->conn->privateData;
3676 3677 3678 3679 3680
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_SharesInfo *sharesInfo = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
3681
    char *taskInfoErrorMessage = NULL;
3682
    size_t i;
3683

3684
    virCheckFlags(0, -1);
3685 3686 3687 3688 3689 3690 3691 3692
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_RESERVATION,
                               VIR_TYPED_PARAM_LLONG,
                               VIR_DOMAIN_SCHEDULER_LIMIT,
                               VIR_TYPED_PARAM_LLONG,
                               VIR_DOMAIN_SCHEDULER_SHARES,
                               VIR_TYPED_PARAM_INT,
                               NULL) < 0)
3693
        return -1;
3694

3695
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
3696
        return -1;
3697 3698
    }

3699
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
3700
          (priv->primary, domain->uuid, NULL, &virtualMachine,
3701
           priv->parsedUri->autoAnswer) < 0 ||
3702 3703
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_ResourceAllocationInfo_Alloc(&spec->cpuAllocation) < 0) {
M
Matthias Bolte 已提交
3704
        goto cleanup;
3705 3706 3707
    }

    for (i = 0; i < nparams; ++i) {
3708
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_RESERVATION)) {
3709
            if (esxVI_Long_Alloc(&spec->cpuAllocation->reservation) < 0) {
M
Matthias Bolte 已提交
3710
                goto cleanup;
3711 3712 3713
            }

            if (params[i].value.l < 0) {
3714 3715 3716
                virReportError(VIR_ERR_INVALID_ARG,
                               _("Could not set reservation to %lld MHz, expecting "
                                 "positive value"), params[i].value.l);
M
Matthias Bolte 已提交
3717
                goto cleanup;
3718 3719 3720
            }

            spec->cpuAllocation->reservation->value = params[i].value.l;
3721
        } else if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_LIMIT)) {
3722
            if (esxVI_Long_Alloc(&spec->cpuAllocation->limit) < 0) {
M
Matthias Bolte 已提交
3723
                goto cleanup;
3724 3725 3726
            }

            if (params[i].value.l < -1) {
3727 3728 3729 3730
                virReportError(VIR_ERR_INVALID_ARG,
                               _("Could not set limit to %lld MHz, expecting "
                                 "positive value or -1 (unlimited)"),
                               params[i].value.l);
M
Matthias Bolte 已提交
3731
                goto cleanup;
3732 3733 3734
            }

            spec->cpuAllocation->limit->value = params[i].value.l;
3735
        } else if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_SHARES)) {
3736 3737
            if (esxVI_SharesInfo_Alloc(&sharesInfo) < 0 ||
                esxVI_Int_Alloc(&sharesInfo->shares) < 0) {
M
Matthias Bolte 已提交
3738
                goto cleanup;
3739 3740 3741
            }

            spec->cpuAllocation->shares = sharesInfo;
3742
            sharesInfo = NULL;
3743

3744
            if (params[i].value.i >= 0) {
3745
                spec->cpuAllocation->shares->level = esxVI_SharesLevel_Custom;
3746
                spec->cpuAllocation->shares->shares->value = params[i].value.i;
3747
            } else {
3748
                switch (params[i].value.i) {
3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
                  case -1:
                    spec->cpuAllocation->shares->level = esxVI_SharesLevel_Low;
                    spec->cpuAllocation->shares->shares->value = -1;
                    break;

                  case -2:
                    spec->cpuAllocation->shares->level =
                      esxVI_SharesLevel_Normal;
                    spec->cpuAllocation->shares->shares->value = -1;
                    break;

                  case -3:
                    spec->cpuAllocation->shares->level =
                      esxVI_SharesLevel_High;
                    spec->cpuAllocation->shares->shares->value = -1;
                    break;

                  default:
3767 3768 3769 3770
                    virReportError(VIR_ERR_INVALID_ARG,
                                   _("Could not set shares to %d, expecting positive "
                                     "value or -1 (low), -2 (normal) or -3 (high)"),
                                   params[i].value.i);
M
Matthias Bolte 已提交
3771
                    goto cleanup;
3772 3773 3774 3775 3776
                }
            }
        }
    }

3777
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
3778
                              &task) < 0 ||
3779
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
3780
                                    esxVI_Occurrence_RequiredItem,
3781
                                    priv->parsedUri->autoAnswer, &taskInfoState,
3782
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
3783
        goto cleanup;
3784 3785 3786
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
3787 3788 3789
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not change scheduler parameters: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
3790
        goto cleanup;
3791 3792
    }

M
Matthias Bolte 已提交
3793 3794
    result = 0;

3795
  cleanup:
3796
    esxVI_SharesInfo_Free(&sharesInfo);
3797 3798 3799
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
3800
    VIR_FREE(taskInfoErrorMessage);
3801 3802 3803 3804

    return result;
}

3805 3806 3807 3808 3809 3810
static int
esxDomainSetSchedulerParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int nparams)
{
    return esxDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
}
3811

E
Eric Blake 已提交
3812 3813 3814 3815 3816 3817
/* The subset of migration flags we are able to support.  */
#define ESX_MIGRATION_FLAGS                     \
    (VIR_MIGRATE_PERSIST_DEST |                 \
     VIR_MIGRATE_UNDEFINE_SOURCE |              \
     VIR_MIGRATE_LIVE |                         \
     VIR_MIGRATE_PAUSED)
3818 3819 3820 3821 3822

static int
esxDomainMigratePrepare(virConnectPtr dconn,
                        char **cookie ATTRIBUTE_UNUSED,
                        int *cookielen ATTRIBUTE_UNUSED,
3823 3824
                        const char *uri_in ATTRIBUTE_UNUSED,
                        char **uri_out,
E
Eric Blake 已提交
3825
                        unsigned long flags,
3826 3827 3828
                        const char *dname ATTRIBUTE_UNUSED,
                        unsigned long resource ATTRIBUTE_UNUSED)
{
3829
    esxPrivate *priv = dconn->privateData;
3830

E
Eric Blake 已提交
3831 3832
    virCheckFlags(ESX_MIGRATION_FLAGS, -1);

3833
    if (!uri_in) {
3834 3835 3836
        if (virAsprintf(uri_out, "vpxmigr://%s/%s/%s",
                        priv->vCenter->ipAddress,
                        priv->vCenter->computeResource->resourcePool->value,
3837
                        priv->vCenter->hostSystem->_reference->value) < 0)
3838
            return -1;
3839 3840
    }

3841
    return 0;
3842 3843 3844 3845 3846 3847 3848 3849 3850
}



static int
esxDomainMigratePerform(virDomainPtr domain,
                        const char *cookie ATTRIBUTE_UNUSED,
                        int cookielen ATTRIBUTE_UNUSED,
                        const char *uri,
E
Eric Blake 已提交
3851
                        unsigned long flags,
3852 3853 3854
                        const char *dname,
                        unsigned long bandwidth ATTRIBUTE_UNUSED)
{
M
Matthias Bolte 已提交
3855
    int result = -1;
M
Matthias Bolte 已提交
3856
    esxPrivate *priv = domain->conn->privateData;
M
Martin Kletzander 已提交
3857
    virURIPtr parsedUri = NULL;
3858 3859 3860
    char *saveptr;
    char *path_resourcePool;
    char *path_hostSystem;
3861
    esxVI_ObjectContent *virtualMachine = NULL;
3862 3863
    esxVI_ManagedObjectReference resourcePool;
    esxVI_ManagedObjectReference hostSystem;
3864 3865 3866
    esxVI_Event *eventList = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
3867
    char *taskInfoErrorMessage = NULL;
3868

E
Eric Blake 已提交
3869 3870
    virCheckFlags(ESX_MIGRATION_FLAGS, -1);

3871
    if (!priv->vCenter) {
3872 3873
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Migration not possible without a vCenter"));
M
Matthias Bolte 已提交
3874
        return -1;
3875 3876
    }

3877
    if (dname) {
3878 3879
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Renaming domains on migration not supported"));
M
Matthias Bolte 已提交
3880
        return -1;
3881 3882
    }

3883
    if (esxVI_EnsureSession(priv->vCenter) < 0) {
M
Matthias Bolte 已提交
3884
        return -1;
3885 3886
    }

3887
    /* Parse migration URI */
3888
    if (!(parsedUri = virURIParse(uri)))
M
Matthias Bolte 已提交
3889
        return -1;
3890

3891
    if (!parsedUri->scheme || STRCASENEQ(parsedUri->scheme, "vpxmigr")) {
3892 3893
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Only vpxmigr:// migration URIs are supported"));
M
Matthias Bolte 已提交
3894
        goto cleanup;
3895 3896
    }

3897
    if (STRCASENEQ(priv->vCenter->ipAddress, parsedUri->server)) {
3898 3899 3900
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Migration source and destination have to refer to "
                         "the same vCenter"));
3901 3902 3903 3904 3905 3906
        goto cleanup;
    }

    path_resourcePool = strtok_r(parsedUri->path, "/", &saveptr);
    path_hostSystem = strtok_r(NULL, "", &saveptr);

3907
    if (!path_resourcePool || !path_hostSystem) {
3908 3909
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Migration URI has to specify resource pool and host system"));
M
Matthias Bolte 已提交
3910
        goto cleanup;
3911 3912
    }

3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
    resourcePool._next = NULL;
    resourcePool._type = esxVI_Type_ManagedObjectReference;
    resourcePool.type = (char *)"ResourcePool";
    resourcePool.value = path_resourcePool;

    hostSystem._next = NULL;
    hostSystem._type = esxVI_Type_ManagedObjectReference;
    hostSystem.type = (char *)"HostSystem";
    hostSystem.value = path_hostSystem;

    /* Lookup VirtualMachine */
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
          (priv->vCenter, domain->uuid, NULL, &virtualMachine,
3926
           priv->parsedUri->autoAnswer) < 0) {
M
Matthias Bolte 已提交
3927
        goto cleanup;
3928 3929 3930
    }

    /* Validate the purposed migration */
3931
    if (esxVI_ValidateMigration(priv->vCenter, virtualMachine->obj,
3932 3933
                                esxVI_VirtualMachinePowerState_Undefined, NULL,
                                &resourcePool, &hostSystem, &eventList) < 0) {
M
Matthias Bolte 已提交
3934
        goto cleanup;
3935 3936
    }

3937
    if (eventList) {
3938 3939 3940 3941
        /*
         * FIXME: Need to report the complete list of events. Limit reporting
         *        to the first event for now.
         */
3942
        if (eventList->fullFormattedMessage) {
3943 3944 3945
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not migrate domain, validation reported a "
                             "problem: %s"), eventList->fullFormattedMessage);
3946
        } else {
3947 3948 3949
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not migrate domain, validation reported a "
                             "problem"));
3950 3951
        }

M
Matthias Bolte 已提交
3952
        goto cleanup;
3953 3954 3955
    }

    /* Perform the purposed migration */
3956 3957
    if (esxVI_MigrateVM_Task(priv->vCenter, virtualMachine->obj,
                             &resourcePool, &hostSystem,
3958 3959 3960
                             esxVI_VirtualMachineMovePriority_DefaultPriority,
                             esxVI_VirtualMachinePowerState_Undefined,
                             &task) < 0 ||
3961
        esxVI_WaitForTaskCompletion(priv->vCenter, task, domain->uuid,
3962
                                    esxVI_Occurrence_RequiredItem,
3963
                                    priv->parsedUri->autoAnswer, &taskInfoState,
3964
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
3965
        goto cleanup;
3966 3967 3968
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
3969 3970 3971 3972
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not migrate domain, migration task finished with "
                         "an error: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
3973
        goto cleanup;
3974 3975
    }

M
Matthias Bolte 已提交
3976 3977
    result = 0;

3978
  cleanup:
3979
    virURIFree(parsedUri);
3980 3981 3982
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_Event_Free(&eventList);
    esxVI_ManagedObjectReference_Free(&task);
3983
    VIR_FREE(taskInfoErrorMessage);
3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994

    return result;
}



static virDomainPtr
esxDomainMigrateFinish(virConnectPtr dconn, const char *dname,
                       const char *cookie ATTRIBUTE_UNUSED,
                       int cookielen ATTRIBUTE_UNUSED,
                       const char *uri ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3995
                       unsigned long flags)
3996
{
E
Eric Blake 已提交
3997 3998
    virCheckFlags(ESX_MIGRATION_FLAGS, NULL);

3999 4000 4001 4002 4003
    return esxDomainLookupByName(dconn, dname);
}



M
Matthias Bolte 已提交
4004 4005 4006 4007
static unsigned long long
esxNodeGetFreeMemory(virConnectPtr conn)
{
    unsigned long long result = 0;
M
Matthias Bolte 已提交
4008
    esxPrivate *priv = conn->privateData;
M
Matthias Bolte 已提交
4009 4010 4011 4012 4013
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *resourcePool = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_ResourcePoolResourceUsage *resourcePoolResourceUsage = NULL;

4014
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4015
        return 0;
M
Matthias Bolte 已提交
4016 4017 4018
    }

    /* Get memory usage of resource pool */
4019
    if (esxVI_String_AppendValueToList(&propertyNameList,
M
Matthias Bolte 已提交
4020
                                       "runtime.memory") < 0 ||
4021 4022
        esxVI_LookupObjectContentByType(priv->primary,
                                        priv->primary->computeResource->resourcePool,
4023
                                        "ResourcePool", propertyNameList,
4024 4025
                                        &resourcePool,
                                        esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4026
        goto cleanup;
M
Matthias Bolte 已提交
4027 4028
    }

4029
    for (dynamicProperty = resourcePool->propSet; dynamicProperty;
M
Matthias Bolte 已提交
4030 4031 4032
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "runtime.memory")) {
            if (esxVI_ResourcePoolResourceUsage_CastFromAnyType
4033
                  (dynamicProperty->val, &resourcePoolResourceUsage) < 0) {
M
Matthias Bolte 已提交
4034
                goto cleanup;
M
Matthias Bolte 已提交
4035 4036 4037 4038 4039 4040 4041 4042
            }

            break;
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

4043
    if (!resourcePoolResourceUsage) {
4044 4045
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not retrieve memory usage of resource pool"));
M
Matthias Bolte 已提交
4046
        goto cleanup;
M
Matthias Bolte 已提交
4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060
    }

    result = resourcePoolResourceUsage->unreservedForVm->value;

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&resourcePool);
    esxVI_ResourcePoolResourceUsage_Free(&resourcePoolResourceUsage);

    return result;
}



4061
static int
4062
esxConnectIsEncrypted(virConnectPtr conn)
4063
{
M
Matthias Bolte 已提交
4064
    esxPrivate *priv = conn->privateData;
4065

4066
    if (STRCASEEQ(priv->parsedUri->transport, "https")) {
4067 4068 4069 4070 4071 4072 4073 4074 4075
        return 1;
    } else {
        return 0;
    }
}



static int
4076
esxConnectIsSecure(virConnectPtr conn)
4077
{
M
Matthias Bolte 已提交
4078
    esxPrivate *priv = conn->privateData;
4079

4080
    if (STRCASEEQ(priv->parsedUri->transport, "https")) {
4081 4082 4083 4084 4085 4086 4087 4088
        return 1;
    } else {
        return 0;
    }
}



4089
static int
4090
esxConnectIsAlive(virConnectPtr conn)
4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105
{
    esxPrivate *priv = conn->privateData;

    /* XXX we should be able to do something better than this but this is
     * simple, safe, and good enough for now. In worst case, the function will
     * return true even though the connection is not alive.
     */
    if (priv->primary)
        return 1;
    else
        return 0;
}



4106 4107 4108
static int
esxDomainIsActive(virDomainPtr domain)
{
M
Matthias Bolte 已提交
4109
    int result = -1;
M
Matthias Bolte 已提交
4110
    esxPrivate *priv = domain->conn->privateData;
4111 4112 4113 4114
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

4115
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4116
        return -1;
4117 4118
    }

4119
    if (esxVI_String_AppendValueToList(&propertyNameList,
4120
                                       "runtime.powerState") < 0 ||
4121
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
4122
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
4123
                                         esxVI_Occurrence_RequiredItem) < 0 ||
4124
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
4125
        goto cleanup;
4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
        result = 1;
    } else {
        result = 0;
    }

  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}



static int
4144
esxDomainIsPersistent(virDomainPtr domain)
4145
{
4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
    /* ESX has no concept of transient domains, so all of them are
     * persistent.  However, we do want to check for existence. */
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_ObjectContent *virtualMachine = NULL;

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

    if (esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
                                         NULL, &virtualMachine,
                                         esxVI_Occurrence_RequiredItem) < 0)
        goto cleanup;

    result = 1;

cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
4166 4167
}

M
Matthias Bolte 已提交
4168 4169


4170 4171 4172
static int
esxDomainIsUpdated(virDomainPtr domain ATTRIBUTE_UNUSED)
{
4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192
    /* ESX domains never have a persistent state that differs from
     * current state.  However, we do want to check for existence.  */
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_ObjectContent *virtualMachine = NULL;

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

    if (esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
                                         NULL, &virtualMachine,
                                         esxVI_Occurrence_RequiredItem) < 0)
        goto cleanup;

    result = 0;

cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
4193
}
4194

M
Matthias Bolte 已提交
4195 4196


4197 4198
static virDomainSnapshotPtr
esxDomainSnapshotCreateXML(virDomainPtr domain, const char *xmlDesc,
4199
                           unsigned int flags)
4200 4201 4202 4203 4204 4205 4206 4207
{
    esxPrivate *priv = domain->conn->privateData;
    virDomainSnapshotDefPtr def = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
4208
    char *taskInfoErrorMessage = NULL;
4209
    virDomainSnapshotPtr snapshot = NULL;
4210 4211
    bool diskOnly = (flags & VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY) != 0;
    bool quiesce = (flags & VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE) != 0;
4212

4213 4214 4215 4216 4217
    /* ESX supports disk-only and quiesced snapshots; libvirt tracks no
     * snapshot metadata so supporting that flag is trivial.  */
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY |
                  VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
                  VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA, NULL);
4218

4219
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4220
        return NULL;
4221 4222
    }

4223
    def = virDomainSnapshotDefParseString(xmlDesc, priv->caps,
4224
                                          priv->xmlopt, 0, 0);
4225

4226
    if (!def) {
M
Matthias Bolte 已提交
4227
        return NULL;
4228 4229
    }

4230
    if (def->ndisks) {
4231 4232
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("disk snapshots not supported yet"));
4233 4234 4235
        return NULL;
    }

4236
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
4237
          (priv->primary, domain->uuid, NULL, &virtualMachine,
4238
           priv->parsedUri->autoAnswer) < 0 ||
4239
        esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4240 4241
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, def->name,
4242
                                    &snapshotTree, NULL,
4243
                                    esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
4244
        goto cleanup;
4245 4246
    }

4247
    if (snapshotTree) {
4248 4249
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Snapshot '%s' already exists"), def->name);
M
Matthias Bolte 已提交
4250
        goto cleanup;
4251 4252
    }

4253
    if (esxVI_CreateSnapshot_Task(priv->primary, virtualMachine->obj,
4254
                                  def->name, def->description,
4255 4256 4257
                                  diskOnly ? esxVI_Boolean_False : esxVI_Boolean_True,
                                  quiesce ? esxVI_Boolean_True : esxVI_Boolean_False,
                                  &task) < 0 ||
4258
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
4259
                                    esxVI_Occurrence_RequiredItem,
4260
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4261
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
4262
        goto cleanup;
4263 4264 4265
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4266 4267
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not create snapshot: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
4268
        goto cleanup;
4269 4270 4271 4272 4273 4274 4275 4276 4277
    }

    snapshot = virGetDomainSnapshot(domain, def->name);

  cleanup:
    virDomainSnapshotDefFree(def);
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    esxVI_ManagedObjectReference_Free(&task);
4278
    VIR_FREE(taskInfoErrorMessage);
4279 4280 4281 4282 4283 4284 4285

    return snapshot;
}



static char *
4286 4287
esxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
4288 4289 4290 4291 4292 4293 4294 4295 4296
{
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTreeParent = NULL;
    virDomainSnapshotDef def;
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";
    char *xml = NULL;

4297 4298
    virCheckFlags(0, NULL);

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

4301
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4302
        return NULL;
4303 4304
    }

4305
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
4306 4307 4308 4309
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
                                    &snapshotTree, &snapshotTreeParent,
                                    esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4310
        goto cleanup;
4311 4312 4313 4314
    }

    def.name = snapshot->name;
    def.description = snapshotTree->description;
4315
    def.parent = snapshotTreeParent ? snapshotTreeParent->name : NULL;
4316 4317 4318

    if (esxVI_DateTime_ConvertToCalendarTime(snapshotTree->createTime,
                                             &def.creationTime) < 0) {
M
Matthias Bolte 已提交
4319
        goto cleanup;
4320 4321 4322 4323 4324 4325 4326
    }

    def.state = esxVI_VirtualMachinePowerState_ConvertToLibvirt
                  (snapshotTree->state);

    virUUIDFormat(snapshot->domain->uuid, uuid_string);

4327
    xml = virDomainSnapshotDefFormat(uuid_string, &def, flags, 0);
4328 4329 4330 4331 4332 4333 4334 4335 4336 4337

  cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);

    return xml;
}



static int
4338
esxDomainSnapshotNum(virDomainPtr domain, unsigned int flags)
4339
{
M
Matthias Bolte 已提交
4340
    int count;
4341 4342
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
4343
    bool recurse;
4344
    bool leaves;
4345

4346
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
4347 4348
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4349 4350

    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_ROOTS) == 0;
4351
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4352

4353
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4354
        return -1;
4355 4356
    }

4357 4358 4359 4360
    /* ESX snapshots do not require libvirt to maintain any metadata.  */
    if (flags & VIR_DOMAIN_SNAPSHOT_LIST_METADATA)
        return 0;

4361
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4362
                                         &rootSnapshotTreeList) < 0) {
M
Matthias Bolte 已提交
4363
        return -1;
4364 4365
    }

4366 4367
    count = esxVI_GetNumberOfSnapshotTrees(rootSnapshotTreeList, recurse,
                                           leaves);
4368 4369 4370

    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

M
Matthias Bolte 已提交
4371
    return count;
4372 4373 4374 4375 4376 4377
}



static int
esxDomainSnapshotListNames(virDomainPtr domain, char **names, int nameslen,
4378
                           unsigned int flags)
4379
{
M
Matthias Bolte 已提交
4380
    int result;
4381 4382
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
4383
    bool recurse;
4384
    bool leaves;
4385 4386

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
4387 4388
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4389

4390
    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_ROOTS) == 0;
4391
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4392

4393
    if (!names || nameslen < 0) {
4394
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid argument"));
4395 4396 4397
        return -1;
    }

4398
    if (nameslen == 0 || (flags & VIR_DOMAIN_SNAPSHOT_LIST_METADATA)) {
4399 4400 4401
        return 0;
    }

4402
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4403
        return -1;
4404 4405
    }

4406
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4407
                                         &rootSnapshotTreeList) < 0) {
M
Matthias Bolte 已提交
4408
        return -1;
4409 4410
    }

4411
    result = esxVI_GetSnapshotTreeNames(rootSnapshotTreeList, names, nameslen,
4412
                                        recurse, leaves);
4413 4414 4415 4416 4417 4418 4419 4420

    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

    return result;
}



4421 4422 4423 4424 4425 4426 4427 4428
static int
esxDomainSnapshotNumChildren(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    int count = -1;
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    bool recurse;
4429
    bool leaves;
4430 4431

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
4432 4433
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4434 4435

    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS) != 0;
4436
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456

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

    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
                                         &rootSnapshotTreeList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotTreeList, snapshot->name,
                                    &snapshotTree, NULL,
                                    esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    /* ESX snapshots do not require libvirt to maintain any metadata.  */
    if (flags & VIR_DOMAIN_SNAPSHOT_LIST_METADATA) {
        count = 0;
        goto cleanup;
    }

    count = esxVI_GetNumberOfSnapshotTrees(snapshotTree->childSnapshotList,
4457
                                           recurse, leaves);
4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476

cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

    return count;
}



static int
esxDomainSnapshotListChildrenNames(virDomainSnapshotPtr snapshot,
                                   char **names, int nameslen,
                                   unsigned int flags)
{
    int result = -1;
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    bool recurse;
4477
    bool leaves;
4478 4479

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
4480 4481
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4482 4483

    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS) != 0;
4484
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4485

4486
    if (!names || nameslen < 0) {
4487
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid argument"));
4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513
        return -1;
    }

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

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

    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
                                         &rootSnapshotTreeList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotTreeList, snapshot->name,
                                    &snapshotTree, NULL,
                                    esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    /* ESX snapshots do not require libvirt to maintain any metadata.  */
    if (flags & VIR_DOMAIN_SNAPSHOT_LIST_METADATA) {
        result = 0;
        goto cleanup;
    }

    result = esxVI_GetSnapshotTreeNames(snapshotTree->childSnapshotList,
4514
                                        names, nameslen, recurse, leaves);
4515 4516 4517 4518 4519 4520 4521 4522 4523

cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

    return result;
}



4524 4525
static virDomainSnapshotPtr
esxDomainSnapshotLookupByName(virDomainPtr domain, const char *name,
4526
                              unsigned int flags)
4527 4528 4529 4530 4531 4532
{
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    virDomainSnapshotPtr snapshot = NULL;

4533 4534
    virCheckFlags(0, NULL);

4535
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4536
        return NULL;
4537 4538
    }

4539
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4540 4541
                                         &rootSnapshotTreeList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotTreeList, name, &snapshotTree,
4542
                                    NULL,
4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562
                                    esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    snapshot = virGetDomainSnapshot(domain, name);

  cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

    return snapshot;
}



static int
esxDomainHasCurrentSnapshot(virDomainPtr domain, unsigned int flags)
{
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *currentSnapshotTree = NULL;

4563
    virCheckFlags(0, -1);
4564

4565
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4566
        return -1;
4567 4568
    }

4569
    if (esxVI_LookupCurrentSnapshotTree(priv->primary, domain->uuid,
4570 4571
                                        &currentSnapshotTree,
                                        esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
4572
        return -1;
4573 4574
    }

4575
    if (currentSnapshotTree) {
M
Matthias Bolte 已提交
4576 4577
        esxVI_VirtualMachineSnapshotTree_Free(&currentSnapshotTree);
        return 1;
4578 4579
    }

M
Matthias Bolte 已提交
4580
    return 0;
4581 4582 4583 4584
}



E
Eric Blake 已提交
4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608
static virDomainSnapshotPtr
esxDomainSnapshotGetParent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTreeParent = NULL;
    virDomainSnapshotPtr parent = NULL;

    virCheckFlags(0, NULL);

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

    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
                                    &snapshotTree, &snapshotTreeParent,
                                    esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    if (!snapshotTreeParent) {
4609 4610 4611
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("snapshot '%s' does not have a parent"),
                       snapshotTree->name);
E
Eric Blake 已提交
4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624
        goto cleanup;
    }

    parent = virGetDomainSnapshot(snapshot->domain, snapshotTreeParent->name);

cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);

    return parent;
}



4625 4626 4627 4628 4629
static virDomainSnapshotPtr
esxDomainSnapshotCurrent(virDomainPtr domain, unsigned int flags)
{
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *currentSnapshotTree = NULL;
M
Matthias Bolte 已提交
4630
    virDomainSnapshotPtr snapshot = NULL;
4631

4632
    virCheckFlags(0, NULL);
4633

4634
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4635
        return NULL;
4636 4637
    }

4638
    if (esxVI_LookupCurrentSnapshotTree(priv->primary, domain->uuid,
4639 4640
                                        &currentSnapshotTree,
                                        esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4641
        return NULL;
4642 4643 4644 4645 4646 4647 4648 4649 4650 4651
    }

    snapshot = virGetDomainSnapshot(domain, currentSnapshotTree->name);

    esxVI_VirtualMachineSnapshotTree_Free(&currentSnapshotTree);

    return snapshot;
}


4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720
static int
esxDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *currentSnapshotTree = NULL;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

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

    /* Check that snapshot exists.  */
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
                                    &snapshotTree, NULL,
                                    esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    if (esxVI_LookupCurrentSnapshotTree(priv->primary, snapshot->domain->uuid,
                                        &currentSnapshotTree,
                                        esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    ret = STREQ(snapshot->name, currentSnapshotTree->name);

cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&currentSnapshotTree);
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    return ret;
}


static int
esxDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

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

    /* Check that snapshot exists.  If so, there is no metadata.  */
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
                                    &snapshotTree, NULL,
                                    esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    ret = 0;

cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    return ret;
}

4721 4722 4723 4724

static int
esxDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, unsigned int flags)
{
M
Matthias Bolte 已提交
4725
    int result = -1;
4726 4727 4728 4729 4730
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
4731
    char *taskInfoErrorMessage = NULL;
4732

4733
    virCheckFlags(0, -1);
4734

4735
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4736
        return -1;
4737 4738
    }

4739
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
4740 4741
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
4742
                                    &snapshotTree, NULL,
4743
                                    esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4744
        goto cleanup;
4745 4746
    }

4747
    if (esxVI_RevertToSnapshot_Task(priv->primary, snapshotTree->snapshot, NULL,
4748
                                    esxVI_Boolean_Undefined, &task) < 0 ||
4749
        esxVI_WaitForTaskCompletion(priv->primary, task, snapshot->domain->uuid,
4750
                                    esxVI_Occurrence_RequiredItem,
4751
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4752
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
4753
        goto cleanup;
4754 4755 4756
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4757 4758 4759
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not revert to snapshot '%s': %s"), snapshot->name,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
4760
        goto cleanup;
4761 4762
    }

M
Matthias Bolte 已提交
4763 4764
    result = 0;

4765 4766 4767
  cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    esxVI_ManagedObjectReference_Free(&task);
4768
    VIR_FREE(taskInfoErrorMessage);
4769 4770 4771 4772 4773 4774 4775 4776 4777

    return result;
}



static int
esxDomainSnapshotDelete(virDomainSnapshotPtr snapshot, unsigned int flags)
{
M
Matthias Bolte 已提交
4778
    int result = -1;
4779 4780 4781 4782 4783 4784
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    esxVI_Boolean removeChildren = esxVI_Boolean_False;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
4785
    char *taskInfoErrorMessage = NULL;
4786

4787 4788
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
                  VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY, -1);
4789

4790
    if (esxVI_EnsureSession(priv->primary) < 0) {
M
Matthias Bolte 已提交
4791
        return -1;
4792 4793 4794 4795 4796 4797
    }

    if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN) {
        removeChildren = esxVI_Boolean_True;
    }

4798
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
4799 4800
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
4801
                                    &snapshotTree, NULL,
4802
                                    esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4803
        goto cleanup;
4804 4805
    }

4806 4807 4808 4809 4810 4811 4812
    /* ESX snapshots do not require any libvirt metadata, making this
     * flag trivial once we know we have a valid snapshot.  */
    if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY) {
        result = 0;
        goto cleanup;
    }

4813
    if (esxVI_RemoveSnapshot_Task(priv->primary, snapshotTree->snapshot,
4814
                                  removeChildren, &task) < 0 ||
4815
        esxVI_WaitForTaskCompletion(priv->primary, task, snapshot->domain->uuid,
4816
                                    esxVI_Occurrence_RequiredItem,
4817
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4818
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
4819
        goto cleanup;
4820 4821 4822
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4823 4824 4825
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not delete snapshot '%s': %s"), snapshot->name,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
4826
        goto cleanup;
4827 4828
    }

M
Matthias Bolte 已提交
4829 4830
    result = 0;

4831 4832 4833
  cleanup:
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    esxVI_ManagedObjectReference_Free(&task);
4834
    VIR_FREE(taskInfoErrorMessage);
4835 4836 4837 4838 4839 4840

    return result;
}



4841
static int
4842
esxDomainSetMemoryParameters(virDomainPtr domain, virTypedParameterPtr params,
4843 4844 4845 4846 4847 4848 4849 4850
                             int nparams, unsigned int flags)
{
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
4851
    char *taskInfoErrorMessage = NULL;
4852
    size_t i;
4853 4854

    virCheckFlags(0, -1);
4855 4856 4857 4858
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_MEMORY_MIN_GUARANTEE,
                               VIR_TYPED_PARAM_ULLONG,
                               NULL) < 0)
4859
        return -1;
4860 4861 4862 4863 4864 4865 4866

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

    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
          (priv->primary, domain->uuid, NULL, &virtualMachine,
4867
           priv->parsedUri->autoAnswer) < 0 ||
4868 4869 4870 4871 4872 4873
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_ResourceAllocationInfo_Alloc(&spec->memoryAllocation) < 0) {
        goto cleanup;
    }

    for (i = 0; i < nparams; ++i) {
4874
        if (STREQ(params[i].field, VIR_DOMAIN_MEMORY_MIN_GUARANTEE)) {
4875 4876 4877 4878 4879
            if (esxVI_Long_Alloc(&spec->memoryAllocation->reservation) < 0) {
                goto cleanup;
            }

            spec->memoryAllocation->reservation->value =
4880
              VIR_DIV_UP(params[i].value.ul, 1024); /* Scale from kilobytes to megabytes */
4881 4882 4883 4884 4885 4886 4887
        }
    }

    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
                              &task) < 0 ||
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
                                    esxVI_Occurrence_RequiredItem,
4888
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4889
                                    &taskInfoErrorMessage) < 0) {
4890 4891 4892 4893
        goto cleanup;
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4894 4895 4896
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not change memory parameters: %s"),
                       taskInfoErrorMessage);
4897 4898 4899 4900 4901 4902 4903 4904 4905
        goto cleanup;
    }

    result = 0;

  cleanup:
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
4906
    VIR_FREE(taskInfoErrorMessage);
4907 4908 4909 4910 4911 4912 4913

    return result;
}



static int
4914
esxDomainGetMemoryParameters(virDomainPtr domain, virTypedParameterPtr params,
4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943
                             int *nparams, unsigned int flags)
{
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_Long *reservation = NULL;

    virCheckFlags(0, -1);

    if (*nparams == 0) {
        *nparams = 1; /* min_guarantee */
        return 0;
    }

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

    if (esxVI_String_AppendValueToList
          (&propertyNameList, "config.memoryAllocation.reservation") < 0 ||
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
                                         propertyNameList, &virtualMachine,
                                         esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_GetLong(virtualMachine, "config.memoryAllocation.reservation",
                      &reservation, esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

4944 4945 4946 4947
    /* Scale from megabytes to kilobytes */
    if (virTypedParameterAssign(params, VIR_DOMAIN_MEMORY_MIN_GUARANTEE,
                                VIR_TYPED_PARAM_ULLONG,
                                reservation->value * 1024) < 0)
4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960
        goto cleanup;

    *nparams = 1;
    result = 0;

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_Long_Free(&reservation);

    return result;
}

4961 4962
#define MATCH(FLAG) (flags & (FLAG))
static int
4963 4964 4965
esxConnectListAllDomains(virConnectPtr conn,
                         virDomainPtr **domains,
                         unsigned int flags)
4966 4967 4968
{
    int ret = -1;
    esxPrivate *priv = conn->privateData;
4969 4970
    bool needIdentity;
    bool needPowerState;
4971 4972 4973
    virDomainPtr dom;
    virDomainPtr *doms = NULL;
    size_t ndoms = 0;
4974
    esxVI_String *propertyNameList = NULL;
4975 4976
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
4977
    esxVI_AutoStartDefaults *autoStartDefaults = NULL;
4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994
    esxVI_VirtualMachinePowerState powerState;
    esxVI_AutoStartPowerInfo *powerInfoList = NULL;
    esxVI_AutoStartPowerInfo *powerInfo = NULL;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
    char *name = NULL;
    int id;
    unsigned char uuid[VIR_UUID_BUFLEN];
    int count = 0;
    bool autostart;
    int state;

    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);

    /* check for flags that would produce empty output lists:
     * - persistence: all esx machines are persistent
     * - managed save: esx doesn't support managed save
     */
4995
    if ((MATCH(VIR_CONNECT_LIST_DOMAINS_TRANSIENT) &&
4996 4997 4998 4999 5000
         !MATCH(VIR_CONNECT_LIST_DOMAINS_PERSISTENT)) ||
        (MATCH(VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE) &&
         !MATCH(VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE))) {
        if (domains &&
            VIR_ALLOC_N(*domains, 1) < 0)
5001
            goto cleanup;
5002 5003 5004 5005 5006

        ret = 0;
        goto cleanup;
    }

5007
    if (esxVI_EnsureSession(priv->primary) < 0)
5008 5009 5010 5011 5012
        return -1;

    /* check system default autostart value */
    if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART)) {
        if (esxVI_LookupAutoStartDefaults(priv->primary,
5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025
                                          &autoStartDefaults) < 0) {
            goto cleanup;
        }

        if (autoStartDefaults->enabled == esxVI_Boolean_True) {
            if (esxVI_LookupAutoStartPowerInfoList(priv->primary,
                                                   &powerInfoList) < 0) {
                goto cleanup;
            }
        }
    }

    needIdentity = MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT) ||
5026
                   domains;
5027 5028 5029 5030 5031 5032 5033

    if (needIdentity) {
        /* Request required data for esxVI_GetVirtualMachineIdentity */
        if (esxVI_String_AppendValueListToList(&propertyNameList,
                                               "configStatus\0"
                                               "name\0"
                                               "config.uuid\0") < 0) {
5034
            goto cleanup;
5035 5036 5037 5038 5039
        }
    }

    needPowerState = MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) ||
                     MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE) ||
5040
                     domains;
5041

5042 5043 5044
    if (needPowerState) {
        if (esxVI_String_AppendValueToList(&propertyNameList,
                                           "runtime.powerState") < 0) {
5045
            goto cleanup;
5046
        }
5047 5048
    }

5049
    if (esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
5050 5051 5052 5053 5054
                                       &virtualMachineList) < 0)
        goto cleanup;

    if (domains) {
        if (VIR_ALLOC_N(doms, 1) < 0)
5055
            goto cleanup;
5056 5057 5058
        ndoms = 1;
    }

5059
    for (virtualMachine = virtualMachineList; virtualMachine;
5060
         virtualMachine = virtualMachine->_next) {
5061 5062
        if (needIdentity) {
            VIR_FREE(name);
5063

5064 5065 5066 5067 5068
            if (esxVI_GetVirtualMachineIdentity(virtualMachine, &id,
                                                &name, uuid) < 0) {
                goto cleanup;
            }
        }
5069

5070 5071 5072 5073 5074 5075
        if (needPowerState) {
            if (esxVI_GetVirtualMachinePowerState(virtualMachine,
                                                  &powerState) < 0) {
                goto cleanup;
            }
        }
5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086

        /* filter by active state */
        if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) &&
            !((MATCH(VIR_CONNECT_LIST_DOMAINS_ACTIVE) &&
               powerState != esxVI_VirtualMachinePowerState_PoweredOff) ||
              (MATCH(VIR_CONNECT_LIST_DOMAINS_INACTIVE) &&
               powerState == esxVI_VirtualMachinePowerState_PoweredOff)))
            continue;

        /* filter by snapshot existence */
        if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT)) {
5087 5088
            esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

5089 5090 5091 5092 5093 5094
            if (esxVI_LookupRootSnapshotTreeList(priv->primary, uuid,
                                                 &rootSnapshotTreeList) < 0) {
                goto cleanup;
            }

            if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT) &&
5095
                   rootSnapshotTreeList) ||
5096
                  (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) &&
5097
                   !rootSnapshotTreeList)))
5098 5099 5100 5101 5102 5103 5104
                continue;
        }

        /* filter by autostart */
        if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART)) {
            autostart = false;

5105
            if (autoStartDefaults->enabled == esxVI_Boolean_True) {
5106
                for (powerInfo = powerInfoList; powerInfo;
5107 5108 5109 5110
                     powerInfo = powerInfo->_next) {
                    if (STREQ(powerInfo->key->value, virtualMachine->obj->value)) {
                        if (STRCASEEQ(powerInfo->startAction, "powerOn"))
                            autostart = true;
5111

5112 5113
                        break;
                    }
5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126
                }
            }

            if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_AUTOSTART) &&
                   autostart) ||
                  (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART) &&
                   !autostart)))
                continue;
        }

        /* filter by domain state */
        if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE)) {
            state = esxVI_VirtualMachinePowerState_ConvertToLibvirt(powerState);
5127

5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146
            if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_RUNNING) &&
                   state == VIR_DOMAIN_RUNNING) ||
                  (MATCH(VIR_CONNECT_LIST_DOMAINS_PAUSED) &&
                   state == VIR_DOMAIN_PAUSED) ||
                  (MATCH(VIR_CONNECT_LIST_DOMAINS_SHUTOFF) &&
                   state == VIR_DOMAIN_SHUTOFF) ||
                  (MATCH(VIR_CONNECT_LIST_DOMAINS_OTHER) &&
                   (state != VIR_DOMAIN_RUNNING &&
                    state != VIR_DOMAIN_PAUSED &&
                    state != VIR_DOMAIN_SHUTOFF))))
                continue;
        }

        /* just count the machines */
        if (!doms) {
            count++;
            continue;
        }

5147
        if (VIR_RESIZE_N(doms, ndoms, count, 2) < 0)
5148
            goto cleanup;
5149

5150 5151 5152
        if (!(dom = virGetDomain(conn, name, uuid)))
            goto cleanup;

5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169
        /* Only running/suspended virtual machines have an ID != -1 */
        if (powerState != esxVI_VirtualMachinePowerState_PoweredOff)
            dom->id = id;
        else
            dom->id = -1;

        doms[count++] = dom;
    }

    if (doms)
        *domains = doms;
    doms = NULL;
    ret = count;

cleanup:
    if (doms) {
        for (id = 0; id < count; id++) {
5170
            virDomainFree(doms[id]);
5171
        }
5172 5173

        VIR_FREE(doms);
5174
    }
5175

5176
    VIR_FREE(name);
5177 5178
    esxVI_AutoStartDefaults_Free(&autoStartDefaults);
    esxVI_AutoStartPowerInfo_Free(&powerInfoList);
5179 5180
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);
5181 5182
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

5183 5184 5185
    return ret;
}
#undef MATCH
5186 5187


5188
static virDriver esxDriver = {
5189 5190
    .no = VIR_DRV_ESX,
    .name = "ESX",
5191 5192 5193 5194 5195 5196
    .connectOpen = esxConnectOpen, /* 0.7.0 */
    .connectClose = esxConnectClose, /* 0.7.0 */
    .connectSupportsFeature = esxConnectSupportsFeature, /* 0.7.0 */
    .connectGetType = esxConnectGetType, /* 0.7.0 */
    .connectGetVersion = esxConnectGetVersion, /* 0.7.0 */
    .connectGetHostname = esxConnectGetHostname, /* 0.7.0 */
5197
    .nodeGetInfo = esxNodeGetInfo, /* 0.7.0 */
5198 5199 5200 5201
    .connectGetCapabilities = esxConnectGetCapabilities, /* 0.7.1 */
    .connectListDomains = esxConnectListDomains, /* 0.7.0 */
    .connectNumOfDomains = esxConnectNumOfDomains, /* 0.7.0 */
    .connectListAllDomains = esxConnectListAllDomains, /* 0.10.2 */
5202 5203 5204 5205 5206 5207
    .domainLookupByID = esxDomainLookupByID, /* 0.7.0 */
    .domainLookupByUUID = esxDomainLookupByUUID, /* 0.7.0 */
    .domainLookupByName = esxDomainLookupByName, /* 0.7.0 */
    .domainSuspend = esxDomainSuspend, /* 0.7.0 */
    .domainResume = esxDomainResume, /* 0.7.0 */
    .domainShutdown = esxDomainShutdown, /* 0.7.0 */
5208
    .domainShutdownFlags = esxDomainShutdownFlags, /* 0.9.10 */
5209 5210
    .domainReboot = esxDomainReboot, /* 0.7.0 */
    .domainDestroy = esxDomainDestroy, /* 0.7.0 */
5211
    .domainDestroyFlags = esxDomainDestroyFlags, /* 0.9.4 */
5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224
    .domainGetOSType = esxDomainGetOSType, /* 0.7.0 */
    .domainGetMaxMemory = esxDomainGetMaxMemory, /* 0.7.0 */
    .domainSetMaxMemory = esxDomainSetMaxMemory, /* 0.7.0 */
    .domainSetMemory = esxDomainSetMemory, /* 0.7.0 */
    .domainSetMemoryParameters = esxDomainSetMemoryParameters, /* 0.8.6 */
    .domainGetMemoryParameters = esxDomainGetMemoryParameters, /* 0.8.6 */
    .domainGetInfo = esxDomainGetInfo, /* 0.7.0 */
    .domainGetState = esxDomainGetState, /* 0.9.2 */
    .domainSetVcpus = esxDomainSetVcpus, /* 0.7.0 */
    .domainSetVcpusFlags = esxDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = esxDomainGetVcpusFlags, /* 0.8.5 */
    .domainGetMaxVcpus = esxDomainGetMaxVcpus, /* 0.7.0 */
    .domainGetXMLDesc = esxDomainGetXMLDesc, /* 0.7.0 */
5225 5226 5227 5228
    .connectDomainXMLFromNative = esxConnectDomainXMLFromNative, /* 0.7.0 */
    .connectDomainXMLToNative = esxConnectDomainXMLToNative, /* 0.7.2 */
    .connectListDefinedDomains = esxConnectListDefinedDomains, /* 0.7.0 */
    .connectNumOfDefinedDomains = esxConnectNumOfDefinedDomains, /* 0.7.0 */
5229 5230 5231 5232
    .domainCreate = esxDomainCreate, /* 0.7.0 */
    .domainCreateWithFlags = esxDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = esxDomainDefineXML, /* 0.7.2 */
    .domainUndefine = esxDomainUndefine, /* 0.7.1 */
5233
    .domainUndefineFlags = esxDomainUndefineFlags, /* 0.9.4 */
5234 5235 5236 5237
    .domainGetAutostart = esxDomainGetAutostart, /* 0.9.0 */
    .domainSetAutostart = esxDomainSetAutostart, /* 0.9.0 */
    .domainGetSchedulerType = esxDomainGetSchedulerType, /* 0.7.0 */
    .domainGetSchedulerParameters = esxDomainGetSchedulerParameters, /* 0.7.0 */
5238
    .domainGetSchedulerParametersFlags = esxDomainGetSchedulerParametersFlags, /* 0.9.2 */
5239
    .domainSetSchedulerParameters = esxDomainSetSchedulerParameters, /* 0.7.0 */
5240
    .domainSetSchedulerParametersFlags = esxDomainSetSchedulerParametersFlags, /* 0.9.2 */
5241 5242 5243 5244
    .domainMigratePrepare = esxDomainMigratePrepare, /* 0.7.0 */
    .domainMigratePerform = esxDomainMigratePerform, /* 0.7.0 */
    .domainMigrateFinish = esxDomainMigrateFinish, /* 0.7.0 */
    .nodeGetFreeMemory = esxNodeGetFreeMemory, /* 0.7.2 */
5245 5246
    .connectIsEncrypted = esxConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = esxConnectIsSecure, /* 0.7.3 */
5247 5248 5249 5250 5251 5252 5253
    .domainIsActive = esxDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = esxDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = esxDomainIsUpdated, /* 0.8.6 */
    .domainSnapshotCreateXML = esxDomainSnapshotCreateXML, /* 0.8.0 */
    .domainSnapshotGetXMLDesc = esxDomainSnapshotGetXMLDesc, /* 0.8.0 */
    .domainSnapshotNum = esxDomainSnapshotNum, /* 0.8.0 */
    .domainSnapshotListNames = esxDomainSnapshotListNames, /* 0.8.0 */
5254 5255
    .domainSnapshotNumChildren = esxDomainSnapshotNumChildren, /* 0.9.7 */
    .domainSnapshotListChildrenNames = esxDomainSnapshotListChildrenNames, /* 0.9.7 */
5256 5257
    .domainSnapshotLookupByName = esxDomainSnapshotLookupByName, /* 0.8.0 */
    .domainHasCurrentSnapshot = esxDomainHasCurrentSnapshot, /* 0.8.0 */
E
Eric Blake 已提交
5258
    .domainSnapshotGetParent = esxDomainSnapshotGetParent, /* 0.9.7 */
5259 5260
    .domainSnapshotCurrent = esxDomainSnapshotCurrent, /* 0.8.0 */
    .domainRevertToSnapshot = esxDomainRevertToSnapshot, /* 0.8.0 */
5261 5262
    .domainSnapshotIsCurrent = esxDomainSnapshotIsCurrent, /* 0.9.13 */
    .domainSnapshotHasMetadata = esxDomainSnapshotHasMetadata, /* 0.9.13 */
5263
    .domainSnapshotDelete = esxDomainSnapshotDelete, /* 0.8.0 */
5264
    .connectIsAlive = esxConnectIsAlive, /* 0.9.8 */
5265 5266 5267 5268 5269 5270 5271
};



int
esxRegister(void)
{
5272 5273 5274 5275 5276
    if (virRegisterDriver(&esxDriver) < 0 ||
        esxInterfaceRegister() < 0 ||
        esxNetworkRegister() < 0 ||
        esxStorageRegister() < 0 ||
        esxDeviceRegister() < 0 ||
M
Matthias Bolte 已提交
5277 5278
        esxSecretRegister() < 0 ||
        esxNWFilterRegister() < 0) {
5279 5280
        return -1;
    }
5281 5282 5283

    return 0;
}