vz_driver.c 87.3 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 38 39 40 41 42
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.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"
43
#include "virerror.h"
44
#include "viralloc.h"
45
#include "virlog.h"
46
#include "vircommand.h"
D
Dmitry Guryanov 已提交
47
#include "configmake.h"
48
#include "virfile.h"
49
#include "virstoragefile.h"
D
Dmitry Guryanov 已提交
50
#include "nodeinfo.h"
51
#include "virstring.h"
52
#include "cpu/cpu.h"
53
#include "virtypedparam.h"
54 55
#include "virhostmem.h"
#include "virhostcpu.h"
D
Dmitry Guryanov 已提交
56

57 58 59
#include "vz_driver.h"
#include "vz_utils.h"
#include "vz_sdk.h"
D
Dmitry Guryanov 已提交
60 61 62

#define VIR_FROM_THIS VIR_FROM_PARALLELS

63 64
VIR_LOG_INIT("parallels.parallels_driver");

D
Dmitry Guryanov 已提交
65 66
#define PRLCTL                      "prlctl"

67
static virClassPtr vzDriverClass;
D
Dmitry Guryanov 已提交
68

69 70 71 72 73 74 75
static virMutex vz_driver_lock;
static vzDriverPtr vz_driver;
static vzConnPtr vz_conn_list;

static vzDriverPtr
vzDriverObjNew(void);

D
Dmitry Guryanov 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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 已提交
97
static virCapsPtr
98
vzBuildCapabilities(void)
D
Dmitry Guryanov 已提交
99
{
100 101 102 103
    virCapsPtr caps = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUDataPtr data = NULL;
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
104 105 106 107 108
    virDomainOSType ostypes[] = {
        VIR_DOMAIN_OSTYPE_HVM,
        VIR_DOMAIN_OSTYPE_EXE
    };
    virArch archs[] = { VIR_ARCH_I686, VIR_ARCH_X86_64 };
109
    const char *const emulators[] = { "vz", "parallels"};
D
Dmitry Guryanov 已提交
110
    virDomainVirtType virt_types[] = {
111 112
        VIR_DOMAIN_VIRT_VZ,
        VIR_DOMAIN_VIRT_PARALLELS
D
Dmitry Guryanov 已提交
113 114
    };
    size_t i, j, k;
D
Dmitry Guryanov 已提交
115

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

120
    if (nodeCapsInitNUMA(caps) < 0)
121
        goto error;
D
Dmitry Guryanov 已提交
122

D
Dmitry Guryanov 已提交
123 124 125 126 127 128
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            for (k = 0; k < 2; k++)
                if (vzCapsAddGuestDomain(caps, ostypes[i], archs[j],
                                         emulators[k], virt_types[k]) < 0)
                    goto error;
129

130
    if (nodeGetInfo(&nodeinfo))
131 132
        goto error;

133
    if (VIR_ALLOC(cpu) < 0)
134 135 136 137 138 139 140 141 142 143
        goto error;

    cpu->arch = caps->host.arch;
    cpu->type = VIR_CPU_TYPE_HOST;
    cpu->sockets = nodeinfo.sockets;
    cpu->cores = nodeinfo.cores;
    cpu->threads = nodeinfo.threads;

    caps->host.cpu = cpu;

144 145 146
    if (virCapabilitiesAddHostMigrateTransport(caps, "vzmigr") < 0)
        goto error;

147 148 149 150 151 152 153
    if (!(data = cpuNodeData(cpu->arch))
        || cpuDecode(cpu, data, NULL, 0, NULL) < 0) {
        goto cleanup;
    }

 cleanup:
    cpuDataFree(data);
D
Dmitry Guryanov 已提交
154 155
    return caps;

156
 error:
157
    virObjectUnref(caps);
158
    goto cleanup;
D
Dmitry Guryanov 已提交
159 160
}

161 162 163 164 165 166 167 168 169 170 171 172 173
static void vzDriverDispose(void * obj)
{
    vzDriverPtr driver = obj;

    if (driver->server) {
        prlsdkUnsubscribeFromPCSEvents(driver);
        prlsdkDisconnect(driver);
    }

    virObjectUnref(driver->domains);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->xmlopt);
    virObjectEventStateFree(driver->domainEventState);
174
    virSysinfoDefFree(driver->hostsysinfo);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
}

static int vzDriverOnceInit(void)
{
    if (!(vzDriverClass = virClassNew(virClassForObjectLockable(),
                                      "vzDriver",
                                      sizeof(vzDriver),
                                      vzDriverDispose)))
        return -1;

    return 0;
}
VIR_ONCE_GLOBAL_INIT(vzDriver)

vzDriverPtr
vzGetDriverConnection(void)
{
    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);
    driver = vz_driver;
    vz_driver = NULL;

    privconn_list = vz_conn_list;
    vz_conn_list = NULL;

    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 已提交
226
static char *
227
vzConnectGetCapabilities(virConnectPtr conn)
D
Dmitry Guryanov 已提交
228
{
229
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
230 231
    char *xml;

232
    xml = virCapabilitiesFormatXML(privconn->driver->caps);
D
Dmitry Guryanov 已提交
233 234
    return xml;
}
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
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 已提交
257

258
static int
259
vzDomainDefPostParse(virDomainDefPtr def,
M
Michal Privoznik 已提交
260
                     virCapsPtr caps ATTRIBUTE_UNUSED,
261
                     unsigned int parseFlags ATTRIBUTE_UNUSED,
262
                     void *opaque ATTRIBUTE_UNUSED)
263
{
264 265 266
    if (vzDomainDefAddDefaultInputDevices(def) < 0)
        return -1;

267 268 269 270 271 272 273 274
    return 0;
}

static int
vzDomainDefValidate(const virDomainDef *def,
                    virCapsPtr caps ATTRIBUTE_UNUSED,
                    void *opaque)
{
275 276 277
    if (vzCheckUnsupportedControllers(def, opaque) < 0)
        return -1;

278 279 280 281
    return 0;
}

static int
282
vzDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
M
Michal Privoznik 已提交
283 284
                           const virDomainDef *def,
                           virCapsPtr caps ATTRIBUTE_UNUSED,
285
                           unsigned int parseFlags ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
286
                           void *opaque ATTRIBUTE_UNUSED)
287
{
288 289 290 291
    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) &&
        !dev->data.net->model &&
292
        def->os.type == VIR_DOMAIN_OSTYPE_HVM &&
293
        VIR_STRDUP(dev->data.net->model, "e1000") < 0)
294
        return -1;
295

296 297 298 299 300 301 302 303
    return 0;
}

static int
vzDomainDeviceDefValidate(const virDomainDeviceDef *dev,
                          const virDomainDef *def,
                          void *opaque ATTRIBUTE_UNUSED)
{
304 305
    if (dev->type == VIR_DOMAIN_DEVICE_DISK)
        return vzCheckUnsupportedDisk(def, dev->data.disk, opaque);
306 307
    else if (dev->type == VIR_DOMAIN_DEVICE_GRAPHICS)
        return vzCheckUnsupportedGraphics(dev->data.graphics);
308 309

    return 0;
310 311
}

312 313 314 315
static virDomainXMLPrivateDataCallbacks vzDomainXMLPrivateDataCallbacksPtr = {
    .alloc = vzDomObjAlloc,
    .free = vzDomObjFree,
};
316

M
Maxim Nestratov 已提交
317
static virDomainDefParserConfig vzDomainDefParserConfig = {
318
    .macPrefix = {0x42, 0x1C, 0x00},
319
    .domainPostParseCallback = vzDomainDefPostParse,
320 321 322
    .devicesPostParseCallback = vzDomainDeviceDefPostParse,
    .domainValidateCallback = vzDomainDefValidate,
    .deviceValidateCallback = vzDomainDeviceDefValidate,
323 324
};

