parallels_driver.c 69.7 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 "c-ctype.h"
53
#include "virstring.h"
54
#include "cpu/cpu.h"
D
Dmitry Guryanov 已提交
55 56

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

#define VIR_FROM_THIS VIR_FROM_PARALLELS

62 63
VIR_LOG_INIT("parallels.parallels_driver");

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

67 68 69 70 71 72 73 74
#define parallelsDomNotFoundError(domain)                                \
    do {                                                                 \
        char uuidstr[VIR_UUID_STRING_BUFLEN];                            \
        virUUIDFormat(domain->uuid, uuidstr);                            \
        virReportError(VIR_ERR_NO_DOMAIN,                                \
                       _("no domain with matching uuid '%s'"), uuidstr); \
    } while (0)

75
static int parallelsConnectClose(virConnectPtr conn);
D
Dmitry Guryanov 已提交
76

77 78 79 80 81 82 83 84 85 86 87 88 89
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 已提交
90
void
D
Dmitry Guryanov 已提交
91 92 93 94 95
parallelsDriverLock(parallelsConnPtr driver)
{
    virMutexLock(&driver->lock);
}

D
Dmitry Guryanov 已提交
96
void
D
Dmitry Guryanov 已提交
97 98 99 100 101 102
parallelsDriverUnlock(parallelsConnPtr driver)
{
    virMutexUnlock(&driver->lock);
}


103 104 105 106 107 108 109 110
static void
parallelsDomObjFreePrivate(void *p)
{
    parallelsDomObjPtr pdom = p;

    if (!pdom)
        return;

111
    virBitmapFree(pdom->cpumask);
112
    VIR_FREE(pdom->uuid);
113
    VIR_FREE(pdom->home);
114 115 116
    VIR_FREE(p);
};

D
Dmitry Guryanov 已提交
117 118 119
static virCapsPtr
parallelsBuildCapabilities(void)
{
120 121 122
    virCapsPtr caps = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUDataPtr data = NULL;
D
Dmitry Guryanov 已提交
123
    virCapsGuestPtr guest;
124
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
125

126
    if ((caps = virCapabilitiesNew(virArchFromHost(),
127
                                   false, false)) == NULL)
128
        return NULL;
D
Dmitry Guryanov 已提交
129 130

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

133 134 135
    if ((guest = virCapabilitiesAddGuest(caps, "hvm",
                                         VIR_ARCH_X86_64,
                                         "parallels",
D
Dmitry Guryanov 已提交
136
                                         NULL, 0, NULL)) == NULL)
137
        goto error;
D
Dmitry Guryanov 已提交
138 139 140

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

143 144 145
    if ((guest = virCapabilitiesAddGuest(caps, "exe",
                                         VIR_ARCH_X86_64,
                                         "parallels",
146
                                         NULL, 0, NULL)) == NULL)
147
        goto error;
148 149 150

    if (virCapabilitiesAddGuestDomain(guest,
                                      "parallels", NULL, NULL, 0, NULL) == NULL)
151
        goto error;
152

153
    if (nodeGetInfo(&nodeinfo))
154 155
        goto error;

156
    if (VIR_ALLOC(cpu) < 0)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        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 已提交
174 175
    return caps;

176
 error:
177
    virObjectUnref(caps);
178
    goto cleanup;
D
Dmitry Guryanov 已提交
179 180 181
}

static char *
182
parallelsConnectGetCapabilities(virConnectPtr conn)
D
Dmitry Guryanov 已提交
183 184 185 186 187
{
    parallelsConnPtr privconn = conn->privateData;
    char *xml;

    parallelsDriverLock(privconn);
188
    xml = virCapabilitiesFormatXML(privconn->caps);
D
Dmitry Guryanov 已提交
189 190 191 192
    parallelsDriverUnlock(privconn);
    return xml;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static int
parallelsGetSerialInfo(virDomainChrDefPtr chr,
                       const char *name, virJSONValuePtr value)
{
    const char *tmp;

    chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
    chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
    if (virStrToLong_i(name + strlen("serial"),
                       NULL, 10, &chr->target.port) < 0) {
        parallelsParseError();
        return -1;
    }

    if (virJSONValueObjectHasKey(value, "output")) {
        chr->source.type = VIR_DOMAIN_CHR_TYPE_FILE;

        tmp = virJSONValueObjectGetString(value, "output");
        if (!tmp) {
            parallelsParseError();
            return -1;
        }

216 217
        if (VIR_STRDUP(chr->source.data.file.path, tmp) < 0)
            return -1;
218 219 220 221 222 223 224 225 226
    } else if (virJSONValueObjectHasKey(value, "socket")) {
        chr->source.type = VIR_DOMAIN_CHR_TYPE_UNIX;

        tmp = virJSONValueObjectGetString(value, "socket");
        if (!tmp) {
            parallelsParseError();
            return -1;
        }

227 228
        if (VIR_STRDUP(chr->source.data.nix.path, tmp) < 0)
            return -1;
229 230 231 232 233 234 235 236 237 238
        chr->source.data.nix.listen = false;
    } else if (virJSONValueObjectHasKey(value, "real")) {
        chr->source.type = VIR_DOMAIN_CHR_TYPE_DEV;

        tmp = virJSONValueObjectGetString(value, "real");
        if (!tmp) {
            parallelsParseError();
            return -1;
        }

239 240
        if (VIR_STRDUP(chr->source.data.file.path, tmp) < 0)
            return -1;
241 242 243 244 245 246 247 248 249
    } else {
        parallelsParseError();
        return -1;
    }

    return 0;
}

static int
250
parallelsAddSerialInfo(virDomainChrDefPtr **serials, size_t *nserials,
251 252 253 254 255
                       const char *key, virJSONValuePtr value)
{
    virDomainChrDefPtr chr = NULL;

    if (!(chr = virDomainChrDefNew()))
256
        goto cleanup;
257 258 259 260

    if (parallelsGetSerialInfo(chr, key, value))
        goto cleanup;

261
    if (VIR_APPEND_ELEMENT(*serials, *nserials, chr) < 0)
262
        goto cleanup;
263 264 265

    return 0;

266
 cleanup:
267 268 269 270
    virDomainChrDefFree(chr);
    return -1;
}

271 272 273 274 275 276 277 278 279 280 281
static int
parallelsAddVideoInfo(virDomainDefPtr def, virJSONValuePtr value)
{
    virDomainVideoDefPtr video = NULL;
    virDomainVideoAccelDefPtr accel = NULL;
    const char *tmp;
    char *endptr;
    unsigned long mem;

    if (!(tmp = virJSONValueObjectGetString(value, "size"))) {
        parallelsParseError();
282
        goto error;
283 284 285 286
    }

    if (virStrToLong_ul(tmp, &endptr, 10, &mem) < 0) {
        parallelsParseError();
287
        goto error;
288 289 290 291
    }

    if (!STREQ(endptr, "Mb")) {
        parallelsParseError();
292
        goto error;
293 294 295
    }

    if (VIR_ALLOC(video) < 0)
296
        goto error;
297 298

    if (VIR_ALLOC(accel) < 0)
299
        goto error;
300

301
    if (VIR_APPEND_ELEMENT_COPY(def->videos, def->nvideos, video) < 0)
302
        goto error;
303 304 305 306 307 308 309 310

    video->type = VIR_DOMAIN_VIDEO_TYPE_VGA;
    video->vram = mem << 20;
    video->heads = 1;
    video->accel = accel;

    return 0;

311
 error:
312
    VIR_FREE(accel);
313 314 315 316
    virDomainVideoDefFree(video);
    return -1;
}

317 318 319 320 321 322 323 324 325 326 327 328
static int
parallelsGetHddInfo(virDomainDefPtr def,
                    virDomainDiskDefPtr disk,
                    const char *key,
                    virJSONValuePtr value)
{
    const char *tmp;
    unsigned int idx;

    disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;

    if (virJSONValueObjectHasKey(value, "real") == 1) {
E
Eric Blake 已提交
329
        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_BLOCK);
330 331 332 333 334 335

        if (!(tmp = virJSONValueObjectGetString(value, "real"))) {
            parallelsParseError();
            return -1;
        }

336
        if (virDomainDiskSetSource(disk, tmp) < 0)
337 338
            return -1;
    } else {
E
Eric Blake 已提交
339
        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
340 341 342 343 344 345

        if (!(tmp = virJSONValueObjectGetString(value, "image"))) {
            parallelsParseError();
            return -1;
        }

346
        if (virDomainDiskSetSource(disk, tmp) < 0)
347
            return -1;
348 349

        virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_PLOOP);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    }

    tmp = virJSONValueObjectGetString(value, "port");
    if (!tmp && !IS_CT(def)) {
        parallelsParseError();
        return -1;
    }

    if (tmp) {
        if (STRPREFIX(tmp, "ide")) {
            disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
        } else if (STRPREFIX(tmp, "sata")) {
            disk->bus = VIR_DOMAIN_DISK_BUS_SATA;
        } else if (STRPREFIX(tmp, "scsi")) {
            disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
        } else {
            parallelsParseError();
            return -1;
        }

        char *colonp;
        unsigned int pos;

        if (!(colonp = strchr(tmp, ':'))) {
            parallelsParseError();
            return -1;
        }

        if (virStrToLong_ui(colonp + 1, NULL, 10, &pos) < 0) {
            parallelsParseError();
            return -1;
        }

        disk->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE;
        disk->info.addr.drive.target = pos;
    } else {
        /* Actually there are no disk devices in containers, but in
         * in Parallels Cloud Server we mount disk images as container's
         * root fs during start, so it looks like a disk device. */
        disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
    }

    if (virStrToLong_ui(key + strlen("hdd"), NULL, 10, &idx) < 0) {
        parallelsParseError();
        return -1;
    }

    if (!(disk->dst = virIndexToDiskName(idx, "sd")))
        return -1;

    return 0;
}

