vz_sdk.c 135.4 KB
Newer Older
1
/*
2
 * vz_sdk.c: core driver functions for managing
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Parallels Cloud Server hosts
 *
 * Copyright (C) 2014 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>
24
#include <stdarg.h>
25 26 27

#include "virerror.h"
#include "viralloc.h"
28 29
#include "virstring.h"
#include "virlog.h"
30
#include "datatypes.h"
31
#include "domain_conf.h"
32
#include "virtime.h"
33
#include "virhostcpu.h"
34
#include "virsocketaddr.h"
35

36
#include "storage/storage_driver.h"
37
#include "vz_sdk.h"
38 39 40 41

#define VIR_FROM_THIS VIR_FROM_PARALLELS
#define JOB_INFINIT_WAIT_TIMEOUT UINT_MAX

42 43
static int
prlsdkUUIDParse(const char *uuidstr, unsigned char *uuid);
44 45
static void
prlsdkConvertError(PRL_RESULT pret);
46 47
static PRL_RESULT
prlsdkEventsHandler(PRL_HANDLE prlEvent, PRL_VOID_PTR opaque);
48

49 50
VIR_LOG_INIT("parallels.sdk");

N
Nikolay Shirokovskiy 已提交
51 52 53
static PRL_HANDLE
prlsdkFindNetByMAC(PRL_HANDLE sdkdom, virMacAddrPtr mac);
static PRL_HANDLE
54
prlsdkGetDisk(PRL_HANDLE sdkdom, virDomainDiskDefPtr disk);
55 56 57
static bool
prlsdkInBootList(PRL_HANDLE sdkdom,
                 PRL_HANDLE sdktargetdev);
N
Nikolay Shirokovskiy 已提交
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
 * Log error description
 */
static void
logPrlErrorHelper(PRL_RESULT err, const char *filename,
                  const char *funcname, size_t linenr)
{
    char *msg1 = NULL, *msg2 = NULL;
    PRL_UINT32 len = 0;

    /* Get required buffer length */
    PrlApi_GetResultDescription(err, PRL_TRUE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg1, len) < 0)
        goto cleanup;

    /* get short error description */
    PrlApi_GetResultDescription(err, PRL_TRUE, PRL_FALSE, msg1, &len);

    PrlApi_GetResultDescription(err, PRL_FALSE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg2, len) < 0)
        goto cleanup;

    /* get long error description */
    PrlApi_GetResultDescription(err, PRL_FALSE, PRL_FALSE, msg2, &len);

    virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR,
                         filename, funcname, linenr,
                         _("%s %s"), msg1, msg2);

 cleanup:
    VIR_FREE(msg1);
    VIR_FREE(msg2);
}

#define logPrlError(code)                          \
    logPrlErrorHelper(code, __FILE__,              \
M
Michal Privoznik 已提交
97
                      __FUNCTION__, __LINE__)
98

99 100 101 102 103 104 105 106
#define prlsdkCheckRetGoto(ret, label)             \
    do {                                           \
        if (PRL_FAILED(ret)) {                     \
            logPrlError(ret);                      \
            goto label;                            \
        }                                          \
    } while (0)

107 108 109 110 111 112 113 114
#define prlsdkCheckRetExit(ret, code)              \
    do {                                           \
        if (PRL_FAILED(ret)) {                     \
            logPrlError(ret);                      \
            return code;                           \
        }                                          \
    } while (0)

115
static void
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
logPrlEventErrorHelper(PRL_HANDLE event, const char *filename,
                       const char *funcname, size_t linenr)
{
    char *msg1 = NULL, *msg2 = NULL;
    PRL_UINT32 len = 0;

    PrlEvent_GetErrString(event, PRL_TRUE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg1, len) < 0)
        goto cleanup;

    PrlEvent_GetErrString(event, PRL_TRUE, PRL_FALSE, msg1, &len);

    PrlEvent_GetErrString(event, PRL_FALSE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg2, len) < 0)
        goto cleanup;

    PrlEvent_GetErrString(event, PRL_FALSE, PRL_FALSE, msg2, &len);

    virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR,
                         filename, funcname, linenr,
                         _("%s %s"), msg1, msg2);
 cleanup:
    VIR_FREE(msg1);
    VIR_FREE(msg2);
}

144 145
static PRL_RESULT
getJobResultHelper(PRL_HANDLE job, unsigned int timeout, PRL_HANDLE *result,
146 147 148 149 150
                   const char *filename, const char *funcname,
                   size_t linenr)
{
    PRL_RESULT ret, retCode;

151
    if (PRL_FAILED(ret = PrlJob_Wait(job, timeout))) {
152 153 154 155
        logPrlErrorHelper(ret, filename, funcname, linenr);
        goto cleanup;
    }

156
    if (PRL_FAILED(ret = PrlJob_GetRetCode(job, &retCode))) {
157 158 159 160 161 162 163
        logPrlErrorHelper(ret, filename, funcname, linenr);
        goto cleanup;
    }

    if (retCode) {
        PRL_HANDLE err_handle;

164 165
        ret = retCode;

166
        /* Sometimes it's possible to get additional error info. */
167
        if (PRL_FAILED(retCode = PrlJob_GetError(job, &err_handle))) {
168 169 170 171
            logPrlErrorHelper(ret, filename, funcname, linenr);
            goto cleanup;
        }

172 173 174 175 176 177 178 179 180
        if (PRL_FAILED(retCode = PrlEvent_GetErrCode(err_handle, &retCode))) {
            logPrlErrorHelper(ret, filename, funcname, linenr);
            if (PRL_ERR_NO_DATA != retCode)
                logPrlError(retCode);
            PrlHandle_Free(err_handle);
            goto cleanup;
        }

        logPrlEventErrorHelper(err_handle, filename, funcname, linenr);
181 182 183

        PrlHandle_Free(err_handle);
    } else {
184
        ret = PrlJob_GetResult(job, result);
185 186
        if (PRL_FAILED(ret)) {
            logPrlErrorHelper(ret, filename, funcname, linenr);
187 188
            PrlHandle_Free(*result);
            *result = NULL;
189 190
            goto cleanup;
        }
191 192

        ret = PRL_ERR_SUCCESS;
193
    }
194 195 196

 cleanup:
    PrlHandle_Free(job);
197
    return ret;
198 199
}

200 201
#define getJobResult(job, result)                       \
    getJobResultHelper(job, JOB_INFINIT_WAIT_TIMEOUT,   \
M
Michal Privoznik 已提交
202
                       result, __FILE__, __FUNCTION__, __LINE__)
203

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static PRL_RESULT
getDomainJobResultHelper(PRL_HANDLE job, virDomainObjPtr dom,
                         unsigned int timeout, PRL_HANDLE *result,
                         const char *filename, const char *funcname,
                         size_t linenr)
{
    PRL_RESULT pret;

    if (dom)
        virObjectUnlock(dom);
    pret = getJobResultHelper(job, timeout, result, filename, funcname, linenr);
    if (dom)
        virObjectLock(dom);

    return pret;
}

#define getDomainJobResult(job, dom, result)                            \
    getDomainJobResultHelper(job, dom, JOB_INFINIT_WAIT_TIMEOUT,        \
                             result, __FILE__, __FUNCTION__, __LINE__)

225
static PRL_RESULT
226 227 228 229
waitJobHelper(PRL_HANDLE job, unsigned int timeout,
              const char *filename, const char *funcname,
              size_t linenr)
{
230 231
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_RESULT ret;
232

233 234 235 236
    ret = getJobResultHelper(job, timeout, &result,
                             filename, funcname, linenr);
    PrlHandle_Free(result);
    return ret;
237 238
}

239 240
#define waitJob(job)                                        \
    waitJobHelper(job, JOB_INFINIT_WAIT_TIMEOUT, __FILE__,  \
M
Michal Privoznik 已提交
241
                  __FUNCTION__, __LINE__)
242

243 244 245 246 247
static PRL_RESULT
waitDomainJobHelper(PRL_HANDLE job, virDomainObjPtr dom, unsigned int timeout,
                    const char *filename, const char *funcname,
                    size_t linenr)
{
248
    vzDomObjPtr pdom = dom->privateData;
249 250
    PRL_RESULT ret;

251 252 253 254 255 256 257
    if (pdom->job.cancelled) {
        virReportError(VIR_ERR_OPERATION_ABORTED, "%s",
                       _("Operation cancelled by client"));
        return PRL_ERR_FAILURE;
    }

    pdom->job.sdkJob = job;
258 259 260 261 262
    if (dom)
        virObjectUnlock(dom);
    ret = waitJobHelper(job, timeout, filename, funcname, linenr);
    if (dom)
        virObjectLock(dom);
263
    pdom->job.sdkJob = NULL;
264 265 266 267 268 269 270 271

    return ret;
}

#define waitDomainJob(job, dom)                                         \
    waitDomainJobHelper(job, dom, JOB_INFINIT_WAIT_TIMEOUT, __FILE__,   \
                        __FUNCTION__, __LINE__)

272 273
typedef PRL_RESULT (*prlsdkParamGetterType)(PRL_HANDLE, char*, PRL_UINT32*);

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
int
prlsdkCancelJob(virDomainObjPtr dom)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;
    PRL_HANDLE job;

    if (!privdom->job.active) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("no job is active on the domain"));
        return -1;
    }

   privdom->job.cancelled = true;
   job = PrlJob_Cancel(privdom->job.sdkJob);

   virObjectUnlock(dom);
   pret = waitJobHelper(job, JOB_INFINIT_WAIT_TIMEOUT,
                        __FILE__, __FUNCTION__, __LINE__);
   virObjectLock(dom);

   return PRL_FAILED(pret) ? -1 : 0;
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
static char*
prlsdkGetStringParamVar(prlsdkParamGetterType getter, PRL_HANDLE handle)
{
    PRL_RESULT pret;
    PRL_UINT32 buflen = 0;
    char *str = NULL;

    pret = getter(handle, NULL, &buflen);
    prlsdkCheckRetGoto(pret, error);

    if (VIR_ALLOC_N(str, buflen) < 0)
        goto error;

    pret = getter(handle, str, &buflen);
    prlsdkCheckRetGoto(pret, error);

    return str;

 error:
    VIR_FREE(str);
    return NULL;
}

static PRL_RESULT
prlsdkGetStringParamBuf(prlsdkParamGetterType getter,
                        PRL_HANDLE handle, char *buf, size_t size)
{
    PRL_UINT32 buflen = size;
    return getter(handle, buf, &buflen);
}
328

329
int
330
prlsdkInit(void)
331 332 333
{
    PRL_RESULT ret;

334 335 336
    /* Disable console output */
    PrlApi_SwitchConsoleLogging(0);

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    ret = PrlApi_InitEx(PARALLELS_API_VER, PAM_SERVER, 0, 0);
    if (PRL_FAILED(ret)) {
        logPrlError(ret);
        return -1;
    }

    return 0;
};

void
prlsdkDeinit(void)
{
    PrlApi_Deinit();
};

int
353
prlsdkConnect(vzDriverPtr driver)
354
{
355 356
    int ret = -1;
    PRL_RESULT pret;
357
    PRL_HANDLE job = PRL_INVALID_HANDLE;
358 359
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_HANDLE response = PRL_INVALID_HANDLE;
360
    char session_uuid[VIR_UUID_STRING_BRACED_BUFLEN];
361

362 363
    pret = PrlSrv_Create(&driver->server);
    prlsdkCheckRetExit(pret, -1);
364

365
    job = PrlSrv_LoginLocalEx(driver->server, NULL, 0,
366
                              PSL_HIGH_SECURITY, PACF_NON_INTERACTIVE_MODE);
367
    if (PRL_FAILED(getJobResult(job, &result)))
368
        goto destroy;
369 370

    pret = PrlResult_GetParam(result, &response);
371
    prlsdkCheckRetGoto(pret, logoff);
372

373 374
    pret = prlsdkGetStringParamBuf(PrlLoginResponse_GetSessionUuid,
                                   response, session_uuid, sizeof(session_uuid));
375
    prlsdkCheckRetGoto(pret, logoff);
376 377

    if (prlsdkUUIDParse(session_uuid, driver->session_uuid) < 0)
378 379 380 381 382 383
        goto logoff;

    pret = PrlSrv_RegEventHandler(driver->server,
                                  prlsdkEventsHandler,
                                  driver);
    prlsdkCheckRetGoto(pret, logoff);
384 385 386 387 388 389 390 391

    ret = 0;

 cleanup:
    PrlHandle_Free(result);
    PrlHandle_Free(response);

    return ret;
392 393 394 395 396 397 398 399 400 401

 logoff:
    job = PrlSrv_Logoff(driver->server);
    waitJob(job);

 destroy:
    PrlHandle_Free(driver->server);
    driver->server = PRL_INVALID_HANDLE;

    goto cleanup;
402 403 404
}

void
405
prlsdkDisconnect(vzDriverPtr driver)
406 407
{
    PRL_HANDLE job;
408 409 410 411 412 413 414 415 416 417
    PRL_RESULT ret;

    if (driver->server == PRL_INVALID_HANDLE)
        return;

    ret = PrlSrv_UnregEventHandler(driver->server,
                                   prlsdkEventsHandler,
                                   driver);
    if (PRL_FAILED(ret))
        logPrlError(ret);
418

419
    job = PrlSrv_Logoff(driver->server);
420
    waitJob(job);
421

422
    PrlHandle_Free(driver->server);
423
    driver->server = PRL_INVALID_HANDLE;
424
}
425 426

static int
427
prlsdkSdkDomainLookup(vzDriverPtr driver,
428 429 430 431 432 433 434 435 436
                      const char *id,
                      unsigned int flags,
                      PRL_HANDLE *sdkdom)
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_RESULT pret = PRL_ERR_UNINITIALIZED;
    int ret = -1;

437
    job = PrlSrv_GetVmConfig(driver->server, id, flags);
438
    if (PRL_FAILED(getJobResult(job, &result)))
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, sdkdom);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
    PrlHandle_Free(result);
    return ret;
}

static void
prlsdkUUIDFormat(const unsigned char *uuid, char *uuidstr)
{
    virUUIDFormat(uuid, uuidstr + 1);

    uuidstr[0] = '{';
    uuidstr[VIR_UUID_STRING_BUFLEN] = '}';
    uuidstr[VIR_UUID_STRING_BUFLEN + 1] = '\0';
}

static PRL_HANDLE
462
prlsdkSdkDomainLookupByUUID(vzDriverPtr driver, const unsigned char *uuid)
463
{
464
    char uuidstr[VIR_UUID_STRING_BRACED_BUFLEN];
465 466 467 468
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;

    prlsdkUUIDFormat(uuid, uuidstr);

469
    if (prlsdkSdkDomainLookup(driver, uuidstr,
470 471 472 473 474 475 476 477 478 479
                              PGVC_SEARCH_BY_UUID, &sdkdom) < 0) {
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
        return PRL_INVALID_HANDLE;
    }

    return sdkdom;
}

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
PRL_HANDLE
prlsdkSdkDomainLookupByName(vzDriverPtr driver, const char *name)
{
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;

    if (prlsdkSdkDomainLookup(driver, name,
                              PGVC_SEARCH_BY_NAME, &sdkdom) < 0) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
        return PRL_INVALID_HANDLE;
    }

    return sdkdom;
}

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static int
prlsdkUUIDParse(const char *uuidstr, unsigned char *uuid)
{
    char *tmp = NULL;
    int ret = -1;

    virCheckNonNullArgGoto(uuidstr, error);
    virCheckNonNullArgGoto(uuid, error);

    if (VIR_STRDUP(tmp, uuidstr) < 0)
        goto error;

    tmp[strlen(tmp) - 1] = '\0';

    /* trim curly braces */
510
    if (virUUIDParse(tmp + 1, uuid) < 0)
511 512 513 514 515 516 517 518 519
        goto error;

    ret = 0;
 error:
    VIR_FREE(tmp);
    return ret;
}

static int
520
prlsdkGetDomainState(virDomainObjPtr dom, PRL_HANDLE sdkdom, VIRTUAL_MACHINE_STATE_PTR vmState)
521 522 523 524 525 526 527 528 529
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_HANDLE vmInfo = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
    int ret = -1;

    job = PrlVm_GetState(sdkdom);

530
    if (PRL_FAILED(getDomainJobResult(job, dom, &result)))
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, &vmInfo);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmInfo_GetState(vmInfo, vmState);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
    PrlHandle_Free(vmInfo);
    PrlHandle_Free(result);
    return ret;
}

static int
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
prlsdkAddDomainVideoInfoCt(virDomainDefPtr def)
{
    virDomainVideoDefPtr video = NULL;
    int ret = -1;

    if (def->ngraphics == 0)
        return 0;

    if (VIR_ALLOC(video) < 0)
        goto cleanup;

    video->type = VIR_DOMAIN_VIDEO_TYPE_PARALLELS;
    video->vram = 0;
    video->heads = 1;

    if (VIR_APPEND_ELEMENT(def->videos, def->nvideos, video) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    virDomainVideoDefFree(video);

    return ret;
}

static int
prlsdkAddDomainVideoInfoVm(PRL_HANDLE sdkdom, virDomainDefPtr def)
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
{
    virDomainVideoDefPtr video = NULL;
    virDomainVideoAccelDefPtr accel = NULL;
    PRL_RESULT ret;
    PRL_UINT32 videoRam;

    /* video info */
    ret = PrlVmCfg_GetVideoRamSize(sdkdom, &videoRam);
    prlsdkCheckRetGoto(ret, error);

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

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

    if (VIR_APPEND_ELEMENT_COPY(def->videos, def->nvideos, video) < 0)
        goto error;

    video->type = VIR_DOMAIN_VIDEO_TYPE_VGA;
    video->vram = videoRam << 10; /* from mbibytes to kbibytes */
    video->heads = 1;
    video->accel = accel;

    return 0;

 error:
    VIR_FREE(accel);
    virDomainVideoDefFree(video);
    return -1;
}

607
static int
608
prlsdkGetDiskId(PRL_HANDLE disk, int *bus, char **dst)
609 610 611 612 613 614 615
{
    PRL_RESULT pret;
    PRL_UINT32 pos, ifType;

    pret = PrlVmDev_GetStackIndex(disk, &pos);
    prlsdkCheckRetExit(pret, -1);

616 617
    pret = PrlVmDev_GetIfaceType(disk, &ifType);
    prlsdkCheckRetExit(pret, -1);
618 619 620 621 622 623 624

    switch (ifType) {
    case PMS_IDE_DEVICE:
        *bus = VIR_DOMAIN_DISK_BUS_IDE;
        *dst = virIndexToDiskName(pos, "hd");
        break;
    case PMS_SCSI_DEVICE:
625
    case PMS_UNKNOWN_DEVICE:
626 627 628 629 630 631 632 633 634 635 636 637 638
        *bus = VIR_DOMAIN_DISK_BUS_SCSI;
        *dst = virIndexToDiskName(pos, "sd");
        break;
    case PMS_SATA_DEVICE:
        *bus = VIR_DOMAIN_DISK_BUS_SATA;
        *dst = virIndexToDiskName(pos, "sd");
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown disk bus: %X"), ifType);
        return -1;
    }

639 640 641
    if (NULL == *dst)
        return -1;

642 643 644
    return 0;
}

645
static int
646
prlsdkGetDiskInfo(vzDriverPtr driver,
647
                  PRL_HANDLE prldisk,
D
Dmitry Guryanov 已提交
648
                  virDomainDiskDefPtr disk,
649 650
                  bool isCdrom,
                  bool isCt)
651 652 653 654
{
    char *buf = NULL;
    PRL_RESULT pret;
    PRL_UINT32 emulatedType;
655
    PRL_UINT32 size;
656
    virDomainDeviceDriveAddressPtr address;
657
    int busIdx, devIdx;
658 659 660 661 662 663
    int ret = -1;

    pret = PrlVmDev_GetEmulatedType(prldisk, &emulatedType);
    prlsdkCheckRetGoto(pret, cleanup);
    if (emulatedType == PDT_USE_IMAGE_FILE) {
        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
664
        if (isCdrom) {
665
            virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
666 667
        } else {
            if (isCt)
668
                virDomainDiskSetFormat(disk, driver->vzCaps.ctDiskFormat);
669
            else
670
                virDomainDiskSetFormat(disk, driver->vzCaps.vmDiskFormat);
671
        }
672 673
    } else {
        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_BLOCK);
674
        virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
675 676
    }

677
    if (isCdrom) {
D
Dmitry Guryanov 已提交
678
        disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
679 680
        disk->src->readonly = true;
    } else {
D
Dmitry Guryanov 已提交
681
        disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
682
    }
D
Dmitry Guryanov 已提交
683

684
    if (!(buf = prlsdkGetStringParamVar(PrlVmDev_GetFriendlyName, prldisk)))
685 686
        goto cleanup;

687
    if (*buf != '\0' && virDomainDiskSetSource(disk, buf) < 0)
688 689
        goto cleanup;

690
    if (prlsdkGetDiskId(prldisk, &disk->bus, &disk->dst) < 0)
691 692
        goto cleanup;

693
    if (virDiskNameToBusDeviceIndex(disk, &busIdx, &devIdx) < 0)
694
        goto cleanup;
695

696 697 698 699 700
    address = &disk->info.addr.drive;
    address->bus = busIdx;
    address->target = 0;
    address->unit = devIdx;

701 702
    disk->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE;

703 704 705 706 707 708 709 710
    if (!isCdrom) {
        if (!(disk->serial = prlsdkGetStringParamVar(PrlVmDevHd_GetSerialNumber, prldisk)))
            goto cleanup;

        if (*disk->serial == '\0')
            VIR_FREE(disk->serial);
    }

711 712 713
    if (virDomainDiskSetDriver(disk, "vz") < 0)
        goto cleanup;

714 715 716 717 718 719 720
    if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
        pret = PrlVmDevHd_GetDiskSize(prldisk, &size);
        prlsdkCheckRetGoto(pret, cleanup);
        /* from MiB to bytes */
        disk->src->capacity = ((unsigned long long)size) << 20;
    }

