vz_driver.c 113.9 KB
Newer Older
D
Dmitry Guryanov 已提交
1
/*
2
 * vz_driver.c: core driver functions for managing
D
Dmitry Guryanov 已提交
3 4
 * Parallels Cloud Server hosts
 *
5
 * Copyright (C) 2014-2015 Red Hat, Inc.
D
Dmitry Guryanov 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2012 Parallels, Inc.
 *
 * 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
D
Dmitry Guryanov 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/statvfs.h>

#include "datatypes.h"
38
#include "virerror.h"
39
#include "viralloc.h"
40
#include "virlog.h"
41
#include "vircommand.h"
D
Dmitry Guryanov 已提交
42
#include "configmake.h"
43
#include "virfile.h"
44
#include "virpidfile.h"
45
#include "virstoragefile.h"
46
#include "virstring.h"
47
#include "cpu/cpu.h"
48
#include "virtypedparam.h"
49 50
#include "virhostmem.h"
#include "virhostcpu.h"
51
#include "viraccessapicheck.h"
D
Dmitry Guryanov 已提交
52

53 54 55
#include "vz_driver.h"
#include "vz_utils.h"
#include "vz_sdk.h"
D
Dmitry Guryanov 已提交
56 57 58

#define VIR_FROM_THIS VIR_FROM_PARALLELS

59 60
VIR_LOG_INIT("parallels.parallels_driver");

D
Dmitry Guryanov 已提交
61 62
#define PRLCTL                      "prlctl"

63
#define VZ_STATEDIR RUNSTATEDIR "/libvirt/vz"
64

65
static virClassPtr vzDriverClass;
D
Dmitry Guryanov 已提交
66

67 68 69
static bool vz_driver_privileged;
/* pid file FD, ensures two copies of the driver can't use the same root */
static int vz_driver_lock_fd = -1;
70 71 72 73 74 75 76
static virMutex vz_driver_lock;
static vzDriverPtr vz_driver;
static vzConnPtr vz_conn_list;

static vzDriverPtr
vzDriverObjNew(void);

D
Dmitry Guryanov 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static int
vzCapsAddGuestDomain(virCapsPtr caps,
                     virDomainOSType ostype,
                     virArch arch,
                     const char * emulator,
                     virDomainVirtType virt_type)
{
    virCapsGuestPtr guest;

    if ((guest = virCapabilitiesAddGuest(caps, ostype, arch, emulator,
                                         NULL, 0, NULL)) == NULL)
        return -1;


    if (virCapabilitiesAddGuestDomain(guest, virt_type,
                                      NULL, NULL, 0, NULL) == NULL)
        return -1;

    return 0;
}

D
Dmitry Guryanov 已提交
98
static virCapsPtr
99
vzBuildCapabilities(void)
D
Dmitry Guryanov 已提交
100
{
101 102
    virCapsPtr caps = NULL;
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
103 104 105 106 107
    virDomainOSType ostypes[] = {
        VIR_DOMAIN_OSTYPE_HVM,
        VIR_DOMAIN_OSTYPE_EXE
    };
    virArch archs[] = { VIR_ARCH_I686, VIR_ARCH_X86_64 };
108
    const char *const emulators[] = { "vz", "parallels"};
D
Dmitry Guryanov 已提交
109
    virDomainVirtType virt_types[] = {
110 111
        VIR_DOMAIN_VIRT_VZ,
        VIR_DOMAIN_VIRT_PARALLELS
D
Dmitry Guryanov 已提交
112 113
    };
    size_t i, j, k;
D
Dmitry Guryanov 已提交
114

115
    if ((caps = virCapabilitiesNew(virArchFromHost(),
116
                                   false, false)) == NULL)
117
        return NULL;
D
Dmitry Guryanov 已提交
118

M
Martin Kletzander 已提交
119
    if (virCapabilitiesInitNUMA(caps) < 0)
120
        goto error;
D
Dmitry Guryanov 已提交
121

122 123
    if (virCapabilitiesInitCaches(caps) < 0)
        goto error;
124

125
    verify(G_N_ELEMENTS(archs) == G_N_ELEMENTS(emulators));
126

127 128 129
    for (i = 0; i < G_N_ELEMENTS(ostypes); i++)
        for (j = 0; j < G_N_ELEMENTS(archs); j++)
            for (k = 0; k < G_N_ELEMENTS(emulators); k++)
D
Dmitry Guryanov 已提交
130 131 132
                if (vzCapsAddGuestDomain(caps, ostypes[i], archs[j],
                                         emulators[k], virt_types[k]) < 0)
                    goto error;
133

M
Martin Kletzander 已提交
134
    if (virCapabilitiesGetNodeInfo(&nodeinfo))
135 136
        goto error;

137
    if (!(caps->host.cpu = virCPUGetHost(caps->host.arch, VIR_CPU_TYPE_HOST,
138
                                         &nodeinfo, NULL)))
139 140
        goto error;

141 142 143
    if (virCapabilitiesAddHostMigrateTransport(caps, "vzmigr") < 0)
        goto error;

D
Dmitry Guryanov 已提交
144 145
    return caps;

146
 error:
147
    virObjectUnref(caps);
148
    return NULL;
D
Dmitry Guryanov 已提交
149 150
}

151 152 153 154
static void vzDriverDispose(void * obj)
{
    vzDriverPtr driver = obj;

155
    prlsdkDisconnect(driver);
156 157 158
    virObjectUnref(driver->domains);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->xmlopt);
159
    virObjectUnref(driver->domainEventState);
160
    virSysinfoDefFree(driver->hostsysinfo);
161 162 163 164
}

static int vzDriverOnceInit(void)
{
165
    if (!VIR_CLASS_NEW(vzDriver, virClassForObjectLockable()))
166 167 168 169
        return -1;

    return 0;
}
170
VIR_ONCE_GLOBAL_INIT(vzDriver);
171 172 173 174

vzDriverPtr
vzGetDriverConnection(void)
{
175 176 177 178 179
    if (!vz_driver_privileged) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("vz state driver is not active"));
        return NULL;
    }
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    virMutexLock(&vz_driver_lock);
    if (!vz_driver)
        vz_driver = vzDriverObjNew();
    virObjectRef(vz_driver);
    virMutexUnlock(&vz_driver_lock);

    return vz_driver;
}

void
vzDestroyDriverConnection(void)
{
    vzDriverPtr driver;
    vzConnPtr privconn_list;

    virMutexLock(&vz_driver_lock);
196 197
    driver = g_steal_pointer(&vz_driver);
    privconn_list = g_steal_pointer(&vz_conn_list);
198 199 200 201 202 203 204 205 206 207 208
    virMutexUnlock(&vz_driver_lock);

    while (privconn_list) {
        vzConnPtr privconn = privconn_list;
        privconn_list = privconn->next;
        virConnectCloseCallbackDataCall(privconn->closeCallback,
                                        VIR_CONNECT_CLOSE_REASON_EOF);
    }
    virObjectUnref(driver);
}

D
Dmitry Guryanov 已提交
209
static char *
210
vzConnectGetCapabilities(virConnectPtr conn)
D
Dmitry Guryanov 已提交
211
{
212
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
213

214 215 216
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

M
Michal Privoznik 已提交
217
    return virCapabilitiesFormatXML(privconn->driver->caps);
D
Dmitry Guryanov 已提交
218
}
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
static int
vzDomainDefAddDefaultInputDevices(virDomainDefPtr def)
{
    if (def->ngraphics == 0)
        return 0;

    int bus = IS_CT(def) ? VIR_DOMAIN_INPUT_BUS_PARALLELS :
                           VIR_DOMAIN_INPUT_BUS_PS2;

    if (virDomainDefMaybeAddInput(def,
                                  VIR_DOMAIN_INPUT_TYPE_MOUSE,
                                  bus) < 0)
        return -1;

    if (virDomainDefMaybeAddInput(def,
                                  VIR_DOMAIN_INPUT_TYPE_KBD,
                                  bus) < 0)
        return -1;

    return 0;
}
D
Dmitry Guryanov 已提交
241

242
static int
243
vzDomainDefPostParse(virDomainDefPtr def,
J
Ján Tomko 已提交
244 245 246 247
                     virCapsPtr caps G_GNUC_UNUSED,
                     unsigned int parseFlags G_GNUC_UNUSED,
                     void *opaque G_GNUC_UNUSED,
                     void *parseOpaque G_GNUC_UNUSED)
248
{
249 250 251
    if (vzDomainDefAddDefaultInputDevices(def) < 0)
        return -1;

252 253 254 255 256
    return 0;
}

static int
vzDomainDefValidate(const virDomainDef *def,
J
Ján Tomko 已提交
257
                    virCapsPtr caps G_GNUC_UNUSED,
258 259
                    void *opaque)
{
260 261 262
    if (vzCheckUnsupportedControllers(def, opaque) < 0)
        return -1;

263 264 265 266
    return 0;
}

static int
267
vzDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
M
Michal Privoznik 已提交
268
                           const virDomainDef *def,
J
Ján Tomko 已提交
269 270 271 272
                           virCapsPtr caps G_GNUC_UNUSED,
                           unsigned int parseFlags G_GNUC_UNUSED,
                           void *opaque G_GNUC_UNUSED,
                           void *parseOpaque G_GNUC_UNUSED)
273
{
274 275 276
    if (dev->type == VIR_DOMAIN_DEVICE_NET &&
        (dev->data.net->type == VIR_DOMAIN_NET_TYPE_NETWORK ||
         dev->data.net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) &&
C
Cole Robinson 已提交
277 278 279
        dev->data.net->model == VIR_DOMAIN_NET_MODEL_UNKNOWN &&
        def->os.type == VIR_DOMAIN_OSTYPE_HVM)
        dev->data.net->model = VIR_DOMAIN_NET_MODEL_E1000;
280

281 282 283 284 285 286
    return 0;
}

static int
vzDomainDeviceDefValidate(const virDomainDeviceDef *dev,
                          const virDomainDef *def,
J
Ján Tomko 已提交
287
                          void *opaque G_GNUC_UNUSED)
288
{
289 290
    if (dev->type == VIR_DOMAIN_DEVICE_DISK)
        return vzCheckUnsupportedDisk(def, dev->data.disk, opaque);
291 292
    else if (dev->type == VIR_DOMAIN_DEVICE_GRAPHICS)
        return vzCheckUnsupportedGraphics(dev->data.graphics);
293 294

    return 0;
295 296
}

297 298 299 300
static virDomainXMLPrivateDataCallbacks vzDomainXMLPrivateDataCallbacksPtr = {
    .alloc = vzDomObjAlloc,
    .free = vzDomObjFree,
};
301

M
Maxim Nestratov 已提交
302
static virDomainDefParserConfig vzDomainDefParserConfig = {
303
    .macPrefix = {0x42, 0x1C, 0x00},
304
    .domainPostParseCallback = vzDomainDefPostParse,
305 306 307
    .devicesPostParseCallback = vzDomainDeviceDefPostParse,
    .domainValidateCallback = vzDomainDefValidate,
    .deviceValidateCallback = vzDomainDeviceDefValidate,
308 309
};

310 311
static vzDriverPtr
vzDriverObjNew(void)
D
Dmitry Guryanov 已提交
312
{
313
    vzDriverPtr driver;
D
Dmitry Guryanov 已提交
314

315 316
    if (vzDriverInitialize() < 0)
        return NULL;
D
Dmitry Guryanov 已提交
317

318 319
    if (!(driver = virObjectLockableNew(vzDriverClass)))
        return NULL;
320

321 322 323 324
    vzDomainDefParserConfig.priv = &driver->vzCaps;

    if (!(driver->caps = vzBuildCapabilities()) ||
        !(driver->xmlopt = virDomainXMLOptionNew(&vzDomainDefParserConfig,
325
                                                 &vzDomainXMLPrivateDataCallbacksPtr,
326
                                                 NULL, NULL, NULL)) ||
327 328 329
        !(driver->domains = virDomainObjListNew()) ||
        !(driver->domainEventState = virObjectEventStateNew()) ||
        (vzInitVersion(driver) < 0) ||
330
        (prlsdkConnect(driver) < 0)) {
331 332 333
        virObjectUnref(driver);
        return NULL;
    }
D
Dmitry Guryanov 已提交
334

335
    driver->hostsysinfo = virSysinfoRead();
336
    ignore_value(prlsdkLoadDomains(driver));
337 338 339 340 341

    /* As far as waitDomainJob finally calls virReportErrorHelper
     * and we are not going to report it, reset it expicitly*/
    virResetLastError();

342
    return driver;
D
Dmitry Guryanov 已提交
343 344 345
}

static virDrvOpenStatus
346
vzConnectOpen(virConnectPtr conn,
J
Ján Tomko 已提交
347 348
              virConnectAuthPtr auth G_GNUC_UNUSED,
              virConfPtr conf G_GNUC_UNUSED,
M
Michal Privoznik 已提交
349
              unsigned int flags)
D
Dmitry Guryanov 已提交
350
{
351 352
    vzDriverPtr driver = NULL;
    vzConnPtr privconn = NULL;
D
Dmitry Guryanov 已提交
353 354 355 356

    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    /* From this point on, the connection is for us. */
357
    if (STRNEQ(conn->uri->path, "/system")) {
358
        virReportError(VIR_ERR_INTERNAL_ERROR,
359
                       _("Unexpected Virtuozzo URI path '%s', try vz:///system"),
360
                       conn->uri->path);
D
Dmitry Guryanov 已提交
361 362 363
        return VIR_DRV_OPEN_ERROR;
    }

364 365 366
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    if (!(driver = vzGetDriverConnection()))
        return VIR_DRV_OPEN_ERROR;

    if (VIR_ALLOC(privconn) < 0)
        goto error;

    conn->privateData = privconn;
    privconn->driver = driver;

    if (!(privconn->closeCallback = virNewConnectCloseCallbackData()))
        goto error;

    virMutexLock(&vz_driver_lock);
    privconn->next = vz_conn_list;
    vz_conn_list = privconn;
    virMutexUnlock(&vz_driver_lock);
D
Dmitry Guryanov 已提交
383 384

    return VIR_DRV_OPEN_SUCCESS;
385 386 387 388 389 390 391

 error:

    conn->privateData = NULL;
    virObjectUnref(driver);
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
D
Dmitry Guryanov 已提交
392 393 394
}

static int
395
vzConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
396
{
397
    vzConnPtr curr, *prev = &vz_conn_list;
398
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
399

400 401 402
    if (!privconn)
        return 0;

403 404 405 406 407 408 409
    virMutexLock(&vz_driver_lock);
    for (curr = vz_conn_list; curr; prev = &curr->next, curr = curr->next) {
        if (curr == privconn) {
            *prev = curr->next;
            break;
        }
    }
D
Dmitry Guryanov 已提交
410

411
    virMutexUnlock(&vz_driver_lock);
D
Dmitry Guryanov 已提交
412

413 414
    virObjectUnref(privconn->closeCallback);
    virObjectUnref(privconn->driver);
D
Dmitry Guryanov 已提交
415
    VIR_FREE(privconn);
416
    conn->privateData = NULL;
D
Dmitry Guryanov 已提交
417 418 419 420
    return 0;
}