static int
parallelsAddHddInfo(virDomainDefPtr def, const char *key, virJSONValuePtr value)
{
    virDomainDiskDefPtr disk = NULL;

408
    if (!(disk = virDomainDiskDefNew()))
409
        goto error;
410 411 412 413

    if (parallelsGetHddInfo(def, disk, key, value))
        goto error;

414
    if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
415
        goto error;
416 417 418

    return 0;

419
 error:
420 421 422 423
    virDomainDiskDefFree(disk);
    return -1;
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437
static inline unsigned char hex2int(char c)
{
    if (c <= '9')
        return c - '0';
    else
        return 10 + c - 'A';
}

/*
 * Parse MAC address in format XXXXXXXXXXXX.
 */
static int
parallelsMacAddrParse(const char *str, virMacAddrPtr addr)
{
438
    size_t i;
439 440 441 442 443 444 445 446 447 448 449 450

    if (strlen(str) != 12)
        goto error;

    for (i = 0; i < 6; i++) {
        if (!c_isxdigit(str[2 * i]) || !c_isxdigit(str[2 * i + 1]))
            goto error;

        addr->addr[i] = (hex2int(str[2 * i]) << 4) + hex2int(str[2 * i + 1]);
    }

    return 0;
451
 error:
452 453 454 455 456 457 458 459 460 461 462 463 464 465
    virReportError(VIR_ERR_INVALID_ARG,
                   _("Invalid MAC address format '%s'"), str);
    return -1;
}

static int
parallelsGetNetInfo(virDomainNetDefPtr net,
                    const char *key,
                    virJSONValuePtr value)
{
    const char *tmp;

    /* use device name, shown by prlctl as target device
     * for identifying network adapter in virDomainDefineXML */
466
    if (VIR_STRDUP(net->ifname, key) < 0)
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        goto error;

    net->type = VIR_DOMAIN_NET_TYPE_NETWORK;

    if (!(tmp = virJSONValueObjectGetString(value, "mac"))) {
        parallelsParseError();
        return -1;
    }

    if (parallelsMacAddrParse(tmp, &net->mac) < 0) {
        parallelsParseError();
        goto error;
    }


482 483 484 485 486 487
    if (virJSONValueObjectHasKey(value, "network")) {
        if (!(tmp = virJSONValueObjectGetString(value, "network"))) {
            parallelsParseError();
            goto error;
        }

488
        if (VIR_STRDUP(net->data.network.name, tmp) < 0)
489 490 491 492 493 494 495 496 497 498 499 500
            goto error;
    } else if (virJSONValueObjectHasKey(value, "type")) {
        if (!(tmp = virJSONValueObjectGetString(value, "type"))) {
            parallelsParseError();
            goto error;
        }

        if (!STREQ(tmp, "routed")) {
            parallelsParseError();
            goto error;
        }

501 502
        if (VIR_STRDUP(net->data.network.name,
                       PARALLELS_ROUTED_NETWORK_NAME) < 0)
503 504 505
            goto error;
    } else {
        parallelsParseError();
506 507 508 509
        goto error;
    }

    net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP;
510 511 512
    if ((tmp = virJSONValueObjectGetString(value, "state")) &&
        STREQ(tmp, "disconnected")) {
        net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN;
513 514 515 516
    }

    return 0;

517
 error:
518 519 520 521 522 523 524 525 526
    return -1;
}

static int
parallelsAddNetInfo(virDomainDefPtr def, const char *key, virJSONValuePtr value)
{
    virDomainNetDefPtr net = NULL;

    if (VIR_ALLOC(net) < 0)
527
        goto error;
528 529 530 531 532

    if (parallelsGetNetInfo(net, key, value))
        goto error;

    if (VIR_EXPAND_N(def->nets, def->nnets, 1) < 0)
533
        goto error;
534 535 536 537 538

    def->nets[def->nnets - 1] = net;

    return 0;

539
 error:
540 541 542 543
    virDomainNetDefFree(net);
    return -1;
}

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
static int
parallelsAddDomainHardware(virDomainDefPtr def, virJSONValuePtr jobj)
{
    int n;
    size_t i;
    virJSONValuePtr value;
    const char *key;

    n = virJSONValueObjectKeysNumber(jobj);
    if (n < 1)
        goto cleanup;

    for (i = 0; i < n; i++) {
        key = virJSONValueObjectGetKey(jobj, i);
        value = virJSONValueObjectGetValue(jobj, i);

        if (STRPREFIX(key, "serial")) {
561 562 563 564 565 566 567 568 569 570
            if (parallelsAddSerialInfo(&def->serials,
                                       &def->nserials, key, value))
                goto cleanup;
            if (def->nconsoles == 0) {
                if (parallelsAddSerialInfo(&def->consoles,
                                           &def->nconsoles, key, value))
                    goto cleanup;
            }
        } else if (STREQ(key, "video")) {
            if (parallelsAddVideoInfo(def, value))
571
                goto cleanup;
572 573 574
        } else if (STRPREFIX(key, "hdd")) {
            if (parallelsAddHddInfo(def, key, value))
                goto cleanup;
575 576 577
        } else if (STRPREFIX(key, "net")) {
            if (parallelsAddNetInfo(def, key, value))
                goto cleanup;
578 579 580 581 582
        }
    }

    return 0;

583
 cleanup:
584 585 586
    return -1;
}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
static int
parallelsAddVNCInfo(virDomainDefPtr def, virJSONValuePtr jobj_root)
{
    const char *tmp;
    unsigned int port;
    virJSONValuePtr jobj;
    int ret = -1;
    virDomainGraphicsDefPtr gr = NULL;

    jobj = virJSONValueObjectGet(jobj_root, "Remote display");
    if (!jobj) {
        parallelsParseError();
        goto cleanup;
    }

    tmp = virJSONValueObjectGetString(jobj, "mode");
    if (!tmp) {
        parallelsParseError();
        goto cleanup;
    }

    if (STREQ(tmp, "off")) {
        ret = 0;
        goto cleanup;
    }

    if (VIR_ALLOC(gr) < 0)
614
        goto cleanup;
615 616 617 618

    if (STREQ(tmp, "auto")) {
        if (virJSONValueObjectGetNumberUint(jobj, "port", &port) < 0)
            port = 0;
619
        gr->data.vnc.autoport = true;
620 621 622 623 624
    } else {
        if (virJSONValueObjectGetNumberUint(jobj, "port", &port) < 0) {
            parallelsParseError();
            goto cleanup;
        }
625
        gr->data.vnc.autoport = false;
626 627 628 629 630 631 632
    }

    gr->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
    gr->data.vnc.port = port;
    gr->data.vnc.keymap = NULL;
    gr->data.vnc.socket = NULL;
    gr->data.vnc.auth.passwd = NULL;
633
    gr->data.vnc.auth.expires = false;
634 635 636 637 638 639 640 641
    gr->data.vnc.auth.connected = 0;

    if (!(tmp = virJSONValueObjectGetString(jobj, "address"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (VIR_ALLOC(gr->listens) < 0)
642
        goto cleanup;
643 644 645

    gr->nListens = 1;

646 647
    if (VIR_STRDUP(gr->listens[0].address, tmp) < 0)
        goto cleanup;
648 649 650

    gr->listens[0].type = VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS;

651
    if (VIR_APPEND_ELEMENT(def->graphics, def->ngraphics, gr) < 0)
652
        goto cleanup;
653 654 655

    return 0;

656
 cleanup:
657 658 659 660
    virDomainGraphicsDefFree(gr);
    return ret;
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
/*
 * Must be called with privconn->lock held
 */
static virDomainObjPtr
parallelsLoadDomain(parallelsConnPtr privconn, virJSONValuePtr jobj)
{
    virDomainObjPtr dom = NULL;
    virDomainDefPtr def = NULL;
    parallelsDomObjPtr pdom = NULL;
    virJSONValuePtr jobj2, jobj3;
    const char *tmp;
    char *endptr;
    unsigned long mem;
    unsigned int x;
    const char *autostart;
    const char *state;
677
    int hostcpus;
678 679

    if (VIR_ALLOC(def) < 0)
680
        goto cleanup;
681

682 683 684
    if (VIR_ALLOC(pdom) < 0)
        goto cleanup;

685 686 687 688 689 690 691
    def->virtType = VIR_DOMAIN_VIRT_PARALLELS;
    def->id = -1;

    if (!(tmp = virJSONValueObjectGetString(jobj, "Name"))) {
        parallelsParseError();
        goto cleanup;
    }
692 693
    if (VIR_STRDUP(def->name, tmp) < 0)
        goto cleanup;
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

    if (!(tmp = virJSONValueObjectGetString(jobj, "ID"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (virUUIDParse(tmp, def->uuid) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("UUID in config file malformed"));
        goto cleanup;
    }

    if (!(tmp = virJSONValueObjectGetString(jobj, "Description"))) {
        parallelsParseError();
        goto cleanup;
    }
710 711
    if (VIR_STRDUP(def->description, tmp) < 0)
        goto cleanup;
712 713 714 715 716 717 718 719 720 721 722

    if (!(jobj2 = virJSONValueObjectGet(jobj, "Hardware"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(jobj3 = virJSONValueObjectGet(jobj2, "cpu"))) {
        parallelsParseError();
        goto cleanup;
    }

723 724 725 726 727 728 729
    if (virJSONValueObjectGetNumberUint(jobj3, "cpus", &x) == 0) {
        def->vcpus = x;
        def->maxvcpus = x;
    } else if ((tmp = virJSONValueObjectGetString(jobj3, "cpus"))) {
        if (STREQ(tmp, "unlimited")) {
            virNodeInfo nodeinfo;

730
            if (nodeGetInfo(&nodeinfo) < 0) {
731 732 733 734 735 736 737 738 739 740 741 742
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Can't get node info"));
                goto cleanup;
            }

            def->vcpus = nodeinfo.cpus;
            def->maxvcpus = def->vcpus;
        } else {
            parallelsParseError();
            goto cleanup;
        }
    } else {
743 744 745 746
        parallelsParseError();
        goto cleanup;
    }

747 748 749 750 751 752 753 754 755 756 757 758 759
    if ((hostcpus = nodeGetCPUCount()) < 0)
        goto cleanup;

    if (!(tmp = virJSONValueObjectGetString(jobj3, "mask"))) {
        /* Absence of this field means that all domains cpus are available */
        if (!(pdom->cpumask = virBitmapNew(hostcpus)))
            goto cleanup;
        virBitmapSetAll(pdom->cpumask);
    } else {
        if (virBitmapParse(tmp, 0, &pdom->cpumask, hostcpus) < 0)
            goto cleanup;
    }

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    if (!(jobj3 = virJSONValueObjectGet(jobj2, "memory"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(tmp = virJSONValueObjectGetString(jobj3, "size"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (virStrToLong_ul(tmp, &endptr, 10, &mem) < 0) {
        parallelsParseError();
        goto cleanup;
    }

    if (!STREQ(endptr, "Mb")) {
        parallelsParseError();
        goto cleanup;
    }

    def->mem.max_balloon = mem;
    def->mem.max_balloon <<= 10;
    def->mem.cur_balloon = def->mem.max_balloon;

784 785 786 787 788 789
    if (!(tmp = virJSONValueObjectGetString(jobj, "Type"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (STREQ(tmp, "CT")) {
790 791 792 793
        if (VIR_STRDUP(def->os.type, "exe") < 0)
            goto cleanup;
        if (VIR_STRDUP(def->os.init, "/sbin/init") < 0)
            goto cleanup;
794
    } else if (STREQ(tmp, "VM")) {
795 796
        if (VIR_STRDUP(def->os.type, "hvm") < 0)
            goto cleanup;
797
    }
798

799
    def->os.arch = VIR_ARCH_X86_64;
800 801 802 803 804 805 806 807

    if (virJSONValueObjectGetNumberUint(jobj, "EnvID", &x) < 0)
        goto cleanup;
    pdom->id = x;
    if (!(tmp = virJSONValueObjectGetString(jobj, "ID"))) {
        parallelsParseError();
        goto cleanup;
    }
808 809
    if (VIR_STRDUP(pdom->uuid, tmp) < 0)
        goto cleanup;
810

811 812 813 814 815
    if (!(tmp = virJSONValueObjectGetString(jobj, "Home"))) {
        parallelsParseError();
        goto cleanup;
    }

816 817
    if (VIR_STRDUP(pdom->home, tmp) < 0)
        goto cleanup;
818

819 820 821 822 823 824 825 826 827 828
    if (!(state = virJSONValueObjectGetString(jobj, "State"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(autostart = virJSONValueObjectGetString(jobj, "Autostart"))) {
        parallelsParseError();
        goto cleanup;
    }

829 830 831
    if (parallelsAddDomainHardware(def, jobj2) < 0)
        goto cleanup;

832 833 834
    if (parallelsAddVNCInfo(def, jobj) < 0)
        goto cleanup;

835
    if (!(dom = virDomainObjListAdd(privconn->domains, def,
836
                                    privconn->xmlopt,
837
                                    0, NULL)))
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
        goto cleanup;
    /* dom is locked here */

    dom->privateDataFreeFunc = parallelsDomObjFreePrivate;
    dom->privateData = pdom;
    dom->persistent = 1;

    /* TODO: handle all possible states */
    if (STREQ(state, "running")) {
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
        def->id = pdom->id;
    }

    if (STREQ(autostart, "on"))
        dom->autostart = 1;
    else
        dom->autostart = 0;

857
    virObjectUnlock(dom);
858 859 860

    return dom;

861
 cleanup:
862 863 864 865 866 867 868 869 870 871 872 873 874 875
    virDomainDefFree(def);
    parallelsDomObjFreePrivate(pdom);
    return NULL;
}

/*
 * Must be called with privconn->lock held
 *
 * if domain_name is NULL - load information about all
 * registered domains.
 */
static int
parallelsLoadDomains(parallelsConnPtr privconn, const char *domain_name)
{
876 877
    int count;
    size_t i;
878 879 880 881 882 883
    virJSONValuePtr jobj;
    virJSONValuePtr jobj2;
    virDomainObjPtr dom = NULL;
    int ret = -1;

    jobj = parallelsParseOutput(PRLCTL, "list", "-j", "-a", "-i", "-H",
884
                                "--vmtype", "all", domain_name, NULL);
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
    if (!jobj) {
        parallelsParseError();
        goto cleanup;
    }

    count = virJSONValueArraySize(jobj);
    if (count < 0) {
        parallelsParseError();
        goto cleanup;
    }

    for (i = 0; i < count; i++) {
        jobj2 = virJSONValueArrayGet(jobj, i);
        if (!jobj2) {
            parallelsParseError();
            goto cleanup;
        }

        dom = parallelsLoadDomain(privconn, jobj2);
        if (!dom)
            goto cleanup;
    }

    ret = 0;

910
 cleanup:
911 912 913 914
    virJSONValueFree(jobj);
    return ret;
}

915

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
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;
}


935 936
virDomainDefParserConfig parallelsDomainDefParserConfig = {
    .macPrefix = {0x42, 0x1C, 0x00},
937 938
    .devicesPostParseCallback = parallelsDomainDeviceDefPostParse,
    .domainPostParseCallback = parallelsDomainDefPostParse,
939 940 941
};


D
Dmitry Guryanov 已提交
942 943 944 945 946
static int
parallelsOpenDefault(virConnectPtr conn)
{
    parallelsConnPtr privconn;

947
    if (VIR_ALLOC(privconn) < 0)
D
Dmitry Guryanov 已提交
948 949 950 951
        return VIR_DRV_OPEN_ERROR;
    if (virMutexInit(&privconn->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize mutex"));
952 953 954 955 956 957
        goto err_free;
    }

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

960 961 962
    if (prlsdkConnect(privconn) < 0)
        goto err_free;

D
Dmitry Guryanov 已提交
963 964 965
    if (!(privconn->caps = parallelsBuildCapabilities()))
        goto error;

966 967
    if (!(privconn->xmlopt = virDomainXMLOptionNew(&parallelsDomainDefParserConfig,
                                                 NULL, NULL)))
968 969
        goto error;

970
    if (!(privconn->domains = virDomainObjListNew()))
D
Dmitry Guryanov 已提交
971 972 973 974
        goto error;

    conn->privateData = privconn;

975 976 977
    if (parallelsLoadDomains(privconn, NULL))
        goto error;

D
Dmitry Guryanov 已提交
978 979
    return VIR_DRV_OPEN_SUCCESS;

980
 error:
981
    virObjectUnref(privconn->domains);
982
    virObjectUnref(privconn->caps);
D
Dmitry Guryanov 已提交
983
    virStoragePoolObjListFree(&privconn->pools);
984 985 986
    prlsdkDisconnect(privconn);
    prlsdkDeinit();
 err_free:
D
Dmitry Guryanov 已提交
987 988 989 990 991
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
}

static virDrvOpenStatus
992 993 994
parallelsConnectOpen(virConnectPtr conn,
                     virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                     unsigned int flags)
D
Dmitry Guryanov 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
{
    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. */
1011 1012 1013 1014
    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 已提交
1015 1016 1017
        return VIR_DRV_OPEN_ERROR;
    }

1018
    if ((ret = parallelsOpenDefault(conn)) != VIR_DRV_OPEN_SUCCESS)
D
Dmitry Guryanov 已提交
1019 1020 1021 1022 1023 1024
        return ret;

    return VIR_DRV_OPEN_SUCCESS;
}

static int
1025
parallelsConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
1026 1027 1028 1029
{
    parallelsConnPtr privconn = conn->privateData;

    parallelsDriverLock(privconn);
1030
    virObjectUnref(privconn->caps);
1031
    virObjectUnref(privconn->xmlopt);
1032
    virObjectUnref(privconn->domains);
1033
    prlsdkDisconnect(privconn);
D
Dmitry Guryanov 已提交
1034
    conn->privateData = NULL;
1035
    prlsdkDeinit();
D
Dmitry Guryanov 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044

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

    VIR_FREE(privconn);
    return 0;
}

static int
1045
parallelsConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
1046
{
1047 1048 1049 1050 1051 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 1082 1083 1084
    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;

1085
 cleanup:
1086 1087 1088 1089
    VIR_FREE(output);
    return ret;
}

1090 1091 1092 1093 1094 1095 1096

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


1097
static int
1098
parallelsConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1099 1100 1101 1102 1103
{
    parallelsConnPtr privconn = conn->privateData;
    int n;

    parallelsDriverLock(privconn);
1104 1105
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                     NULL, NULL);
1106 1107 1108 1109 1110 1111
    parallelsDriverUnlock(privconn);

    return n;
}

static int
1112
parallelsConnectNumOfDomains(virConnectPtr conn)
1113 1114 1115 1116 1117
{
    parallelsConnPtr privconn = conn->privateData;
    int count;

    parallelsDriverLock(privconn);
1118 1119
    count = virDomainObjListNumOfDomains(privconn->domains, true,
                                         NULL, NULL);
1120 1121 1122 1123 1124 1125
    parallelsDriverUnlock(privconn);

    return count;
}

static int
1126
parallelsConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
1127 1128 1129 1130 1131 1132
{
    parallelsConnPtr privconn = conn->privateData;
    int n;

    parallelsDriverLock(privconn);
    memset(names, 0, sizeof(*names) * maxnames);
1133
    n = virDomainObjListGetInactiveNames(privconn->domains, names,
1134
                                         maxnames, NULL, NULL);
1135 1136 1137 1138 1139 1140
    parallelsDriverUnlock(privconn);

    return n;
}

static int
1141
parallelsConnectNumOfDefinedDomains(virConnectPtr conn)
1142 1143 1144 1145 1146
{
    parallelsConnPtr privconn = conn->privateData;
    int count;

    parallelsDriverLock(privconn);
1147 1148
    count = virDomainObjListNumOfDomains(privconn->domains, false,
                                         NULL, NULL);
1149 1150 1151 1152 1153 1154
    parallelsDriverUnlock(privconn);

    return count;
}

static int
1155 1156 1157
parallelsConnectListAllDomains(virConnectPtr conn,
                               virDomainPtr **domains,
                               unsigned int flags)
1158 1159 1160 1161
{
    parallelsConnPtr privconn = conn->privateData;
    int ret = -1;

O
Osier Yang 已提交
1162
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
1163
    parallelsDriverLock(privconn);
1164 1165
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
1166 1167 1168 1169 1170 1171
    parallelsDriverUnlock(privconn);

    return ret;
}

static virDomainPtr
1172
parallelsDomainLookupByID(virConnectPtr conn, int id)
1173 1174 1175 1176 1177 1178
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

    parallelsDriverLock(privconn);
1179
    dom = virDomainObjListFindByID(privconn->domains, id);
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    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;

1191
 cleanup:
1192
    if (dom)
1193
        virObjectUnlock(dom);
1194 1195 1196 1197
    return ret;
}

static virDomainPtr
1198
parallelsDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
1199 1200 1201 1202 1203 1204
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

    parallelsDriverLock(privconn);
1205
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    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;

1220
 cleanup:
1221
    if (dom)
1222
        virObjectUnlock(dom);
1223 1224 1225 1226
    return ret;
}

static virDomainPtr
1227
parallelsDomainLookupByName(virConnectPtr conn, const char *name)
1228 1229 1230 1231 1232 1233
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

    parallelsDriverLock(privconn);
1234
    dom = virDomainObjListFindByName(privconn->domains, name);
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    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;

1247
 cleanup:
1248
    if (dom)
1249
        virObjectUnlock(dom);
1250 1251 1252 1253
    return ret;
}

static int
1254
parallelsDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
1255 1256 1257 1258 1259 1260
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    parallelsDriverLock(privconn);
1261
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    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;

1276
 cleanup:
1277
    if (privdom)
1278
        virObjectUnlock(privdom);
1279 1280 1281 1282
    return ret;
}

static char *
1283
parallelsDomainGetOSType(virDomainPtr domain)
1284 1285 1286 1287 1288 1289 1290
{
    parallelsConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;

    char *ret = NULL;

    parallelsDriverLock(privconn);
1291
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
1292 1293 1294 1295 1296
    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

1297
    ignore_value(VIR_STRDUP(ret, privdom->def->os.type));
1298

1299
 cleanup:
1300
    if (privdom)
1301
        virObjectUnlock(privdom);
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
    parallelsDriverUnlock(privconn);
    return ret;
}

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

    parallelsDriverLock(privconn);
1314
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
1315 1316 1317 1318 1319 1320 1321
    if (privdom == NULL) {
        parallelsDomNotFoundError(domain);
        goto cleanup;
    }

    ret = 1;

1322
 cleanup:
1323
    if (privdom)
1324
        virObjectUnlock(privdom);
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    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);
1339
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    parallelsDriverUnlock(privconn);

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

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

1350
 cleanup:
1351
    if (privdom)
1352
        virObjectUnlock(privdom);
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
    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);
1367
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    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);

1380
 cleanup:
1381
    if (privdom)
1382
        virObjectUnlock(privdom);
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    return ret;
}

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

    parallelsDriverLock(privconn);
1394
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
    parallelsDriverUnlock(privconn);

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

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

1405
 cleanup:
1406
    if (privdom)
1407
        virObjectUnlock(privdom);
1408
    return ret;
D
Dmitry Guryanov 已提交
1409 1410
}

1411
typedef int (*parallelsChangeStateFunc)(virDomainObjPtr privdom);
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
#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);
1426
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    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;

1448
 cleanup:
1449
    if (privdom)
1450
        virObjectUnlock(privdom);
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

    return ret;
}

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

static int
1461
parallelsDomainSuspend(virDomainPtr domain)
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
{
    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
1475
parallelsDomainResume(virDomainPtr domain)
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
{
    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
1503
parallelsDomainDestroy(virDomainPtr domain)
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
{
    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
1517
parallelsDomainShutdown(virDomainPtr domain)
1518 1519 1520 1521 1522 1523 1524
{
    return parallelsDomainChangeState(domain,
                                      VIR_DOMAIN_RUNNING, "running",
                                      parallelsStop,
                                      VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
}

1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
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 &&
1545
        (old->data.vnc.port != 0 && new->data.vnc.port != 0)) {
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566

        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;
1567
 error:
1568
    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1569 1570 1571 1572 1573 1574 1575 1576 1577
                   _("changing display parameters is not supported "
                     "by parallels driver"));
    return -1;
}

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

1580 1581 1582
    if (nold != nnew)
        goto error;

1583
    for (i = 0; i < nold; i++) {
1584 1585 1586
        virDomainChrDefPtr oldserial = oldserials[i];
        virDomainChrDefPtr newserial = NULL;

1587
        for (j = 0; j < nnew; j++) {
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
            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;
1605
        if (newserial->source.type == VIR_DOMAIN_CHR_TYPE_UNIX &&
1606 1607 1608 1609 1610 1611 1612 1613 1614
           (!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;
1615
 error:
1616
    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
                   _("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) {
1631
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1632 1633 1634 1635 1636 1637 1638 1639
                       _("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) {
1640
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1641 1642 1643 1644 1645 1646
                       _("Only VGA video device is "
                         "supported by parallels driver"));
        return -1;
    }

    if (new->heads != 1) {
1647
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1648 1649 1650 1651 1652 1653 1654 1655 1656
                       _("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) {

1657
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1658 1659 1660
                   _("Changing video acceleration parameters is "
                     "not supported by parallels driver"));
        return -1;
1661

1662 1663 1664
    }

    if (old->vram != new->vram) {
1665
        if (new->vram % (1 << 10) != 0) {
1666
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1667 1668 1669 1670
                       _("Video RAM size should be multiple of 1Mb."));
            return -1;
        }

1671
        snprintf(str_vram, 31, "%dK", new->vram);
1672 1673 1674 1675 1676 1677 1678 1679 1680
        str_vram[31] = '\0';

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

D
Dmitry Guryanov 已提交
1681 1682
static int parallelsAddHdd(parallelsDomObjPtr pdom,
                           virDomainDiskDefPtr disk)
1683 1684
{
    int ret = -1;
D
Dmitry Guryanov 已提交
1685 1686
    const char *src = virDomainDiskGetSource(disk);
    int type = virDomainDiskGetType(disk);
1687 1688 1689 1690
    const char *strbus;

    virCommandPtr cmd = virCommandNewArgList(PRLCTL, "set", pdom->uuid,
                                             "--device-add", "hdd", NULL);
D
Dmitry Guryanov 已提交
1691 1692 1693 1694 1695

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

        if (format != VIR_STORAGE_FILE_PLOOP) {
1696
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
D
Dmitry Guryanov 已提交
1697 1698 1699 1700 1701 1702 1703 1704
                           _("Invalid disk format: %d"), type);
            goto cleanup;
        }

        virCommandAddArg(cmd, "--image");
    } else if (VIR_STORAGE_TYPE_BLOCK) {
        virCommandAddArg(cmd, "--device");
    } else {
1705
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
D
Dmitry Guryanov 已提交
1706 1707 1708 1709 1710
                       _("Invalid disk type: %d"), type);
        goto cleanup;
    }

    virCommandAddArg(cmd, src);
1711 1712

    if (!(strbus = parallelsGetDiskBusName(disk->bus))) {
1713
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
                       _("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);

1724
    if (virCommandRun(cmd, NULL) < 0)
1725 1726 1727 1728
        goto cleanup;

    ret = 0;

1729
 cleanup:
D
Dmitry Guryanov 已提交
1730
    virCommandFree(cmd);
1731 1732 1733
    return ret;
}

1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
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;
}

1750
static int
D
Dmitry Guryanov 已提交
1751
parallelsApplyDisksParams(parallelsDomObjPtr pdom,
1752 1753 1754
                          virDomainDiskDefPtr *olddisks, int nold,
                          virDomainDiskDefPtr *newdisks, int nnew)
{
1755
    size_t i, j;
1756

1757
    for (i = 0; i < nold; i++) {
1758 1759
        virDomainDiskDefPtr newdisk = NULL;
        virDomainDiskDefPtr olddisk = olddisks[i];
1760
        for (j = 0; j < nnew; j++) {
1761 1762 1763 1764 1765 1766 1767
            if (STREQ_NULLABLE(newdisks[j]->dst, olddisk->dst)) {
                newdisk = newdisks[j];
                break;
            }
        }

        if (!newdisk) {
1768
            if (parallelsRemoveHdd(pdom, olddisk)) {
1769
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1770 1771 1772 1773 1774 1775
                               _("Can't remove disk '%s' "
                                 "in the specified config"), olddisks[i]->serial);
                return -1;
            }

            continue;
1776 1777 1778 1779
        }

        if (olddisk->bus != newdisk->bus ||
            olddisk->info.addr.drive.target != newdisk->info.addr.drive.target ||
1780 1781
            !STREQ_NULLABLE(virDomainDiskGetSource(olddisk),
                            virDomainDiskGetSource(newdisk))) {
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792

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

1793
            if (!(strbus = parallelsGetDiskBusName(newdisk->bus))) {
1794
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1795
                               _("Unsupported disk bus: %d"), newdisk->bus);
1796
                return -1;
1797
            }
1798

1799 1800 1801 1802 1803 1804 1805 1806 1807
            if (parallelsCmdRun(PRLCTL, "set", pdom->uuid,
                                "--device-set", prlname,
                                "--iface", strbus,
                                "--position", strpos,
                                "--image", newdisk->src, NULL))
                return -1;
        }
    }

1808 1809 1810 1811 1812 1813 1814 1815 1816
    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 已提交
1817
        if (parallelsAddHdd(pdom, newdisk))
1818 1819 1820
            return -1;
    }

1821 1822 1823
    return 0;
}

1824 1825 1826 1827 1828 1829
static int parallelsApplyIfaceParams(parallelsDomObjPtr pdom,
                                     virDomainNetDefPtr oldnet,
                                     virDomainNetDefPtr newnet)
{
    bool create = false;
    bool is_changed = false;
1830
    virCommandPtr cmd = NULL;
1831
    char strmac[VIR_MAC_STRING_BUFLEN];
1832
    size_t i;
1833
    int ret = -1;
1834 1835 1836

    if (!oldnet) {
        create = true;
1837
        if (VIR_ALLOC(oldnet) < 0)
1838 1839 1840 1841
            return -1;
    }

    if (!create && oldnet->type != newnet->type) {
1842
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1843
                       _("Changing network type is not supported"));
1844
        goto cleanup;
1845 1846 1847
    }

    if (!STREQ_NULLABLE(oldnet->model, newnet->model)) {
1848
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1849
                       _("Changing network device model is not supported"));
1850
        goto cleanup;
1851 1852 1853 1854
    }

    if (!STREQ_NULLABLE(oldnet->data.network.portgroup,
                        newnet->data.network.portgroup)) {
1855
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1856
                       _("Changing network portgroup is not supported"));
1857
        goto cleanup;
1858 1859 1860 1861
    }

    if (!virNetDevVPortProfileEqual(oldnet->virtPortProfile,
                                    newnet->virtPortProfile)) {
1862
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1863
                       _("Changing virtual port profile is not supported"));
1864
        goto cleanup;
1865 1866 1867
    }

    if (newnet->tune.sndbuf_specified) {
1868
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1869
                       _("Setting send buffer size is not supported"));
1870
        goto cleanup;
1871 1872 1873
    }

    if (!STREQ_NULLABLE(oldnet->script, newnet->script)) {
1874
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1875
                       _("Setting startup script is not supported"));
1876
        goto cleanup;
1877 1878 1879
    }

    if (!STREQ_NULLABLE(oldnet->filter, newnet->filter)) {
1880
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1881
                       _("Changing filter params is not supported"));
1882
        goto cleanup;
1883 1884 1885
    }

    if (newnet->bandwidth != NULL) {
1886
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1887
                       _("Setting bandwidth params is not supported"));
1888
        goto cleanup;
1889 1890 1891 1892
    }

    for (i = 0; i < sizeof(newnet->vlan); i++) {
        if (((char *)&newnet->vlan)[i] != 0) {
1893
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1894
                           _("Setting vlan params is not supported"));
1895
            goto cleanup;
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
        }
    }

    /* 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)) {
1917 1918 1919 1920 1921 1922 1923 1924
        if (STREQ_NULLABLE(newnet->data.network.name,
                           PARALLELS_ROUTED_NETWORK_NAME)) {
            virCommandAddArgFormat(cmd, "--type=routed");
        } else {
            virCommandAddArgFormat(cmd, "--network=%s",
                                   newnet->data.network.name);
        }

1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
        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 */