721 722 723 724 725 726 727
    ret = 0;

 cleanup:
    VIR_FREE(buf);
    return ret;
}

728 729 730 731 732 733
static int
prlsdkGetFSInfo(PRL_HANDLE prldisk,
                virDomainFSDefPtr fs)
{
    char *buf = NULL;
    int ret = -1;
734 735
    char **matches = NULL;
    virURIPtr uri = NULL;
736 737 738 739 740 741 742 743 744 745

    fs->type = VIR_DOMAIN_FS_TYPE_FILE;
    fs->fsdriver = VIR_DOMAIN_FS_DRIVER_TYPE_PLOOP;
    fs->accessmode = VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH;
    fs->wrpolicy = VIR_DOMAIN_FS_WRPOLICY_DEFAULT;
    fs->format = VIR_STORAGE_FILE_PLOOP;

    fs->readonly = false;
    fs->symlinksResolved = false;

746
    if (!(buf = prlsdkGetStringParamVar(PrlVmDevHd_GetStorageURL, prldisk)))
747 748
        goto cleanup;

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
    if (!virStringIsEmpty(buf)) {
        if (!(uri = virURIParse(buf)))
            goto cleanup;
        if (STRNEQ("libvirt", uri->scheme)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown uri scheme: '%s'"),
                           uri->scheme);
            goto cleanup;
        }

        if (!(matches = virStringSplitCount(uri->path, "/", 0, NULL)) ||
            !matches[0]) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("splitting StorageUrl failed %s"), uri->path);
            goto cleanup;
        }
        if (!matches[1]) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("can't identify pool in uri %s "), uri->path);
            goto cleanup;
        }
        if (!matches[2]) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("can't identify volume in uri %s"), uri->path);
            goto cleanup;
        }
        fs->type = VIR_DOMAIN_FS_TYPE_VOLUME;
        if (VIR_ALLOC(fs->src->srcpool) < 0)
            goto cleanup;
        if (VIR_STRDUP(fs->src->srcpool->pool, matches[1]) < 0)
            goto cleanup;
        if (VIR_STRDUP(fs->src->srcpool->volume, matches[2]) < 0)
            goto cleanup;
        VIR_FREE(buf);
    } else {
        fs->type = VIR_DOMAIN_FS_TYPE_FILE;
        if (!(buf = prlsdkGetStringParamVar(PrlVmDev_GetImagePath, prldisk)))
            goto cleanup;
787

788 789 790
        fs->src->path = buf;
        buf = NULL;
    }
791
    if (!(buf = prlsdkGetStringParamVar(PrlVmDevHd_GetMountPoint, prldisk)))
792 793 794 795 796 797 798 799 800
        goto cleanup;

    fs->dst = buf;
    buf = NULL;

    ret = 0;

 cleanup:
    VIR_FREE(buf);
801
    virStringListFree(matches);
802 803 804
    return ret;
}

805
static int
806
prlsdkAddDomainHardDisksInfo(vzDriverPtr driver, PRL_HANDLE sdkdom, virDomainDefPtr def)
807 808 809 810 811 812
{
    PRL_RESULT pret;
    PRL_UINT32 hddCount;
    PRL_UINT32 i;
    PRL_HANDLE hdd = PRL_INVALID_HANDLE;
    virDomainDiskDefPtr disk = NULL;
813
    virDomainFSDefPtr fs = NULL;
814 815 816 817 818

    pret = PrlVmCfg_GetHardDisksCount(sdkdom, &hddCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < hddCount; ++i) {
819 820 821

        PRL_UINT32 emulatedType;

822 823 824
        pret = PrlVmCfg_GetHardDisk(sdkdom, i, &hdd);
        prlsdkCheckRetGoto(pret, error);

825 826 827
        pret = PrlVmDev_GetEmulatedType(hdd, &emulatedType);
        prlsdkCheckRetGoto(pret, error);

828 829
        if (IS_CT(def) &&
            prlsdkInBootList(sdkdom, hdd)) {
830

831
            if (!(fs = virDomainFSDefNew()))
832 833 834 835 836 837 838 839 840
                goto error;

            if (prlsdkGetFSInfo(hdd, fs) < 0)
                goto error;

            if (virDomainFSInsert(def, fs) < 0)
                goto error;

            fs = NULL;
841 842 843
            PrlHandle_Free(hdd);
            hdd = PRL_INVALID_HANDLE;
        } else {
844
            if (!(disk = virDomainDiskDefNew(NULL)))
845 846
                goto error;

847
            if (prlsdkGetDiskInfo(driver, hdd, disk, false, IS_CT(def)) < 0)
848 849
                goto error;

850
            if (virDomainDiskInsert(def, disk) < 0)
851
                goto error;
852 853 854 855

            disk = NULL;
            PrlHandle_Free(hdd);
            hdd = PRL_INVALID_HANDLE;
856 857 858 859 860 861 862 863
        }
    }

    return 0;

 error:
    PrlHandle_Free(hdd);
    virDomainDiskDefFree(disk);
864
    virDomainFSDefFree(fs);
865 866 867
    return -1;
}

D
Dmitry Guryanov 已提交
868
static int
869
prlsdkAddDomainOpticalDisksInfo(vzDriverPtr driver, PRL_HANDLE sdkdom, virDomainDefPtr def)
D
Dmitry Guryanov 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883
{
    PRL_RESULT pret;
    PRL_UINT32 cdromsCount;
    PRL_UINT32 i;
    PRL_HANDLE cdrom = PRL_INVALID_HANDLE;
    virDomainDiskDefPtr disk = NULL;

    pret = PrlVmCfg_GetOpticalDisksCount(sdkdom, &cdromsCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < cdromsCount; ++i) {
        pret = PrlVmCfg_GetOpticalDisk(sdkdom, i, &cdrom);
        prlsdkCheckRetGoto(pret, error);

884
        if (!(disk = virDomainDiskDefNew(NULL)))
D
Dmitry Guryanov 已提交
885 886
            goto error;

887
        if (prlsdkGetDiskInfo(driver, cdrom, disk, true, IS_CT(def)) < 0)
D
Dmitry Guryanov 已提交
888 889 890 891 892
            goto error;

        PrlHandle_Free(cdrom);
        cdrom = PRL_INVALID_HANDLE;

893
        if (virDomainDiskInsert(def, disk) < 0)
D
Dmitry Guryanov 已提交
894 895 896 897 898 899 900 901 902 903 904
            goto error;
    }

    return 0;

 error:
    PrlHandle_Free(cdrom);
    virDomainDiskDefFree(disk);
    return -1;
}

905
static virNetDevIPAddrPtr
906 907 908 909 910
prlsdkParseNetAddress(char *addr)
{
    char *maskstr = NULL;
    int nbits;
    virSocketAddr mask;
911
    virNetDevIPAddrPtr ip = NULL, ret = NULL;
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960

    if (!(maskstr = strchr(addr, '/')))
        goto cleanup;

    *maskstr = '\0';
    ++maskstr;

    if (VIR_ALLOC(ip) < 0)
        goto cleanup;

    if (virSocketAddrParse(&ip->address, addr, AF_UNSPEC) < 0)
        goto cleanup;

    if (virSocketAddrParse(&mask, maskstr, AF_UNSPEC) < 0)
        goto cleanup;

    if ((nbits = virSocketAddrGetNumNetmaskBits(&mask)) < 0)
        goto cleanup;
    ip->prefix = nbits;

    ret = ip;
    ip = NULL;

 cleanup:
    if (!ret)
        VIR_WARN("cannot parse network address '%s'", addr);

    VIR_FREE(ip);
    VIR_FREE(addr);

    return ret;
}

static int
prlsdkGetNetAddresses(PRL_HANDLE sdknet, virDomainNetDefPtr net)
{
    int ret = -1;
    PRL_HANDLE addrlist = PRL_INVALID_HANDLE;
    PRL_UINT32 num;
    size_t i;
    PRL_RESULT pret;

    pret = PrlVmDevNet_GetNetAddresses(sdknet, &addrlist);
    prlsdkCheckRetGoto(pret, cleanup);

    PrlStrList_GetItemsCount(addrlist, &num);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < num; ++i) {
961
        virNetDevIPAddrPtr ip = NULL;
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
        PRL_UINT32 buflen = 0;
        char *addr;

        pret = PrlStrList_GetItem(addrlist, i, NULL, &buflen);
        prlsdkCheckRetGoto(pret, cleanup);

        if (VIR_ALLOC_N(addr, buflen) < 0)
            goto cleanup;

        pret = PrlStrList_GetItem(addrlist, i, addr, &buflen);
        prlsdkCheckRetGoto(pret, cleanup);

        if (!(ip = prlsdkParseNetAddress(addr)))
            continue;

977
        if (VIR_APPEND_ELEMENT(net->guestIP.ips, net->guestIP.nips, ip) < 0) {
978 979 980 981 982 983 984 985 986 987 988 989
            VIR_FREE(ip);
            goto cleanup;
        }
    }

    ret = 0;
 cleanup:

    PrlHandle_Free(addrlist);
    return ret;
}

990 991 992 993 994 995
static int
prlsdkGetRoutes(PRL_HANDLE sdknet, virDomainNetDefPtr net)
{
    int ret = -1;
    char *gw = NULL;
    char *gw6 = NULL;
996
    virNetDevIPRoutePtr route = NULL;
997 998 999 1000 1001 1002 1003 1004

    if (!(gw = prlsdkGetStringParamVar(PrlVmDevNet_GetDefaultGateway, sdknet)))
        goto cleanup;

    if (!(gw6 = prlsdkGetStringParamVar(PrlVmDevNet_GetDefaultGatewayIPv6, sdknet)))
        goto cleanup;

    if (*gw != '\0') {
1005 1006

        if (!(route = virNetDevIPRouteCreate(_("Domain interface"),
1007 1008 1009 1010
                                               "ipv4", VIR_SOCKET_ADDR_IPV4_ALL,
                                               NULL, gw, 0, true, 0, false)))
            goto cleanup;

1011
        if (VIR_APPEND_ELEMENT(net->guestIP.routes, net->guestIP.nroutes, route) < 0)
1012 1013 1014 1015
            goto cleanup;
    }

    if (*gw6 != '\0') {
1016
        if (!(route = virNetDevIPRouteCreate(_("Domain interface"),
1017 1018 1019 1020
                                               "ipv6", VIR_SOCKET_ADDR_IPV6_ALL,
                                               NULL, gw6, 0, true, 0, false)))
            goto cleanup;

1021
        if (VIR_APPEND_ELEMENT(net->guestIP.routes, net->guestIP.nroutes, route) < 0)
1022 1023 1024 1025 1026 1027
            goto cleanup;
    }

    ret = 0;

 cleanup:
1028
    virNetDevIPRouteFree(route);
1029 1030 1031 1032 1033 1034
    VIR_FREE(gw);
    VIR_FREE(gw6);

    return ret;
}

1035 1036 1037 1038 1039 1040 1041
static int
prlsdkGetNetInfo(PRL_HANDLE netAdapter, virDomainNetDefPtr net, bool isCt)
{
    char macstr[VIR_MAC_STRING_BUFLEN];
    PRL_UINT32 netAdapterIndex;
    PRL_UINT32 emulatedType;
    PRL_RESULT pret;
1042
    PRL_BOOL isConnected, isMacFilter;
1043 1044 1045 1046
    int ret = -1;

    /* use device name, shown by prlctl as target device
     * for identifying network adapter in virDomainDefineXML */
1047 1048
    if (!(net->ifname = prlsdkGetStringParamVar(PrlVmDevNet_GetHostInterfaceName,
                                                netAdapter)))
1049 1050
        goto cleanup;

1051 1052 1053
    pret = PrlVmDev_GetIndex(netAdapter, &netAdapterIndex);
    prlsdkCheckRetGoto(pret, cleanup);

1054 1055 1056 1057
    if (isCt && netAdapterIndex == (PRL_UINT32) -1) {
        /* venet devices don't have mac address and
         * always up */
        net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP;
1058
        net->type = VIR_DOMAIN_NET_TYPE_NETWORK;
1059
        if (VIR_STRDUP(net->data.network.name,
1060
                       PARALLELS_DOMAIN_ROUTED_NETWORK_NAME) < 0)
1061 1062 1063 1064
            goto cleanup;
        return 0;
    }

1065 1066
    pret = prlsdkGetStringParamBuf(PrlVmDevNet_GetMacAddressCanonical,
                                   netAdapter, macstr, sizeof(macstr));
1067 1068 1069 1070 1071
    prlsdkCheckRetGoto(pret, cleanup);

    if (virMacAddrParse(macstr, &net->mac) < 0)
        goto cleanup;

1072 1073 1074
    if (prlsdkGetNetAddresses(netAdapter, net) < 0)
        goto cleanup;

1075 1076 1077
    if (prlsdkGetRoutes(netAdapter, net) < 0)
        goto cleanup;

1078 1079 1080 1081
    pret = PrlVmDev_GetEmulatedType(netAdapter, &emulatedType);
    prlsdkCheckRetGoto(pret, cleanup);

    if (emulatedType == PNA_ROUTED) {
1082
        net->type = VIR_DOMAIN_NET_TYPE_NETWORK;
1083
        if (VIR_STRDUP(net->data.network.name,
1084
                       PARALLELS_DOMAIN_ROUTED_NETWORK_NAME) < 0)
1085 1086
            goto cleanup;
    } else {
1087
        char *netid =
1088
              prlsdkGetStringParamVar(PrlVmDevNet_GetVirtualNetworkId,
1089
                                      netAdapter);
1090

1091
        if (emulatedType == PNA_BRIDGE) {
1092
            net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
1093 1094
            if (netid)
                net->data.bridge.brname = netid;
1095 1096
        } else {
            net->type = VIR_DOMAIN_NET_TYPE_NETWORK;
1097 1098
            if (netid)
                net->data.network.name = netid;
1099
        }
1100 1101
    }

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    if (!isCt) {
        PRL_VM_NET_ADAPTER_TYPE type;
        pret = PrlVmDevNet_GetAdapterType(netAdapter, &type);
        prlsdkCheckRetGoto(pret, cleanup);

        switch (type) {
        case PNT_RTL:
            if (VIR_STRDUP(net->model, "rtl8139") < 0)
                goto cleanup;
            break;
        case PNT_E1000:
            if (VIR_STRDUP(net->model, "e1000") < 0)
                goto cleanup;
            break;
        case PNT_VIRTIO:
            if (VIR_STRDUP(net->model, "virtio") < 0)
                goto cleanup;
            break;
        default:
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown adapter type: %X"), type);
            goto cleanup;
        }
    }

1127 1128 1129 1130 1131 1132 1133 1134
    pret = PrlVmDev_IsConnected(netAdapter, &isConnected);
    prlsdkCheckRetGoto(pret, cleanup);

    if (isConnected)
        net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP;
    else
        net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN;

1135 1136 1137 1138 1139 1140
    pret = PrlVmDevNet_IsPktFilterPreventMacSpoof(netAdapter, &isMacFilter);
    prlsdkCheckRetGoto(pret, cleanup);

    net->trustGuestRxFilters = isMacFilter ? VIR_TRISTATE_BOOL_YES :
                                             VIR_TRISTATE_BOOL_NO;

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    ret = 0;
 cleanup:
    return ret;
}

static int
prlsdkAddDomainNetInfo(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    virDomainNetDefPtr net = NULL;
    PRL_RESULT ret;
    PRL_HANDLE netAdapter;
    PRL_UINT32 netAdaptersCount;
    PRL_UINT32 i;

    ret = PrlVmCfg_GetNetAdaptersCount(sdkdom, &netAdaptersCount);
    prlsdkCheckRetGoto(ret, error);
    for (i = 0; i < netAdaptersCount; ++i) {
        ret = PrlVmCfg_GetNetAdapter(sdkdom, i, &netAdapter);
        prlsdkCheckRetGoto(ret, error);

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

        if (prlsdkGetNetInfo(netAdapter, net, IS_CT(def)) < 0)
            goto error;

        PrlHandle_Free(netAdapter);
        netAdapter = PRL_INVALID_HANDLE;

        if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0)
            goto error;
    }

    return 0;

 error:
    PrlHandle_Free(netAdapter);
    virDomainNetDefFree(net);
    return -1;
}

static int
prlsdkGetSerialInfo(PRL_HANDLE serialPort, virDomainChrDefPtr chr)
{
    PRL_RESULT pret;
    PRL_UINT32 serialPortIndex;
    PRL_UINT32 emulatedType;
    char *friendlyName = NULL;
1189
    PRL_SERIAL_PORT_SOCKET_OPERATION_MODE socket_mode;
1190 1191 1192
    char *uristr = NULL;
    virURIPtr uri = NULL;
    int ret = -1;
1193 1194 1195 1196

    chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
    chr->targetTypeAttr = false;
    pret = PrlVmDev_GetIndex(serialPort, &serialPortIndex);
1197
    prlsdkCheckRetGoto(pret, cleanup);
1198 1199 1200
    chr->target.port = serialPortIndex;

    pret = PrlVmDev_GetEmulatedType(serialPort, &emulatedType);
1201
    prlsdkCheckRetGoto(pret, cleanup);
1202

1203 1204
    if (!(friendlyName = prlsdkGetStringParamVar(PrlVmDev_GetFriendlyName,
                                                 serialPort)))
1205
        goto cleanup;
1206

1207
    pret = PrlVmDevSerial_GetSocketMode(serialPort, &socket_mode);
1208
    prlsdkCheckRetGoto(pret, cleanup);
1209

1210 1211
    switch (emulatedType) {
    case PDT_USE_OUTPUT_FILE:
1212 1213
        chr->source->type = VIR_DOMAIN_CHR_TYPE_FILE;
        chr->source->data.file.path = friendlyName;
1214
        friendlyName = NULL;
1215 1216
        break;
    case PDT_USE_SERIAL_PORT_SOCKET_MODE:
1217 1218 1219
        chr->source->type = VIR_DOMAIN_CHR_TYPE_UNIX;
        chr->source->data.nix.path = friendlyName;
        chr->source->data.nix.listen = socket_mode == PSP_SERIAL_SOCKET_SERVER;
1220
        friendlyName = NULL;
1221 1222
        break;
    case PDT_USE_REAL_DEVICE:
1223 1224
        chr->source->type = VIR_DOMAIN_CHR_TYPE_DEV;
        chr->source->data.file.path = friendlyName;
1225 1226 1227
        friendlyName = NULL;
        break;
    case PDT_USE_TCP:
1228
        chr->source->type = VIR_DOMAIN_CHR_TYPE_TCP;
1229 1230 1231 1232
        if (virAsprintf(&uristr, "tcp://%s", friendlyName) < 0)
            goto cleanup;
        if (!(uri = virURIParse(uristr)))
            goto cleanup;
1233
        if (VIR_STRDUP(chr->source->data.tcp.host, uri->server) < 0)
1234
            goto cleanup;
1235
        if (virAsprintf(&chr->source->data.tcp.service, "%d", uri->port) < 0)
1236
            goto cleanup;
1237
        chr->source->data.tcp.listen = socket_mode == PSP_SERIAL_SOCKET_SERVER;
1238 1239
        break;
    case PDT_USE_UDP:
1240
        chr->source->type = VIR_DOMAIN_CHR_TYPE_UDP;
1241 1242 1243 1244
        if (virAsprintf(&uristr, "udp://%s", friendlyName) < 0)
            goto cleanup;
        if (!(uri = virURIParse(uristr)))
            goto cleanup;
1245
        if (VIR_STRDUP(chr->source->data.udp.bindHost, uri->server) < 0)
1246
            goto cleanup;
1247
        if (virAsprintf(&chr->source->data.udp.bindService, "%d", uri->port) < 0)
1248
            goto cleanup;
1249
        if (VIR_STRDUP(chr->source->data.udp.connectHost, uri->server) < 0)
1250
            goto cleanup;
1251
        if (virAsprintf(&chr->source->data.udp.connectService, "%d", uri->port) < 0)
1252
            goto cleanup;
1253 1254 1255 1256
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown serial type: %X"), emulatedType);
1257
        goto cleanup;
1258 1259 1260
        break;
    }

1261 1262 1263
    ret = 0;

 cleanup:
1264
    VIR_FREE(friendlyName);
1265 1266 1267 1268
    VIR_FREE(uristr);
    virURIFree(uri);

    return ret;
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
}