static int
421
vzConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
422
{
423
    vzConnPtr privconn = conn->privateData;
424 425 426 427

    if (virConnectGetVersionEnsureACL(conn) < 0)
        return -1;

428
    *hvVer = privconn->driver->vzVersion;
429
    return 0;
430 431
}

432

433
static char *vzConnectGetHostname(virConnectPtr conn)
434
{
435 436 437
    if (virConnectGetHostnameEnsureACL(conn) < 0)
        return NULL;

438 439 440
    return virGetHostname();
}

441 442 443 444 445 446 447 448 449
static char *
vzConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
    vzDriverPtr driver = privconn->driver;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

450 451 452
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

453 454 455 456 457 458 459 460 461 462 463
    if (!driver->hostsysinfo) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Host SMBIOS information is not available"));
        return NULL;
    }

    if (virSysinfoFormat(&buf, driver->hostsysinfo) < 0)
        return NULL;

    return virBufferContentAndReset(&buf);
}
464

465
static int
466
vzConnectListDomains(virConnectPtr conn, int *ids, int maxids)
467
{
468
    vzConnPtr privconn = conn->privateData;
469

470 471 472
    if (virConnectListDomainsEnsureACL(conn) < 0)
        return -1;

M
Michal Privoznik 已提交
473 474 475
    return virDomainObjListGetActiveIDs(privconn->driver->domains, ids,
                                        maxids, virConnectListDomainsCheckACL,
                                        conn);
476 477 478
}

static int
479
vzConnectNumOfDomains(virConnectPtr conn)
480
{
481
    vzConnPtr privconn = conn->privateData;
482

483 484 485
    if (virConnectNumOfDomainsEnsureACL(conn) < 0)
        return -1;

M
Michal Privoznik 已提交
486 487
    return virDomainObjListNumOfDomains(privconn->driver->domains, true,
                                        virConnectNumOfDomainsCheckACL, conn);
488 489 490
}

static int
491
vzConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
492
{
493
    vzConnPtr privconn = conn->privateData;
494

495 496 497
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

498
    memset(names, 0, sizeof(*names) * maxnames);
M
Michal Privoznik 已提交
499 500 501 502
    return virDomainObjListGetInactiveNames(privconn->driver->domains, names,
                                            maxnames,
                                            virConnectListDefinedDomainsCheckACL,
                                            conn);
503 504 505
}

static int
506
vzConnectNumOfDefinedDomains(virConnectPtr conn)
507
{
508
    vzConnPtr privconn = conn->privateData;
509

510 511 512
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

M
Michal Privoznik 已提交
513 514 515
    return virDomainObjListNumOfDomains(privconn->driver->domains, false,
                                        virConnectNumOfDefinedDomainsCheckACL,
                                        conn);
516 517 518
}

static int
519
vzConnectListAllDomains(virConnectPtr conn,
M
Michal Privoznik 已提交
520 521
                        virDomainPtr **domains,
                        unsigned int flags)
522
{
523
    vzConnPtr privconn = conn->privateData;
524

O
Osier Yang 已提交
525
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
526 527 528 529

    if (virConnectListAllDomainsEnsureACL(conn) < 0)
        return -1;

M
Michal Privoznik 已提交
530 531
    return virDomainObjListExport(privconn->driver->domains, conn, domains,
                                  virConnectListAllDomainsCheckACL, flags);
532 533 534
}

static virDomainPtr
535
vzDomainLookupByID(virConnectPtr conn, int id)
536
{
537
    vzConnPtr privconn = conn->privateData;
538
    virDomainPtr ret = NULL;
539 540
    virDomainObjPtr dom;

541
    dom = virDomainObjListFindByID(privconn->driver->domains, id);
542 543 544

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
545
        return NULL;
546 547
    }

548 549 550
    if (virDomainLookupByIDEnsureACL(conn, dom->def) < 0)
        goto cleanup;

551
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
552

553
 cleanup:
554
    virDomainObjEndAPI(&dom);
555 556 557 558
    return ret;
}

static virDomainPtr
559
vzDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
560
{
561
    vzConnPtr privconn = conn->privateData;
562
    virDomainPtr ret = NULL;
563 564
    virDomainObjPtr dom;

565
    dom = virDomainObjListFindByUUID(privconn->driver->domains, uuid);
566 567 568 569 570 571

    if (dom == NULL) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
572
        return NULL;
573 574
    }

575 576 577
    if (virDomainLookupByUUIDEnsureACL(conn, dom->def) < 0)
        goto cleanup;

578
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
579

580
 cleanup:
581
    virDomainObjEndAPI(&dom);
582 583 584 585
    return ret;
}

static virDomainPtr
586
vzDomainLookupByName(virConnectPtr conn, const char *name)
587
{
588
    vzConnPtr privconn = conn->privateData;
589
    virDomainPtr ret = NULL;
590 591
    virDomainObjPtr dom;

592
    dom = virDomainObjListFindByName(privconn->driver->domains, name);
593 594 595 596

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
597
        return NULL;
598 599
    }

600 601 602
    if (virDomainLookupByNameEnsureACL(conn, dom->def) < 0)
        goto cleanup;

603
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
604

605
 cleanup:
606
    virDomainObjEndAPI(&dom);
607 608 609 610
    return ret;
}

static int
611
vzDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
612
{
613
    virDomainObjPtr dom;
614
    vzDomObjPtr privdom;
615 616
    int ret = -1;

J
John Ferlan 已提交
617
    if (!(dom = vzDomObjFromDomain(domain)))
618 619
        goto cleanup;

620 621 622
    if (virDomainGetInfoEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

623 624
    info->state = virDomainObjGetState(dom, NULL);
    info->memory = dom->def->mem.cur_balloon;
625
    info->maxMem = virDomainDefGetMemoryTotal(dom->def);
626
    info->nrVirtCpu = virDomainDefGetVcpus(dom->def);
627
    info->cpuTime = 0;
628

629 630 631
    privdom = dom->privateData;

    if (PRL_INVALID_HANDLE != privdom->stats && virDomainObjIsActive(dom)) {
632 633 634
        unsigned long long vtime;
        size_t i;

635
        for (i = 0; i < virDomainDefGetVcpus(dom->def); ++i) {
636
            if (prlsdkGetVcpuStats(privdom->stats, i, &vtime) < 0) {
637 638 639 640 641 642 643
                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                               _("cannot read cputime for domain"));
                goto cleanup;
            }
            info->cpuTime += vtime;
        }
    }
644 645
    ret = 0;

646
 cleanup:
647
    virDomainObjEndAPI(&dom);
648 649 650 651
    return ret;
}

static char *
652
vzDomainGetOSType(virDomainPtr domain)
653
{
654
    virDomainObjPtr dom;
655 656
    char *ret = NULL;

657
    if (!(dom = vzDomObjFromDomain(domain)))
658
        return NULL;
659

660 661 662
    if (virDomainGetOSTypeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

663
    ret = g_strdup(virDomainOSTypeToString(dom->def->os.type));
664

665
 cleanup:
J
John Ferlan 已提交
666
    virDomainObjEndAPI(&dom);
667 668 669 670
    return ret;
}

static int
671
vzDomainIsPersistent(virDomainPtr domain)
672
{
673
    virDomainObjPtr dom;
674
    int ret = -1;
675

676
    if (!(dom = vzDomObjFromDomain(domain)))
677
        return -1;
678

679 680 681 682 683 684
    if (virDomainIsPersistentEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = 1;

 cleanup:
J
John Ferlan 已提交
685
    virDomainObjEndAPI(&dom);
686
    return ret;
687 688 689
}

static int
690
vzDomainGetState(virDomainPtr domain,
M
Michal Privoznik 已提交
691
                 int *state, int *reason, unsigned int flags)
692
{
693
    virDomainObjPtr dom;
694
    int ret = -1;
695

696 697
    virCheckFlags(0, -1);

698
    if (!(dom = vzDomObjFromDomain(domain)))
699
        return -1;
700

701 702 703
    if (virDomainGetStateEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

704
    *state = virDomainObjGetState(dom, reason);
705
    ret = 0;
706

707
 cleanup:
J
John Ferlan 已提交
708
    virDomainObjEndAPI(&dom);
709
    return ret;
710 711 712
}

static char *
713
vzDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
714
{
715
    vzConnPtr privconn = domain->conn->privateData;
716
    virDomainDefPtr def;
717
    virDomainObjPtr dom;
718 719
    char *ret = NULL;

720
    virCheckFlags(VIR_DOMAIN_XML_COMMON_FLAGS, NULL);
721

722
    if (!(dom = vzDomObjFromDomain(domain)))
723
        return NULL;
724

725 726 727
    if (virDomainGetXMLDescEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

728
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
729
        dom->newDef ? dom->newDef : dom->def;
730

731
    ret = virDomainDefFormat(def, privconn->driver->caps, flags);
732

733
 cleanup:
J
John Ferlan 已提交
734
    virDomainObjEndAPI(&dom);
735 736 737 738
    return ret;
}

static int
739
vzDomainGetAutostart(virDomainPtr domain, int *autostart)
740
{
741
    virDomainObjPtr dom;
742
    int ret = -1;
743

744
    if (!(dom = vzDomObjFromDomain(domain)))
745
        return -1;
746

747 748 749
    if (virDomainGetAutostartEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

750
    *autostart = dom->autostart;
751
    ret = 0;
752

753
 cleanup:
J
John Ferlan 已提交
754
    virDomainObjEndAPI(&dom);
755
    return ret;
D
Dmitry Guryanov 已提交
756 757
}

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
static int
vzEnsureDomainExists(virDomainObjPtr dom)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    if (!dom->removing)
        return 0;

    virUUIDFormat(dom->def->uuid, uuidstr);
    virReportError(VIR_ERR_NO_DOMAIN,
                   _("no domain with matching uuid '%s' (%s)"),
                   uuidstr, dom->def->name);

    return -1;
}

774
static virDomainPtr
775
vzDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
776
{
777
    vzConnPtr privconn = conn->privateData;
778
    virDomainPtr retdom = NULL;
779
    virDomainDefPtr def;
780
    virDomainObjPtr dom = NULL;
781
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
782
    vzDriverPtr driver = privconn->driver;
783
    bool job = false;
784

785 786 787
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
788
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
789

790
    if ((def = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
791
                                       NULL, parse_flags)) == NULL)
792 793
        goto cleanup;

794 795 796
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

797 798 799
    if (virDomainDefineXMLFlagsEnsureACL(conn, def) < 0)
        goto cleanup;

800
    dom = virDomainObjListFindByUUID(driver->domains, def->uuid);
801
    if (dom == NULL) {
802
        virResetLastError();
803
        if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
804
            if (prlsdkCreateVm(driver, def))
805
                goto cleanup;
806
        } else if (def->os.type == VIR_DOMAIN_OSTYPE_EXE) {
807
            if (prlsdkCreateCt(conn, def))
808 809 810
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INVALID_ARG,
811 812
                           _("Unsupported OS type: %s"),
                           virDomainOSTypeToString(def->os.type));
813
            goto cleanup;
814
        }
815

816
        if (!(dom = prlsdkAddDomainByUUID(driver, def->uuid)))
817
            goto cleanup;
818
    } else {
819 820
        int state, reason;

821
        state = virDomainObjGetState(dom, &reason);
822 823 824 825 826 827 828 829 830 831 832 833 834

        if (state == VIR_DOMAIN_SHUTOFF &&
            reason == VIR_DOMAIN_SHUTOFF_SAVED) {

            /* PCS doesn't store domain config in managed save state file.
             * It's forbidden to change config for VMs in this state.
             * It's possible to change config for containers, but after
             * restoring domain will have that new config, not a config,
             * which domain had at the moment of virDomainManagedSave.
             *
             * So forbid this operation, if config is changed. If it's
             * not changed - just do nothing. */

835
            if (!virDomainDefCheckABIStability(dom->def, def, driver->xmlopt)) {
836 837 838 839 840 841
                virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                               _("Can't change domain configuration "
                                 "in managed save state"));
                goto cleanup;
            }
        } else {
842 843 844 845
            if (vzDomainObjBeginJob(dom) < 0)
                goto cleanup;
            job = true;

846 847 848
            if (vzEnsureDomainExists(dom) < 0)
                goto cleanup;

849
            if (prlsdkApplyConfig(driver, dom, def))
850
                goto cleanup;
851

852
            if (prlsdkUpdateDomain(driver, dom))
853 854
                goto cleanup;
        }
855 856
    }

857
    retdom = virGetDomain(conn, def->name, def->uuid, def->id);
858

859
 cleanup:
860 861 862
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
863
    virDomainDefFree(def);
864
    return retdom;
865 866
}

867
static virDomainPtr
868
vzDomainDefineXML(virConnectPtr conn, const char *xml)
869
{
870
    return vzDomainDefineXMLFlags(conn, xml, 0);
871 872 873
}


874
static int
875
vzNodeGetInfo(virConnectPtr conn,
M
Michal Privoznik 已提交
876
              virNodeInfoPtr nodeinfo)
877
{
878 879 880
    if (virNodeGetInfoEnsureACL(conn) < 0)
        return -1;

M
Martin Kletzander 已提交
881
    return virCapabilitiesGetNodeInfo(nodeinfo);
882 883
}

J
Ján Tomko 已提交
884
static int vzConnectIsEncrypted(virConnectPtr conn G_GNUC_UNUSED)
885 886 887 888 889
{
    /* Encryption is not relevant / applicable to way we talk to PCS */
    return 0;
}

J
Ján Tomko 已提交
890
static int vzConnectIsSecure(virConnectPtr conn G_GNUC_UNUSED)
891 892 893 894 895
{
    /* We run CLI tools directly so this is secure */
    return 1;
}

J
Ján Tomko 已提交
896
static int vzConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED)
897 898 899 900
{
    return 1;
}

901

902
static char *
903
vzConnectBaselineCPU(virConnectPtr conn,
M
Michal Privoznik 已提交
904 905 906
                     const char **xmlCPUs,
                     unsigned int ncpus,
                     unsigned int flags)
907
{
J
Jiri Denemark 已提交
908 909 910 911
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr cpu = NULL;
    char *cpustr = NULL;

912 913
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

914 915 916
    if (virConnectBaselineCPUEnsureACL(conn) < 0)
        return NULL;

J
Jiri Denemark 已提交
917 918 919
    if (!(cpus = virCPUDefListParse(xmlCPUs, ncpus, VIR_CPU_TYPE_HOST)))
        goto cleanup;

920
    if (!(cpu = virCPUBaseline(VIR_ARCH_NONE, cpus, ncpus, NULL, NULL, false)))
J
Jiri Denemark 已提交
921 922 923 924 925 926
        goto cleanup;

    if ((flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) &&
        virCPUExpandFeatures(cpus[0]->arch, cpu) < 0)
        goto cleanup;

927
    cpustr = virCPUDefFormat(cpu, NULL);
J
Jiri Denemark 已提交
928 929 930 931 932 933

 cleanup:
    virCPUDefListFree(cpus);
    virCPUDefFree(cpu);

    return cpustr;
934 935 936
}