1939 1940
        ret = 0;
        goto cleanup;
1941 1942
    }

1943 1944
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;
1945

1946 1947
    ret = 0;

1948
 cleanup:
1949 1950 1951 1952
    if (create)
        VIR_FREE(oldnet);
    virCommandFree(cmd);
    return ret;
1953 1954 1955 1956 1957 1958 1959
}

static int
parallelsApplyIfacesParams(parallelsDomObjPtr pdom,
                            virDomainNetDefPtr *oldnets, int nold,
                            virDomainNetDefPtr *newnets, int nnew)
{
1960
    size_t i, j;
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
    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;
}

2004
static int
D
Dmitry Guryanov 已提交
2005
parallelsApplyChanges(virDomainObjPtr dom, virDomainDefPtr new)
2006 2007
{
    char buf[32];
2008
    size_t i;
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025

    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)) {
2026
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2027 2028 2029 2030 2031
                       _("titles are not supported by parallels driver"));
        return -1;
    }

    if (new->blkio.ndevices > 0) {
2032
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2033 2034 2035 2036 2037 2038 2039
                       _("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) {
2040
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2041 2042 2043 2044 2045 2046
                       _("changing balloon parameters is not supported "
                         "by parallels driver"));
           return -1;
        }

        if (new->mem.max_balloon % (1 << 10) != 0) {
2047
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
                       _("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;
    }

2060
    if (old->mem.nhugepages != new->mem.nhugepages ||
2061 2062 2063 2064 2065
        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) {

2066
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2067 2068 2069 2070 2071 2072 2073
                       _("Memory parameter is not supported "
                         "by parallels driver"));
        return -1;
    }

    if (old->vcpus != new->vcpus) {
        if (new->vcpus != new->maxvcpus) {
2074
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
                       _("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) {
2088
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2089 2090 2091 2092 2093
                       _("changing cpu placement mode is not supported "
                         "by parallels driver"));
        return -1;
    }

2094 2095 2096
    if ((old->cpumask != NULL || new->cpumask != NULL) &&
        (old->cpumask == NULL || new->cpumask == NULL ||
        !virBitmapEqual(old->cpumask, new->cpumask))) {
2097

2098
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2099 2100 2101 2102 2103 2104
                       _("changing cpu mask is not supported "
                         "by parallels driver"));
        return -1;
    }

    if (old->cputune.shares != new->cputune.shares ||
2105
        old->cputune.sharesSpecified != new->cputune.sharesSpecified ||
2106 2107 2108 2109
        old->cputune.period != new->cputune.period ||
        old->cputune.quota != new->cputune.quota ||
        old->cputune.nvcpupin != new->cputune.nvcpupin) {

2110
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2111 2112 2113 2114
                       _("cputune is not supported by parallels driver"));
        return -1;
    }