static int
prlsdkAddSerialInfo(PRL_HANDLE sdkdom,
                    virDomainChrDefPtr **serials,
                    size_t *nserials)
{
    PRL_RESULT ret;
    PRL_HANDLE serialPort;
    PRL_UINT32 serialPortsCount;
    PRL_UINT32 i;
    virDomainChrDefPtr chr = NULL;

    ret = PrlVmCfg_GetSerialPortsCount(sdkdom, &serialPortsCount);
    prlsdkCheckRetGoto(ret, cleanup);
    for (i = 0; i < serialPortsCount; ++i) {
        ret = PrlVmCfg_GetSerialPort(sdkdom, i, &serialPort);
        prlsdkCheckRetGoto(ret, cleanup);

1289
        if (!(chr = virDomainChrDefNew(NULL)))
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
            goto cleanup;

        if (prlsdkGetSerialInfo(serialPort, chr))
            goto cleanup;

        PrlHandle_Free(serialPort);
        serialPort = PRL_INVALID_HANDLE;

        if (VIR_APPEND_ELEMENT(*serials, *nserials, chr) < 0)
            goto cleanup;
    }

    return 0;

 cleanup:
    PrlHandle_Free(serialPort);
    virDomainChrDefFree(chr);
    return -1;
}


static int
1312
prlsdkAddDomainHardware(vzDriverPtr driver, PRL_HANDLE sdkdom, virDomainDefPtr def)
1313
{
1314 1315
    if (IS_CT(def)) {
        if (prlsdkAddDomainVideoInfoCt(def) < 0)
1316
            goto error;
1317 1318 1319 1320
    } else {
        if (prlsdkAddDomainVideoInfoVm(sdkdom, def) < 0)
            goto error;
    }
1321

1322
    if (prlsdkAddDomainHardDisksInfo(driver, sdkdom, def) < 0)
1323 1324
        goto error;

1325
    if (prlsdkAddDomainOpticalDisksInfo(driver, sdkdom, def) < 0)
D
Dmitry Guryanov 已提交
1326 1327
        goto error;

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    if (prlsdkAddDomainNetInfo(sdkdom, def) < 0)
        goto error;

    if (prlsdkAddSerialInfo(sdkdom,
                            &def->serials,
                            &def->nserials) < 0)
        goto error;

    return 0;
 error:
    return -1;
}


static int
prlsdkAddVNCInfo(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    virDomainGraphicsDefPtr gr = NULL;
    PRL_VM_REMOTE_DISPLAY_MODE vncMode;
    PRL_UINT32 port;
    PRL_RESULT pret;
N
Nikolay Shirokovskiy 已提交
1349
    char *passwd = NULL;
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

    pret = PrlVmCfg_GetVNCMode(sdkdom, &vncMode);
    prlsdkCheckRetGoto(pret, error);

    if (vncMode == PRD_DISABLED)
        return 0;

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

N
Nikolay Shirokovskiy 已提交
1360 1361 1362 1363 1364 1365 1366 1367
    if (!(passwd = prlsdkGetStringParamVar(PrlVmCfg_GetVNCPassword, sdkdom)))
        goto error;

    if (*passwd != '\0') {
        gr->data.vnc.auth.passwd = passwd;
        passwd = NULL;
    }

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    pret = PrlVmCfg_GetVNCPort(sdkdom, &port);
    prlsdkCheckRetGoto(pret, error);

    gr->data.vnc.autoport = (vncMode == PRD_AUTO);
    gr->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
    gr->data.vnc.port = port;

    if (VIR_ALLOC(gr->listens) < 0)
        goto error;

    gr->nListens = 1;

1380 1381
    if (!(gr->listens[0].address = prlsdkGetStringParamVar(PrlVmCfg_GetVNCHostName,
                                                           sdkdom)))
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
        goto error;

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

    if (VIR_APPEND_ELEMENT(def->graphics, def->ngraphics, gr) < 0)
        goto error;

    return 0;

 error:
    virDomainGraphicsDefFree(gr);
N
Nikolay Shirokovskiy 已提交
1393
    VIR_FREE(passwd);
1394 1395 1396
    return -1;
}

M
Maxim Nestratov 已提交
1397
static void
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
prlsdkConvertDomainState(VIRTUAL_MACHINE_STATE domainState,
                         PRL_UINT32 envId,
                         virDomainObjPtr dom)
{
    switch (domainState) {
    case VMS_STOPPED:
    case VMS_MOUNTED:
        virDomainObjSetState(dom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
        dom->def->id = -1;
        break;
    case VMS_STARTING:
    case VMS_COMPACTING:
    case VMS_RESETTING:
    case VMS_PAUSING:
    case VMS_RECONNECTING:
    case VMS_RUNNING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
        dom->def->id = envId;
        break;
    case VMS_PAUSED:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_USER);
        dom->def->id = envId;
        break;
    case VMS_SUSPENDED:
    case VMS_DELETING_STATE:
    case VMS_SUSPENDING_SYNC:
        virDomainObjSetState(dom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SAVED);
        dom->def->id = -1;
        break;
    case VMS_STOPPING:
        virDomainObjSetState(dom, VIR_DOMAIN_SHUTDOWN,
                             VIR_DOMAIN_SHUTDOWN_USER);
        dom->def->id = envId;
        break;
    case VMS_SNAPSHOTING:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_SNAPSHOT);
        dom->def->id = envId;
        break;
    case VMS_MIGRATING:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_MIGRATION);
        dom->def->id = envId;
        break;
    case VMS_SUSPENDING:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_SAVE);
        dom->def->id = envId;
        break;
    case VMS_RESTORING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_RESTORED);
        dom->def->id = envId;
        break;
    case VMS_CONTINUING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_UNPAUSED);
        dom->def->id = envId;
        break;
    case VMS_RESUMING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_RESTORED);
        dom->def->id = envId;
        break;
    case VMS_UNKNOWN:
M
Maxim Nestratov 已提交
1467
    default:
1468 1469
        virDomainObjSetState(dom, VIR_DOMAIN_NOSTATE,
                             VIR_DOMAIN_NOSTATE_UNKNOWN);
M
Maxim Nestratov 已提交
1470
        dom->def->id = -1;
1471 1472 1473 1474 1475 1476
        break;
    }
}

static int
prlsdkConvertCpuInfo(PRL_HANDLE sdkdom,
1477 1478
                     virDomainDefPtr def,
                     virDomainXMLOptionPtr xmlopt)
1479 1480 1481 1482 1483 1484 1485
{
    char *buf;
    int hostcpus;
    PRL_UINT32 cpuCount;
    PRL_RESULT pret;
    int ret = -1;

1486
    if ((hostcpus = virHostCPUGetCount()) < 0)
1487 1488 1489 1490 1491 1492 1493 1494 1495
        goto cleanup;

    /* get number of CPUs */
    pret = PrlVmCfg_GetCpuCount(sdkdom, &cpuCount);
    prlsdkCheckRetGoto(pret, cleanup);

    if (cpuCount > hostcpus)
        cpuCount = hostcpus;

1496
    if (virDomainDefSetVcpusMax(def, cpuCount, xmlopt) < 0)
1497 1498
        goto cleanup;

1499 1500
    if (virDomainDefSetVcpus(def, cpuCount) < 0)
        goto cleanup;
1501

1502
    if (!(buf = prlsdkGetStringParamVar(PrlVmCfg_GetCpuMask, sdkdom)))
1503 1504 1505
        goto cleanup;

    if (strlen(buf) == 0) {
M
Maxim Nestratov 已提交
1506
        if (!(def->cpumask = virBitmapNew(hostcpus)))
1507
            goto cleanup;
M
Maxim Nestratov 已提交
1508
        virBitmapSetAll(def->cpumask);
1509
    } else {
1510
        if (virBitmapParse(buf, &def->cpumask, hostcpus) < 0)
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
            goto cleanup;
    }

    ret = 0;
 cleanup:
    VIR_FREE(buf);
    return ret;
}

static int
prlsdkConvertDomainType(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    PRL_VM_TYPE domainType;
    PRL_RESULT pret;

    pret = PrlVmCfg_GetVmType(sdkdom, &domainType);
    prlsdkCheckRetGoto(pret, error);

    switch (domainType) {
    case PVT_VM:
1531
        def->os.type = VIR_DOMAIN_OSTYPE_HVM;
1532 1533
        break;
    case PVT_CT:
1534
        def->os.type = VIR_DOMAIN_OSTYPE_EXE;
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
        if (VIR_STRDUP(def->os.init, "/sbin/init") < 0)
            return -1;
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown domain type: %X"), domainType);
        return -1;
    }

    return 0;

 error:
    return -1;
}

static int
prlsdkConvertCpuMode(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    PRL_RESULT pret;
    PRL_CPU_MODE cpuMode;

    pret = PrlVmCfg_GetCpuMode(sdkdom, &cpuMode);
    prlsdkCheckRetGoto(pret, error);

    switch (cpuMode) {
    case PCM_CPU_MODE_32:
        def->os.arch = VIR_ARCH_I686;
        break;
    case PCM_CPU_MODE_64:
        def->os.arch = VIR_ARCH_X86_64;
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU mode: %X"), cpuMode);
        return -1;
    }

    return 0;
 error:
    return -1;
}

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
static PRL_HANDLE
prlsdkGetDevByDevIndex(PRL_HANDLE sdkdom, PRL_DEVICE_TYPE type, PRL_UINT32 devIndex)
{
    PRL_RESULT pret;
    PRL_UINT32 index, num;
    PRL_HANDLE dev = PRL_INVALID_HANDLE;
    size_t i;

    pret = PrlVmCfg_GetDevsCountByType(sdkdom, type, &num);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < num; ++i) {
        pret = PrlVmCfg_GetDevByType(sdkdom, type, i, &dev);
        prlsdkCheckRetGoto(pret, error);

        pret = PrlVmDev_GetIndex(dev, &index);
        prlsdkCheckRetGoto(pret, error);

        if (index == devIndex)
            break;

        PrlHandle_Free(dev);
        dev = PRL_INVALID_HANDLE;
    }

    return dev;

 error:
    PrlHandle_Free(dev);
    return PRL_INVALID_HANDLE;
}

static virDomainDiskDefPtr
virFindDiskBootIndex(virDomainDefPtr def, virDomainDiskDevice type, int index)
{
    size_t i;
    int c = 0;

    for (i = 0; i < def->ndisks; ++i) {
        if (def->disks[i]->device != type)
            continue;
        if (c == index)
            return def->disks[i];
        ++c;
    }

    return NULL;
}

1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
static bool
prlsdkInBootList(PRL_HANDLE sdkdom,
                 PRL_HANDLE sdktargetdev)
{
    bool ret = false;
    PRL_RESULT pret;
    PRL_UINT32 bootNum;
    PRL_HANDLE bootDev = PRL_INVALID_HANDLE;
    PRL_BOOL inUse;
    PRL_DEVICE_TYPE sdkType, targetType;
    PRL_UINT32 sdkIndex, targetIndex;
    size_t i;

    pret = PrlVmDev_GetType(sdktargetdev, &targetType);
    prlsdkCheckRetExit(pret, -1);

    pret = PrlVmDev_GetIndex(sdktargetdev, &targetIndex);
    prlsdkCheckRetExit(pret, -1);

    pret = PrlVmCfg_GetBootDevCount(sdkdom, &bootNum);
    prlsdkCheckRetExit(pret, -1);

    for (i = 0; i < bootNum; ++i) {
        pret = PrlVmCfg_GetBootDev(sdkdom, i, &bootDev);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlBootDev_IsInUse(bootDev, &inUse);
        prlsdkCheckRetGoto(pret, cleanup);

        if (!inUse) {
            PrlHandle_Free(bootDev);
            bootDev = PRL_INVALID_HANDLE;
            continue;
        }

        pret = PrlBootDev_GetType(bootDev, &sdkType);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlBootDev_GetIndex(bootDev, &sdkIndex);
        prlsdkCheckRetGoto(pret, cleanup);

        PrlHandle_Free(bootDev);
        bootDev = PRL_INVALID_HANDLE;

        if (sdkIndex == targetIndex && sdkType == targetType) {
            ret = true;
            break;
        }
    }

 cleanup:
    PrlHandle_Free(bootDev);
    return ret;
}
1680 1681 1682 1683 1684 1685 1686 1687
static int
prlsdkBootOrderCheck(PRL_HANDLE sdkdom, PRL_DEVICE_TYPE sdkType, int sdkIndex,
                     virDomainDefPtr def, int bootIndex)
{
    char *sdkName = NULL;
    PRL_HANDLE dev = PRL_INVALID_HANDLE;
    virDomainDiskDefPtr disk;
    virDomainDiskDevice device;
1688 1689
    int bus;
    char *dst = NULL;
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
    int ret = -1;

    dev = prlsdkGetDevByDevIndex(sdkdom, sdkType, sdkIndex);
    if (dev == PRL_INVALID_HANDLE) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Can't find boot device of type: %d, device index: %d"),
                       sdkType, sdkIndex);
        return -1;
    }

    switch (sdkType) {
    case PDE_OPTICAL_DISK:
    case PDE_HARD_DISK:
        switch (sdkType) {
        case PDE_OPTICAL_DISK:
            device = VIR_DOMAIN_DISK_DEVICE_CDROM;
            break;
        case PDE_HARD_DISK:
            device = VIR_DOMAIN_DISK_DEVICE_DISK;
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported disk type %d"), sdkType);
            goto cleanup;
        }

        if (!(disk = virFindDiskBootIndex(def, device, bootIndex))) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
Y
Yuri Chornoivan 已提交
1718
                           _("Can't find boot device of type: %s, index: %d"),
1719 1720 1721 1722
                           virDomainDiskDeviceTypeToString(device), bootIndex);
            goto cleanup;
        }

1723
        if (prlsdkGetDiskId(dev, &bus, &dst) < 0)
1724 1725 1726 1727
            goto cleanup;

        if (!(bus == disk->bus && STREQ(disk->dst, dst)))
            VIR_WARN("Unrepresentable boot order configuration");
1728 1729 1730

        break;
    case PDE_GENERIC_NETWORK_ADAPTER:
1731 1732
        if (!(sdkName = prlsdkGetStringParamVar(PrlVmDevNet_GetHostInterfaceName,
                                                dev)))
1733 1734 1735 1736
            goto cleanup;

        if (bootIndex >= def->nnets) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
Y
Yuri Chornoivan 已提交
1737
                           _("Can't find network boot device for index: %d"),
1738 1739 1740 1741
                           bootIndex);
            goto cleanup;
        }

1742 1743
        if (STRNEQ(sdkName, def->nets[bootIndex]->ifname))
            VIR_WARN("Unrepresentable boot order configuration");
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757

        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unexpected device type %d"), sdkType);
        goto cleanup;
    }

    ret = 0;

 cleanup:

    VIR_FREE(sdkName);
    PrlHandle_Free(dev);
1758
    VIR_FREE(dst);
1759 1760 1761 1762
    return ret;
}

static int
1763
prlsdkConvertBootOrderVm(PRL_HANDLE sdkdom, virDomainDefPtr def)
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
{
    int ret = -1;
    PRL_RESULT pret;
    PRL_UINT32 bootNum;
    PRL_HANDLE bootDev = PRL_INVALID_HANDLE;
    PRL_BOOL inUse;
    PRL_DEVICE_TYPE sdkType;
    virDomainBootOrder type;
    PRL_UINT32 prevBootIndex = 0, bootIndex, sdkIndex;
    int bootUsage[VIR_DOMAIN_BOOT_LAST] = { 0 };
    size_t i;

    pret = PrlVmCfg_GetBootDevCount(sdkdom, &bootNum);
    prlsdkCheckRetExit(pret, -1);

    def->os.nBootDevs = 0;

    if (bootNum > VIR_DOMAIN_MAX_BOOT_DEVS) {
        bootNum = VIR_DOMAIN_MAX_BOOT_DEVS;
        VIR_WARN("Too many boot devices");
    }

    for (i = 0; i < bootNum; ++i) {
        pret = PrlVmCfg_GetBootDev(sdkdom, i, &bootDev);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlBootDev_IsInUse(bootDev, &inUse);
        prlsdkCheckRetGoto(pret, cleanup);

        if (!inUse) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Boot ordering with disabled items is not supported"));
            goto cleanup;
        }

        pret = PrlBootDev_GetSequenceIndex(bootDev, &bootIndex);
        prlsdkCheckRetGoto(pret, cleanup);

        /* bootIndex is started from 1 */
        if (bootIndex <= prevBootIndex) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Unsupported boot order configuration"));
            goto cleanup;
        }
        prevBootIndex = bootIndex;

        pret = PrlBootDev_GetType(bootDev, &sdkType);
        prlsdkCheckRetGoto(pret, cleanup);

        if (sdkType == PDE_FLOPPY_DISK) {
            VIR_WARN("Skipping floppy from boot order.");
            continue;
        }

        switch (sdkType) {
        case PDE_OPTICAL_DISK:
            type = VIR_DOMAIN_BOOT_CDROM;
            break;
        case PDE_HARD_DISK:
            type = VIR_DOMAIN_BOOT_DISK;
            break;
        case PDE_GENERIC_NETWORK_ADAPTER:
            type = VIR_DOMAIN_BOOT_NET;
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unexpected boot device type %i"), sdkType);
            goto cleanup;
        }

        pret = PrlBootDev_GetIndex(bootDev, &sdkIndex);
        prlsdkCheckRetGoto(pret, cleanup);

        if (prlsdkBootOrderCheck(sdkdom, sdkType, sdkIndex, def, bootUsage[type]) < 0)
            goto cleanup;

        bootUsage[type]++;
        def->os.bootDevs[def->os.nBootDevs++] = type;

        PrlHandle_Free(bootDev);
        bootDev = PRL_INVALID_HANDLE;
    }

    ret = 0;

 cleanup:
    PrlHandle_Free(bootDev);
    return ret;
}

1854 1855 1856
/* if dom is NULL adds new domain into domain list
 * if dom not NULL updates given locked dom object.
 *
1857
 * Returned object is locked and referenced.
1858 1859 1860 1861 1862 1863
 */

static virDomainObjPtr
prlsdkLoadDomain(vzDriverPtr driver,
                 PRL_HANDLE sdkdom,
                 virDomainObjPtr dom)
1864
{
1865
    virDomainDefPtr def = NULL;
1866
    vzDomObjPtr pdom = NULL;
1867 1868 1869 1870 1871 1872
    VIRTUAL_MACHINE_STATE domainState;

    PRL_RESULT pret;
    PRL_UINT32 ram;
    PRL_UINT32 envId;
    PRL_VM_AUTOSTART_OPTION autostart;
1873
    PRL_HANDLE job;
1874
    char uuidstr[VIR_UUID_STRING_BRACED_BUFLEN];
1875

1876
    if (!(def = virDomainDefNew()))
1877 1878
        goto error;

1879 1880 1881 1882 1883 1884
    if (!(def->name = prlsdkGetStringParamVar(PrlVmCfg_GetName, sdkdom)))
        goto error;

    pret = prlsdkGetStringParamBuf(PrlVmCfg_GetUuid,
                                   sdkdom, uuidstr, sizeof(uuidstr));
    prlsdkCheckRetGoto(pret, error);
1885

1886 1887 1888
    if (prlsdkUUIDParse(uuidstr, def->uuid) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain UUID is malformed or empty"));
1889
        goto error;
1890 1891 1892
    }

    def->virtType = VIR_DOMAIN_VIRT_VZ;
1893 1894 1895 1896 1897 1898 1899 1900

    def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
    def->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
    def->onCrash = VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY;

    /* get RAM parameters */
    pret = PrlVmCfg_GetRamSize(sdkdom, &ram);
    prlsdkCheckRetGoto(pret, error);
1901
    virDomainDefSetMemoryTotal(def, ram << 10); /* RAM size obtained in Mbytes,
1902 1903
                                                     convert to Kbytes */
    def->mem.cur_balloon = ram << 10;
1904

1905
    if (prlsdkConvertCpuInfo(sdkdom, def, driver->xmlopt) < 0)
1906 1907 1908 1909 1910 1911 1912 1913
        goto error;

    if (prlsdkConvertCpuMode(sdkdom, def) < 0)
        goto error;

    if (prlsdkConvertDomainType(sdkdom, def) < 0)
        goto error;

1914 1915 1916 1917
    if (prlsdkAddVNCInfo(sdkdom, def) < 0)
        goto error;

    /* depends on prlsdkAddVNCInfo */
1918
    if (prlsdkAddDomainHardware(driver, sdkdom, def) < 0)
1919 1920
        goto error;

1921
    /* depends on prlsdkAddDomainHardware */
1922 1923
    if (!IS_CT(def) && prlsdkConvertBootOrderVm(sdkdom, def) < 0)
        goto error;
1924

1925 1926 1927
    pret = PrlVmCfg_GetEnvId(sdkdom, &envId);
    prlsdkCheckRetGoto(pret, error);

1928 1929
    pret = PrlVmCfg_GetAutoStart(sdkdom, &autostart);
    prlsdkCheckRetGoto(pret, error);
1930 1931 1932 1933 1934 1935
    if (autostart != PAO_VM_START_ON_LOAD &&
        autostart != PAO_VM_START_MANUAL) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown autostart mode: %X"), autostart);
        goto error;
    }
