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

#include <config.h>

#include "internal.h"
27
#include "virdomainobjlist.h"
28
#include "snapshot_conf.h"
29
#include "virauth.h"
30
#include "viralloc.h"
31
#include "virfile.h"
32
#include "virlog.h"
33
#include "viruuid.h"
34
#include "vmx.h"
35
#include "virtypedparam.h"
36
#include "esx_driver.h"
37 38 39 40
#include "esx_interface_driver.h"
#include "esx_network_driver.h"
#include "esx_storage_driver.h"
#include "esx_private.h"
41 42 43
#include "esx_vi.h"
#include "esx_vi_methods.h"
#include "esx_util.h"
44
#include "esx_stream.h"
45
#include "virstring.h"
46
#include "viruri.h"
47 48 49

#define VIR_FROM_THIS VIR_FROM_ESX

50 51
VIR_LOG_INIT("esx.esx_driver");

52 53
static int esxDomainGetMaxVcpus(virDomainPtr domain);

54 55 56 57
typedef struct _esxVMX_Data esxVMX_Data;

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



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

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



79
/*
80
 * Parse a file name from a .vmx file and convert it to datastore path format
N
Nitesh Konkar 已提交
81
 * if possible. A .vmx file can contain file names in various formats:
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 *
 * - 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
 *
103 104 105 106 107 108 109 110
 * - 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.
 *
111 112 113 114 115 116 117
 * 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
118
 * function via the opaque parameter by the caller of virVMXParseConfig.
119 120 121 122 123 124 125 126 127
 *
 * 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.
 */
128
static char *
129
esxParseVMXFileName(const char *fileName, void *opaque)
130
{
131
    char *result = NULL;
132
    esxVMX_Data *data = opaque;
133
    esxVI_String *propertyNameList = NULL;
134
    esxVI_ObjectContent *datastoreList = NULL;
135
    esxVI_ObjectContent *datastore = NULL;
136 137 138 139 140 141 142 143
    esxVI_DatastoreHostMount *hostMount = NULL;
    char *datastoreName;
    char *tmp;
    char *saveptr;
    char *strippedFileName = NULL;
    char *copyOfFileName = NULL;
    char *directoryAndFileName;

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

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

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

171
            tmp = (char *)STRSKIP(fileName, hostMount->mountInfo->path);
172

173
            if (!tmp)
174
                continue;
175

176
            /* Found a match. Strip leading separators */
177
            while (*tmp == '/' || *tmp == '\\')
178
                ++tmp;
179

180
            if (VIR_STRDUP(strippedFileName, tmp) < 0)
181
                goto cleanup;
182

183
            tmp = strippedFileName;
184

185 186
            /* Convert \ to / */
            while (*tmp != '\0') {
187
                if (*tmp == '\\')
188
                    *tmp = '/';
189

190 191
                ++tmp;
            }
192

193
            if (virAsprintf(&result, "[%s] %s", datastoreName,
194
                            strippedFileName) < 0)
195
                goto cleanup;
196

197 198
            break;
        }
199

200
        /* Fallback to direct datastore name match */
201
        if (!result && STRPREFIX(fileName, "/vmfs/volumes/")) {
202
            if (VIR_STRDUP(copyOfFileName, fileName) < 0)
203
                goto cleanup;
204

205
            /* Expected format: '/vmfs/volumes/<datastore>/<path>' */
206 207 208
            if (!(tmp = STRSKIP(copyOfFileName, "/vmfs/volumes/")) ||
                !(datastoreName = strtok_r(tmp, "/", &saveptr))    ||
                !(directoryAndFileName = strtok_r(NULL, "", &saveptr))) {
209 210 211
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("File name '%s' doesn't have expected format "
                                 "'/vmfs/volumes/<datastore>/<path>'"), fileName);
212 213
                goto cleanup;
            }
214

215
            esxVI_ObjectContent_Free(&datastoreList);
216

217 218 219 220 221
            if (esxVI_LookupDatastoreByName(data->ctx, datastoreName,
                                            NULL, &datastoreList,
                                            esxVI_Occurrence_OptionalItem) < 0) {
                goto cleanup;
            }
222

223
            if (!datastoreList) {
224 225 226
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("File name '%s' refers to non-existing datastore '%s'"),
                               fileName, datastoreName);
227 228
                goto cleanup;
            }
229

230
            if (virAsprintf(&result, "[%s] %s", datastoreName,
231
                            directoryAndFileName) < 0)
232
                goto cleanup;
233 234
        }

235
        /* If it's an absolute path outside of a datastore just use it as is */
236
        if (!result && *fileName == '/') {
237
            /* FIXME: need to deal with Windows paths here too */
238
            if (VIR_STRDUP(result, fileName) < 0)
239 240 241
                goto cleanup;
        }

242
        if (!result) {
243 244
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not handle file name '%s'"), fileName);
245
            goto cleanup;
246
        }
247
    }
248

249
 cleanup:
250 251 252 253 254
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&datastoreList);
    esxVI_DatastoreHostMount_Free(&hostMount);
    VIR_FREE(strippedFileName);
    VIR_FREE(copyOfFileName);
255

256
    return result;
257 258 259 260
}



261
/*
E
Eric Blake 已提交
262
 * This function does the inverse of esxParseVMXFileName. It takes a file name
263 264
 * in datastore path format or in absolute format and converts it to a file
 * name that can be used in a .vmx file.
265 266 267 268 269
 *
 * 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
270
 * datastore and its mount path. Finally concatenate the mount path, directory
E
Eric Blake 已提交
271
 * and file name to an absolute path and return it. Detect the separator type
272 273
 * based on the mount path.
 */
274
static char *
275
esxFormatVMXFileName(const char *fileName, void *opaque)
276 277
{
    bool success = false;
278
    char *result = NULL;
279
    esxVMX_Data *data = opaque;
280
    char *datastoreName = NULL;
281
    char *directoryAndFileName = NULL;
282 283 284 285 286
    esxVI_ObjectContent *datastore = NULL;
    esxVI_DatastoreHostMount *hostMount = NULL;
    char separator = '/';
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *tmp;
287
    size_t length;
288

289 290 291 292 293 294
    if (*fileName == '[') {
        /* Parse datastore path and lookup datastore */
        if (esxUtil_ParseDatastorePath(fileName, &datastoreName, NULL,
                                       &directoryAndFileName) < 0) {
            goto cleanup;
        }
295

296 297 298
        if (esxVI_LookupDatastoreByName(data->ctx, datastoreName, NULL, &datastore,
                                        esxVI_Occurrence_RequiredItem) < 0 ||
            esxVI_LookupDatastoreHostMount(data->ctx, datastore->obj,
299 300
                                           &hostMount,
                                           esxVI_Occurrence_RequiredItem) < 0) {
301 302
            goto cleanup;
        }
303

304
        /* Detect separator type */
305
        if (strchr(hostMount->mountInfo->path, '\\'))
306
            separator = '\\';
307

308 309
        /* Strip trailing separators */
        length = strlen(hostMount->mountInfo->path);
310

311
        while (length > 0 && hostMount->mountInfo->path[length - 1] == separator)
312
            --length;
313

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

317 318
        if (separator != '/') {
            tmp = directoryAndFileName;
319

320
            while (*tmp != '\0') {
321
                if (*tmp == '/')
322
                    *tmp = separator;
323

324 325
                ++tmp;
            }
326
        }
327

328 329
        virBufferAddChar(&buffer, separator);
        virBufferAdd(&buffer, directoryAndFileName, -1);
330

331
        if (virBufferCheckError(&buffer) < 0)
332 333 334 335 336
            goto cleanup;

        result = virBufferContentAndReset(&buffer);
    } else if (*fileName == '/') {
        /* FIXME: need to deal with Windows paths here too */
337
        if (VIR_STRDUP(result, fileName) < 0)
338 339
            goto cleanup;
    } else {
340 341
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not handle file name '%s'"), fileName);
342 343 344 345 346 347 348
        goto cleanup;
    }

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

    success = true;

349
 cleanup:
350
    if (! success) {
351
        virBufferFreeAndReset(&buffer);
352
        VIR_FREE(result);
353 354 355
    }

    VIR_FREE(datastoreName);
356
    VIR_FREE(directoryAndFileName);
357 358
    esxVI_ObjectContent_Free(&datastore);
    esxVI_DatastoreHostMount_Free(&hostMount);
359

360
    return result;
361 362 363 364 365 366 367 368 369 370
}



static int
esxAutodetectSCSIControllerModel(virDomainDiskDefPtr def, int *model,
                                 void *opaque)
{
    int result = -1;
    esxVMX_Data *data = opaque;
371
    esxVI_FileInfo *fileInfo = NULL;
372
    esxVI_VmDiskFileInfo *vmDiskFileInfo = NULL;
373
    const char *src = virDomainDiskGetSource(def);
374 375 376

    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK ||
        def->bus != VIR_DOMAIN_DISK_BUS_SCSI ||
E
Eric Blake 已提交
377
        virDomainDiskGetType(def) != VIR_STORAGE_TYPE_FILE ||
378
        !src || !STRPREFIX(src, "[")) {
379 380 381 382 383 384 385
        /*
         * This isn't a file-based SCSI disk device with a datastore related
         * source path => do nothing.
         */
        return 0;
    }

386
    if (esxVI_LookupFileInfoByDatastorePath(data->ctx, src,
387
                                            false, &fileInfo,
388
                                            esxVI_Occurrence_RequiredItem) < 0) {
389 390 391
        goto cleanup;
    }

392
    vmDiskFileInfo = esxVI_VmDiskFileInfo_DynamicCast(fileInfo);
393

394
    if (!vmDiskFileInfo || !vmDiskFileInfo->controllerType) {
395
        virReportError(VIR_ERR_INTERNAL_ERROR,
396
                       _("Could not lookup controller model for '%s'"), src);
397 398 399 400 401
        goto cleanup;
    }

    if (STRCASEEQ(vmDiskFileInfo->controllerType,
                  "VirtualBusLogicController")) {
402
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_BUSLOGIC;
403 404
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "VirtualLsiLogicController")) {
405
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC;
406 407
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "VirtualLsiLogicSASController")) {
408
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSISAS1068;
409 410
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "ParaVirtualSCSIController")) {
411
        *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VMPVSCSI;
412
    } else {
413 414
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Found unexpected controller model '%s' for disk '%s'"),
415
                       vmDiskFileInfo->controllerType, src);
416 417 418 419 420
        goto cleanup;
    }

    result = 0;

421
 cleanup:
422
    esxVI_FileInfo_Free(&fileInfo);
423 424 425 426

    return result;
}

427 428


429
static esxVI_Boolean
430
esxSupportsLongMode(esxPrivate *priv)
431 432 433 434 435 436
{
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_HostCpuIdInfo *hostCpuIdInfoList = NULL;
    esxVI_HostCpuIdInfo *hostCpuIdInfo = NULL;
437
    esxVI_ParsedHostCpuIdInfo parsedHostCpuIdInfo;
438 439
    char edxLongModeBit = '?';

440
    if (priv->supportsLongMode != esxVI_Boolean_Undefined)
441 442
        return priv->supportsLongMode;

443
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
444
        return esxVI_Boolean_Undefined;
445

446
    if (esxVI_String_AppendValueToList(&propertyNameList,
447
                                       "hardware.cpuFeature") < 0 ||
448 449
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
450
        goto cleanup;
451 452
    }

453
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
454 455 456
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "hardware.cpuFeature")) {
            if (esxVI_HostCpuIdInfo_CastListFromAnyType
457
                  (dynamicProperty->val, &hostCpuIdInfoList) < 0) {
M
Matthias Bolte 已提交
458
                goto cleanup;
459 460
            }

461
            for (hostCpuIdInfo = hostCpuIdInfoList; hostCpuIdInfo;
462 463
                 hostCpuIdInfo = hostCpuIdInfo->_next) {
                if (hostCpuIdInfo->level->value == -2147483647) { /* 0x80000001 */
464 465
                    if (esxVI_ParseHostCpuIdInfo(&parsedHostCpuIdInfo,
                                                 hostCpuIdInfo) < 0) {
M
Matthias Bolte 已提交
466
                        goto cleanup;
467 468
                    }

469
                    edxLongModeBit = parsedHostCpuIdInfo.edx[29];
470 471 472 473 474 475

                    if (edxLongModeBit == '1') {
                        priv->supportsLongMode = esxVI_Boolean_True;
                    } else if (edxLongModeBit == '0') {
                        priv->supportsLongMode = esxVI_Boolean_False;
                    } else {
476 477 478 479 480
                        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 已提交
481
                        goto cleanup;
482 483 484 485 486 487 488 489 490 491 492 493
                    }

                    break;
                }
            }

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

494
 cleanup:
M
Matthias Bolte 已提交
495 496 497 498
    /*
     * 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.
     */
499 500 501 502 503 504 505 506 507
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);
    esxVI_HostCpuIdInfo_Free(&hostCpuIdInfoList);

    return priv->supportsLongMode;
}



508 509 510 511 512 513
static int
esxLookupHostSystemBiosUuid(esxPrivate *priv, unsigned char *uuid)
{
    int result = -1;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
514
    char *uuid_string = NULL;
515

516
    if (esxVI_EnsureSession(priv->primary) < 0)
517 518 519 520
        return -1;

    if (esxVI_String_AppendValueToList(&propertyNameList,
                                       "hardware.systemInfo.uuid") < 0 ||
521
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
522 523 524
                                         &hostSystem) < 0 ||
        esxVI_GetStringValue(hostSystem, "hardware.systemInfo.uuid",
                             &uuid_string, esxVI_Occurrence_RequiredItem) < 0) {
525 526 527
        goto cleanup;
    }

528 529 530
    if (strlen(uuid_string) > 0) {
        if (virUUIDParse(uuid_string, uuid) < 0) {
            VIR_WARN("Could not parse host UUID from string '%s'", uuid_string);
531

532 533
            /* HostSystem has an invalid UUID, ignore it */
            memset(uuid, 0, VIR_UUID_BUFLEN);
534
        }
535 536 537
    } else {
        /* HostSystem has an empty UUID */
        memset(uuid, 0, VIR_UUID_BUFLEN);
538 539 540 541
    }

    result = 0;

542
 cleanup:
543 544 545 546 547 548 549
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return result;
}


550
static virCapsPtr
551
esxCapsInit(esxPrivate *priv)
552
{
553
    esxVI_Boolean supportsLongMode = esxSupportsLongMode(priv);
554 555 556
    virCapsPtr caps = NULL;
    virCapsGuestPtr guest = NULL;

557
    if (supportsLongMode == esxVI_Boolean_Undefined)
558 559 560
        return NULL;

    if (supportsLongMode == esxVI_Boolean_True) {
561
        caps = virCapabilitiesNew(VIR_ARCH_X86_64, true, true);
562
    } else {
563
        caps = virCapabilitiesNew(VIR_ARCH_I686, true, true);
564
    }
565

566
    if (!caps)
567 568
        return NULL;

569
    virCapabilitiesAddHostMigrateTransport(caps, "vpxmigr");
570

571

572
    if (esxLookupHostSystemBiosUuid(priv, caps->host.host_uuid) < 0)
573 574
        goto failure;

575
    /* i686 */
576
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM,
577 578
                                    VIR_ARCH_I686,
                                    NULL, NULL, 0,
579
                                    NULL);
580

581
    if (!guest)
582 583
        goto failure;

584
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_VMWARE, NULL, NULL, 0, NULL))
585 586
        goto failure;

587 588
    /* x86_64 */
    if (supportsLongMode == esxVI_Boolean_True) {
589
        guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM,
590 591
                                        VIR_ARCH_X86_64,
                                        NULL, NULL,
592 593
                                        0, NULL);

594
        if (!guest)
595 596
            goto failure;

597
        if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_VMWARE, NULL, NULL, 0, NULL))
598 599 600
            goto failure;
    }

601 602
    return caps;

603
 failure:
604
    virObjectUnref(caps);
605 606 607 608 609 610

    return NULL;
}



611
static int
612 613
esxConnectToHost(esxPrivate *priv,
                 virConnectPtr conn,
614
                 virConnectAuthPtr auth,
615
                 char **vCenterIPAddress)
616 617 618 619 620 621 622 623 624
{
    int result = -1;
    char ipAddress[NI_MAXHOST] = "";
    char *username = NULL;
    char *password = NULL;
    char *url = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_Boolean inMaintenanceMode = esxVI_Boolean_Undefined;
625 626 627
    esxVI_ProductLine expectedProductLine = STRCASEEQ(conn->uri->scheme, "esx")
        ? esxVI_ProductLine_ESX
        : esxVI_ProductLine_GSX;
628

629
    if (!vCenterIPAddress || *vCenterIPAddress) {
630
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
631 632 633
        return -1;
    }

634
    if (esxUtil_ResolveHostname(conn->uri->server, ipAddress, NI_MAXHOST) < 0)
635 636
        return -1;

637
    if (conn->uri->user) {
638
        if (VIR_STRDUP(username, conn->uri->user) < 0)
639 640
            goto cleanup;
    } else {
641
        username = virAuthGetUsername(conn, auth, "esx", "root", conn->uri->server);
642

643
        if (!username) {
644
            virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed"));
645 646 647 648
            goto cleanup;
        }
    }

649
    password = virAuthGetPassword(conn, auth, "esx", username, conn->uri->server);
650

651
    if (!password) {
652
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed"));
653 654 655
        goto cleanup;
    }

656
    if (virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport,
657
                    conn->uri->server, conn->uri->port) < 0)
658 659 660 661
        goto cleanup;

    if (esxVI_Context_Alloc(&priv->host) < 0 ||
        esxVI_Context_Connect(priv->host, url, ipAddress, username, password,
662
                              priv->parsedUri) < 0 ||
663
        esxVI_Context_LookupManagedObjects(priv->host) < 0) {
664 665 666
        goto cleanup;
    }

667 668 669 670 671 672 673
    if (priv->host->productLine != expectedProductLine) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting '%s' to be a %s host but found a %s host"),
                       conn->uri->server,
                       esxVI_ProductLineToDisplayName(expectedProductLine),
                       esxVI_ProductLineToDisplayName(priv->host->productLine));
        goto cleanup;