325 326
static vzDriverPtr
vzDriverObjNew(void)
D
Dmitry Guryanov 已提交
327
{
328
    vzDriverPtr driver;
D
Dmitry Guryanov 已提交
329

330 331
    if (vzDriverInitialize() < 0)
        return NULL;
D
Dmitry Guryanov 已提交
332

333 334
    if (!(driver = virObjectLockableNew(vzDriverClass)))
        return NULL;
335

336 337 338 339
    vzDomainDefParserConfig.priv = &driver->vzCaps;

    if (!(driver->caps = vzBuildCapabilities()) ||
        !(driver->xmlopt = virDomainXMLOptionNew(&vzDomainDefParserConfig,
340 341
                                                 &vzDomainXMLPrivateDataCallbacksPtr,
                                                 NULL)) ||
342 343 344 345
        !(driver->domains = virDomainObjListNew()) ||
        !(driver->domainEventState = virObjectEventStateNew()) ||
        (vzInitVersion(driver) < 0) ||
        (prlsdkConnect(driver) < 0) ||
346
        (prlsdkSubscribeToPCSEvents(driver) < 0)) {
347 348 349
        virObjectUnref(driver);
        return NULL;
    }
D
Dmitry Guryanov 已提交
350

351
    driver->hostsysinfo = virSysinfoRead();
352
    ignore_value(prlsdkLoadDomains(driver));
353 354 355 356 357

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

358
    return driver;
D
Dmitry Guryanov 已提交
359 360 361
}

static virDrvOpenStatus
362
vzConnectOpen(virConnectPtr conn,
M
Michal Privoznik 已提交
363
              virConnectAuthPtr auth ATTRIBUTE_UNUSED,
364
              virConfPtr conf ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
365
              unsigned int flags)
D
Dmitry Guryanov 已提交
366
{
367 368
    vzDriverPtr driver = NULL;
    vzConnPtr privconn = NULL;
D
Dmitry Guryanov 已提交
369 370 371 372 373 374

    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    if (!conn->uri)
        return VIR_DRV_OPEN_DECLINED;

375 376 377 378 379 380 381 382 383 384 385
    if (!conn->uri->scheme)
        return VIR_DRV_OPEN_DECLINED;

    if (STRNEQ(conn->uri->scheme, "vz") &&
        STRNEQ(conn->uri->scheme, "parallels"))
        return VIR_DRV_OPEN_DECLINED;

    if (STREQ(conn->uri->scheme, "vz") && STRNEQ(conn->driver->name, "vz"))
        return VIR_DRV_OPEN_DECLINED;

    if (STREQ(conn->uri->scheme, "parallels") && STRNEQ(conn->driver->name, "Parallels"))
D
Dmitry Guryanov 已提交
386 387 388 389 390 391 392
        return VIR_DRV_OPEN_DECLINED;

    /* Remote driver should handle these. */
    if (conn->uri->server)
        return VIR_DRV_OPEN_DECLINED;

    /* From this point on, the connection is for us. */
393
    if (STRNEQ_NULLABLE(conn->uri->path, "/system")) {
394
        virReportError(VIR_ERR_INTERNAL_ERROR,
395
                       _("Unexpected Virtuozzo URI path '%s', try vz:///system"),
396
                       conn->uri->path);
D
Dmitry Guryanov 已提交
397 398 399
        return VIR_DRV_OPEN_ERROR;
    }

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    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 已提交
416 417

    return VIR_DRV_OPEN_SUCCESS;
418 419 420 421 422 423 424

 error:

    conn->privateData = NULL;
    virObjectUnref(driver);
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
D
Dmitry Guryanov 已提交
425 426 427
}

static int
428
vzConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
429
{
430
    vzConnPtr curr, *prev = &vz_conn_list;
431
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
432

433 434 435
    if (!privconn)
        return 0;

436 437 438 439 440 441 442
    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 已提交
443

444
    virMutexUnlock(&vz_driver_lock);
D
Dmitry Guryanov 已提交
445

446 447
    virObjectUnref(privconn->closeCallback);
    virObjectUnref(privconn->driver);
D
Dmitry Guryanov 已提交
448
    VIR_FREE(privconn);
449
    conn->privateData = NULL;
D
Dmitry Guryanov 已提交
450 451 452 453
    return 0;
}

static int
454
vzConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
455
{
456
    vzConnPtr privconn = conn->privateData;
457
    *hvVer = privconn->driver->vzVersion;
458
    return 0;
459 460
}

461

462
static char *vzConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
463 464 465 466
{
    return virGetHostname();
}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
static char *
vzConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
    vzDriverPtr driver = privconn->driver;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

    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;
    if (virBufferCheckError(&buf) < 0)
        return NULL;

    return virBufferContentAndReset(&buf);
}
489

490
static int
491
vzConnectListDomains(virConnectPtr conn, int *ids, int maxids)
492
{
493
    vzConnPtr privconn = conn->privateData;
494 495
    int n;

496
    n = virDomainObjListGetActiveIDs(privconn->driver->domains, ids, maxids,
497
                                     NULL, NULL);
498 499 500 501 502

    return n;
}

static int
503
vzConnectNumOfDomains(virConnectPtr conn)
504
{
505
    vzConnPtr privconn = conn->privateData;
506 507
    int count;

508
    count = virDomainObjListNumOfDomains(privconn->driver->domains, true,
509
                                         NULL, NULL);
510 511 512 513 514

    return count;
}

static int
515
vzConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
516
{
517
    vzConnPtr privconn = conn->privateData;
518 519 520
    int n;

    memset(names, 0, sizeof(*names) * maxnames);
521
    n = virDomainObjListGetInactiveNames(privconn->driver->domains, names,
522
                                         maxnames, NULL, NULL);
523 524 525 526 527

    return n;
}

static int
528
vzConnectNumOfDefinedDomains(virConnectPtr conn)
529
{
530
    vzConnPtr privconn = conn->privateData;
531 532
    int count;

533
    count = virDomainObjListNumOfDomains(privconn->driver->domains, false,
534
                                         NULL, NULL);
535 536 537 538
    return count;
}

static int
539
vzConnectListAllDomains(virConnectPtr conn,
M
Michal Privoznik 已提交
540 541
                        virDomainPtr **domains,
                        unsigned int flags)
542
{
543
    vzConnPtr privconn = conn->privateData;
544 545
    int ret = -1;

O
Osier Yang 已提交
546
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
547
    ret = virDomainObjListExport(privconn->driver->domains, conn, domains,
548
                                 NULL, flags);
549 550 551 552 553

    return ret;
}

static virDomainPtr
554
vzDomainLookupByID(virConnectPtr conn, int id)
555
{
556
    vzConnPtr privconn = conn->privateData;
557
    virDomainPtr ret;
558 559
    virDomainObjPtr dom;

560
    dom = virDomainObjListFindByID(privconn->driver->domains, id);
561 562 563

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
564
        return NULL;
565 566 567 568 569 570
    }

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

571
    virObjectUnlock(dom);
572 573 574 575
    return ret;
}

static virDomainPtr
576
vzDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
577
{
578
    vzConnPtr privconn = conn->privateData;
579
    virDomainPtr ret;
580 581
    virDomainObjPtr dom;

582
    dom = virDomainObjListFindByUUID(privconn->driver->domains, uuid);
583 584 585 586 587 588

    if (dom == NULL) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
589
        return NULL;
590 591 592 593 594 595
    }

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

596
    virObjectUnlock(dom);
597 598 599 600
    return ret;
}

static virDomainPtr
601
vzDomainLookupByName(virConnectPtr conn, const char *name)
602
{
603
    vzConnPtr privconn = conn->privateData;
604
    virDomainPtr ret;
605 606
    virDomainObjPtr dom;

607
    dom = virDomainObjListFindByName(privconn->driver->domains, name);
608 609 610 611

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
612
        return NULL;
613 614 615 616 617 618
    }

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

619
    virDomainObjEndAPI(&dom);
620 621 622 623
    return ret;
}

