parallels_driver.c 53.2 KB
Newer Older
D
Dmitry Guryanov 已提交
1 2 3 4
/*
 * parallels_driver.c: core driver functions for managing
 * Parallels Cloud Server hosts
 *
5
 * Copyright (C) 2014 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 43
 * <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 <paths.h>
#include <pwd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/statvfs.h>

#include "datatypes.h"
44
#include "virerror.h"
45
#include "viralloc.h"
46
#include "virlog.h"
47
#include "vircommand.h"
D
Dmitry Guryanov 已提交
48
#include "configmake.h"
49
#include "virfile.h"
50
#include "virstoragefile.h"
D
Dmitry Guryanov 已提交
51
#include "nodeinfo.h"
52
#include "virstring.h"
53
#include "cpu/cpu.h"
D
Dmitry Guryanov 已提交
54 55

#include "parallels_driver.h"
56
#include "parallels_utils.h"
57
#include "parallels_sdk.h"
D
Dmitry Guryanov 已提交
58 59 60

#define VIR_FROM_THIS VIR_FROM_PARALLELS

61 62
VIR_LOG_INIT("parallels.parallels_driver");

D
Dmitry Guryanov 已提交
63
#define PRLCTL                      "prlctl"
64
#define PRLSRVCTL                   "prlsrvctl"
D
Dmitry Guryanov 已提交
65

66
static int parallelsConnectClose(virConnectPtr conn);
D
Dmitry Guryanov 已提交
67

68 69 70 71 72 73 74 75 76 77 78 79 80
static const char * parallelsGetDiskBusName(int bus) {
    switch (bus) {
    case VIR_DOMAIN_DISK_BUS_IDE:
        return "ide";
    case VIR_DOMAIN_DISK_BUS_SATA:
        return "sata";
    case VIR_DOMAIN_DISK_BUS_SCSI:
        return "scsi";
    default:
        return NULL;
    }
}

D
Dmitry Guryanov 已提交
81
void
D
Dmitry Guryanov 已提交
82 83 84 85 86
parallelsDriverLock(parallelsConnPtr driver)
{
    virMutexLock(&driver->lock);
}

D
Dmitry Guryanov 已提交
87
void
D
Dmitry Guryanov 已提交
88 89 90 91 92 93 94 95
parallelsDriverUnlock(parallelsConnPtr driver)
{
    virMutexUnlock(&driver->lock);
}

static virCapsPtr
parallelsBuildCapabilities(void)
{
96 97 98
    virCapsPtr caps = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUDataPtr data = NULL;
D
Dmitry Guryanov 已提交
99
    virCapsGuestPtr guest;
100
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
101

102
    if ((caps = virCapabilitiesNew(virArchFromHost(),
103
                                   false, false)) == NULL)
104
        return NULL;
D
Dmitry Guryanov 已提交
105 106

    if (nodeCapsInitNUMA(caps) < 0)
107
        goto error;
D
Dmitry Guryanov 已提交
108

109 110 111
    if ((guest = virCapabilitiesAddGuest(caps, "hvm",
                                         VIR_ARCH_X86_64,
                                         "parallels",
D
Dmitry Guryanov 已提交
112
                                         NULL, 0, NULL)) == NULL)
113
        goto error;
D
Dmitry Guryanov 已提交
114 115 116

    if (virCapabilitiesAddGuestDomain(guest,
                                      "parallels", NULL, NULL, 0, NULL) == NULL)
117
        goto error;
D
Dmitry Guryanov 已提交
118

119 120 121
    if ((guest = virCapabilitiesAddGuest(caps, "exe",
                                         VIR_ARCH_X86_64,
                                         "parallels",
122
                                         NULL, 0, NULL)) == NULL)
123
        goto error;
124 125 126

    if (virCapabilitiesAddGuestDomain(guest,
                                      "parallels", NULL, NULL, 0, NULL) == NULL)
127
        goto error;
128

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

132
    if (VIR_ALLOC(cpu) < 0)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        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;

    if (!(data = cpuNodeData(cpu->arch))
        || cpuDecode(cpu, data, NULL, 0, NULL) < 0) {
        goto cleanup;
    }

 cleanup:
    cpuDataFree(data);
D
Dmitry Guryanov 已提交
150 151
    return caps;

152
 error:
153
    virObjectUnref(caps);
154
    goto cleanup;
D
Dmitry Guryanov 已提交
155 156 157
}

static char *
158
parallelsConnectGetCapabilities(virConnectPtr conn)
D
Dmitry Guryanov 已提交
159 160 161 162 163
{
    parallelsConnPtr privconn = conn->privateData;
    char *xml;

    parallelsDriverLock(privconn);
164
    xml = virCapabilitiesFormatXML(privconn->caps);
D
Dmitry Guryanov 已提交
165 166 167 168
    parallelsDriverUnlock(privconn);
    return xml;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static int
parallelsDomainDefPostParse(virDomainDefPtr def ATTRIBUTE_UNUSED,
                            virCapsPtr caps ATTRIBUTE_UNUSED,
                            void *opaque ATTRIBUTE_UNUSED)
{
    return 0;
}


static int
parallelsDomainDeviceDefPostParse(virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
                                  const virDomainDef *def ATTRIBUTE_UNUSED,
                                  virCapsPtr caps ATTRIBUTE_UNUSED,
                                  void *opaque ATTRIBUTE_UNUSED)
{
    return 0;
}


188 189
virDomainDefParserConfig parallelsDomainDefParserConfig = {
    .macPrefix = {0x42, 0x1C, 0x00},
190 191
    .devicesPostParseCallback = parallelsDomainDeviceDefPostParse,
    .domainPostParseCallback = parallelsDomainDefPostParse,
192 193 194
};


D
Dmitry Guryanov 已提交
195 196 197 198 199
static int
parallelsOpenDefault(virConnectPtr conn)
{
    parallelsConnPtr privconn;

200
    if (VIR_ALLOC(privconn) < 0)
D
Dmitry Guryanov 已提交
201 202 203 204
        return VIR_DRV_OPEN_ERROR;
    if (virMutexInit(&privconn->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize mutex"));
205 206 207 208 209 210
        goto err_free;
    }

    if (prlsdkInit(privconn)) {
        VIR_DEBUG("%s", _("Can't initialize Parallels SDK"));
        goto err_free;
D
Dmitry Guryanov 已提交
211 212
    }

213 214 215
    if (prlsdkConnect(privconn) < 0)
        goto err_free;

D
Dmitry Guryanov 已提交
216 217 218
    if (!(privconn->caps = parallelsBuildCapabilities()))
        goto error;

219 220
    if (!(privconn->xmlopt = virDomainXMLOptionNew(&parallelsDomainDefParserConfig,
                                                 NULL, NULL)))
221 222
        goto error;

223
    if (!(privconn->domains = virDomainObjListNew()))
D
Dmitry Guryanov 已提交
224 225
        goto error;

226 227 228 229 230 231
    if (!(privconn->domainEventState = virObjectEventStateNew()))
        goto error;

    if (prlsdkSubscribeToPCSEvents(privconn))
        goto error;

D
Dmitry Guryanov 已提交
232 233
    conn->privateData = privconn;

234
    if (prlsdkLoadDomains(privconn))
235 236
        goto error;

D
Dmitry Guryanov 已提交
237 238
    return VIR_DRV_OPEN_SUCCESS;

239
 error:
240
    virObjectUnref(privconn->domains);
241
    virObjectUnref(privconn->caps);
D
Dmitry Guryanov 已提交
242
    virStoragePoolObjListFree(&privconn->pools);
243
    virObjectEventStateFree(privconn->domainEventState);
244 245 246
    prlsdkDisconnect(privconn);
    prlsdkDeinit();
 err_free:
D
Dmitry Guryanov 已提交
247 248 249 250 251
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
}

static virDrvOpenStatus
252 253 254
parallelsConnectOpen(virConnectPtr conn,
                     virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                     unsigned int flags)
D
Dmitry Guryanov 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
{
    int ret;

    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    if (!conn->uri)
        return VIR_DRV_OPEN_DECLINED;

    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "parallels"))
        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. */