674 675 676 677 678 679
    }

    /* Query the host for maintenance mode and vCenter IP address */
    if (esxVI_String_AppendValueListToList(&propertyNameList,
                                           "runtime.inMaintenanceMode\0"
                                           "summary.managementServerIp\0") < 0 ||
680 681
        esxVI_LookupHostSystemProperties(priv->host, propertyNameList,
                                         &hostSystem) < 0 ||
682 683 684 685
        esxVI_GetBoolean(hostSystem, "runtime.inMaintenanceMode",
                         &inMaintenanceMode,
                         esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_GetStringValue(hostSystem, "summary.managementServerIp",
686
                             vCenterIPAddress,
687 688 689 690 691
                             esxVI_Occurrence_OptionalItem) < 0) {
        goto cleanup;
    }

    /* Warn if host is in maintenance mode */
692
    if (inMaintenanceMode == esxVI_Boolean_True)
693
        VIR_WARN("The server is in maintenance mode");
694

695
    if (VIR_STRDUP(*vCenterIPAddress, *vCenterIPAddress) < 0)
696
        goto cleanup;
697 698 699

    result = 0;

700
 cleanup:
701
    VIR_FREE(username);
M
Matthias Bolte 已提交
702
    VIR_FREE(password);
703 704 705 706 707 708 709 710 711 712
    VIR_FREE(url);
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return result;
}



static int
713 714
esxConnectToVCenter(esxPrivate *priv,
                    virConnectPtr conn,
715 716
                    virConnectAuthPtr auth,
                    const char *hostname,
717
                    const char *hostSystemIPAddress)
718 719 720 721 722 723 724
{
    int result = -1;
    char ipAddress[NI_MAXHOST] = "";
    char *username = NULL;
    char *password = NULL;
    char *url = NULL;

725
    if (!hostSystemIPAddress &&
726
        (!priv->parsedUri->path || STREQ(priv->parsedUri->path, "/"))) {
727 728
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Path has to specify the datacenter and compute resource"));
729 730 731
        return -1;
    }

732
    if (esxUtil_ResolveHostname(hostname, ipAddress, NI_MAXHOST) < 0)
733 734
        return -1;

735
    if (conn->uri->user) {
736
        if (VIR_STRDUP(username, conn->uri->user) < 0)
737 738
            goto cleanup;
    } else {
739
        username = virAuthGetUsername(conn, auth, "esx", "administrator", hostname);
740

741
        if (!username) {
742
            virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed"));
743 744 745 746
            goto cleanup;
        }
    }

747
    password = virAuthGetPassword(conn, auth, "esx", username, hostname);
748

749
    if (!password) {
750
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed"));
751 752 753
        goto cleanup;
    }

754
    if (virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport,
755
                    hostname, conn->uri->port) < 0)
756 757 758 759
        goto cleanup;

    if (esxVI_Context_Alloc(&priv->vCenter) < 0 ||
        esxVI_Context_Connect(priv->vCenter, url, ipAddress, username,
760
                              password, priv->parsedUri) < 0) {
761 762 763
        goto cleanup;
    }

764
    if (priv->vCenter->productLine != esxVI_ProductLine_VPX) {
765
        virReportError(VIR_ERR_INTERNAL_ERROR,
766 767 768 769
                       _("Expecting '%s' to be a %s host but found a %s host"),
                       hostname,
                       esxVI_ProductLineToDisplayName(esxVI_ProductLine_VPX),
                       esxVI_ProductLineToDisplayName(priv->vCenter->productLine));
770 771 772
        goto cleanup;
    }

773
    if (hostSystemIPAddress) {
774
        if (esxVI_Context_LookupManagedObjectsByHostSystemIp
775
              (priv->vCenter, hostSystemIPAddress) < 0) {
776 777 778
            goto cleanup;
        }
    } else {
779 780
        if (esxVI_Context_LookupManagedObjectsByPath(priv->vCenter,
                                                     priv->parsedUri->path) < 0) {
781 782 783 784
            goto cleanup;
        }
    }

785 786
    result = 0;

787
 cleanup:
788
    VIR_FREE(username);
M
Matthias Bolte 已提交
789
    VIR_FREE(password);
790 791 792 793 794 795 796
    VIR_FREE(url);

    return result;
}



797
/*
798 799
 * URI format: {vpx|esx|gsx}://[<username>@]<hostname>[:<port>]/[<path>][?<query parameter>...]
 *             <path> = [<folder>/...]<datacenter>/[<folder>/...]<computeresource>[/<hostsystem>]
800
 *
801 802
 * If no port is specified the default port is set dependent on the scheme and
 * transport parameter:
803 804
 * - vpx+http  80
 * - vpx+https 443
805
 * - esx+http  80
806
 * - esx+https 443
807 808 809
 * - gsx+http  8222
 * - gsx+https 8333
 *
810 811 812
 * 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
813 814
 * can be omitted. As datacenters and computeresources can be organized in
 * folders those have to be included in <path>.
815
 *
816 817
 * Optional query parameters:
 * - transport={http|https}
818
 * - vcenter={<vcenter>|*}             only useful for an esx:// connection
819 820
 * - no_verify={0|1}
 * - auto_answer={0|1}
M
Matthias Bolte 已提交
821
 * - proxy=[{http|socks|socks4|socks4a|socks5}://]<hostname>[:<port>]
822
 *
823 824 825
 * If no transport parameter is specified https is used.
 *
 * The vcenter parameter is only necessary for migration, because the vCenter
826
 * server is in charge to initiate a migration between two ESX hosts. The
827
 * vcenter parameter can be set to an explicitly hostname or to *. If set to *,
828 829
 * 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.
830 831
 *
 * If the no_verify parameter is set to 1, this disables libcurl client checks
832
 * of the server's certificate. The default value is 0.
833 834 835
 *
 * 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
836
 * questions will be reported as errors. The default value is 0.
M
Matthias Bolte 已提交
837 838 839 840
 *
 * 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.
841 842
 */
static virDrvOpenStatus
843
esxConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
844
               virConfPtr conf ATTRIBUTE_UNUSED,
845
               unsigned int flags)
846
{
M
Matthias Bolte 已提交
847
    virDrvOpenStatus result = VIR_DRV_OPEN_ERROR;
848
    esxPrivate *priv = NULL;
849 850
    char *potentialVCenterIPAddress = NULL;
    char vCenterIPAddress[NI_MAXHOST] = "";
851

E
Eric Blake 已提交
852 853
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

854
    if (STRCASENEQ(conn->uri->scheme, "vpx") &&
855
        STRNEQ(conn->uri->path, "/")) {
856 857 858 859
        VIR_WARN("Ignoring unexpected path '%s' for non-vpx scheme '%s'",
                 conn->uri->path, conn->uri->scheme);
    }

860
    /* Require server part */
861
    if (!conn->uri->server) {
862 863
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("URI is missing the server part"));
864 865 866 867
        return VIR_DRV_OPEN_ERROR;
    }

    /* Require auth */
868
    if (!auth || !auth->cb) {
869 870
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Missing or invalid auth pointer"));
871
        return VIR_DRV_OPEN_ERROR;
872 873 874
    }

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

878
    if (esxUtil_ParseUri(&priv->parsedUri, conn->uri) < 0)
879 880
        goto cleanup;

M
Matthias Bolte 已提交
881 882
    priv->maxVcpus = -1;
    priv->supportsVMotion = esxVI_Boolean_Undefined;
883
    priv->supportsLongMode = esxVI_Boolean_Undefined;
884
    priv->supportsScreenshot = esxVI_Boolean_Undefined;
885 886
    priv->usedCpuTimeCounterId = -1;

M
Matthias Bolte 已提交
887 888 889 890 891 892 893
    /*
     * 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) {
894 895
        if (STRCASEEQ(conn->uri->scheme, "vpx") ||
            STRCASEEQ(conn->uri->scheme, "esx")) {
896
            if (STRCASEEQ(priv->parsedUri->transport, "https")) {
M
Matthias Bolte 已提交
897 898 899 900 901
                conn->uri->port = 443;
            } else {
                conn->uri->port = 80;
            }
        } else { /* GSX */
902
            if (STRCASEEQ(priv->parsedUri->transport, "https")) {
M
Matthias Bolte 已提交
903 904 905 906
                conn->uri->port = 8333;
            } else {
                conn->uri->port = 8222;
            }
907
        }
M
Matthias Bolte 已提交
908
    }
909

910 911 912
    if (STRCASEEQ(conn->uri->scheme, "esx") ||
        STRCASEEQ(conn->uri->scheme, "gsx")) {
        /* Connect to host */
913
        if (esxConnectToHost(priv, conn, auth,
914
                             &potentialVCenterIPAddress) < 0) {
M
Matthias Bolte 已提交
915
            goto cleanup;
916
        }
917

918
        /* Connect to vCenter */
919
        if (priv->parsedUri->vCenter) {
920
            if (STREQ(priv->parsedUri->vCenter, "*")) {
921
                if (!potentialVCenterIPAddress) {
922 923
                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                   _("This host is not managed by a vCenter"));
M
Matthias Bolte 已提交
924
                    goto cleanup;
925 926
                }

927 928
                if (!virStrcpyStatic(vCenterIPAddress,
                                     potentialVCenterIPAddress)) {
929 930
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("vCenter IP address %s too big for destination"),
931
                                   potentialVCenterIPAddress);
932 933 934
                    goto cleanup;
                }
            } else {
935
                if (esxUtil_ResolveHostname(priv->parsedUri->vCenter,
936
                                            vCenterIPAddress, NI_MAXHOST) < 0) {
937 938
                    goto cleanup;
                }
939

940 941
                if (potentialVCenterIPAddress &&
                    STRNEQ(vCenterIPAddress, potentialVCenterIPAddress)) {
942 943
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("This host is managed by a vCenter with IP "
N
Nitesh Konkar 已提交
944
                                     "address %s, but a mismatching vCenter '%s' "
945
                                     "(%s) has been specified"),
946 947
                                   potentialVCenterIPAddress, priv->parsedUri->vCenter,
                                   vCenterIPAddress);
M
Matthias Bolte 已提交
948
                    goto cleanup;
949 950
                }
            }
951

952
            if (esxConnectToVCenter(priv, conn, auth,
953
                                    vCenterIPAddress,
954
                                    priv->host->ipAddress) < 0) {
955 956
                goto cleanup;
            }
957 958
        }

959 960 961
        priv->primary = priv->host;
    } else { /* VPX */
        /* Connect to vCenter */
962
        if (esxConnectToVCenter(priv, conn, auth,
963 964
                                conn->uri->server,
                                NULL) < 0) {
M
Matthias Bolte 已提交
965
            goto cleanup;
966 967
        }

968
        priv->primary = priv->vCenter;
969 970
    }

M
Matthias Bolte 已提交
971
    /* Setup capabilities */
972
    priv->caps = esxCapsInit(priv);
973

974
    if (!priv->caps)
M
Matthias Bolte 已提交
975
        goto cleanup;
976

977
    if (!(priv->xmlopt = virVMXDomainXMLConfInit()))
978 979
        goto cleanup;

980 981
    conn->privateData = priv;
    priv = NULL;
M
Matthias Bolte 已提交
982
    result = VIR_DRV_OPEN_SUCCESS;
983

984
 cleanup:
985
    esxFreePrivate(&priv);
986
    VIR_FREE(potentialVCenterIPAddress);
987

M
Matthias Bolte 已提交
988
    return result;
989 990 991 992 993
}



static int
994
esxConnectClose(virConnectPtr conn)
995
{
M
Matthias Bolte 已提交
996
    esxPrivate *priv = conn->privateData;
E
Eric Blake 已提交
997
    int result = 0;
998

999
    if (priv->host) {
1000 1001 1002 1003 1004
        if (esxVI_EnsureSession(priv->host) < 0 ||
            esxVI_Logout(priv->host) < 0) {
            result = -1;
        }
    }
1005

1006
    if (priv->vCenter) {
E
Eric Blake 已提交
1007 1008 1009 1010
        if (esxVI_EnsureSession(priv->vCenter) < 0 ||
            esxVI_Logout(priv->vCenter) < 0) {
            result = -1;
        }
1011 1012
    }

1013
    esxFreePrivate(&priv);
1014 1015 1016

    conn->privateData = NULL;

E
Eric Blake 已提交
1017
    return result;
1018 1019 1020 1021 1022
}



static esxVI_Boolean
1023
esxSupportsVMotion(esxPrivate *priv)
1024 1025 1026 1027
{
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;

1028
    if (priv->supportsVMotion != esxVI_Boolean_Undefined)
M
Matthias Bolte 已提交
1029
        return priv->supportsVMotion;
1030

1031
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1032
        return esxVI_Boolean_Undefined;
1033

1034
    if (esxVI_String_AppendValueToList(&propertyNameList,
1035
                                       "capability.vmotionSupported") < 0 ||
1036
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
1037 1038
                                         &hostSystem) < 0 ||
        esxVI_GetBoolean(hostSystem, "capability.vmotionSupported",
1039 1040 1041
                         &priv->supportsVMotion,
                         esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
1042 1043
    }

1044
 cleanup:
M
Matthias Bolte 已提交
1045 1046 1047 1048
    /*
     * 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.
     */
1049 1050 1051
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

M
Matthias Bolte 已提交
1052
    return priv->supportsVMotion;
1053 1054 1055 1056
}



1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
static esxVI_Boolean
esxSupportsScreenshot(esxPrivate *priv)
{
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;

    if (priv->supportsScreenshot != esxVI_Boolean_Undefined)
        return priv->supportsScreenshot;

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

    if (esxVI_String_AppendValueToList(&propertyNameList,
                                       "capability.screenshotSupported") < 0 ||
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0 ||
        esxVI_GetBoolean(hostSystem, "capability.screenshotSupported",
                         &priv->supportsScreenshot,
                         esxVI_Occurrence_RequiredItem) < 0)
        goto cleanup;

 cleanup:
    /*
     * If we goto cleanup in case of an error then priv->supportsScreenshot is
     * still esxVI_Boolean_Undefined, therefore we don't need to set it.
     */
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return priv->supportsScreenshot;
}



1091
static int
1092
esxConnectSupportsFeature(virConnectPtr conn, int feature)
1093
{
M
Matthias Bolte 已提交
1094
    esxPrivate *priv = conn->privateData;
M
Matthias Bolte 已提交
1095
    esxVI_Boolean supportsVMotion = esxVI_Boolean_Undefined;
1096

1097
    switch ((virDrvFeature) feature) {
1098
      case VIR_DRV_FEATURE_MIGRATION_V1:
1099
        supportsVMotion = esxSupportsVMotion(priv);
1100

1101
        if (supportsVMotion == esxVI_Boolean_Undefined)
1102 1103
            return -1;

M
Matthias Bolte 已提交
1104
        /* Migration is only possible via a vCenter and if VMotion is enabled */
1105
        return priv->vCenter &&
M
Matthias Bolte 已提交
1106
               supportsVMotion == esxVI_Boolean_True ? 1 : 0;
1107

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    case VIR_DRV_FEATURE_FD_PASSING:
    case VIR_DRV_FEATURE_MIGRATE_CHANGE_PROTECTION:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
    case VIR_DRV_FEATURE_MIGRATION_OFFLINE:
    case VIR_DRV_FEATURE_MIGRATION_P2P:
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_MIGRATION_V3:
    case VIR_DRV_FEATURE_PROGRAM_KEEPALIVE:
    case VIR_DRV_FEATURE_REMOTE:
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
    case VIR_DRV_FEATURE_TYPED_PARAM_STRING:
    case VIR_DRV_FEATURE_XML_MIGRATABLE:
    default:
1123 1124 1125 1126 1127 1128 1129
        return 0;
    }
}



static const char *
1130
esxConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
1131 1132 1133 1134 1135 1136 1137
{
    return "ESX";
}



static int
1138
esxConnectGetVersion(virConnectPtr conn, unsigned long *version)
1139
{
M
Matthias Bolte 已提交
1140
    esxPrivate *priv = conn->privateData;
1141

1142
    *version = priv->primary->productVersion;
1143 1144 1145 1146 1147 1148 1149

    return 0;
}



static char *
1150
esxConnectGetHostname(virConnectPtr conn)
1151
{
M
Matthias Bolte 已提交
1152
    esxPrivate *priv = conn->privateData;
1153 1154 1155 1156 1157 1158 1159
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    const char *hostName = NULL;
    const char *domainName = NULL;
    char *complete = NULL;

1160
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1161
        return NULL;
1162 1163

    if (esxVI_String_AppendValueListToList
1164
          (&propertyNameList,
1165 1166
           "config.network.dnsConfig.hostName\0"
           "config.network.dnsConfig.domainName\0") < 0 ||
1167 1168
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
1169
        goto cleanup;
1170 1171
    }

1172
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
1173 1174 1175
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name,
                  "config.network.dnsConfig.hostName")) {
1176
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1177
                                         esxVI_Type_String) < 0) {
M
Matthias Bolte 已提交
1178
                goto cleanup;
1179 1180 1181 1182 1183
            }

            hostName = dynamicProperty->val->string;
        } else if (STREQ(dynamicProperty->name,
                         "config.network.dnsConfig.domainName")) {
1184
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1185
                                         esxVI_Type_String) < 0) {
M
Matthias Bolte 已提交
1186
                goto cleanup;
1187 1188 1189 1190 1191 1192 1193 1194
            }

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

1195
    if (!hostName || strlen(hostName) < 1) {
1196 1197
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing or empty 'hostName' property"));
M
Matthias Bolte 已提交
1198
        goto cleanup;
1199 1200
    }

1201
    if (!domainName || strlen(domainName) < 1) {
1202
        if (VIR_STRDUP(complete, hostName) < 0)
M
Matthias Bolte 已提交
1203
            goto cleanup;
1204
    } else {
1205
        if (virAsprintf(&complete, "%s.%s", hostName, domainName) < 0)
M
Matthias Bolte 已提交
1206
            goto cleanup;
1207 1208
    }

1209
 cleanup:
M
Matthias Bolte 已提交
1210 1211
    /*
     * If we goto cleanup in case of an error then complete is still NULL,
1212
     * either VIR_STRDUP returned -1 or virAsprintf failed. When virAsprintf
M
Matthias Bolte 已提交
1213 1214
     * fails it guarantees setting complete to NULL
     */
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return complete;
}



static int
esxNodeGetInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo)
{
M
Matthias Bolte 已提交
1226
    int result = -1;
M
Matthias Bolte 已提交
1227
    esxPrivate *priv = conn->privateData;
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    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;

1239
    memset(nodeinfo, 0, sizeof(*nodeinfo));
1240

1241
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1242
        return -1;
1243

1244
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1245 1246 1247 1248 1249 1250 1251
                                           "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 ||
1252 1253
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
1254
        goto cleanup;
1255 1256
    }

1257
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
1258 1259
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "hardware.cpuInfo.hz")) {
1260
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1261
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
1262
                goto cleanup;
1263 1264 1265 1266 1267
            }

            cpuInfo_hz = dynamicProperty->val->int64;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.cpuInfo.numCpuCores")) {