937
static int
938
vzDomainGetVcpus(virDomainPtr domain,
M
Michal Privoznik 已提交
939 940 941 942
                 virVcpuInfoPtr info,
                 int maxinfo,
                 unsigned char *cpumaps,
                 int maplen)
943
{
944
    virDomainObjPtr dom = NULL;
945 946 947
    size_t i;
    int ret = -1;

J
John Ferlan 已提交
948
    if (!(dom = vzDomObjFromDomain(domain)))
949
        return -1;
950

951 952 953
    if (virDomainGetVcpusEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

954
    if (!virDomainObjIsActive(dom)) {
955 956 957 958 959 960 961 962
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",
                       _("cannot list vcpu pinning for an inactive domain"));
        goto cleanup;
    }

    if (maxinfo >= 1) {
        if (info != NULL) {
963 964
        vzDomObjPtr privdom;

965
            memset(info, 0, sizeof(*info) * maxinfo);
966 967
            privdom = dom->privateData;

968 969 970
            for (i = 0; i < maxinfo; i++) {
                info[i].number = i;
                info[i].state = VIR_VCPU_RUNNING;
971
                if (prlsdkGetVcpuStats(privdom->stats, i, &info[i].cpuTime) < 0)
N
Nikolay Shirokovskiy 已提交
972
                    goto cleanup;
973 974 975 976
            }
        }
        if (cpumaps != NULL) {
            memset(cpumaps, 0, maplen * maxinfo);
977
            for (i = 0; i < maxinfo; i++)
978
                virBitmapToDataBuf(dom->def->cpumask,
979 980
                                   VIR_GET_CPUMAP(cpumaps, maplen, i),
                                   maplen);
981 982 983 984 985
        }
    }
    ret = maxinfo;

 cleanup:
986
    virDomainObjEndAPI(&dom);
987 988 989 990
    return ret;
}


991
static int
992
vzNodeGetCPUMap(virConnectPtr conn,
M
Michal Privoznik 已提交
993 994 995
                unsigned char **cpumap,
                unsigned int *online,
                unsigned int flags)
996
{
997 998 999
    if (virNodeGetCPUMapEnsureACL(conn) < 0)
        return -1;

1000
    return virHostCPUGetMap(cpumap, online, flags);
1001 1002
}

1003
static int
1004
vzConnectDomainEventRegisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
1005 1006 1007 1008 1009
                                virDomainPtr domain,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
                                virFreeCallback freecb)
1010 1011
{
    int ret = -1;
1012
    vzConnPtr privconn = conn->privateData;
1013 1014 1015 1016

    if (virConnectDomainEventRegisterAnyEnsureACL(conn) < 0)
        return -1;

1017
    if (virDomainEventStateRegisterID(conn,
1018
                                      privconn->driver->domainEventState,
1019 1020 1021 1022 1023 1024 1025
                                      domain, eventID,
                                      callback, opaque, freecb, &ret) < 0)
        ret = -1;
    return ret;
}

static int
1026
vzConnectDomainEventDeregisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
1027
                                  int callbackID)
1028
{
1029
    vzConnPtr privconn = conn->privateData;
1030

1031 1032 1033
    if (virConnectDomainEventDeregisterAnyEnsureACL(conn) < 0)
        return -1;

1034
    if (virObjectEventStateDeregisterID(conn,
1035
                                        privconn->driver->domainEventState,
1036
                                        callbackID, true) < 0)
1037
        return -1;
1038

1039
    return 0;
1040
}
1041