271 272 273 274
    if (!STREQ_NULLABLE(conn->uri->path, "/system")) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unexpected Parallels URI path '%s', try parallels:///system"),
                       conn->uri->path);
D
Dmitry Guryanov 已提交
275 276 277
        return VIR_DRV_OPEN_ERROR;
    }

278
    if ((ret = parallelsOpenDefault(conn)) != VIR_DRV_OPEN_SUCCESS)
D
Dmitry Guryanov 已提交
279 280 281 282 283 284
        return ret;

    return VIR_DRV_OPEN_SUCCESS;
}

static int
285
parallelsConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
286 287 288 289
{
    parallelsConnPtr privconn = conn->privateData;

    parallelsDriverLock(privconn);
290
    prlsdkUnsubscribeFromPCSEvents(privconn);
291
    virObjectUnref(privconn->caps);
292
    virObjectUnref(privconn->xmlopt);
293
    virObjectUnref(privconn->domains);
294
    virObjectEventStateFree(privconn->domainEventState);
295
    prlsdkDisconnect(privconn);
D
Dmitry Guryanov 已提交
296
    conn->privateData = NULL;
297
    prlsdkDeinit();
D
Dmitry Guryanov 已提交
298 299 300 301 302 303 304 305 306

    parallelsDriverUnlock(privconn);
    virMutexDestroy(&privconn->lock);

    VIR_FREE(privconn);
    return 0;
}

static int
307
parallelsConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
308
{
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    char *output, *sVer, *tmp;
    const char *searchStr = "prlsrvctl version ";
    int ret = -1;

    output = parallelsGetOutput(PRLSRVCTL, "--help", NULL);

    if (!output) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(sVer = strstr(output, searchStr))) {
        parallelsParseError();
        goto cleanup;
    }

    sVer = sVer + strlen(searchStr);

    /* parallels server has versions number like 6.0.17977.782218,
     * so libvirt can handle only first two numbers. */
    if (!(tmp = strchr(sVer, '.'))) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(tmp = strchr(tmp + 1, '.'))) {
        parallelsParseError();
        goto cleanup;
    }

    tmp[0] = '\0';
    if (virParseVersionString(sVer, hvVer, true) < 0) {
        parallelsParseError();
        goto cleanup;
    }

    ret = 0;

347
 cleanup:
348 349 350 351
    VIR_FREE(output);
    return ret;
}

352 353 354 355 356 357 358

static char *parallelsConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


359
static int
360
parallelsConnectListDomains(virConnectPtr conn, int *ids, int maxids)
361 362 363 364 365
{
    parallelsConnPtr privconn = conn->privateData;
    int n;

    parallelsDriverLock(privconn);
366 367
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                     NULL, NULL);
368 369 370 371 372 373
    parallelsDriverUnlock(privconn);

    return n;
}

static int
374
parallelsConnectNumOfDomains(virConnectPtr conn)
375 376 377 378 379
{
    parallelsConnPtr privconn = conn->privateData;
    int count;

    parallelsDriverLock(privconn);
380 381
    count = virDomainObjListNumOfDomains(privconn->domains, true,
                                         NULL, NULL);
382 383 384 385 386 387
    parallelsDriverUnlock(privconn);

    return count;
}

static int
388
parallelsConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
389 390 391 392 393 394
{
    parallelsConnPtr privconn = conn->privateData;
    int n;

    parallelsDriverLock(privconn);
    memset(names, 0, sizeof(*names) * maxnames);
395
    n = virDomainObjListGetInactiveNames(privconn->domains, names,
396
                                         maxnames, NULL, NULL);
397 398 399 400 401 402
    parallelsDriverUnlock(privconn);

    return n;
}

static int
403
parallelsConnectNumOfDefinedDomains(virConnectPtr conn)
404 405 406 407 408
{
    parallelsConnPtr privconn = conn->privateData;
    int count;

    parallelsDriverLock(privconn);
409 410
    count = virDomainObjListNumOfDomains(privconn->domains, false,
                                         NULL, NULL);
411 412 413 414 415 416
    parallelsDriverUnlock(privconn);

    return count;
}

static int
417 418 419
parallelsConnectListAllDomains(virConnectPtr conn,
                               virDomainPtr **domains,
                               unsigned int flags)
420 421 422 423
{
    parallelsConnPtr privconn = conn->privateData;
    int ret = -1;

O
Osier Yang 已提交
424
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
425
    parallelsDriverLock(privconn);
426 427
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
428 429 430 431 432 433
    parallelsDriverUnlock(privconn);

    return ret;
}

static virDomainPtr
434
parallelsDomainLookupByID(virConnectPtr conn, int id)
435 436 437 438 439 440
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

    parallelsDriverLock(privconn);
441
    dom = virDomainObjListFindByID(privconn->domains, id);
442 443 444 445 446 447 448 449 450 451 452
    parallelsDriverUnlock(privconn);

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }

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

453
 cleanup:
454
    if (dom)
455
        virObjectUnlock(dom);
456 457 458 459
    return ret;
}

static virDomainPtr
460
parallelsDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
461 462 463 464 465 466
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

    parallelsDriverLock(privconn);
467
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
468 469 470 471 472 473 474 475 476 477 478 479 480 481
    parallelsDriverUnlock(privconn);

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

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

482
 cleanup:
483
    if (dom)
484
        virObjectUnlock(dom);
485 486 487 488
    return ret;
}

static virDomainPtr
489
parallelsDomainLookupByName(virConnectPtr conn, const char *name)
490 491 492 493 494 495
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

    parallelsDriverLock(privconn);
496
    dom = virDomainObjListFindByName(privconn->domains, name);
497 498 499 500 501 502 503 504 505 506 507 508
    parallelsDriverUnlock(privconn);

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
        goto cleanup;
    }

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