1936

1937
    if (prlsdkGetDomainState(dom, sdkdom, &domainState) < 0)
1938 1939
        goto error;

1940
    if (!IS_CT(def) && virDomainDefAddImplicitDevices(def) < 0)
1941 1942
        goto error;

1943
    if (def->ngraphics > 0) {
M
Michal Privoznik 已提交
1944 1945
        int bus = IS_CT(def) ? VIR_DOMAIN_INPUT_BUS_PARALLELS :
                               VIR_DOMAIN_INPUT_BUS_PS2;
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957

        if (virDomainDefMaybeAddInput(def,
                                      VIR_DOMAIN_INPUT_TYPE_MOUSE,
                                      bus) < 0)
            goto error;

        if (virDomainDefMaybeAddInput(def,
                                      VIR_DOMAIN_INPUT_TYPE_KBD,
                                      bus) < 0)
            goto error;
    }

1958 1959 1960
    if (!dom) {
        virDomainObjPtr olddom = NULL;

1961 1962 1963 1964
        job = PrlVm_SubscribeToPerfStats(sdkdom, NULL);
        if (PRL_FAILED(waitJob(job)))
            goto error;

1965
        virObjectLock(driver);
1966
        if (!(olddom = virDomainObjListFindByUUIDRef(driver->domains, def->uuid)))
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
            dom = virDomainObjListAdd(driver->domains, def, driver->xmlopt, 0, NULL);
        virObjectUnlock(driver);

        if (olddom) {
            virDomainDefFree(def);
            return olddom;
        } else if (!dom) {
            goto error;
        }

1977
        virObjectRef(dom);
1978
        pdom = dom->privateData;
1979
        pdom->sdkdom = sdkdom;
1980 1981 1982 1983 1984 1985 1986 1987
        PrlHandle_AddRef(sdkdom);
        dom->persistent = 1;
    } else {
        /* assign new virDomainDef without any checks
         * we can't use virDomainObjAssignDef, because it checks
         * for state and domain name */
        virDomainDefFree(dom->def);
        dom->def = def;
1988 1989
    }

1990
    pdom = dom->privateData;
1991 1992
    pdom->id = envId;

M
Maxim Nestratov 已提交
1993 1994
    prlsdkConvertDomainState(domainState, envId, dom);

1995 1996 1997 1998
    if (autostart == PAO_VM_START_ON_LOAD)
        dom->autostart = 1;
    else
        dom->autostart = 0;
1999

2000 2001
    return dom;

2002 2003
 error:
    virDomainDefFree(def);
2004
    return NULL;
2005 2006 2007
}

int
2008
prlsdkLoadDomains(vzDriverPtr driver)
2009 2010 2011
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result;
2012
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;
2013 2014 2015 2016 2017
    PRL_UINT32 paramsCount;
    PRL_RESULT pret;
    size_t i = 0;
    virDomainObjPtr dom;

2018
    job = PrlSrv_GetVmListEx(driver->server, PVTF_VM | PVTF_CT);
2019

2020
    if (PRL_FAILED(getJobResult(job, &result)))
2021 2022 2023 2024 2025 2026 2027
        return -1;

    pret = PrlResult_GetParamsCount(result, &paramsCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < paramsCount; i++) {
        pret = PrlResult_GetParamByIndex(result, i, &sdkdom);
2028 2029
        prlsdkCheckRetGoto(pret, error);

2030 2031
        dom = prlsdkLoadDomain(driver, sdkdom, NULL);
        virDomainObjEndAPI(&dom);
2032 2033

        PrlHandle_Free(sdkdom);
2034
        sdkdom = PRL_INVALID_HANDLE;
2035 2036 2037 2038 2039 2040
    }

    PrlHandle_Free(result);
    return 0;

 error:
2041
    PrlHandle_Free(sdkdom);
2042 2043 2044 2045
    PrlHandle_Free(result);
    return -1;
}

2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
virDomainObjPtr
prlsdkAddDomainByUUID(vzDriverPtr driver, const unsigned char *uuid)
{
    PRL_HANDLE sdkdom;
    virDomainObjPtr dom;

    sdkdom = prlsdkSdkDomainLookupByUUID(driver, uuid);
    if (sdkdom == PRL_INVALID_HANDLE)
        return NULL;

    dom = prlsdkLoadDomain(driver, sdkdom, NULL);

    PrlHandle_Free(sdkdom);
    return dom;
}

virDomainObjPtr
prlsdkAddDomainByName(vzDriverPtr driver, const char *name)
{
    PRL_HANDLE sdkdom;
    virDomainObjPtr dom;

    sdkdom = prlsdkSdkDomainLookupByName(driver, name);
    if (sdkdom == PRL_INVALID_HANDLE)
        return NULL;

    dom = prlsdkLoadDomain(driver, sdkdom, NULL);

    PrlHandle_Free(sdkdom);
    return dom;
}

2078
int
2079
prlsdkUpdateDomain(vzDriverPtr driver, virDomainObjPtr dom)
2080 2081
{
    PRL_HANDLE job;
2082
    vzDomObjPtr pdom = dom->privateData;
2083 2084

    job = PrlVm_RefreshConfig(pdom->sdkdom);
2085
    if (waitDomainJob(job, dom))
2086 2087
        return -1;

2088
    return prlsdkLoadDomain(driver, pdom->sdkdom, dom) ? 0 : -1;
2089 2090
}

2091 2092 2093 2094 2095
static void
prlsdkSendEvent(vzDriverPtr driver,
                virDomainObjPtr dom,
                virDomainEventType lvEventType,
                int lvEventTypeDetails)
2096
{
2097
    virObjectEventPtr event;
2098 2099 2100 2101

    event = virDomainEventLifecycleNewFromObj(dom,
                                              lvEventType,
                                              lvEventTypeDetails);
2102 2103
    if (event)
        virObjectEventStateQueue(driver->domainEventState, event);
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
}

static void
prlsdkNewStateToEvent(VIRTUAL_MACHINE_STATE domainState,
                      virDomainEventType *lvEventType,
                      int *lvEventTypeDetails)
{
    /* We skip all intermediate states here, because
     * libvirt doesn't have correspoding event types for
     * them */
    switch (domainState) {
    case VMS_STOPPED:
    case VMS_MOUNTED:
        *lvEventType = VIR_DOMAIN_EVENT_STOPPED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN;
        break;
    case VMS_RUNNING:
        *lvEventType = VIR_DOMAIN_EVENT_STARTED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_STARTED_BOOTED;
        break;
    case VMS_PAUSED:
        *lvEventType = VIR_DOMAIN_EVENT_SUSPENDED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_SUSPENDED_PAUSED;
        break;
    case VMS_SUSPENDED:
        *lvEventType = VIR_DOMAIN_EVENT_STOPPED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_STOPPED_SAVED;
        break;
    default:
        VIR_DEBUG("Skip sending event about changing state to %X",
                  domainState);
        break;
    }
}

2139
static void
2140
prlsdkHandleVmStateEvent(vzDriverPtr driver,
2141 2142 2143 2144 2145 2146 2147
                         PRL_HANDLE prlEvent,
                         unsigned char *uuid)
{
    PRL_RESULT pret = PRL_ERR_FAILURE;
    PRL_HANDLE eventParam = PRL_INVALID_HANDLE;
    PRL_INT32 domainState;
    virDomainObjPtr dom = NULL;
2148
    vzDomObjPtr pdom;
2149 2150 2151
    virDomainEventType lvEventType = 0;
    int lvEventTypeDetails = 0;

2152
    dom = virDomainObjListFindByUUID(driver->domains, uuid);
2153 2154 2155
    if (dom == NULL)
        return;

2156 2157 2158 2159 2160 2161 2162
    pret = PrlEvent_GetParamByName(prlEvent, "vminfo_vm_state", &eventParam);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlEvtPrm_ToInt32(eventParam, &domainState);
    prlsdkCheckRetGoto(pret, cleanup);

    pdom = dom->privateData;
M
Maxim Nestratov 已提交
2163 2164

    prlsdkConvertDomainState(domainState, pdom->id, dom);
2165 2166 2167 2168 2169

    prlsdkNewStateToEvent(domainState,
                          &lvEventType,
                          &lvEventTypeDetails);

2170
    prlsdkSendEvent(driver, dom, lvEventType, lvEventTypeDetails);
2171 2172

 cleanup:
2173
    PrlHandle_Free(eventParam);
2174 2175
    virObjectUnlock(dom);
    return;
2176 2177
}

2178
static void
2179
prlsdkHandleVmConfigEvent(vzDriverPtr driver,
2180 2181 2182
                          unsigned char *uuid)
{
    virDomainObjPtr dom = NULL;
2183
    bool job = false;
2184

2185
    dom = virDomainObjListFindByUUIDRef(driver->domains, uuid);
2186 2187
    if (dom == NULL)
        return;
2188

2189 2190 2191 2192
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2193 2194 2195
    if (dom->removing)
        goto cleanup;

2196
    if (prlsdkUpdateDomain(driver, dom) < 0)
2197 2198
        goto cleanup;

2199
    prlsdkSendEvent(driver, dom, VIR_DOMAIN_EVENT_DEFINED,
M
Michal Privoznik 已提交
2200
                    VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2201 2202

 cleanup:
2203 2204 2205
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2206
    return;
2207 2208
}

2209
static void
2210
prlsdkHandleVmAddedEvent(vzDriverPtr driver,
M
Michal Privoznik 已提交
2211
                         unsigned char *uuid)
2212 2213 2214
{
    virDomainObjPtr dom = NULL;

2215
    if (!(dom = virDomainObjListFindByUUIDRef(driver->domains, uuid)) &&
2216 2217
        !(dom = prlsdkAddDomainByUUID(driver, uuid)))
        goto cleanup;
2218

2219
    prlsdkSendEvent(driver, dom, VIR_DOMAIN_EVENT_DEFINED,
M
Michal Privoznik 已提交
2220
                    VIR_DOMAIN_EVENT_DEFINED_ADDED);
2221

2222
 cleanup:
2223
    virDomainObjEndAPI(&dom);
2224
    return;
2225 2226
}

2227
static void
2228
prlsdkHandleVmRemovedEvent(vzDriverPtr driver,
2229 2230 2231 2232
                           unsigned char *uuid)
{
    virDomainObjPtr dom = NULL;

2233
    dom = virDomainObjListFindByUUID(driver->domains, uuid);
2234 2235 2236 2237
    /* domain was removed from the list from the libvirt
     * API function in current connection */
    if (dom == NULL)
        return;
2238

2239
    prlsdkSendEvent(driver, dom, VIR_DOMAIN_EVENT_UNDEFINED,
M
Michal Privoznik 已提交
2240
                    VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2241

2242
    virDomainObjListRemove(driver->domains, dom);
2243
    return;
2244 2245
}

2246
static void
2247
prlsdkHandlePerfEvent(vzDriverPtr driver,
M
Michal Privoznik 已提交
2248 2249
                      PRL_HANDLE event,
                      unsigned char *uuid)
2250 2251
{
    virDomainObjPtr dom = NULL;
2252
    vzDomObjPtr privdom = NULL;
2253

2254 2255
    if (!(dom = virDomainObjListFindByUUID(driver->domains, uuid))) {
        PrlHandle_Free(event);
2256
        return;
2257
    }
2258

2259 2260 2261
    privdom = dom->privateData;
    PrlHandle_Free(privdom->stats);
    privdom->stats = event;
2262

2263
    virObjectUnlock(dom);
2264 2265
}

2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
static void
prlsdkHandleMigrationProgress(vzDriverPtr driver,
                              PRL_HANDLE event,
                              unsigned char *uuid)
{
    virDomainObjPtr dom = NULL;
    vzDomObjPtr privdom = NULL;
    PRL_UINT32 progress;
    PRL_HANDLE param = PRL_INVALID_HANDLE;
    PRL_RESULT pret;

    if (!(dom = virDomainObjListFindByUUID(driver->domains, uuid)))
        return;

    pret = PrlEvent_GetParam(event, 0, &param);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlEvtPrm_ToUint32(param, &progress);
    prlsdkCheckRetGoto(pret, cleanup);

    privdom = dom->privateData;
    privdom->job.progress = progress;

 cleanup:
    PrlHandle_Free(param);
    virObjectUnlock(dom);
}

2294 2295
static PRL_RESULT
prlsdkEventsHandler(PRL_HANDLE prlEvent, PRL_VOID_PTR opaque)
2296
{
2297
    vzDriverPtr driver = opaque;
2298
    PRL_RESULT pret = PRL_ERR_FAILURE;
2299
    PRL_HANDLE_TYPE handleType;
2300
    char uuidstr[VIR_UUID_STRING_BRACED_BUFLEN];
2301 2302 2303
    unsigned char uuid[VIR_UUID_BUFLEN];
    PRL_EVENT_TYPE prlEventType;

2304
    pret = PrlHandle_GetType(prlEvent, &handleType);
2305
    prlsdkCheckRetGoto(pret, cleanup);
2306

2307 2308 2309 2310
    /* Currently, there is no need to handle anything but events */
    if (handleType != PHT_EVENT)
        goto cleanup;

2311
    if (driver == NULL)
2312 2313
        goto cleanup;

2314 2315
    pret = prlsdkGetStringParamBuf(PrlEvent_GetIssuerId,
                                   prlEvent, uuidstr, sizeof(uuidstr));
2316
    prlsdkCheckRetGoto(pret, cleanup);
2317

2318 2319 2320
    pret = PrlEvent_GetType(prlEvent, &prlEventType);
    prlsdkCheckRetGoto(pret, cleanup);

2321 2322
    if (prlsdkUUIDParse(uuidstr, uuid) < 0) {
        VIR_DEBUG("Skipping event type %d", prlEventType);
2323
        goto cleanup;
2324
    }
2325 2326

    switch (prlEventType) {
M
Michal Privoznik 已提交
2327
    case PET_DSP_EVT_VM_STATE_CHANGED:
2328
        prlsdkHandleVmStateEvent(driver, prlEvent, uuid);
M
Michal Privoznik 已提交
2329 2330
        break;
    case PET_DSP_EVT_VM_CONFIG_CHANGED:
2331
        prlsdkHandleVmConfigEvent(driver, uuid);
M
Michal Privoznik 已提交
2332 2333 2334
        break;
    case PET_DSP_EVT_VM_CREATED:
    case PET_DSP_EVT_VM_ADDED:
2335
        prlsdkHandleVmAddedEvent(driver, uuid);
M
Michal Privoznik 已提交
2336 2337 2338
        break;
    case PET_DSP_EVT_VM_DELETED:
    case PET_DSP_EVT_VM_UNREGISTERED:
2339
        prlsdkHandleVmRemovedEvent(driver, uuid);
M
Michal Privoznik 已提交
2340 2341
        break;
    case PET_DSP_EVT_VM_PERFSTATS:
2342
        prlsdkHandlePerfEvent(driver, prlEvent, uuid);
M
Michal Privoznik 已提交
2343 2344 2345
        /* above function takes own of event */
        prlEvent = PRL_INVALID_HANDLE;
        break;
2346
    case PET_DSP_EVT_DISP_CONNECTION_CLOSED:
2347
        vzDestroyDriverConnection();
2348
        break;
2349 2350 2351
    case PET_DSP_EVT_VM_MIGRATE_PROGRESS_CHANGED:
        prlsdkHandleMigrationProgress(driver, prlEvent, uuid);
        break;
M
Michal Privoznik 已提交
2352
    default:
2353
        VIR_DEBUG("Skipping event of type %d", prlEventType);
2354 2355 2356 2357
    }

 cleanup:
    PrlHandle_Free(prlEvent);
2358
    return PRL_ERR_SUCCESS;
2359 2360
}

2361
int prlsdkStart(virDomainObjPtr dom)
2362 2363
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
2364 2365 2366 2367 2368 2369 2370 2371
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;

    job = PrlVm_StartEx(privdom->sdkdom, PSM_VM_START, 0);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
        return -1;
    }
2372

2373
    return 0;
2374 2375
}

2376
int prlsdkKill(virDomainObjPtr dom)
2377 2378
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
2379 2380
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;
2381

2382 2383 2384 2385 2386
    job = PrlVm_StopEx(privdom->sdkdom, PSM_KILL, 0);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
        return -1;
    }
2387

2388
    return 0;
2389 2390
}

2391
int prlsdkStop(virDomainObjPtr dom)
2392 2393
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
2394 2395
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;
2396

2397 2398 2399 2400 2401
    job = PrlVm_StopEx(privdom->sdkdom, PSM_SHUTDOWN, 0);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
        return -1;
    }
2402

2403
    return 0;
2404 2405
}

2406
int prlsdkPause(virDomainObjPtr dom)
2407 2408
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
2409 2410 2411 2412 2413 2414 2415 2416
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;

    job = PrlVm_Pause(privdom->sdkdom, false);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
        return -1;
    }
2417

2418
    return 0;
2419 2420
}

2421
int prlsdkResume(virDomainObjPtr dom)
2422 2423
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
2424 2425 2426 2427 2428 2429 2430 2431
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;

    job = PrlVm_Resume(privdom->sdkdom);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
        return -1;
    }
2432

2433
    return 0;
2434 2435
}

2436
int prlsdkSuspend(virDomainObjPtr dom)
2437
{
2438 2439
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    vzDomObjPtr privdom = dom->privateData;
2440
    PRL_RESULT pret;
2441

2442 2443 2444
    job = PrlVm_Suspend(privdom->sdkdom);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
2445
        return -1;
2446
    }
2447

2448
    return 0;
2449
}
2450

2451
int prlsdkRestart(virDomainObjPtr dom)
2452
{
2453 2454 2455
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;
2456

2457 2458 2459
    job = PrlVm_Restart(privdom->sdkdom);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
2460
        return -1;
2461
    }
2462

2463 2464
    return 0;
}
2465

2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
int prlsdkReset(virDomainObjPtr dom)
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    vzDomObjPtr privdom = dom->privateData;
    PRL_RESULT pret;

    job = PrlVm_Reset(privdom->sdkdom);
    if (PRL_FAILED(pret = waitDomainJob(job, dom))) {
        prlsdkConvertError(pret);
        return -1;
    }

    return 0;
}

2481 2482 2483 2484
static void
prlsdkConvertError(PRL_RESULT pret)
{
    virErrorNumber virerr;
2485

2486 2487 2488 2489 2490 2491 2492 2493 2494
    switch (pret) {
    case PRL_ERR_DISP_VM_IS_NOT_STARTED:
    case PRL_ERR_DISP_VM_IS_NOT_STOPPED:
    case PRL_ERR_INVALID_ACTION_REQUESTED:
    case PRL_ERR_UNIMPLEMENTED:
        virerr = VIR_ERR_OPERATION_INVALID;
        break;
    default:
        virerr = VIR_ERR_OPERATION_FAILED;
2495 2496
    }

2497 2498
    virResetLastError();
    virReportError(virerr, "%s", _("Can't change domain state."));
2499
}
2500 2501 2502 2503 2504 2505 2506

static int
prlsdkCheckUnsupportedParams(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    size_t i;
    PRL_VM_TYPE vmType;
    PRL_RESULT pret;
2507
    virDomainNumatuneMemMode memMode;
2508 2509
    int bus = IS_CT(def) ? VIR_DOMAIN_INPUT_BUS_PARALLELS :
                           VIR_DOMAIN_INPUT_BUS_PS2;
2510 2511 2512

    if (def->title) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2513
                       _("titles are not supported by vz driver"));