1268
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1269
                                         esxVI_Type_Short) < 0) {
M
Matthias Bolte 已提交
1270
                goto cleanup;
1271 1272 1273 1274 1275
            }

            cpuInfo_numCpuCores = dynamicProperty->val->int16;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.cpuInfo.numCpuPackages")) {
1276
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1277
                                         esxVI_Type_Short) < 0) {
M
Matthias Bolte 已提交
1278
                goto cleanup;
1279 1280 1281 1282 1283
            }

            cpuInfo_numCpuPackages = dynamicProperty->val->int16;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.cpuInfo.numCpuThreads")) {
1284
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1285
                                         esxVI_Type_Short) < 0) {
M
Matthias Bolte 已提交
1286
                goto cleanup;
1287 1288 1289 1290
            }

            cpuInfo_numCpuThreads = dynamicProperty->val->int16;
        } else if (STREQ(dynamicProperty->name, "hardware.memorySize")) {
1291
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1292
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
1293
                goto cleanup;
1294 1295 1296 1297 1298
            }

            memorySize = dynamicProperty->val->int64;
        } else if (STREQ(dynamicProperty->name,
                         "hardware.numaInfo.numNodes")) {
1299
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1300
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
1301
                goto cleanup;
1302 1303 1304 1305 1306
            }

            numaInfo_numNodes = dynamicProperty->val->int32;
        } else if (STREQ(dynamicProperty->name,
                         "summary.hardware.cpuModel")) {
1307
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1308
                                         esxVI_Type_String) < 0) {
M
Matthias Bolte 已提交
1309
                goto cleanup;
1310 1311 1312 1313 1314 1315
            }

            ptr = dynamicProperty->val->string;

            /* Strip the string to fit more relevant information in 32 chars */
            while (*ptr != '\0') {
M
Matthias Bolte 已提交
1316 1317
                if (STRPREFIX(ptr, "  ")) {
                    memmove(ptr, ptr + 1, strlen(ptr + 1) + 1);
1318
                    continue;
1319
                } else if (STRPREFIX(ptr, "(R)") || STRPREFIX(ptr, "(C)")) {
M
Matthias Bolte 已提交
1320
                    memmove(ptr, ptr + 3, strlen(ptr + 3) + 1);
1321
                    continue;
1322 1323 1324
                } else if (STRPREFIX(ptr, "(TM)")) {
                    memmove(ptr, ptr + 4, strlen(ptr + 4) + 1);
                    continue;
1325 1326 1327 1328 1329
                }

                ++ptr;
            }

1330 1331 1332
            if (!virStrncpy(nodeinfo->model, dynamicProperty->val->string,
                            sizeof(nodeinfo->model) - 1,
                            sizeof(nodeinfo->model))) {
1333 1334 1335
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("CPU Model %s too long for destination"),
                               dynamicProperty->val->string);
M
Matthias Bolte 已提交
1336
                goto cleanup;
C
Chris Lalancette 已提交
1337
            }
1338 1339 1340 1341 1342 1343 1344
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

    nodeinfo->memory = memorySize / 1024; /* Scale from bytes to kilobytes */
    nodeinfo->cpus = cpuInfo_numCpuCores;
1345
    nodeinfo->mhz = cpuInfo_hz / (1000 * 1000); /* Scale from hz to mhz */
1346 1347 1348 1349 1350 1351 1352 1353 1354
    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 已提交
1355 1356
    result = 0;

1357
 cleanup:
1358 1359 1360 1361 1362 1363 1364 1365
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

    return result;
}



1366
static char *
1367
esxConnectGetCapabilities(virConnectPtr conn)
1368
{
M
Matthias Bolte 已提交
1369
    esxPrivate *priv = conn->privateData;
1370

1371
    return virCapabilitiesFormatXML(priv->caps);
1372 1373 1374 1375
}



1376
static int
1377
esxConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1378
{
M
Matthias Bolte 已提交
1379
    bool success = false;
M
Matthias Bolte 已提交
1380
    esxPrivate *priv = conn->privateData;
1381 1382 1383 1384 1385 1386
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    int count = 0;

1387
    if (maxids == 0)
1388 1389
        return 0;

1390
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1391
        return -1;
1392

1393
    if (esxVI_String_AppendValueToList(&propertyNameList,
1394
                                       "runtime.powerState") < 0 ||
1395 1396
        esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
                                       &virtualMachineList) < 0) {
M
Matthias Bolte 已提交
1397
        goto cleanup;
1398 1399
    }

1400
    for (virtualMachine = virtualMachineList; virtualMachine;
1401
         virtualMachine = virtualMachine->_next) {
1402
        if (esxVI_GetVirtualMachinePowerState(virtualMachine,
1403
                                              &powerState) < 0) {
M
Matthias Bolte 已提交
1404
            goto cleanup;
1405 1406
        }

1407
        if (powerState != esxVI_VirtualMachinePowerState_PoweredOn)
1408 1409 1410 1411 1412
            continue;

        if (esxUtil_ParseVirtualMachineIDString(virtualMachine->obj->value,
                                                &ids[count]) < 0 ||
            ids[count] <= 0) {
1413 1414 1415
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to parse positive integer from '%s'"),
                           virtualMachine->obj->value);
M
Matthias Bolte 已提交
1416
            goto cleanup;
1417 1418 1419 1420
        }

        count++;

1421
        if (count >= maxids)
1422 1423 1424
            break;
    }

M
Matthias Bolte 已提交
1425 1426
    success = true;

1427
 cleanup:
1428 1429 1430
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);

M
Matthias Bolte 已提交
1431
    return success ? count : -1;
1432 1433 1434 1435 1436
}



static int
1437
esxConnectNumOfDomains(virConnectPtr conn)
1438
{
M
Matthias Bolte 已提交
1439
    esxPrivate *priv = conn->privateData;
1440

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

1444
    return esxVI_LookupNumberOfDomainsByPowerState
1445
             (priv->primary, esxVI_VirtualMachinePowerState_PoweredOn, false);
1446 1447 1448 1449 1450 1451 1452
}



static virDomainPtr
esxDomainLookupByID(virConnectPtr conn, int id)
{
M
Matthias Bolte 已提交
1453
    esxPrivate *priv = conn->privateData;
1454 1455 1456 1457
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
M
Matthias Bolte 已提交
1458 1459 1460
    int id_candidate = -1;
    char *name_candidate = NULL;
    unsigned char uuid_candidate[VIR_UUID_BUFLEN];
1461 1462
    virDomainPtr domain = NULL;

1463
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1464
        return NULL;
1465

1466
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1467
                                           "configStatus\0"
1468 1469
                                           "name\0"
                                           "runtime.powerState\0"
1470
                                           "config.uuid\0") < 0 ||
1471 1472
        esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
                                       &virtualMachineList) < 0) {
M
Matthias Bolte 已提交
1473
        goto cleanup;
1474 1475
    }

1476
    for (virtualMachine = virtualMachineList; virtualMachine;
1477
         virtualMachine = virtualMachine->_next) {
1478
        if (esxVI_GetVirtualMachinePowerState(virtualMachine,
1479
                                              &powerState) < 0) {
M
Matthias Bolte 已提交
1480
            goto cleanup;
1481 1482 1483
        }

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

M
Matthias Bolte 已提交
1487
        VIR_FREE(name_candidate);
1488

1489
        if (esxVI_GetVirtualMachineIdentity(virtualMachine,
M
Matthias Bolte 已提交
1490 1491
                                            &id_candidate, &name_candidate,
                                            uuid_candidate) < 0) {
M
Matthias Bolte 已提交
1492
            goto cleanup;
1493 1494
        }

1495
        if (id != id_candidate)
1496 1497
            continue;

1498
        domain = virGetDomain(conn, name_candidate, uuid_candidate, id);
1499

1500
        if (!domain)
M
Matthias Bolte 已提交
1501
            goto cleanup;
1502 1503 1504 1505

        break;
    }

1506
    if (!domain)
1507
        virReportError(VIR_ERR_NO_DOMAIN, _("No domain with ID %d"), id);
1508

1509
 cleanup:
1510 1511
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);
M
Matthias Bolte 已提交
1512
    VIR_FREE(name_candidate);
1513 1514 1515 1516 1517 1518 1519 1520 1521

    return domain;
}



static virDomainPtr
esxDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
M
Matthias Bolte 已提交
1522
    esxPrivate *priv = conn->privateData;
1523 1524 1525
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
1526 1527
    int id = -1;
    char *name = NULL;
1528 1529
    virDomainPtr domain = NULL;

1530
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1531
        return NULL;
1532

1533
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1534
                                           "name\0"
1535
                                           "runtime.powerState\0") < 0 ||
1536
        esxVI_LookupVirtualMachineByUuid(priv->primary, uuid, propertyNameList,
1537
                                         &virtualMachine,
M
Matthias Bolte 已提交
1538
                                         esxVI_Occurrence_RequiredItem) < 0 ||
1539 1540
        esxVI_GetVirtualMachineIdentity(virtualMachine, &id, &name, NULL) < 0 ||
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1541
        goto cleanup;
1542 1543
    }

1544
    /* Only running/suspended virtual machines have an ID != -1 */
1545 1546 1547 1548
    if (powerState == esxVI_VirtualMachinePowerState_PoweredOff)
        id = -1;

    domain = virGetDomain(conn, name, uuid, id);
1549

1550
 cleanup:
1551
    esxVI_String_Free(&propertyNameList);
1552 1553
    esxVI_ObjectContent_Free(&virtualMachine);
    VIR_FREE(name);
1554 1555 1556 1557 1558 1559 1560 1561 1562

    return domain;
}



static virDomainPtr
esxDomainLookupByName(virConnectPtr conn, const char *name)
{
M
Matthias Bolte 已提交
1563
    esxPrivate *priv = conn->privateData;
1564 1565 1566
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
1567 1568
    int id = -1;
    unsigned char uuid[VIR_UUID_BUFLEN];
1569 1570
    virDomainPtr domain = NULL;

1571
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1572
        return NULL;
1573

1574
    if (esxVI_String_AppendValueListToList(&propertyNameList,
1575
                                           "configStatus\0"
1576
                                           "runtime.powerState\0"
1577
                                           "config.uuid\0") < 0 ||
1578
        esxVI_LookupVirtualMachineByName(priv->primary, name, propertyNameList,
1579 1580
                                         &virtualMachine,
                                         esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
1581
        goto cleanup;
1582 1583
    }

1584
    if (!virtualMachine) {
1585
        virReportError(VIR_ERR_NO_DOMAIN, _("No domain with name '%s'"), name);
M
Matthias Bolte 已提交
1586
        goto cleanup;
1587
    }
1588

M
Matthias Bolte 已提交
1589 1590 1591
    if (esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, uuid) < 0 ||
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
        goto cleanup;
1592
    }
1593

1594
    /* Only running/suspended virtual machines have an ID != -1 */
1595 1596 1597 1598
    if (powerState == esxVI_VirtualMachinePowerState_PoweredOff)
        id = -1;

    domain = virGetDomain(conn, name, uuid, id);
1599

1600
 cleanup:
1601
    esxVI_String_Free(&propertyNameList);
1602
    esxVI_ObjectContent_Free(&virtualMachine);
1603 1604 1605 1606 1607 1608 1609 1610 1611

    return domain;
}



static int
esxDomainSuspend(virDomainPtr domain)
{
M
Matthias Bolte 已提交
1612
    int result = -1;
M
Matthias Bolte 已提交
1613
    esxPrivate *priv = domain->conn->privateData;
1614 1615 1616 1617 1618
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
1619
    char *taskInfoErrorMessage = NULL;
1620

1621
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1622
        return -1;
1623

1624
    if (esxVI_String_AppendValueToList(&propertyNameList,
1625
                                       "runtime.powerState") < 0 ||
1626
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
1627
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
1628
           priv->parsedUri->autoAnswer) < 0 ||
1629
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1630
        goto cleanup;
1631 1632 1633
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1634 1635
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1636
        goto cleanup;
1637 1638
    }

1639 1640
    if (esxVI_SuspendVM_Task(priv->primary, virtualMachine->obj, &task) < 0 ||
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
1641
                                    esxVI_Occurrence_RequiredItem,
1642
                                    priv->parsedUri->autoAnswer, &taskInfoState,
1643
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
1644
        goto cleanup;
1645 1646 1647
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
1648 1649
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not suspend domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
1650
        goto cleanup;
1651 1652
    }

M
Matthias Bolte 已提交
1653 1654
    result = 0;

1655
 cleanup:
1656 1657 1658
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
1659
    VIR_FREE(taskInfoErrorMessage);
1660 1661 1662 1663 1664 1665 1666 1667 1668

    return result;
}



static int
esxDomainResume(virDomainPtr domain)
{
M
Matthias Bolte 已提交
1669
    int result = -1;
M
Matthias Bolte 已提交
1670
    esxPrivate *priv = domain->conn->privateData;
1671 1672 1673 1674 1675
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
1676
    char *taskInfoErrorMessage = NULL;
1677

1678
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1679
        return -1;
1680

1681
    if (esxVI_String_AppendValueToList(&propertyNameList,
1682
                                       "runtime.powerState") < 0 ||
1683
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
1684
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
1685
           priv->parsedUri->autoAnswer) < 0 ||
1686
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1687
        goto cleanup;
1688 1689 1690
    }

    if (powerState != esxVI_VirtualMachinePowerState_Suspended) {
1691
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not suspended"));
M
Matthias Bolte 已提交
1692
        goto cleanup;
1693 1694
    }

1695
    if (esxVI_PowerOnVM_Task(priv->primary, virtualMachine->obj, NULL,
1696
                             &task) < 0 ||
1697
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
1698
                                    esxVI_Occurrence_RequiredItem,
1699
                                    priv->parsedUri->autoAnswer, &taskInfoState,
1700
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
1701
        goto cleanup;
1702 1703 1704
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
1705 1706
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not resume domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
1707
        goto cleanup;
1708 1709
    }

M
Matthias Bolte 已提交
1710 1711
    result = 0;

1712
 cleanup:
1713 1714 1715
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
1716
    VIR_FREE(taskInfoErrorMessage);
1717 1718 1719 1720 1721 1722 1723

    return result;
}



static int
1724
esxDomainShutdownFlags(virDomainPtr domain, unsigned int flags)
1725
{
M
Matthias Bolte 已提交
1726
    int result = -1;
M
Matthias Bolte 已提交
1727
    esxPrivate *priv = domain->conn->privateData;
1728 1729 1730 1731
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

1732 1733
    virCheckFlags(0, -1);

1734
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1735
        return -1;
1736

1737
    if (esxVI_String_AppendValueToList(&propertyNameList,
1738
                                       "runtime.powerState") < 0 ||
1739
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
1740
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
1741
                                         esxVI_Occurrence_RequiredItem) < 0 ||
1742
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1743
        goto cleanup;
1744 1745 1746
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1747 1748
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1749
        goto cleanup;
1750 1751
    }

1752
    if (esxVI_ShutdownGuest(priv->primary, virtualMachine->obj) < 0)
M
Matthias Bolte 已提交
1753
        goto cleanup;
1754

M
Matthias Bolte 已提交
1755 1756
    result = 0;

1757
 cleanup:
1758 1759 1760 1761 1762 1763 1764
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}


1765 1766 1767 1768 1769 1770
static int
esxDomainShutdown(virDomainPtr domain)
{
    return esxDomainShutdownFlags(domain, 0);
}

1771 1772

static int
E
Eric Blake 已提交
1773
esxDomainReboot(virDomainPtr domain, unsigned int flags)
1774
{
M
Matthias Bolte 已提交
1775
    int result = -1;
M
Matthias Bolte 已提交
1776
    esxPrivate *priv = domain->conn->privateData;
1777 1778 1779 1780
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

E
Eric Blake 已提交
1781 1782
    virCheckFlags(0, -1);

1783
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1784
        return -1;
1785

1786
    if (esxVI_String_AppendValueToList(&propertyNameList,
1787
                                       "runtime.powerState") < 0 ||
1788
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
1789
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
1790
                                         esxVI_Occurrence_RequiredItem) < 0 ||
1791
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1792
        goto cleanup;
1793 1794 1795
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1796 1797
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1798
        goto cleanup;
1799 1800
    }

1801
    if (esxVI_RebootGuest(priv->primary, virtualMachine->obj) < 0)
M
Matthias Bolte 已提交
1802
        goto cleanup;
1803

M
Matthias Bolte 已提交
1804 1805
    result = 0;

1806
 cleanup:
1807 1808 1809 1810 1811 1812 1813 1814 1815
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}



static int
1816 1817
esxDomainDestroyFlags(virDomainPtr domain,
                      unsigned int flags)
1818
{
M
Matthias Bolte 已提交
1819
    int result = -1;
M
Matthias Bolte 已提交
1820
    esxPrivate *priv = domain->conn->privateData;
1821
    esxVI_Context *ctx = NULL;
1822 1823 1824 1825 1826
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
1827
    char *taskInfoErrorMessage = NULL;
1828

1829 1830
    virCheckFlags(0, -1);

1831
    if (priv->vCenter) {
1832 1833 1834 1835 1836
        ctx = priv->vCenter;
    } else {
        ctx = priv->host;
    }

1837
    if (esxVI_EnsureSession(ctx) < 0)
M
Matthias Bolte 已提交
1838
        return -1;
1839

1840
    if (esxVI_String_AppendValueToList(&propertyNameList,
1841
                                       "runtime.powerState") < 0 ||
1842
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
1843
          (ctx, domain->uuid, propertyNameList, &virtualMachine,
1844
           priv->parsedUri->autoAnswer) < 0 ||
1845
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
1846
        goto cleanup;
1847 1848 1849
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
1850 1851
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
M
Matthias Bolte 已提交
1852
        goto cleanup;
1853 1854
    }

1855
    if (esxVI_PowerOffVM_Task(ctx, virtualMachine->obj, &task) < 0 ||
1856 1857
        esxVI_WaitForTaskCompletion(ctx, task, domain->uuid,
                                    esxVI_Occurrence_RequiredItem,
1858
                                    priv->parsedUri->autoAnswer, &taskInfoState,
1859
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
1860
        goto cleanup;
1861 1862 1863
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
1864 1865
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not destroy domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
1866
        goto cleanup;
1867 1868
    }

1869
    domain->id = -1;
M
Matthias Bolte 已提交
1870 1871
    result = 0;

1872
 cleanup:
1873 1874 1875
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
1876
    VIR_FREE(taskInfoErrorMessage);
1877 1878 1879 1880 1881

    return result;
}


1882 1883 1884 1885 1886 1887
static int
esxDomainDestroy(virDomainPtr dom)
{
    return esxDomainDestroyFlags(dom, 0);
}

1888 1889

static char *
1890
esxDomainGetOSType(virDomainPtr domain ATTRIBUTE_UNUSED)
1891
{
1892
    char *osType;
1893

1894
    ignore_value(VIR_STRDUP(osType, "hvm"));
1895
    return osType;
1896 1897 1898 1899
}



1900
static unsigned long long
1901 1902
esxDomainGetMaxMemory(virDomainPtr domain)
{
M
Matthias Bolte 已提交
1903
    esxPrivate *priv = domain->conn->privateData;
1904 1905 1906 1907 1908
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    unsigned long memoryMB = 0;

1909
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1910
        return 0;
1911

1912
    if (esxVI_String_AppendValueToList(&propertyNameList,
1913
                                       "config.hardware.memoryMB") < 0 ||
1914
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
1915
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
1916
                                         esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
1917
        goto cleanup;
1918 1919
    }

1920
    for (dynamicProperty = virtualMachine->propSet; dynamicProperty;
1921 1922
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "config.hardware.memoryMB")) {
1923
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1924
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
1925
                goto cleanup;
1926 1927 1928
            }

            if (dynamicProperty->val->int32 < 0) {
1929 1930 1931
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Got invalid memory size %d"),
                               dynamicProperty->val->int32);
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
            } else {
                memoryMB = dynamicProperty->val->int32;
            }

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