509
 cleanup:
510
    if (dom)
511
        virObjectUnlock(dom);
512 513 514 515
    return ret;
}

static int
516
parallelsDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
517 518 519 520 521 522
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    parallelsDriverLock(privconn);
523
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
524 525 526 527 528 529 530 531 532 533 534 535 536 537
    parallelsDriverUnlock(privconn);

    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    info->state = virDomainObjGetState(privdom, NULL);
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = 0;
    ret = 0;

538
 cleanup:
539
    if (privdom)
540
        virObjectUnlock(privdom);
541 542 543 544
    return ret;
}

static char *
545
parallelsDomainGetOSType(virDomainPtr domain)
546 547 548 549 550 551 552
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;

    char *ret = NULL;

    parallelsDriverLock(privconn);
553
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
554 555 556 557 558
    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

559
    ignore_value(VIR_STRDUP(ret, privdom->def->os.type));
560

561
 cleanup:
562
    if (privdom)
563
        virObjectUnlock(privdom);
564 565 566 567 568 569 570 571 572 573 574 575
    parallelsDriverUnlock(privconn);
    return ret;
}

static int
parallelsDomainIsPersistent(virDomainPtr domain)
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    parallelsDriverLock(privconn);
576
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
577 578 579 580 581 582 583
    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    ret = 1;

584
 cleanup:
585
    if (privdom)
586
        virObjectUnlock(privdom);
587 588 589 590 591 592 593 594 595 596 597 598 599 600
    parallelsDriverUnlock(privconn);
    return ret;
}

static int
parallelsDomainGetState(virDomainPtr domain,
                  int *state, int *reason, unsigned int flags)
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;
    virCheckFlags(0, -1);

    parallelsDriverLock(privconn);
601
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
602 603 604 605 606 607 608 609 610 611
    parallelsDriverUnlock(privconn);

    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    *state = virDomainObjGetState(privdom, reason);
    ret = 0;

612
 cleanup:
613
    if (privdom)
614
        virObjectUnlock(privdom);
615 616 617 618 619 620 621 622 623 624 625 626 627 628
    return ret;
}

static char *
parallelsDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainDefPtr def;
    virDomainObjPtr privdom;
    char *ret = NULL;

    /* Flags checked by virDomainDefFormat */

    parallelsDriverLock(privconn);
629
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
630 631 632 633 634 635 636 637 638 639 640 641
    parallelsDriverUnlock(privconn);

    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;

    ret = virDomainDefFormat(def, flags);

642
 cleanup:
643
    if (privdom)
644
        virObjectUnlock(privdom);
645 646 647 648 649 650 651 652 653 654 655
    return ret;
}

static int
parallelsDomainGetAutostart(virDomainPtr domain, int *autostart)
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    parallelsDriverLock(privconn);
656
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
657 658 659 660 661 662 663 664 665 666
    parallelsDriverUnlock(privconn);

    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    *autostart = privdom->autostart;
    ret = 0;

667
 cleanup:
668
    if (privdom)
669
        virObjectUnlock(privdom);
670
    return ret;
D
Dmitry Guryanov 已提交
671 672
}

673
typedef int (*parallelsChangeStateFunc)(virDomainObjPtr privdom);
674 675 676 677 678 679 680 681 682 683 684 685 686 687
#define PARALLELS_UUID(x)     (((parallelsDomObjPtr)(x->privateData))->uuid)

static int
parallelsDomainChangeState(virDomainPtr domain,
                           virDomainState req_state, const char *req_state_name,
                           parallelsChangeStateFunc chstate,
                           virDomainState new_state, int reason)
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int state;
    int ret = -1;

    parallelsDriverLock(privconn);
688
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    parallelsDriverUnlock(privconn);

    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    state = virDomainObjGetState(privdom, NULL);
    if (state != req_state) {
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not %s"),
                       privdom->def->name, req_state_name);
        goto cleanup;
    }

    if (chstate(privdom))
        goto cleanup;

    virDomainObjSetState(privdom, new_state, reason);

    ret = 0;

710
 cleanup:
711
    if (privdom)
712
        virObjectUnlock(privdom);
713 714 715 716 717 718 719 720 721 722

    return ret;
}

static int parallelsPause(virDomainObjPtr privdom)
{
    return parallelsCmdRun(PRLCTL, "pause", PARALLELS_UUID(privdom), NULL);
}