static int
624
vzDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
625
{
626
    virDomainObjPtr dom;
627
    vzDomObjPtr privdom;
628 629
    int ret = -1;

630
    if (!(dom = vzDomObjFromDomainRef(domain)))
631 632
        goto cleanup;

633 634
    info->state = virDomainObjGetState(dom, NULL);
    info->memory = dom->def->mem.cur_balloon;
635
    info->maxMem = virDomainDefGetMemoryTotal(dom->def);
636
    info->nrVirtCpu = virDomainDefGetVcpus(dom->def);
637
    info->cpuTime = 0;
638

639 640 641
    privdom = dom->privateData;

    if (PRL_INVALID_HANDLE != privdom->stats && virDomainObjIsActive(dom)) {
642 643 644
        unsigned long long vtime;
        size_t i;

645
        for (i = 0; i < virDomainDefGetVcpus(dom->def); ++i) {
646
            if (prlsdkGetVcpuStats(privdom->stats, i, &vtime) < 0) {
647 648 649 650 651 652 653
                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                               _("cannot read cputime for domain"));
                goto cleanup;
            }
            info->cpuTime += vtime;
        }
    }
654 655
    ret = 0;

656
 cleanup:
657
    virDomainObjEndAPI(&dom);
658 659 660 661
    return ret;
}

static char *
662
vzDomainGetOSType(virDomainPtr domain)
663
{
664
    virDomainObjPtr dom;
665 666
    char *ret = NULL;

667
    if (!(dom = vzDomObjFromDomain(domain)))
668
        return NULL;
669

670
    ignore_value(VIR_STRDUP(ret, virDomainOSTypeToString(dom->def->os.type)));
671

672
    virObjectUnlock(dom);
673 674 675 676
    return ret;
}

static int
677
vzDomainIsPersistent(virDomainPtr domain)
678
{
679
    virDomainObjPtr dom;
680

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

684 685
    virObjectUnlock(dom);
    return 1;
686 687 688
}

static int
689
vzDomainGetState(virDomainPtr domain,
M
Michal Privoznik 已提交
690
                 int *state, int *reason, unsigned int flags)
691
{
692
    virDomainObjPtr dom;
693

694 695
    virCheckFlags(0, -1);

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

699
    *state = virDomainObjGetState(dom, reason);
700

701 702
    virObjectUnlock(dom);
    return 0;
703 704 705
}

static char *
706
vzDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
707
{
708
    vzConnPtr privconn = domain->conn->privateData;
709
    virDomainDefPtr def;
710
    virDomainObjPtr dom;
711 712 713 714
    char *ret = NULL;

    /* Flags checked by virDomainDefFormat */

715
    if (!(dom = vzDomObjFromDomain(domain)))
716
        return NULL;
717 718

    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
719
        dom->newDef ? dom->newDef : dom->def;
720

721
    ret = virDomainDefFormat(def, privconn->driver->caps, flags);
722

723
    virObjectUnlock(dom);
724 725 726 727
    return ret;
}

static int
728
vzDomainGetAutostart(virDomainPtr domain, int *autostart)
729
{
730
    virDomainObjPtr dom;
731

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

735
    *autostart = dom->autostart;
736

737 738
    virObjectUnlock(dom);
    return 0;
D
Dmitry Guryanov 已提交
739 740
}

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
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;
}

757
static virDomainPtr
758
vzDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
759
{
760
    vzConnPtr privconn = conn->privateData;
761
    virDomainPtr retdom = NULL;
762
    virDomainDefPtr def;
763
    virDomainObjPtr dom = NULL;
764
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
765
    vzDriverPtr driver = privconn->driver;
766
    bool job = false;
767

768 769 770
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
771
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
772

773
    if ((def = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
774
                                       parse_flags)) == NULL)
775 776
        goto cleanup;

777
    dom = virDomainObjListFindByUUIDRef(driver->domains, def->uuid);
778
    if (dom == NULL) {
779
        virResetLastError();
780
        if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
781
            if (prlsdkCreateVm(driver, def))
782
                goto cleanup;
783
        } else if (def->os.type == VIR_DOMAIN_OSTYPE_EXE) {
784
            if (prlsdkCreateCt(conn, def))
785 786 787
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INVALID_ARG,
788 789
                           _("Unsupported OS type: %s"),
                           virDomainOSTypeToString(def->os.type));
790
            goto cleanup;
791
        }
792

793
        if (!(dom = prlsdkAddDomainByUUID(driver, def->uuid)))
794
            goto cleanup;
795
    } else {
796 797
        int state, reason;

798
        state = virDomainObjGetState(dom, &reason);
799 800 801 802 803 804 805 806 807 808 809 810 811

        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. */

812
            if (!virDomainDefCheckABIStability(dom->def, def)) {
813 814 815 816 817 818
                virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                               _("Can't change domain configuration "
                                 "in managed save state"));
                goto cleanup;
            }
        } else {
819 820 821 822
            if (vzDomainObjBeginJob(dom) < 0)
                goto cleanup;
            job = true;

823 824 825
            if (vzEnsureDomainExists(dom) < 0)
                goto cleanup;

826
            if (prlsdkApplyConfig(driver, dom, def))
827
                goto cleanup;
828

829
            if (prlsdkUpdateDomain(driver, dom))
830 831
                goto cleanup;
        }
832 833
    }

834 835 836
    retdom = virGetDomain(conn, def->name, def->uuid);
    if (retdom)
        retdom->id = def->id;
837

838
 cleanup:
839 840 841
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
842
    virDomainDefFree(def);
843
    return retdom;
844 845
}

846
static virDomainPtr
847
vzDomainDefineXML(virConnectPtr conn, const char *xml)
848
{
849
    return vzDomainDefineXMLFlags(conn, xml, 0);
850 851 852
}


853
static int
854
vzNodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
855
              virNodeInfoPtr nodeinfo)
856
{
857
    return nodeGetInfo(nodeinfo);
858 859
}

860
static int vzConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
861 862 863 864 865
{
    /* Encryption is not relevant / applicable to way we talk to PCS */
    return 0;
}

866
static int vzConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
867 868 869 870 871
{
    /* We run CLI tools directly so this is secure */
    return 1;
}

872
static int vzConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
873 874 875 876
{
    return 1;
}

877

878
static char *
879
vzConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
880 881 882
                     const char **xmlCPUs,
                     unsigned int ncpus,
                     unsigned int flags)
883 884 885 886 887 888 889
{
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

    return cpuBaselineXML(xmlCPUs, ncpus, NULL, 0, flags);
}


890
static int
891
vzDomainGetVcpus(virDomainPtr domain,
M
Michal Privoznik 已提交
892 893 894 895
                 virVcpuInfoPtr info,
                 int maxinfo,
                 unsigned char *cpumaps,
                 int maplen)
896
{
897
    virDomainObjPtr dom = NULL;
898 899 900
    size_t i;
    int ret = -1;

901
    if (!(dom = vzDomObjFromDomainRef(domain)))
902
        return -1;
903

904
    if (!virDomainObjIsActive(dom)) {
905 906 907 908 909 910 911 912
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",
                       _("cannot list vcpu pinning for an inactive domain"));
        goto cleanup;
    }

    if (maxinfo >= 1) {
        if (info != NULL) {
913 914
        vzDomObjPtr privdom;

915
            memset(info, 0, sizeof(*info) * maxinfo);
916 917
            privdom = dom->privateData;

918 919 920
            for (i = 0; i < maxinfo; i++) {
                info[i].number = i;
                info[i].state = VIR_VCPU_RUNNING;
921
                if (prlsdkGetVcpuStats(privdom->stats, i, &info[i].cpuTime) < 0)
N
Nikolay Shirokovskiy 已提交
922
                    goto cleanup;
923 924 925 926
            }
        }
        if (cpumaps != NULL) {
            memset(cpumaps, 0, maplen * maxinfo);
927
            for (i = 0; i < maxinfo; i++)
928
                virBitmapToDataBuf(dom->def->cpumask,
929 930
                                   VIR_GET_CPUMAP(cpumaps, maplen, i),
                                   maplen);
931 932 933 934 935
        }
    }
    ret = maxinfo;

 cleanup:
936
    virDomainObjEndAPI(&dom);
937 938 939 940
    return ret;
}


941
static int
942
vzNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
943 944 945
                unsigned char **cpumap,
                unsigned int *online,
                unsigned int flags)
946
{
947
    return virHostCPUGetMap(cpumap, online, flags);
948 949
}