2514 2515 2516 2517 2518 2519
        return -1;
    }

    if (def->blkio.ndevices > 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("blkio parameters are not supported "
2520
                         "by vz driver"));
2521 2522 2523
        return -1;
    }

2524
    if (virDomainDefGetMemoryTotal(def) != def->mem.cur_balloon) {
2525
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
2526 2527 2528
                       _("changing balloon parameters is not supported "
                         "by vz driver"));
        return -1;
2529 2530
    }

2531
    if (virDomainDefGetMemoryTotal(def) % (1 << 10) != 0) {
2532
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
2533
                       _("Memory size should be multiple of 1Mb."));
2534 2535 2536 2537
        return -1;
    }

    if (def->mem.nhugepages ||
2538 2539
        virMemoryLimitIsSet(def->mem.hard_limit) ||
        virMemoryLimitIsSet(def->mem.soft_limit) ||
2540
        def->mem.min_guarantee ||
2541
        virMemoryLimitIsSet(def->mem.swap_hard_limit)) {
2542 2543 2544

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Memory parameter is not supported "
2545
                         "by vz driver"));
2546 2547 2548
        return -1;
    }

2549
    if (virDomainDefHasVcpusOffline(def)) {
2550
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
2551
                       _("current vcpus must be equal to maxvcpus"));
2552 2553 2554 2555 2556 2557
        return -1;
    }

    if (def->placement_mode) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing cpu placement mode is not supported "
2558
                         "by vz driver"));
2559 2560 2561 2562 2563 2564
        return -1;
    }

    if (def->cputune.shares ||
        def->cputune.sharesSpecified ||
        def->cputune.period ||
2565
        def->cputune.quota) {
2566 2567

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2568
                       _("cputune is not supported by vz driver"));
2569 2570 2571
        return -1;
    }

2572
    for (i = 0; i < virDomainDefGetVcpusMax(def); i++) {
2573
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(def, i);
2574 2575 2576 2577 2578 2579

        if (vcpu->cpumask &&
            !virBitmapEqual(def->cpumask, vcpu->cpumask)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("vcpupin cpumask differs from default cpumask"));
            return -1;
2580 2581 2582
        }
    }

2583 2584 2585 2586 2587 2588

    /*
     * Though we don't support NUMA configuration at the moment
     * virDomainDefPtr always contain non zero NUMA configuration
     * So, just make sure this configuration does't differ from auto generated.
     */
2589 2590
    if ((virDomainNumatuneGetMode(def->numa, -1, &memMode) == 0 &&
         memMode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) ||
M
Michal Privoznik 已提交
2591
        virDomainNumatuneHasPerNodeBinding(def->numa)) {
2592
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
2593 2594
                       _("numa parameters are not supported "
                         "by vz driver"));
2595 2596 2597 2598 2599 2600 2601 2602 2603
        return -1;
    }

    if (def->onReboot != VIR_DOMAIN_LIFECYCLE_RESTART ||
        def->onPoweroff != VIR_DOMAIN_LIFECYCLE_DESTROY ||
        def->onCrash != VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("on_reboot, on_poweroff and on_crash parameters "
2604
                         "are not supported by vz driver"));
2605 2606 2607
        return -1;
    }

2608
    /* we fill only type and arch fields in vzLoadDomain for
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
     * hvm type and also init for containers, so we can check that all
     * other paramenters are null and boot devices config is default */

    if (def->os.machine != NULL || def->os.bootmenu != 0 ||
        def->os.kernel != NULL || def->os.initrd != NULL ||
        def->os.cmdline != NULL || def->os.root != NULL ||
        def->os.loader != NULL || def->os.bootloader != NULL ||
        def->os.bootloaderArgs != NULL || def->os.smbios_mode != 0 ||
        def->os.bios.useserial != 0) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing OS parameters is not supported "
2621
                         "by vz driver"));
2622 2623 2624 2625 2626 2627 2628 2629 2630
        return -1;
    }

    pret = PrlVmCfg_GetVmType(sdkdom, &vmType);
    if (PRL_FAILED(pret)) {
        logPrlError(pret);
        return -1;
    }

2631 2632
    if (!(vmType == PVT_VM && !IS_CT(def)) &&
        !(vmType == PVT_CT && IS_CT(def))) {
2633 2634 2635

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing OS type is not supported "
2636
                         "by vz driver"));
2637 2638 2639
        return -1;
    }

2640
    if (!IS_CT(def)) {
2641
        if (def->os.init != NULL || def->os.initargv != NULL) {
2642
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2643
                           _("unsupported OS parameters"));
2644 2645 2646 2647
            return -1;
        }
    } else {
        if (def->os.nBootDevs != 0 ||
2648
            STRNEQ_NULLABLE(def->os.init, "/sbin/init") ||
2649 2650 2651
            (def->os.initargv != NULL && def->os.initargv[0] != NULL)) {

            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2652
                           _("unsupported OS parameters"));
2653 2654 2655 2656 2657 2658 2659
            return -1;
        }
    }

    if (def->emulator) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing emulator is not supported "
2660
                         "by vz driver"));
2661 2662 2663 2664 2665 2666 2667
        return -1;
    }

    for (i = 0; i < VIR_DOMAIN_FEATURE_LAST; i++) {
        if (def->features[i]) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("changing features is not supported "
2668
                             "by vz driver"));
2669 2670 2671 2672 2673 2674 2675 2676 2677
            return -1;
        }
    }

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

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing clock parameters is not supported "
2678
                         "by vz driver"));
2679 2680 2681
        return -1;
    }

2682 2683 2684
    if (!IS_CT(def) && def->nfss != 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Filesystems in VMs are not supported "
2685
                         "by vz driver"));
2686 2687 2688 2689
        return -1;
    }

    if (def->nsounds != 0 || def->nhostdevs != 0 ||
2690 2691 2692 2693 2694 2695
        def->nredirdevs != 0 || def->nsmartcards != 0 ||
        def->nparallels || def->nchannels != 0 ||
        def->nleases != 0 || def->nhubs != 0) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing devices parameters is not supported "
2696
                         "by vz driver"));
2697 2698 2699
        return -1;
    }

2700 2701 2702 2703 2704 2705 2706 2707
    /* check we have only default input devices */
    if (def->ngraphics > 0) {
        if (def->ninputs != 2 ||
            def->inputs[0]->bus != bus ||
            def->inputs[1]->bus != bus ||
            !((def->inputs[0]->type == VIR_DOMAIN_INPUT_TYPE_MOUSE &&
               def->inputs[1]->type == VIR_DOMAIN_INPUT_TYPE_KBD) ||
              (def->inputs[0]->type == VIR_DOMAIN_INPUT_TYPE_KBD &&
2708
               def->inputs[1]->type == VIR_DOMAIN_INPUT_TYPE_MOUSE))) {
2709

2710 2711 2712 2713 2714
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported input device configuration"));
            return -1;
        }
    } else if (def->ninputs != 0) {
2715
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2716
                       _("input devices without vnc are not supported"));
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
        return -1;
    }

    return 0;
}

static int prlsdkClearDevices(PRL_HANDLE sdkdom)
{
    PRL_RESULT pret;
    PRL_UINT32 n, i;
    PRL_HANDLE devList;
    PRL_HANDLE dev;
    int ret = -1;

    pret = PrlVmCfg_GetAllDevices(sdkdom, &devList);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlHndlList_GetItemsCount(devList, &n);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < n; i++) {
        pret = PrlHndlList_GetItem(devList, i, &dev);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlVmDev_Remove(dev);
        PrlHandle_Free(dev);
    }

    ret = 0;
 cleanup:
    PrlHandle_Free(devList);
    return ret;
}

2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
static int
prlsdkRemoveBootDevices(PRL_HANDLE sdkdom)
{
    PRL_RESULT pret;
    PRL_UINT32 i, devCount;
    PRL_HANDLE dev = PRL_INVALID_HANDLE;
    PRL_DEVICE_TYPE devType;

    pret = PrlVmCfg_GetBootDevCount(sdkdom, &devCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < devCount; i++) {

        /* always get device by index 0, because device list resort after delete */
        pret = PrlVmCfg_GetBootDev(sdkdom, 0, &dev);
        prlsdkCheckRetGoto(pret, error);

        pret = PrlBootDev_GetType(dev, &devType);
        prlsdkCheckRetGoto(pret, error);

        pret = PrlBootDev_Remove(dev);
        prlsdkCheckRetGoto(pret, error);
    }

    return 0;

 error:
    return -1;
}

static int
prlsdkAddDeviceToBootList(PRL_HANDLE sdkdom,
                          PRL_UINT32 devIndex,
                          PRL_DEVICE_TYPE devType,
                          PRL_UINT32 bootSequence)
{
    PRL_RESULT pret;
    PRL_HANDLE bootDev = PRL_INVALID_HANDLE;

    pret = PrlVmCfg_CreateBootDev(sdkdom, &bootDev);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetIndex(bootDev, devIndex);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetType(bootDev, devType);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetSequenceIndex(bootDev, bootSequence);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetInUse(bootDev, PRL_TRUE);
    prlsdkCheckRetGoto(pret, error);

    return 0;

 error:
    if (bootDev != PRL_INVALID_HANDLE)
        PrlBootDev_Remove(bootDev);

    return -1;
}

2814 2815 2816 2817
static int prlsdkCheckVideoUnsupportedParams(virDomainDefPtr def)
{
    virDomainVideoDefPtr v;

2818
    if (IS_CT(def)) {
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
        if (def->nvideos == 0) {
            return 0;
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Video adapters are not supported "
                             "int containers."));
            return -1;
        }
    } else {
        if (def->nvideos != 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2830
                           _("vz driver supports "
2831 2832 2833 2834 2835 2836 2837 2838 2839
                             "only one video adapter."));
            return -1;
        }
    }

    v = def->videos[0];

    if (v->type != VIR_DOMAIN_VIDEO_TYPE_VGA) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2840
                       _("vz driver supports "
2841 2842 2843 2844 2845 2846
                         "only VGA video adapters."));
        return -1;
    }

    if (v->heads != 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2847
                       _("vz driver doesn't support "
2848 2849 2850 2851
                         "multihead video adapters."));
        return -1;
    }

2852
    if (v->accel != NULL && (v->accel->accel2d || v->accel->accel3d)) {
2853
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2854
                       _("vz driver doesn't support "
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
                         "setting video acceleration parameters."));
        return -1;
    }

    return 0;
}

static int prlsdkCheckSerialUnsupportedParams(virDomainChrDefPtr chr)
{
    if (chr->deviceType != VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified character device type is not supported "
2867
                         "by vz driver."));
2868 2869 2870 2871 2872 2873
        return -1;
    }

    if (chr->targetTypeAttr) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified character device target type is not "
2874
                         "supported by vz driver."));
2875 2876 2877
        return -1;
    }

2878 2879 2880 2881 2882
    if (chr->source->type != VIR_DOMAIN_CHR_TYPE_DEV &&
        chr->source->type != VIR_DOMAIN_CHR_TYPE_FILE &&
        chr->source->type != VIR_DOMAIN_CHR_TYPE_UNIX &&
        chr->source->type != VIR_DOMAIN_CHR_TYPE_TCP &&
        chr->source->type != VIR_DOMAIN_CHR_TYPE_UDP) {
2883 2884 2885 2886


        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified character device source type is not "
2887
                         "supported by vz driver."));
2888 2889 2890 2891 2892 2893
        return -1;
    }

    if (chr->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting device info for character devices is not "
2894
                         "supported by vz driver."));
2895 2896 2897
        return -1;
    }

2898
    if (chr->source->nseclabels > 0) {
2899 2900
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting security labels is not "
2901
                         "supported by vz driver."));
2902 2903 2904
        return -1;
    }

2905 2906
   if (chr->source->type == VIR_DOMAIN_CHR_TYPE_TCP &&
        chr->source->data.tcp.protocol != VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW) {
2907 2908 2909
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Protocol '%s' is not supported for "
                         "tcp character device."),
2910
                       virDomainChrTcpProtocolTypeToString(chr->source->data.tcp.protocol));
2911 2912 2913
        return -1;
    }

2914 2915 2916 2917 2918
    if (chr->source->type == VIR_DOMAIN_CHR_TYPE_UDP &&
        (STRNEQ(chr->source->data.udp.bindHost,
                chr->source->data.udp.connectHost) ||
         STRNEQ(chr->source->data.udp.bindService,
                chr->source->data.udp.connectService))) {
2919 2920 2921 2922 2923 2924
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Different bind and connect parameters for "
                         "udp character device is not supported."));
        return -1;
    }

2925 2926 2927 2928 2929
    return 0;
}

static int prlsdkCheckNetUnsupportedParams(virDomainNetDefPtr net)
{
2930 2931
    if (net->type != VIR_DOMAIN_NET_TYPE_NETWORK &&
        net->type != VIR_DOMAIN_NET_TYPE_BRIDGE) {
2932 2933
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified network adapter type is not "
2934
                         "supported by vz driver."));
2935 2936 2937 2938 2939 2940
        return -1;
    }

    if (net->backend.tap || net->backend.vhost) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Interface backend parameters are not "
2941
                         "supported by vz driver."));
2942 2943 2944 2945 2946 2947
        return -1;
    }

    if (net->data.network.portgroup) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Virtual network portgroups are not "
2948
                         "supported by vz driver."));
2949 2950 2951 2952 2953 2954
        return -1;
    }

    if (net->tune.sndbuf_specified) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting interface sndbuf is not "
2955
                         "supported by vz driver."));
2956 2957 2958 2959 2960 2961
        return -1;
    }

    if (net->script) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting interface script is not "
2962
                         "supported by vz driver."));
2963 2964 2965
        return -1;
    }

2966
    if (net->ifname_guest) {
2967 2968
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting guest interface name is not "
2969
                         "supported by vz driver."));
2970 2971 2972 2973 2974 2975
        return -1;
    }

    if (net->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting device info for network devices is not "
2976
                         "supported by vz driver."));
2977 2978 2979 2980 2981 2982
        return -1;
    }

    if (net->filter) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting network filter is not "
2983
                         "supported by vz driver."));
2984 2985 2986 2987 2988 2989
        return -1;
    }

    if (net->bandwidth) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting network bandwidth is not "
2990
                         "supported by vz driver."));
2991 2992 2993 2994 2995 2996
        return -1;
    }

    if (net->vlan.trunk) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting up vlans is not "
2997
                         "supported by vz driver."));
2998 2999 3000 3001 3002 3003
        return -1;
    }

    return 0;
}

3004 3005
static int prlsdkCheckFSUnsupportedParams(virDomainFSDefPtr fs)
{
3006 3007
    if (fs->type != VIR_DOMAIN_FS_TYPE_FILE &&
        fs->type != VIR_DOMAIN_FS_TYPE_VOLUME) {
3008
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3009 3010
                       _("Only file based or volume based filesystems "
                         "are supported by vz driver."));
3011 3012 3013 3014 3015 3016
        return -1;
    }

    if (fs->fsdriver != VIR_DOMAIN_FS_DRIVER_TYPE_PLOOP) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only ploop fs driver is "
3017
                         "supported by vz driver."));
3018 3019 3020 3021 3022 3023
        return -1;
    }

    if (fs->accessmode != VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Changing fs access mode is not "
3024
                         "supported by vz driver."));
3025 3026 3027 3028 3029 3030
        return -1;
    }

    if (fs->wrpolicy != VIR_DOMAIN_FS_WRPOLICY_DEFAULT) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Changing fs write policy is not "
3031
                         "supported by vz driver."));
3032 3033 3034 3035 3036 3037
        return -1;
    }

    if (fs->format != VIR_STORAGE_FILE_PLOOP) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only ploop disk images are "
3038
                         "supported by vz driver."));
3039 3040 3041 3042 3043 3044
        return -1;
    }

    if (fs->readonly) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting readonly for filesystems is "
3045
                         "not supported by vz driver."));
3046 3047 3048 3049 3050 3051
        return -1;
    }

    if (fs->space_hard_limit || fs->space_soft_limit) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting fs quotas is not "
3052
                         "supported by vz driver."));
3053 3054 3055 3056 3057 3058
        return -1;
    }

    return 0;
}

3059 3060
static int prlsdkApplyGraphicsParams(PRL_HANDLE sdkdom,
                                     virDomainGraphicsDefPtr gr)
3061
{
3062
    virDomainGraphicsListenDefPtr glisten;
3063 3064 3065
    PRL_RESULT pret;
    int ret  = -1;

3066 3067 3068
    if (!gr) {
        pret = PrlVmCfg_SetVNCMode(sdkdom, PRD_DISABLED);
        prlsdkCheckRetExit(pret, -1);
3069
        return 0;
3070
    }
3071

N
Nikolay Shirokovskiy 已提交
3072 3073 3074
    pret = PrlVmCfg_SetVNCPassword(sdkdom, gr->data.vnc.auth.passwd ? : "");
    prlsdkCheckRetExit(pret, -1);

3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
    if (gr->data.vnc.autoport) {
        pret = PrlVmCfg_SetVNCMode(sdkdom, PRD_AUTO);
        prlsdkCheckRetGoto(pret, cleanup);
    } else {
        pret = PrlVmCfg_SetVNCMode(sdkdom, PRD_MANUAL);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlVmCfg_SetVNCPort(sdkdom, gr->data.vnc.port);
        prlsdkCheckRetGoto(pret, cleanup);
    }

3086 3087
    glisten = virDomainGraphicsGetListen(gr, 0);
    pret = PrlVmCfg_SetVNCHostName(sdkdom, glisten && glisten->address ?
3088
                                           glisten->address : VIR_LOOPBACK_IPV4_ADDR);
3089
    prlsdkCheckRetGoto(pret, cleanup);
3090

3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
    ret = 0;
 cleanup:
    return ret;
}

static int prlsdkApplyVideoParams(PRL_HANDLE sdkdom ATTRIBUTE_UNUSED, virDomainDefPtr def)
{
    PRL_RESULT pret;

    if (def->nvideos == 0)
        return 0;

    if (IS_CT(def)) {
        /* ignore video parameters */
        return 0;
    }

    if (prlsdkCheckVideoUnsupportedParams(def))
        return -1;

    pret = PrlVmCfg_SetVideoRamSize(sdkdom, def->videos[0]->vram >> 10);
    prlsdkCheckRetGoto(pret, error);

    return 0;
 error:
    return -1;
}

static int prlsdkAddSerial(PRL_HANDLE sdkdom, virDomainChrDefPtr chr)
{
    PRL_RESULT pret;
    PRL_HANDLE sdkchr = PRL_INVALID_HANDLE;
    PRL_VM_DEV_EMULATION_TYPE emutype;
M
Michal Privoznik 已提交
3124
    PRL_SERIAL_PORT_SOCKET_OPERATION_MODE socket_mode = PSP_SERIAL_SOCKET_SERVER;
3125
    char *path;
3126
    char *url = NULL;
3127 3128 3129 3130 3131 3132 3133 3134
    int ret = -1;

    if (prlsdkCheckSerialUnsupportedParams(chr) < 0)
        return -1;

    pret = PrlVmCfg_CreateVmDev(sdkdom, PDE_SERIAL_PORT, &sdkchr);
    prlsdkCheckRetGoto(pret, cleanup);

3135
    switch (chr->source->type) {
3136 3137
    case VIR_DOMAIN_CHR_TYPE_DEV:
        emutype = PDT_USE_REAL_DEVICE;
3138
        path = chr->source->data.file.path;
3139 3140 3141
        break;
    case VIR_DOMAIN_CHR_TYPE_FILE:
        emutype = PDT_USE_OUTPUT_FILE;
3142
        path = chr->source->data.file.path;
3143 3144 3145
        break;
    case VIR_DOMAIN_CHR_TYPE_UNIX:
        emutype = PDT_USE_SERIAL_PORT_SOCKET_MODE;
3146 3147
        path = chr->source->data.nix.path;
        if (!chr->source->data.nix.listen)
3148 3149 3150 3151 3152
            socket_mode = PSP_SERIAL_SOCKET_CLIENT;
        break;
    case VIR_DOMAIN_CHR_TYPE_TCP:
        emutype = PDT_USE_TCP;
        if (virAsprintf(&url, "%s:%s",
3153 3154
                        chr->source->data.tcp.host,
                        chr->source->data.tcp.service) < 0)
3155
            goto cleanup;
3156
        if (!chr->source->data.tcp.listen)
3157
            socket_mode = PSP_SERIAL_SOCKET_CLIENT;
3158 3159 3160 3161 3162
        path = url;
        break;
    case VIR_DOMAIN_CHR_TYPE_UDP:
        emutype = PDT_USE_UDP;
        if (virAsprintf(&url, "%s:%s",
3163 3164
                        chr->source->data.udp.bindHost,
                        chr->source->data.udp.bindService) < 0)
3165 3166
            goto cleanup;
        path = url;
3167 3168 3169
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3170
                       _("vz driver doesn't support "
3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
                         "specified serial source type."));
        goto cleanup;
    }

    pret = PrlVmDev_SetEmulatedType(sdkchr, emutype);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetSysName(sdkchr, path);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetFriendlyName(sdkchr, path);
    prlsdkCheckRetGoto(pret, cleanup);