2115
    if (!virDomainNumatuneEquals(old->numatune, new->numatune)) {
2116
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2117 2118 2119 2120 2121 2122 2123 2124 2125
                        _("numa parameters are not supported "
                          "by parallels driver"));
        return -1;
    }

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

2126
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2127 2128 2129 2130 2131
                       _("on_reboot, on_poweroff and on_crash parameters "
                         "are not supported by parallels driver"));
        return -1;
    }

2132 2133 2134 2135
    /* 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 */

2136
    if (!STREQ_NULLABLE(old->os.type, new->os.type) ||
2137
        old->os.arch != new->os.arch ||
2138 2139 2140 2141 2142 2143
        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) {
2144

2145
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2146 2147 2148 2149
                       _("changing OS parameters is not supported "
                         "by parallels driver"));
        return -1;
    }
2150 2151 2152 2153 2154
    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) {

2155
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2156 2157 2158 2159 2160 2161 2162 2163 2164
                           _("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)) {

2165
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2166 2167 2168 2169 2170 2171
                           _("changing OS parameters is not supported "
                             "by parallels driver"));
            return -1;
        }
    }

2172 2173

    if (!STREQ_NULLABLE(old->emulator, new->emulator)) {
2174
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2175 2176 2177 2178 2179
                       _("changing emulator is not supported "
                         "by parallels driver"));
        return -1;
    }