1942
 cleanup:
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
    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 已提交
1954
    int result = -1;
M
Matthias Bolte 已提交
1955
    esxPrivate *priv = domain->conn->privateData;
1956
    esxVI_String *propertyNameList = NULL;
1957
    esxVI_ObjectContent *virtualMachine = NULL;
1958
    esxVI_VirtualMachinePowerState powerState;
1959 1960 1961
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
1962
    char *taskInfoErrorMessage = NULL;
1963

1964
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
1965
        return -1;
1966

1967 1968 1969 1970
    if (esxVI_String_AppendValueToList(&propertyNameList,
                                       "runtime.powerState") < 0 ||
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
1971
           priv->parsedUri->autoAnswer) < 0 ||
1972 1973 1974 1975 1976
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
        goto cleanup;
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
1977 1978
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered off"));
1979 1980 1981 1982
        goto cleanup;
    }

    if (esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
1983
        esxVI_Long_Alloc(&spec->memoryMB) < 0) {
M
Matthias Bolte 已提交
1984
        goto cleanup;
1985 1986
    }

1987
    /* max-memory must be a multiple of 4096 kilobyte */
1988
    spec->memoryMB->value =
1989
      VIR_DIV_UP(memory, 4096) * 4; /* Scale from kilobytes to megabytes */
1990

1991
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
1992
                              &task) < 0 ||
1993
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
1994
                                    esxVI_Occurrence_RequiredItem,
1995
                                    priv->parsedUri->autoAnswer, &taskInfoState,
1996
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
1997
        goto cleanup;
1998 1999 2000
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2001 2002 2003
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not set max-memory to %lu kilobytes: %s"), memory,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2004
        goto cleanup;
2005 2006
    }

M
Matthias Bolte 已提交
2007 2008
    result = 0;

2009
 cleanup:
2010
    esxVI_String_Free(&propertyNameList);
2011 2012 2013
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
2014
    VIR_FREE(taskInfoErrorMessage);
2015 2016 2017 2018 2019 2020 2021 2022 2023

    return result;
}



static int
esxDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
M
Matthias Bolte 已提交
2024
    int result = -1;
M
Matthias Bolte 已提交
2025
    esxPrivate *priv = domain->conn->privateData;
2026 2027 2028 2029
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
2030
    char *taskInfoErrorMessage = NULL;
2031

2032
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
2033
        return -1;
2034

2035
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
2036
          (priv->primary, domain->uuid, NULL, &virtualMachine,
2037
           priv->parsedUri->autoAnswer) < 0 ||
2038 2039 2040
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_ResourceAllocationInfo_Alloc(&spec->memoryAllocation) < 0 ||
        esxVI_Long_Alloc(&spec->memoryAllocation->limit) < 0) {
M
Matthias Bolte 已提交
2041
        goto cleanup;
2042 2043 2044
    }

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

2047
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
2048
                              &task) < 0 ||
2049
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
2050
                                    esxVI_Occurrence_RequiredItem,
2051
                                    priv->parsedUri->autoAnswer, &taskInfoState,
2052
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
2053
        goto cleanup;
2054 2055 2056
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2057 2058 2059
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not set memory to %lu kilobytes: %s"), memory,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2060
        goto cleanup;
2061 2062
    }

M
Matthias Bolte 已提交
2063 2064
    result = 0;

2065
 cleanup:
2066 2067 2068
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
2069
    VIR_FREE(taskInfoErrorMessage);
2070 2071 2072 2073 2074 2075

    return result;
}



2076 2077 2078 2079 2080 2081 2082 2083 2084
/*
 * 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

2085 2086 2087
static int
esxDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
M
Matthias Bolte 已提交
2088
    int result = -1;
M
Matthias Bolte 已提交
2089
    esxPrivate *priv = domain->conn->privateData;
2090 2091 2092 2093 2094
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_VirtualMachinePowerState powerState;
    int64_t memory_limit = -1;
2095
#if ESX_QUERY_FOR_USED_CPU_TIME
2096 2097 2098 2099 2100 2101 2102
    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;
2103 2104
    esxVI_PerfEntityMetricBase *perfEntityMetricBase = NULL;
    esxVI_PerfEntityMetricBase *perfEntityMetricBaseList = NULL;
2105 2106 2107
    esxVI_PerfEntityMetric *perfEntityMetric = NULL;
    esxVI_PerfMetricIntSeries *perfMetricIntSeries = NULL;
    esxVI_Long *value = NULL;
2108
#endif
2109

2110
    memset(info, 0, sizeof(*info));
M
Matthias Bolte 已提交
2111

2112
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
2113
        return -1;
2114

2115
    if (esxVI_String_AppendValueListToList(&propertyNameList,
2116 2117 2118 2119
                                           "runtime.powerState\0"
                                           "config.hardware.memoryMB\0"
                                           "config.hardware.numCPU\0"
                                           "config.memoryAllocation.limit\0") < 0 ||
2120
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
2121
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
2122
                                         esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
2123
        goto cleanup;
2124 2125 2126 2127
    }

    info->state = VIR_DOMAIN_NOSTATE;

2128
    for (dynamicProperty = virtualMachine->propSet; dynamicProperty;
2129 2130 2131
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "runtime.powerState")) {
            if (esxVI_VirtualMachinePowerState_CastFromAnyType
2132
                  (dynamicProperty->val, &powerState) < 0) {
M
Matthias Bolte 已提交
2133
                goto cleanup;
2134 2135
            }

2136 2137
            info->state = esxVI_VirtualMachinePowerState_ConvertToLibvirt
                            (powerState);
2138
        } else if (STREQ(dynamicProperty->name, "config.hardware.memoryMB")) {
2139
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2140
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
2141
                goto cleanup;
2142 2143 2144 2145
            }

            info->maxMem = dynamicProperty->val->int32 * 1024; /* Scale from megabyte to kilobyte */
        } else if (STREQ(dynamicProperty->name, "config.hardware.numCPU")) {
2146
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2147
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
2148
                goto cleanup;
2149 2150 2151 2152 2153
            }

            info->nrVirtCpu = dynamicProperty->val->int32;
        } else if (STREQ(dynamicProperty->name,
                         "config.memoryAllocation.limit")) {
2154
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2155
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
2156
                goto cleanup;
2157 2158 2159 2160
            }

            memory_limit = dynamicProperty->val->int64;

2161
            if (memory_limit > 0)
2162 2163 2164 2165 2166 2167 2168 2169 2170
                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;

2171
#if ESX_QUERY_FOR_USED_CPU_TIME
2172
    /* Verify the cached 'used CPU time' performance counter ID */
2173
    /* FIXME: Currently no host for a vpx:// connection */
2174
    if (priv->host) {
2175
        if (info->state == VIR_DOMAIN_RUNNING && priv->usedCpuTimeCounterId >= 0) {
2176
            if (esxVI_Int_Alloc(&counterId) < 0)
2177
                goto cleanup;
2178

2179
            counterId->value = priv->usedCpuTimeCounterId;
2180

2181
            if (esxVI_Int_AppendToList(&counterIdList, counterId) < 0)
2182
                goto cleanup;
2183

2184 2185 2186 2187
            if (esxVI_QueryPerfCounter(priv->host, counterIdList,
                                       &perfCounterInfo) < 0) {
                goto cleanup;
            }
2188

2189 2190 2191 2192 2193
            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);
2194

2195 2196 2197 2198 2199
                priv->usedCpuTimeCounterId = -1;
            }

            esxVI_Int_Free(&counterIdList);
            esxVI_PerfCounterInfo_Free(&perfCounterInfo);
2200 2201
        }

2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
        /*
         * 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;
            }
2212

2213
            for (perfMetricId = perfMetricIdList; perfMetricId;
2214 2215 2216
                 perfMetricId = perfMetricId->_next) {
                VIR_DEBUG("perfMetricId counterId %d, instance '%s'",
                          perfMetricId->counterId->value, perfMetricId->instance);
2217

2218
                counterId = NULL;
2219

2220 2221 2222 2223 2224
                if (esxVI_Int_DeepCopy(&counterId, perfMetricId->counterId) < 0 ||
                    esxVI_Int_AppendToList(&counterIdList, counterId) < 0) {
                    goto cleanup;
                }
            }
2225

2226 2227
            if (esxVI_QueryPerfCounter(priv->host, counterIdList,
                                       &perfCounterInfoList) < 0) {
M
Matthias Bolte 已提交
2228
                goto cleanup;
2229 2230
            }

2231
            for (perfCounterInfo = perfCounterInfoList; perfCounterInfo;
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
                 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;
                }
2248 2249
            }

2250
            if (priv->usedCpuTimeCounterId < 0)
2251
                VIR_WARN("Could not find 'used CPU time' performance counter");
2252 2253
        }

2254 2255 2256 2257 2258 2259
        /*
         * 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);
2260

2261 2262 2263 2264 2265 2266
            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;
            }
2267

2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
            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;
            }
2278

2279
            for (perfEntityMetricBase = perfEntityMetricBaseList;
2280
                 perfEntityMetricBase;
2281
                 perfEntityMetricBase = perfEntityMetricBase->_next) {
2282
                VIR_DEBUG("perfEntityMetric ...");
2283

2284 2285
                perfEntityMetric =
                  esxVI_PerfEntityMetric_DynamicCast(perfEntityMetricBase);
2286

2287
                if (!perfEntityMetric) {
2288 2289 2290
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("QueryPerf returned object with unexpected type '%s'"),
                                   esxVI_Type_ToString(perfEntityMetricBase->_type));
2291
                    goto cleanup;
2292
                }
2293

2294 2295
                perfMetricIntSeries =
                  esxVI_PerfMetricIntSeries_DynamicCast(perfEntityMetric->value);
2296

2297
                if (!perfMetricIntSeries) {
2298 2299 2300
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("QueryPerf returned object with unexpected type '%s'"),
                                   esxVI_Type_ToString(perfEntityMetric->value->_type));
2301
                    goto cleanup;
2302
                }
2303

2304
                for (; perfMetricIntSeries;
2305
                     perfMetricIntSeries = perfMetricIntSeries->_next) {
2306
                    VIR_DEBUG("perfMetricIntSeries ...");
2307

2308
                    for (value = perfMetricIntSeries->value;
2309
                         value;
2310 2311 2312
                         value = value->_next) {
                        VIR_DEBUG("value %lld", (long long int)value->value);
                    }
2313 2314 2315
                }
            }

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

2318
            /*
E
Eric Blake 已提交
2319
             * FIXME: Cannot map between relative used-cpu-time and absolute
2320 2321 2322
             *        info->cpuTime
             */
        }
2323
    }
2324
#endif
2325

M
Matthias Bolte 已提交
2326 2327
    result = 0;

2328
 cleanup:
2329
#if ESX_QUERY_FOR_USED_CPU_TIME
2330 2331 2332 2333
    /*
     * Remove values owned by data structures to prevent them from being freed
     * by the call to esxVI_PerfQuerySpec_Free().
     */
2334
    if (querySpec) {
2335 2336 2337
        querySpec->entity = NULL;
        querySpec->format = NULL;

2338
        if (querySpec->metricId)
2339 2340
            querySpec->metricId->instance = NULL;
    }
2341
#endif
2342

2343 2344
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);
2345
#if ESX_QUERY_FOR_USED_CPU_TIME
2346 2347 2348 2349
    esxVI_PerfMetricId_Free(&perfMetricIdList);
    esxVI_Int_Free(&counterIdList);
    esxVI_PerfCounterInfo_Free(&perfCounterInfoList);
    esxVI_PerfQuerySpec_Free(&querySpec);
2350
    esxVI_PerfEntityMetricBase_Free(&perfEntityMetricBaseList);
2351
#endif
2352 2353 2354 2355 2356 2357

    return result;
}



2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
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);

2372
    if (esxVI_EnsureSession(priv->primary) < 0)
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
        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;

2391
 cleanup:
2392 2393 2394 2395 2396 2397 2398 2399
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
}



2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
static char *
esxDomainScreenshot(virDomainPtr domain, virStreamPtr stream,
                    unsigned int screen, unsigned int flags)
{
    char *mimeType = NULL;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_Boolean supportsScreenshot = esxVI_Boolean_Undefined;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *url = NULL;

    virCheckFlags(0, NULL);

    if (screen != 0) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Screen cannot be selected"));
        return NULL;
    }

    supportsScreenshot = esxSupportsScreenshot(priv);

    if (supportsScreenshot == esxVI_Boolean_Undefined)
        return NULL;

    if (supportsScreenshot != esxVI_Boolean_True) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("Screenshot feature is unsupported"));
        return NULL;
    }

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

    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;

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOn) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered on"));
        goto cleanup;
    }

    /* Build URL */
    virBufferAsprintf(&buffer, "%s://%s:%d/screen?id=", priv->parsedUri->transport,
                      domain->conn->uri->server, domain->conn->uri->port);
    virBufferURIEncodeString(&buffer, virtualMachine->obj->value);

    if (virBufferCheckError(&buffer))
        goto cleanup;

    url = virBufferContentAndReset(&buffer);

    if (VIR_STRDUP(mimeType, "image/png") < 0)
        goto cleanup;

    if (esxStreamOpenDownload(stream, priv, url, 0, 0) < 0) {
        VIR_FREE(mimeType);
        goto cleanup;
    }

 cleanup:
    virBufferFreeAndReset(&buffer);

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

    return mimeType;
}



2478
static int
2479 2480
esxDomainSetVcpusFlags(virDomainPtr domain, unsigned int nvcpus,
                       unsigned int flags)
2481
{
M
Matthias Bolte 已提交
2482
    int result = -1;
M
Matthias Bolte 已提交
2483
    esxPrivate *priv = domain->conn->privateData;
M
Matthias Bolte 已提交
2484
    int maxVcpus;
2485 2486 2487 2488
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
2489
    char *taskInfoErrorMessage = NULL;
2490

2491
    if (flags != VIR_DOMAIN_AFFECT_LIVE) {
2492
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
2493 2494 2495
        return -1;
    }

2496
    if (nvcpus < 1) {
2497 2498
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Requested number of virtual CPUs must at least be 1"));
M
Matthias Bolte 已提交
2499
        return -1;
2500 2501
    }

2502
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
2503
        return -1;
2504

M
Matthias Bolte 已提交
2505
    maxVcpus = esxDomainGetMaxVcpus(domain);
2506

2507
    if (maxVcpus < 0)
M
Matthias Bolte 已提交
2508
        return -1;
2509

M
Matthias Bolte 已提交
2510
    if (nvcpus > maxVcpus) {
2511 2512 2513 2514
        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 已提交
2515
        return -1;
2516 2517
    }

2518
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
2519
          (priv->primary, domain->uuid, NULL, &virtualMachine,
2520
           priv->parsedUri->autoAnswer) < 0 ||
2521 2522
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_Int_Alloc(&spec->numCPUs) < 0) {
M
Matthias Bolte 已提交
2523
        goto cleanup;
2524 2525 2526 2527
    }

    spec->numCPUs->value = nvcpus;

2528
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
2529
                              &task) < 0 ||
2530
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
2531
                                    esxVI_Occurrence_RequiredItem,
2532
                                    priv->parsedUri->autoAnswer, &taskInfoState,
2533
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
2534
        goto cleanup;
2535 2536 2537
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2538 2539 2540
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not set number of virtual CPUs to %d: %s"), nvcpus,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2541
        goto cleanup;
2542 2543
    }

M
Matthias Bolte 已提交
2544 2545
    result = 0;

2546
 cleanup:
2547 2548 2549
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
2550
    VIR_FREE(taskInfoErrorMessage);
2551 2552 2553 2554 2555

    return result;
}


M
Matthias Bolte 已提交
2556

2557 2558 2559
static int
esxDomainSetVcpus(virDomainPtr domain, unsigned int nvcpus)
{
2560
    return esxDomainSetVcpusFlags(domain, nvcpus, VIR_DOMAIN_AFFECT_LIVE);
2561 2562
}

2563

M
Matthias Bolte 已提交
2564

2565
static int
2566
esxDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
2567
{
M
Matthias Bolte 已提交
2568
    esxPrivate *priv = domain->conn->privateData;
2569 2570 2571 2572
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;

2573
    if (flags != (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
2574
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"), flags);
2575 2576 2577
        return -1;
    }

2578
    if (priv->maxVcpus > 0)
M
Matthias Bolte 已提交
2579
        return priv->maxVcpus;
2580

M
Matthias Bolte 已提交
2581 2582
    priv->maxVcpus = -1;

2583
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
2584
        return -1;
2585

2586
    if (esxVI_String_AppendValueToList(&propertyNameList,
2587
                                       "capability.maxSupportedVcpus") < 0 ||
2588 2589
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0) {
M
Matthias Bolte 已提交
2590
        goto cleanup;
2591 2592
    }

2593
    for (dynamicProperty = hostSystem->propSet; dynamicProperty;
2594 2595
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "capability.maxSupportedVcpus")) {
2596
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
2597
                                         esxVI_Type_Int) < 0) {
M
Matthias Bolte 已提交
2598
                goto cleanup;
2599 2600
            }

M
Matthias Bolte 已提交
2601
            priv->maxVcpus = dynamicProperty->val->int32;
2602 2603 2604 2605 2606 2607
            break;
        } else {
            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
        }
    }

2608
 cleanup:
2609 2610 2611
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&hostSystem);