1042 1043
static int
vzDomainSuspend(virDomainPtr domain)
1044
{
1045 1046 1047 1048 1049
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

J
John Ferlan 已提交
1050
    if (!(dom = vzDomObjFromDomain(domain)))
1051 1052
        return -1;

1053 1054 1055
    if (virDomainSuspendEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkPause(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1077 1078
}

1079 1080
static int
vzDomainResume(virDomainPtr domain)
1081
{
1082 1083 1084 1085 1086
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

J
John Ferlan 已提交
1087
    if (!(dom = vzDomObjFromDomain(domain)))
1088 1089
        return -1;

1090 1091 1092
    if (virDomainResumeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkResume(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1114 1115
}

1116
static int
1117
vzDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
1118
{
1119 1120 1121 1122 1123
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1124 1125
    virCheckFlags(0, -1);

J
John Ferlan 已提交
1126
    if (!(dom = vzDomObjFromDomain(domain)))
1127 1128
        return -1;

1129 1130 1131
    if (virDomainCreateWithFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkStart(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1153 1154
}

1155
static int
1156
vzDomainDestroyFlags(virDomainPtr domain, unsigned int flags)
1157
{
1158 1159 1160 1161 1162
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1163 1164
    virCheckFlags(0, -1);

J
John Ferlan 已提交
1165
    if (!(dom = vzDomObjFromDomain(domain)))
1166 1167
        return -1;

1168 1169 1170
    if (virDomainDestroyFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkKill(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1192 1193
}

1194
static int
1195 1196 1197 1198 1199 1200 1201
vzDomainDestroy(virDomainPtr dom)
{
    return vzDomainDestroyFlags(dom, 0);
}

static int
vzDomainShutdownFlags(virDomainPtr domain, unsigned int flags)
1202
{
1203 1204 1205 1206 1207
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1208 1209
    virCheckFlags(0, -1);

J
John Ferlan 已提交
1210
    if (!(dom = vzDomObjFromDomain(domain)))
1211 1212
        return -1;

1213 1214 1215
    if (virDomainShutdownFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkStop(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1237 1238
}

1239 1240 1241 1242 1243
static int vzDomainShutdown(virDomainPtr dom)
{
    return vzDomainShutdownFlags(dom, 0);
}

1244 1245
static int
vzDomainReboot(virDomainPtr domain, unsigned int flags)
1246
{
1247 1248 1249 1250 1251
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1252
    virCheckFlags(0, -1);
1253

J
John Ferlan 已提交
1254
    if (!(dom = vzDomObjFromDomain(domain)))
1255 1256
        return -1;

1257 1258 1259
    if (virDomainRebootEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkRestart(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1281 1282
}

1283
static int vzDomainIsActive(virDomainPtr domain)
1284 1285 1286 1287
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

1288
    if (!(dom = vzDomObjFromDomain(domain)))
1289 1290
        return -1;

1291 1292 1293
    if (virDomainIsActiveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1294
    ret = virDomainObjIsActive(dom);
1295 1296

 cleanup:
J
John Ferlan 已提交
1297
    virDomainObjEndAPI(&dom);
1298 1299 1300 1301

    return ret;
}

1302
static int
1303
vzDomainCreate(virDomainPtr domain)
1304
{
1305
    return vzDomainCreateWithFlags(domain, 0);
1306 1307
}

1308
static int
1309
vzDomainUndefineFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1310
                      unsigned int flags)
1311
{
1312
    vzConnPtr privconn = domain->conn->privateData;
1313
    virDomainObjPtr dom = NULL;
1314 1315
    int ret = -1;
    bool job = false;
1316

1317 1318
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
1319

J
John Ferlan 已提交
1320
    if (!(dom = vzDomObjFromDomain(domain)))
1321 1322
        return -1;

1323 1324 1325
    if (virDomainUndefineFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1326 1327 1328 1329
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1330 1331 1332
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1333
    ret = prlsdkUnregisterDomain(privconn->driver, dom, flags);
1334 1335 1336 1337 1338 1339

 cleanup:

    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1340 1341

    return ret;
1342 1343 1344
}

static int
1345
vzDomainUndefine(virDomainPtr domain)
1346
{
1347
    return vzDomainUndefineFlags(domain, 0);
1348 1349
}

1350
static int
1351
vzDomainHasManagedSaveImage(virDomainPtr domain, unsigned int flags)
1352 1353
{
    virDomainObjPtr dom = NULL;
1354
    int state, reason;
1355
    int ret = -1;
1356 1357 1358

    virCheckFlags(0, -1);

1359
    if (!(dom = vzDomObjFromDomain(domain)))
1360 1361
        return -1;

1362 1363 1364
    if (virDomainHasManagedSaveImageEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1365 1366 1367
    state = virDomainObjGetState(dom, &reason);
    if (state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED)
        ret = 1;
1368 1369 1370 1371
    else
        ret = 0;

 cleanup:
J
John Ferlan 已提交
1372
    virDomainObjEndAPI(&dom);
1373

1374 1375 1376 1377
    return ret;
}

static int
1378
vzDomainManagedSave(virDomainPtr domain, unsigned int flags)
1379
{
1380
    vzConnPtr privconn = domain->conn->privateData;
1381 1382 1383
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;
1384
    bool job = false;
1385 1386 1387 1388

    virCheckFlags(VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

J
John Ferlan 已提交
1389
    if (!(dom = vzDomObjFromDomain(domain)))
1390 1391
        return -1;

1392 1393 1394
    if (virDomainManagedSaveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1395 1396 1397 1398
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1399 1400 1401
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1402 1403
    state = virDomainObjGetState(dom, &reason);

1404 1405 1406
    if (state == VIR_DOMAIN_RUNNING && (flags & VIR_DOMAIN_SAVE_PAUSED) &&
        prlsdkPause(dom) < 0)
        goto cleanup;
1407

1408 1409 1410 1411 1412 1413 1414
    if (prlsdkSuspend(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;
1415 1416

 cleanup:
1417 1418 1419
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1420 1421 1422 1423
    return ret;
}

static int
1424
vzDomainManagedSaveRemove(virDomainPtr domain, unsigned int flags)
1425 1426 1427 1428 1429 1430 1431
{
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
1432
    if (!(dom = vzDomObjFromDomain(domain)))
1433 1434
        return -1;

1435 1436 1437
    if (virDomainManagedSaveRemoveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1438 1439 1440 1441 1442
    state = virDomainObjGetState(dom, &reason);

    if (!(state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED))
        goto cleanup;

1443
    ret = prlsdkDomainManagedSaveRemove(dom);
1444 1445

 cleanup:
1446
    virDomainObjEndAPI(&dom);
1447
    return ret;
1448 1449
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
static int vzCheckConfigUpdateFlags(virDomainObjPtr dom, unsigned int *flags)
{
    if (virDomainObjUpdateModificationImpact(dom, flags) < 0)
        return -1;

    if (!(*flags & VIR_DOMAIN_AFFECT_CONFIG)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("domain config update needs VIR_DOMAIN_AFFECT_CONFIG "
                         "flag to be set"));
        return -1;
    }

    if (virDomainObjIsActive(dom) && !(*flags & VIR_DOMAIN_AFFECT_LIVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Updates on a running domain need "
                         "VIR_DOMAIN_AFFECT_LIVE flag"));
        return -1;
    }

    return 0;
}

1472
static int vzDomainAttachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1473
                                     unsigned int flags)
1474 1475
{
    int ret = -1;
1476
    vzConnPtr privconn = domain->conn->privateData;
1477
    virDomainDeviceDefPtr dev = NULL;
1478
    virDomainObjPtr dom = NULL;
1479
    vzDriverPtr driver = privconn->driver;
1480
    bool job = false;
1481 1482 1483 1484

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
1485
    if (!(dom = vzDomObjFromDomain(domain)))
1486
        return -1;
1487

1488
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
1489 1490
        goto cleanup;

1491 1492 1493
    if (virDomainAttachDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1494
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
1495
                                  driver->xmlopt, NULL, VIR_DOMAIN_XML_INACTIVE);
1496 1497 1498
    if (dev == NULL)
        goto cleanup;

1499 1500 1501 1502
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1503 1504 1505
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1506 1507
    if (prlsdkAttachDevice(driver, dom, dev) < 0)
        goto cleanup;
1508

1509 1510 1511
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1512 1513
    ret = 0;
 cleanup:
1514
    virDomainDeviceDefFree(dev);
1515 1516 1517
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1518 1519 1520
    return ret;
}

1521
static int vzDomainAttachDevice(virDomainPtr domain, const char *xml)
1522
{
1523
    return vzDomainAttachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1524
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1525 1526
}

1527
static int vzDomainDetachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1528
                                     unsigned int flags)
1529 1530
{
    int ret = -1;
1531
    vzConnPtr privconn = domain->conn->privateData;
1532
    virDomainDeviceDefPtr dev = NULL;
1533
    virDomainObjPtr dom = NULL;
1534
    vzDriverPtr driver = privconn->driver;
1535
    bool job = false;
1536 1537 1538 1539

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
1540
    dom = vzDomObjFromDomain(domain);
1541
    if (dom == NULL)
1542 1543
        return -1;

1544
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
1545 1546
        goto cleanup;

1547 1548 1549
    if (virDomainDetachDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1550
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
1551
                                  driver->xmlopt, NULL,
1552 1553
                                  VIR_DOMAIN_XML_INACTIVE |
                                  VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE);
1554 1555 1556
    if (dev == NULL)
        goto cleanup;

1557 1558 1559 1560
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1561 1562 1563
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1564 1565
    if (prlsdkDetachDevice(driver, dom, dev) < 0)
        goto cleanup;
1566

1567 1568 1569
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1570 1571
    ret = 0;
 cleanup:
1572
    virDomainDeviceDefFree(dev);
1573 1574 1575
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1576

1577 1578 1579
    return ret;
}

1580
static int vzDomainDetachDevice(virDomainPtr domain, const char *xml)
1581
{
1582
    return vzDomainDetachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1583
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1584 1585
}

1586 1587 1588 1589 1590 1591 1592 1593
static int
vzDomainSetUserPassword(virDomainPtr domain,
                        const char *user,
                        const char *password,
                        unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
1594
    bool job = false;
1595 1596

    virCheckFlags(0, -1);
J
John Ferlan 已提交
1597
    if (!(dom = vzDomObjFromDomain(domain)))
1598 1599
        return -1;

1600 1601 1602
    if (virDomainSetUserPasswordEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1603 1604 1605 1606
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1607 1608 1609
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1610 1611
    ret = prlsdkDomainSetUserPassword(dom, user, password);

1612 1613 1614 1615
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1616 1617 1618
    return ret;
}

1619
static int vzDomainUpdateDeviceFlags(virDomainPtr domain,
N
Nikolay Shirokovskiy 已提交
1620 1621 1622 1623
                                     const char *xml,
                                     unsigned int flags)
{
    int ret = -1;
1624 1625
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom = NULL;
N
Nikolay Shirokovskiy 已提交
1626 1627
    virDomainDeviceDefPtr dev = NULL;
    vzDriverPtr driver = privconn->driver;
1628
    bool job = false;
N
Nikolay Shirokovskiy 已提交
1629 1630 1631 1632

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
1633
    if (!(dom = vzDomObjFromDomain(domain)))
N
Nikolay Shirokovskiy 已提交
1634 1635
        return -1;

1636 1637 1638
    if (virDomainUpdateDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1639
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
N
Nikolay Shirokovskiy 已提交
1640 1641
        goto cleanup;

1642
    if (!(dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
1643
                                        driver->xmlopt, NULL,
N
Nikolay Shirokovskiy 已提交
1644 1645 1646
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto cleanup;

1647 1648 1649 1650
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1651 1652 1653
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1654
    if (prlsdkUpdateDevice(driver, dom, dev) < 0)
N
Nikolay Shirokovskiy 已提交
1655 1656
        goto cleanup;

1657 1658 1659
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

N
Nikolay Shirokovskiy 已提交
1660 1661 1662 1663
    ret = 0;
 cleanup:

    virDomainDeviceDefFree(dev);
1664 1665 1666
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
N
Nikolay Shirokovskiy 已提交
1667 1668 1669 1670
    return ret;
}


1671
static unsigned long long
1672
vzDomainGetMaxMemory(virDomainPtr domain)
1673 1674 1675 1676
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

1677
    if (!(dom = vzDomObjFromDomain(domain)))
1678 1679
        return -1;

1680 1681 1682
    if (virDomainGetMaxMemoryEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1683
    ret = virDomainDefGetMemoryTotal(dom->def);
1684 1685

 cleanup:
J
John Ferlan 已提交
1686
    virDomainObjEndAPI(&dom);
1687 1688 1689
    return ret;
}

1690
static int
1691 1692 1693
vzDomainBlockStatsImpl(virDomainObjPtr dom,
                       const char *path,
                       virDomainBlockStatsPtr stats)
1694
{
1695
    vzDomObjPtr privdom = dom->privateData;
1696 1697 1698 1699 1700 1701
    size_t i;
    int idx;

    if (*path) {
        if ((idx = virDomainDiskIndexByName(dom->def, path, false)) < 0) {
            virReportError(VIR_ERR_INVALID_ARG, _("invalid path: %s"), path);
1702
            return -1;
1703
        }
M
Maxim Nestratov 已提交
1704 1705 1706 1707
        if (prlsdkGetBlockStats(privdom->stats,
                                dom->def->disks[idx],
                                stats,
                                IS_CT(dom->def)) < 0)
1708
            return -1;
1709 1710 1711
    } else {
        virDomainBlockStatsStruct s;

1712
#define PARALLELS_ZERO_STATS(VAR, TYPE, NAME) \
1713 1714 1715 1716 1717 1718 1719
        stats->VAR = 0;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_ZERO_STATS)

#undef PARALLELS_ZERO_STATS

        for (i = 0; i < dom->def->ndisks; i++) {
M
Maxim Nestratov 已提交
1720 1721 1722 1723
            if (prlsdkGetBlockStats(privdom->stats,
                                    dom->def->disks[i],
                                    &s,
                                    IS_CT(dom->def)) < 0)
1724
                return -1;
1725

1726 1727
#define PARALLELS_SUM_STATS(VAR, TYPE, NAME) \
    if (s.VAR != -1) \
M
Michal Privoznik 已提交
1728
        stats->VAR += s.VAR;
1729

M
Michal Privoznik 已提交
1730
        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_SUM_STATS)
1731

1732
#undef PARALLELS_SUM_STATS
1733 1734 1735
        }
    }
    stats->errs = -1;
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
    return 0;
}

static int
vzDomainBlockStats(virDomainPtr domain,
                   const char *path,
                   virDomainBlockStatsPtr stats)
{
    virDomainObjPtr dom;
    int ret = -1;

J
John Ferlan 已提交
1747
    if (!(dom = vzDomObjFromDomain(domain)))
1748 1749
        return -1;

1750 1751 1752
    if (virDomainBlockStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1753 1754 1755
    if (vzDomainBlockStatsImpl(dom, path, stats) < 0)
        goto cleanup;

1756 1757 1758
    ret = 0;

 cleanup:
1759
    virDomainObjEndAPI(&dom);
1760 1761 1762 1763

    return ret;
}

1764 1765 1766 1767 1768 1769 1770 1771
static int
vzDomainBlockStatsToParams(virDomainBlockStatsPtr stats,
                           virTypedParameterPtr params,
                           int *nparams)
{
    size_t i;

    if (*nparams == 0) {
1772 1773
#define PARALLELS_COUNT_STATS(VAR, TYPE, NAME) \
        if ((stats->VAR) != -1) \
1774 1775 1776 1777 1778 1779 1780 1781 1782
            ++*nparams;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_COUNT_STATS)

#undef PARALLELS_COUNT_STATS
        return 0;
    }

    i = 0;
1783 1784 1785 1786 1787 1788
#define PARALLELS_BLOCK_STATS_ASSIGN_PARAM(VAR, TYPE, NAME) \
    if (i < *nparams && (stats->VAR) != -1) { \
        if (virTypedParameterAssign(params + i, TYPE, \
                                    VIR_TYPED_PARAM_LLONG, (stats->VAR)) < 0) \
            return -1; \
        i++; \
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
    }

    PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_BLOCK_STATS_ASSIGN_PARAM)

#undef PARALLELS_BLOCK_STATS_ASSIGN_PARAM

    *nparams = i;
    return 0;
}

1799
static int
1800
vzDomainBlockStatsFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1801 1802 1803 1804
                        const char *path,
                        virTypedParameterPtr params,
                        int *nparams,
                        unsigned int flags)
1805 1806
{
    virDomainBlockStatsStruct stats;
1807
    virDomainObjPtr dom;
1808 1809 1810 1811 1812 1813
    int ret = -1;

    virCheckFlags(VIR_TYPED_PARAM_STRING_OKAY, -1);
    /* We don't return strings, and thus trivially support this flag.  */
    flags &= ~VIR_TYPED_PARAM_STRING_OKAY;

J
John Ferlan 已提交
1814
    if (!(dom = vzDomObjFromDomain(domain)))
1815 1816
        return -1;

1817 1818 1819
    if (virDomainBlockStatsFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1820
    if (vzDomainBlockStatsImpl(dom, path, &stats) < 0)
1821 1822
        goto cleanup;

1823
    if (vzDomainBlockStatsToParams(&stats, params, nparams) < 0)
1824 1825 1826 1827 1828
        goto cleanup;

    ret = 0;

 cleanup:
1829 1830
    virDomainObjEndAPI(&dom);

1831 1832 1833
    return ret;
}

1834 1835
static int
vzDomainInterfaceStats(virDomainPtr domain,
1836
                         const char *device,
1837 1838 1839
                         virDomainInterfaceStatsPtr stats)
{
    virDomainObjPtr dom = NULL;
1840
    vzDomObjPtr privdom;
1841
    int ret = -1;
1842

J
John Ferlan 已提交
1843
    if (!(dom = vzDomObjFromDomain(domain)))
1844 1845
        return -1;

1846 1847 1848
    if (virDomainInterfaceStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1849 1850
    privdom = dom->privateData;

1851
    ret = prlsdkGetNetStats(privdom->stats, privdom->sdkdom, device, stats);
1852 1853

 cleanup:
1854 1855 1856 1857
    virDomainObjEndAPI(&dom);

    return ret;
}
1858

N
Nikolay Shirokovskiy 已提交
1859 1860 1861 1862 1863 1864 1865
static int
vzDomainMemoryStats(virDomainPtr domain,
                    virDomainMemoryStatPtr stats,
                    unsigned int nr_stats,
                    unsigned int flags)
{
    virDomainObjPtr dom = NULL;
1866
    vzDomObjPtr privdom;
N
Nikolay Shirokovskiy 已提交
1867 1868 1869
    int ret = -1;

    virCheckFlags(0, -1);
J
John Ferlan 已提交
1870
    if (!(dom = vzDomObjFromDomain(domain)))
N
Nikolay Shirokovskiy 已提交
1871 1872
        return -1;

1873 1874 1875
    if (virDomainMemoryStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1876 1877 1878
    privdom = dom->privateData;

    ret = prlsdkGetMemoryStats(privdom->stats, stats, nr_stats);
1879 1880

 cleanup:
N
Nikolay Shirokovskiy 已提交
1881 1882 1883 1884 1885
    virDomainObjEndAPI(&dom);

    return ret;
}

1886
static int
1887
vzDomainGetVcpusFlags(virDomainPtr domain,
1888 1889
                      unsigned int flags)
{
1890
    virDomainObjPtr dom;
1891
    int ret = -1;
1892 1893 1894 1895 1896

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

1897
    if (!(dom = vzDomObjFromDomain(domain)))
1898
        return -1;
1899

1900 1901 1902
    if (virDomainGetVcpusFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1903
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
1904
        ret = virDomainDefGetVcpusMax(dom->def);
1905
    else
1906
        ret = virDomainDefGetVcpus(dom->def);
1907

1908
 cleanup:
J
John Ferlan 已提交
1909
    virDomainObjEndAPI(&dom);
1910 1911 1912 1913

    return ret;
}

1914
static int vzDomainGetMaxVcpus(virDomainPtr domain)
1915
{
1916 1917
    return vzDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
                                          VIR_DOMAIN_VCPU_MAXIMUM));
1918 1919
}

1920
static int vzDomainIsUpdated(virDomainPtr domain)
1921
{
1922
    virDomainObjPtr dom;
1923
    int ret = -1;
1924 1925 1926

    /* As far as VZ domains are always updated (e.g. current==persistent),
     * we just check for domain existence */
1927
    if (!(dom = vzDomObjFromDomain(domain)))
1928
        return -1;
1929

1930 1931 1932 1933 1934 1935
    if (virDomainIsUpdatedEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
J
John Ferlan 已提交
1936
    virDomainObjEndAPI(&dom);
1937
    return ret;
1938 1939
}

1940
static int vzConnectGetMaxVcpus(virConnectPtr conn,
1941 1942
                                const char *type)
{
1943 1944 1945
    if (virConnectGetMaxVcpusEnsureACL(conn) < 0)
        return -1;

1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
    /* As far as we have no limitation for containers
     * we report maximum */
    if (type == NULL || STRCASEEQ(type, "vz") || STRCASEEQ(type, "parallels"))
        return 1028;

    virReportError(VIR_ERR_INVALID_ARG,
                   _("unknown type '%s'"), type);
    return -1;
}

1956
static int
1957
vzNodeGetCPUStats(virConnectPtr conn,
1958 1959 1960 1961 1962
                  int cpuNum,
                  virNodeCPUStatsPtr params,
                  int *nparams,
                  unsigned int flags)
{
1963 1964 1965
    if (virNodeGetCPUStatsEnsureACL(conn) < 0)
        return -1;

1966
    return virHostCPUGetStats(cpuNum, params, nparams, flags);
1967 1968 1969
}

static int
1970
vzNodeGetMemoryStats(virConnectPtr conn,
1971 1972 1973 1974 1975
                     int cellNum,
                     virNodeMemoryStatsPtr params,
                     int *nparams,
                     unsigned int flags)
{
1976 1977 1978
    if (virNodeGetMemoryStatsEnsureACL(conn) < 0)
        return -1;

1979
    return virHostMemGetStats(cellNum, params, nparams, flags);
1980 1981 1982
}

static int
1983
vzNodeGetCellsFreeMemory(virConnectPtr conn,
1984 1985 1986 1987
                         unsigned long long *freeMems,
                         int startCell,
                         int maxCells)
{
1988 1989 1990
    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
        return -1;

1991
    return virHostMemGetCellsFree(freeMems, startCell, maxCells);
1992 1993 1994
}

static unsigned long long
1995
vzNodeGetFreeMemory(virConnectPtr conn)
1996 1997
{
    unsigned long long freeMem;
1998 1999 2000 2001

    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
        return -1;

2002
    if (virHostMemGetInfo(NULL, &freeMem) < 0)
2003 2004 2005 2006
        return 0;
    return freeMem;
}

2007 2008 2009 2010 2011 2012 2013 2014 2015
static int
vzConnectRegisterCloseCallback(virConnectPtr conn,
                               virConnectCloseFunc cb,
                               void *opaque,
                               virFreeCallback freecb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

2016 2017 2018
    if (virConnectRegisterCloseCallbackEnsureACL(conn) < 0)
        return -1;

2019 2020
    virObjectLock(privconn->driver);

2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
    if (virConnectCloseCallbackDataGetCallback(privconn->closeCallback) != NULL) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A close callback is already registered"));
        goto cleanup;
    }

    virConnectCloseCallbackDataRegister(privconn->closeCallback, conn, cb,
                                        opaque, freecb);
    ret = 0;

 cleanup:
2032
    virObjectUnlock(privconn->driver);
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042

    return ret;
}

static int
vzConnectUnregisterCloseCallback(virConnectPtr conn, virConnectCloseFunc cb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

2043 2044 2045
    if (virConnectUnregisterCloseCallbackEnsureACL(conn) < 0)
        return -1;

2046
    virObjectLock(privconn->driver);
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057

    if (virConnectCloseCallbackDataGetCallback(privconn->closeCallback) != cb) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A different callback was requested"));
        goto cleanup;
    }

    virConnectCloseCallbackDataUnregister(privconn->closeCallback, cb);
    ret = 0;

 cleanup:
2058
    virObjectUnlock(privconn->driver);
2059 2060 2061 2062

    return ret;
}

2063 2064
static int vzDomainSetMemoryFlags(virDomainPtr domain, unsigned long memory,
                                  unsigned int flags)
2065 2066 2067
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
2068
    bool job = false;
2069 2070 2071 2072

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
2073
    if (!(dom = vzDomObjFromDomain(domain)))
2074 2075
        return -1;

2076
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
2077 2078
        goto cleanup;

2079 2080 2081
    if (virDomainSetMemoryFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

2082 2083 2084 2085
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2086 2087 2088
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2089 2090 2091 2092
    ret = prlsdkSetMemsize(dom, memory >> 10);

 cleanup:

2093 2094 2095
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2096 2097 2098 2099 2100
    return ret;
}

static int vzDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
2101 2102 2103 2104
    virDomainObjPtr dom = NULL;
    int ret = -1;
    bool job = false;

J
John Ferlan 已提交
2105
    if (!(dom = vzDomObjFromDomain(domain)))
2106 2107
        return -1;

2108 2109 2110
    if (virDomainSetMemoryEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    ret = prlsdkSetMemsize(dom, memory >> 10);

 cleanup:

    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
2126 2127
}

2128
static virDomainMomentObjPtr
2129 2130
vzSnapObjFromName(virDomainSnapshotObjListPtr snapshots, const char *name)
{
2131
    virDomainMomentObjPtr snap = NULL;
2132 2133 2134 2135 2136 2137 2138 2139
    snap = virDomainSnapshotFindByName(snapshots, name);
    if (!snap)
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("no domain snapshot with matching name '%s'"), name);

    return snap;
}

2140
static virDomainMomentObjPtr
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
vzSnapObjFromSnapshot(virDomainSnapshotObjListPtr snapshots,
                      virDomainSnapshotPtr snapshot)
{
    return vzSnapObjFromName(snapshots, snapshot->name);
}

static int
vzDomainSnapshotNum(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2157
    if (!(dom = vzDomObjFromDomain(domain)))
2158 2159
        return -1;

2160 2161 2162
    if (virDomainSnapshotNumEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2163 2164 2165 2166 2167 2168 2169
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(snapshots, NULL, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2170
    virDomainObjEndAPI(&dom);
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187

    return n;
}

static int
vzDomainSnapshotListNames(virDomainPtr domain,
                          char **names,
                          int nameslen,
                          unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2188
    if (!(dom = vzDomObjFromDomain(domain)))
2189 2190
        return -1;

2191 2192 2193
    if (virDomainSnapshotListNamesEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2194 2195 2196 2197 2198 2199 2200
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(snapshots, NULL, names, nameslen, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2201
    virDomainObjEndAPI(&dom);
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217

    return n;
}

static int
vzDomainListAllSnapshots(virDomainPtr domain,
                         virDomainSnapshotPtr **snaps,
                         unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2218
    if (!(dom = vzDomObjFromDomain(domain)))
2219 2220
        return -1;

2221 2222 2223
    if (virDomainListAllSnapshotsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2224 2225 2226 2227 2228 2229 2230
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    n = virDomainListSnapshots(snapshots, NULL, domain, snaps, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2231
    virDomainObjEndAPI(&dom);
2232 2233 2234 2235 2236 2237 2238 2239 2240

    return n;
}

static char *
vzDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    char *xml = NULL;
2241
    virDomainMomentObjPtr snap;
2242 2243 2244 2245
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDomainSnapshotObjListPtr snapshots = NULL;
    vzConnPtr privconn = snapshot->domain->conn->privateData;

2246
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_XML_SECURE, NULL);
2247

J
John Ferlan 已提交
2248
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2249 2250
        return NULL;

2251 2252 2253
    if (virDomainSnapshotGetXMLDescEnsureACL(snapshot->domain->conn, dom->def, flags) < 0)
        goto cleanup;

2254 2255 2256 2257 2258 2259 2260 2261
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

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

2262 2263
    xml = virDomainSnapshotDefFormat(uuidstr, virDomainSnapshotObjGetDef(snap),
                                     privconn->driver->caps,
2264
                                     privconn->driver->xmlopt,
2265
                                     virDomainSnapshotFormatConvertXMLFlags(flags));
2266 2267 2268

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2269
    virDomainObjEndAPI(&dom);
2270 2271 2272 2273 2274 2275 2276 2277

    return xml;
}

static int
vzDomainSnapshotNumChildren(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
2278
    virDomainMomentObjPtr snap;
2279 2280 2281 2282 2283 2284
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2285
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2286 2287
        return -1;

2288 2289 2290
    if (virDomainSnapshotNumChildrenEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(snapshots, snap, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2301
    virDomainObjEndAPI(&dom);
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312

    return n;
}

static int
vzDomainSnapshotListChildrenNames(virDomainSnapshotPtr snapshot,
                                  char **names,
                                  int nameslen,
                                  unsigned int flags)
{
    virDomainObjPtr dom;
2313
    virDomainMomentObjPtr snap;
2314 2315 2316 2317 2318 2319
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2320
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2321 2322
        return -1;

2323 2324 2325
    if (virDomainSnapshotListChildrenNamesEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(snapshots, snap, names, nameslen, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2336
    virDomainObjEndAPI(&dom);
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346

    return n;
}

static int
vzDomainSnapshotListAllChildren(virDomainSnapshotPtr snapshot,
                                virDomainSnapshotPtr **snaps,
                                unsigned int flags)
{
    virDomainObjPtr dom;
2347
    virDomainMomentObjPtr snap;
2348 2349 2350 2351 2352 2353
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2354
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2355 2356
        return -1;

2357 2358 2359
    if (virDomainSnapshotListAllChildrenEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(snapshots, snap, snapshot->domain, snaps, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2370
    virDomainObjEndAPI(&dom);
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380

    return n;
}

static virDomainSnapshotPtr
vzDomainSnapshotLookupByName(virDomainPtr domain,
                             const char *name,
                             unsigned int flags)
{
    virDomainObjPtr dom;
2381
    virDomainMomentObjPtr snap;
2382 2383 2384 2385 2386
    virDomainSnapshotPtr snapshot = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, NULL);

J
John Ferlan 已提交
2387
    if (!(dom = vzDomObjFromDomain(domain)))
2388 2389
        return NULL;

2390 2391 2392
    if (virDomainSnapshotLookupByNameEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2393 2394 2395 2396 2397 2398 2399 2400 2401
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromName(snapshots, name)))
        goto cleanup;

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

 cleanup:
2402
    virDomainObjEndAPI(&dom);
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
    virDomainSnapshotObjListFree(snapshots);

    return snapshot;
}

static int
vzDomainHasCurrentSnapshot(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
2417
    if (!(dom = vzDomObjFromDomain(domain)))
2418 2419
        return -1;

2420 2421 2422
    if (virDomainHasCurrentSnapshotEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2423 2424 2425
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

2426
    ret = virDomainSnapshotGetCurrent(snapshots) != NULL;
2427 2428 2429

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2430
    virDomainObjEndAPI(&dom);
2431 2432 2433 2434 2435 2436 2437 2438

    return ret;
}

static virDomainSnapshotPtr
vzDomainSnapshotGetParent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
2439
    virDomainMomentObjPtr snap;
2440 2441 2442 2443 2444
    virDomainSnapshotPtr parent = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, NULL);

J
John Ferlan 已提交
2445
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2446 2447
        return NULL;

2448 2449 2450
    if (virDomainSnapshotGetParentEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2451 2452 2453 2454 2455 2456
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

2457
    if (!snap->def->parent_name) {
2458 2459 2460 2461 2462 2463
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("snapshot '%s' does not have a parent"),
                       snap->def->name);
        goto cleanup;
    }

2464
    parent = virGetDomainSnapshot(snapshot->domain, snap->def->parent_name);
2465 2466 2467

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2468
    virDomainObjEndAPI(&dom);
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478

    return parent;
}

static virDomainSnapshotPtr
vzDomainSnapshotCurrent(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotPtr snapshot = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;
2479
    virDomainMomentObjPtr current;
2480 2481 2482

    virCheckFlags(0, NULL);

J
John Ferlan 已提交
2483
    if (!(dom = vzDomObjFromDomain(domain)))
2484 2485
        return NULL;

2486 2487 2488
    if (virDomainSnapshotCurrentEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2489 2490 2491
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

2492
    if (!(current = virDomainSnapshotGetCurrent(snapshots))) {
2493 2494 2495 2496 2497 2498 2499 2500 2501
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT, "%s",
                       _("the domain does not have a current snapshot"));
        goto cleanup;
    }

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2502
    virDomainObjEndAPI(&dom);
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512

    return snapshot;
}

static int
vzDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
    virDomainSnapshotObjListPtr snapshots = NULL;
2513
    virDomainMomentObjPtr current;
2514 2515 2516

    virCheckFlags(0, -1);

J
John Ferlan 已提交
2517
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2518 2519
        return -1;

2520 2521 2522
    if (virDomainSnapshotIsCurrentEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2523 2524 2525
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

2526
    current = virDomainSnapshotGetCurrent(snapshots);
2527 2528 2529 2530
    ret = current && STREQ(snapshot->name, current->def->name);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2531
    virDomainObjEndAPI(&dom);
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541

    return ret;
}

static int
vzDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
2542
    virDomainMomentObjPtr snap;
2543 2544 2545 2546
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
2547
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2548 2549
        return -1;

2550 2551 2552
    if (virDomainSnapshotHasMetadataEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    ret = 1;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2563 2564
    virDomainObjEndAPI(&dom);

2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
    return ret;
}

static virDomainSnapshotPtr
vzDomainSnapshotCreateXML(virDomainPtr domain,
                          const char *xmlDesc,
                          unsigned int flags)
{
    virDomainSnapshotPtr snapshot = NULL;
    virDomainObjPtr dom;
    vzConnPtr privconn = domain->conn->privateData;
    vzDriverPtr driver = privconn->driver;
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
    virDomainSnapshotObjListPtr snapshots = NULL;
2579
    virDomainMomentObjPtr current;
2580
    bool job = false;
2581
    g_autoptr(virDomainSnapshotDef) def = NULL;
2582

2583
    virCheckFlags(VIR_DOMAIN_SNAPSHOT_CREATE_VALIDATE, NULL);
2584

J
John Ferlan 已提交
2585
    if (!(dom = vzDomObjFromDomain(domain)))
2586 2587
        return NULL;

2588 2589 2590
    if (virDomainSnapshotCreateXMLEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

2591 2592 2593
    if (flags & VIR_DOMAIN_SNAPSHOT_CREATE_VALIDATE)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_VALIDATE;

2594
    if (!(def = virDomainSnapshotDefParseString(xmlDesc, driver->caps,
2595
                                                driver->xmlopt, NULL, NULL,
2596
                                                parse_flags)))
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
        goto cleanup;

    if (def->ndisks > 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("configuring disks is not supported for vz snapshots"));
        goto cleanup;
    }

    if (def->memory) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("configuring memory location is not supported"));
        goto cleanup;
    }

2611 2612 2613 2614
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2615 2616 2617
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2618
    /* snaphot name is ignored, it will be set to auto generated by sdk uuid */
2619
    if (prlsdkCreateSnapshot(dom, def->parent.description) < 0)
2620 2621 2622 2623 2624
        goto cleanup;

    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

2625
    if (!(current = virDomainSnapshotGetCurrent(snapshots))) {
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("can't find created snapshot"));
        goto cleanup;
    }

    /* hopefully new current snapshot is newly created one */
    snapshot = virGetDomainSnapshot(domain, current->def->name);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2636 2637 2638
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650

    return snapshot;
}

static int
vzDomainSnapshotDelete(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN, -1);

J
John Ferlan 已提交
2651
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2652 2653
        return -1;

2654 2655 2656
    if (virDomainSnapshotDeleteEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2657 2658 2659
    ret = prlsdkDeleteSnapshot(dom, snapshot->name,
                               flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN);

2660
 cleanup:
2661
    virDomainObjEndAPI(&dom);
2662 2663 2664 2665 2666 2667 2668 2669 2670

    return ret;
}

static int
vzDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
2671
    bool job = false;
2672 2673 2674

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED, -1);

J
John Ferlan 已提交
2675
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2676 2677
        return -1;

2678 2679 2680
    if (virDomainRevertToSnapshotEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2681 2682 2683 2684
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2685 2686 2687
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2688 2689
    ret = prlsdkSwitchToSnapshot(dom, snapshot->name,
                                 flags & VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED);
2690 2691 2692 2693
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2694 2695 2696 2697

    return ret;
}

2698 2699 2700 2701 2702 2703
enum vzMigrationCookieFeatures {
    VZ_MIGRATION_COOKIE_SESSION_UUID  = (1 << 0),
    VZ_MIGRATION_COOKIE_DOMAIN_UUID = (1 << 1),
    VZ_MIGRATION_COOKIE_DOMAIN_NAME = (1 << 1),
};

2704 2705 2706
typedef struct _vzMigrationCookie vzMigrationCookie;
typedef vzMigrationCookie *vzMigrationCookiePtr;
struct _vzMigrationCookie {
2707 2708
    unsigned char *session_uuid;
    unsigned char *uuid;
2709 2710 2711 2712 2713 2714 2715 2716
    char *name;
};

static void
vzMigrationCookieFree(vzMigrationCookiePtr mig)
{
    if (!mig)
        return;
2717 2718 2719

    VIR_FREE(mig->session_uuid);
    VIR_FREE(mig->uuid);
2720 2721 2722 2723 2724
    VIR_FREE(mig->name);
    VIR_FREE(mig);
}

static int
2725 2726 2727 2728
vzBakeCookie(vzDriverPtr driver,
             virDomainObjPtr dom,
             char **cookieout, int *cookieoutlen,
             unsigned int flags)
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!cookieout || !cookieoutlen) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Migration cookie parameters are not provided."));
        return -1;
    }

    *cookieout = NULL;
    *cookieoutlen = 0;

    virBufferAddLit(&buf, "<vz-migration>\n");
    virBufferAdjustIndent(&buf, 2);
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765

    if (flags & VZ_MIGRATION_COOKIE_SESSION_UUID) {
        virUUIDFormat(driver->session_uuid, uuidstr);
        virBufferAsprintf(&buf, "<session-uuid>%s</session-uuid>\n", uuidstr);
    }

    if (flags & VZ_MIGRATION_COOKIE_DOMAIN_UUID) {
        unsigned char fakeuuid[VIR_UUID_BUFLEN] = { 0 };

        /* if dom is NULL just pass some parsable uuid for backward compat.
         * It is not used by peer */
        virUUIDFormat(dom ? dom->def->uuid : fakeuuid, uuidstr);
        virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", uuidstr);
    }

    if (flags & VZ_MIGRATION_COOKIE_DOMAIN_NAME) {
        /* if dom is NULL just pass some name for backward compat.
         * It is not used by peer */
        virBufferAsprintf(&buf, "<name>%s</name>\n", dom ? dom->def->name :
                                                           "__fakename__");
    }

2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</vz-migration>\n");

    *cookieout = virBufferContentAndReset(&buf);
    *cookieoutlen = strlen(*cookieout) + 1;

    return 0;
}

static vzMigrationCookiePtr
2776
vzEatCookie(const char *cookiein, int cookieinlen, unsigned int flags)
2777 2778 2779
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctx = NULL;
2780
    char *tmp = NULL;
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
    vzMigrationCookiePtr mig = NULL;

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

    if (!cookiein || cookieinlen <= 0 || cookiein[cookieinlen - 1] != '\0') {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Invalid migration cookie"));
        goto error;
    }

    if (!(doc = virXMLParseStringCtxt(cookiein,
                                      _("(_migration_cookie)"), &ctx)))
        goto error;

2796 2797 2798 2799
    if ((flags & VZ_MIGRATION_COOKIE_SESSION_UUID)
        && (!(tmp = virXPathString("string(./session-uuid[1])", ctx))
            || (VIR_ALLOC_N(mig->session_uuid, VIR_UUID_BUFLEN) < 0)
            || (virUUIDParse(tmp, mig->session_uuid) < 0))) {
2800
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2801 2802
                       _("missing or malformed session-uuid element "
                         "in migration data"));
2803 2804 2805 2806 2807
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2808 2809 2810 2811
    if ((flags & VZ_MIGRATION_COOKIE_DOMAIN_UUID)
        && (!(tmp = virXPathString("string(./uuid[1])", ctx))
            || (VIR_ALLOC_N(mig->uuid, VIR_UUID_BUFLEN) < 0)
            || (virUUIDParse(tmp, mig->uuid) < 0))) {
2812
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2813
                       _("missing or malformed uuid element in migration data"));
2814 2815 2816 2817 2818
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2819 2820
    if ((flags & VZ_MIGRATION_COOKIE_DOMAIN_NAME)
        && !(mig->name = virXPathString("string(./name[1])", ctx))) {
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("missing name element in migration data"));
        goto error;
    }

 cleanup:
    xmlXPathFreeContext(ctx);
    xmlFreeDoc(doc);
    return mig;

 error:
    vzMigrationCookieFree(mig);
    mig = NULL;
    goto cleanup;
}

2837 2838 2839
#define VZ_MIGRATION_FLAGS         (VIR_MIGRATE_PAUSED | \
                                    VIR_MIGRATE_PEER2PEER | \
                                    VIR_MIGRATE_LIVE | \
2840
                                    VIR_MIGRATE_UNDEFINE_SOURCE | \
2841
                                    VIR_MIGRATE_PERSIST_DEST | \
2842
                                    VIR_MIGRATE_NON_SHARED_INC)
2843

2844
#define VZ_MIGRATION_PARAMETERS \
2845 2846 2847
    VIR_MIGRATE_PARAM_DEST_XML,         VIR_TYPED_PARAM_STRING, \
    VIR_MIGRATE_PARAM_URI,              VIR_TYPED_PARAM_STRING, \
    VIR_MIGRATE_PARAM_DEST_NAME,        VIR_TYPED_PARAM_STRING, \
2848
    VIR_MIGRATE_PARAM_BANDWIDTH,        VIR_TYPED_PARAM_ULLONG, \
2849 2850
    NULL

2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
static char *
vzDomainMigrateBeginStep(virDomainObjPtr dom,
                         vzDriverPtr driver,
                         virTypedParameterPtr params,
                         int nparams,
                         char **cookieout,
                         int *cookieoutlen)
{
    /* we can't do this check via VZ_MIGRATION_PARAMETERS as on preparation
     * step domain xml will be passed via this parameter and it is a common
     * style to use single allowed parameter list definition in all steps */
    if (virTypedParamsGet(params, nparams, VIR_MIGRATE_PARAM_DEST_XML)) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("Changing destination XML is not supported"));
        return NULL;
    }

    /* session uuid, domain uuid and domain name are for backward compat */
    if (vzBakeCookie(driver, dom, cookieout, cookieoutlen,
                     VZ_MIGRATION_COOKIE_SESSION_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_NAME) < 0)
        return NULL;

    return virDomainDefFormat(dom->def, driver->caps,
                              VIR_DOMAIN_XML_MIGRATABLE);
}

2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
static char *
vzDomainMigrateBegin3Params(virDomainPtr domain,
                            virTypedParameterPtr params,
                            int nparams,
                            char **cookieout,
                            int *cookieoutlen,
                            unsigned int flags)
{
    char *xml = NULL;
    virDomainObjPtr dom = NULL;
    vzConnPtr privconn = domain->conn->privateData;
2890
    unsigned long long bandwidth = 0;
2891 2892 2893 2894 2895 2896

    virCheckFlags(VZ_MIGRATION_FLAGS, NULL);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
    if (virTypedParamsGetULLong(params, nparams, VIR_MIGRATE_PARAM_BANDWIDTH,
                                &bandwidth) < 0)
        goto cleanup;

    if (bandwidth > 0) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("Bandwidth rate limiting is not supported"));
        goto cleanup;
    }

2907 2908 2909
    if (!(dom = vzDomObjFromDomain(domain)))
        goto cleanup;

2910 2911 2912
    if (virDomainMigrateBegin3ParamsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2913 2914
    xml = vzDomainMigrateBeginStep(dom, privconn->driver, params, nparams,
                                   cookieout, cookieoutlen);
2915 2916 2917

 cleanup:

J
John Ferlan 已提交
2918
    virDomainObjEndAPI(&dom);
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
    return xml;
}

static char*
vzMigrationCreateURI(void)
{
    char *hostname = NULL;
    char *uri = NULL;

    if (!(hostname = virGetHostname()))
        goto cleanup;

    if (STRPREFIX(hostname, "localhost")) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("hostname on destination resolved to localhost,"
                         " but migration requires an FQDN"));
        goto cleanup;
    }

2938
    uri = g_strdup_printf("vzmigr://%s", hostname);
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948

 cleanup:
    VIR_FREE(hostname);
    return uri;
}

static int
vzDomainMigratePrepare3Params(virConnectPtr conn,
                              virTypedParameterPtr params,
                              int nparams,
J
Ján Tomko 已提交
2949 2950
                              const char *cookiein G_GNUC_UNUSED,
                              int cookieinlen G_GNUC_UNUSED,
2951 2952 2953 2954 2955 2956
                              char **cookieout,
                              int *cookieoutlen,
                              char **uri_out,
                              unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
2957
    vzDriverPtr driver = privconn->driver;
2958 2959
    const char *miguri = NULL;
    const char *dname = NULL;
2960 2961
    const char *dom_xml = NULL;
    virDomainDefPtr def = NULL;
2962 2963 2964 2965 2966 2967 2968 2969 2970
    int ret = -1;

    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI, &miguri) < 0 ||
2971 2972
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_XML, &dom_xml) < 0 ||
2973 2974 2975 2976 2977 2978 2979 2980 2981
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &dname) < 0)
        goto cleanup;

    /* We must set uri_out if miguri is not set. This is direct
     * managed migration requirement */
    if (!miguri && !(*uri_out = vzMigrationCreateURI()))
        goto cleanup;

2982 2983 2984 2985 2986 2987
    /* domain uuid and domain name are for backward compat */
    if (vzBakeCookie(privconn->driver, NULL,
                     cookieout, cookieoutlen,
                     VZ_MIGRATION_COOKIE_SESSION_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_NAME) < 0)
2988 2989
        goto cleanup;

2990
    if (!(def = virDomainDefParseString(dom_xml, driver->caps, driver->xmlopt,
2991
                                        NULL,
2992 2993 2994 2995 2996
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
        goto cleanup;

    if (dname) {
        VIR_FREE(def->name);
2997
        def->name = g_strdup(dname);
2998 2999 3000 3001 3002
    }

    if (virDomainMigratePrepare3ParamsEnsureACL(conn, def) < 0)
        goto cleanup;

3003 3004 3005
    ret = 0;

 cleanup:
3006
    virDomainDefFree(def);
3007 3008 3009 3010
    return ret;
}

static int
J
Ján Tomko 已提交
3011
vzConnectSupportsFeature(virConnectPtr conn G_GNUC_UNUSED, int feature)
3012
{
3013 3014 3015
    if (virConnectSupportsFeatureEnsureACL(conn) < 0)
        return -1;

3016
    switch ((virDrvFeature) feature) {
3017
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
3018
    case VIR_DRV_FEATURE_MIGRATION_P2P:
3019
        return 1;
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032
    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_V1:
    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:
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074
    default:
        return 0;
    }
}

static virURIPtr
vzParseVzURI(const char *uri_str)
{
    virURIPtr uri = NULL;

    if (!(uri = virURIParse(uri_str)))
        goto error;

    if (!uri->scheme || !uri->server) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("scheme and host are mandatory vz migration URI: %s"),
                       uri_str);
        goto error;
    }

    if (uri->user || uri->path || uri->query || uri->fragment) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("only scheme, host and port are supported in "
                         "vz migration URI: %s"), uri_str);
        goto error;
    }

    if (STRNEQ(uri->scheme, "vzmigr")) {
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
                       _("unsupported scheme %s in migration URI %s"),
                       uri->scheme, uri_str);
        goto error;
    }

    return uri;

 error:
    virURIFree(uri);
    return NULL;
}

static int
3075 3076
vzDomainMigratePerformStep(virDomainObjPtr dom,
                           vzDriverPtr driver,
3077 3078 3079 3080 3081
                           virTypedParameterPtr params,
                           int nparams,
                           const char *cookiein,
                           int cookieinlen,
                           unsigned int flags)
3082 3083
{
    int ret = -1;
3084
    vzDomObjPtr privdom = dom->privateData;
3085 3086 3087 3088
    virURIPtr vzuri = NULL;
    const char *miguri = NULL;
    const char *dname = NULL;
    vzMigrationCookiePtr mig = NULL;
3089
    bool job = false;
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI, &miguri) < 0 ||
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &dname) < 0)
        goto cleanup;

    if (!miguri) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("migrate uri is not set"));
        goto cleanup;
    }