2180 2181
    for (i = 0; i < VIR_DOMAIN_FEATURE_LAST; i++) {
        if (old->features[i] != new->features[i]) {
2182
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2183 2184 2185 2186
                           _("changing features is not supported "
                             "by parallels driver"));
            return -1;
        }
2187 2188 2189 2190 2191
    }

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

2192
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2193 2194 2195 2196 2197 2198 2199 2200 2201
                       _("changing clock parameters is not supported "
                         "by parallels driver"));
        return -1;
    }

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

2202
    if (new->nfss != 0 ||
2203 2204 2205 2206 2207
        new->nsounds != 0 || new->nhostdevs != 0 ||
        new->nredirdevs != 0 || new->nsmartcards != 0 ||
        new->nparallels || new->nchannels != 0 ||
        new->nleases != 0 || new->nhubs != 0) {

2208
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
                       _("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))) {

2220
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
                       _("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 已提交
2237
    if (parallelsApplyDisksParams(pdom, old->disks, old->ndisks,
2238 2239
                                  new->disks, new->ndisks) < 0)
        return -1;
2240 2241 2242
    if (parallelsApplyIfacesParams(pdom, old->nets, old->nnets,
                                  new->nets, new->nnets) < 0)
        return -1;
2243

2244 2245 2246
    return 0;
}

2247
static int
2248
parallelsCreateVm(virConnectPtr conn ATTRIBUTE_UNUSED, virDomainDefPtr def)
2249 2250 2251 2252 2253
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    virUUIDFormat(def->uuid, uuidstr);

2254
    if (parallelsCmdRun(PRLCTL, "create", def->name, "--no-hdd",
2255
                        "--uuid", uuidstr, NULL) < 0)
2256
        return -1;
2257 2258 2259 2260

    return 0;
}

2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
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;

2284
 error:
2285 2286 2287
    return -1;
}