950
static int
951
vzConnectDomainEventRegisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
952 953 954 955 956
                                virDomainPtr domain,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
                                virFreeCallback freecb)
957 958
{
    int ret = -1;
959
    vzConnPtr privconn = conn->privateData;
960
    if (virDomainEventStateRegisterID(conn,
961
                                      privconn->driver->domainEventState,
962 963 964 965 966 967 968
                                      domain, eventID,
                                      callback, opaque, freecb, &ret) < 0)
        ret = -1;
    return ret;
}

static int
969
vzConnectDomainEventDeregisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
970
                                  int callbackID)
971
{
972
    vzConnPtr privconn = conn->privateData;
973 974

    if (virObjectEventStateDeregisterID(conn,
975
                                        privconn->driver->domainEventState,
976
                                        callbackID) < 0)
977
        return -1;
978

979
    return 0;
980
}
981

982 983
static int
vzDomainSuspend(virDomainPtr domain)
984
{
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

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

    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;
1014 1015
}

1016 1017
static int
vzDomainResume(virDomainPtr domain)
1018
{
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

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

    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;
1048 1049
}

1050 1051
static int
vzDomainCreate(virDomainPtr domain)
1052
{
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

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

    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;
1082 1083
}

1084 1085
static int
vzDomainDestroy(virDomainPtr domain)
1086
{
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

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

    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;
1116 1117
}

1118 1119
static int
vzDomainShutdown(virDomainPtr domain)
1120
{
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

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

    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;
1150 1151
}

1152 1153
static int
vzDomainReboot(virDomainPtr domain, unsigned int flags)
1154
{
1155 1156 1157 1158 1159
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1160
    virCheckFlags(0, -1);
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

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

    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;
1186 1187
}

1188
static int vzDomainIsActive(virDomainPtr domain)
1189 1190 1191 1192
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

1193
    if (!(dom = vzDomObjFromDomain(domain)))
1194 1195 1196 1197 1198 1199 1200 1201
        return -1;

    ret = virDomainObjIsActive(dom);
    virObjectUnlock(dom);

    return ret;
}

1202
static int
1203
vzDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
1204 1205 1206 1207
{
    /* we don't support any create flags */
    virCheckFlags(0, -1);

1208
    return vzDomainCreate(domain);
1209 1210
}

1211
static int
1212
vzDomainUndefineFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1213
                      unsigned int flags)
1214
{
1215
    vzConnPtr privconn = domain->conn->privateData;
1216
    virDomainObjPtr dom = NULL;
1217 1218
    int ret = -1;
    bool job = false;
1219

1220 1221
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
1222

1223
    if (!(dom = vzDomObjFromDomainRef(domain)))
1224 1225
        return -1;

1226 1227 1228 1229
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1230 1231 1232
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1233
    ret = prlsdkUnregisterDomain(privconn->driver, dom, flags);
1234 1235 1236 1237 1238 1239

 cleanup:

    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1240 1241

    return ret;
1242 1243 1244
}

static int
1245
vzDomainUndefine(virDomainPtr domain)
1246
{
1247
    return vzDomainUndefineFlags(domain, 0);
1248 1249
}

1250
static int
1251
vzDomainHasManagedSaveImage(virDomainPtr domain, unsigned int flags)
1252 1253
{
    virDomainObjPtr dom = NULL;
1254 1255
    int state, reason;
    int ret = 0;
1256 1257 1258

    virCheckFlags(0, -1);

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

1262 1263 1264
    state = virDomainObjGetState(dom, &reason);
    if (state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED)
        ret = 1;
1265 1266
    virObjectUnlock(dom);

1267 1268 1269 1270
    return ret;
}

static int
1271
vzDomainManagedSave(virDomainPtr domain, unsigned int flags)
1272
{
1273
    vzConnPtr privconn = domain->conn->privateData;
1274 1275 1276
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;
1277
    bool job = false;
1278 1279 1280 1281

    virCheckFlags(VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

1282
    if (!(dom = vzDomObjFromDomainRef(domain)))
1283 1284
        return -1;

1285 1286 1287 1288
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1289 1290 1291
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1292 1293
    state = virDomainObjGetState(dom, &reason);

1294 1295 1296
    if (state == VIR_DOMAIN_RUNNING && (flags & VIR_DOMAIN_SAVE_PAUSED) &&
        prlsdkPause(dom) < 0)
        goto cleanup;
1297

1298 1299 1300 1301 1302 1303 1304
    if (prlsdkSuspend(dom) < 0)
        goto cleanup;

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

    ret = 0;
1305 1306

 cleanup:
1307 1308 1309
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1310 1311 1312 1313
    return ret;
}

static int
1314
vzDomainManagedSaveRemove(virDomainPtr domain, unsigned int flags)
1315 1316 1317 1318 1319 1320 1321
{
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(0, -1);

1322
    if (!(dom = vzDomObjFromDomainRef(domain)))
1323 1324 1325 1326 1327 1328 1329
        return -1;

    state = virDomainObjGetState(dom, &reason);

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

1330
    ret = prlsdkDomainManagedSaveRemove(dom);
1331 1332

 cleanup:
1333
    virDomainObjEndAPI(&dom);
1334
    return ret;
1335 1336
}

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
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;
}

1359
static int vzDomainAttachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1360
                                     unsigned int flags)
1361 1362
{
    int ret = -1;
1363
    vzConnPtr privconn = domain->conn->privateData;
1364
    virDomainDeviceDefPtr dev = NULL;
1365
    virDomainObjPtr dom = NULL;
1366
    vzDriverPtr driver = privconn->driver;
1367
    bool job = false;
1368 1369 1370 1371

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1372
    if (!(dom = vzDomObjFromDomainRef(domain)))
1373
        return -1;
1374

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

1378 1379
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
                                  driver->xmlopt, VIR_DOMAIN_XML_INACTIVE);
1380 1381 1382
    if (dev == NULL)
        goto cleanup;

1383 1384 1385 1386
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1387 1388 1389
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1390 1391
    if (prlsdkAttachDevice(driver, dom, dev) < 0)
        goto cleanup;
1392

1393 1394 1395
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1396 1397
    ret = 0;
 cleanup:
1398
    virDomainDeviceDefFree(dev);
1399 1400 1401
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1402 1403 1404
    return ret;
}

1405
static int vzDomainAttachDevice(virDomainPtr domain, const char *xml)
1406
{
1407
    return vzDomainAttachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1408
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1409 1410
}

1411
static int vzDomainDetachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1412
                                     unsigned int flags)
1413 1414
{
    int ret = -1;
1415
    vzConnPtr privconn = domain->conn->privateData;
1416
    virDomainDeviceDefPtr dev = NULL;
1417
    virDomainObjPtr dom = NULL;
1418
    vzDriverPtr driver = privconn->driver;
1419
    bool job = false;
1420 1421 1422 1423

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1424
    dom = vzDomObjFromDomainRef(domain);
1425
    if (dom == NULL)
1426 1427
        return -1;

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

1431 1432
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
                                  driver->xmlopt,
1433 1434
                                  VIR_DOMAIN_XML_INACTIVE |
                                  VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE);
1435 1436 1437
    if (dev == NULL)
        goto cleanup;

1438 1439 1440 1441
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1442 1443 1444
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1445 1446
    if (prlsdkDetachDevice(driver, dom, dev) < 0)
        goto cleanup;
1447

1448 1449 1450
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1451 1452
    ret = 0;
 cleanup:
1453
    virDomainDeviceDefFree(dev);
1454 1455 1456
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1457

1458 1459 1460
    return ret;
}

1461
static int vzDomainDetachDevice(virDomainPtr domain, const char *xml)
1462
{
1463
    return vzDomainDetachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1464
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1465 1466
}

1467 1468 1469 1470 1471 1472 1473 1474
static int
vzDomainSetUserPassword(virDomainPtr domain,
                        const char *user,
                        const char *password,
                        unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
1475
    bool job = false;
1476 1477

    virCheckFlags(0, -1);
1478
    if (!(dom = vzDomObjFromDomainRef(domain)))
1479 1480
        return -1;

1481 1482 1483 1484
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1485 1486 1487
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1488 1489
    ret = prlsdkDomainSetUserPassword(dom, user, password);

1490 1491 1492 1493
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1494 1495 1496
    return ret;
}