3103 3104
    if (!(mig = vzEatCookie(cookiein, cookieinlen,
                            VZ_MIGRATION_COOKIE_SESSION_UUID)))
3105 3106
        goto cleanup;

3107 3108 3109
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;
3110
    privdom->job.hasProgress = true;
3111

3112 3113 3114
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

3115 3116 3117 3118 3119 3120
    if (!(vzuri = vzParseVzURI(miguri)))
        goto cleanup;

    if (prlsdkMigrate(dom, vzuri, mig->session_uuid, dname, flags) < 0)
        goto cleanup;

3121
    virDomainObjListRemove(driver->domains, dom);
3122 3123 3124 3125

    ret = 0;

 cleanup:
3126 3127
    if (job)
        vzDomainObjEndJob(dom);
3128 3129 3130 3131 3132 3133
    virURIFree(vzuri);
    vzMigrationCookieFree(mig);

    return ret;
}

3134
static int
3135 3136
vzDomainMigratePerformP2P(virDomainObjPtr dom,
                          vzDriverPtr driver,
3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
                          const char *dconnuri,
                          virTypedParameterPtr orig_params,
                          int nparams,
                          unsigned int flags)
{
    virDomainPtr ddomain = NULL;
    char *uri = NULL;
    char *cookiein = NULL;
    char *cookieout = NULL;
    char *dom_xml = NULL;
    int cookieinlen = 0;
    int cookieoutlen = 0;
    virErrorPtr orig_err = NULL;
    int cancelled = 1;
    virConnectPtr dconn = NULL;
    virTypedParameterPtr params = NULL;
    int ret = -1;
P
Pavel Glushchak 已提交
3154
    int maxparams = nparams;
3155 3156 3157 3158 3159 3160 3161

    if (virTypedParamsCopy(&params, orig_params, nparams) < 0)
        return -1;

    if (!(dconn = virConnectOpen(dconnuri)))
        goto done;

3162 3163
    if (!(dom_xml = vzDomainMigrateBeginStep(dom, driver, params, nparams,
                                             &cookieout, &cookieoutlen)))
3164 3165
        goto done;

P
Pavel Glushchak 已提交
3166 3167 3168 3169
    if (virTypedParamsAddString(&params, &nparams, &maxparams,
                                VIR_MIGRATE_PARAM_DEST_XML, dom_xml) < 0)
        goto done;

3170
    cookiein = g_steal_pointer(&cookieout);
3171 3172
    cookieinlen = cookieoutlen;
    cookieoutlen = 0;
3173 3174
    virObjectUnlock(dom);
    ret = dconn->driver->domainMigratePrepare3Params
3175
            (dconn, params, nparams, cookiein, cookieinlen,
3176 3177 3178
             &cookieout, &cookieoutlen, &uri, flags);
    virObjectLock(dom);
    if (ret < 0)
3179
        goto done;
3180
    ret = -1;
3181 3182 3183 3184 3185 3186

    /* preparation step was successful, thus on any error we must perform
     * finish step to finalize migration on target
     */
    if (uri && virTypedParamsReplaceString(&params, &nparams,
                                           VIR_MIGRATE_PARAM_URI, uri) < 0) {
3187
        virErrorPreserveLast(&orig_err);
3188 3189 3190 3191
        goto finish;
    }

    VIR_FREE(cookiein);
3192
    cookiein = g_steal_pointer(&cookieout);
3193 3194
    cookieinlen = cookieoutlen;
    cookieoutlen = 0;
3195
    if (vzDomainMigratePerformStep(dom, driver, params, nparams, cookiein,
3196
                                   cookieinlen, flags) < 0) {
3197
        virErrorPreserveLast(&orig_err);
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
        goto finish;
    }

    cancelled = 0;

 finish:
    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, NULL) <= 0 &&
        virTypedParamsReplaceString(&params, &nparams,
                                    VIR_MIGRATE_PARAM_DEST_NAME,
3208
                                    dom->def->name) < 0)