2288 2289 2290 2291 2292 2293
static virDomainPtr
parallelsDomainDefineXML(virConnectPtr conn, const char *xml)
{
    parallelsConnPtr privconn = conn->privateData;
    virDomainPtr ret = NULL;
    virDomainDefPtr def;
2294
    virDomainObjPtr olddom = NULL;
2295 2296

    parallelsDriverLock(privconn);
2297 2298
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_PARALLELS,
2299 2300 2301 2302 2303 2304
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Can't parse XML desc"));
        goto cleanup;
    }

2305 2306 2307
    olddom = virDomainObjListFindByUUID(privconn->domains, def->uuid);
    if (olddom == NULL) {
        virResetLastError();
2308 2309 2310 2311 2312 2313 2314 2315 2316
        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);
2317
            goto cleanup;
2318
        }
2319 2320
        if (parallelsLoadDomains(privconn, def->name))
            goto cleanup;
2321
        olddom = virDomainObjListFindByName(privconn->domains, def->name);
2322
        if (!olddom) {
2323 2324
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Domain for '%s' is not defined after creation"),
E
Eric Blake 已提交
2325
                           def->name ? def->name : _("(unnamed)"));
2326 2327
            goto cleanup;
        }
2328 2329
    }

D
Dmitry Guryanov 已提交
2330
    if (parallelsApplyChanges(olddom, def) < 0) {
2331
        virObjectUnlock(olddom);
2332 2333
        goto cleanup;
    }