1497
static int vzDomainUpdateDeviceFlags(virDomainPtr domain,
N
Nikolay Shirokovskiy 已提交
1498 1499 1500 1501
                                     const char *xml,
                                     unsigned int flags)
{
    int ret = -1;
1502 1503
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom = NULL;
N
Nikolay Shirokovskiy 已提交
1504 1505
    virDomainDeviceDefPtr dev = NULL;
    vzDriverPtr driver = privconn->driver;
1506
    bool job = false;
N
Nikolay Shirokovskiy 已提交
1507 1508 1509 1510

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1511
    if (!(dom = vzDomObjFromDomainRef(domain)))
N
Nikolay Shirokovskiy 已提交
1512 1513
        return -1;

1514
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
N
Nikolay Shirokovskiy 已提交
1515 1516
        goto cleanup;

1517
    if (!(dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
N
Nikolay Shirokovskiy 已提交
1518 1519 1520 1521
                                        driver->xmlopt,
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto cleanup;

1522 1523 1524 1525
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1526 1527 1528
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1529
    if (prlsdkUpdateDevice(driver, dom, dev) < 0)
N
Nikolay Shirokovskiy 已提交
1530 1531 1532 1533 1534 1535
        goto cleanup;

    ret = 0;
 cleanup:

    virDomainDeviceDefFree(dev);
1536 1537 1538
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
N
Nikolay Shirokovskiy 已提交
1539 1540 1541 1542
    return ret;
}


1543
static unsigned long long
1544
vzDomainGetMaxMemory(virDomainPtr domain)
1545 1546 1547 1548
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

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

1552
    ret = virDomainDefGetMemoryTotal(dom->def);
1553 1554 1555 1556
    virObjectUnlock(dom);
    return ret;
}

1557
static int
1558
vzDomainBlockStats(virDomainPtr domain, const char *path,
M
Michal Privoznik 已提交
1559
                   virDomainBlockStatsPtr stats)
1560 1561
{
    virDomainObjPtr dom = NULL;
1562
    vzDomObjPtr privdom;
1563 1564 1565 1566
    int ret = -1;
    size_t i;
    int idx;

1567
    if (!(dom = vzDomObjFromDomainRef(domain)))
1568 1569
        return -1;

1570 1571
    privdom = dom->privateData;

1572 1573 1574 1575 1576
    if (*path) {
        if ((idx = virDomainDiskIndexByName(dom->def, path, false)) < 0) {
            virReportError(VIR_ERR_INVALID_ARG, _("invalid path: %s"), path);
            goto cleanup;
        }
1577
        if (prlsdkGetBlockStats(privdom->stats, dom->def->disks[idx], stats) < 0)
1578 1579 1580 1581
            goto cleanup;
    } else {
        virDomainBlockStatsStruct s;

M
Michal Privoznik 已提交
1582
#define PARALLELS_ZERO_STATS(VAR, TYPE, NAME)      \
1583 1584 1585 1586 1587 1588 1589
        stats->VAR = 0;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_ZERO_STATS)

#undef PARALLELS_ZERO_STATS

        for (i = 0; i < dom->def->ndisks; i++) {
1590
            if (prlsdkGetBlockStats(privdom->stats, dom->def->disks[i], &s) < 0)
1591 1592
                goto cleanup;

1593
#define PARALLELS_SUM_STATS(VAR, TYPE, NAME)        \
M
Michal Privoznik 已提交
1594 1595
    if (s.VAR != -1)                                \
        stats->VAR += s.VAR;
1596

M
Michal Privoznik 已提交
1597
        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_SUM_STATS)
1598

1599
#undef PARALLELS_SUM_STATS
1600 1601 1602 1603 1604 1605
        }
    }
    stats->errs = -1;
    ret = 0;

 cleanup:
1606
    virDomainObjEndAPI(&dom);
1607 1608 1609 1610 1611

    return ret;
}

static int
1612
vzDomainBlockStatsFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1613 1614 1615 1616
                        const char *path,
                        virTypedParameterPtr params,
                        int *nparams,
                        unsigned int flags)
1617 1618 1619 1620 1621 1622 1623 1624 1625
{
    virDomainBlockStatsStruct stats;
    int ret = -1;
    size_t i;

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

1626
    if (vzDomainBlockStats(domain, path, &stats) < 0)
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
        goto cleanup;

    if (*nparams == 0) {
#define PARALLELS_COUNT_STATS(VAR, TYPE, NAME)       \
        if ((stats.VAR) != -1)                       \
            ++*nparams;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_COUNT_STATS)

#undef PARALLELS_COUNT_STATS
        ret = 0;
        goto cleanup;
    }

    i = 0;
#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)   \
            goto cleanup;                                                      \
        i++;                                                                   \
    }

    PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_BLOCK_STATS_ASSIGN_PARAM)

#undef PARALLELS_BLOCK_STATS_ASSIGN_PARAM

    *nparams = i;
    ret = 0;

 cleanup:
    return ret;
}

1661 1662 1663 1664 1665 1666
static int
vzDomainInterfaceStats(virDomainPtr domain,
                         const char *path,
                         virDomainInterfaceStatsPtr stats)
{
    virDomainObjPtr dom = NULL;
1667
    vzDomObjPtr privdom;
1668 1669 1670 1671 1672
    int ret;

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

1673 1674 1675
    privdom = dom->privateData;

    ret = prlsdkGetNetStats(privdom->stats, privdom->sdkdom, path, stats);
1676 1677 1678 1679
    virDomainObjEndAPI(&dom);

    return ret;
}
1680

N
Nikolay Shirokovskiy 已提交
1681 1682 1683 1684 1685 1686 1687
static int
vzDomainMemoryStats(virDomainPtr domain,
                    virDomainMemoryStatPtr stats,
                    unsigned int nr_stats,
                    unsigned int flags)
{
    virDomainObjPtr dom = NULL;
1688
    vzDomObjPtr privdom;
N
Nikolay Shirokovskiy 已提交
1689 1690 1691 1692 1693 1694
    int ret = -1;

    virCheckFlags(0, -1);
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

1695 1696 1697
    privdom = dom->privateData;

    ret = prlsdkGetMemoryStats(privdom->stats, stats, nr_stats);
N
Nikolay Shirokovskiy 已提交
1698 1699 1700 1701 1702
    virDomainObjEndAPI(&dom);

    return ret;
}

1703
static int
1704
vzDomainGetVcpusFlags(virDomainPtr domain,
1705 1706
                      unsigned int flags)
{
1707 1708
    virDomainObjPtr dom;
    int ret;
1709 1710 1711 1712 1713

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

1714
    if (!(dom = vzDomObjFromDomain(domain)))
1715
        return -1;
1716 1717

    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
1718
        ret = virDomainDefGetVcpusMax(dom->def);
1719
    else
1720
        ret = virDomainDefGetVcpus(dom->def);
1721

1722
    virObjectUnlock(dom);
1723 1724 1725 1726

    return ret;
}

1727
static int vzDomainGetMaxVcpus(virDomainPtr domain)
1728
{
1729 1730
    return vzDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
                                          VIR_DOMAIN_VCPU_MAXIMUM));
1731 1732
}

1733
static int vzDomainIsUpdated(virDomainPtr domain)
1734
{
1735
    virDomainObjPtr dom;
1736 1737 1738

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

1742 1743
    virObjectUnlock(dom);
    return 0;
1744 1745
}