static int
723
parallelsDomainSuspend(virDomainPtr domain)
724 725 726 727 728 729 730 731 732 733 734 735 736
{
    return parallelsDomainChangeState(domain,
                                      VIR_DOMAIN_RUNNING, "running",
                                      parallelsPause,
                                      VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
}

static int parallelsResume(virDomainObjPtr privdom)
{
    return parallelsCmdRun(PRLCTL, "resume", PARALLELS_UUID(privdom), NULL);
}

static int
737
parallelsDomainResume(virDomainPtr domain)
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
{
    return parallelsDomainChangeState(domain,
                                      VIR_DOMAIN_PAUSED, "paused",
                                      parallelsResume,
                                      VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_UNPAUSED);
}

static int parallelsStart(virDomainObjPtr privdom)
{
    return parallelsCmdRun(PRLCTL, "start", PARALLELS_UUID(privdom), NULL);
}

static int
parallelsDomainCreate(virDomainPtr domain)
{
    return parallelsDomainChangeState(domain,
                                      VIR_DOMAIN_SHUTOFF, "stopped",
                                      parallelsStart,
                                      VIR_DOMAIN_RUNNING, VIR_DOMAIN_EVENT_STARTED_BOOTED);
}

static int parallelsKill(virDomainObjPtr privdom)
{
    return parallelsCmdRun(PRLCTL, "stop", PARALLELS_UUID(privdom), "--kill", NULL);
}

static int
765
parallelsDomainDestroy(virDomainPtr domain)
766 767 768 769 770 771 772 773 774 775 776 777 778
{
    return parallelsDomainChangeState(domain,
                                      VIR_DOMAIN_RUNNING, "running",
                                      parallelsKill,
                                      VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_DESTROYED);
}

static int parallelsStop(virDomainObjPtr privdom)
{
    return parallelsCmdRun(PRLCTL, "stop", PARALLELS_UUID(privdom), NULL);
}

static int
779
parallelsDomainShutdown(virDomainPtr domain)
780 781 782 783 784 785 786
{
    return parallelsDomainChangeState(domain,
                                      VIR_DOMAIN_RUNNING, "running",
                                      parallelsStop,
                                      VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
}

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
static int
parallelsApplyGraphicsParams(virDomainGraphicsDefPtr *oldgraphics, int nold,
                             virDomainGraphicsDefPtr *newgraphics, int nnew)
{
    virDomainGraphicsDefPtr new, old;

    /* parallels server supports only 1 VNC display per VM */
    if (nold != nnew || nnew > 1)
        goto error;

    if (nnew == 0)
        return 0;

    if (newgraphics[0]->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC)
        goto error;

    old = oldgraphics[0];
    new = newgraphics[0];

    if (old->data.vnc.port != new->data.vnc.port &&
807
        (old->data.vnc.port != 0 && new->data.vnc.port != 0)) {
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828

        goto error;
    } else if (old->data.vnc.autoport != new->data.vnc.autoport ||
        new->data.vnc.keymap != NULL ||
        new->data.vnc.socket != NULL ||
        !STREQ_NULLABLE(old->data.vnc.auth.passwd, new->data.vnc.auth.passwd) ||
        old->data.vnc.auth.expires != new->data.vnc.auth.expires ||
        old->data.vnc.auth.validTo != new->data.vnc.auth.validTo ||
        old->data.vnc.auth.connected != new->data.vnc.auth.connected) {

        goto error;
    } else if (old->nListens != new->nListens ||
               new->nListens > 1 ||
               old->listens[0].type != new->listens[0].type ||
                 !STREQ_NULLABLE(old->listens[0].address, new->listens[0].address) ||
                 !STREQ_NULLABLE(old->listens[0].network, new->listens[0].network)) {

        goto error;
    }

    return 0;
829
 error:
830
    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
831 832 833 834 835 836 837 838 839
                   _("changing display parameters is not supported "
                     "by parallels driver"));
    return -1;
}

static int
parallelsApplySerialParams(virDomainChrDefPtr *oldserials, int nold,
                           virDomainChrDefPtr *newserials, int nnew)
{
840
    size_t i, j;
841

842 843 844
    if (nold != nnew)
        goto error;

845
    for (i = 0; i < nold; i++) {
846 847 848
        virDomainChrDefPtr oldserial = oldserials[i];
        virDomainChrDefPtr newserial = NULL;

849
        for (j = 0; j < nnew; j++) {
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
            if (newserials[j]->target.port == oldserial->target.port) {
                newserial = newserials[j];
                break;
            }
        }

        if (!newserial)
            goto error;

        if (oldserial->source.type != newserial->source.type)
            goto error;

        if ((newserial->source.type == VIR_DOMAIN_CHR_TYPE_DEV ||
            newserial->source.type == VIR_DOMAIN_CHR_TYPE_FILE) &&
            !STREQ_NULLABLE(oldserial->source.data.file.path,
                            newserial->source.data.file.path))
            goto error;
867
        if (newserial->source.type == VIR_DOMAIN_CHR_TYPE_UNIX &&
868 869 870 871 872 873 874 875 876
           (!STREQ_NULLABLE(oldserial->source.data.nix.path,
                            newserial->source.data.nix.path) ||
            oldserial->source.data.nix.listen == newserial->source.data.nix.listen)) {

            goto error;
        }
    }

    return 0;
877
 error:
878
    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
879 880 881 882 883 884 885 886 887 888 889 890 891 892
                   _("changing serial device parameters is "
                     "not supported by parallels driver"));
    return -1;
}

static int
parallelsApplyVideoParams(parallelsDomObjPtr pdom,
                          virDomainVideoDefPtr *oldvideos, int nold,
                           virDomainVideoDefPtr *newvideos, int nnew)
{
    virDomainVideoDefPtr old, new;
    char str_vram[32];

    if (nold != 1 || nnew != 1) {
893
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
894 895 896 897 898 899 900 901
                       _("Only one video device is "
                         "supported by parallels driver"));
        return -1;
    }

    old = oldvideos[0];
    new = newvideos[0];
    if (new->type != VIR_DOMAIN_VIDEO_TYPE_VGA) {
902
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
903 904 905 906 907 908
                       _("Only VGA video device is "
                         "supported by parallels driver"));
        return -1;
    }

    if (new->heads != 1) {
909
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
910 911 912 913 914 915 916 917 918
                       _("Only one monitor is supported by parallels driver"));
        return -1;
    }

    /* old->accel must be always non-NULL */
    if (new->accel == NULL ||
        old->accel->support2d != new->accel->support2d ||
        old->accel->support3d != new->accel->support3d) {

919
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
920 921 922
                   _("Changing video acceleration parameters is "
                     "not supported by parallels driver"));
        return -1;
923

924 925 926
    }

    if (old->vram != new->vram) {
927
        if (new->vram % (1 << 10) != 0) {
928
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
929 930 931 932
                       _("Video RAM size should be multiple of 1Mb."));
            return -1;
        }

933
        snprintf(str_vram, 31, "%dK", new->vram);
934 935 936 937 938 939 940 941 942
        str_vram[31] = '\0';

        if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                            "--videosize", str_vram, NULL))
            return -1;
    }
    return 0;
}

D
Dmitry Guryanov 已提交
943 944
static int parallelsAddHdd(parallelsDomObjPtr pdom,
                           virDomainDiskDefPtr disk)
945 946
{
    int ret = -1;
D
Dmitry Guryanov 已提交
947 948
    const char *src = virDomainDiskGetSource(disk);
    int type = virDomainDiskGetType(disk);
949 950 951 952
    const char *strbus;

    virCommandPtr cmd = virCommandNewArgList(PRLCTL, "set", pdom->uuid,
                                             "--device-add", "hdd", NULL);
D
Dmitry Guryanov 已提交
953 954 955 956 957

    if (type == VIR_STORAGE_TYPE_FILE) {
        int format = virDomainDiskGetFormat(disk);

        if (format != VIR_STORAGE_FILE_PLOOP) {
958
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
D
Dmitry Guryanov 已提交
959 960 961 962 963 964 965 966
                           _("Invalid disk format: %d"), type);
            goto cleanup;
        }

        virCommandAddArg(cmd, "--image");
    } else if (VIR_STORAGE_TYPE_BLOCK) {
        virCommandAddArg(cmd, "--device");
    } else {
967
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
D
Dmitry Guryanov 已提交
968 969 970 971 972
                       _("Invalid disk type: %d"), type);
        goto cleanup;
    }

    virCommandAddArg(cmd, src);
973 974

    if (!(strbus = parallelsGetDiskBusName(disk->bus))) {
975
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
976 977 978 979 980 981 982 983 984 985
                       _("Invalid disk bus: %d"), disk->bus);
        goto cleanup;
    }

    virCommandAddArgFormat(cmd, "--iface=%s", strbus);

    if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE)
        virCommandAddArgFormat(cmd, "--position=%d",
                               disk->info.addr.drive.target);

986
    if (virCommandRun(cmd, NULL) < 0)
987 988 989 990
        goto cleanup;

    ret = 0;

991
 cleanup:
D
Dmitry Guryanov 已提交
992
    virCommandFree(cmd);
993 994 995
    return ret;
}

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
static int parallelsRemoveHdd(parallelsDomObjPtr pdom,
                              virDomainDiskDefPtr disk)
{
    char prlname[16];

    prlname[15] = '\0';
    snprintf(prlname, 15, "hdd%d", virDiskNameToIndex(disk->dst));

    if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                        "--device-del", prlname,
                        "--detach-only", NULL))
        return -1;

    return 0;
}