3209 3210
        goto done;

3211
    virObjectUnlock(dom);
3212 3213 3214
    ddomain = dconn->driver->domainMigrateFinish3Params(dconn, params, nparams,
                                                        NULL, 0, NULL, NULL,
                                                        flags, cancelled);
3215
    virObjectLock(dom);
3216 3217 3218 3219 3220 3221 3222
    if (ddomain)
        ret = 0;
    virObjectUnref(ddomain);

    /* confirm step is NOOP thus no need to call it */

 done:
3223
    virErrorRestore(&orig_err);
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
    VIR_FREE(dom_xml);
    VIR_FREE(uri);
    VIR_FREE(cookiein);
    VIR_FREE(cookieout);
    virTypedParamsFree(params, nparams);
    virObjectUnref(dconn);
    return ret;
}

static int
vzDomainMigratePerform3Params(virDomainPtr domain,
                              const char *dconnuri,
                              virTypedParameterPtr params,
                              int nparams,
                              const char *cookiein,
                              int cookieinlen,
J
Ján Tomko 已提交
3240 3241
                              char **cookieout G_GNUC_UNUSED,
                              int *cookieoutlen G_GNUC_UNUSED,
3242 3243
                              unsigned int flags)
{
3244
    int ret = -1;
3245 3246 3247
    virDomainObjPtr dom;
    vzConnPtr privconn = domain->conn->privateData;

3248 3249 3250 3251 3252
    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        return -1;

J
John Ferlan 已提交
3253
    if (!(dom = vzDomObjFromDomain(domain)))
3254 3255
        return -1;

3256 3257 3258
    if (virDomainMigratePerform3ParamsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3259
    if (flags & VIR_MIGRATE_PEER2PEER)
3260 3261
        ret = vzDomainMigratePerformP2P(dom, privconn->driver, dconnuri,
                                        params, nparams, flags);
3262
    else
3263 3264
        ret = vzDomainMigratePerformStep(dom, privconn->driver, params, nparams,
                                         cookiein, cookieinlen, flags);
3265

3266
 cleanup:
3267 3268 3269
    virDomainObjEndAPI(&dom);

    return ret;
3270 3271
}

3272 3273 3274 3275
static virDomainPtr
vzDomainMigrateFinish3Params(virConnectPtr dconn,
                             virTypedParameterPtr params,
                             int nparams,
J
Ján Tomko 已提交
3276 3277 3278 3279
                             const char *cookiein G_GNUC_UNUSED,
                             int cookieinlen G_GNUC_UNUSED,
                             char **cookieout G_GNUC_UNUSED,
                             int *cookieoutlen G_GNUC_UNUSED,
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
                             unsigned int flags,
                             int cancelled)
{
    virDomainObjPtr dom = NULL;
    virDomainPtr domain = NULL;
    vzConnPtr privconn = dconn->privateData;
    vzDriverPtr driver = privconn->driver;
    const char *name = NULL;

    virCheckFlags(VZ_MIGRATION_FLAGS, NULL);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
3292 3293 3294 3295
        return NULL;

    if (cancelled)
        return NULL;
3296 3297 3298

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &name) < 0)
3299
        return NULL;
3300 3301


3302
    if (!(dom = prlsdkAddDomainByName(driver, name)))
3303 3304
        goto cleanup;

3305 3306 3307 3308 3309
    /* At first glace at may look strange that we add domain and
     * then check ACL but we touch only cache and not real system state */
    if (virDomainMigrateFinish3ParamsEnsureACL(dconn, dom->def) < 0)
        goto cleanup;

3310
    domain = virGetDomain(dconn, dom->def->name, dom->def->uuid, dom->def->id);
3311 3312 3313 3314

 cleanup:
    /* In this situation we have to restore domain on source. But the migration
     * is already finished. */
3315
    if (!domain)
3316
        VIR_WARN("Can't provide domain '%s' after successful migration.", name);
3317
    virDomainObjEndAPI(&dom);
3318 3319 3320 3321
    return domain;
}