1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
static int vzConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                const char *type)
{
    /* 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;
}

1759 1760 1761 1762 1763 1764 1765
static int
vzNodeGetCPUStats(virConnectPtr conn ATTRIBUTE_UNUSED,
                  int cpuNum,
                  virNodeCPUStatsPtr params,
                  int *nparams,
                  unsigned int flags)
{
1766
    return virHostCPUGetStats(cpuNum, params, nparams, flags);
1767 1768 1769 1770 1771 1772 1773 1774 1775
}

static int
vzNodeGetMemoryStats(virConnectPtr conn ATTRIBUTE_UNUSED,
                     int cellNum,
                     virNodeMemoryStatsPtr params,
                     int *nparams,
                     unsigned int flags)
{
1776
    return virHostMemGetStats(cellNum, params, nparams, flags);
1777 1778 1779 1780 1781 1782 1783 1784
}

static int
vzNodeGetCellsFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED,
                         unsigned long long *freeMems,
                         int startCell,
                         int maxCells)
{
1785
    return virHostMemGetCellsFree(freeMems, startCell, maxCells);
1786 1787 1788 1789 1790 1791
}

static unsigned long long
vzNodeGetFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    unsigned long long freeMem;
1792
    if (virHostMemGetInfo(NULL, &freeMem) < 0)
1793 1794 1795 1796
        return 0;
    return freeMem;
}

1797 1798 1799 1800 1801 1802 1803 1804 1805
static int
vzConnectRegisterCloseCallback(virConnectPtr conn,
                               virConnectCloseFunc cb,
                               void *opaque,
                               virFreeCallback freecb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

1806 1807
    virObjectLock(privconn->driver);

1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
    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:
1819
    virObjectUnlock(privconn->driver);
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829

    return ret;
}

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

1830
    virObjectLock(privconn->driver);
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841

    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:
1842
    virObjectUnlock(privconn->driver);
1843 1844 1845 1846

    return ret;
}

1847 1848 1849 1850 1851
static int vzDomainSetMemoryFlagsImpl(virDomainPtr domain, unsigned long memory,
                                      unsigned int flags, bool useflags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
1852
    bool job = false;
1853 1854 1855 1856

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1857
    if (!(dom = vzDomObjFromDomainRef(domain)))
1858 1859 1860 1861 1862
        return -1;

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

1863 1864 1865 1866
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1867 1868 1869
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1870 1871 1872 1873
    ret = prlsdkSetMemsize(dom, memory >> 10);

 cleanup:

1874 1875 1876
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
    return ret;
}

static int vzDomainSetMemoryFlags(virDomainPtr domain, unsigned long memory,
                                  unsigned int flags)
{
    return vzDomainSetMemoryFlagsImpl(domain, memory, flags, true);
}

static int vzDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
    return vzDomainSetMemoryFlagsImpl(domain, memory, 0, false);
}

1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
static virDomainSnapshotObjPtr
vzSnapObjFromName(virDomainSnapshotObjListPtr snapshots, const char *name)
{
    virDomainSnapshotObjPtr snap = NULL;
    snap = virDomainSnapshotFindByName(snapshots, name);
    if (!snap)
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("no domain snapshot with matching name '%s'"), name);

    return snap;
}

static virDomainSnapshotObjPtr
vzSnapObjFromSnapshot(virDomainSnapshotObjListPtr snapshots,
                      virDomainSnapshotPtr snapshot)
{
    return vzSnapObjFromName(snapshots, snapshot->name);
}

static int
vzCurrentSnapshotIterator(void *payload,
                              const void *name ATTRIBUTE_UNUSED,
                              void *data)
{
    virDomainSnapshotObjPtr snapshot = payload;
    virDomainSnapshotObjPtr *current = data;

    if (snapshot->def->current)
        *current = snapshot;

    return 0;
}

static virDomainSnapshotObjPtr
vzFindCurrentSnapshot(virDomainSnapshotObjListPtr snapshots)
{
    virDomainSnapshotObjPtr current = NULL;

    virDomainSnapshotForEach(snapshots, vzCurrentSnapshotIterator, &current);
    return current;
}

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

1943
    if (!(dom = vzDomObjFromDomainRef(domain)))
1944 1945 1946 1947 1948 1949 1950 1951 1952
        return -1;

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

    n = virDomainSnapshotObjListNum(snapshots, NULL, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
1953
    virDomainObjEndAPI(&dom);
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970

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

1971
    if (!(dom = vzDomObjFromDomainRef(domain)))
1972 1973 1974 1975 1976 1977 1978 1979 1980
        return -1;

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

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
1981
    virDomainObjEndAPI(&dom);
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997

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

1998
    if (!(dom = vzDomObjFromDomainRef(domain)))
1999 2000 2001 2002 2003 2004 2005 2006 2007
        return -1;

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

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2008
    virDomainObjEndAPI(&dom);
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024

    return n;
}

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

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

2025
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
        return NULL;

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

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

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

    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->driver->caps,
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2042
    virDomainObjEndAPI(&dom);
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057

    return xml;
}

static int
vzDomainSnapshotNumChildren(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

2058
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
        return -1;

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

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

    n = virDomainSnapshotObjListNum(snapshots, snap, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2071
    virDomainObjEndAPI(&dom);
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089

    return n;
}

static int
vzDomainSnapshotListChildrenNames(virDomainSnapshotPtr snapshot,
                                  char **names,
                                  int nameslen,
                                  unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

2090
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
        return -1;

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

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

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2103
    virDomainObjEndAPI(&dom);
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120

    return n;
}

static int
vzDomainSnapshotListAllChildren(virDomainSnapshotPtr snapshot,
                                virDomainSnapshotPtr **snaps,
                                unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

2121
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
        return -1;

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

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

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2134
    virDomainObjEndAPI(&dom);
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150

    return n;
}

static virDomainSnapshotPtr
vzDomainSnapshotLookupByName(virDomainPtr domain,
                             const char *name,
                             unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotPtr snapshot = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, NULL);

2151
    if (!(dom = vzDomObjFromDomainRef(domain)))
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
        return NULL;

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

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

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

 cleanup:
2163
    virDomainObjEndAPI(&dom);
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
    virDomainSnapshotObjListFree(snapshots);

    return snapshot;
}

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

    virCheckFlags(0, -1);

2178
    if (!(dom = vzDomObjFromDomainRef(domain)))
2179 2180 2181 2182 2183 2184 2185 2186 2187
        return -1;

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

    ret = vzFindCurrentSnapshot(snapshots) != NULL;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2188
    virDomainObjEndAPI(&dom);
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202

    return ret;
}

static virDomainSnapshotPtr
vzDomainSnapshotGetParent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotPtr parent = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, NULL);

2203
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
        return NULL;

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

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

    if (!snap->def->parent) {
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("snapshot '%s' does not have a parent"),
                       snap->def->name);
        goto cleanup;
    }

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2223
    virDomainObjEndAPI(&dom);
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237

    return parent;
}

static virDomainSnapshotPtr
vzDomainSnapshotCurrent(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotPtr snapshot = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;
    virDomainSnapshotObjPtr current;

    virCheckFlags(0, NULL);

2238
    if (!(dom = vzDomObjFromDomainRef(domain)))
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
        return NULL;

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

    if (!(current = vzFindCurrentSnapshot(snapshots))) {
        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);
2254
    virDomainObjEndAPI(&dom);
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268

    return snapshot;
}

static int
vzDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
    virDomainSnapshotObjListPtr snapshots = NULL;
    virDomainSnapshotObjPtr current;

    virCheckFlags(0, -1);

2269
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
        return -1;

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

    current = vzFindCurrentSnapshot(snapshots);
    ret = current && STREQ(snapshot->name, current->def->name);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2280
    virDomainObjEndAPI(&dom);
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295

    return ret;
}

static int
vzDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, -1);

2296
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
        return -1;

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

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

    ret = 1;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2309 2310
    virDomainObjEndAPI(&dom);

2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
    return ret;
}

static virDomainSnapshotPtr
vzDomainSnapshotCreateXML(virDomainPtr domain,
                          const char *xmlDesc,
                          unsigned int flags)
{
    virDomainSnapshotDefPtr def = NULL;
    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;
    virDomainSnapshotObjPtr current;
2327
    bool job = false;
2328 2329 2330

    virCheckFlags(0, NULL);

2331
    if (!(dom = vzDomObjFromDomainRef(domain)))
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
        return NULL;

    if (!(def = virDomainSnapshotDefParseString(xmlDesc, driver->caps,
                                                driver->xmlopt, parse_flags)))
        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;
    }

2350 2351 2352 2353
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2354 2355 2356
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
    /* snaphot name is ignored, it will be set to auto generated by sdk uuid */
    if (prlsdkCreateSnapshot(dom, def->description) < 0)
        goto cleanup;

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

    if (!(current = vzFindCurrentSnapshot(snapshots))) {
        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);
    virDomainSnapshotDefFree(def);
2376 2377 2378
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390

    return snapshot;
}

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

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN, -1);