3184 3185
    pret = PrlVmDevSerial_SetSocketMode(sdkchr, socket_mode);
    prlsdkCheckRetGoto(pret, cleanup);
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195

    pret = PrlVmDev_SetEnabled(sdkchr, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetIndex(sdkchr, chr->target.port);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;
 cleanup:
    PrlHandle_Free(sdkchr);
3196
    VIR_FREE(url);
3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
    return ret;
}

#define PRL_MAC_STRING_BUFNAME  13

static const char * prlsdkFormatMac(virMacAddrPtr mac, char *macstr)
{
    snprintf(macstr, PRL_MAC_STRING_BUFNAME,
             "%02X%02X%02X%02X%02X%02X",
             mac->addr[0], mac->addr[1], mac->addr[2],
             mac->addr[3], mac->addr[4], mac->addr[5]);
    macstr[PRL_MAC_STRING_BUFNAME - 1] = '\0';
    return macstr;
}

3212 3213 3214 3215
static int prlsdkConfigureGateways(PRL_HANDLE sdknet, virDomainNetDefPtr net)
{
    int ret = -1;
    size_t i;
3216
    virNetDevIPRoutePtr route4 = NULL, route6 = NULL;
3217 3218 3219
    char *gw4 = NULL, *gw6 = NULL;
    PRL_RESULT pret;

3220
    for (i = 0; i < net->guestIP.nroutes; i++) {
3221 3222 3223
        virSocketAddrPtr addrdst, gateway;
        virSocketAddr zero;

3224 3225
        addrdst = virNetDevIPRouteGetAddress(net->guestIP.routes[i]);
        gateway = virNetDevIPRouteGetGateway(net->guestIP.routes[i]);
3226 3227 3228 3229 3230 3231

        ignore_value(virSocketAddrParse(&zero,
                                (VIR_SOCKET_ADDR_IS_FAMILY(addrdst, AF_INET)
                                 ? VIR_SOCKET_ADDR_IPV4_ALL
                                 : VIR_SOCKET_ADDR_IPV6_ALL),
                                VIR_SOCKET_ADDR_FAMILY(addrdst)));
3232 3233 3234
        /* virSocketAddrParse raises an error
         * and we are not going to report it, reset it expicitly*/
        virResetLastError();
3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249

        if (!virSocketAddrEqual(addrdst, &zero)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Support only default gateway"));
            return -1;
        }

        switch (VIR_SOCKET_ADDR_FAMILY(gateway)) {
        case AF_INET:
            if (route4) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("Support only one IPv4 default gateway"));
                return -1;
            }

3250
            route4 = net->guestIP.routes[i];
3251 3252 3253 3254 3255 3256 3257 3258 3259

            break;
        case AF_INET6:
            if (route6) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("Support only one IPv6 default gateway"));
                return -1;
            }

3260
            route6 = net->guestIP.routes[i];
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273

            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported address family %d "
                             "Only IPv4 or IPv6 default gateway"),
                           VIR_SOCKET_ADDR_FAMILY(gateway));

            return -1;
        }
    }

    if (route4 &&
3274
        !(gw4 = virSocketAddrFormat(virNetDevIPRouteGetGateway(route4))))
3275 3276 3277 3278 3279 3280
        goto cleanup;

    pret = PrlVmDevNet_SetDefaultGateway(sdknet, gw4 ? : "");
    prlsdkCheckRetGoto(pret, cleanup);

    if (route6 &&
3281
        !(gw6 = virSocketAddrFormat(virNetDevIPRouteGetGateway(route6))))
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295
        goto cleanup;

    pret = PrlVmDevNet_SetDefaultGatewayIPv6(sdknet, gw6 ? : "");
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
    VIR_FREE(gw4);
    VIR_FREE(gw6);

    return ret;
}

3296 3297
static int prlsdkConfigureNet(vzDriverPtr driver ATTRIBUTE_UNUSED,
                              virDomainObjPtr dom ATTRIBUTE_UNUSED,
N
Nikolay Shirokovskiy 已提交
3298 3299 3300
                              PRL_HANDLE sdkdom,
                              virDomainNetDefPtr net,
                              bool isCt, bool create)
3301 3302 3303
{
    PRL_RESULT pret;
    PRL_HANDLE sdknet = PRL_INVALID_HANDLE;
3304 3305
    PRL_HANDLE addrlist = PRL_INVALID_HANDLE;
    size_t i;
3306 3307
    int ret = -1;
    char macstr[PRL_MAC_STRING_BUFNAME];
3308 3309 3310
    char *addrstr = NULL;
    bool ipv6present = false;
    bool ipv4present = false;
3311 3312 3313 3314

    if (prlsdkCheckNetUnsupportedParams(net) < 0)
        return -1;

N
Nikolay Shirokovskiy 已提交
3315 3316 3317 3318 3319 3320 3321 3322
    if (create) {
        pret = PrlVmCfg_CreateVmDev(sdkdom, PDE_GENERIC_NETWORK_ADAPTER, &sdknet);
        prlsdkCheckRetGoto(pret, cleanup);
    } else {
        sdknet = prlsdkFindNetByMAC(sdkdom, &net->mac);
        if (sdknet == PRL_INVALID_HANDLE)
            return -1;
    }
3323 3324 3325 3326

    pret = PrlVmDev_SetEnabled(sdknet, 1);
    prlsdkCheckRetGoto(pret, cleanup);

3327 3328 3329
    pret = PrlVmDev_SetConnected(sdknet, net->linkstate !=
                                 VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN);

3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340
    prlsdkCheckRetGoto(pret, cleanup);

    if (net->ifname) {
        pret = PrlVmDevNet_SetHostInterfaceName(sdknet, net->ifname);
        prlsdkCheckRetGoto(pret, cleanup);
    }

    prlsdkFormatMac(&net->mac, macstr);
    pret = PrlVmDevNet_SetMacAddress(sdknet, macstr);
    prlsdkCheckRetGoto(pret, cleanup);

3341 3342 3343
    pret = PrlApi_CreateStringsList(&addrlist);
    prlsdkCheckRetGoto(pret, cleanup);

3344
    for (i = 0; i < net->guestIP.nips; i++) {
3345 3346
        char *tmpstr;

3347
        if (AF_INET == VIR_SOCKET_ADDR_FAMILY(&net->guestIP.ips[i]->address))
3348
            ipv4present = true;
3349
        else if (AF_INET6 == VIR_SOCKET_ADDR_FAMILY(&net->guestIP.ips[i]->address))
3350 3351 3352 3353
            ipv6present = true;
        else
            continue;

3354
        if (!(tmpstr = virSocketAddrFormat(&net->guestIP.ips[i]->address)))
3355 3356
            goto cleanup;

3357
        if (virAsprintf(&addrstr, "%s/%d", tmpstr, net->guestIP.ips[i]->prefix) < 0) {
3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
            VIR_FREE(tmpstr);
            goto cleanup;
        }

        VIR_FREE(tmpstr);
        pret = PrlStrList_AddItem(addrlist, addrstr);
        prlsdkCheckRetGoto(pret, cleanup);

        VIR_FREE(addrstr);
    }

    if (ipv4present || ipv6present) {
        pret = PrlVmDevNet_SetNetAddresses(sdknet, addrlist);
        prlsdkCheckRetGoto(pret, cleanup);
    }

    pret = PrlVmDevNet_SetConfigureWithDhcp(sdknet, !ipv4present);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDevNet_SetConfigureWithDhcpIPv6(sdknet, !ipv6present);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDevNet_SetAutoApply(sdknet, true);
    prlsdkCheckRetGoto(pret, cleanup);

3383 3384
    if (prlsdkConfigureGateways(sdknet, net))
        goto cleanup;
3385

3386 3387
    if (isCt) {
        if (net->model)
M
Michal Privoznik 已提交
3388 3389
            VIR_WARN("Setting network adapter for containers is not "
                     "supported by vz driver.");
3390
    } else {
3391 3392 3393 3394 3395 3396 3397 3398
        if (STREQ(net->model, "rtl8139")) {
            pret = PrlVmDevNet_SetAdapterType(sdknet, PNT_RTL);
        } else if (STREQ(net->model, "e1000")) {
            pret = PrlVmDevNet_SetAdapterType(sdknet, PNT_E1000);
        } else if (STREQ(net->model, "virtio")) {
            pret = PrlVmDevNet_SetAdapterType(sdknet, PNT_VIRTIO);
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
3399 3400
                           _("Specified network adapter model is not "
                             "supported by vz driver."));
3401 3402 3403
            goto cleanup;
        }
        prlsdkCheckRetGoto(pret, cleanup);
3404 3405
    }

3406 3407 3408 3409
    if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
        if (STREQ(net->data.network.name, PARALLELS_DOMAIN_ROUTED_NETWORK_NAME)) {
            pret = PrlVmDev_SetEmulatedType(sdknet, PNA_ROUTED);
            prlsdkCheckRetGoto(pret, cleanup);
3410 3411
        } else {
            pret = PrlVmDev_SetEmulatedType(sdknet, PNA_BRIDGED_NETWORK);
3412 3413 3414 3415 3416 3417
            prlsdkCheckRetGoto(pret, cleanup);

            pret = PrlVmDevNet_SetVirtualNetworkId(sdknet, net->data.network.name);
            prlsdkCheckRetGoto(pret, cleanup);
        }

3418
    } else if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
3419

3420
        pret = PrlVmDev_SetEmulatedType(sdknet, PNA_BRIDGE);
3421 3422
        prlsdkCheckRetGoto(pret, cleanup);

3423
        pret = PrlVmDevNet_SetVirtualNetworkId(sdknet, net->data.bridge.brname);
3424 3425 3426
        prlsdkCheckRetGoto(pret, cleanup);
    }

3427 3428
    pret = PrlVmDevNet_SetPktFilterPreventMacSpoof(sdknet,
                net->trustGuestRxFilters == VIR_TRISTATE_BOOL_YES);
3429 3430 3431 3432
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;
 cleanup:
3433 3434
    VIR_FREE(addrstr);
    PrlHandle_Free(addrlist);
3435 3436 3437 3438
    PrlHandle_Free(sdknet);
    return ret;
}

3439 3440
static PRL_HANDLE
prlsdkFindNetByMAC(PRL_HANDLE sdkdom, virMacAddrPtr mac)
3441 3442 3443 3444 3445 3446 3447
{
    PRL_RESULT pret;
    PRL_UINT32 adaptersCount;
    PRL_UINT32 i;
    PRL_HANDLE adapter = PRL_INVALID_HANDLE;
    char adapterMac[PRL_MAC_STRING_BUFNAME];
    char expectedMac[PRL_MAC_STRING_BUFNAME];
3448
    char virMac[VIR_MAC_STRING_BUFLEN];
3449

3450 3451
    prlsdkFormatMac(mac, expectedMac);

3452 3453 3454 3455 3456 3457 3458
    pret = PrlVmCfg_GetNetAdaptersCount(sdkdom, &adaptersCount);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < adaptersCount; ++i) {
        pret = PrlVmCfg_GetNetAdapter(sdkdom, i, &adapter);
        prlsdkCheckRetGoto(pret, cleanup);

3459 3460
        pret = prlsdkGetStringParamBuf(PrlVmDevNet_GetMacAddress,
                                       adapter, adapterMac, sizeof(adapterMac));
3461 3462
        prlsdkCheckRetGoto(pret, cleanup);

3463
        if (STREQ(adapterMac, expectedMac))
3464
            return adapter;
3465

3466 3467
        PrlHandle_Free(adapter);
        adapter = PRL_INVALID_HANDLE;
3468 3469
    }

3470 3471 3472
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("No net with mac '%s'"), virMacAddrFormat(mac, virMac));

3473 3474
 cleanup:
    PrlHandle_Free(adapter);
3475
    return adapter;
3476 3477
}

N
Nikolay Shirokovskiy 已提交
3478 3479 3480 3481
static int prlsdkConfigureDisk(vzDriverPtr driver,
                               PRL_HANDLE sdkdom,
                               virDomainDiskDefPtr disk,
                               bool create)
3482 3483 3484 3485 3486 3487
{
    PRL_RESULT pret;
    PRL_HANDLE sdkdisk = PRL_INVALID_HANDLE;
    int ret = -1;
    PRL_VM_DEV_EMULATION_TYPE emutype;
    PRL_MASS_STORAGE_INTERFACE_TYPE sdkbus;
3488 3489
    int idx;
    virDomainDeviceDriveAddressPtr drive;
3490
    PRL_DEVICE_TYPE devType;
M
Mikhail Feoktistov 已提交
3491
    PRL_CLUSTERED_DEVICE_SUBTYPE scsiModel;
3492
    const char *path = disk->src->path ? : "";
3493

D
Dmitry Guryanov 已提交
3494
    if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
3495
        devType = PDE_HARD_DISK;
D
Dmitry Guryanov 已提交
3496
    else
3497 3498
        devType = PDE_OPTICAL_DISK;

N
Nikolay Shirokovskiy 已提交
3499 3500 3501 3502
    if (create) {
        pret = PrlVmCfg_CreateVmDev(sdkdom, devType, &sdkdisk);
        prlsdkCheckRetGoto(pret, cleanup);
    } else {
3503
        sdkdisk = prlsdkGetDisk(sdkdom, disk);
N
Nikolay Shirokovskiy 已提交
3504 3505 3506
        if (sdkdisk == PRL_INVALID_HANDLE)
            return -1;
    }
3507 3508 3509 3510 3511 3512 3513

    pret = PrlVmDev_SetEnabled(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetConnected(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

3514
    if (disk->src->type == VIR_STORAGE_TYPE_FILE)
3515
        emutype = PDT_USE_IMAGE_FILE;
3516
    else
3517 3518 3519 3520 3521
        emutype = PDT_USE_REAL_DEVICE;

    pret = PrlVmDev_SetEmulatedType(sdkdisk, emutype);
    prlsdkCheckRetGoto(pret, cleanup);

3522 3523
    pret = PrlVmDev_SetSysName(sdkdisk, path);
    prlsdkCheckRetGoto(pret, cleanup);
3524

3525 3526
    pret = PrlVmDev_SetFriendlyName(sdkdisk, path);
    prlsdkCheckRetGoto(pret, cleanup);
3527

3528 3529
    drive = &disk->info.addr.drive;

3530 3531 3532
    switch (disk->bus) {
    case VIR_DOMAIN_DISK_BUS_IDE:
        sdkbus = PMS_IDE_DEVICE;
3533
        idx = 2 * drive->bus + drive->unit;
3534 3535 3536
        break;
    case VIR_DOMAIN_DISK_BUS_SCSI:
        sdkbus = PMS_SCSI_DEVICE;
3537
        idx = drive->unit;
3538 3539 3540
        break;
    case VIR_DOMAIN_DISK_BUS_SATA:
        sdkbus = PMS_SATA_DEVICE;
3541
        idx = drive->unit;
3542 3543 3544 3545
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified disk bus is not "
3546
                         "supported by vz driver."));
3547 3548 3549
        goto cleanup;
    }

M
Mikhail Feoktistov 已提交
3550
    if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
3551
        if (vzGetDefaultSCSIModel(driver, &scsiModel) < 0)
M
Mikhail Feoktistov 已提交
3552 3553 3554 3555 3556
            goto cleanup;
        pret = PrlVmDev_SetSubType(sdkdisk, scsiModel);
        prlsdkCheckRetGoto(pret, cleanup);
    }

3557 3558 3559
    pret = PrlVmDev_SetIfaceType(sdkdisk, sdkbus);
    prlsdkCheckRetGoto(pret, cleanup);

3560
    pret = PrlVmDev_SetStackIndex(sdkdisk, idx);
3561 3562
    prlsdkCheckRetGoto(pret, cleanup);

3563 3564 3565 3566 3567
    if (devType == PDE_HARD_DISK) {
        pret = PrlVmDevHd_SetSerialNumber(sdkdisk, disk->serial);
        prlsdkCheckRetGoto(pret, cleanup);
    }

3568 3569 3570 3571 3572 3573
    return 0;
 cleanup:
    PrlHandle_Free(sdkdisk);
    return ret;
}

3574
static PRL_HANDLE
3575
prlsdkGetDisk(PRL_HANDLE sdkdom, virDomainDiskDefPtr disk)
3576 3577
{
    PRL_RESULT pret;
3578
    PRL_UINT32 num;
3579
    size_t i;
3580
    PRL_HANDLE sdkdisk = PRL_INVALID_HANDLE;
3581 3582
    int bus;
    char *dst = NULL;
3583
    PRL_DEVICE_TYPE devType;
3584

3585 3586 3587 3588 3589 3590
    if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
        devType = PDE_HARD_DISK;
    else
        devType = PDE_OPTICAL_DISK;

    pret = PrlVmCfg_GetDevsCountByType(sdkdom, devType, &num);
3591 3592
    prlsdkCheckRetGoto(pret, error);

3593 3594
    for (i = 0; i < num; ++i) {
        pret = PrlVmCfg_GetDevByType(sdkdom, devType, i, &sdkdisk);
3595 3596
        prlsdkCheckRetGoto(pret, error);

3597
        if (prlsdkGetDiskId(sdkdisk, &bus, &dst) < 0)
3598 3599 3600 3601
            goto error;

        if (disk->bus == bus && STREQ(disk->dst, dst)) {
            VIR_FREE(dst);
3602
            return sdkdisk;
3603 3604
        }

3605 3606
        PrlHandle_Free(sdkdisk);
        sdkdisk = PRL_INVALID_HANDLE;
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616
        VIR_FREE(dst);
    }

    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("No disk with bus '%s' and target '%s'"),
                   virDomainDiskBusTypeToString(disk->bus), disk->dst);
    return PRL_INVALID_HANDLE;

 error:
    VIR_FREE(dst);
3617
    PrlHandle_Free(sdkdisk);
3618 3619 3620
    return PRL_INVALID_HANDLE;
}

3621
int
3622
prlsdkAttachDevice(vzDriverPtr driver,
M
Mikhail Feoktistov 已提交
3623
                   virDomainObjPtr dom,
3624
                   virDomainDeviceDefPtr dev)
3625
{
3626
    vzDomObjPtr privdom = dom->privateData;
3627 3628 3629
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_BeginEdit(privdom->sdkdom);
3630
    if (PRL_FAILED(waitDomainJob(job, dom)))
3631
        return -1;
3632

3633 3634
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
N
Nikolay Shirokovskiy 已提交
3635
        if (prlsdkConfigureDisk(driver, privdom->sdkdom,
3636
                                dev->data.disk, true) < 0)
3637 3638 3639 3640 3641 3642 3643 3644
            return -1;

        break;
    case VIR_DOMAIN_DEVICE_NET:
        if (!IS_CT(dom->def)) {
            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                           _("attaching network device to VM is unsupported"));
            return -1;
3645
        }
3646

3647
        if (prlsdkConfigureNet(driver, dom, privdom->sdkdom, dev->data.net,
N
Nikolay Shirokovskiy 已提交
3648
                               IS_CT(dom->def), true) < 0)
3649 3650
            return -1;

3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
        break;
    case VIR_DOMAIN_DEVICE_GRAPHICS:
        if (dom->def->ngraphics > 0) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("domain already has VNC graphics"));
            return -1;
        }

        if (prlsdkApplyGraphicsParams(privdom->sdkdom, dev->data.graphics) < 0)
            return -1;

3662 3663 3664 3665 3666 3667
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("attaching device type '%s' is unsupported"),
                       virDomainDeviceTypeToString(dev->type));
        return -1;
3668 3669
    }

3670
    job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
3671
    if (PRL_FAILED(waitDomainJob(job, dom)))
3672 3673 3674
        return -1;

    return 0;
3675 3676
}

3677
int
3678
prlsdkDetachDevice(vzDriverPtr driver ATTRIBUTE_UNUSED,
3679 3680
                   virDomainObjPtr dom,
                   virDomainDeviceDefPtr dev)