1012
static int
D
Dmitry Guryanov 已提交
1013
parallelsApplyDisksParams(parallelsDomObjPtr pdom,
1014 1015 1016
                          virDomainDiskDefPtr *olddisks, int nold,
                          virDomainDiskDefPtr *newdisks, int nnew)
{
1017
    size_t i, j;
1018

1019
    for (i = 0; i < nold; i++) {
1020 1021
        virDomainDiskDefPtr newdisk = NULL;
        virDomainDiskDefPtr olddisk = olddisks[i];
1022
        for (j = 0; j < nnew; j++) {
1023 1024 1025 1026 1027 1028 1029
            if (STREQ_NULLABLE(newdisks[j]->dst, olddisk->dst)) {
                newdisk = newdisks[j];
                break;
            }
        }

        if (!newdisk) {
1030
            if (parallelsRemoveHdd(pdom, olddisk)) {
1031
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1032 1033 1034 1035 1036 1037
                               _("Can't remove disk '%s' "
                                 "in the specified config"), olddisks[i]->serial);
                return -1;
            }

            continue;
1038 1039 1040 1041
        }

        if (olddisk->bus != newdisk->bus ||
            olddisk->info.addr.drive.target != newdisk->info.addr.drive.target ||
1042 1043
            !STREQ_NULLABLE(virDomainDiskGetSource(olddisk),
                            virDomainDiskGetSource(newdisk))) {
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

            char prlname[16];
            char strpos[16];
            const char *strbus;

            prlname[15] = '\0';
            snprintf(prlname, 15, "hdd%d", virDiskNameToIndex(newdisk->dst));

            strpos[15] = '\0';
            snprintf(strpos, 15, "%d", newdisk->info.addr.drive.target);

1055
            if (!(strbus = parallelsGetDiskBusName(newdisk->bus))) {
1056
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1057
                               _("Unsupported disk bus: %d"), newdisk->bus);
1058
                return -1;
1059
            }
1060

1061 1062 1063 1064 1065 1066 1067 1068 1069
            if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                                "--device-set", prlname,
                                "--iface", strbus,
                                "--position", strpos,
                                "--image", newdisk->src, NULL))
                return -1;
        }
    }

1070 1071 1072 1073 1074 1075 1076 1077 1078
    for (i = 0; i < nnew; i++) {
        virDomainDiskDefPtr newdisk = newdisks[i];
        bool found = false;
        for (j = 0; j < nold; j++)
            if (STREQ_NULLABLE(olddisks[j]->dst, newdisk->dst))
                found = true;
        if (found)
            continue;

D
Dmitry Guryanov 已提交
1079
        if (parallelsAddHdd(pdom, newdisk))
1080 1081 1082
            return -1;
    }

1083 1084 1085
    return 0;
}

1086 1087 1088 1089 1090 1091
static int parallelsApplyIfaceParams(parallelsDomObjPtr pdom,
                                     virDomainNetDefPtr oldnet,
                                     virDomainNetDefPtr newnet)
{
    bool create = false;
    bool is_changed = false;
1092
    virCommandPtr cmd = NULL;
1093
    char strmac[VIR_MAC_STRING_BUFLEN];
1094
    size_t i;
1095
    int ret = -1;
1096 1097 1098

    if (!oldnet) {
        create = true;
1099
        if (VIR_ALLOC(oldnet) < 0)
1100 1101 1102 1103
            return -1;
    }

    if (!create && oldnet->type != newnet->type) {
1104
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1105
                       _("Changing network type is not supported"));
1106
        goto cleanup;
1107 1108 1109
    }

    if (!STREQ_NULLABLE(oldnet->model, newnet->model)) {
1110
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1111
                       _("Changing network device model is not supported"));
1112
        goto cleanup;
1113 1114 1115 1116
    }

    if (!STREQ_NULLABLE(oldnet->data.network.portgroup,
                        newnet->data.network.portgroup)) {
1117
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1118
                       _("Changing network portgroup is not supported"));
1119
        goto cleanup;
1120 1121 1122 1123
    }

    if (!virNetDevVPortProfileEqual(oldnet->virtPortProfile,
                                    newnet->virtPortProfile)) {
1124
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1125
                       _("Changing virtual port profile is not supported"));
1126
        goto cleanup;
1127 1128 1129
    }

    if (newnet->tune.sndbuf_specified) {
1130
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1131
                       _("Setting send buffer size is not supported"));
1132
        goto cleanup;
1133 1134 1135
    }

    if (!STREQ_NULLABLE(oldnet->script, newnet->script)) {
1136
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1137
                       _("Setting startup script is not supported"));
1138
        goto cleanup;
1139 1140 1141
    }

    if (!STREQ_NULLABLE(oldnet->filter, newnet->filter)) {
1142
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1143
                       _("Changing filter params is not supported"));
1144
        goto cleanup;
1145 1146 1147
    }

    if (newnet->bandwidth != NULL) {
1148
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1149
                       _("Setting bandwidth params is not supported"));
1150
        goto cleanup;
1151 1152 1153 1154
    }

    for (i = 0; i < sizeof(newnet->vlan); i++) {
        if (((char *)&newnet->vlan)[i] != 0) {
1155
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1156
                           _("Setting vlan params is not supported"));
1157
            goto cleanup;
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
        }
    }

    /* Here we know, that there are no differences, that are forbidden.
     * Check is something changed, if no - do nothing */

    if (create) {
        cmd = virCommandNewArgList(PRLCTL, "set", pdom->uuid,
                                   "--device-add", "net", NULL);
    } else {
        cmd = virCommandNewArgList(PRLCTL, "set", pdom->uuid,
                                   "--device-set", newnet->ifname, NULL);
    }

    if (virMacAddrCmp(&oldnet->mac, &newnet->mac)) {
        virMacAddrFormat(&newnet->mac, strmac);
        virCommandAddArgFormat(cmd, "--mac=%s", strmac);
        is_changed = true;
    }

    if (!STREQ_NULLABLE(oldnet->data.network.name, newnet->data.network.name)) {
1179 1180 1181 1182 1183 1184 1185 1186
        if (STREQ_NULLABLE(newnet->data.network.name,
                           PARALLELS_ROUTED_NETWORK_NAME)) {
            virCommandAddArgFormat(cmd, "--type=routed");
        } else {
            virCommandAddArgFormat(cmd, "--network=%s",
                                   newnet->data.network.name);
        }

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
        is_changed = true;
    }

    if (oldnet->linkstate != newnet->linkstate) {
        if (newnet->linkstate == VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP) {
            virCommandAddArgFormat(cmd, "--connect");
        } else if (newnet->linkstate == VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN) {
            virCommandAddArgFormat(cmd, "--disconnect");
        }
        is_changed = true;
    }

    if (!create && !is_changed) {
        /* nothing changed - no need to run prlctl */
1201 1202
        ret = 0;
        goto cleanup;
1203 1204
    }