2391
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2392 2393 2394 2395 2396
        return -1;

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

2397
    virDomainObjEndAPI(&dom);
2398 2399 2400 2401 2402 2403 2404 2405 2406

    return ret;
}

static int
vzDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
2407
    bool job = false;
2408 2409 2410 2411 2412 2413

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED, -1);

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

2414 2415 2416 2417
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2418 2419 2420
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2421 2422
    ret = prlsdkSwitchToSnapshot(dom, snapshot->name,
                                 flags & VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED);
2423 2424 2425 2426
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2427 2428 2429 2430

    return ret;
}

2431 2432 2433 2434 2435 2436
enum vzMigrationCookieFeatures {
    VZ_MIGRATION_COOKIE_SESSION_UUID  = (1 << 0),
    VZ_MIGRATION_COOKIE_DOMAIN_UUID = (1 << 1),
    VZ_MIGRATION_COOKIE_DOMAIN_NAME = (1 << 1),
};

2437 2438 2439
typedef struct _vzMigrationCookie vzMigrationCookie;
typedef vzMigrationCookie *vzMigrationCookiePtr;
struct _vzMigrationCookie {
2440 2441
    unsigned char *session_uuid;
    unsigned char *uuid;
2442 2443 2444 2445 2446 2447 2448 2449
    char *name;
};

static void
vzMigrationCookieFree(vzMigrationCookiePtr mig)
{
    if (!mig)
        return;
2450 2451 2452

    VIR_FREE(mig->session_uuid);
    VIR_FREE(mig->uuid);
2453 2454 2455 2456 2457
    VIR_FREE(mig->name);
    VIR_FREE(mig);
}

static int
2458 2459 2460 2461
vzBakeCookie(vzDriverPtr driver,
             virDomainObjPtr dom,
             char **cookieout, int *cookieoutlen,
             unsigned int flags)
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
{
    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);
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498

    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__");
    }

2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</vz-migration>\n");

    if (virBufferCheckError(&buf) < 0)
        return -1;

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

    return 0;
}

static vzMigrationCookiePtr
2512
vzEatCookie(const char *cookiein, int cookieinlen, unsigned int flags)
2513 2514 2515
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctx = NULL;
2516
    char *tmp = NULL;
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
    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;

2532 2533 2534 2535
    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))) {
2536
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2537 2538
                       _("missing or malformed session-uuid element "
                         "in migration data"));
2539 2540 2541 2542 2543
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2544 2545 2546 2547
    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))) {
2548
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2549
                       _("missing or malformed uuid element in migration data"));
2550 2551 2552 2553 2554
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2555 2556
    if ((flags & VZ_MIGRATION_COOKIE_DOMAIN_NAME)
        && !(mig->name = virXPathString("string(./name[1])", ctx))) {
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
        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;
}

2573 2574
#define VZ_MIGRATION_FLAGS         (VIR_MIGRATE_PAUSED |        \
                                    VIR_MIGRATE_PEER2PEER)
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610

#define VZ_MIGRATION_PARAMETERS                                 \
    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, \
    NULL

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;

    virCheckFlags(VZ_MIGRATION_FLAGS, NULL);

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

    /* 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"));
        goto cleanup;
    }

    if (!(dom = vzDomObjFromDomain(domain)))
        goto cleanup;

2611
    /* session uuid, domain uuid and domain name are for backward compat */
2612 2613 2614 2615
    if (vzBakeCookie(privconn->driver, dom, cookieout, cookieoutlen,
                     VZ_MIGRATION_COOKIE_SESSION_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_NAME) < 0)
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
        goto cleanup;

    xml = virDomainDefFormat(dom->def, privconn->driver->caps,
                             VIR_DOMAIN_XML_MIGRATABLE);

 cleanup:

    if (dom)
        virObjectUnlock(dom);
    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;
    }

    if (virAsprintf(&uri, "vzmigr://%s", hostname) < 0)
        goto cleanup;

 cleanup:
    VIR_FREE(hostname);
    return uri;
}

static int
vzDomainMigratePrepare3Params(virConnectPtr conn,
                              virTypedParameterPtr params,
                              int nparams,
2656 2657
                              const char *cookiein ATTRIBUTE_UNUSED,
                              int cookieinlen ATTRIBUTE_UNUSED,
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
                              char **cookieout,
                              int *cookieoutlen,
                              char **uri_out,
                              unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
    const char *miguri = NULL;
    const char *dname = NULL;
    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 ||
        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;

2684 2685 2686 2687 2688 2689
    /* 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)
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
        goto cleanup;

    ret = 0;

 cleanup:
    return ret;
}

static int
vzConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
2703
    case VIR_DRV_FEATURE_MIGRATION_P2P:
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746
        return 1;
    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
2747 2748 2749 2750 2751 2752
vzDomainMigratePerformStep(virDomainPtr domain,
                           virTypedParameterPtr params,
                           int nparams,
                           const char *cookiein,
                           int cookieinlen,
                           unsigned int flags)
2753 2754 2755
{
    int ret = -1;
    virDomainObjPtr dom = NULL;
2756
    vzDomObjPtr privdom;
2757 2758 2759 2760 2761
    virURIPtr vzuri = NULL;
    vzConnPtr privconn = domain->conn->privateData;
    const char *miguri = NULL;
    const char *dname = NULL;
    vzMigrationCookiePtr mig = NULL;
2762
    bool job = false;
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780

    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 ||
        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;
    }

2781 2782
    if (!(mig = vzEatCookie(cookiein, cookieinlen,
                            VZ_MIGRATION_COOKIE_SESSION_UUID)))
2783 2784
        goto cleanup;

2785
    if (!(dom = vzDomObjFromDomainRef(domain)))
2786 2787
        goto cleanup;

2788 2789 2790
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;
2791 2792
    privdom = dom->privateData;
    privdom->job.hasProgress = true;
2793

2794 2795 2796
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2797 2798 2799 2800 2801 2802 2803
    if (!(vzuri = vzParseVzURI(miguri)))
        goto cleanup;

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

    virDomainObjListRemove(privconn->driver->domains, dom);
2804
    virObjectLock(dom);
2805 2806 2807 2808

    ret = 0;

 cleanup:
2809 2810 2811
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2812 2813 2814 2815 2816 2817
    virURIFree(vzuri);
    vzMigrationCookieFree(mig);

    return ret;
}

2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 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 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
static int
vzDomainMigratePerformP2P(virDomainPtr domain,
                          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;

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

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

    if (!(dom_xml = vzDomainMigrateBegin3Params(domain, params, nparams,
                                                &cookieout, &cookieoutlen,
                                                flags)))
        goto done;

    cookiein = cookieout;
    cookieinlen = cookieoutlen;
    cookieout = NULL;
    cookieoutlen = 0;
    if (dconn->driver->domainMigratePrepare3Params
            (dconn, params, nparams, cookiein, cookieinlen,
             &cookieout, &cookieoutlen, &uri, flags) < 0)
        goto done;

    /* 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) {
        orig_err = virSaveLastError();
        goto finish;
    }

    VIR_FREE(cookiein);
    cookiein = cookieout;
    cookieinlen = cookieoutlen;
    cookieout = NULL;
    cookieoutlen = 0;
    if (vzDomainMigratePerformStep(domain, params, nparams, cookiein,
                                   cookieinlen, flags) < 0) {
        orig_err = virSaveLastError();
        goto finish;
    }

    cancelled = 0;

 finish:
    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, NULL) <= 0 &&
        virTypedParamsReplaceString(&params, &nparams,
                                    VIR_MIGRATE_PARAM_DEST_NAME,
                                    domain->name) < 0)
        goto done;

    ddomain = dconn->driver->domainMigrateFinish3Params(dconn, params, nparams,
                                                        NULL, 0, NULL, NULL,
                                                        flags, cancelled);
    if (ddomain)
        ret = 0;
    virObjectUnref(ddomain);

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

 done:
    if (orig_err) {
        virSetError(orig_err);
        virFreeError(orig_err);
    }
    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,
                              char **cookieout ATTRIBUTE_UNUSED,
                              int *cookieoutlen ATTRIBUTE_UNUSED,
                              unsigned int flags)
{
    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

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

    if (flags & VIR_MIGRATE_PEER2PEER)
        return vzDomainMigratePerformP2P(domain, dconnuri, params, nparams, flags);
    else
        return vzDomainMigratePerformStep(domain, params, nparams,
                                          cookiein, cookieinlen, flags);

}

2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
static virDomainPtr
vzDomainMigrateFinish3Params(virConnectPtr dconn,
                             virTypedParameterPtr params,
                             int nparams,
                             const char *cookiein ATTRIBUTE_UNUSED,
                             int cookieinlen ATTRIBUTE_UNUSED,
                             char **cookieout ATTRIBUTE_UNUSED,
                             int *cookieoutlen ATTRIBUTE_UNUSED,
                             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)
2955 2956 2957 2958
        return NULL;

    if (cancelled)
        return NULL;
2959 2960 2961

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &name) < 0)
2962
        return NULL;
2963 2964


2965
    if (!(dom = prlsdkAddDomainByName(driver, name)))
2966 2967 2968 2969 2970 2971 2972 2973 2974
        goto cleanup;

    domain = virGetDomain(dconn, dom->def->name, dom->def->uuid);
    if (domain)
        domain->id = dom->def->id;

 cleanup:
    /* In this situation we have to restore domain on source. But the migration
     * is already finished. */
2975
    if (!domain)
2976
        VIR_WARN("Can't provide domain '%s' after successfull migration.", name);
2977
    virDomainObjEndAPI(&dom);
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997
    return domain;
}