M
Matthias Bolte 已提交
2612
    return priv->maxVcpus;
2613 2614
}

M
Matthias Bolte 已提交
2615 2616


2617 2618 2619
static int
esxDomainGetMaxVcpus(virDomainPtr domain)
{
2620
    return esxDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2621 2622
                                           VIR_DOMAIN_VCPU_MAXIMUM));
}
2623

M
Matthias Bolte 已提交
2624 2625


2626
static char *
2627
esxDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2628
{
M
Matthias Bolte 已提交
2629
    esxPrivate *priv = domain->conn->privateData;
2630 2631
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
2632 2633
    esxVI_VirtualMachinePowerState powerState;
    int id;
2634
    char *moref = NULL;
2635
    char *vmPathName = NULL;
2636
    char *datastoreName = NULL;
M
Matthias Bolte 已提交
2637
    char *directoryName = NULL;
2638
    char *directoryAndFileName = NULL;
2639
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
2640 2641
    char *url = NULL;
    char *vmx = NULL;
2642
    virVMXContext ctx;
2643
    esxVMX_Data data;
2644 2645 2646
    virDomainDefPtr def = NULL;
    char *xml = NULL;

E
Eric Blake 已提交
2647 2648
    /* Flags checked by virDomainDefFormat */

2649
    memset(&data, 0, sizeof(data));
2650

2651
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
2652
        return NULL;
2653

2654 2655 2656
    if (esxVI_String_AppendValueListToList(&propertyNameList,
                                           "config.files.vmPathName\0"
                                           "runtime.powerState\0") < 0 ||
2657
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
2658
                                         propertyNameList, &virtualMachine,
2659
                                         esxVI_Occurrence_RequiredItem) < 0 ||
2660
        esxVI_GetVirtualMachineMORef(virtualMachine, &moref) < 0 ||
2661 2662
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0 ||
        esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, NULL) < 0 ||
2663 2664
        esxVI_GetStringValue(virtualMachine, "config.files.vmPathName",
                             &vmPathName, esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
2665
        goto cleanup;
2666 2667
    }

2668
    if (esxUtil_ParseDatastorePath(vmPathName, &datastoreName, &directoryName,
2669
                                   &directoryAndFileName) < 0) {
M
Matthias Bolte 已提交
2670
        goto cleanup;
2671 2672
    }

2673
    virBufferAsprintf(&buffer, "%s://%s:%d/folder/", priv->parsedUri->transport,
2674
                      domain->conn->uri->server, domain->conn->uri->port);
2675
    virBufferURIEncodeString(&buffer, directoryAndFileName);
2676
    virBufferAddLit(&buffer, "?dcPath=");
2677
    virBufferURIEncodeString(&buffer, priv->primary->datacenterPath);
2678 2679 2680
    virBufferAddLit(&buffer, "&dsName=");
    virBufferURIEncodeString(&buffer, datastoreName);

2681
    if (virBufferCheckError(&buffer) < 0)
M
Matthias Bolte 已提交
2682
        goto cleanup;
2683

2684 2685
    url = virBufferContentAndReset(&buffer);

2686
    if (esxVI_CURL_Download(priv->primary->curl, url, &vmx, 0, NULL) < 0)
M
Matthias Bolte 已提交
2687
        goto cleanup;
2688

2689
    data.ctx = priv->primary;
2690

2691
    if (!directoryName) {
2692
        if (virAsprintf(&data.datastorePathWithoutFileName, "[%s]",
2693
                        datastoreName) < 0)
2694 2695 2696
            goto cleanup;
    } else {
        if (virAsprintf(&data.datastorePathWithoutFileName, "[%s] %s",
2697
                        datastoreName, directoryName) < 0)
2698 2699
            goto cleanup;
    }
2700 2701 2702 2703 2704

    ctx.opaque = &data;
    ctx.parseFileName = esxParseVMXFileName;
    ctx.formatFileName = NULL;
    ctx.autodetectSCSIControllerModel = NULL;
2705
    ctx.datacenterPath = priv->primary->datacenterPath;
2706
    ctx.moref = moref;
2707

2708
    def = virVMXParseConfig(&ctx, priv->xmlopt, priv->caps, vmx);
2709

2710
    if (def) {
2711
        if (powerState != esxVI_VirtualMachinePowerState_PoweredOff)
2712 2713
            def->id = id;

2714
        xml = virDomainDefFormat(def, priv->caps,
2715
                                 virDomainDefFormatConvertXMLFlags(flags));
2716 2717
    }

2718
 cleanup:
2719
    if (!url)
M
Matthias Bolte 已提交
2720 2721
        virBufferFreeAndReset(&buffer);

2722 2723
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);
2724
    VIR_FREE(moref);
2725
    VIR_FREE(datastoreName);
M
Matthias Bolte 已提交
2726
    VIR_FREE(directoryName);
2727
    VIR_FREE(directoryAndFileName);
2728
    VIR_FREE(url);
2729
    VIR_FREE(data.datastorePathWithoutFileName);
2730
    VIR_FREE(vmx);
2731
    virDomainDefFree(def);
2732 2733 2734 2735 2736 2737 2738

    return xml;
}



static char *
2739 2740 2741
esxConnectDomainXMLFromNative(virConnectPtr conn, const char *nativeFormat,
                              const char *nativeConfig,
                              unsigned int flags)
2742
{
M
Matthias Bolte 已提交
2743
    esxPrivate *priv = conn->privateData;
2744
    virVMXContext ctx;
2745
    esxVMX_Data data;
2746 2747 2748
    virDomainDefPtr def = NULL;
    char *xml = NULL;

E
Eric Blake 已提交
2749 2750
    virCheckFlags(0, NULL);

2751
    memset(&data, 0, sizeof(data));
2752

2753
    if (STRNEQ(nativeFormat, "vmware-vmx")) {
2754 2755
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Unsupported config format '%s'"), nativeFormat);
2756
        return NULL;
2757 2758
    }

2759
    data.ctx = priv->primary;
2760
    data.datastorePathWithoutFileName = (char *)"[?] ?";
2761 2762 2763 2764 2765

    ctx.opaque = &data;
    ctx.parseFileName = esxParseVMXFileName;
    ctx.formatFileName = NULL;
    ctx.autodetectSCSIControllerModel = NULL;
2766
    ctx.datacenterPath = NULL;
2767
    ctx.moref = NULL;
2768

2769
    def = virVMXParseConfig(&ctx, priv->xmlopt, priv->caps, nativeConfig);
2770

2771
    if (def)
2772 2773
        xml = virDomainDefFormat(def, priv->caps,
                                 VIR_DOMAIN_DEF_FORMAT_INACTIVE);
2774 2775 2776 2777 2778 2779 2780 2781

    virDomainDefFree(def);

    return xml;
}



M
Matthias Bolte 已提交
2782
static char *
2783 2784 2785
esxConnectDomainXMLToNative(virConnectPtr conn, const char *nativeFormat,
                            const char *domainXml,
                            unsigned int flags)
M
Matthias Bolte 已提交
2786
{
M
Matthias Bolte 已提交
2787
    esxPrivate *priv = conn->privateData;
2788 2789
    int virtualHW_version;
    virVMXContext ctx;
2790
    esxVMX_Data data;
M
Matthias Bolte 已提交
2791 2792 2793
    virDomainDefPtr def = NULL;
    char *vmx = NULL;

E
Eric Blake 已提交
2794 2795
    virCheckFlags(0, NULL);

2796
    memset(&data, 0, sizeof(data));
2797

M
Matthias Bolte 已提交
2798
    if (STRNEQ(nativeFormat, "vmware-vmx")) {
2799 2800
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Unsupported config format '%s'"), nativeFormat);
M
Matthias Bolte 已提交
2801 2802 2803
        return NULL;
    }

2804
    virtualHW_version = esxVI_ProductVersionToDefaultVirtualHWVersion
2805
                          (priv->primary->productLine, priv->primary->productVersion);
2806

2807
    if (virtualHW_version < 0)
2808 2809
        return NULL;

2810
    def = virDomainDefParseString(domainXml, priv->caps, priv->xmlopt,
2811
                                  NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE);
M
Matthias Bolte 已提交
2812

2813
    if (!def)
M
Matthias Bolte 已提交
2814 2815
        return NULL;

2816
    data.ctx = priv->primary;
2817
    data.datastorePathWithoutFileName = NULL;
2818 2819 2820 2821 2822

    ctx.opaque = &data;
    ctx.parseFileName = NULL;
    ctx.formatFileName = esxFormatVMXFileName;
    ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;
2823
    ctx.datacenterPath = NULL;
2824
    ctx.moref = NULL;
2825

2826
    vmx = virVMXFormatConfig(&ctx, priv->xmlopt, def, virtualHW_version);
M
Matthias Bolte 已提交
2827 2828 2829 2830 2831 2832 2833 2834

    virDomainDefFree(def);

    return vmx;
}



2835
static int
2836
esxConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
2837
{
M
Matthias Bolte 已提交
2838
    bool success = false;
M
Matthias Bolte 已提交
2839
    esxPrivate *priv = conn->privateData;
2840 2841 2842 2843 2844
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;
    int count = 0;
2845
    size_t i;
2846

2847
    if (maxnames == 0)
2848 2849
        return 0;

2850
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
2851
        return -1;
2852

2853
    if (esxVI_String_AppendValueListToList(&propertyNameList,
2854 2855
                                           "name\0"
                                           "runtime.powerState\0") < 0 ||
2856 2857
        esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
                                       &virtualMachineList) < 0) {
M
Matthias Bolte 已提交
2858
        goto cleanup;
2859 2860
    }

2861
    for (virtualMachine = virtualMachineList; virtualMachine;
2862
         virtualMachine = virtualMachine->_next) {
2863
        if (esxVI_GetVirtualMachinePowerState(virtualMachine,
2864
                                              &powerState) < 0) {
M
Matthias Bolte 已提交
2865
            goto cleanup;
2866 2867
        }

2868
        if (powerState == esxVI_VirtualMachinePowerState_PoweredOn)
2869 2870
            continue;

2871
        names[count] = NULL;
2872

2873 2874 2875
        if (esxVI_GetVirtualMachineIdentity(virtualMachine, NULL, &names[count],
                                            NULL) < 0) {
            goto cleanup;
2876 2877
        }

2878 2879
        ++count;

2880
        if (count >= maxnames)
2881 2882 2883
            break;
    }

M
Matthias Bolte 已提交
2884
    success = true;
2885

2886
 cleanup:
M
Matthias Bolte 已提交
2887
    if (! success) {
2888
        for (i = 0; i < count; ++i)
M
Matthias Bolte 已提交
2889
            VIR_FREE(names[i]);
2890

M
Matthias Bolte 已提交
2891
        count = -1;
2892 2893
    }

M
Matthias Bolte 已提交
2894 2895
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);
2896

M
Matthias Bolte 已提交
2897
    return count;
2898 2899 2900 2901 2902
}



static int
2903
esxConnectNumOfDefinedDomains(virConnectPtr conn)
2904
{
M
Matthias Bolte 已提交
2905
    esxPrivate *priv = conn->privateData;
2906

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

2910
    return esxVI_LookupNumberOfDomainsByPowerState
2911
             (priv->primary, esxVI_VirtualMachinePowerState_PoweredOn, true);
2912 2913 2914 2915 2916
}



static int
2917
esxDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
2918
{
M
Matthias Bolte 已提交
2919
    int result = -1;
M
Matthias Bolte 已提交
2920
    esxPrivate *priv = domain->conn->privateData;
2921 2922 2923
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;
2924
    int id = -1;
2925 2926
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
2927
    char *taskInfoErrorMessage = NULL;
2928

2929 2930
    virCheckFlags(0, -1);

2931
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
2932
        return -1;
2933

2934
    if (esxVI_String_AppendValueToList(&propertyNameList,
2935
                                       "runtime.powerState") < 0 ||
2936
        esxVI_LookupVirtualMachineByUuidAndPrepareForTask
2937
          (priv->primary, domain->uuid, propertyNameList, &virtualMachine,
2938
           priv->parsedUri->autoAnswer) < 0 ||
2939 2940
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0 ||
        esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, NULL) < 0) {
M
Matthias Bolte 已提交
2941
        goto cleanup;
2942 2943 2944
    }

    if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
2945 2946
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not powered off"));
M
Matthias Bolte 已提交
2947
        goto cleanup;
2948 2949
    }

2950
    if (esxVI_PowerOnVM_Task(priv->primary, virtualMachine->obj, NULL,
2951
                             &task) < 0 ||
2952
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
2953
                                    esxVI_Occurrence_RequiredItem,
2954
                                    priv->parsedUri->autoAnswer, &taskInfoState,
2955
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
2956
        goto cleanup;
2957 2958 2959
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
2960 2961
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not start domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
2962
        goto cleanup;
2963 2964
    }

2965
    domain->id = id;
M
Matthias Bolte 已提交
2966 2967
    result = 0;

2968
 cleanup:
2969 2970 2971
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);
    esxVI_ManagedObjectReference_Free(&task);
2972
    VIR_FREE(taskInfoErrorMessage);
2973 2974 2975 2976

    return result;
}

2977 2978


2979 2980 2981 2982 2983
static int
esxDomainCreate(virDomainPtr domain)
{
    return esxDomainCreateWithFlags(domain, 0);
}
2984

2985 2986


M
Matthias Bolte 已提交
2987
static virDomainPtr
2988
esxDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
M
Matthias Bolte 已提交
2989
{
M
Matthias Bolte 已提交
2990
    esxPrivate *priv = conn->privateData;
M
Matthias Bolte 已提交
2991 2992
    virDomainDefPtr def = NULL;
    char *vmx = NULL;
2993
    size_t i;
2994
    virDomainDiskDefPtr disk = NULL;
M
Matthias Bolte 已提交
2995
    esxVI_ObjectContent *virtualMachine = NULL;
2996 2997
    int virtualHW_version;
    virVMXContext ctx;
2998
    esxVMX_Data data;
M
Matthias Bolte 已提交
2999 3000
    char *datastoreName = NULL;
    char *directoryName = NULL;
3001
    char *escapedName = NULL;
M
Matthias Bolte 已提交
3002 3003 3004 3005 3006 3007 3008 3009
    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;
3010
    char *taskInfoErrorMessage = NULL;
M
Matthias Bolte 已提交
3011
    virDomainPtr domain = NULL;
3012
    const char *src;
3013
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
M
Matthias Bolte 已提交
3014

3015 3016 3017
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
3018
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
3019

3020
    memset(&data, 0, sizeof(data));
3021

3022
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
3023
        return NULL;
M
Matthias Bolte 已提交
3024 3025

    /* Parse domain XML */
3026
    def = virDomainDefParseString(xml, priv->caps, priv->xmlopt,
3027
                                  NULL, parse_flags);
M
Matthias Bolte 已提交
3028

3029
    if (!def)
M
Matthias Bolte 已提交
3030
        return NULL;
M
Matthias Bolte 已提交
3031

3032 3033 3034
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

M
Matthias Bolte 已提交
3035
    /* Check if an existing domain should be edited */
3036
    if (esxVI_LookupVirtualMachineByUuid(priv->primary, def->uuid, NULL,
M
Matthias Bolte 已提交
3037
                                         &virtualMachine,
M
Matthias Bolte 已提交
3038
                                         esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
3039
        goto cleanup;
M
Matthias Bolte 已提交
3040 3041
    }

3042
    if (!virtualMachine &&
3043 3044 3045 3046 3047 3048
        esxVI_LookupVirtualMachineByName(priv->primary, def->name, NULL,
                                         &virtualMachine,
                                         esxVI_Occurrence_OptionalItem) < 0) {
        goto cleanup;
    }

3049
    if (virtualMachine) {
M
Matthias Bolte 已提交
3050
        /* FIXME */
3051 3052 3053
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain already exists, editing existing domains is not "
                         "supported yet"));
M
Matthias Bolte 已提交
3054
        goto cleanup;
M
Matthias Bolte 已提交
3055 3056 3057
    }

    /* Build VMX from domain XML */
3058
    virtualHW_version = esxVI_ProductVersionToDefaultVirtualHWVersion
3059
                          (priv->primary->productLine, priv->primary->productVersion);
3060

3061
    if (virtualHW_version < 0)
3062 3063
        goto cleanup;

3064
    data.ctx = priv->primary;
3065
    data.datastorePathWithoutFileName = NULL;
3066 3067 3068 3069 3070

    ctx.opaque = &data;
    ctx.parseFileName = NULL;
    ctx.formatFileName = esxFormatVMXFileName;
    ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;
3071
    ctx.datacenterPath = NULL;
3072
    ctx.moref = NULL;
3073

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

3076
    if (!vmx)
M
Matthias Bolte 已提交
3077
        goto cleanup;
M
Matthias Bolte 已提交
3078

3079 3080 3081
    /*
     * 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
N
Nehal J Wani 已提交
3082
     * first disk, because it may be CDROM disk and ISO images are normally not
3083 3084 3085
     * 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 已提交
3086
    if (def->ndisks < 1) {
3087 3088 3089
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain XML doesn't contain any disks, cannot deduce "
                         "datastore and path for VMX file"));
M
Matthias Bolte 已提交
3090
        goto cleanup;
3091 3092 3093 3094
    }

    for (i = 0; i < def->ndisks; ++i) {
        if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
E
Eric Blake 已提交
3095
            virDomainDiskGetType(def->disks[i]) == VIR_STORAGE_TYPE_FILE) {
3096 3097 3098 3099 3100
            disk = def->disks[i];
            break;
        }
    }

3101
    if (!disk) {
3102 3103 3104
        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 已提交
3105
        goto cleanup;
M
Matthias Bolte 已提交
3106 3107
    }

3108 3109
    src = virDomainDiskGetSource(disk);
    if (!src) {
3110 3111 3112
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("First file-based harddisk has no source, cannot deduce "
                         "datastore and path for VMX file"));
M
Matthias Bolte 已提交
3113
        goto cleanup;
M
Matthias Bolte 已提交
3114 3115
    }

3116
    if (esxUtil_ParseDatastorePath(src, &datastoreName, &directoryName,
3117
                                   NULL) < 0) {
M
Matthias Bolte 已提交
3118
        goto cleanup;
M
Matthias Bolte 已提交
3119 3120
    }

3121
    if (! virFileHasSuffix(src, ".vmdk")) {
3122 3123
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting source '%s' of first file-based harddisk to "
3124
                         "be a VMDK image"), src);
M
Matthias Bolte 已提交
3125
        goto cleanup;
M
Matthias Bolte 已提交
3126 3127
    }

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

3131
    if (directoryName) {
M
Matthias Bolte 已提交
3132 3133 3134 3135
        virBufferURIEncodeString(&buffer, directoryName);
        virBufferAddChar(&buffer, '/');
    }

3136 3137
    escapedName = esxUtil_EscapeDatastoreItem(def->name);

3138
    if (!escapedName)
3139 3140 3141
        goto cleanup;

    virBufferURIEncodeString(&buffer, escapedName);
M
Matthias Bolte 已提交
3142
    virBufferAddLit(&buffer, ".vmx?dcPath=");
3143
    virBufferURIEncodeString(&buffer, priv->primary->datacenterPath);
M
Matthias Bolte 已提交
3144 3145 3146
    virBufferAddLit(&buffer, "&dsName=");
    virBufferURIEncodeString(&buffer, datastoreName);

3147
    if (virBufferCheckError(&buffer) < 0)
M
Matthias Bolte 已提交
3148
        goto cleanup;
M
Matthias Bolte 已提交
3149 3150 3151

    url = virBufferContentAndReset(&buffer);

3152 3153 3154 3155 3156 3157
    /* Check, if VMX file already exists */
    /* FIXME */

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