3681
{
3682
    int ret = -1;
3683
    vzDomObjPtr privdom = dom->privateData;
3684
    PRL_HANDLE job = PRL_INVALID_HANDLE;
3685
    PRL_HANDLE sdkdev = PRL_INVALID_HANDLE;
3686
    PRL_RESULT pret;
3687 3688

    job = PrlVm_BeginEdit(privdom->sdkdom);
3689
    if (PRL_FAILED(waitDomainJob(job, dom)))
3690 3691
        goto cleanup;

3692 3693
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
3694
        sdkdev = prlsdkGetDisk(privdom->sdkdom, dev->data.disk);
3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
        if (sdkdev == PRL_INVALID_HANDLE)
            goto cleanup;

        pret = PrlVmDev_Remove(sdkdev);
        prlsdkCheckRetGoto(pret, cleanup);

        break;
    case VIR_DOMAIN_DEVICE_NET:
        if (!IS_CT(dom->def)) {
            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                           _("detaching network device from VM is unsupported"));
            goto cleanup;
        }

        sdkdev = prlsdkFindNetByMAC(privdom->sdkdom, &dev->data.net->mac);
        if (sdkdev == PRL_INVALID_HANDLE)
            goto cleanup;

        pret = PrlVmDev_Remove(sdkdev);
        prlsdkCheckRetGoto(pret, cleanup);

3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
        break;
    case VIR_DOMAIN_DEVICE_GRAPHICS:
        if (dom->def->ngraphics < 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("cannot find VNC graphics device"));
            goto cleanup;
        }

        if (prlsdkApplyGraphicsParams(privdom->sdkdom, NULL) < 0)
            goto cleanup;

3727 3728 3729 3730 3731 3732 3733
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("detaching device type '%s' is unsupported"),
                       virDomainDeviceTypeToString(dev->type));
        goto cleanup;
    }
3734 3735

    job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
3736
    if (PRL_FAILED(waitDomainJob(job, dom)))
3737 3738 3739
        goto cleanup;

    ret = 0;
3740 3741

 cleanup:
3742

3743
    PrlHandle_Free(sdkdev);
3744 3745 3746
    return ret;
}

N
Nikolay Shirokovskiy 已提交
3747 3748 3749 3750 3751 3752 3753 3754 3755
int
prlsdkUpdateDevice(vzDriverPtr driver,
                   virDomainObjPtr dom,
                   virDomainDeviceDefPtr dev)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_BeginEdit(privdom->sdkdom);
3756
    if (PRL_FAILED(waitDomainJob(job, dom)))
N
Nikolay Shirokovskiy 已提交
3757 3758 3759 3760 3761
        return -1;

    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
        if (prlsdkConfigureDisk(driver, privdom->sdkdom, dev->data.disk,
3762
                                false) < 0)
N
Nikolay Shirokovskiy 已提交
3763 3764 3765 3766
            return -1;

        break;
    case VIR_DOMAIN_DEVICE_NET:
3767
        if (prlsdkConfigureNet(driver, dom, privdom->sdkdom, dev->data.net,
N
Nikolay Shirokovskiy 已提交
3768 3769 3770
                               IS_CT(dom->def), false) < 0)
            return -1;

3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
        break;
    case VIR_DOMAIN_DEVICE_GRAPHICS:
        if (dom->def->ngraphics < 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("cannot find VNC graphics device"));
            return -1;
        }

        if (prlsdkApplyGraphicsParams(privdom->sdkdom, dev->data.graphics) < 0)
            return -1;

N
Nikolay Shirokovskiy 已提交
3782 3783 3784 3785 3786 3787 3788 3789 3790
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("updating device type '%s' is unsupported"),
                       virDomainDeviceTypeToString(dev->type));
        return -1;
    }

    job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
3791
    if (PRL_FAILED(waitDomainJob(job, dom)))
N
Nikolay Shirokovskiy 已提交
3792 3793 3794 3795 3796
        return -1;

    return 0;
}

3797 3798 3799 3800 3801 3802
static int
prlsdkAddFS(PRL_HANDLE sdkdom, virDomainFSDefPtr fs)
{
    PRL_RESULT pret;
    PRL_HANDLE sdkdisk = PRL_INVALID_HANDLE;
    int ret = -1;
3803
    char *storage = NULL;
3804

3805 3806 3807
    if (fs->type == VIR_DOMAIN_FS_TYPE_TEMPLATE)
        return 0;

3808 3809 3810 3811 3812 3813
    if (prlsdkCheckFSUnsupportedParams(fs) < 0)
        return -1;

    pret = PrlVmCfg_CreateVmDev(sdkdom, PDE_HARD_DISK, &sdkdisk);
    prlsdkCheckRetGoto(pret, cleanup);

3814 3815 3816 3817 3818 3819 3820 3821
    if (fs->type == VIR_DOMAIN_FS_TYPE_VOLUME) {
        if (virAsprintf(&storage, "libvirt://localhost/%s/%s", fs->src->srcpool->pool,
                        fs->src->srcpool->volume) < 0)
            goto cleanup;
        pret = PrlVmDevHd_SetStorageURL(sdkdisk, storage);
        prlsdkCheckRetGoto(pret, cleanup);
    }

3822 3823 3824 3825 3826 3827 3828 3829 3830
    pret = PrlVmDev_SetEnabled(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetConnected(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetEmulatedType(sdkdisk, PDT_USE_IMAGE_FILE);
    prlsdkCheckRetGoto(pret, cleanup);

3831
    pret = PrlVmDev_SetSysName(sdkdisk, fs->src->path);
3832 3833
    prlsdkCheckRetGoto(pret, cleanup);

3834
    pret = PrlVmDev_SetImagePath(sdkdisk, fs->src->path);
3835 3836
    prlsdkCheckRetGoto(pret, cleanup);

3837
    pret = PrlVmDev_SetFriendlyName(sdkdisk, fs->src->path);
3838 3839 3840 3841 3842 3843 3844 3845
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDevHd_SetMountPoint(sdkdisk, fs->dst);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
3846
    VIR_FREE(storage);
3847 3848 3849
    PrlHandle_Free(sdkdisk);
    return ret;
}
3850 3851 3852 3853 3854 3855 3856

static int
prlsdkSetBootOrderCt(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    size_t i;
    PRL_HANDLE hdd = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
3857
    bool rootfs = false;
3858 3859 3860
    int ret = -1;

    for (i = 0; i < def->nfss; i++) {
3861 3862 3863 3864

        pret = prlsdkAddDeviceToBootList(sdkdom, i, PDE_HARD_DISK, i + 1);
        prlsdkCheckRetExit(pret, -1);

3865
        if (STREQ(def->fss[i]->dst, "/"))
3866
            rootfs = true;
3867 3868
    }

3869 3870 3871 3872
    if (!rootfs) {
        /* if we have root mounted we don't need to explicitly set boot order */
        pret = PrlVmCfg_GetHardDisk(sdkdom, def->nfss, &hdd);
        prlsdkCheckRetExit(pret, -1);
3873

3874 3875 3876
        PrlVmDevHd_SetMountPoint(hdd, "/");
        prlsdkCheckRetGoto(pret, cleanup);
    }
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925

    ret = 0;

 cleanup:
    PrlHandle_Free(hdd);
    return ret;
}

static int
prlsdkSetBootOrderVm(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    size_t i;
    int idx[VIR_DOMAIN_BOOT_LAST] = { 0 };
    int bootIndex = 0;
    PRL_RESULT pret;
    PRL_UINT32 num;
    int sdkType;
    virDomainBootOrder virType;

    for (i = 0; i < def->os.nBootDevs; ++i) {
        virType = def->os.bootDevs[i];

        switch (virType) {
        case VIR_DOMAIN_BOOT_CDROM:
            sdkType = PDE_OPTICAL_DISK;
            break;
        case VIR_DOMAIN_BOOT_DISK:
            sdkType = PDE_HARD_DISK;
            break;
        case VIR_DOMAIN_BOOT_NET:
            sdkType = PDE_GENERIC_NETWORK_ADAPTER;
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported boot device type: '%s'"),
                           virDomainBootTypeToString(virType));
            return -1;
        }

        pret = PrlVmCfg_GetDevsCountByType(sdkdom, sdkType, &num);
        prlsdkCheckRetExit(pret, -1);

        pret = prlsdkAddDeviceToBootList(sdkdom, idx[virType]++, sdkType, bootIndex++);
        prlsdkCheckRetExit(pret, -1);
    }

    return 0;
}

3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938
int
prlsdkDomainSetUserPassword(virDomainObjPtr dom,
                            const char *user,
                            const char *password)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_SetUserPasswd(privdom->sdkdom,
                              user,
                              password,
                              0);

3939
    if (PRL_FAILED(waitDomainJob(job, dom)))
3940
        return -1;
3941

3942
    return 0;
3943 3944
}

3945
static int
3946
prlsdkDoApplyConfig(vzDriverPtr driver,
3947
                    virDomainObjPtr dom,
3948
                    PRL_HANDLE sdkdom,
3949
                    virDomainDefPtr def)
3950 3951 3952
{
    PRL_RESULT pret;
    size_t i;
3953
    char uuidstr[VIR_UUID_STRING_BRACED_BUFLEN];
M
Maxim Nestratov 已提交
3954
    char *mask = NULL;
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975

    if (prlsdkCheckUnsupportedParams(sdkdom, def) < 0)
        return -1;

    if (def->description) {
        pret = PrlVmCfg_SetDescription(sdkdom, def->description);
        prlsdkCheckRetGoto(pret, error);
    }

    if (def->name) {
        pret = PrlVmCfg_SetName(sdkdom, def->name);
        prlsdkCheckRetGoto(pret, error);
    }

    if (def->uuid) {
        prlsdkUUIDFormat(def->uuid, uuidstr);

        pret = PrlVmCfg_SetUuid(sdkdom, uuidstr);
        prlsdkCheckRetGoto(pret, error);
    }

3976
    pret = PrlVmCfg_SetRamSize(sdkdom, virDomainDefGetMemoryTotal(def) >> 10);
3977 3978
    prlsdkCheckRetGoto(pret, error);

3979
    pret = PrlVmCfg_SetCpuCount(sdkdom, virDomainDefGetVcpus(def));
3980 3981
    prlsdkCheckRetGoto(pret, error);

M
Maxim Nestratov 已提交
3982 3983 3984 3985 3986 3987 3988
    if (!(mask = virBitmapFormat(def->cpumask)))
        goto error;

    pret = PrlVmCfg_SetCpuMask(sdkdom, mask);
    prlsdkCheckRetGoto(pret, error);
    VIR_FREE(mask);

3989
    switch (def->os.arch) {
M
Michal Privoznik 已提交
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000
    case VIR_ARCH_X86_64:
        pret = PrlVmCfg_SetCpuMode(sdkdom, PCM_CPU_MODE_64);
        break;
    case VIR_ARCH_I686:
        pret = PrlVmCfg_SetCpuMode(sdkdom, PCM_CPU_MODE_32);
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU mode: %s"),
                       virArchToString(def->os.arch));
        goto error;
4001 4002 4003
    }
    prlsdkCheckRetGoto(pret, error);

4004 4005 4006
    if (prlsdkClearDevices(sdkdom) < 0)
        goto error;

4007 4008 4009
    if (prlsdkRemoveBootDevices(sdkdom) < 0)
        goto error;

4010
    for (i = 0; i < def->nnets; i++) {
4011
        if (prlsdkConfigureNet(driver, dom, sdkdom, def->nets[i],
N
Nikolay Shirokovskiy 已提交
4012
                               IS_CT(def), true) < 0)
M
Michal Privoznik 已提交
4013
            goto error;
4014 4015
    }

4016 4017 4018 4019 4020 4021 4022 4023
    if (def->ngraphics > 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("vz driver supports only VNC graphics"));
        goto error;
    }

    if (prlsdkApplyGraphicsParams(sdkdom,
                                  def->ngraphics == 1 ? def->graphics[0] : NULL) < 0)
4024 4025 4026 4027 4028 4029
        goto error;

    if (prlsdkApplyVideoParams(sdkdom, def) < 0)
        goto error;

    for (i = 0; i < def->nserials; i++) {
M
Mikhail Feoktistov 已提交
4030 4031
        if (prlsdkAddSerial(sdkdom, def->serials[i]) < 0)
            goto error;
4032 4033
    }

4034 4035
    /* It is important that we add filesystems first and then disks as we rely
     * on this information in prlsdkSetBootOrderCt */
4036 4037 4038 4039 4040
    for (i = 0; i < def->nfss; i++) {
        if (prlsdkAddFS(sdkdom, def->fss[i]) < 0)
            goto error;
    }

4041 4042
    /* filesystems first, disks go after them as we rely on this order in
     * prlsdkSetBootOrderCt */
4043
    for (i = 0; i < def->ndisks; i++) {
N
Nikolay Shirokovskiy 已提交
4044
        if (prlsdkConfigureDisk(driver, sdkdom, def->disks[i],
4045
                                true) < 0)
4046 4047
            goto error;
    }
4048

4049 4050 4051 4052 4053
    if (IS_CT(def)) {
        if (prlsdkSetBootOrderCt(sdkdom, def) < 0)
            goto error;
    } else {
        if (prlsdkSetBootOrderVm(sdkdom, def) < 0)
M
Mikhail Feoktistov 已提交
4054
            goto error;
4055 4056 4057 4058 4059
    }

    return 0;

 error:
M
Maxim Nestratov 已提交
4060 4061
    VIR_FREE(mask);

M
Michal Privoznik 已提交
4062
    return -1;
4063 4064 4065
}

int
4066
prlsdkApplyConfig(vzDriverPtr driver,
4067 4068 4069
                  virDomainObjPtr dom,
                  virDomainDefPtr new)
{
4070
    vzDomObjPtr privdom = dom->privateData;
4071 4072 4073
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    int ret;

4074
    job = PrlVm_BeginEdit(privdom->sdkdom);
4075
    if (PRL_FAILED(waitDomainJob(job, dom)))
4076 4077
        return -1;

4078
    ret = prlsdkDoApplyConfig(driver, dom, privdom->sdkdom, new);
4079 4080

    if (ret == 0) {
4081
        job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
4082
        if (PRL_FAILED(waitDomainJob(job, dom)))
4083
            ret = -1;
4084 4085 4086 4087
    }

    return ret;
}
4088 4089

int
4090
prlsdkCreateVm(vzDriverPtr driver, virDomainDefPtr def)
4091 4092 4093 4094 4095 4096 4097 4098
{
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_HANDLE srvconf = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
    int ret = -1;

4099
    pret = PrlSrv_CreateVm(driver->server, &sdkdom);
4100 4101
    prlsdkCheckRetGoto(pret, cleanup);

4102
    job = PrlSrv_GetSrvConfig(driver->server);
4103
    if (PRL_FAILED(getJobResult(job, &result)))
4104 4105 4106 4107 4108 4109 4110 4111
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, &srvconf);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmCfg_SetDefaultConfig(sdkdom, srvconf, PVS_GUEST_VER_LIN_REDHAT, 0);
    prlsdkCheckRetGoto(pret, cleanup);

4112 4113 4114
    pret = PrlVmCfg_SetOfflineManagementEnabled(sdkdom, 0);
    prlsdkCheckRetGoto(pret, cleanup);

4115
    if (prlsdkDoApplyConfig(driver, NULL, sdkdom, def) < 0)
4116 4117 4118
        goto cleanup;

    job = PrlVm_Reg(sdkdom, "", 1);
4119
    if (PRL_FAILED(waitJob(job)))
4120 4121 4122
        goto cleanup;

    ret = 0;
4123 4124 4125

 cleanup:
    PrlHandle_Free(sdkdom);
4126 4127 4128
    PrlHandle_Free(srvconf);
    PrlHandle_Free(result);

4129 4130 4131
    return ret;
}

4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175
static int
virStorageTranslatePoolLocal(virConnectPtr conn, virStorageSourcePtr src)
{

    virStoragePoolPtr pool = NULL;
    virStorageVolPtr vol = NULL;
    virStorageVolInfo info;
    int ret = -1;

    if (!(pool = virStoragePoolLookupByName(conn, src->srcpool->pool)))
        return -1;
    if (virStoragePoolIsActive(pool) != 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("storage pool '%s' containing volume '%s' "
                         "is not active"), src->srcpool->pool,
                       src->srcpool->volume);
        goto cleanup;
    }

    if (!(vol = virStorageVolLookupByName(pool, src->srcpool->volume)))
        goto cleanup;

    if (virStorageVolGetInfo(vol, &info) < 0)
        goto cleanup;

    if (info.type != VIR_STORAGE_VOL_PLOOP) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported volume format '%s'"),
                       virStorageVolTypeToString(info.type));
        goto cleanup;
    }

    if (!(src->path = virStorageVolGetPath(vol)))
        goto cleanup;

    ret = 0;

 cleanup:
    virObjectUnref(pool);
    virObjectUnref(vol);
    return ret;
}


4176
int
4177
prlsdkCreateCt(virConnectPtr conn, virDomainDefPtr def)
4178 4179 4180 4181 4182 4183
{
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;
    PRL_GET_VM_CONFIG_PARAM_DATA confParam;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
4184
    PRL_UINT32 flags;
4185 4186
    vzConnPtr privconn = conn->privateData;
    vzDriverPtr driver = privconn->driver;
4187
    int ret = -1;
4188 4189
    int useTemplate = 0;
    size_t i;
4190

4191 4192
    for (i = 0; i < def->nfss; i++) {
        if (useTemplate) {
4193
            virReportError(VIR_ERR_INVALID_ARG, "%s",
4194
                           _("Unsupported configuration"));
4195 4196
            return -1;
        }
4197 4198
        if (def->fss[i]->type == VIR_DOMAIN_FS_TYPE_TEMPLATE)
            useTemplate = 1;
4199 4200 4201 4202 4203
        if (def->fss[i]->type == VIR_DOMAIN_FS_TYPE_VOLUME) {
            if (virStorageTranslatePoolLocal(conn, def->fss[i]->src) < 0)
                goto cleanup;
        }

4204 4205 4206 4207 4208 4209
    }

    if (useTemplate && def->nfss > 1) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Unsupported configuration"));
        return -1;
4210 4211 4212 4213 4214 4215
    }

    confParam.nVmType = PVT_CT;
    confParam.sConfigSample = "vswap.1024MB";
    confParam.nOsVersion = 0;

4216
    job = PrlSrv_GetDefaultVmConfig(driver->server, &confParam, 0);
4217
    if (PRL_FAILED(getJobResult(job, &result)))
4218 4219 4220 4221 4222
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, &sdkdom);
    prlsdkCheckRetGoto(pret, cleanup);

4223
    if (useTemplate) {
4224
        pret = PrlVmCfg_SetOsTemplate(sdkdom, def->fss[0]->src->path);
4225
        prlsdkCheckRetGoto(pret, cleanup);
4226

4227 4228
    }

4229
    if (prlsdkDoApplyConfig(driver, NULL, sdkdom, def) < 0)
4230 4231
        goto cleanup;

4232 4233 4234 4235
    flags = PACF_NON_INTERACTIVE_MODE;
    if (!useTemplate)
        flags |= PRNVM_PRESERVE_DISK;
    job = PrlVm_RegEx(sdkdom, "", flags);
4236
    if (PRL_FAILED(waitJob(job)))
4237 4238 4239
        goto cleanup;

    ret = 0;
4240 4241 4242

 cleanup:
    PrlHandle_Free(sdkdom);
4243
    PrlHandle_Free(result);
4244
    return ret;
4245
}
4246

4247 4248 4249 4250 4251 4252 4253 4254
/**
 * prlsdkDetachDomainHardDisks:
 *
 * @sdkdom: domain handle
 *
 * Returns 0 if hard disks were successfully detached or not detected.
 */
static int
4255
prlsdkDetachDomainHardDisks(virDomainObjPtr dom)
4256 4257 4258 4259 4260 4261
{
    int ret = -1;
    PRL_RESULT pret;
    PRL_UINT32 hddCount;
    PRL_UINT32 i;
    PRL_HANDLE job;
4262
    PRL_HANDLE sdkdisk = PRL_INVALID_HANDLE;
4263 4264
    vzDomObjPtr pdom = dom->privateData;
    PRL_HANDLE sdkdom = pdom->sdkdom;
4265 4266

    job = PrlVm_BeginEdit(sdkdom);
4267
    if (PRL_FAILED(waitDomainJob(job, dom)))
4268 4269 4270 4271 4272 4273
        goto cleanup;

    pret = PrlVmCfg_GetHardDisksCount(sdkdom, &hddCount);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < hddCount; ++i) {
M
Mikhail Feoktistov 已提交
4274
        pret = PrlVmCfg_GetHardDisk(sdkdom, 0, &sdkdisk);
4275 4276 4277 4278 4279 4280 4281
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlVmDev_Remove(sdkdisk);
        prlsdkCheckRetGoto(pret, cleanup);

        PrlHandle_Free(sdkdisk);
        sdkdisk = PRL_INVALID_HANDLE;
4282 4283 4284
    }

    job = PrlVm_CommitEx(sdkdom, PVCF_DETACH_HDD_BUNDLE);
4285
    if (PRL_FAILED(waitDomainJob(job, dom)))