1205 1206
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;
1207

1208 1209
    ret = 0;

1210
 cleanup:
1211 1212 1213 1214
    if (create)
        VIR_FREE(oldnet);
    virCommandFree(cmd);
    return ret;
1215 1216 1217 1218 1219 1220 1221
}

static int
parallelsApplyIfacesParams(parallelsDomObjPtr pdom,
                            virDomainNetDefPtr *oldnets, int nold,
                            virDomainNetDefPtr *newnets, int nnew)
{
1222
    size_t i, j;
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    virDomainNetDefPtr newnet;
    virDomainNetDefPtr oldnet;
    bool found;

    for (i = 0; i < nold; i++) {
        newnet = NULL;
        oldnet = oldnets[i];
        for (j = 0; j < nnew; j++) {
            if (STREQ_NULLABLE(newnets[j]->ifname, oldnet->ifname)) {
                newnet = newnets[j];
                break;
            }
        }

        if (!newnet) {
            if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                                "--device-del", oldnet->ifname, NULL) < 0)
                return -1;

            continue;
        }

        if (parallelsApplyIfaceParams(pdom, oldnet, newnet) < 0)
            return -1;
    }

    for (i = 0; i < nnew; i++) {
        newnet = newnets[i];
        found = false;

        for (j = 0; j < nold; j++)
            if (STREQ_NULLABLE(oldnets[j]->ifname, newnet->ifname))
                found = true;
        if (found)
            continue;

        if (parallelsApplyIfaceParams(pdom, NULL, newnet))
            return -1;
    }

    return 0;
}

1266
static int
D
Dmitry Guryanov 已提交
1267
parallelsApplyChanges(virDomainObjPtr dom, virDomainDefPtr new)
1268 1269
{
    char buf[32];
1270
    size_t i;
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

    virDomainDefPtr old = dom->def;
    parallelsDomObjPtr pdom = dom->privateData;

    if (new->description && !STREQ_NULLABLE(old->description, new->description)) {
        if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                            "--description", new->description, NULL))
            return -1;
    }

    if (new->name && !STREQ_NULLABLE(old->name, new->name)) {
        if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                            "--name", new->name, NULL))
            return -1;
    }

    if (new->title && !STREQ_NULLABLE(old->title, new->title)) {
1288
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1289 1290 1291 1292 1293
                       _("titles are not supported by parallels driver"));
        return -1;
    }

    if (new->blkio.ndevices > 0) {
1294
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1295 1296 1297 1298 1299 1300 1301
                       _("blkio parameters are not supported "
                         "by parallels driver"));
        return -1;
    }

    if (old->mem.max_balloon != new->mem.max_balloon) {
        if (new->mem.max_balloon != new->mem.cur_balloon) {
1302
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1303 1304 1305 1306 1307 1308
                       _("changing balloon parameters is not supported "
                         "by parallels driver"));
           return -1;
        }

        if (new->mem.max_balloon % (1 << 10) != 0) {
1309
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
                       _("Memory size should be multiple of 1Mb."));
            return -1;
        }

        snprintf(buf, 31, "%llu", new->mem.max_balloon >> 10);
        buf[31] = '\0';

        if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                            "--memsize", buf, NULL))
            return -1;
    }

1322
    if (old->mem.nhugepages != new->mem.nhugepages ||
1323 1324 1325 1326 1327
        old->mem.hard_limit != new->mem.hard_limit ||
        old->mem.soft_limit != new->mem.soft_limit ||
        old->mem.min_guarantee != new->mem.min_guarantee ||
        old->mem.swap_hard_limit != new->mem.swap_hard_limit) {

1328
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1329 1330 1331 1332 1333 1334 1335
                       _("Memory parameter is not supported "
                         "by parallels driver"));
        return -1;
    }

    if (old->vcpus != new->vcpus) {
        if (new->vcpus != new->maxvcpus) {
1336
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
                       _("current vcpus must be equal to maxvcpus"));
            return -1;
        }

        snprintf(buf, 31, "%d", new->vcpus);
        buf[31] = '\0';

        if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                            "--cpus", buf, NULL))
            return -1;
    }

    if (old->placement_mode != new->placement_mode) {
1350
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1351 1352 1353 1354 1355
                       _("changing cpu placement mode is not supported "
                         "by parallels driver"));
        return -1;
    }

1356 1357 1358
    if ((old->cpumask != NULL || new->cpumask != NULL) &&
        (old->cpumask == NULL || new->cpumask == NULL ||
        !virBitmapEqual(old->cpumask, new->cpumask))) {
1359

1360
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1361 1362 1363 1364 1365 1366
                       _("changing cpu mask is not supported "
                         "by parallels driver"));
        return -1;
    }

    if (old->cputune.shares != new->cputune.shares ||
1367
        old->cputune.sharesSpecified != new->cputune.sharesSpecified ||
1368 1369 1370 1371
        old->cputune.period != new->cputune.period ||
        old->cputune.quota != new->cputune.quota ||
        old->cputune.nvcpupin != new->cputune.nvcpupin) {

1372
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1373 1374 1375 1376
                       _("cputune is not supported by parallels driver"));
        return -1;
    }

1377
    if (!virDomainNumatuneEquals(old->numatune, new->numatune)) {
1378
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1379 1380 1381 1382 1383 1384 1385 1386 1387
                        _("numa parameters are not supported "
                          "by parallels driver"));
        return -1;
    }

    if (old->onReboot != new->onReboot ||
        old->onPoweroff != new->onPoweroff ||
        old->onCrash != new->onCrash) {

1388
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1389 1390 1391 1392 1393
                       _("on_reboot, on_poweroff and on_crash parameters "
                         "are not supported by parallels driver"));
        return -1;
    }

1394 1395 1396 1397
    /* we fill only type and arch fields in parallelsLoadDomain for
     * hvm type and also init for containers, so we can check that all
     * other paramenters are null and boot devices config is default */

1398
    if (!STREQ_NULLABLE(old->os.type, new->os.type) ||
1399
        old->os.arch != new->os.arch ||
1400 1401 1402 1403 1404 1405
        new->os.machine != NULL || new->os.bootmenu != 0 ||
        new->os.kernel != NULL || new->os.initrd != NULL ||
        new->os.cmdline != NULL || new->os.root != NULL ||
        new->os.loader != NULL || new->os.bootloader != NULL ||
        new->os.bootloaderArgs != NULL || new->os.smbios_mode != 0 ||
        new->os.bios.useserial != 0) {
1406

1407
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1408 1409 1410 1411
                       _("changing OS parameters is not supported "
                         "by parallels driver"));
        return -1;
    }