static int
vzDomainMigrateConfirm3Params(virDomainPtr domain ATTRIBUTE_UNUSED,
                              virTypedParameterPtr params,
                              int nparams,
                              const char *cookiein ATTRIBUTE_UNUSED,
                              int cookieinlen ATTRIBUTE_UNUSED,
                              unsigned int flags,
                              int cancelled ATTRIBUTE_UNUSED)
{
    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

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

    return 0;
}

2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
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;
    int ret;

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

    ret = vzDomainGetJobInfoImpl(dom, info);

    virObjectUnlock(dom);
    return ret;
}

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 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
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;

    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:
    virObjectUnlock(dom);

    return ret;
}

3106
static virHypervisorDriver vzHypervisorDriver = {
3107
    .name = "vz",
3108 3109 3110 3111
    .connectOpen = vzConnectOpen,            /* 0.10.0 */
    .connectClose = vzConnectClose,          /* 0.10.0 */
    .connectGetVersion = vzConnectGetVersion,   /* 0.10.0 */
    .connectGetHostname = vzConnectGetHostname,      /* 0.10.0 */
3112
    .connectGetSysinfo = vzConnectGetSysinfo, /* 1.3.4 */
3113
    .connectGetMaxVcpus = vzConnectGetMaxVcpus, /* 1.2.21 */
3114
    .nodeGetInfo = vzNodeGetInfo,      /* 0.10.0 */
3115 3116 3117 3118
    .nodeGetCPUStats = vzNodeGetCPUStats,      /* 1.2.21 */
    .nodeGetMemoryStats = vzNodeGetMemoryStats, /* 1.2.21 */
    .nodeGetCellsFreeMemory = vzNodeGetCellsFreeMemory, /* 1.2.21 */
    .nodeGetFreeMemory = vzNodeGetFreeMemory, /* 1.2.21 */
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
    .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 */
    .domainShutdown = vzDomainShutdown, /* 0.10.0 */
    .domainCreate = vzDomainCreate,    /* 0.10.0 */
    .domainCreateWithFlags = vzDomainCreateWithFlags, /* 1.2.10 */
P
Pavel Hrdina 已提交
3142
    .domainReboot = vzDomainReboot, /* 1.3.0 */
3143 3144 3145 3146 3147 3148 3149 3150 3151
    .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 */
3152 3153 3154
    .domainIsUpdated = vzDomainIsUpdated,     /* 1.2.21 */
    .domainGetVcpusFlags = vzDomainGetVcpusFlags, /* 1.2.21 */
    .domainGetMaxVcpus = vzDomainGetMaxVcpus, /* 1.2.21 */
3155
    .domainSetUserPassword = vzDomainSetUserPassword, /* 2.0.0 */
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
    .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 */
3166 3167 3168 3169
    .domainBlockStats = vzDomainBlockStats, /* 1.2.17 */
    .domainBlockStatsFlags = vzDomainBlockStatsFlags, /* 1.2.17 */
    .domainInterfaceStats = vzDomainInterfaceStats, /* 1.2.17 */
    .domainMemoryStats = vzDomainMemoryStats, /* 1.2.17 */
3170 3171
    .connectRegisterCloseCallback = vzConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = vzConnectUnregisterCloseCallback, /* 1.3.2 */
3172 3173
    .domainSetMemoryFlags = vzDomainSetMemoryFlags, /* 1.3.4 */
    .domainSetMemory = vzDomainSetMemory, /* 1.3.4 */
3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
    .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 */
3190 3191 3192 3193 3194 3195
    .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 已提交
3196
    .domainUpdateDeviceFlags = vzDomainUpdateDeviceFlags, /* 2.0.0 */
3197
    .domainGetJobInfo = vzDomainGetJobInfo, /* 2.2.0 */
3198
    .domainGetJobStats = vzDomainGetJobStats, /* 2.2.0 */
D
Dmitry Guryanov 已提交
3199 3200
};

3201
static virConnectDriver vzConnectDriver = {
3202
    .hypervisorDriver = &vzHypervisorDriver,
3203 3204
};

3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
static int
vzStateCleanup(void)
{
    virObjectUnref(vz_driver);
    vz_driver = NULL;
    virMutexDestroy(&vz_driver_lock);
    prlsdkDeinit();
    return 0;
}

static int
vzStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                  virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                  void *opaque ATTRIBUTE_UNUSED)
{
    if (prlsdkInit() < 0) {
        VIR_DEBUG("%s", _("Can't initialize Parallels SDK"));
        return -1;
    }

   if (virMutexInit(&vz_driver_lock) < 0)
        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();
    return 0;

 error:
    vzStateCleanup();
    return -1;
}

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

3244
/* Parallels domain type backward compatibility*/
3245
static virHypervisorDriver parallelsHypervisorDriver;
3246 3247
static virConnectDriver parallelsConnectDriver;

D
Dmitry Guryanov 已提交
3248
/**
3249
 * vzRegister:
D
Dmitry Guryanov 已提交
3250
 *
3251
 * Registers the vz driver
D
Dmitry Guryanov 已提交
3252 3253
 */
int
3254
vzRegister(void)
D
Dmitry Guryanov 已提交
3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
{
    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);

3266
    /* Backward compatibility with Parallels domain type */
3267 3268
    parallelsHypervisorDriver = vzHypervisorDriver;
    parallelsHypervisorDriver.name = "Parallels";
3269
    parallelsConnectDriver = vzConnectDriver;
3270
    parallelsConnectDriver.hypervisorDriver = &parallelsHypervisorDriver;
3271
    if (virRegisterConnectDriver(&parallelsConnectDriver, true) < 0)
D
Dmitry Guryanov 已提交
3272
        return -1;
D
Dmitry Guryanov 已提交
3273

3274
    if (virRegisterConnectDriver(&vzConnectDriver, true) < 0)
3275 3276
        return -1;

3277 3278 3279
    if (virRegisterStateDriver(&vzStateDriver) < 0)
        return -1;

D
Dmitry Guryanov 已提交
3280 3281
    return 0;
}