static int
J
Ján Tomko 已提交
3322
vzDomainMigrateConfirm3Params(virDomainPtr domain G_GNUC_UNUSED,
3323 3324
                              virTypedParameterPtr params,
                              int nparams,
J
Ján Tomko 已提交
3325 3326
                              const char *cookiein G_GNUC_UNUSED,
                              int cookieinlen G_GNUC_UNUSED,
3327
                              unsigned int flags,
J
Ján Tomko 已提交
3328
                              int cancelled G_GNUC_UNUSED)
3329 3330 3331 3332 3333 3334 3335 3336 3337
{
    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        return -1;

    return 0;
}

3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
static int
vzDomainGetJobInfoImpl(virDomainObjPtr dom, virDomainJobInfoPtr info)
{
    vzDomObjPtr privdom = dom->privateData;
    vzDomainJobObjPtr job = &privdom->job;

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

    if (!job->active || !job->hasProgress)
        return 0;

    if (vzDomainJobUpdateTime(job) < 0)
        return -1;

    info->type = VIR_DOMAIN_JOB_UNBOUNDED;
    info->dataTotal = 100;
    info->dataProcessed = job->progress;
    info->dataRemaining = 100 - job->progress;
    info->timeElapsed = job->elapsed;

    return 0;
}

static int
vzDomainGetJobInfo(virDomainPtr domain, virDomainJobInfoPtr info)
{
    virDomainObjPtr dom;
3365
    int ret = -1;
3366 3367 3368 3369

    if (!(dom = vzDomObjFromDomain(domain)))
        return -1;

3370 3371 3372
    if (virDomainGetJobInfoEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3373 3374
    ret = vzDomainGetJobInfoImpl(dom, info);

3375
 cleanup:
J
John Ferlan 已提交
3376
    virDomainObjEndAPI(&dom);
3377 3378 3379
    return ret;
}

3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
static int
vzDomainJobInfoToParams(virDomainJobInfoPtr info,
                        int *type,
                        virTypedParameterPtr *params,
                        int *nparams)
{
    virTypedParameterPtr par = NULL;
    int maxpar = 0;
    int npar = 0;

    if (virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_TIME_ELAPSED,
                                info->timeElapsed) < 0 ||
        virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_DATA_TOTAL,
                                info->dataTotal) < 0 ||
        virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_DATA_PROCESSED,
                                info->dataProcessed) < 0 ||
        virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_DATA_REMAINING,
                                info->dataRemaining) < 0)
        goto error;


    *type = info->type;
    *params = par;
    *nparams = npar;
    return 0;

 error:
    virTypedParamsFree(par, npar);
    return -1;
}

static int
vzDomainGetJobStats(virDomainPtr domain,
                    int *type,
                    virTypedParameterPtr *params,
                    int *nparams,
                    unsigned int flags)
{
    virDomainJobInfo info;
    virDomainObjPtr dom;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(dom = vzDomObjFromDomain(domain)))
        return -1;

3431 3432 3433
    if (virDomainGetJobStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447
    if (vzDomainGetJobInfoImpl(dom, &info) < 0)
        goto cleanup;

    if (info.type == VIR_DOMAIN_JOB_NONE) {
        *type = VIR_DOMAIN_JOB_NONE;
        *params = NULL;
        *nparams = 0;
        ret = 0;
        goto cleanup;
    }

    ret = vzDomainJobInfoToParams(&info, type, params, nparams);

 cleanup:
J
John Ferlan 已提交
3448
    virDomainObjEndAPI(&dom);
3449 3450 3451 3452

    return ret;
}

3453 3454 3455
#define VZ_ADD_STAT_PARAM_UUL(group, field, counter) \
do { \
    if (stat.field != -1) { \
3456 3457
        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, \
                   group ".%zu." counter, i); \
3458 3459 3460 3461 3462 3463 3464
        if (virTypedParamsAddULLong(&record->params, \
                                    &record->nparams, \
                                    maxparams, \
                                    param_name, \
                                    stat.field) < 0) \
            return -1; \
    } \
3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492
} while (0)

static int
vzDomainGetBlockStats(virDomainObjPtr dom,
                      virDomainStatsRecordPtr record,
                      int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    size_t i;
    char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "block.count",
                              dom->def->ndisks) < 0)
        return -1;

    for (i = 0; i < dom->def->ndisks; i++) {
        virDomainBlockStatsStruct stat;
        virDomainDiskDefPtr disk = dom->def->disks[i];

        if (prlsdkGetBlockStats(privdom->stats,
                                disk,
                                &stat,
                                IS_CT(dom->def)) < 0)
            return -1;

3493 3494
        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
                   "block.%zu.name", i);
3495 3496 3497 3498 3499 3500 3501 3502
        if (virTypedParamsAddString(&record->params,
                                    &record->nparams,
                                    maxparams,
                                    param_name,
                                    disk->dst) < 0)
            return -1;

        if (virStorageSourceIsLocalStorage(disk->src) && disk->src->path) {
3503 3504
            g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
                       "block.%zu.path", i);
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518
            if (virTypedParamsAddString(&record->params,
                                        &record->nparams,
                                        maxparams,
                                        param_name,
                                        disk->src->path) < 0)
                return -1;
        }

        VZ_ADD_STAT_PARAM_UUL("block", rd_req, "rd.reqs");
        VZ_ADD_STAT_PARAM_UUL("block", rd_bytes, "rd.bytes");
        VZ_ADD_STAT_PARAM_UUL("block", wr_req, "wr.reqs");
        VZ_ADD_STAT_PARAM_UUL("block", wr_bytes, "wr.bytes");

        if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
3519 3520
            g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
                       "block.%zu.capacity", i);
3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
            if (virTypedParamsAddULLong(&record->params,
                                        &record->nparams,
                                        maxparams,
                                        param_name,
                                        disk->src->capacity) < 0)
                return -1;
        }

    }

    return 0;
}

3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557
static int
vzDomainGetNetStats(virDomainObjPtr dom,
                    virDomainStatsRecordPtr record,
                    int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    size_t i;
    char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "net.count",
                              dom->def->nnets) < 0)
        return -1;

    for (i = 0; i < dom->def->nnets; i++) {
        virDomainInterfaceStatsStruct stat;
        virDomainNetDefPtr net = dom->def->nets[i];

        if (prlsdkGetNetStats(privdom->stats, privdom->sdkdom, net->ifname,
                              &stat) < 0)
            return -1;

3558
        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "net.%zu.name", i);
3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
        if (virTypedParamsAddString(&record->params,
                                    &record->nparams,
                                    maxparams,
                                    param_name,
                                    net->ifname) < 0)
            return -1;

        VZ_ADD_STAT_PARAM_UUL("net", rx_bytes, "rx.bytes");
        VZ_ADD_STAT_PARAM_UUL("net", rx_packets, "rx.pkts");
        VZ_ADD_STAT_PARAM_UUL("net", tx_bytes, "tx.bytes");
        VZ_ADD_STAT_PARAM_UUL("net", tx_packets, "tx.pkts");
    }

    return 0;
}

3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
static int
vzDomainGetVCPUStats(virDomainObjPtr dom,
                     virDomainStatsRecordPtr record,
                     int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    size_t i;
    char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "vcpu.current",
                              virDomainDefGetVcpus(dom->def)) < 0)
        return -1;

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "vcpu.maximum",
                              virDomainDefGetVcpusMax(dom->def)) < 0)
        return -1;

    for (i = 0; i < virDomainDefGetVcpusMax(dom->def); i++) {
        int state = dom->def->vcpus[i]->online ? VIR_VCPU_RUNNING :
                                                 VIR_VCPU_OFFLINE;
        unsigned long long time;

3603
        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "vcpu.%zu.state", i);
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613
        if (virTypedParamsAddInt(&record->params,
                                 &record->nparams,
                                 maxparams,
                                 param_name,
                                 state) < 0)
            return -1;

        if (prlsdkGetVcpuStats(privdom->stats, i, &time) < 0)
            return -1;

3614
        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "vcpu.%zu.time", i);
3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
        if (virTypedParamsAddULLong(&record->params,
                                    &record->nparams,
                                    maxparams,
                                    param_name,
                                    time) < 0)
            return -1;
    }

    return 0;
}

3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
static int
vzDomainGetBalloonStats(virDomainObjPtr dom,
                        virDomainStatsRecordPtr record,
                        int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    virDomainMemoryStatStruct stats[VIR_DOMAIN_MEMORY_STAT_NR];
    size_t i;
    int n;

    if (virTypedParamsAddULLong(&record->params,
                                &record->nparams,
                                maxparams,
                                "balloon.maximum",
                                virDomainDefGetMemoryTotal(dom->def)) < 0)
        return -1;

    if (virTypedParamsAddULLong(&record->params,
                                &record->nparams,
                                maxparams,
                                "balloon.current",
                                virDomainDefGetMemoryTotal(dom->def)) < 0)
        return -1;

    n = prlsdkGetMemoryStats(privdom->stats, stats, VIR_DOMAIN_MEMORY_STAT_NR);
    if (n < 0)
        return -1;

3654 3655 3656 3657 3658 3659 3660
#define STORE_MEM_RECORD(TAG, NAME) \
    if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_ ##TAG) \
        if (virTypedParamsAddULLong(&record->params, \
                                    &record->nparams, \
                                    maxparams, \
                                    "balloon." NAME, \
                                    stats[i].val) < 0) \
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676
            return -1;

    for (i = 0; i < n; i++) {
        STORE_MEM_RECORD(SWAP_IN, "swap_in")
        STORE_MEM_RECORD(SWAP_OUT, "swap_out")
        STORE_MEM_RECORD(MAJOR_FAULT, "major_fault")
        STORE_MEM_RECORD(MINOR_FAULT, "minor_fault")
        STORE_MEM_RECORD(AVAILABLE, "available")
        STORE_MEM_RECORD(UNUSED, "unused")
    }

#undef STORE_MEM_RECORD

    return 0;
}

3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
static int
vzDomainGetStateStats(virDomainObjPtr dom,
                      virDomainStatsRecordPtr record,
                      int *maxparams)
{
    if (virTypedParamsAddInt(&record->params,
                             &record->nparams,
                             maxparams,
                             "state.state",
                             dom->state.state) < 0)
        return -1;

    if (virTypedParamsAddInt(&record->params,
                             &record->nparams,
                             maxparams,
                             "state.reason",
                             dom->state.reason) < 0)
        return -1;

    return 0;
}

3699 3700 3701 3702 3703 3704 3705 3706 3707 3708
static virDomainStatsRecordPtr
vzDomainGetAllStats(virConnectPtr conn,
                    virDomainObjPtr dom)
{
    virDomainStatsRecordPtr stat;
    int maxparams = 0;

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

3709 3710 3711
    if (vzDomainGetStateStats(dom, stat, &maxparams) < 0)
        goto error;

3712 3713
    if (vzDomainGetBlockStats(dom, stat, &maxparams) < 0)
        goto error;
3714 3715 3716

    if (vzDomainGetNetStats(dom, stat, &maxparams) < 0)
        goto error;
3717 3718 3719

    if (vzDomainGetVCPUStats(dom, stat, &maxparams) < 0)
        goto error;
3720 3721 3722

    if (vzDomainGetBalloonStats(dom, stat, &maxparams) < 0)
        goto error;
3723

3724
    if (!(stat->dom = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id)))
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820
        goto error;

    return stat;

 error:
    virTypedParamsFree(stat->params, stat->nparams);
    virObjectUnref(stat->dom);
    VIR_FREE(stat);
    return NULL;
}

static int
vzConnectGetAllDomainStats(virConnectPtr conn,
                           virDomainPtr *domains,
                           unsigned int ndomains,
                           unsigned int stats,
                           virDomainStatsRecordPtr **retStats,
                           unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
    vzDriverPtr driver = privconn->driver;
    unsigned int lflags = flags & (VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE |
                                   VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT |
                                   VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE);
    unsigned int supported = VIR_DOMAIN_STATS_STATE |
                             VIR_DOMAIN_STATS_VCPU |
                             VIR_DOMAIN_STATS_INTERFACE |
                             VIR_DOMAIN_STATS_BALLOON |
                             VIR_DOMAIN_STATS_BLOCK;
    virDomainObjPtr *doms = NULL;
    size_t ndoms;
    virDomainStatsRecordPtr *tmpstats = NULL;
    int nstats = 0;
    int ret = -1;
    size_t i;

    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE |
                  VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT |
                  VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE |
                  VIR_CONNECT_GET_ALL_DOMAINS_STATS_ENFORCE_STATS, -1);

    if (virConnectGetAllDomainStatsEnsureACL(conn) < 0)
        return -1;

    if (!stats) {
        stats = supported;
    } else if ((flags & VIR_CONNECT_GET_ALL_DOMAINS_STATS_ENFORCE_STATS) &&
               (stats & ~supported)) {
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
                       _("Stats types bits 0x%x are not supported by this daemon"),
                       stats & ~supported);
        return -1;
    }

    if (ndomains) {
        if (virDomainObjListConvert(driver->domains, conn, domains, ndomains, &doms,
                                    &ndoms, virConnectGetAllDomainStatsCheckACL,
                                    lflags, true) < 0)
            return -1;
    } else {
        if (virDomainObjListCollect(driver->domains, conn, &doms, &ndoms,
                                    virConnectGetAllDomainStatsCheckACL,
                                    lflags) < 0)
            return -1;
    }

    if (VIR_ALLOC_N(tmpstats, ndoms + 1) < 0)
        goto cleanup;

    for (i = 0; i < ndoms; i++) {
        virDomainStatsRecordPtr tmp;
        virDomainObjPtr dom = doms[i];

        virObjectLock(dom);
        tmp = vzDomainGetAllStats(conn, dom);
        virObjectUnlock(dom);

        if (!tmp)
            goto cleanup;

        tmpstats[nstats++] = tmp;
    }

    *retStats = tmpstats;
    tmpstats = NULL;
    ret = nstats;

 cleanup:
    virDomainStatsRecordListFree(tmpstats);
    virObjectListFreeCount(doms, ndoms);

    return ret;
}

#undef VZ_ADD_STAT_PARAM_UUL