1412 1413 1414 1415 1416
    if (STREQ(new->os.type, "hvm")) {
        if (new->os.nBootDevs != 1 ||
            new->os.bootDevs[0] != VIR_DOMAIN_BOOT_DISK ||
            new->os.init != NULL || new->os.initargv != NULL) {

1417
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1418 1419 1420 1421 1422 1423 1424 1425 1426
                           _("changing OS parameters is not supported "
                             "by parallels driver"));
            return -1;
        }
    } else {
        if (new->os.nBootDevs != 0 ||
            !STREQ_NULLABLE(old->os.init, new->os.init) ||
            (new->os.initargv != NULL && new->os.initargv[0] != NULL)) {

1427
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1428 1429 1430 1431 1432 1433
                           _("changing OS parameters is not supported "
                             "by parallels driver"));
            return -1;
        }
    }

1434 1435

    if (!STREQ_NULLABLE(old->emulator, new->emulator)) {
1436
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1437 1438 1439 1440 1441
                       _("changing emulator is not supported "
                         "by parallels driver"));
        return -1;
    }

1442 1443
    for (i = 0; i < VIR_DOMAIN_FEATURE_LAST; i++) {
        if (old->features[i] != new->features[i]) {
1444
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1445 1446 1447 1448
                           _("changing features is not supported "
                             "by parallels driver"));
            return -1;
        }
1449 1450 1451 1452 1453
    }

    if (new->clock.offset != VIR_DOMAIN_CLOCK_OFFSET_UTC ||
        new->clock.ntimers != 0) {

1454
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1455 1456 1457 1458 1459 1460 1461 1462 1463
                       _("changing clock parameters is not supported "
                         "by parallels driver"));
        return -1;
    }

    if (parallelsApplyGraphicsParams(old->graphics, old->ngraphics,
                                   new->graphics, new->ngraphics) < 0)
        return -1;

1464
    if (new->nfss != 0 ||
1465 1466 1467 1468 1469
        new->nsounds != 0 || new->nhostdevs != 0 ||
        new->nredirdevs != 0 || new->nsmartcards != 0 ||
        new->nparallels || new->nchannels != 0 ||
        new->nleases != 0 || new->nhubs != 0) {

1470
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
                       _("changing devices parameters is not supported "
                         "by parallels driver"));
        return -1;
    }

    /* there may be one auto-input */
    if (new->ninputs > 1 ||
        (new->ninputs > 1 &&
        (new->inputs[0]->type != VIR_DOMAIN_INPUT_TYPE_MOUSE ||
        new->inputs[0]->bus != VIR_DOMAIN_INPUT_BUS_PS2))) {

1482
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
                       _("changing input devices parameters is not supported "
                         "by parallels driver"));
    }


    if (parallelsApplySerialParams(old->serials, old->nserials,
                                   new->serials, new->nserials) < 0)
        return -1;

    if (parallelsApplySerialParams(old->consoles, old->nconsoles,
                                   new->consoles, new->nconsoles) < 0)
        return -1;

    if (parallelsApplyVideoParams(pdom, old->videos, old->nvideos,
                                   new->videos, new->nvideos) < 0)
        return -1;
D
Dmitry Guryanov 已提交
1499
    if (parallelsApplyDisksParams(pdom, old->disks, old->ndisks,
1500 1501
                                  new->disks, new->ndisks) < 0)
        return -1;
1502 1503 1504
    if (parallelsApplyIfacesParams(pdom, old->nets, old->nnets,
                                  new->nets, new->nnets) < 0)
        return -1;
1505

1506 1507 1508
    return 0;
}

1509
static int
1510
parallelsCreateVm(virConnectPtr conn ATTRIBUTE_UNUSED, virDomainDefPtr def)
1511 1512 1513 1514 1515
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    virUUIDFormat(def->uuid, uuidstr);

1516
    if (parallelsCmdRun(PRLCTL, "create", def->name, "--no-hdd",
1517
                        "--uuid", uuidstr, NULL) < 0)
1518
        return -1;
1519 1520 1521 1522

    return 0;
}

1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
static int
parallelsCreateCt(virConnectPtr conn ATTRIBUTE_UNUSED, virDomainDefPtr def)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    virUUIDFormat(def->uuid, uuidstr);

    if (def->nfss != 1 ||
        def->fss[0]->type != VIR_DOMAIN_FS_TYPE_TEMPLATE) {

        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("There must be only 1 template FS for "
                         "container creation"));
        goto error;
    }

    if (parallelsCmdRun(PRLCTL, "create", def->name, "--vmtype", "ct",
                        "--uuid", uuidstr,
                        "--ostemplate", def->fss[0]->src, NULL) < 0)
        goto error;

    return 0;

1546
 error:
1547 1548 1549
    return -1;
}

1550 1551 1552 1553 1554 1555
static virDomainPtr
parallelsDomainDefineXML(virConnectPtr conn, const char *xml)
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainDefPtr def;
1556
    virDomainObjPtr olddom = NULL;
1557
    virDomainObjPtr dom = NULL;
1558 1559

    parallelsDriverLock(privconn);
1560 1561
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_PARALLELS,
1562 1563 1564 1565 1566 1567
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Can't parse XML desc"));
        goto cleanup;
    }

1568 1569 1570
    olddom = virDomainObjListFindByUUID(privconn->domains, def->uuid);
    if (olddom == NULL) {
        virResetLastError();
1571 1572 1573 1574 1575 1576 1577 1578 1579
        if (STREQ(def->os.type, "hvm")) {
            if (parallelsCreateVm(conn, def))
                goto cleanup;
        } else if (STREQ(def->os.type, "exe")) {
            if (parallelsCreateCt(conn, def))
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("Unsupported OS type: %s"), def->os.type);
1580
            goto cleanup;
1581
        }
1582 1583 1584 1585
        dom = prlsdkAddDomain(privconn, def->uuid);
        if (dom)
            virObjectUnlock(dom);
        else
1586
            goto cleanup;
1587
        olddom = virDomainObjListFindByName(privconn->domains, def->name);
1588
        if (!olddom) {
1589 1590
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Domain for '%s' is not defined after creation"),
E
Eric Blake 已提交
1591
                           def->name ? def->name : _("(unnamed)"));
1592 1593
            goto cleanup;
        }
1594 1595
    }

D
Dmitry Guryanov 已提交
1596
    if (parallelsApplyChanges(olddom, def) < 0) {
1597
        virObjectUnlock(olddom);
1598 1599
        goto cleanup;
    }
1600
    virObjectUnlock(olddom);
1601

1602
    ret = virGetDomain(conn, def->name, def->uuid);
1603
    if (ret)
1604
        ret->id = def->id;
1605

1606
 cleanup:
1607 1608 1609 1610 1611
    virDomainDefFree(def);
    parallelsDriverUnlock(privconn);
    return ret;
}

1612 1613 1614 1615 1616 1617 1618
static int
parallelsNodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virNodeInfoPtr nodeinfo)
{
    return nodeGetInfo(nodeinfo);
}

1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
static int parallelsConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Encryption is not relevant / applicable to way we talk to PCS */
    return 0;
}

static int parallelsConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* We run CLI tools directly so this is secure */
    return 1;
}