3158
    if (esxVI_CURL_Upload(priv->primary->curl, url, vmx) < 0)
3159 3160 3161
        goto cleanup;

    /* Register the domain */
3162
    if (directoryName) {
M
Matthias Bolte 已提交
3163
        if (virAsprintf(&datastoreRelatedPath, "[%s] %s/%s.vmx", datastoreName,
3164
                        directoryName, escapedName) < 0)
M
Matthias Bolte 已提交
3165
            goto cleanup;
M
Matthias Bolte 已提交
3166 3167
    } else {
        if (virAsprintf(&datastoreRelatedPath, "[%s] %s.vmx", datastoreName,
3168
                        escapedName) < 0)
M
Matthias Bolte 已提交
3169
            goto cleanup;
M
Matthias Bolte 已提交
3170 3171
    }

3172
    if (esxVI_RegisterVM_Task(priv->primary, priv->primary->datacenter->vmFolder,
M
Matthias Bolte 已提交
3173
                              datastoreRelatedPath, NULL, esxVI_Boolean_False,
3174 3175 3176 3177
                              priv->primary->computeResource->resourcePool,
                              priv->primary->hostSystem->_reference,
                              &task) < 0 ||
        esxVI_WaitForTaskCompletion(priv->primary, task, def->uuid,
3178
                                    esxVI_Occurrence_OptionalItem,
3179
                                    priv->parsedUri->autoAnswer, &taskInfoState,
3180
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
3181
        goto cleanup;
M
Matthias Bolte 已提交
3182 3183 3184
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
3185 3186
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not define domain: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
3187
        goto cleanup;
M
Matthias Bolte 已提交
3188 3189
    }

3190
    domain = virGetDomain(conn, def->name, def->uuid, -1);
M
Matthias Bolte 已提交
3191 3192 3193

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

3194
 cleanup:
3195
    if (!url)
M
Matthias Bolte 已提交
3196 3197
        virBufferFreeAndReset(&buffer);

M
Matthias Bolte 已提交
3198 3199 3200 3201
    virDomainDefFree(def);
    VIR_FREE(vmx);
    VIR_FREE(datastoreName);
    VIR_FREE(directoryName);
3202
    VIR_FREE(escapedName);
M
Matthias Bolte 已提交
3203 3204 3205 3206 3207 3208 3209
    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);
3210
    VIR_FREE(taskInfoErrorMessage);
M
Matthias Bolte 已提交
3211 3212 3213 3214

    return domain;
}

3215 3216 3217 3218 3219
static virDomainPtr
esxDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return esxDomainDefineXMLFlags(conn, xml, 0);
}
M
Matthias Bolte 已提交
3220

3221
static int
3222 3223
esxDomainUndefineFlags(virDomainPtr domain,
                       unsigned int flags)
3224
{
M
Matthias Bolte 已提交
3225
    int result = -1;
M
Matthias Bolte 已提交
3226
    esxPrivate *priv = domain->conn->privateData;
3227
    esxVI_Context *ctx = NULL;
3228 3229 3230 3231
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

3232 3233 3234 3235
    /* 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);
3236

3237
    if (priv->vCenter) {
3238 3239 3240 3241 3242
        ctx = priv->vCenter;
    } else {
        ctx = priv->host;
    }

3243
    if (esxVI_EnsureSession(ctx) < 0)
M
Matthias Bolte 已提交
3244
        return -1;
3245

3246
    if (esxVI_String_AppendValueToList(&propertyNameList,
3247
                                       "runtime.powerState") < 0 ||
3248 3249
        esxVI_LookupVirtualMachineByUuid(ctx, domain->uuid, propertyNameList,
                                         &virtualMachine,
M
Matthias Bolte 已提交
3250
                                         esxVI_Occurrence_RequiredItem) < 0 ||
3251
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
3252
        goto cleanup;
3253 3254 3255 3256
    }

    if (powerState != esxVI_VirtualMachinePowerState_Suspended &&
        powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
3257 3258
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not suspended or powered off"));
M
Matthias Bolte 已提交
3259
        goto cleanup;
3260 3261
    }

3262
    if (esxVI_UnregisterVM(ctx, virtualMachine->obj) < 0)
M
Matthias Bolte 已提交
3263
        goto cleanup;
3264

M
Matthias Bolte 已提交
3265 3266
    result = 0;

3267
 cleanup:
3268 3269 3270 3271 3272 3273 3274
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}


3275 3276 3277 3278 3279
static int
esxDomainUndefine(virDomainPtr domain)
{
    return esxDomainUndefineFlags(domain, 0);
}
3280

3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293
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;

3294
    if (esxVI_EnsureSession(priv->primary) < 0)
3295 3296 3297
        return -1;

    /* Check general autostart config */
3298
    if (esxVI_LookupAutoStartDefaults(priv->primary, &defaults) < 0)
3299 3300 3301 3302 3303 3304 3305 3306 3307
        goto cleanup;

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

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

3311
    if (!powerInfoList) {
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322
        /* 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;
    }

3323
    for (powerInfo = powerInfoList; powerInfo;
3324 3325
         powerInfo = powerInfo->_next) {
        if (STREQ(powerInfo->key->value, virtualMachine->obj->value)) {
3326
            if (STRCASEEQ(powerInfo->startAction, "powerOn"))
3327 3328 3329 3330 3331 3332 3333 3334
                *autostart = 1;

            break;
        }
    }

    result = 0;

3335
 cleanup:
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
    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;

3358
    if (esxVI_EnsureSession(priv->primary) < 0)
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
        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.
         */
3375
        if (esxVI_LookupAutoStartDefaults(priv->primary, &defaults) < 0)
3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
            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;
            }

3390
            for (powerInfo = powerInfoList; powerInfo;
3391 3392
                 powerInfo = powerInfo->_next) {
                if (STRNEQ(powerInfo->key->value, virtualMachine->obj->value)) {
3393 3394 3395
                    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                                   _("Cannot enable general autostart option "
                                     "without affecting other domains"));
3396 3397 3398 3399 3400
                    goto cleanup;
                }
            }

            /* Enable autostart in general */
3401
            if (esxVI_AutoStartDefaults_Alloc(&spec->defaults) < 0)
3402 3403 3404 3405 3406 3407 3408 3409 3410
                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 ||
3411
        esxVI_Int_Alloc(&newPowerInfo->stopDelay) < 0) {
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
        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";

3423 3424 3425 3426 3427
    if (esxVI_AutoStartPowerInfo_AppendToList(&spec->powerInfo,
                                              newPowerInfo) < 0) {
        goto cleanup;
    }

3428
    newPowerInfo = NULL;
3429

3430 3431 3432 3433 3434 3435 3436 3437 3438
    if (esxVI_ReconfigureAutostart
          (priv->primary,
           priv->primary->hostSystem->configManager->autoStartManager,
           spec) < 0) {
        goto cleanup;
    }

    result = 0;

3439
 cleanup:
3440
    if (newPowerInfo) {
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450
        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);

3451
    esxVI_AutoStartPowerInfo_Free(&newPowerInfo);
3452

3453 3454 3455 3456 3457
    return result;
}



3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
/*
 * 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:
 *
3468
 * - reservation (VIR_TYPED_PARAM_LLONG >= 0, in megaherz)
3469
 *
3470
 *   The amount of CPU resource that is guaranteed to be available to the domain.
3471 3472
 *
 *
3473
 * - limit (VIR_TYPED_PARAM_LLONG >= 0, or -1, in megaherz)
3474
 *
3475 3476
 *   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
3477 3478 3479 3480
 *   utilization of the domain is unlimited. If the limit is not set to -1, it
 *   must be greater than or equal to the reservation.
 *
 *
3481
 * - shares (VIR_TYPED_PARAM_INT >= 0, or in {-1, -2, -3}, no unit)
3482 3483 3484 3485 3486 3487
 *
 *   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'.
 */
3488
static char *
3489
esxDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED, int *nparams)
3490
{
3491
    char *type;
3492

3493
    if (VIR_STRDUP(type, "allocation") < 0)
3494
        return NULL;
3495

3496
    if (nparams)
3497
        *nparams = 3; /* reservation, limit, shares */
3498 3499 3500 3501 3502 3503 3504

    return type;
}



static int
3505 3506 3507
esxDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                     virTypedParameterPtr params, int *nparams,
                                     unsigned int flags)
3508
{
M
Matthias Bolte 已提交
3509
    int result = -1;
M
Matthias Bolte 已提交
3510
    esxPrivate *priv = domain->conn->privateData;
3511 3512 3513 3514 3515
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_SharesInfo *sharesInfo = NULL;
    unsigned int mask = 0;
3516
    size_t i = 0;
3517

3518 3519
    virCheckFlags(0, -1);

3520
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
3521
        return -1;
3522

3523
    if (esxVI_String_AppendValueListToList(&propertyNameList,
3524 3525 3526
                                           "config.cpuAllocation.reservation\0"
                                           "config.cpuAllocation.limit\0"
                                           "config.cpuAllocation.shares\0") < 0 ||
3527
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
3528
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
3529
                                         esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
3530
        goto cleanup;
3531 3532 3533
    }

    for (dynamicProperty = virtualMachine->propSet;
3534
         dynamicProperty && mask != 7 && i < 3 && i < *nparams;
3535 3536
         dynamicProperty = dynamicProperty->_next) {
        if (STREQ(dynamicProperty->name, "config.cpuAllocation.reservation") &&
M
Matthias Bolte 已提交
3537
            ! (mask & (1 << 0))) {
3538
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
3539
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
3540
                goto cleanup;
3541
            }
3542 3543 3544 3545 3546
            if (virTypedParameterAssign(&params[i],
                                        VIR_DOMAIN_SCHEDULER_RESERVATION,
                                        VIR_TYPED_PARAM_LLONG,
                                        dynamicProperty->val->int64) < 0)
                goto cleanup;
3547 3548 3549 3550
            mask |= 1 << 0;
            ++i;
        } else if (STREQ(dynamicProperty->name,
                         "config.cpuAllocation.limit") &&
M
Matthias Bolte 已提交
3551
                   ! (mask & (1 << 1))) {
3552
            if (esxVI_AnyType_ExpectType(dynamicProperty->val,
3553
                                         esxVI_Type_Long) < 0) {
M
Matthias Bolte 已提交
3554
                goto cleanup;
3555
            }
3556 3557 3558 3559 3560
            if (virTypedParameterAssign(&params[i],
                                        VIR_DOMAIN_SCHEDULER_LIMIT,
                                        VIR_TYPED_PARAM_LLONG,
                                        dynamicProperty->val->int64) < 0)
                goto cleanup;
3561 3562 3563 3564
            mask |= 1 << 1;
            ++i;
        } else if (STREQ(dynamicProperty->name,
                         "config.cpuAllocation.shares") &&
M
Matthias Bolte 已提交
3565
                   ! (mask & (1 << 2))) {
3566 3567 3568 3569
            if (virTypedParameterAssign(&params[i],
                                        VIR_DOMAIN_SCHEDULER_SHARES,
                                        VIR_TYPED_PARAM_INT, 0) < 0)
                goto cleanup;
3570
            if (esxVI_SharesInfo_CastFromAnyType(dynamicProperty->val,
3571
                                                 &sharesInfo) < 0) {
M
Matthias Bolte 已提交
3572
                goto cleanup;
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
            }

            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;

3592
              case esxVI_SharesLevel_Undefined:
3593
              default:
3594
                virReportEnumRangeError(esxVI_SharesLevel, sharesInfo->level);
3595
                esxVI_SharesInfo_Free(&sharesInfo);
M
Matthias Bolte 已提交
3596
                goto cleanup;
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
            }

            esxVI_SharesInfo_Free(&sharesInfo);

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

    *nparams = i;
M
Matthias Bolte 已提交
3609
    result = 0;
3610

3611
 cleanup:
3612 3613 3614 3615 3616 3617
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
}

3618 3619 3620 3621 3622 3623
static int
esxDomainGetSchedulerParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int *nparams)
{
    return esxDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
}
3624 3625 3626


static int
3627 3628 3629
esxDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                     virTypedParameterPtr params, int nparams,
                                     unsigned int flags)
3630
{
M
Matthias Bolte 已提交
3631
    int result = -1;
M
Matthias Bolte 已提交
3632
    esxPrivate *priv = domain->conn->privateData;
3633 3634 3635 3636 3637
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachineConfigSpec *spec = NULL;
    esxVI_SharesInfo *sharesInfo = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
3638
    char *taskInfoErrorMessage = NULL;
3639
    size_t i;
3640

3641
    virCheckFlags(0, -1);
3642 3643 3644 3645 3646 3647 3648 3649
    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)
3650
        return -1;
3651

3652
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
3653
        return -1;
3654

3655
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
3656
          (priv->primary, domain->uuid, NULL, &virtualMachine,
3657
           priv->parsedUri->autoAnswer) < 0 ||
3658 3659
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_ResourceAllocationInfo_Alloc(&spec->cpuAllocation) < 0) {
M
Matthias Bolte 已提交
3660
        goto cleanup;
3661 3662 3663
    }

    for (i = 0; i < nparams; ++i) {
3664
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_RESERVATION)) {
3665
            if (esxVI_Long_Alloc(&spec->cpuAllocation->reservation) < 0)
M
Matthias Bolte 已提交
3666
                goto cleanup;
3667 3668

            if (params[i].value.l < 0) {
3669 3670 3671
                virReportError(VIR_ERR_INVALID_ARG,
                               _("Could not set reservation to %lld MHz, expecting "
                                 "positive value"), params[i].value.l);
M
Matthias Bolte 已提交
3672
                goto cleanup;
3673 3674 3675
            }

            spec->cpuAllocation->reservation->value = params[i].value.l;
3676
        } else if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_LIMIT)) {
3677
            if (esxVI_Long_Alloc(&spec->cpuAllocation->limit) < 0)
M
Matthias Bolte 已提交
3678
                goto cleanup;
3679 3680

            if (params[i].value.l < -1) {
3681 3682 3683 3684
                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 已提交
3685
                goto cleanup;
3686 3687 3688
            }

            spec->cpuAllocation->limit->value = params[i].value.l;
3689
        } else if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_SHARES)) {
3690 3691
            if (esxVI_SharesInfo_Alloc(&sharesInfo) < 0 ||
                esxVI_Int_Alloc(&sharesInfo->shares) < 0) {
M
Matthias Bolte 已提交
3692
                goto cleanup;
3693 3694 3695
            }

            spec->cpuAllocation->shares = sharesInfo;
3696
            sharesInfo = NULL;
3697

3698
            if (params[i].value.i >= 0) {
3699
                spec->cpuAllocation->shares->level = esxVI_SharesLevel_Custom;
3700
                spec->cpuAllocation->shares->shares->value = params[i].value.i;
3701
            } else {
3702
                switch (params[i].value.i) {
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
                  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:
3721 3722 3723 3724
                    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 已提交
3725
                    goto cleanup;
3726 3727 3728 3729 3730
                }
            }
        }
    }

3731
    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
3732
                              &task) < 0 ||
3733
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
3734
                                    esxVI_Occurrence_RequiredItem,
3735
                                    priv->parsedUri->autoAnswer, &taskInfoState,
3736
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
3737
        goto cleanup;
3738 3739 3740
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
3741 3742 3743
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not change scheduler parameters: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
3744
        goto cleanup;
3745 3746
    }

M
Matthias Bolte 已提交
3747 3748
    result = 0;

3749
 cleanup:
3750
    esxVI_SharesInfo_Free(&sharesInfo);
3751 3752 3753
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
3754
    VIR_FREE(taskInfoErrorMessage);
3755 3756 3757 3758

    return result;
}

3759 3760 3761 3762 3763 3764
static int
esxDomainSetSchedulerParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int nparams)
{
    return esxDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
}
3765

E
Eric Blake 已提交
3766
/* The subset of migration flags we are able to support.  */
3767 3768 3769 3770
#define ESX_MIGRATION_FLAGS \
    (VIR_MIGRATE_PERSIST_DEST | \
     VIR_MIGRATE_UNDEFINE_SOURCE | \
     VIR_MIGRATE_LIVE | \
E
Eric Blake 已提交
3771
     VIR_MIGRATE_PAUSED)
3772 3773 3774 3775 3776

static int
esxDomainMigratePrepare(virConnectPtr dconn,
                        char **cookie ATTRIBUTE_UNUSED,
                        int *cookielen ATTRIBUTE_UNUSED,
3777 3778
                        const char *uri_in ATTRIBUTE_UNUSED,
                        char **uri_out,
E
Eric Blake 已提交
3779
                        unsigned long flags,
3780 3781 3782
                        const char *dname ATTRIBUTE_UNUSED,
                        unsigned long resource ATTRIBUTE_UNUSED)
{
3783
    esxPrivate *priv = dconn->privateData;
3784

E
Eric Blake 已提交
3785 3786
    virCheckFlags(ESX_MIGRATION_FLAGS, -1);

3787
    if (!uri_in) {
3788 3789 3790
        if (virAsprintf(uri_out, "vpxmigr://%s/%s/%s",
                        priv->vCenter->ipAddress,
                        priv->vCenter->computeResource->resourcePool->value,
3791
                        priv->vCenter->hostSystem->_reference->value) < 0)
3792
            return -1;
3793 3794
    }

3795
    return 0;
3796 3797 3798 3799 3800 3801 3802 3803 3804
}