3821 3822 3823 3824 3825 3826
static int
vzDomainAbortJob(virDomainPtr domain)
{
    virDomainObjPtr dom;
    int ret = -1;

J
John Ferlan 已提交
3827
    if (!(dom = vzDomObjFromDomain(domain)))
3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840
        return -1;

    if (virDomainAbortJobEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = prlsdkCancelJob(dom);

 cleanup:
    virDomainObjEndAPI(&dom);

    return ret;
}

3841 3842 3843 3844 3845 3846 3847 3848 3849
static int
vzDomainReset(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
    bool job = false;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
3850
    if (!(dom = vzDomObjFromDomain(domain)))
3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871
        return -1;

    if (virDomainResetEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    ret = prlsdkReset(dom);

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
}

3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
static int vzDomainSetVcpusFlags(virDomainPtr domain, unsigned int nvcpus,
                                 unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
    bool job = false;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
3882
    if (!(dom = vzDomObjFromDomain(domain)))
3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911
        goto cleanup;

    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
        goto cleanup;

    if (virDomainSetVcpusFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    ret = prlsdkSetCpuCount(dom, nvcpus);

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
}

static int vzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
{
    return vzDomainSetVcpusFlags(dom, nvcpus,
                                 VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG);
}
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924
static int
vzDomainBlockResize(virDomainPtr domain,
                    const char *path,
                    unsigned long long size,
                    unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    virDomainDiskDefPtr disk = NULL;
    int ret = -1;
    bool job = false;

    virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES, -1);

J
John Ferlan 已提交
3925
    if (!(dom = vzDomObjFromDomain(domain)))
3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948
        goto cleanup;

    if (virDomainBlockResizeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    if (path[0] == '\0') {
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("empty path"));
        goto cleanup;
    }

    /* sdk wants Mb */
    if (flags & VIR_DOMAIN_BLOCK_RESIZE_BYTES)
        size /= 1024;
    size /= 1024;

    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

3949
    if (virDomainObjCheckActive(dom) < 0)
3950 3951
        goto cleanup;

3952 3953 3954 3955 3956 3957
    if (!(disk = virDomainDiskByName(dom->def, path, false))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
        goto cleanup;
    }

3958 3959 3960 3961 3962 3963 3964 3965
    ret = prlsdkResizeImage(dom, disk, size);

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
}
3966

3967
static virHypervisorDriver vzHypervisorDriver = {
3968
    .name = "vz",
3969 3970 3971 3972
    .connectOpen = vzConnectOpen,            /* 0.10.0 */
    .connectClose = vzConnectClose,          /* 0.10.0 */
    .connectGetVersion = vzConnectGetVersion,   /* 0.10.0 */
    .connectGetHostname = vzConnectGetHostname,      /* 0.10.0 */
3973
    .connectGetSysinfo = vzConnectGetSysinfo, /* 1.3.4 */
3974
    .connectGetMaxVcpus = vzConnectGetMaxVcpus, /* 1.2.21 */
3975
    .nodeGetInfo = vzNodeGetInfo,      /* 0.10.0 */
3976 3977 3978 3979
    .nodeGetCPUStats = vzNodeGetCPUStats,      /* 1.2.21 */
    .nodeGetMemoryStats = vzNodeGetMemoryStats, /* 1.2.21 */
    .nodeGetCellsFreeMemory = vzNodeGetCellsFreeMemory, /* 1.2.21 */
    .nodeGetFreeMemory = vzNodeGetFreeMemory, /* 1.2.21 */
3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
    .connectGetCapabilities = vzConnectGetCapabilities,      /* 0.10.0 */
    .connectBaselineCPU = vzConnectBaselineCPU, /* 1.2.6 */
    .connectListDomains = vzConnectListDomains,      /* 0.10.0 */
    .connectNumOfDomains = vzConnectNumOfDomains,    /* 0.10.0 */
    .connectListDefinedDomains = vzConnectListDefinedDomains,        /* 0.10.0 */
    .connectNumOfDefinedDomains = vzConnectNumOfDefinedDomains,      /* 0.10.0 */
    .connectListAllDomains = vzConnectListAllDomains, /* 0.10.0 */
    .domainLookupByID = vzDomainLookupByID,    /* 0.10.0 */
    .domainLookupByUUID = vzDomainLookupByUUID,        /* 0.10.0 */
    .domainLookupByName = vzDomainLookupByName,        /* 0.10.0 */
    .domainGetOSType = vzDomainGetOSType,    /* 0.10.0 */
    .domainGetInfo = vzDomainGetInfo,  /* 0.10.0 */
    .domainGetState = vzDomainGetState,        /* 0.10.0 */
    .domainGetXMLDesc = vzDomainGetXMLDesc,    /* 0.10.0 */
    .domainIsPersistent = vzDomainIsPersistent,        /* 0.10.0 */
    .domainGetAutostart = vzDomainGetAutostart,        /* 0.10.0 */
    .domainGetVcpus = vzDomainGetVcpus, /* 1.2.6 */
    .domainSuspend = vzDomainSuspend,    /* 0.10.0 */
    .domainResume = vzDomainResume,    /* 0.10.0 */
    .domainDestroy = vzDomainDestroy,  /* 0.10.0 */
4000
    .domainDestroyFlags = vzDomainDestroyFlags,  /* 2.2.0 */
4001
    .domainShutdown = vzDomainShutdown, /* 0.10.0 */
4002
    .domainShutdownFlags = vzDomainShutdownFlags, /* 2.2.0 */
4003 4004
    .domainCreate = vzDomainCreate,    /* 0.10.0 */
    .domainCreateWithFlags = vzDomainCreateWithFlags, /* 1.2.10 */
P
Pavel Hrdina 已提交
4005
    .domainReboot = vzDomainReboot, /* 1.3.0 */
4006 4007 4008 4009 4010 4011 4012 4013 4014
    .domainDefineXML = vzDomainDefineXML,      /* 0.10.0 */
    .domainDefineXMLFlags = vzDomainDefineXMLFlags, /* 1.2.12 */
    .domainUndefine = vzDomainUndefine, /* 1.2.10 */
    .domainUndefineFlags = vzDomainUndefineFlags, /* 1.2.10 */
    .domainAttachDevice = vzDomainAttachDevice, /* 1.2.15 */
    .domainAttachDeviceFlags = vzDomainAttachDeviceFlags, /* 1.2.15 */
    .domainDetachDevice = vzDomainDetachDevice, /* 1.2.15 */
    .domainDetachDeviceFlags = vzDomainDetachDeviceFlags, /* 1.2.15 */
    .domainIsActive = vzDomainIsActive, /* 1.2.10 */
4015
    .domainIsUpdated = vzDomainIsUpdated,     /* 1.2.21 */
4016 4017
    .domainSetVcpus = vzDomainSetVcpus, /* 3.3.0 */
    .domainSetVcpusFlags = vzDomainSetVcpusFlags, /* 3.3.0 */
4018 4019
    .domainGetVcpusFlags = vzDomainGetVcpusFlags, /* 1.2.21 */
    .domainGetMaxVcpus = vzDomainGetMaxVcpus, /* 1.2.21 */
4020
    .domainSetUserPassword = vzDomainSetUserPassword, /* 2.0.0 */
4021 4022 4023 4024 4025 4026 4027 4028 4029 4030
    .connectDomainEventRegisterAny = vzConnectDomainEventRegisterAny, /* 1.2.10 */
    .connectDomainEventDeregisterAny = vzConnectDomainEventDeregisterAny, /* 1.2.10 */
    .nodeGetCPUMap = vzNodeGetCPUMap, /* 1.2.8 */
    .connectIsEncrypted = vzConnectIsEncrypted, /* 1.2.5 */
    .connectIsSecure = vzConnectIsSecure, /* 1.2.5 */
    .connectIsAlive = vzConnectIsAlive, /* 1.2.5 */
    .domainHasManagedSaveImage = vzDomainHasManagedSaveImage, /* 1.2.13 */
    .domainManagedSave = vzDomainManagedSave, /* 1.2.14 */
    .domainManagedSaveRemove = vzDomainManagedSaveRemove, /* 1.2.14 */
    .domainGetMaxMemory = vzDomainGetMaxMemory, /* 1.2.15 */
4031 4032 4033 4034
    .domainBlockStats = vzDomainBlockStats, /* 1.2.17 */
    .domainBlockStatsFlags = vzDomainBlockStatsFlags, /* 1.2.17 */
    .domainInterfaceStats = vzDomainInterfaceStats, /* 1.2.17 */
    .domainMemoryStats = vzDomainMemoryStats, /* 1.2.17 */
4035 4036
    .connectRegisterCloseCallback = vzConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = vzConnectUnregisterCloseCallback, /* 1.3.2 */
4037 4038
    .domainSetMemoryFlags = vzDomainSetMemoryFlags, /* 1.3.4 */
    .domainSetMemory = vzDomainSetMemory, /* 1.3.4 */
4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054
    .domainSnapshotNum = vzDomainSnapshotNum, /* 1.3.5 */
    .domainSnapshotListNames = vzDomainSnapshotListNames, /* 1.3.5 */
    .domainListAllSnapshots = vzDomainListAllSnapshots, /* 1.3.5 */
    .domainSnapshotGetXMLDesc = vzDomainSnapshotGetXMLDesc, /* 1.3.5 */
    .domainSnapshotNumChildren = vzDomainSnapshotNumChildren, /* 1.3.5 */
    .domainSnapshotListChildrenNames = vzDomainSnapshotListChildrenNames, /* 1.3.5 */
    .domainSnapshotListAllChildren = vzDomainSnapshotListAllChildren, /* 1.3.5 */
    .domainSnapshotLookupByName = vzDomainSnapshotLookupByName, /* 1.3.5 */
    .domainHasCurrentSnapshot = vzDomainHasCurrentSnapshot, /* 1.3.5 */
    .domainSnapshotGetParent = vzDomainSnapshotGetParent, /* 1.3.5 */
    .domainSnapshotCurrent = vzDomainSnapshotCurrent, /* 1.3.5 */
    .domainSnapshotIsCurrent = vzDomainSnapshotIsCurrent, /* 1.3.5 */
    .domainSnapshotHasMetadata = vzDomainSnapshotHasMetadata, /* 1.3.5 */
    .domainSnapshotCreateXML = vzDomainSnapshotCreateXML, /* 1.3.5 */
    .domainSnapshotDelete = vzDomainSnapshotDelete, /* 1.3.5 */
    .domainRevertToSnapshot = vzDomainRevertToSnapshot, /* 1.3.5 */
4055 4056 4057 4058 4059 4060
    .connectSupportsFeature = vzConnectSupportsFeature, /* 1.3.5 */
    .domainMigrateBegin3Params = vzDomainMigrateBegin3Params, /* 1.3.5 */
    .domainMigratePrepare3Params = vzDomainMigratePrepare3Params, /* 1.3.5 */
    .domainMigratePerform3Params = vzDomainMigratePerform3Params, /* 1.3.5 */
    .domainMigrateFinish3Params = vzDomainMigrateFinish3Params, /* 1.3.5 */
    .domainMigrateConfirm3Params = vzDomainMigrateConfirm3Params, /* 1.3.5 */
N
Nikolay Shirokovskiy 已提交
4061
    .domainUpdateDeviceFlags = vzDomainUpdateDeviceFlags, /* 2.0.0 */
4062
    .domainGetJobInfo = vzDomainGetJobInfo, /* 2.2.0 */
4063
    .domainGetJobStats = vzDomainGetJobStats, /* 2.2.0 */
4064
    .connectGetAllDomainStats = vzConnectGetAllDomainStats, /* 3.1.0 */
4065
    .domainAbortJob = vzDomainAbortJob, /* 3.1.0 */
4066
    .domainReset = vzDomainReset, /* 3.1.0 */
4067
    .domainBlockResize = vzDomainBlockResize, /* 3.3.0 */
D
Dmitry Guryanov 已提交
4068 4069
};

4070
static virConnectDriver vzConnectDriver = {
4071
    .localOnly = true,
4072
    .uriSchemes = (const char *[]){ "vz", NULL },
4073
    .hypervisorDriver = &vzHypervisorDriver,
4074 4075
};

4076 4077 4078
static int
vzStateCleanup(void)
{
4079 4080 4081 4082 4083 4084 4085 4086
    if (vz_driver_privileged) {
        virObjectUnref(vz_driver);
        vz_driver = NULL;
        if (vz_driver_lock_fd != -1)
            virPidFileRelease(VZ_STATEDIR, "driver", vz_driver_lock_fd);
        virMutexDestroy(&vz_driver_lock);
        prlsdkDeinit();
    }
4087 4088 4089 4090
    return 0;
}

static int
4091
vzStateInitialize(bool privileged,
J
Ján Tomko 已提交
4092 4093
                  virStateInhibitCallback callback G_GNUC_UNUSED,
                  void *opaque G_GNUC_UNUSED)
4094
{
4095
    if (!privileged)
4096
        return VIR_DRV_STATE_INIT_SKIPPED;
4097 4098 4099 4100 4101 4102

    vz_driver_privileged = privileged;

    if (virFileMakePathWithMode(VZ_STATEDIR, S_IRWXU) < 0) {
        virReportSystemError(errno, _("cannot create state directory '%s'"),
                             VZ_STATEDIR);
4103
        return VIR_DRV_STATE_INIT_ERROR;
4104 4105 4106
    }

    if ((vz_driver_lock_fd =
4107
         virPidFileAcquire(VZ_STATEDIR, "driver", false, getpid())) < 0)
4108
        return VIR_DRV_STATE_INIT_ERROR;
4109

4110 4111
    if (prlsdkInit() < 0) {
        VIR_DEBUG("%s", _("Can't initialize Parallels SDK"));
4112
        return VIR_DRV_STATE_INIT_ERROR;
4113 4114
    }

4115
    if (virMutexInit(&vz_driver_lock) < 0)
4116 4117 4118 4119 4120
        goto error;

    /* Failing to create driver here is not fatal and only means
     * that next driver client will try once more when connecting */
    vz_driver = vzDriverObjNew();
4121
    return VIR_DRV_STATE_INIT_COMPLETE;
4122 4123 4124

 error:
    vzStateCleanup();
4125
    return VIR_DRV_STATE_INIT_ERROR;
4126 4127 4128 4129 4130 4131 4132 4133
}

static virStateDriver vzStateDriver = {
    .name = "vz",
    .stateInitialize = vzStateInitialize,
    .stateCleanup = vzStateCleanup,
};

4134
/* Parallels domain type backward compatibility*/
4135
static virHypervisorDriver parallelsHypervisorDriver;
N
Nikolay Shirokovskiy 已提交
4136 4137 4138 4139 4140
static virConnectDriver parallelsConnectDriver = {
    .localOnly = true,
    .uriSchemes = (const char *[]){ "parallels", NULL },
    .hypervisorDriver = &parallelsHypervisorDriver,
};
4141

D
Dmitry Guryanov 已提交
4142
/**
4143
 * vzRegister:
D
Dmitry Guryanov 已提交
4144
 *
4145
 * Registers the vz driver
D
Dmitry Guryanov 已提交
4146 4147
 */
int
4148
vzRegister(void)
D
Dmitry Guryanov 已提交
4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159
{
    char *prlctl_path;

    prlctl_path = virFindFileInPath(PRLCTL);
    if (!prlctl_path) {
        VIR_DEBUG("%s", _("Can't find prlctl command in the PATH env"));
        return 0;
    }

    VIR_FREE(prlctl_path);

4160
    /* Backward compatibility with Parallels domain type */
4161 4162
    parallelsHypervisorDriver = vzHypervisorDriver;
    parallelsHypervisorDriver.name = "Parallels";
4163
    if (virRegisterConnectDriver(&parallelsConnectDriver, true) < 0)
D
Dmitry Guryanov 已提交
4164
        return -1;
D
Dmitry Guryanov 已提交
4165

4166
    if (virRegisterConnectDriver(&vzConnectDriver, true) < 0)
4167 4168
        return -1;

4169 4170 4171
    if (virRegisterStateDriver(&vzStateDriver) < 0)
        return -1;

D
Dmitry Guryanov 已提交
4172 4173
    return 0;
}