4286
        goto cleanup;
4287

4288
    ret = 0;
4289

4290 4291
 cleanup:
    PrlHandle_Free(sdkdisk);
4292 4293 4294
    return ret;
}

4295
int
4296
prlsdkUnregisterDomain(vzDriverPtr driver, virDomainObjPtr dom, unsigned int flags)
4297
{
4298
    vzDomObjPtr privdom = dom->privateData;
4299
    PRL_HANDLE job;
4300
    virDomainSnapshotObjListPtr snapshots = NULL;
4301
    VIRTUAL_MACHINE_STATE domainState;
4302 4303
    int ret = -1;
    int num;
4304

4305
    if (prlsdkGetDomainState(dom, privdom->sdkdom, &domainState) < 0)
4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316
        return -1;

    if (VMS_SUSPENDED == domainState &&
        !(flags & VIR_DOMAIN_UNDEFINE_MANAGED_SAVE)) {

        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Refusing to undefine while domain managed "
                         "save image exists"));
        return -1;
    }

4317
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
4318
        return -1;
4319

4320 4321 4322 4323
    if ((num = virDomainSnapshotObjListNum(snapshots, NULL, 0)) < 0)
        goto cleanup;

    if (num > 0 && !(flags & VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA)) {
4324 4325
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("Refusing to undefine while snapshots exist"));
4326
        goto cleanup;
4327
    }
4328

4329
    if (prlsdkDetachDomainHardDisks(dom))
4330
        goto cleanup;
4331 4332

    job = PrlVm_Delete(privdom->sdkdom, PRL_INVALID_HANDLE);
4333
    if (PRL_FAILED(waitDomainJob(job, dom)))
4334
        goto cleanup;
4335

4336 4337
    prlsdkSendEvent(driver, dom, VIR_DOMAIN_EVENT_UNDEFINED,
                    VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
4338

4339
    virDomainObjListRemove(driver->domains, dom);
4340
    virObjectLock(dom);
4341 4342 4343 4344 4345 4346

    ret = 0;
 cleanup:

    virDomainSnapshotObjListFree(snapshots);
    return ret;
4347
}
4348 4349

int
4350
prlsdkDomainManagedSaveRemove(virDomainObjPtr dom)
4351
{
4352
    vzDomObjPtr privdom = dom->privateData;
4353 4354 4355
    PRL_HANDLE job;

    job = PrlVm_DropSuspendedState(privdom->sdkdom);
4356
    if (PRL_FAILED(waitDomainJob(job, dom)))
4357 4358 4359 4360
        return -1;

    return 0;
}
4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392

static int
prlsdkExtractStatsParam(PRL_HANDLE sdkstats, const char *name, long long *val)
{
    PRL_HANDLE param = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
    PRL_INT64 pval = 0;
    int ret = -1;

    pret = PrlEvent_GetParamByName(sdkstats, name, &param);
    if (pret == PRL_ERR_NO_DATA) {
        *val = -1;
        ret = 0;
        goto cleanup;
    } else if (PRL_FAILED(pret)) {
        logPrlError(pret);
        goto cleanup;
    }
    pret = PrlEvtPrm_ToInt64(param, &pval);
    prlsdkCheckRetGoto(pret, cleanup);

    *val = pval;
    ret = 0;

 cleanup:
    PrlHandle_Free(param);
    return ret;
}

#define PARALLELS_STATISTICS_TIMEOUT (60 * 1000)

int
4393 4394
prlsdkGetBlockStats(PRL_HANDLE sdkstats,
                    virDomainDiskDefPtr disk,
M
Maxim Nestratov 已提交
4395 4396
                    virDomainBlockStatsPtr stats,
                    bool isCt)
4397 4398 4399 4400 4401 4402 4403 4404
{
    virDomainDeviceDriveAddressPtr address;
    int idx;
    const char *prefix;
    int ret = -1;
    char *name = NULL;

    address = &disk->info.addr.drive;
M
Maxim Nestratov 已提交
4405 4406 4407

    if (isCt) {
        prefix = "hdd";
4408
        idx = address->unit;
M
Maxim Nestratov 已提交
4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427
    } else {
        switch (disk->bus) {
        case VIR_DOMAIN_DISK_BUS_IDE:
            prefix = "ide";
            idx = address->bus * 2 + address->unit;
            break;
        case VIR_DOMAIN_DISK_BUS_SATA:
            prefix = "sata";
            idx = address->unit;
            break;
        case VIR_DOMAIN_DISK_BUS_SCSI:
            prefix = "scsi";
            idx = address->unit;
            break;
        default:
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown disk bus: %X"), disk->bus);
            goto cleanup;
        }
4428 4429 4430 4431 4432 4433
    }


#define PRLSDK_GET_STAT_PARAM(VAL, TYPE, NAME)                          \
    if (virAsprintf(&name, "devices.%s%d.%s", prefix, idx, NAME) < 0)   \
        goto cleanup;                                                   \
4434
    if (prlsdkExtractStatsParam(sdkstats, name, &stats->VAL) < 0)       \
4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448
        goto cleanup;                                                   \
    VIR_FREE(name);

    PARALLELS_BLOCK_STATS_FOREACH(PRLSDK_GET_STAT_PARAM)

#undef PRLSDK_GET_STAT_PARAM

    ret = 0;

 cleanup:

    VIR_FREE(name);
    return ret;
}
4449

4450

4451
static PRL_HANDLE
4452
prlsdkFindNetByPath(PRL_HANDLE sdkdom, const char *path)
4453 4454 4455 4456 4457 4458 4459
{
    PRL_UINT32 count = 0;
    PRL_RESULT pret;
    size_t i;
    char *name = NULL;
    PRL_HANDLE net = PRL_INVALID_HANDLE;

4460
    pret = PrlVmCfg_GetNetAdaptersCount(sdkdom, &count);
4461 4462 4463
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < count; ++i) {
4464
        pret = PrlVmCfg_GetNetAdapter(sdkdom, i, &net);
4465 4466
        prlsdkCheckRetGoto(pret, error);

4467 4468
        if (!(name = prlsdkGetStringParamVar(PrlVmDevNet_GetHostInterfaceName,
                                             net)))
4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490
            goto error;

        if (STREQ(name, path))
            break;

        VIR_FREE(name);
        PrlHandle_Free(net);
        net = PRL_INVALID_HANDLE;
    }

    if (net == PRL_INVALID_HANDLE)
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
    return net;

 error:
    VIR_FREE(name);
    PrlHandle_Free(net);
    return PRL_INVALID_HANDLE;
}

int
4491
prlsdkGetNetStats(PRL_HANDLE sdkstats, PRL_HANDLE sdkdom, const char *path,
4492 4493 4494 4495 4496 4497 4498 4499
                  virDomainInterfaceStatsPtr stats)
{
    int ret = -1;
    PRL_UINT32 net_index = -1;
    char *name = NULL;
    PRL_RESULT pret;
    PRL_HANDLE net = PRL_INVALID_HANDLE;

4500
    net = prlsdkFindNetByPath(sdkdom, path);
4501 4502 4503 4504 4505 4506 4507
    if (net == PRL_INVALID_HANDLE)
       goto cleanup;

    pret = PrlVmDev_GetIndex(net, &net_index);
    prlsdkCheckRetGoto(pret, cleanup);

#define PRLSDK_GET_NET_COUNTER(VAL, NAME)                           \
4508
    if (virAsprintf(&name, "net.nic%u.%s", net_index, NAME) < 0)    \
4509
        goto cleanup;                                               \
4510
    if (prlsdkExtractStatsParam(sdkstats, name, &stats->VAL) < 0)   \
4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531
        goto cleanup;                                               \
    VIR_FREE(name);

    PRLSDK_GET_NET_COUNTER(rx_bytes, "bytes_in")
    PRLSDK_GET_NET_COUNTER(rx_packets, "pkts_in")
    PRLSDK_GET_NET_COUNTER(tx_bytes, "bytes_out")
    PRLSDK_GET_NET_COUNTER(tx_packets, "pkts_out")
    stats->rx_errs = -1;
    stats->rx_drop = -1;
    stats->tx_errs = -1;
    stats->tx_drop = -1;

#undef PRLSDK_GET_NET_COUNTER
    ret = 0;

 cleanup:
    VIR_FREE(name);
    PrlHandle_Free(net);

    return ret;
}
N
Nikolay Shirokovskiy 已提交
4532 4533

int
4534
prlsdkGetVcpuStats(PRL_HANDLE sdkstats, int idx, unsigned long long *vtime)
N
Nikolay Shirokovskiy 已提交
4535 4536 4537 4538 4539 4540 4541
{
    char *name = NULL;
    long long ptime = 0;
    int ret = -1;

    if (virAsprintf(&name, "guest.vcpu%u.time", (unsigned int)idx) < 0)
        goto cleanup;
4542
    if (prlsdkExtractStatsParam(sdkstats, name, &ptime) < 0)
N
Nikolay Shirokovskiy 已提交
4543 4544 4545 4546 4547 4548 4549 4550
        goto cleanup;
    *vtime = ptime == -1 ? 0 : ptime;
    ret = 0;

 cleanup:
    VIR_FREE(name);
    return ret;
}
N
Nikolay Shirokovskiy 已提交
4551 4552

int
4553
prlsdkGetMemoryStats(PRL_HANDLE sdkstats,
N
Nikolay Shirokovskiy 已提交
4554 4555 4556 4557 4558 4559 4560 4561
                     virDomainMemoryStatPtr stats,
                     unsigned int nr_stats)
{
    int ret = -1;
    long long v = 0, t = 0, u = 0;
    size_t i = 0;

#define PRLSDK_GET_COUNTER(NAME, VALUE)                             \
4562
    if (prlsdkExtractStatsParam(sdkstats, NAME, &VALUE) < 0)        \
N
Nikolay Shirokovskiy 已提交
4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611
        goto cleanup;                                               \

#define PRLSDK_MEMORY_STAT_SET(TAG, VALUE)                          \
    if (i < nr_stats) {                                             \
        stats[i].tag = (TAG);                                       \
        stats[i].val = (VALUE);                                     \
        i++;                                                        \
    }

    i = 0;

    // count to kb
    PRLSDK_GET_COUNTER("guest.ram.swap_in", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_SWAP_IN, v << 12)

    PRLSDK_GET_COUNTER("guest.ram.swap_out", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_SWAP_OUT, v << 12)

    PRLSDK_GET_COUNTER("guest.ram.minor_fault", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT, v)

    PRLSDK_GET_COUNTER("guest.ram.major_fault", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT, v)

    PRLSDK_GET_COUNTER("guest.ram.total", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_AVAILABLE, v << 10)

    PRLSDK_GET_COUNTER("guest.ram.balloon_actual", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_ACTUAL_BALLOON, v << 10)

    PRLSDK_GET_COUNTER("guest.ram.usage", u)
    PRLSDK_GET_COUNTER("guest.ram.total", t)
    if (u != -1 && t != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_UNUSED, (t - u) << 10)

#undef PRLSDK_GET_COUNTER
#undef PRLSDK_MEMORY_STAT_SET

    ret = i;
 cleanup:

    return ret;
}
4612 4613 4614 4615 4616 4617 4618 4619 4620

/* memsize is in MiB */
int prlsdkSetMemsize(virDomainObjPtr dom, unsigned int memsize)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job;
    PRL_RESULT pret;

    job = PrlVm_BeginEdit(privdom->sdkdom);
4621
    if (PRL_FAILED(waitDomainJob(job, dom)))
4622 4623 4624 4625 4626 4627
        goto error;

    pret = PrlVmCfg_SetRamSize(privdom->sdkdom, memsize);
    prlsdkCheckRetGoto(pret, error);

    job = PrlVm_CommitEx(privdom->sdkdom, 0);
4628
    if (PRL_FAILED(waitDomainJob(job, dom)))
4629 4630 4631 4632 4633 4634 4635
        goto error;

    return 0;

 error:
    return -1;
}
4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797

static long long
prlsdkParseDateTime(const char *str)
{
    struct tm tm;
    const char *tmp;

    tmp = strptime(str, "%Y-%m-%d %H:%M:%S", &tm);
    if (!tmp || *tmp != '\0') {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected DateTime format: '%s'"), str);
        return -1;
    }

    return mktime(&tm);
}

static virDomainSnapshotObjListPtr
prlsdkParseSnapshotTree(const char *treexml)
{
    virDomainSnapshotObjListPtr ret = NULL;
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr root;
    xmlNodePtr *nodes = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snapshot;
    virDomainSnapshotObjPtr current = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;
    char *xmlstr = NULL;
    int n;
    size_t i;

    if (!(snapshots = virDomainSnapshotObjListNew()))
        return NULL;

    if (*treexml == '\0')
        return snapshots;

    if (!(xml = virXMLParse(NULL, treexml, _("(snapshot_tree)"))))
        goto cleanup;

    root = xmlDocGetRootElement(xml);
    if (!xmlStrEqual(root->name, BAD_CAST "ParallelsSavedStates")) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected root element: '%s'"), root->name);
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        virReportOOMError();
        goto cleanup;
    }
    ctxt->node = root;

    if ((n = virXPathNodeSet("//SavedStateItem", ctxt, &nodes)) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot extract snapshot nodes"));
        goto cleanup;
    }

    for (i = 0; i < n; i++) {
        if (nodes[i]->parent == root)
            continue;

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

        ctxt->node = nodes[i];

        def->name = virXPathString("string(./@guid)", ctxt);
        if (!def->name) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("missing 'guid' attribute"));
            goto cleanup;
        }

        def->parent = virXPathString("string(../@guid)", ctxt);

        xmlstr = virXPathString("string(./DateTime)", ctxt);
        if (!xmlstr) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("missing 'DateTime' element"));
            goto cleanup;
        }
        if ((def->creationTime = prlsdkParseDateTime(xmlstr)) < 0)
            goto cleanup;
        VIR_FREE(xmlstr);

        def->description = virXPathString("string(./Description)", ctxt);

        def->memory = VIR_DOMAIN_SNAPSHOT_LOCATION_NONE;
        xmlstr = virXPathString("string(./@state)", ctxt);
        if (!xmlstr) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("missing 'state' attribute"));
            goto cleanup;
        } else if (STREQ(xmlstr, "poweron")) {
            def->state = VIR_DOMAIN_RUNNING;
            def->memory = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
        } else if (STREQ(xmlstr, "pause")) {
            def->state = VIR_DOMAIN_PAUSED;
            def->memory = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
        } else if (STREQ(xmlstr, "suspend")) {
            def->state = VIR_DOMAIN_SHUTOFF;
        } else if (STREQ(xmlstr, "poweroff")) {
            def->state = VIR_DOMAIN_SHUTOFF;
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected snapshot state: %s"), xmlstr);
        }
        VIR_FREE(xmlstr);

        xmlstr = virXPathString("string(./@current)", ctxt);
        def->current = xmlstr && STREQ("yes", xmlstr);
        VIR_FREE(xmlstr);

        if (!(snapshot = virDomainSnapshotAssignDef(snapshots, def)))
            goto cleanup;
        def = NULL;

        if (snapshot->def->current) {
            if (current) {
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("too many current snapshots"));
                goto cleanup;
            }
            current = snapshot;
        }
    }

    if (virDomainSnapshotUpdateRelations(snapshots) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("snapshots have inconsistent relations"));
        goto cleanup;
    }

    ret = snapshots;
    snapshots = NULL;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
    VIR_FREE(nodes);
    VIR_FREE(xmlstr);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    VIR_FREE(def);

    return ret;
}

virDomainSnapshotObjListPtr
prlsdkLoadSnapshots(virDomainObjPtr dom)
{
    virDomainSnapshotObjListPtr ret = NULL;
    PRL_HANDLE job;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    vzDomObjPtr privdom = dom->privateData;
    char *treexml = NULL;

    job = PrlVm_GetSnapshotsTreeEx(privdom->sdkdom, PGST_WITHOUT_SCREENSHOTS);
4798
    if (PRL_FAILED(getDomainJobResult(job, dom, &result)))
4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818
        goto cleanup;

    if (!(treexml = prlsdkGetStringParamVar(PrlResult_GetParamAsString, result)))
        goto cleanup;

    ret = prlsdkParseSnapshotTree(treexml);
 cleanup:

    PrlHandle_Free(result);
    VIR_FREE(treexml);
    return ret;
}

int prlsdkCreateSnapshot(virDomainObjPtr dom, const char *description)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job;

    job = PrlVm_CreateSnapshot(privdom->sdkdom, "",
                               description ? : "");
4819
    if (PRL_FAILED(waitDomainJob(job, dom)))
4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830
        return -1;

    return 0;
}

int prlsdkDeleteSnapshot(virDomainObjPtr dom, const char *uuid, bool children)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job;

    job = PrlVm_DeleteSnapshot(privdom->sdkdom, uuid, children);
4831
    if (PRL_FAILED(waitDomainJob(job, dom)))
4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
        return -1;

    return 0;
}

int prlsdkSwitchToSnapshot(virDomainObjPtr dom, const char *uuid, bool paused)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job;
    PRL_UINT32 flags = 0;

    if (paused)
        flags |= PSSF_SKIP_RESUME;

    job = PrlVm_SwitchToSnapshotEx(privdom->sdkdom, uuid, flags);
4847
    if (PRL_FAILED(waitDomainJob(job, dom)))
4848 4849 4850 4851
        return -1;

    return 0;
}
4852 4853 4854 4855 4856 4857 4858 4859

/* high security is default choice for 2 reasons:
 * 1. as this is the highest set security we can't get
 * reject from server with high security settings
 * 2. this is on par with security level of driver
 * connection to dispatcher
 */

4860
#define PRLSDK_MIGRATION_FLAGS (PSL_HIGH_SECURITY | PVMT_DONT_CREATE_DISK)
4861 4862 4863 4864 4865 4866 4867 4868 4869

int prlsdkMigrate(virDomainObjPtr dom, virURIPtr uri,
                  const unsigned char *session_uuid,
                  const char *dname,
                  unsigned int flags)
{
    int ret = -1;
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
4870
    char uuidstr[VIR_UUID_STRING_BRACED_BUFLEN];
4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885
    PRL_UINT32 vzflags = PRLSDK_MIGRATION_FLAGS;

    if (flags & VIR_MIGRATE_PAUSED)
        vzflags |= PVMT_DONT_RESUME_VM;

    prlsdkUUIDFormat(session_uuid, uuidstr);
    job = PrlVm_MigrateWithRenameEx(privdom->sdkdom, uri->server,
                                    uri->port, uuidstr,
                                    dname == NULL ? "" : dname,
                                    "",
                                    vzflags,
                                    0,
                                    PRL_TRUE
                                    );

4886
    if (PRL_FAILED(waitDomainJob(job, dom)))
4887 4888 4889 4890 4891 4892 4893
        goto cleanup;

    ret = 0;

 cleanup:
    return ret;
}
4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916

int prlsdkSetCpuCount(virDomainObjPtr dom, unsigned int count)
{
    vzDomObjPtr privdom = dom->privateData;
    PRL_HANDLE job;
    PRL_RESULT pret;

    job = PrlVm_BeginEdit(privdom->sdkdom);
    if (PRL_FAILED(waitDomainJob(job, dom)))
        goto error;

    pret = PrlVmCfg_SetCpuCount(privdom->sdkdom, count);
    prlsdkCheckRetGoto(pret, error);

    job = PrlVm_CommitEx(privdom->sdkdom, 0);
    if (PRL_FAILED(waitDomainJob(job, dom)))
        goto error;

    return 0;

 error:
    return -1;
}
4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943

int prlsdkResizeImage(virDomainObjPtr dom, virDomainDiskDefPtr disk,
                      unsigned long long newsize)
{
    int ret = -1;
    PRL_RESULT pret;
    vzDomObjPtr privdom = dom->privateData;
    PRL_UINT32 emulatedType;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE prldisk = PRL_INVALID_HANDLE;

    prldisk = prlsdkGetDisk(privdom->sdkdom, disk);
    if (prldisk == PRL_INVALID_HANDLE)
        goto cleanup;

    pret = PrlVmDev_GetEmulatedType(prldisk, &emulatedType);
    prlsdkCheckRetGoto(pret, cleanup);

    if (emulatedType != PDT_USE_IMAGE_FILE &&
        emulatedType != PDT_USE_FILE_SYSTEM) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Only disk image supported for resize"));
        goto cleanup;
    }

    job = PrlVmDev_ResizeImage(prldisk, newsize,
                               PRIF_RESIZE_LAST_PARTITION);
4944
    if (PRL_FAILED(waitDomainJob(job, dom)))
4945 4946 4947 4948 4949 4950 4951 4952 4953
        goto cleanup;

    ret = 0;

 cleanup:

    PrlHandle_Free(prldisk);
    return ret;
}