static int
esxDomainMigratePerform(virDomainPtr domain,
                        const char *cookie ATTRIBUTE_UNUSED,
                        int cookielen ATTRIBUTE_UNUSED,
                        const char *uri,
E
Eric Blake 已提交
3805
                        unsigned long flags,
3806 3807 3808
                        const char *dname,
                        unsigned long bandwidth ATTRIBUTE_UNUSED)
{
M
Matthias Bolte 已提交
3809
    int result = -1;
M
Matthias Bolte 已提交
3810
    esxPrivate *priv = domain->conn->privateData;
M
Martin Kletzander 已提交
3811
    virURIPtr parsedUri = NULL;
3812 3813 3814
    char *saveptr;
    char *path_resourcePool;
    char *path_hostSystem;
3815
    esxVI_ObjectContent *virtualMachine = NULL;
3816 3817
    esxVI_ManagedObjectReference resourcePool;
    esxVI_ManagedObjectReference hostSystem;
3818 3819 3820
    esxVI_Event *eventList = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
3821
    char *taskInfoErrorMessage = NULL;
3822

E
Eric Blake 已提交
3823 3824
    virCheckFlags(ESX_MIGRATION_FLAGS, -1);

3825
    if (!priv->vCenter) {
3826 3827
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Migration not possible without a vCenter"));
M
Matthias Bolte 已提交
3828
        return -1;
3829 3830
    }

3831
    if (dname) {
3832 3833
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Renaming domains on migration not supported"));
M
Matthias Bolte 已提交
3834
        return -1;
3835 3836
    }

3837
    if (esxVI_EnsureSession(priv->vCenter) < 0)
M
Matthias Bolte 已提交
3838
        return -1;
3839

3840
    /* Parse migration URI */
3841
    if (!(parsedUri = virURIParse(uri)))
M
Matthias Bolte 已提交
3842
        return -1;
3843

3844
    if (!parsedUri->scheme || STRCASENEQ(parsedUri->scheme, "vpxmigr")) {
3845 3846
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Only vpxmigr:// migration URIs are supported"));
M
Matthias Bolte 已提交
3847
        goto cleanup;
3848 3849
    }

3850
    if (STRCASENEQ(priv->vCenter->ipAddress, parsedUri->server)) {
3851 3852 3853
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Migration source and destination have to refer to "
                         "the same vCenter"));
3854 3855 3856 3857 3858 3859
        goto cleanup;
    }

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

3860
    if (!path_resourcePool || !path_hostSystem) {
3861 3862
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Migration URI has to specify resource pool and host system"));
M
Matthias Bolte 已提交
3863
        goto cleanup;
3864 3865
    }

3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878
    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,
3879
           priv->parsedUri->autoAnswer) < 0) {
M
Matthias Bolte 已提交
3880
        goto cleanup;
3881 3882 3883
    }

    /* Validate the purposed migration */
3884
    if (esxVI_ValidateMigration(priv->vCenter, virtualMachine->obj,
3885 3886
                                esxVI_VirtualMachinePowerState_Undefined, NULL,
                                &resourcePool, &hostSystem, &eventList) < 0) {
M
Matthias Bolte 已提交
3887
        goto cleanup;
3888 3889
    }

3890
    if (eventList) {
3891 3892 3893 3894
        /*
         * FIXME: Need to report the complete list of events. Limit reporting
         *        to the first event for now.
         */
3895
        if (eventList->fullFormattedMessage) {
3896 3897 3898
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not migrate domain, validation reported a "
                             "problem: %s"), eventList->fullFormattedMessage);
3899
        } else {
3900 3901 3902
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not migrate domain, validation reported a "
                             "problem"));
3903 3904
        }

M
Matthias Bolte 已提交
3905
        goto cleanup;
3906 3907 3908
    }

    /* Perform the purposed migration */
3909 3910
    if (esxVI_MigrateVM_Task(priv->vCenter, virtualMachine->obj,
                             &resourcePool, &hostSystem,
3911 3912 3913
                             esxVI_VirtualMachineMovePriority_DefaultPriority,
                             esxVI_VirtualMachinePowerState_Undefined,
                             &task) < 0 ||
3914
        esxVI_WaitForTaskCompletion(priv->vCenter, task, domain->uuid,
3915
                                    esxVI_Occurrence_RequiredItem,
3916
                                    priv->parsedUri->autoAnswer, &taskInfoState,
3917
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
3918
        goto cleanup;
3919 3920 3921
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
3922 3923 3924 3925
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not migrate domain, migration task finished with "
                         "an error: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
3926
        goto cleanup;
3927 3928
    }

M
Matthias Bolte 已提交
3929 3930
    result = 0;

3931
 cleanup:
3932
    virURIFree(parsedUri);
3933 3934 3935
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_Event_Free(&eventList);
    esxVI_ManagedObjectReference_Free(&task);
3936
    VIR_FREE(taskInfoErrorMessage);
3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947

    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 已提交
3948
                       unsigned long flags)
3949
{
E
Eric Blake 已提交
3950 3951
    virCheckFlags(ESX_MIGRATION_FLAGS, NULL);

3952 3953 3954 3955 3956
    return esxDomainLookupByName(dconn, dname);
}



M
Matthias Bolte 已提交
3957 3958 3959 3960
static unsigned long long
esxNodeGetFreeMemory(virConnectPtr conn)
{
    unsigned long long result = 0;
3961
    unsigned long long usageBytes = 0;
M
Matthias Bolte 已提交
3962
    esxPrivate *priv = conn->privateData;
M
Matthias Bolte 已提交
3963
    esxVI_String *propertyNameList = NULL;
3964 3965 3966
    esxVI_ObjectContent *hostSystem = NULL;
    esxVI_Int *memoryUsage = NULL;
    esxVI_Long *memorySize = NULL;
M
Matthias Bolte 已提交
3967

3968
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
3969
        return 0;
M
Matthias Bolte 已提交
3970

3971 3972 3973 3974 3975 3976 3977 3978 3979 3980
    /* Get memory usage of host system */
    if (esxVI_String_AppendValueListToList(&propertyNameList,
                                           "summary.quickStats.overallMemoryUsage\0"
                                           "hardware.memorySize\0") < 0 ||
        esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
                                         &hostSystem) < 0 ||
        esxVI_GetInt(hostSystem, "summary.quickStats.overallMemoryUsage",
                      &memoryUsage, esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_GetLong(hostSystem, "hardware.memorySize", &memorySize,
                      esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
3981
        goto cleanup;
M
Matthias Bolte 已提交
3982 3983
    }

3984
    usageBytes = (unsigned long long)(memoryUsage->value) * 1048576;
3985
    result = memorySize->value - usageBytes;
M
Matthias Bolte 已提交
3986

3987
 cleanup:
M
Matthias Bolte 已提交
3988
    esxVI_String_Free(&propertyNameList);
3989 3990 3991
    esxVI_ObjectContent_Free(&hostSystem);
    esxVI_Int_Free(&memoryUsage);
    esxVI_Long_Free(&memorySize);
M
Matthias Bolte 已提交
3992 3993 3994 3995 3996 3997

    return result;
}



3998
static int
3999
esxConnectIsEncrypted(virConnectPtr conn)
4000
{
M
Matthias Bolte 已提交
4001
    esxPrivate *priv = conn->privateData;
4002

4003
    return STRCASEEQ(priv->parsedUri->transport, "https");
4004 4005 4006 4007 4008
}



static int
4009
esxConnectIsSecure(virConnectPtr conn)
4010
{
M
Matthias Bolte 已提交
4011
    esxPrivate *priv = conn->privateData;
4012

4013
    return STRCASEEQ(priv->parsedUri->transport, "https");
4014 4015 4016 4017
}



4018
static int
4019
esxConnectIsAlive(virConnectPtr conn)
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
{
    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;
}



4035 4036 4037
static int
esxDomainIsActive(virDomainPtr domain)
{
M
Matthias Bolte 已提交
4038
    int result = -1;
M
Matthias Bolte 已提交
4039
    esxPrivate *priv = domain->conn->privateData;
4040 4041 4042 4043
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_VirtualMachinePowerState powerState;

4044
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4045
        return -1;
4046

4047
    if (esxVI_String_AppendValueToList(&propertyNameList,
4048
                                       "runtime.powerState") < 0 ||
4049
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
4050
                                         propertyNameList, &virtualMachine,
M
Matthias Bolte 已提交
4051
                                         esxVI_Occurrence_RequiredItem) < 0 ||
4052
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
M
Matthias Bolte 已提交
4053
        goto cleanup;
4054 4055 4056 4057 4058 4059 4060 4061
    }

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

4062
 cleanup:
4063 4064 4065 4066 4067 4068 4069 4070 4071
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_String_Free(&propertyNameList);

    return result;
}



static int
4072
esxDomainIsPersistent(virDomainPtr domain)
4073
{
4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089
    /* 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;

4090
 cleanup:
4091 4092 4093
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
4094 4095
}

M
Matthias Bolte 已提交
4096 4097


4098 4099 4100
static int
esxDomainIsUpdated(virDomainPtr domain ATTRIBUTE_UNUSED)
{
4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116
    /* 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;

4117
 cleanup:
4118 4119 4120
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
4121
}
4122

M
Matthias Bolte 已提交
4123 4124


4125 4126
static virDomainSnapshotPtr
esxDomainSnapshotCreateXML(virDomainPtr domain, const char *xmlDesc,
4127
                           unsigned int flags)
4128 4129 4130 4131 4132 4133 4134 4135
{
    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;
4136
    char *taskInfoErrorMessage = NULL;
4137
    virDomainSnapshotPtr snapshot = NULL;
4138 4139
    bool diskOnly = (flags & VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY) != 0;
    bool quiesce = (flags & VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE) != 0;
4140

4141 4142 4143 4144 4145
    /* 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);
4146

4147
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4148
        return NULL;
4149

4150
    def = virDomainSnapshotDefParseString(xmlDesc, priv->caps,
4151
                                          priv->xmlopt, 0);
4152

4153
    if (!def)
M
Matthias Bolte 已提交
4154
        return NULL;
4155

4156
    if (def->ndisks) {
4157 4158
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("disk snapshots not supported yet"));
4159 4160 4161
        return NULL;
    }

4162
    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
4163
          (priv->primary, domain->uuid, NULL, &virtualMachine,
4164
           priv->parsedUri->autoAnswer) < 0 ||
4165
        esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4166 4167
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, def->name,
4168
                                    &snapshotTree, NULL,
4169
                                    esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
4170
        goto cleanup;
4171 4172
    }

4173
    if (snapshotTree) {
4174 4175
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Snapshot '%s' already exists"), def->name);
M
Matthias Bolte 已提交
4176
        goto cleanup;
4177 4178
    }

4179
    if (esxVI_CreateSnapshot_Task(priv->primary, virtualMachine->obj,
4180
                                  def->name, def->description,
4181 4182 4183
                                  diskOnly ? esxVI_Boolean_False : esxVI_Boolean_True,
                                  quiesce ? esxVI_Boolean_True : esxVI_Boolean_False,
                                  &task) < 0 ||
4184
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
4185
                                    esxVI_Occurrence_RequiredItem,
4186
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4187
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
4188
        goto cleanup;
4189 4190 4191
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4192 4193
        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not create snapshot: %s"),
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
4194
        goto cleanup;
4195 4196 4197 4198
    }

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

4199
 cleanup:
4200 4201 4202 4203
    virDomainSnapshotDefFree(def);
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    esxVI_ManagedObjectReference_Free(&task);
4204
    VIR_FREE(taskInfoErrorMessage);
4205 4206 4207 4208 4209 4210 4211

    return snapshot;
}



static char *
4212 4213
esxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
4214 4215 4216 4217 4218 4219 4220 4221 4222
{
    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;

4223 4224
    virCheckFlags(0, NULL);

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

4227
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4228
        return NULL;
4229

4230
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
4231 4232 4233 4234
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
                                    &snapshotTree, &snapshotTreeParent,
                                    esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4235
        goto cleanup;
4236 4237 4238 4239
    }

    def.name = snapshot->name;
    def.description = snapshotTree->description;
4240
    def.parent = snapshotTreeParent ? snapshotTreeParent->name : NULL;
4241 4242 4243

    if (esxVI_DateTime_ConvertToCalendarTime(snapshotTree->createTime,
                                             &def.creationTime) < 0) {
M
Matthias Bolte 已提交
4244
        goto cleanup;
4245 4246 4247 4248 4249 4250 4251
    }

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

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

4252
    xml = virDomainSnapshotDefFormat(uuid_string, &def, priv->caps, priv->xmlopt,
4253 4254
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
4255

4256
 cleanup:
4257 4258 4259 4260 4261 4262 4263 4264
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);

    return xml;
}



static int
4265
esxDomainSnapshotNum(virDomainPtr domain, unsigned int flags)
4266
{
M
Matthias Bolte 已提交
4267
    int count;
4268 4269
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
4270
    bool recurse;
4271
    bool leaves;
4272

4273
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
4274 4275
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4276 4277

    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_ROOTS) == 0;
4278
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4279

4280
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4281
        return -1;
4282

4283 4284 4285 4286
    /* ESX snapshots do not require libvirt to maintain any metadata.  */
    if (flags & VIR_DOMAIN_SNAPSHOT_LIST_METADATA)
        return 0;

4287
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4288
                                         &rootSnapshotTreeList) < 0) {
M
Matthias Bolte 已提交
4289
        return -1;
4290 4291
    }

4292 4293
    count = esxVI_GetNumberOfSnapshotTrees(rootSnapshotTreeList, recurse,
                                           leaves);
4294 4295 4296

    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

M
Matthias Bolte 已提交
4297
    return count;
4298 4299 4300 4301 4302 4303
}



static int
esxDomainSnapshotListNames(virDomainPtr domain, char **names, int nameslen,
4304
                           unsigned int flags)
4305
{
M
Matthias Bolte 已提交
4306
    int result;
4307 4308
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
4309
    bool recurse;
4310
    bool leaves;
4311 4312

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
4313 4314
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4315

4316
    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_ROOTS) == 0;
4317
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4318

4319
    if (!names || nameslen < 0) {
4320
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid argument"));
4321 4322 4323
        return -1;
    }

4324
    if (nameslen == 0 || (flags & VIR_DOMAIN_SNAPSHOT_LIST_METADATA))
4325 4326
        return 0;

4327
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4328
        return -1;
4329

4330
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4331
                                         &rootSnapshotTreeList) < 0) {
M
Matthias Bolte 已提交
4332
        return -1;
4333 4334
    }

4335
    result = esxVI_GetSnapshotTreeNames(rootSnapshotTreeList, names, nameslen,
4336
                                        recurse, leaves);
4337 4338 4339 4340 4341 4342 4343 4344

    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

    return result;
}



4345 4346 4347 4348 4349 4350 4351 4352
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;
4353
    bool leaves;
4354 4355

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
4356 4357
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4358 4359

    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS) != 0;
4360
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4361

4362
    if (esxVI_EnsureSession(priv->primary) < 0)
4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379
        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,
4380
                                           recurse, leaves);
4381

4382
 cleanup:
4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399
    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;
4400
    bool leaves;
4401 4402

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
4403 4404
                  VIR_DOMAIN_SNAPSHOT_LIST_METADATA |
                  VIR_DOMAIN_SNAPSHOT_LIST_LEAVES, -1);
4405 4406

    recurse = (flags & VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS) != 0;
4407
    leaves = (flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) != 0;
4408

4409
    if (!names || nameslen < 0) {
4410
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid argument"));
4411 4412 4413
        return -1;
    }

4414
    if (nameslen == 0)
4415 4416
        return 0;

4417
    if (esxVI_EnsureSession(priv->primary) < 0)
4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434
        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,
4435
                                        names, nameslen, recurse, leaves);
4436

4437
 cleanup:
4438 4439 4440 4441 4442 4443 4444
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

    return result;
}



4445 4446
static virDomainSnapshotPtr
esxDomainSnapshotLookupByName(virDomainPtr domain, const char *name,
4447
                              unsigned int flags)
4448 4449 4450 4451 4452 4453
{
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    virDomainSnapshotPtr snapshot = NULL;

4454 4455
    virCheckFlags(0, NULL);

4456
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4457
        return NULL;
4458

4459
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, domain->uuid,
4460 4461
                                         &rootSnapshotTreeList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotTreeList, name, &snapshotTree,
4462
                                    NULL,
4463 4464 4465 4466 4467 4468
                                    esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    snapshot = virGetDomainSnapshot(domain, name);

4469
 cleanup:
4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

    return snapshot;
}



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

4483
    virCheckFlags(0, -1);
4484

4485
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4486
        return -1;
4487

4488
    if (esxVI_LookupCurrentSnapshotTree(priv->primary, domain->uuid,
4489 4490
                                        &currentSnapshotTree,
                                        esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
4491
        return -1;
4492 4493
    }

4494
    if (currentSnapshotTree) {
M
Matthias Bolte 已提交
4495 4496
        esxVI_VirtualMachineSnapshotTree_Free(&currentSnapshotTree);
        return 1;
4497 4498
    }

M
Matthias Bolte 已提交
4499
    return 0;
4500 4501 4502 4503
}



E
Eric Blake 已提交
4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514
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);

4515
    if (esxVI_EnsureSession(priv->primary) < 0)
E
Eric Blake 已提交
4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526
        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) {
4527 4528 4529
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("snapshot '%s' does not have a parent"),
                       snapshotTree->name);
E
Eric Blake 已提交
4530 4531 4532 4533 4534
        goto cleanup;
    }

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

4535
 cleanup:
E
Eric Blake 已提交
4536 4537 4538 4539 4540 4541 4542
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);

    return parent;
}



4543 4544 4545 4546 4547
static virDomainSnapshotPtr
esxDomainSnapshotCurrent(virDomainPtr domain, unsigned int flags)
{
    esxPrivate *priv = domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *currentSnapshotTree = NULL;
M
Matthias Bolte 已提交
4548
    virDomainSnapshotPtr snapshot = NULL;
4549

4550
    virCheckFlags(0, NULL);
4551

4552
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4553
        return NULL;
4554

4555
    if (esxVI_LookupCurrentSnapshotTree(priv->primary, domain->uuid,
4556 4557
                                        &currentSnapshotTree,
                                        esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4558
        return NULL;
4559 4560 4561 4562 4563 4564 4565 4566 4567 4568
    }

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

    esxVI_VirtualMachineSnapshotTree_Free(&currentSnapshotTree);

    return snapshot;
}


4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579
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);

4580
    if (esxVI_EnsureSession(priv->primary) < 0)
4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599
        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);

4600
 cleanup:
4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616
    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);

4617
    if (esxVI_EnsureSession(priv->primary) < 0)
4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
        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;

4631
 cleanup:
4632 4633 4634 4635
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    return ret;
}

4636 4637 4638 4639