2334
    virObjectUnlock(olddom);
2335

2336
    ret = virGetDomain(conn, def->name, def->uuid);
2337
    if (ret)
2338
        ret->id = def->id;
2339

2340
 cleanup:
2341 2342 2343 2344 2345
    virDomainDefFree(def);
    parallelsDriverUnlock(privconn);
    return ret;
}

2346 2347 2348 2349 2350 2351 2352
static int
parallelsNodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virNodeInfoPtr nodeinfo)
{
    return nodeGetInfo(nodeinfo);
}

2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
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;
}

2370

2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
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);
}


2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
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;
}


2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
static int
parallelsNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
                       unsigned char **cpumap,
                       unsigned int *online,
                       unsigned int flags)
{
    return nodeGetCPUMap(cpumap, online, flags);
}


2464
static virHypervisorDriver parallelsDriver = {
D
Dmitry Guryanov 已提交
2465 2466
    .no = VIR_DRV_PARALLELS,
    .name = "Parallels",
2467 2468 2469
    .connectOpen = parallelsConnectOpen,            /* 0.10.0 */
    .connectClose = parallelsConnectClose,          /* 0.10.0 */
    .connectGetVersion = parallelsConnectGetVersion,   /* 0.10.0 */
2470
    .connectGetHostname = parallelsConnectGetHostname,      /* 0.10.0 */
2471
    .nodeGetInfo = parallelsNodeGetInfo,      /* 0.10.0 */
2472
    .connectGetCapabilities = parallelsConnectGetCapabilities,      /* 0.10.0 */
2473
    .connectBaselineCPU = parallelsConnectBaselineCPU, /* 1.2.6 */
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
    .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 */
2484 2485 2486 2487
    .domainGetState = parallelsDomainGetState,        /* 0.10.0 */
    .domainGetXMLDesc = parallelsDomainGetXMLDesc,    /* 0.10.0 */
    .domainIsPersistent = parallelsDomainIsPersistent,        /* 0.10.0 */
    .domainGetAutostart = parallelsDomainGetAutostart,        /* 0.10.0 */
2488
    .domainGetVcpus = parallelsDomainGetVcpus, /* 1.2.6 */
2489 2490 2491 2492
    .domainSuspend = parallelsDomainSuspend,    /* 0.10.0 */
    .domainResume = parallelsDomainResume,    /* 0.10.0 */
    .domainDestroy = parallelsDomainDestroy,  /* 0.10.0 */
    .domainShutdown = parallelsDomainShutdown, /* 0.10.0 */
2493
    .domainCreate = parallelsDomainCreate,    /* 0.10.0 */
2494
    .domainDefineXML = parallelsDomainDefineXML,      /* 0.10.0 */
2495
    .nodeGetCPUMap = parallelsNodeGetCPUMap, /* 1.2.8 */
2496 2497 2498
    .connectIsEncrypted = parallelsConnectIsEncrypted, /* 1.2.5 */
    .connectIsSecure = parallelsConnectIsSecure, /* 1.2.5 */
    .connectIsAlive = parallelsConnectIsAlive, /* 1.2.5 */
D
Dmitry Guryanov 已提交
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
};

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

2519
    if (virRegisterHypervisorDriver(&parallelsDriver) < 0)
D
Dmitry Guryanov 已提交
2520
        return -1;
D
Dmitry Guryanov 已提交
2521 2522
    if (parallelsStorageRegister())
        return -1;
D
Dmitry Guryanov 已提交
2523 2524
    if (parallelsNetworkRegister())
        return -1;
D
Dmitry Guryanov 已提交
2525 2526 2527

    return 0;
}