static int parallelsConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

1636

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
static char *
parallelsConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                            const char **xmlCPUs,
                            unsigned int ncpus,
                            unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

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


1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
static int
parallelsDomainGetVcpus(virDomainPtr domain,
                        virVcpuInfoPtr info,
                        int maxinfo,
                        unsigned char *cpumaps,
                        int maplen)
{
    parallelsConnPtr privconn = domain->conn->privateData;
    parallelsDomObjPtr privdomdata = NULL;
    virDomainObjPtr privdom = NULL;
    size_t i;
    int v, maxcpu, hostcpus;
    int ret = -1;

    parallelsDriverLock(privconn);
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
    parallelsDriverUnlock(privconn);

    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",
                       _("cannot list vcpu pinning for an inactive domain"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;
    if ((hostcpus = nodeGetCPUCount()) < 0)
        goto cleanup;

    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    if (maxinfo >= 1) {
        if (info != NULL) {
            memset(info, 0, sizeof(*info) * maxinfo);
            for (i = 0; i < maxinfo; i++) {
                info[i].number = i;
                info[i].state = VIR_VCPU_RUNNING;
            }
        }
        if (cpumaps != NULL) {
            unsigned char *tmpmap = NULL;
            int tmpmapLen = 0;

            memset(cpumaps, 0, maplen * maxinfo);
            virBitmapToData(privdomdata->cpumask, &tmpmap, &tmpmapLen);
            if (tmpmapLen > maplen)
                tmpmapLen = maplen;

            for (v = 0; v < maxinfo; v++) {
                unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);
                memcpy(cpumap, tmpmap, tmpmapLen);
            }
            VIR_FREE(tmpmap);
        }
    }
    ret = maxinfo;

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


1720 1721 1722 1723 1724 1725 1726 1727 1728
static int
parallelsNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
                       unsigned char **cpumap,
                       unsigned int *online,
                       unsigned int flags)
{
    return nodeGetCPUMap(cpumap, online, flags);
}

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
static int
parallelsConnectDomainEventRegisterAny(virConnectPtr conn,
                                       virDomainPtr domain,
                                       int eventID,
                                       virConnectDomainEventGenericCallback callback,
                                       void *opaque,
                                       virFreeCallback freecb)
{
    int ret = -1;
    parallelsConnPtr privconn = conn->privateData;
    if (virDomainEventStateRegisterID(conn,
                                      privconn->domainEventState,
                                      domain, eventID,
                                      callback, opaque, freecb, &ret) < 0)
        ret = -1;
    return ret;
}

static int
parallelsConnectDomainEventDeregisterAny(virConnectPtr conn,
                                         int callbackID)
{
    parallelsConnPtr privconn = conn->privateData;
    int ret = -1;

    if (virObjectEventStateDeregisterID(conn,
                                        privconn->domainEventState,
                                        callbackID) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    return ret;
}
1764

1765
static virHypervisorDriver parallelsDriver = {
D
Dmitry Guryanov 已提交
1766 1767
    .no = VIR_DRV_PARALLELS,
    .name = "Parallels",
1768 1769 1770
    .connectOpen = parallelsConnectOpen,            /* 0.10.0 */
    .connectClose = parallelsConnectClose,          /* 0.10.0 */
    .connectGetVersion = parallelsConnectGetVersion,   /* 0.10.0 */
1771
    .connectGetHostname = parallelsConnectGetHostname,      /* 0.10.0 */
1772
    .nodeGetInfo = parallelsNodeGetInfo,      /* 0.10.0 */
1773
    .connectGetCapabilities = parallelsConnectGetCapabilities,      /* 0.10.0 */
1774
    .connectBaselineCPU = parallelsConnectBaselineCPU, /* 1.2.6 */
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
    .connectListDomains = parallelsConnectListDomains,      /* 0.10.0 */
    .connectNumOfDomains = parallelsConnectNumOfDomains,    /* 0.10.0 */
    .connectListDefinedDomains = parallelsConnectListDefinedDomains,        /* 0.10.0 */
    .connectNumOfDefinedDomains = parallelsConnectNumOfDefinedDomains,      /* 0.10.0 */
    .connectListAllDomains = parallelsConnectListAllDomains, /* 0.10.0 */
    .domainLookupByID = parallelsDomainLookupByID,    /* 0.10.0 */
    .domainLookupByUUID = parallelsDomainLookupByUUID,        /* 0.10.0 */
    .domainLookupByName = parallelsDomainLookupByName,        /* 0.10.0 */
    .domainGetOSType = parallelsDomainGetOSType,    /* 0.10.0 */
    .domainGetInfo = parallelsDomainGetInfo,  /* 0.10.0 */
1785 1786 1787 1788
    .domainGetState = parallelsDomainGetState,        /* 0.10.0 */
    .domainGetXMLDesc = parallelsDomainGetXMLDesc,    /* 0.10.0 */
    .domainIsPersistent = parallelsDomainIsPersistent,        /* 0.10.0 */
    .domainGetAutostart = parallelsDomainGetAutostart,        /* 0.10.0 */
1789
    .domainGetVcpus = parallelsDomainGetVcpus, /* 1.2.6 */
1790 1791 1792 1793
    .domainSuspend = parallelsDomainSuspend,    /* 0.10.0 */
    .domainResume = parallelsDomainResume,    /* 0.10.0 */
    .domainDestroy = parallelsDomainDestroy,  /* 0.10.0 */
    .domainShutdown = parallelsDomainShutdown, /* 0.10.0 */
1794
    .domainCreate = parallelsDomainCreate,    /* 0.10.0 */
1795
    .domainDefineXML = parallelsDomainDefineXML,      /* 0.10.0 */
1796 1797
    .connectDomainEventRegisterAny = parallelsConnectDomainEventRegisterAny, /* 1.2.10 */
    .connectDomainEventDeregisterAny = parallelsConnectDomainEventDeregisterAny, /* 1.2.10 */
1798
    .nodeGetCPUMap = parallelsNodeGetCPUMap, /* 1.2.8 */
1799 1800 1801
    .connectIsEncrypted = parallelsConnectIsEncrypted, /* 1.2.5 */
    .connectIsSecure = parallelsConnectIsSecure, /* 1.2.5 */
    .connectIsAlive = parallelsConnectIsAlive, /* 1.2.5 */
D
Dmitry Guryanov 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
};

/**
 * parallelsRegister:
 *
 * Registers the parallels driver
 */
int
parallelsRegister(void)
{
    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);

1822
    if (virRegisterHypervisorDriver(&parallelsDriver) < 0)
D
Dmitry Guryanov 已提交
1823
        return -1;
D
Dmitry Guryanov 已提交
1824 1825
    if (parallelsStorageRegister())
        return -1;
D
Dmitry Guryanov 已提交
1826 1827
    if (parallelsNetworkRegister())
        return -1;
D
Dmitry Guryanov 已提交
1828 1829 1830

    return 0;
}