static int
esxDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, unsigned int flags)
{
M
Matthias Bolte 已提交
4640
    int result = -1;
4641 4642 4643 4644 4645
    esxPrivate *priv = snapshot->domain->conn->privateData;
    esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
    esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
4646
    char *taskInfoErrorMessage = NULL;
4647

4648
    virCheckFlags(0, -1);
4649

4650
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4651
        return -1;
4652

4653
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
4654 4655
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
4656
                                    &snapshotTree, NULL,
4657
                                    esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4658
        goto cleanup;
4659 4660
    }

4661
    if (esxVI_RevertToSnapshot_Task(priv->primary, snapshotTree->snapshot, NULL,
4662
                                    esxVI_Boolean_Undefined, &task) < 0 ||
4663
        esxVI_WaitForTaskCompletion(priv->primary, task, snapshot->domain->uuid,
4664
                                    esxVI_Occurrence_RequiredItem,
4665
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4666
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
4667
        goto cleanup;
4668 4669 4670
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4671 4672 4673
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not revert to snapshot '%s': %s"), snapshot->name,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
4674
        goto cleanup;
4675 4676
    }

M
Matthias Bolte 已提交
4677 4678
    result = 0;

4679
 cleanup:
4680 4681
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    esxVI_ManagedObjectReference_Free(&task);
4682
    VIR_FREE(taskInfoErrorMessage);
4683 4684 4685 4686 4687 4688 4689 4690 4691

    return result;
}



static int
esxDomainSnapshotDelete(virDomainSnapshotPtr snapshot, unsigned int flags)
{
M
Matthias Bolte 已提交
4692
    int result = -1;
4693 4694 4695 4696 4697 4698
    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;
4699
    char *taskInfoErrorMessage = NULL;
4700

4701 4702
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
                  VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY, -1);
4703

4704
    if (esxVI_EnsureSession(priv->primary) < 0)
M
Matthias Bolte 已提交
4705
        return -1;
4706

4707
    if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN)
4708 4709
        removeChildren = esxVI_Boolean_True;

4710
    if (esxVI_LookupRootSnapshotTreeList(priv->primary, snapshot->domain->uuid,
4711 4712
                                         &rootSnapshotList) < 0 ||
        esxVI_GetSnapshotTreeByName(rootSnapshotList, snapshot->name,
4713
                                    &snapshotTree, NULL,
4714
                                    esxVI_Occurrence_RequiredItem) < 0) {
M
Matthias Bolte 已提交
4715
        goto cleanup;
4716 4717
    }

4718 4719 4720 4721 4722 4723 4724
    /* 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;
    }

4725
    if (esxVI_RemoveSnapshot_Task(priv->primary, snapshotTree->snapshot,
4726
                                  removeChildren, &task) < 0 ||
4727
        esxVI_WaitForTaskCompletion(priv->primary, task, snapshot->domain->uuid,
4728
                                    esxVI_Occurrence_RequiredItem,
4729
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4730
                                    &taskInfoErrorMessage) < 0) {
M
Matthias Bolte 已提交
4731
        goto cleanup;
4732 4733 4734
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4735 4736 4737
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not delete snapshot '%s': %s"), snapshot->name,
                       taskInfoErrorMessage);
M
Matthias Bolte 已提交
4738
        goto cleanup;
4739 4740
    }

M
Matthias Bolte 已提交
4741 4742
    result = 0;

4743
 cleanup:
4744 4745
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
    esxVI_ManagedObjectReference_Free(&task);
4746
    VIR_FREE(taskInfoErrorMessage);
4747 4748 4749 4750 4751 4752

    return result;
}



4753
static int
4754
esxDomainSetMemoryParameters(virDomainPtr domain, virTypedParameterPtr params,
4755 4756 4757 4758 4759 4760 4761 4762
                             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;
4763
    char *taskInfoErrorMessage = NULL;
4764
    size_t i;
4765 4766

    virCheckFlags(0, -1);
4767 4768 4769 4770
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_MEMORY_MIN_GUARANTEE,
                               VIR_TYPED_PARAM_ULLONG,
                               NULL) < 0)
4771
        return -1;
4772

4773
    if (esxVI_EnsureSession(priv->primary) < 0)
4774 4775 4776 4777
        return -1;

    if (esxVI_LookupVirtualMachineByUuidAndPrepareForTask
          (priv->primary, domain->uuid, NULL, &virtualMachine,
4778
           priv->parsedUri->autoAnswer) < 0 ||
4779 4780 4781 4782 4783 4784
        esxVI_VirtualMachineConfigSpec_Alloc(&spec) < 0 ||
        esxVI_ResourceAllocationInfo_Alloc(&spec->memoryAllocation) < 0) {
        goto cleanup;
    }

    for (i = 0; i < nparams; ++i) {
4785
        if (STREQ(params[i].field, VIR_DOMAIN_MEMORY_MIN_GUARANTEE)) {
4786
            if (esxVI_Long_Alloc(&spec->memoryAllocation->reservation) < 0)
4787 4788 4789
                goto cleanup;

            spec->memoryAllocation->reservation->value =
4790
              VIR_DIV_UP(params[i].value.ul, 1024); /* Scale from kilobytes to megabytes */
4791 4792 4793 4794 4795 4796 4797
        }
    }

    if (esxVI_ReconfigVM_Task(priv->primary, virtualMachine->obj, spec,
                              &task) < 0 ||
        esxVI_WaitForTaskCompletion(priv->primary, task, domain->uuid,
                                    esxVI_Occurrence_RequiredItem,
4798
                                    priv->parsedUri->autoAnswer, &taskInfoState,
4799
                                    &taskInfoErrorMessage) < 0) {
4800 4801 4802 4803
        goto cleanup;
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
4804 4805 4806
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not change memory parameters: %s"),
                       taskInfoErrorMessage);
4807 4808 4809 4810 4811
        goto cleanup;
    }

    result = 0;

4812
 cleanup:
4813 4814 4815
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_VirtualMachineConfigSpec_Free(&spec);
    esxVI_ManagedObjectReference_Free(&task);
4816
    VIR_FREE(taskInfoErrorMessage);
4817 4818 4819 4820 4821 4822 4823

    return result;
}



static int
4824
esxDomainGetMemoryParameters(virDomainPtr domain, virTypedParameterPtr params,
4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839
                             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;
    }

4840
    if (esxVI_EnsureSession(priv->primary) < 0)
4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852
        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;
    }

4853 4854 4855 4856
    /* Scale from megabytes to kilobytes */
    if (virTypedParameterAssign(params, VIR_DOMAIN_MEMORY_MIN_GUARANTEE,
                                VIR_TYPED_PARAM_ULLONG,
                                reservation->value * 1024) < 0)
4857 4858 4859 4860 4861
        goto cleanup;

    *nparams = 1;
    result = 0;

4862
 cleanup:
4863 4864 4865 4866 4867 4868 4869
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);
    esxVI_Long_Free(&reservation);

    return result;
}

4870 4871
#define MATCH(FLAG) (flags & (FLAG))
static int
4872 4873 4874
esxConnectListAllDomains(virConnectPtr conn,
                         virDomainPtr **domains,
                         unsigned int flags)
4875 4876 4877
{
    int ret = -1;
    esxPrivate *priv = conn->privateData;
4878 4879
    bool needIdentity;
    bool needPowerState;
4880 4881 4882
    virDomainPtr dom;
    virDomainPtr *doms = NULL;
    size_t ndoms = 0;
4883
    esxVI_String *propertyNameList = NULL;
4884 4885
    esxVI_ObjectContent *virtualMachineList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
4886
    esxVI_AutoStartDefaults *autoStartDefaults = NULL;
4887 4888 4889 4890 4891 4892 4893 4894 4895 4896
    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;
4897
    esxVI_DynamicProperty *dynamicProperty = NULL;
4898 4899 4900 4901 4902 4903 4904

    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
     */
4905
    if ((MATCH(VIR_CONNECT_LIST_DOMAINS_TRANSIENT) &&
4906 4907 4908 4909 4910
         !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)
4911
            goto cleanup;
4912 4913 4914 4915 4916

        ret = 0;
        goto cleanup;
    }

4917
    if (esxVI_EnsureSession(priv->primary) < 0)
4918 4919 4920 4921 4922
        return -1;

    /* check system default autostart value */
    if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART)) {
        if (esxVI_LookupAutoStartDefaults(priv->primary,
4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935
                                          &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) ||
4936
                   domains;
4937 4938 4939 4940 4941 4942 4943

    if (needIdentity) {
        /* Request required data for esxVI_GetVirtualMachineIdentity */
        if (esxVI_String_AppendValueListToList(&propertyNameList,
                                               "configStatus\0"
                                               "name\0"
                                               "config.uuid\0") < 0) {
4944
            goto cleanup;
4945 4946 4947 4948 4949
        }
    }

    needPowerState = MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) ||
                     MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE) ||
4950
                     domains;
4951

4952 4953 4954
    if (needPowerState) {
        if (esxVI_String_AppendValueToList(&propertyNameList,
                                           "runtime.powerState") < 0) {
4955
            goto cleanup;
4956
        }
4957 4958
    }

4959 4960 4961 4962 4963 4964 4965
    if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT)) {
        if (esxVI_String_AppendValueToList(&propertyNameList,
                                           "snapshot.rootSnapshotList") < 0) {
            goto cleanup;
        }
    }

4966
    if (esxVI_LookupVirtualMachineList(priv->primary, propertyNameList,
4967 4968 4969 4970 4971
                                       &virtualMachineList) < 0)
        goto cleanup;

    if (domains) {
        if (VIR_ALLOC_N(doms, 1) < 0)
4972
            goto cleanup;
4973 4974 4975
        ndoms = 1;
    }

4976
    for (virtualMachine = virtualMachineList; virtualMachine;
4977
         virtualMachine = virtualMachine->_next) {
4978 4979
        if (needIdentity) {
            VIR_FREE(name);
4980

4981 4982 4983 4984 4985
            if (esxVI_GetVirtualMachineIdentity(virtualMachine, &id,
                                                &name, uuid) < 0) {
                goto cleanup;
            }
        }
4986

4987 4988 4989 4990 4991 4992
        if (needPowerState) {
            if (esxVI_GetVirtualMachinePowerState(virtualMachine,
                                                  &powerState) < 0) {
                goto cleanup;
            }
        }
4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003

        /* 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)) {
5004

5005 5006
            esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
            for (dynamicProperty = virtualMachine->propSet; dynamicProperty;
                dynamicProperty = dynamicProperty->_next) {
                if (STREQ(dynamicProperty->name, "snapshot.rootSnapshotList")) {
                    if (esxVI_VirtualMachineSnapshotTree_CastListFromAnyType
                        (dynamicProperty->val, &rootSnapshotTreeList) < 0) {
                        goto cleanup;
                    }

                    break;
                }
5017 5018 5019
            }

            if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT) &&
5020
                   rootSnapshotTreeList) ||
5021
                  (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) &&
5022
                   !rootSnapshotTreeList)))
5023 5024 5025 5026 5027 5028 5029
                continue;
        }

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

5030
            if (autoStartDefaults->enabled == esxVI_Boolean_True) {
5031
                for (powerInfo = powerInfoList; powerInfo;
5032 5033 5034 5035
                     powerInfo = powerInfo->_next) {
                    if (STREQ(powerInfo->key->value, virtualMachine->obj->value)) {
                        if (STRCASEEQ(powerInfo->startAction, "powerOn"))
                            autostart = true;
5036

5037 5038
                        break;
                    }
5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051
                }
            }

            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);
5052

5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
            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;
        }

5072
        if (VIR_RESIZE_N(doms, ndoms, count, 2) < 0)
5073
            goto cleanup;
5074 5075

        /* Only running/suspended virtual machines have an ID != -1 */
5076 5077 5078 5079 5080
        if (powerState == esxVI_VirtualMachinePowerState_PoweredOff)
            id = -1;

        if (!(dom = virGetDomain(conn, name, uuid, id)))
            goto cleanup;
5081 5082 5083 5084 5085 5086 5087 5088 5089

        doms[count++] = dom;
    }

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

5090
 cleanup:
5091
    if (doms) {
5092
        for (id = 0; id < count; id++)
5093
            virObjectUnref(doms[id]);
5094 5095

        VIR_FREE(doms);
5096
    }
5097

5098
    VIR_FREE(name);
5099 5100
    esxVI_AutoStartDefaults_Free(&autoStartDefaults);
    esxVI_AutoStartPowerInfo_Free(&powerInfoList);
5101 5102
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachineList);
5103 5104
    esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotTreeList);

5105 5106 5107
    return ret;
}
#undef MATCH
5108

5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144
static int
esxDomainHasManagedSaveImage(virDomainPtr domain, unsigned int flags)
{
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_ManagedObjectReference *managedObjectReference = NULL;
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";

    virCheckFlags(0, -1);

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

    virUUIDFormat(domain->uuid, uuid_string);

    if (esxVI_FindByUuid(priv->primary, priv->primary->datacenter->_reference,
                         uuid_string, esxVI_Boolean_True,
                         esxVI_Boolean_Undefined,
                         &managedObjectReference) < 0) {
        return -1;
    }

    if (!managedObjectReference) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("Could not find domain with UUID '%s'"),
                       uuid_string);
        goto cleanup;
    }

    result = 0;

 cleanup:
    esxVI_ManagedObjectReference_Free(&managedObjectReference);
    return result;
}

5145

5146
static virHypervisorDriver esxHypervisorDriver = {
5147
    .name = "ESX",
5148 5149 5150 5151 5152 5153
    .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 */
5154
    .nodeGetInfo = esxNodeGetInfo, /* 0.7.0 */
5155 5156 5157 5158
    .connectGetCapabilities = esxConnectGetCapabilities, /* 0.7.1 */
    .connectListDomains = esxConnectListDomains, /* 0.7.0 */
    .connectNumOfDomains = esxConnectNumOfDomains, /* 0.7.0 */
    .connectListAllDomains = esxConnectListAllDomains, /* 0.10.2 */
5159 5160 5161 5162 5163 5164
    .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 */
5165
    .domainShutdownFlags = esxDomainShutdownFlags, /* 0.9.10 */
5166 5167
    .domainReboot = esxDomainReboot, /* 0.7.0 */
    .domainDestroy = esxDomainDestroy, /* 0.7.0 */
5168
    .domainDestroyFlags = esxDomainDestroyFlags, /* 0.9.4 */
5169 5170 5171 5172 5173 5174 5175 5176
    .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 */
5177
    .domainScreenshot = esxDomainScreenshot, /* 1.2.10 */
5178 5179 5180 5181 5182
    .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 */
5183 5184 5185 5186
    .connectDomainXMLFromNative = esxConnectDomainXMLFromNative, /* 0.7.0 */
    .connectDomainXMLToNative = esxConnectDomainXMLToNative, /* 0.7.2 */
    .connectListDefinedDomains = esxConnectListDefinedDomains, /* 0.7.0 */
    .connectNumOfDefinedDomains = esxConnectNumOfDefinedDomains, /* 0.7.0 */
5187 5188 5189
    .domainCreate = esxDomainCreate, /* 0.7.0 */
    .domainCreateWithFlags = esxDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = esxDomainDefineXML, /* 0.7.2 */
5190
    .domainDefineXMLFlags = esxDomainDefineXMLFlags, /* 1.2.12 */
5191
    .domainUndefine = esxDomainUndefine, /* 0.7.1 */
5192
    .domainUndefineFlags = esxDomainUndefineFlags, /* 0.9.4 */
5193 5194 5195 5196
    .domainGetAutostart = esxDomainGetAutostart, /* 0.9.0 */
    .domainSetAutostart = esxDomainSetAutostart, /* 0.9.0 */
    .domainGetSchedulerType = esxDomainGetSchedulerType, /* 0.7.0 */
    .domainGetSchedulerParameters = esxDomainGetSchedulerParameters, /* 0.7.0 */
5197
    .domainGetSchedulerParametersFlags = esxDomainGetSchedulerParametersFlags, /* 0.9.2 */
5198
    .domainSetSchedulerParameters = esxDomainSetSchedulerParameters, /* 0.7.0 */
5199
    .domainSetSchedulerParametersFlags = esxDomainSetSchedulerParametersFlags, /* 0.9.2 */
5200 5201 5202 5203
    .domainMigratePrepare = esxDomainMigratePrepare, /* 0.7.0 */
    .domainMigratePerform = esxDomainMigratePerform, /* 0.7.0 */
    .domainMigrateFinish = esxDomainMigrateFinish, /* 0.7.0 */
    .nodeGetFreeMemory = esxNodeGetFreeMemory, /* 0.7.2 */
5204 5205
    .connectIsEncrypted = esxConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = esxConnectIsSecure, /* 0.7.3 */
5206 5207 5208 5209 5210 5211 5212
    .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 */
5213 5214
    .domainSnapshotNumChildren = esxDomainSnapshotNumChildren, /* 0.9.7 */
    .domainSnapshotListChildrenNames = esxDomainSnapshotListChildrenNames, /* 0.9.7 */
5215 5216
    .domainSnapshotLookupByName = esxDomainSnapshotLookupByName, /* 0.8.0 */
    .domainHasCurrentSnapshot = esxDomainHasCurrentSnapshot, /* 0.8.0 */
E
Eric Blake 已提交
5217
    .domainSnapshotGetParent = esxDomainSnapshotGetParent, /* 0.9.7 */
5218 5219
    .domainSnapshotCurrent = esxDomainSnapshotCurrent, /* 0.8.0 */
    .domainRevertToSnapshot = esxDomainRevertToSnapshot, /* 0.8.0 */
5220 5221
    .domainSnapshotIsCurrent = esxDomainSnapshotIsCurrent, /* 0.9.13 */
    .domainSnapshotHasMetadata = esxDomainSnapshotHasMetadata, /* 0.9.13 */
5222
    .domainSnapshotDelete = esxDomainSnapshotDelete, /* 0.8.0 */
5223
    .connectIsAlive = esxConnectIsAlive, /* 0.9.8 */
5224
    .domainHasManagedSaveImage = esxDomainHasManagedSaveImage, /* 1.2.13 */
5225 5226 5227
};


5228
static virConnectDriver esxConnectDriver = {
5229
    .uriSchemes = (const char *[]){ "vpx", "esx", "gsx", NULL },
5230 5231 5232 5233 5234
    .hypervisorDriver = &esxHypervisorDriver,
    .interfaceDriver = &esxInterfaceDriver,
    .networkDriver = &esxNetworkDriver,
    .storageDriver = &esxStorageDriver,
};
5235 5236 5237 5238

int
esxRegister(void)
{
5239 5240
    return virRegisterConnectDriver(&esxConnectDriver,
                                    false);
5241
}