openvz_driver.c 71.8 KB
Newer Older
1 2 3
/*
 * openvz_driver.c: core driver methods for managing OpenVZ VEs
 *
4
 * Copyright (C) 2010-2016 Red Hat, Inc.
5 6
 * Copyright (C) 2006, 2007 Binary Karma
 * Copyright (C) 2006 Shuveb Hussain
7
 * Copyright (C) 2007 Anoop Joe Cyriac
8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * License along with this library.  If not, see
O
Osier Yang 已提交
21
 * <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28 29 30 31 32 33 34
 */

#include <config.h>

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

35
#include "virerror.h"
36
#include "datatypes.h"
37
#include "openvz_driver.h"
38
#include "openvz_util.h"
39
#include "virbuffer.h"
40
#include "openvz_conf.h"
41
#include "virhostcpu.h"
42
#include "virhostmem.h"
43
#include "viralloc.h"
E
Eric Blake 已提交
44
#include "virfile.h"
45
#include "virtypedparam.h"
46
#include "virlog.h"
47
#include "vircommand.h"
M
Martin Kletzander 已提交
48
#include "viruri.h"
49
#include "virnetdevtap.h"
50
#include "virstring.h"
51

52 53
#define VIR_FROM_THIS VIR_FROM_OPENVZ

54 55
VIR_LOG_INIT("openvz.openvz_driver");

56 57
#define OPENVZ_NB_MEM_PARAM 3

D
Daniel Veillard 已提交
58
static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid);
59
static int openvzConnectGetMaxVcpus(virConnectPtr conn, const char *type);
60
static int openvzDomainGetMaxVcpus(virDomainPtr dom);
61
static int openvzDomainSetVcpusInternal(virDomainObjPtr vm,
62 63
                                        unsigned int nvcpus,
                                        virDomainXMLOptionPtr xmlopt);
64
static int openvzDomainSetMemoryInternal(virDomainObjPtr vm,
65
                                         unsigned long long memory);
66
static int openvzGetVEStatus(virDomainObjPtr vm, int *status, int *reason);
D
Daniel Veillard 已提交
67

68 69
static void openvzDriverLock(struct openvz_driver *driver)
{
70
    virMutexLock(&driver->lock);
71 72 73 74
}

static void openvzDriverUnlock(struct openvz_driver *driver)
{
75
    virMutexUnlock(&driver->lock);
76 77
}

78 79
struct openvz_driver ovz_driver;

80 81 82 83 84 85 86 87

static virDomainObjPtr
openvzDomObjFromDomainLocked(struct openvz_driver *driver,
                             const unsigned char *uuid)
{
    virDomainObjPtr vm;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

88
    if (!(vm = virDomainObjListFindByUUID(driver->domains, uuid))) {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        virUUIDFormat(uuid, uuidstr);

        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
        return NULL;
    }

    return vm;
}


static virDomainObjPtr
openvzDomObjFromDomain(struct openvz_driver *driver,
                       const unsigned char *uuid)
{
    virDomainObjPtr vm;

    openvzDriverLock(driver);
    vm = openvzDomObjFromDomainLocked(driver, uuid);
    openvzDriverUnlock(driver);
    return vm;
}


113 114 115
static int
openvzDomainDefPostParse(virDomainDefPtr def,
                         virCapsPtr caps ATTRIBUTE_UNUSED,
116
                         unsigned int parseFlags ATTRIBUTE_UNUSED,
117 118
                         void *opaque ATTRIBUTE_UNUSED,
                         void *parseOpaque ATTRIBUTE_UNUSED)
119 120
{
    /* fill the init path */
121
    if (def->os.type == VIR_DOMAIN_OSTYPE_EXE && !def->os.init) {
122 123 124 125
        if (VIR_STRDUP(def->os.init, "/sbin/init") < 0)
            return -1;
    }

126 127 128 129
    return 0;
}


130 131
static int
openvzDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
132
                               const virDomainDef *def ATTRIBUTE_UNUSED,
133
                               virCapsPtr caps ATTRIBUTE_UNUSED,
134
                               unsigned int parseFlags ATTRIBUTE_UNUSED,
135 136
                               void *opaque ATTRIBUTE_UNUSED,
                               void *parseOpaque ATTRIBUTE_UNUSED)
137 138 139 140 141 142
{
    if (dev->type == VIR_DOMAIN_DEVICE_CHR &&
        dev->data.chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
        dev->data.chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE)
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_OPENVZ;

143 144 145 146 147 148 149 150 151 152
    /* forbid capabilities mode hostdev in this kind of hypervisor */
    if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV &&
        dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev mode 'capabilities' is not "
                         "supported in %s"),
                       virDomainVirtTypeToString(def->virtType));
        return -1;
    }

153 154 155 156
    return 0;
}


157
virDomainDefParserConfig openvzDomainDefParserConfig = {
158 159 160
    .domainPostParseCallback = openvzDomainDefPostParse,
    .devicesPostParseCallback = openvzDomainDeviceDefPostParse,
    .features = VIR_DOMAIN_DEF_FEATURE_NAME_SLASH,
161 162 163
};


164 165 166
/* generate arguments to create OpenVZ container
   return -1 - error
           0 - OK
167
   Caller has to free the cmd
168
*/
169 170
static virCommandPtr
openvzDomainDefineCmd(virDomainDefPtr vmdef)
171
{
172 173 174 175
    virCommandPtr cmd = virCommandNewArgList(VZCTL,
                                             "--quiet",
                                             "create",
                                             NULL);
176

177
    if (vmdef == NULL) {
178 179
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Container is not defined"));
180 181
        virCommandFree(cmd);
        return NULL;
182
    }
183

184
    virCommandAddArgList(cmd, vmdef->name, "--name", vmdef->name, NULL);
185

186
    if (vmdef->nfss == 1 &&
187
        vmdef->fss[0]->type == VIR_DOMAIN_FS_TYPE_TEMPLATE) {
188
        virCommandAddArgList(cmd, "--ostemplate", vmdef->fss[0]->src, NULL);
189
    }
190

191
    return cmd;
192 193 194
}


195
static int openvzSetInitialConfig(virDomainDefPtr vmdef)
196 197 198 199
{
    int ret = -1;
    int vpsid;
    char * confdir = NULL;
200
    virCommandPtr cmd = NULL;
201 202

    if (vmdef->nfss > 1) {
203 204
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("only one filesystem supported"));
205 206 207 208 209 210 211
        goto cleanup;
    }

    if (vmdef->nfss == 1 &&
        vmdef->fss[0]->type != VIR_DOMAIN_FS_TYPE_TEMPLATE &&
        vmdef->fss[0]->type != VIR_DOMAIN_FS_TYPE_MOUNT)
    {
212 213
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("filesystem is not of type 'template' or 'mount'"));
214 215 216 217 218 219 220 221
        goto cleanup;
    }


    if (vmdef->nfss == 1 &&
        vmdef->fss[0]->type == VIR_DOMAIN_FS_TYPE_MOUNT)
    {

E
Eric Blake 已提交
222
        if (virStrToLong_i(vmdef->name, NULL, 10, &vpsid) < 0) {
223 224
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not convert domain name to VEID"));
225 226 227 228
            goto cleanup;
        }

        if (openvzCopyDefaultConfig(vpsid) < 0) {
229 230
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not copy default config"));
231 232 233
            goto cleanup;
        }

234
        if (openvzWriteVPSConfigParam(vpsid, "VE_PRIVATE", vmdef->fss[0]->src->path) < 0) {
235 236
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not set the source dir for the filesystem"));
237 238
            goto cleanup;
        }
239 240 241
    } else {
        cmd = openvzDomainDefineCmd(vmdef);
        if (virCommandRun(cmd, NULL) < 0)
242 243 244 245 246
            goto cleanup;
    }

    ret = 0;

247
 cleanup:
J
John Ferlan 已提交
248 249
    VIR_FREE(confdir);
    virCommandFree(cmd);
250

J
John Ferlan 已提交
251
    return ret;
252 253 254
}


255 256
static int
openvzSetDiskQuota(virDomainDefPtr vmdef,
257 258
                   virDomainFSDefPtr fss,
                   bool persist)
259 260 261 262 263 264 265 266
{
    int ret = -1;
    unsigned long long sl, hl;
    virCommandPtr cmd = virCommandNewArgList(VZCTL,
                                             "--quiet",
                                             "set",
                                             vmdef->name,
                                             NULL);
267 268
    if (persist)
        virCommandAddArg(cmd, "--save");
269 270 271 272 273 274 275 276 277 278 279 280 281

    if (fss->type == VIR_DOMAIN_FS_TYPE_TEMPLATE) {
        if (fss->space_hard_limit) {
            hl = VIR_DIV_UP(fss->space_hard_limit, 1024);
            virCommandAddArg(cmd, "--diskspace");

            if (fss->space_soft_limit) {
                sl = VIR_DIV_UP(fss->space_soft_limit, 1024);
                virCommandAddArgFormat(cmd, "%lld:%lld", sl, hl);
            } else {
                virCommandAddArgFormat(cmd, "%lld", hl);
            }
        } else if (fss->space_soft_limit) {
282 283
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("Can't set soft limit without hard limit"));
284 285 286 287 288 289 290 291
            goto cleanup;
        }

        if (virCommandRun(cmd, NULL) < 0)
            goto cleanup;
    }

    ret = 0;
292
 cleanup:
J
John Ferlan 已提交
293
    virCommandFree(cmd);
294

J
John Ferlan 已提交
295
    return ret;
296 297 298
}


299 300 301 302 303 304 305 306
static char *
openvzDomainGetHostname(virDomainPtr dom, unsigned int flags)
{
    char *hostname = NULL;
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;

    virCheckFlags(0, NULL);
307 308
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return NULL;
309 310 311

    hostname = openvzVEGetStringParam(dom, "hostname");
    if (hostname == NULL)
312
        goto cleanup;
313 314 315 316 317

    /* vzlist prints an unset hostname as '-' */
    if (STREQ(hostname, "-")) {
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Hostname of '%s' is unset"), vm->def->name);
318
        VIR_FREE(hostname);
319 320
    }

321
 cleanup:
322
    virDomainObjEndAPI(&vm);
323 324 325 326
    return hostname;
}


327
static virDomainPtr openvzDomainLookupByID(virConnectPtr conn,
328 329
                                           int id)
{
330
    struct openvz_driver *driver = conn->privateData;
331
    virDomainObjPtr vm;
332
    virDomainPtr dom = NULL;
333

334
    openvzDriverLock(driver);
335
    vm = virDomainObjListFindByID(driver->domains, id);
336 337
    openvzDriverUnlock(driver);

338
    if (!vm) {
339 340
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching id '%d'"), id);
341
        goto cleanup;
342 343
    }

344
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid, vm->def->id);
345

346
 cleanup:
347
    virDomainObjEndAPI(&vm);
348 349 350
    return dom;
}

351 352
static int openvzConnectGetVersion(virConnectPtr conn, unsigned long *version)
{
353
    struct  openvz_driver *driver = conn->privateData;
354
    openvzDriverLock(driver);
355
    *version = driver->version;
356
    openvzDriverUnlock(driver);
357 358 359
    return 0;
}

360 361 362 363 364 365 366

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


367
static char *openvzDomainGetOSType(virDomainPtr dom)
368
{
369 370 371
    struct  openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;
372

373 374
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return NULL;
375

376
    ignore_value(VIR_STRDUP(ret, virDomainOSTypeToString(vm->def->os.type)));
377

378
    virDomainObjEndAPI(&vm);
379
    return ret;
380 381 382 383
}


static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn,
384 385
                                             const unsigned char *uuid)
{
386 387 388
    struct  openvz_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
389

390 391
    if (!(vm = openvzDomObjFromDomain(driver, uuid)))
        return NULL;
392

393
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid, vm->def->id);
394

395
    virDomainObjEndAPI(&vm);
396 397 398 399
    return dom;
}

static virDomainPtr openvzDomainLookupByName(virConnectPtr conn,
400 401
                                             const char *name)
{
402 403 404
    struct openvz_driver *driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
405

406
    openvzDriverLock(driver);
407
    vm = virDomainObjListFindByName(driver->domains, name);
408 409
    openvzDriverUnlock(driver);

410
    if (!vm) {
411 412
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
413
        goto cleanup;
414 415
    }

416
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid, vm->def->id);
417

418
 cleanup:
419
    virDomainObjEndAPI(&vm);
420 421 422 423
    return dom;
}

static int openvzDomainGetInfo(virDomainPtr dom,
424 425
                               virDomainInfoPtr info)
{
426 427
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
428
    int state;
429
    int ret = -1;
430

431 432
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
433

434 435 436
    if (openvzGetVEStatus(vm, &state, NULL) == -1)
        goto cleanup;
    info->state = state;
437

438
    if (info->state != VIR_DOMAIN_RUNNING) {
D
Daniel Veillard 已提交
439 440 441
        info->cpuTime = 0;
    } else {
        if (openvzGetProcessInfo(&(info->cpuTime), dom->id) < 0) {
442 443
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("cannot read cputime for domain %d"), dom->id);
444
            goto cleanup;
D
Daniel Veillard 已提交
445 446 447
        }
    }

448
    info->maxMem = virDomainDefGetMemoryTotal(vm->def);
449
    info->memory = vm->def->mem.cur_balloon;
450
    info->nrVirtCpu = virDomainDefGetVcpus(vm->def);
451 452
    ret = 0;

453
 cleanup:
454
    virDomainObjEndAPI(&vm);
455
    return ret;
456 457 458
}


459 460 461 462 463 464 465 466 467 468 469 470
static int
openvzDomainGetState(virDomainPtr dom,
                     int *state,
                     int *reason,
                     unsigned int flags)
{
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

471 472
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
473

474
    ret = openvzGetVEStatus(vm, state, reason);
475

476
    virDomainObjEndAPI(&vm);
477 478 479 480
    return ret;
}


481 482 483 484 485 486
static int openvzDomainIsActive(virDomainPtr dom)
{
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

487 488 489
    if (!(obj = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;

490 491
    ret = virDomainObjIsActive(obj);

492
    virDomainObjEndAPI(&obj);
493 494 495 496 497 498 499 500 501 502
    return ret;
}


static int openvzDomainIsPersistent(virDomainPtr dom)
{
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

503 504 505
    if (!(obj = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;

506 507
    ret = obj->persistent;

508
    virDomainObjEndAPI(&obj);
509 510 511
    return ret;
}

512 513 514 515
static int openvzDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}
516

517
static char *openvzDomainGetXMLDesc(virDomainPtr dom, unsigned int flags) {
518 519 520
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;
521

522
    virCheckFlags(VIR_DOMAIN_XML_COMMON_FLAGS, NULL);
523

524 525
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return NULL;
526

527
    ret = virDomainDefFormat(vm->def, driver->caps,
528
                             virDomainDefFormatConvertXMLFlags(flags));
529

530
    virDomainObjEndAPI(&vm);
531
    return ret;
532
}
533

534

535 536 537 538 539 540
/*
 * Convenient helper to target a command line argv
 * and fill in an empty slot with the supplied
 * key value. This lets us declare the argv on the
 * stack and just splice in the domain name after
 */
E
Eric Blake 已提交
541
#define PROGRAM_SENTINEL ((char *)0x1)
542 543 544 545
static void openvzSetProgramSentinal(const char **prog, const char *key)
{
    const char **tmp = prog;
    while (tmp && *tmp) {
E
Eric Blake 已提交
546
        if (*tmp == PROGRAM_SENTINEL) {
547 548 549 550 551 552
            *tmp = key;
            break;
        }
        tmp++;
    }
}
553

554 555
static int openvzDomainSuspend(virDomainPtr dom)
{
556 557
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
E
Eric Blake 已提交
558
    const char *prog[] = {VZCTL, "--quiet", "chkpnt", PROGRAM_SENTINEL, "--suspend", NULL};
559 560
    int ret = -1;

561 562
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
563

564
    if (virDomainObjCheckActive(vm) < 0)
565 566
        goto cleanup;

J
Jiri Denemark 已提交
567
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_PAUSED) {
568
        openvzSetProgramSentinal(prog, vm->def->name);
569
        if (virRun(prog, NULL) < 0)
570
            goto cleanup;
J
Jiri Denemark 已提交
571
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
572 573 574 575
    }

    ret = 0;

576
 cleanup:
577
    virDomainObjEndAPI(&vm);
578 579 580
    return ret;
}

581 582
static int openvzDomainResume(virDomainPtr dom)
{
J
John Ferlan 已提交
583 584 585 586 587
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    const char *prog[] = {VZCTL, "--quiet", "chkpnt", PROGRAM_SENTINEL, "--resume", NULL};
    int ret = -1;

588 589
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
J
John Ferlan 已提交
590

591
    if (virDomainObjCheckActive(vm) < 0)
J
John Ferlan 已提交
592 593 594 595 596 597 598 599 600 601
        goto cleanup;

    if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
        openvzSetProgramSentinal(prog, vm->def->name);
        if (virRun(prog, NULL) < 0)
            goto cleanup;
        virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_UNPAUSED);
    }

    ret = 0;
602

603
 cleanup:
604
    virDomainObjEndAPI(&vm);
J
John Ferlan 已提交
605
    return ret;
606 607
}

608 609
static int
openvzDomainShutdownFlags(virDomainPtr dom,
610 611
                          unsigned int flags)
{
612 613
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
E
Eric Blake 已提交
614
    const char *prog[] = {VZCTL, "--quiet", "stop", PROGRAM_SENTINEL, NULL};
615
    int ret = -1;
616
    int status;
617

618 619
    virCheckFlags(0, -1);

620 621
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
622

623 624 625
    if (openvzGetVEStatus(vm, &status, NULL) == -1)
        goto cleanup;

626
    openvzSetProgramSentinal(prog, vm->def->name);
627
    if (status != VIR_DOMAIN_RUNNING) {
628 629
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not in running state"));
630
        goto cleanup;
631
    }
632

633
    if (virRun(prog, NULL) < 0)
634
        goto cleanup;
635

636
    vm->def->id = -1;
J
Jiri Denemark 已提交
637
    virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
638
    dom->id = -1;
639
    ret = 0;
640

641
 cleanup:
642
    virDomainObjEndAPI(&vm);
643
    return ret;
644 645
}

646 647 648 649 650 651
static int
openvzDomainShutdown(virDomainPtr dom)
{
    return openvzDomainShutdownFlags(dom, 0);
}

652 653 654 655 656 657 658 659 660 661 662 663
static int
openvzDomainDestroy(virDomainPtr dom)
{
    return openvzDomainShutdownFlags(dom, 0);
}

static int
openvzDomainDestroyFlags(virDomainPtr dom, unsigned int flags)
{
    return openvzDomainShutdownFlags(dom, flags);
}

664
static int openvzDomainReboot(virDomainPtr dom,
E
Eric Blake 已提交
665 666
                              unsigned int flags)
{
667 668
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
E
Eric Blake 已提交
669
    const char *prog[] = {VZCTL, "--quiet", "restart", PROGRAM_SENTINEL, NULL};
670
    int ret = -1;
671
    int status;
672

E
Eric Blake 已提交
673 674
    virCheckFlags(0, -1);

675 676
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
677

678 679 680
    if (openvzGetVEStatus(vm, &status, NULL) == -1)
        goto cleanup;

681
    openvzSetProgramSentinal(prog, vm->def->name);
682
    if (status != VIR_DOMAIN_RUNNING) {
683 684
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not in running state"));
685
        goto cleanup;
686
    }
687

688
    if (virRun(prog, NULL) < 0)
689 690
        goto cleanup;
    ret = 0;
691

J
Jiri Denemark 已提交
692 693
    virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_BOOTED);

694
 cleanup:
695
    virDomainObjEndAPI(&vm);
696
    return ret;
697 698
}

699 700 701 702
static char *
openvzGenerateVethName(int veid, char *dev_name_ve)
{
    int     ifNo = 0;
703
    char    *ret;
704 705 706

    if (sscanf(dev_name_ve, "%*[^0-9]%d", &ifNo) != 1)
        return NULL;
707
    ignore_value(virAsprintf(&ret, "veth%d.%d.", veid, ifNo));
708
    return ret;
709 710 711 712 713
}

static char *
openvzGenerateContainerVethName(int veid)
{
714 715
    char *temp = NULL;
    char *name = NULL;
716 717

    /* try to get line "^NETIF=..." from config */
718
    if (openvzReadVPSConfigParam(veid, "NETIF", &temp) <= 0) {
719
        ignore_value(VIR_STRDUP(name, "eth0"));
720
    } else {
721
        char *saveptr = NULL;
722 723
        char *s;
        int max = 0;
724 725

        /* get maximum interface number (actually, it is the last one) */
726
        for (s = strtok_r(temp, ";", &saveptr); s; s = strtok_r(NULL, ";", &saveptr)) {
727 728 729 730 731 732 733
            int x;

            if (sscanf(s, "ifname=eth%d", &x) != 1) return NULL;
            if (x > max) max = x;
        }

        /* set new name */
734
        ignore_value(virAsprintf(&name, "eth%d", max + 1));
735
    }
736 737 738 739

    VIR_FREE(temp);

    return name;
740 741
}

D
Daniel Veillard 已提交
742 743
static int
openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
744 745
                       virDomainNetDefPtr net,
                       virBufferPtr configBuf)
D
Daniel Veillard 已提交
746
{
747
    int rc = -1;
748
    char macaddr[VIR_MAC_STRING_BUFLEN];
749
    virMacAddr host_mac;
750
    char host_macaddr[VIR_MAC_STRING_BUFLEN];
751
    struct openvz_driver *driver =  conn->privateData;
752
    virCommandPtr cmd = NULL;
753
    char *guest_ifname = NULL;
D
Daniel Veillard 已提交
754 755

    if (net == NULL)
J
John Ferlan 已提交
756 757
        return 0;

D
Daniel Veillard 已提交
758
    if (vpsid == NULL) {
759 760
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Container ID is not specified"));
D
Daniel Veillard 已提交
761 762 763
        return -1;
    }

764 765 766
    if (net->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
        net->type != VIR_DOMAIN_NET_TYPE_ETHERNET)
        return 0;
D
Daniel Veillard 已提交
767

768
    cmd = virCommandNewArgList(VZCTL, "--quiet", "set", vpsid, NULL);
D
Daniel Veillard 已提交
769

770
    virMacAddrFormat(&net->mac, macaddr);
771
    virDomainNetGenerateMAC(driver->xmlopt, &host_mac);
772
    virMacAddrFormat(&host_mac, host_macaddr);
773

774 775
    if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE ||
        (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
776
         net->guestIP.nips == 0)) {
777
        virBuffer buf = VIR_BUFFER_INITIALIZER;
778
        int veid = openvzGetVEID(vpsid);
D
Daniel Veillard 已提交
779

780 781
        /* if net is ethernet and the user has specified guest interface name,
         * let's use it; otherwise generate a new one */
782 783
        if (net->ifname_guest) {
            if (VIR_STRDUP(guest_ifname, net->ifname_guest) < 0)
784 785 786 787
                goto cleanup;
        } else {
            guest_ifname = openvzGenerateContainerVethName(veid);
            if (guest_ifname == NULL) {
J
John Ferlan 已提交
788 789 790
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Could not generate eth name for container"));
                goto cleanup;
791
            }
792 793 794 795 796
        }

        /* if user doesn't specified host interface name,
         * than we need to generate it */
        if (net->ifname == NULL) {
797
            net->ifname = openvzGenerateVethName(veid, guest_ifname);
798
            if (net->ifname == NULL) {
J
John Ferlan 已提交
799 800 801
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Could not generate veth name"));
                goto cleanup;
802 803 804
            }
        }

805
        virBufferAdd(&buf, guest_ifname, -1); /* Guest dev */
806 807 808
        virBufferAsprintf(&buf, ",%s", macaddr); /* Guest dev mac */
        virBufferAsprintf(&buf, ",%s", net->ifname); /* Host dev */
        virBufferAsprintf(&buf, ",%s", host_macaddr); /* Host dev mac */
809

810 811
        if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
            if (driver->version >= VZCTL_BRIDGE_MIN_VERSION) {
812
                virBufferAsprintf(&buf, ",%s", net->data.bridge.brname); /* Host bridge */
813
            } else {
814
                virBufferAsprintf(configBuf, "ifname=%s", guest_ifname);
815 816 817 818
                virBufferAsprintf(configBuf, ",mac=%s", macaddr); /* Guest dev mac */
                virBufferAsprintf(configBuf, ",host_ifname=%s", net->ifname); /* Host dev */
                virBufferAsprintf(configBuf, ",host_mac=%s", host_macaddr); /* Host dev mac */
                virBufferAsprintf(configBuf, ",bridge=%s", net->data.bridge.brname); /* Host bridge */
819
            }
D
Daniel Veillard 已提交
820
        }
821

822 823 824
        /* --netif_add ifname[,mac,host_ifname,host_mac] */
        virCommandAddArg(cmd, "--netif_add");
        virCommandAddArgBuffer(cmd, &buf);
825
    } else if (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
J
John Ferlan 已提交
826
               net->guestIP.nips > 0) {
827 828
        size_t i;

829
        /* --ipadd ip */
830 831
        for (i = 0; i < net->guestIP.nips; i++) {
            char *ipStr = virSocketAddrFormat(&net->guestIP.ips[i]->address);
832 833
            if (!ipStr)
                goto cleanup;
834
            virCommandAddArgList(cmd, "--ipadd", ipStr, NULL);
835
            VIR_FREE(ipStr);
836
        }
D
Daniel Veillard 已提交
837 838
    }

839
    /* TODO: processing NAT and physical device */
D
Daniel Veillard 已提交
840

841 842
    virCommandAddArg(cmd, "--save");
    rc = virCommandRun(cmd, NULL);
D
Daniel Veillard 已提交
843

844 845
 cleanup:
    virCommandFree(cmd);
846
    VIR_FREE(guest_ifname);
D
Daniel Veillard 已提交
847 848 849
    return rc;
}

850 851 852 853 854

static int
openvzDomainSetNetworkConfig(virConnectPtr conn,
                             virDomainDefPtr def)
{
855
    size_t i;
856 857 858
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *param;
    int first = 1;
859
    struct openvz_driver *driver =  conn->privateData;
860

861
    for (i = 0; i < def->nnets; i++) {
862 863 864 865 866 867 868 869 870
        if (driver->version < VZCTL_BRIDGE_MIN_VERSION &&
            def->nets[i]->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
            if (first)
                first = 0;
            else
                virBufferAddLit(&buf, ";");
        }

        if (openvzDomainSetNetwork(conn, def->name, def->nets[i], &buf) < 0) {
871 872
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not configure network"));
873 874 875 876 877 878 879
            goto exit;
        }
    }

    if (driver->version < VZCTL_BRIDGE_MIN_VERSION && def->nnets) {
        param = virBufferContentAndReset(&buf);
        if (param) {
880
            if (openvzWriteVPSConfigParam(strtoI(def->name), "NETIF", param) < 0) {
881
                VIR_FREE(param);
882 883
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("cannot replace NETIF config"));
884 885 886 887 888 889 890 891
                return -1;
            }
            VIR_FREE(param);
        }
    }

    return 0;

892
 exit:
893
    virBufferFreeAndReset(&buf);
894 895 896 897
    return -1;
}


898
static virDomainPtr
899
openvzDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
900
{
901
    struct openvz_driver *driver =  conn->privateData;
902 903
    virDomainDefPtr vmdef = NULL;
    virDomainObjPtr vm = NULL;
904
    virDomainPtr dom = NULL;
905
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
906

907 908 909
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
910
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
911

912
    openvzDriverLock(driver);
913
    if ((vmdef = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
914
                                         NULL, parse_flags)) == NULL)
915
        goto cleanup;
916

917 918 919
    if (virXMLCheckIllegalChars("name", vmdef->name, "\n") < 0)
        goto cleanup;

920
    if (!(vm = virDomainObjListAdd(driver->domains, vmdef,
921
                                   driver->xmlopt,
922
                                   0, NULL)))
923 924
        goto cleanup;
    vmdef = NULL;
925
    vm->persistent = 1;
926

927
    if (openvzSetInitialConfig(vm->def) < 0) {
928
        VIR_ERROR(_("Error creating initial configuration"));
929
        goto cleanup;
930 931
    }

932
    if (vm->def->nfss == 1) {
933
        if (openvzSetDiskQuota(vm->def, vm->def->fss[0], true) < 0) {
934 935
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not set disk quota"));
936 937 938
            goto cleanup;
        }
    }
D
Daniel Veillard 已提交
939

940
    if (openvzSetDefinedUUID(strtoI(vm->def->name), vm->def->uuid) < 0) {
941 942
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not set UUID"));
943
        goto cleanup;
944 945
    }

946 947
    if (openvzDomainSetNetworkConfig(conn, vm->def) < 0)
        goto cleanup;
D
Daniel Veillard 已提交
948

949
    if (virDomainDefHasVcpusOffline(vm->def)) {
950 951
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("current vcpu count must equal maximum"));
E
Eric Blake 已提交
952 953
        goto cleanup;
    }
954
    if (virDomainDefGetVcpusMax(vm->def)) {
955 956
        if (openvzDomainSetVcpusInternal(vm, virDomainDefGetVcpusMax(vm->def),
                                         driver->xmlopt) < 0) {
957
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
958
                           _("Could not set number of vCPUs"));
J
John Ferlan 已提交
959
            goto cleanup;
960 961 962
        }
    }

963 964
    if (vm->def->mem.cur_balloon > 0) {
        if (openvzDomainSetMemoryInternal(vm, vm->def->mem.cur_balloon) < 0) {
965 966
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not set memory size"));
J
John Ferlan 已提交
967
            goto cleanup;
968 969 970
        }
    }

971
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid, -1);
972

973
 cleanup:
974
    virDomainDefFree(vmdef);
975
    virDomainObjEndAPI(&vm);
976
    openvzDriverUnlock(driver);
977 978 979
    return dom;
}

980 981 982 983 984 985
static virDomainPtr
openvzDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return openvzDomainDefineXMLFlags(conn, xml, 0);
}

986
static virDomainPtr
987
openvzDomainCreateXML(virConnectPtr conn, const char *xml,
988
                      unsigned int flags)
989
{
990
    struct openvz_driver *driver =  conn->privateData;
991 992
    virDomainDefPtr vmdef = NULL;
    virDomainObjPtr vm = NULL;
993
    virDomainPtr dom = NULL;
E
Eric Blake 已提交
994
    const char *progstart[] = {VZCTL, "--quiet", "start", PROGRAM_SENTINEL, NULL};
995
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
996

997 998 999
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
1000
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1001

1002
    openvzDriverLock(driver);
1003
    if ((vmdef = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
1004
                                         NULL, parse_flags)) == NULL)
1005
        goto cleanup;
1006

1007
    if (!(vm = virDomainObjListAdd(driver->domains,
1008
                                   vmdef,
1009
                                   driver->xmlopt,
1010
                                   VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
1011 1012
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
1013 1014
        goto cleanup;
    vmdef = NULL;
1015 1016 1017
    /* All OpenVZ domains seem to be persistent - this is a bit of a violation
     * of this libvirt API which is intended for transient domain creation */
    vm->persistent = 1;
1018

1019
    if (openvzSetInitialConfig(vm->def) < 0) {
1020
        VIR_ERROR(_("Error creating initial configuration"));
1021
        goto cleanup;
1022
    }
1023

1024
    if (vm->def->nfss == 1) {
1025
        if (openvzSetDiskQuota(vm->def, vm->def->fss[0], true) < 0) {
1026 1027
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not set disk quota"));
1028 1029 1030 1031
            goto cleanup;
        }
    }

1032
    if (openvzSetDefinedUUID(strtoI(vm->def->name), vm->def->uuid) < 0) {
1033 1034
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not set UUID"));
1035
        goto cleanup;
1036 1037
    }

1038 1039
    if (openvzDomainSetNetworkConfig(conn, vm->def) < 0)
        goto cleanup;
D
Daniel Veillard 已提交
1040

1041
    openvzSetProgramSentinal(progstart, vm->def->name);
1042

1043
    if (virRun(progstart, NULL) < 0)
1044
        goto cleanup;
1045

1046
    vm->pid = strtoI(vm->def->name);
1047
    vm->def->id = vm->pid;
J
Jiri Denemark 已提交
1048
    virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_BOOTED);
1049

1050
    if (virDomainDefGetVcpusMax(vm->def) > 0) {
1051 1052
        if (openvzDomainSetVcpusInternal(vm, virDomainDefGetVcpusMax(vm->def),
                                         driver->xmlopt) < 0) {
1053
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1054
                           _("Could not set number of vCPUs"));
1055
            goto cleanup;
1056 1057 1058
        }
    }

1059
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid, vm->def->id);
1060

1061
 cleanup:
1062
    virDomainDefFree(vmdef);
1063
    virDomainObjEndAPI(&vm);
1064
    openvzDriverUnlock(driver);
1065 1066 1067 1068
    return dom;
}

static int
1069
openvzDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
1070
{
1071 1072
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
E
Eric Blake 已提交
1073
    const char *prog[] = {VZCTL, "--quiet", "start", PROGRAM_SENTINEL, NULL };
1074
    int ret = -1;
1075
    int status;
1076

1077 1078
    virCheckFlags(0, -1);

1079
    openvzDriverLock(driver);
1080
    vm = virDomainObjListFindByName(driver->domains, dom->name);
1081 1082
    openvzDriverUnlock(driver);

1083
    if (!vm) {
1084 1085
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), dom->name);
1086
        goto cleanup;
1087
    }
1088

1089 1090 1091 1092
    if (openvzGetVEStatus(vm, &status, NULL) == -1)
        goto cleanup;

    if (status != VIR_DOMAIN_SHUTOFF) {
1093 1094
        virReportError(VIR_ERR_OPERATION_DENIED, "%s",
                       _("domain is not in shutoff state"));
1095
        goto cleanup;
1096 1097
    }

1098
    openvzSetProgramSentinal(prog, vm->def->name);
1099
    if (virRun(prog, NULL) < 0)
1100
        goto cleanup;
1101

1102 1103
    vm->pid = strtoI(vm->def->name);
    vm->def->id = vm->pid;
1104
    dom->id = vm->pid;
J
Jiri Denemark 已提交
1105
    virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_BOOTED);
1106
    ret = 0;
1107

1108
 cleanup:
1109
    virDomainObjEndAPI(&vm);
1110
    return ret;
1111 1112
}

1113 1114 1115 1116 1117 1118
static int
openvzDomainCreate(virDomainPtr dom)
{
    return openvzDomainCreateWithFlags(dom, 0);
}

1119
static int
1120 1121
openvzDomainUndefineFlags(virDomainPtr dom,
                          unsigned int flags)
1122
{
1123 1124
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
E
Eric Blake 已提交
1125
    const char *prog[] = { VZCTL, "--quiet", "destroy", PROGRAM_SENTINEL, NULL };
1126
    int ret = -1;
1127
    int status;
1128

1129 1130
    virCheckFlags(0, -1);

1131
    openvzDriverLock(driver);
1132
    if (!(vm = openvzDomObjFromDomainLocked(driver, dom->uuid)))
1133
        goto cleanup;
1134

1135 1136 1137
    if (openvzGetVEStatus(vm, &status, NULL) == -1)
        goto cleanup;

1138
    openvzSetProgramSentinal(prog, vm->def->name);
1139
    if (virRun(prog, NULL) < 0)
1140
        goto cleanup;
1141

1142
    if (virDomainObjIsActive(vm))
1143
        vm->persistent = 0;
1144
    else
1145
        virDomainObjListRemove(driver->domains, vm);
1146

1147
    ret = 0;
1148

1149
 cleanup:
1150
    virDomainObjEndAPI(&vm);
1151
    openvzDriverUnlock(driver);
1152
    return ret;
1153 1154
}

1155 1156 1157 1158 1159
static int
openvzDomainUndefine(virDomainPtr dom)
{
    return openvzDomainUndefineFlags(dom, 0);
}
1160 1161 1162
static int
openvzDomainSetAutostart(virDomainPtr dom, int autostart)
{
1163 1164
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
E
Eric Blake 已提交
1165
    const char *prog[] = { VZCTL, "--quiet", "set", PROGRAM_SENTINEL,
D
Daniel Veillard 已提交
1166
                           "--onboot", autostart ? "yes" : "no",
1167
                           "--save", NULL };
1168
    int ret = -1;
1169

1170 1171
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
1172

1173
    openvzSetProgramSentinal(prog, vm->def->name);
1174
    if (virRun(prog, NULL) < 0)
1175 1176
        goto cleanup;
    ret = 0;
1177

1178
 cleanup:
1179
    virDomainObjEndAPI(&vm);
1180
    return ret;
1181 1182 1183 1184 1185
}

static int
openvzDomainGetAutostart(virDomainPtr dom, int *autostart)
{
1186 1187
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
1188
    char *value = NULL;
1189
    int ret = -1;
1190

1191 1192
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
1193

1194
    if (openvzReadVPSConfigParam(strtoI(vm->def->name), "ONBOOT", &value) < 0) {
1195 1196
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not read container config"));
1197
        goto cleanup;
1198 1199 1200
    }

    *autostart = 0;
1201
    if (STREQ(value, "yes"))
D
Daniel Veillard 已提交
1202
        *autostart = 1;
1203
    ret = 0;
1204

1205
 cleanup:
1206 1207
    VIR_FREE(value);

1208
    virDomainObjEndAPI(&vm);
1209
    return ret;
1210 1211
}

1212 1213
static int openvzConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                    const char *type)
1214 1215 1216
{
    if (type == NULL || STRCASEEQ(type, "openvz"))
        return 1028; /* OpenVZ has no limitation */
1217

1218 1219
    virReportError(VIR_ERR_INVALID_ARG,
                   _("unknown type '%s'"), type);
1220 1221 1222
    return -1;
}

1223 1224 1225 1226
static int
openvzDomainGetVcpusFlags(virDomainPtr dom ATTRIBUTE_UNUSED,
                          unsigned int flags)
{
1227
    if (flags != (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
1228 1229
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported flags (0x%x)"), flags);
1230 1231
        return -1;
    }
1232

1233
    return openvzConnectGetMaxVcpus(NULL, "openvz");
1234 1235
}

1236 1237
static int openvzDomainGetMaxVcpus(virDomainPtr dom)
{
1238
    return openvzDomainGetVcpusFlags(dom, (VIR_DOMAIN_AFFECT_LIVE |
1239 1240 1241
                                           VIR_DOMAIN_VCPU_MAXIMUM));
}

1242
static int openvzDomainSetVcpusInternal(virDomainObjPtr vm,
1243 1244
                                        unsigned int nvcpus,
                                        virDomainXMLOptionPtr xmlopt)
1245 1246
{
    char        str_vcpus[32];
E
Eric Blake 已提交
1247
    const char *prog[] = { VZCTL, "--quiet", "set", PROGRAM_SENTINEL,
1248
                           "--cpus", str_vcpus, "--save", NULL };
1249
    unsigned int pcpus;
1250
    pcpus = virHostCPUGetCount();
1251 1252 1253
    if (pcpus > 0 && pcpus < nvcpus)
        nvcpus = pcpus;

J
Ján Tomko 已提交
1254
    snprintf(str_vcpus, sizeof(str_vcpus), "%d", nvcpus);
1255 1256

    openvzSetProgramSentinal(prog, vm->def->name);
1257
    if (virRun(prog, NULL) < 0)
1258 1259
        return -1;

1260
    if (virDomainDefSetVcpusMax(vm->def, nvcpus, xmlopt) < 0)
1261 1262
        return -1;

1263 1264 1265
    if (virDomainDefSetVcpus(vm->def, nvcpus) < 0)
        return -1;

1266 1267 1268
    return 0;
}

1269 1270
static int openvzDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                                     unsigned int flags)
1271 1272 1273 1274
{
    virDomainObjPtr         vm;
    struct openvz_driver   *driver = dom->conn->privateData;
    int                     ret = -1;
1275

1276
    if (flags != VIR_DOMAIN_AFFECT_LIVE) {
1277 1278
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported flags (0x%x)"), flags);
1279 1280 1281
        return -1;
    }

1282 1283
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
1284

1285
    if (nvcpus <= 0) {
1286
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1287 1288 1289 1290
                       _("Number of vCPUs should be >= 1"));
        goto cleanup;
    }

1291
    if (openvzDomainSetVcpusInternal(vm, nvcpus, driver->xmlopt) < 0) {
1292 1293
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not set number of vCPUs"));
1294
        goto cleanup;
1295 1296
    }

1297 1298
    ret = 0;

1299
 cleanup:
1300
    virDomainObjEndAPI(&vm);
1301
    return ret;
1302 1303
}

1304 1305 1306
static int
openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
{
1307
    return openvzDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_AFFECT_LIVE);
1308 1309
}

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323

static int
openvzConnectURIProbe(char **uri)
{
    if (!virFileExists("/proc/vz"))
        return 0;

    if (access("/proc/vz", W_OK) < 0)
        return 0;

    return VIR_STRDUP(*uri, "openvz:///system");
}


1324 1325
static virDrvOpenStatus openvzConnectOpen(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1326
                                          virConfPtr conf ATTRIBUTE_UNUSED,
1327
                                          unsigned int flags)
1328
{
1329
    struct openvz_driver *driver;
1330

E
Eric Blake 已提交
1331 1332
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1333
    /* If path isn't /system, then they typoed, so tell them correct path */
1334
    if (STRNEQ(conn->uri->path, "/system")) {
1335 1336 1337 1338 1339
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected OpenVZ URI path '%s', try openvz:///system"),
                       conn->uri->path);
        return VIR_DRV_OPEN_ERROR;
    }
1340

1341 1342 1343 1344 1345
    if (!virFileExists("/proc/vz")) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("OpenVZ control file /proc/vz does not exist"));
        return VIR_DRV_OPEN_ERROR;
    }
1346

1347 1348 1349 1350
    if (access("/proc/vz", W_OK) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("OpenVZ control file /proc/vz is not accessible"));
        return VIR_DRV_OPEN_ERROR;
1351 1352
    }

1353 1354 1355
    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */

1356
    if (VIR_ALLOC(driver) < 0)
1357 1358
        return VIR_DRV_OPEN_ERROR;

1359
    if (!(driver->domains = virDomainObjListNew()))
1360 1361
        goto cleanup;

1362 1363
    if (!(driver->caps = openvzCapsInit()))
        goto cleanup;
1364

1365
    if (!(driver->xmlopt = virDomainXMLOptionNew(&openvzDomainDefParserConfig,
1366
                                                 NULL, NULL, NULL, NULL)))
1367 1368
        goto cleanup;

1369 1370
    if (openvzLoadDomains(driver) < 0)
        goto cleanup;
1371

1372
    if (openvzExtractVersion(driver) < 0)
1373 1374
        goto cleanup;

1375
    conn->privateData = driver;
1376

1377
    return VIR_DRV_OPEN_SUCCESS;
1378

1379
 cleanup:
1380 1381
    openvzFreeDriver(driver);
    return VIR_DRV_OPEN_ERROR;
1382
};
1383

1384 1385
static int openvzConnectClose(virConnectPtr conn)
{
1386
    struct openvz_driver *driver = conn->privateData;
1387

1388
    openvzFreeDriver(driver);
1389 1390 1391 1392 1393
    conn->privateData = NULL;

    return 0;
}

1394
static const char *openvzConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
1395
    return "OpenVZ";
1396 1397
}

1398 1399
static int openvzConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
1400 1401 1402 1403
    /* Encryption is not relevant / applicable to way we talk to openvz */
    return 0;
}

1404 1405
static int openvzConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
1406 1407 1408 1409
    /* We run CLI tools directly so this is secure */
    return 1;
}

1410
static int
1411
openvzConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1412 1413 1414 1415
{
    return 1;
}

1416
static char *openvzConnectGetCapabilities(virConnectPtr conn) {
1417 1418 1419
    struct openvz_driver *driver = conn->privateData;
    char *ret;

1420
    openvzDriverLock(driver);
1421
    ret = virCapabilitiesFormatXML(driver->caps);
1422
    openvzDriverUnlock(driver);
1423

1424
    return ret;
1425 1426
}

1427
static int openvzConnectListDomains(virConnectPtr conn ATTRIBUTE_UNUSED,
1428 1429
                                    int *ids, int nids)
{
1430
    int got = 0;
1431
    int veid;
1432
    int outfd = -1;
1433
    int rc = -1;
1434 1435
    int ret;
    char buf[32];
1436
    char *endptr;
1437
    virCommandPtr cmd = virCommandNewArgList(VZLIST, "-ovpsid", "-H", NULL);
1438 1439 1440 1441

    virCommandSetOutputFD(cmd, &outfd);
    if (virCommandRunAsync(cmd, NULL) < 0)
        goto cleanup;
1442

E
Eric Blake 已提交
1443
    while (got < nids) {
1444
        ret = openvz_readline(outfd, buf, 32);
E
Eric Blake 已提交
1445 1446
        if (!ret)
            break;
1447
        if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) {
1448 1449
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not parse VPS ID %s"), buf);
1450 1451
            continue;
        }
1452 1453 1454
        ids[got] = veid;
        got ++;
    }
1455 1456 1457

    if (virCommandWait(cmd, NULL) < 0)
        goto cleanup;
1458

G
Guido Günther 已提交
1459 1460
    if (VIR_CLOSE(outfd) < 0) {
        virReportSystemError(errno, "%s", _("failed to close file"));
1461
        goto cleanup;
G
Guido Günther 已提交
1462
    }
1463 1464

    rc = got;
1465
 cleanup:
1466 1467 1468
    VIR_FORCE_CLOSE(outfd);
    virCommandFree(cmd);
    return rc;
1469 1470
}

1471 1472
static int openvzConnectNumOfDomains(virConnectPtr conn)
{
1473
    struct openvz_driver *driver = conn->privateData;
1474
    int n;
1475

1476
    openvzDriverLock(driver);
1477
    n = virDomainObjListNumOfDomains(driver->domains, true, NULL, NULL);
1478
    openvzDriverUnlock(driver);
1479

1480
    return n;
1481 1482
}

1483 1484
static int openvzConnectListDefinedDomains(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           char **const names, int nnames) {
1485
    int got = 0;
G
Guido Günther 已提交
1486
    int veid, outfd = -1, ret;
1487
    int rc = -1;
1488
    char vpsname[32];
1489
    char buf[32];
1490
    char *endptr;
1491 1492
    virCommandPtr cmd = virCommandNewArgList(VZLIST,
                                             "-ovpsid", "-H", "-S", NULL);
1493 1494

    /* the -S options lists only stopped domains */
1495 1496
    virCommandSetOutputFD(cmd, &outfd);
    if (virCommandRunAsync(cmd, NULL) < 0)
G
Guido Günther 已提交
1497
        goto out;
1498

E
Eric Blake 已提交
1499
    while (got < nnames) {
1500
        ret = openvz_readline(outfd, buf, 32);
E
Eric Blake 已提交
1501 1502
        if (!ret)
            break;
1503
        if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) {
1504 1505
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not parse VPS ID %s"), buf);
1506 1507
            continue;
        }
1508
        snprintf(vpsname, sizeof(vpsname), "%d", veid);
1509
        if (VIR_STRDUP(names[got], vpsname) < 0)
G
Guido Günther 已提交
1510
            goto out;
1511 1512
        got ++;
    }
1513 1514 1515 1516

    if (virCommandWait(cmd, NULL) < 0)
        goto out;

G
Guido Günther 已提交
1517 1518 1519 1520
    if (VIR_CLOSE(outfd) < 0) {
        virReportSystemError(errno, "%s", _("failed to close file"));
        goto out;
    }
1521

1522
    rc = got;
1523
 out:
G
Guido Günther 已提交
1524
    VIR_FORCE_CLOSE(outfd);
1525
    virCommandFree(cmd);
1526
    if (rc < 0) {
1527
        for (; got >= 0; got--)
1528 1529 1530
            VIR_FREE(names[got]);
    }
    return rc;
1531 1532
}

1533 1534 1535 1536 1537
static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid)
{
    FILE *fp;
    char *line = NULL;
    size_t line_size = 0;
D
Daniel Veillard 已提交
1538
    unsigned long long usertime, systime, nicetime;
1539
    int readvps = vpsid + 1;  /* ensure readvps is initially different */
1540
    ssize_t ret;
1541
    int err = 0;
D
Daniel Veillard 已提交
1542 1543

/* read statistic from /proc/vz/vestat.
1544
 sample:
D
Daniel Veillard 已提交
1545
Version: 2.2
1546 1547 1548
   VEID     user      nice     system     uptime                 idle   other..
     33       78         0       1330   59454597      142650441835148   other..
     55      178         0       5340   59424597      542650441835148   other..
D
Daniel Veillard 已提交
1549 1550
*/

1551
    if ((fp = fopen("/proc/vz/vestat", "r")) == NULL)
D
Daniel Veillard 已提交
1552 1553 1554
        return -1;

    /*search line with VEID=vpsid*/
E
Eric Blake 已提交
1555
    while (1) {
1556
        ret = getline(&line, &line_size, fp);
1557 1558
        if (ret < 0) {
            err = !feof(fp);
D
Daniel Veillard 已提交
1559
            break;
1560
        }
D
Daniel Veillard 已提交
1561

1562 1563
        if (sscanf(line, "%d %llu %llu %llu",
                   &readvps, &usertime, &nicetime, &systime) == 4
1564 1565 1566 1567 1568 1569 1570
            && readvps == vpsid) { /*found vpsid*/
            /* convert jiffies to nanoseconds */
            *cpuTime = (1000ull * 1000ull * 1000ull
                        * (usertime + nicetime  + systime)
                        / (unsigned long long)sysconf(_SC_CLK_TCK));
            break;
        }
D
Daniel Veillard 已提交
1571 1572
    }

1573 1574
    VIR_FREE(line);
    VIR_FORCE_FCLOSE(fp);
1575
    if (err)
D
Daniel Veillard 已提交
1576 1577 1578 1579 1580 1581 1582 1583
        return -1;

    if (readvps != vpsid) /*not found*/
        return -1;

    return 0;
}

1584 1585
static int openvzConnectNumOfDefinedDomains(virConnectPtr conn)
{
1586
    struct openvz_driver *driver =  conn->privateData;
1587
    int n;
1588

1589
    openvzDriverLock(driver);
1590
    n = virDomainObjListNumOfDomains(driver->domains, false, NULL, NULL);
1591
    openvzDriverUnlock(driver);
1592

1593
    return n;
1594 1595
}

1596
static int
1597
openvzDomainSetMemoryInternal(virDomainObjPtr vm,
1598
                              unsigned long long mem)
1599 1600
{
    char str_mem[16];
E
Eric Blake 已提交
1601
    const char *prog[] = { VZCTL, "--quiet", "set", PROGRAM_SENTINEL,
1602 1603 1604 1605
        "--kmemsize", str_mem, "--save", NULL
    };

    /* memory has to be changed its format from kbyte to byte */
1606
    snprintf(str_mem, sizeof(str_mem), "%llu", mem * 1024);
1607 1608

    openvzSetProgramSentinal(prog, vm->def->name);
1609
    if (virRun(prog, NULL) < 0)
1610 1611 1612 1613
        goto cleanup;

    return 0;

1614
 cleanup:
1615 1616 1617
    return -1;
}

1618 1619 1620 1621 1622 1623 1624

static int
openvzDomainGetBarrierLimit(virDomainPtr domain,
                            const char *param,
                            unsigned long long *barrier,
                            unsigned long long *limit)
{
1625
    int ret = -1;
1626 1627 1628 1629 1630 1631 1632
    char *endp, *output = NULL;
    const char *tmp;
    virCommandPtr cmd = virCommandNewArgList(VZLIST, "--no-header", NULL);

    virCommandSetOutputBuffer(cmd, &output);
    virCommandAddArgFormat(cmd, "-o%s.b,%s.l", param, param);
    virCommandAddArg(cmd, domain->name);
1633
    if (virCommandRun(cmd, NULL) < 0)
1634 1635 1636 1637 1638
        goto cleanup;

    tmp = output;
    virSkipSpaces(&tmp);
    if (virStrToLong_ull(tmp, &endp, 10, barrier) < 0) {
1639
        virReportError(VIR_ERR_INTERNAL_ERROR,
1640
                       _("Can't parse limit from vzlist output '%s'"), output);
1641 1642 1643 1644 1645
        goto cleanup;
    }
    tmp = endp;
    virSkipSpaces(&tmp);
    if (virStrToLong_ull(tmp, &endp, 10, limit) < 0) {
1646
        virReportError(VIR_ERR_INTERNAL_ERROR,
1647
                       _("Can't parse barrier from vzlist output '%s'"), output);
1648 1649 1650 1651
        goto cleanup;
    }

    ret = 0;
1652
 cleanup:
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
    VIR_FREE(output);
    virCommandFree(cmd);
    return ret;
}


static int
openvzDomainSetBarrierLimit(virDomainPtr domain,
                            const  char *param,
                            unsigned long long barrier,
                            unsigned long long limit)
{
1665
    int ret = -1;
1666 1667 1668 1669
    virCommandPtr cmd = virCommandNewArgList(VZCTL, "--quiet", "set", NULL);

    /* LONG_MAX indicates unlimited so reject larger values */
    if (barrier > LONG_MAX || limit > LONG_MAX) {
1670 1671 1672
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Failed to set %s for %s: value too large"), param,
                       domain->name);
1673 1674 1675 1676 1677 1678 1679
        goto cleanup;
    }

    virCommandAddArg(cmd, domain->name);
    virCommandAddArgFormat(cmd, "--%s", param);
    virCommandAddArgFormat(cmd, "%llu:%llu", barrier, limit);
    virCommandAddArg(cmd, "--save");
1680
    if (virCommandRun(cmd, NULL) < 0)
1681 1682 1683
        goto cleanup;

    ret = 0;
1684
 cleanup:
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
    virCommandFree(cmd);
    return ret;
}


static int
openvzDomainGetMemoryParameters(virDomainPtr domain,
                                virTypedParameterPtr params,
                                int *nparams,
                                unsigned int flags)
{
1696 1697
    size_t i;
    int result = -1;
1698 1699 1700 1701 1702 1703
    const char *name;
    long kb_per_pages;
    unsigned long long barrier, limit, val;

    virCheckFlags(0, -1);

1704 1705
    kb_per_pages = openvzKBPerPages();
    if (kb_per_pages < 0)
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
        goto cleanup;

    if (*nparams == 0) {
        *nparams = OPENVZ_NB_MEM_PARAM;
        return 0;
    }

    for (i = 0; i <= *nparams; i++) {
        virMemoryParameterPtr param = &params[i];

        switch (i) {
        case 0:
            name = "privvmpages";
            if (openvzDomainGetBarrierLimit(domain, name, &barrier, &limit) < 0)
                goto cleanup;

1722
            val = (limit == LONG_MAX) ? VIR_DOMAIN_MEMORY_PARAM_UNLIMITED : limit * kb_per_pages;
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
            if (virTypedParameterAssign(param, VIR_DOMAIN_MEMORY_HARD_LIMIT,
                                        VIR_TYPED_PARAM_ULLONG, val) < 0)
                goto cleanup;
            break;

        case 1:
            name = "privvmpages";
            if (openvzDomainGetBarrierLimit(domain, name, &barrier, &limit) < 0)
                goto cleanup;

1733
            val = (barrier == LONG_MAX) ? VIR_DOMAIN_MEMORY_PARAM_UNLIMITED : barrier * kb_per_pages;
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
            if (virTypedParameterAssign(param, VIR_DOMAIN_MEMORY_SOFT_LIMIT,
                                        VIR_TYPED_PARAM_ULLONG, val) < 0)
                goto cleanup;
            break;

        case 2:
            name = "vmguarpages";
            if (openvzDomainGetBarrierLimit(domain, name, &barrier, &limit) < 0)
                goto cleanup;

            val = (barrier == LONG_MAX) ? 0ull : barrier * kb_per_pages;
            if (virTypedParameterAssign(param, VIR_DOMAIN_MEMORY_MIN_GUARANTEE,
                                        VIR_TYPED_PARAM_ULLONG, val) < 0)
                goto cleanup;
            break;
        }
    }

    if (*nparams > OPENVZ_NB_MEM_PARAM)
        *nparams = OPENVZ_NB_MEM_PARAM;
    result = 0;

1756
 cleanup:
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
    return result;
}


static int
openvzDomainSetMemoryParameters(virDomainPtr domain,
                                virTypedParameterPtr params,
                                int nparams,
                                unsigned int flags)
{
1767 1768
    size_t i;
    int result = -1;
1769 1770
    long kb_per_pages;

1771 1772
    kb_per_pages = openvzKBPerPages();
    if (kb_per_pages < 0)
1773 1774 1775
        goto cleanup;

    virCheckFlags(0, -1);
1776 1777 1778 1779 1780 1781 1782 1783
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_MEMORY_HARD_LIMIT,
                               VIR_TYPED_PARAM_ULLONG,
                               VIR_DOMAIN_MEMORY_SOFT_LIMIT,
                               VIR_TYPED_PARAM_ULLONG,
                               VIR_DOMAIN_MEMORY_MIN_GUARANTEE,
                               VIR_TYPED_PARAM_ULLONG,
                               NULL) < 0)
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
        return -1;

    for (i = 0; i < nparams; i++) {
        virTypedParameterPtr param = &params[i];
        unsigned long long barrier, limit;

        if (STREQ(param->field, VIR_DOMAIN_MEMORY_HARD_LIMIT)) {
            if (openvzDomainGetBarrierLimit(domain, "privvmpages",
                                            &barrier, &limit) < 0)
                goto cleanup;
            limit = params[i].value.ul / kb_per_pages;
            if (openvzDomainSetBarrierLimit(domain, "privvmpages",
                                            barrier, limit) < 0)
                goto cleanup;
        } else if (STREQ(param->field, VIR_DOMAIN_MEMORY_SOFT_LIMIT)) {
            if (openvzDomainGetBarrierLimit(domain, "privvmpages",
                                            &barrier, &limit) < 0)
                goto cleanup;
            barrier = params[i].value.ul / kb_per_pages;
            if (openvzDomainSetBarrierLimit(domain, "privvmpages",
                                            barrier, limit) < 0)
                goto cleanup;
        } else if (STREQ(param->field, VIR_DOMAIN_MEMORY_MIN_GUARANTEE)) {
            barrier = params[i].value.ul / kb_per_pages;
            if (openvzDomainSetBarrierLimit(domain, "vmguarpages",
                                            barrier, LONG_MAX) < 0)
                goto cleanup;
        }
    }
    result = 0;
1814
 cleanup:
1815 1816 1817 1818
    return result;
}


1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
static int
openvzGetVEStatus(virDomainObjPtr vm, int *status, int *reason)
{
    virCommandPtr cmd;
    char *outbuf;
    char *line;
    int state;
    int ret = -1;

    cmd = virCommandNewArgList(VZLIST, vm->def->name, "-ostatus", "-H", NULL);
    virCommandSetOutputBuffer(cmd, &outbuf);
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

    if ((line = strchr(outbuf, '\n')) == NULL) {
1834 1835
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failed to parse vzlist output"));
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
        goto cleanup;
    }
    *line++ = '\0';

    state = virDomainObjGetState(vm, reason);

    if (STREQ(outbuf, "running")) {
        /* There is no way to detect whether a domain is paused or not
         * with vzlist */
        if (state == VIR_DOMAIN_PAUSED)
            *status = state;
        else
            *status = VIR_DOMAIN_RUNNING;
    } else {
        *status = VIR_DOMAIN_SHUTOFF;
    }

    ret = 0;

1855
 cleanup:
1856 1857 1858 1859 1860
    virCommandFree(cmd);
    VIR_FREE(outbuf);
    return ret;
}

1861
static int
1862
openvzDomainInterfaceStats(virDomainPtr dom,
1863
                           const char *device,
1864
                           virDomainInterfaceStatsPtr stats)
1865 1866 1867
{
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
M
Michal Privoznik 已提交
1868
    virDomainNetDefPtr net = NULL;
1869 1870
    int ret = -1;

1871 1872
    if (!(vm = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;
1873

1874
    if (virDomainObjCheckActive(vm) < 0)
1875 1876
        goto cleanup;

1877
    if (!(net = virDomainNetFind(vm->def, device)))
M
Michal Privoznik 已提交
1878 1879
        goto cleanup;

1880
    if (virNetDevTapInterfaceStats(net->ifname, stats,
1881
                                   !virDomainNetTypeSharesHostView(net)) < 0)
M
Michal Privoznik 已提交
1882 1883 1884
        goto cleanup;

    ret = 0;
1885

1886
 cleanup:
1887
    virDomainObjEndAPI(&vm);
1888 1889 1890 1891
    return ret;
}


1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
static int
openvzUpdateDevice(virDomainDefPtr vmdef,
                   virDomainDeviceDefPtr dev,
                   bool persist)
{
    virDomainFSDefPtr fs, cur;
    int pos;

    if (dev->type == VIR_DOMAIN_DEVICE_FS) {
        fs = dev->data.fs;
        pos = virDomainFSIndexByName(vmdef, fs->dst);

        if (pos < 0) {
1905 1906
            virReportError(VIR_ERR_INVALID_ARG,
                           _("target %s doesn't exist."), fs->dst);
1907 1908 1909 1910 1911
            return -1;
        }
        cur = vmdef->fss[pos];

        /* We only allow updating the quota */
1912
        if (STRNEQ(cur->src->path, fs->src->path)
1913 1914 1915 1916
            || cur->type != fs->type
            || cur->accessmode != fs->accessmode
            || cur->wrpolicy != fs->wrpolicy
            || cur->readonly != fs->readonly) {
1917
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1918
                           _("Can only modify disk quota"));
1919 1920 1921
            return -1;
        }

1922
        if (openvzSetDiskQuota(vmdef, fs, persist) < 0)
1923 1924 1925 1926
            return -1;
        cur->space_hard_limit = fs->space_hard_limit;
        cur->space_soft_limit = fs->space_soft_limit;
    } else {
1927 1928 1929
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Can't modify device type '%s'"),
                       virDomainDeviceTypeToString(dev->type));
1930 1931 1932 1933 1934 1935 1936 1937 1938
        return -1;
    }

    return 0;
}


static int
openvzDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml,
J
John Ferlan 已提交
1939
                              unsigned int flags)
1940 1941 1942 1943 1944 1945
{
    int ret = -1;
    int veid;
    struct  openvz_driver *driver = dom->conn->privateData;
    virDomainDeviceDefPtr dev = NULL;
    virDomainObjPtr vm = NULL;
1946
    virDomainDefPtr def = NULL;
1947 1948 1949 1950 1951 1952
    bool persist = false;

    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG, -1);

    openvzDriverLock(driver);
1953
    if (!(vm = openvzDomObjFromDomainLocked(driver, dom->uuid)))
1954 1955
        goto cleanup;

1956
    if (virStrToLong_i(vm->def->name, NULL, 10, &veid) < 0) {
1957 1958
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not convert domain name to VEID"));
1959 1960 1961
        goto cleanup;
    }

1962
    if (!(def = virDomainObjGetOneDef(vm, flags)))
1963 1964
        goto cleanup;

1965
    dev = virDomainDeviceDefParse(xml, def, driver->caps, driver->xmlopt,
1966
                                  VIR_DOMAIN_DEF_PARSE_INACTIVE);
1967 1968 1969 1970 1971 1972
    if (!dev)
        goto cleanup;

    if (flags & VIR_DOMAIN_AFFECT_CONFIG)
        persist = true;

1973
    if (openvzUpdateDevice(def, dev, persist) < 0)
1974 1975 1976 1977
        goto cleanup;

    ret = 0;

1978
 cleanup:
1979 1980
    openvzDriverUnlock(driver);
    virDomainDeviceDefFree(dev);
1981
    virDomainObjEndAPI(&vm);
1982 1983 1984
    return ret;
}

1985
static int
1986 1987 1988
openvzConnectListAllDomains(virConnectPtr conn,
                            virDomainPtr **domains,
                            unsigned int flags)
1989 1990 1991 1992
{
    struct openvz_driver *driver = conn->privateData;
    int ret = -1;

O
Osier Yang 已提交
1993
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
1994 1995

    openvzDriverLock(driver);
1996 1997
    ret = virDomainObjListExport(driver->domains, conn, domains,
                                 NULL, flags);
1998 1999 2000 2001 2002
    openvzDriverUnlock(driver);

    return ret;
}

2003

2004 2005 2006 2007 2008

static int
openvzNodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED,
                  virNodeInfoPtr nodeinfo)
{
M
Martin Kletzander 已提交
2009
    return virCapabilitiesGetNodeInfo(nodeinfo);
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
}


static int
openvzNodeGetCPUStats(virConnectPtr conn ATTRIBUTE_UNUSED,
                      int cpuNum,
                      virNodeCPUStatsPtr params,
                      int *nparams,
                      unsigned int flags)
{
2020
    return virHostCPUGetStats(cpuNum, params, nparams, flags);
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
}


static int
openvzNodeGetMemoryStats(virConnectPtr conn ATTRIBUTE_UNUSED,
                         int cellNum,
                         virNodeMemoryStatsPtr params,
                         int *nparams,
                         unsigned int flags)
{
2031
    return virHostMemGetStats(cellNum, params, nparams, flags);
2032 2033 2034 2035 2036 2037 2038 2039 2040
}


static int
openvzNodeGetCellsFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED,
                             unsigned long long *freeMems,
                             int startCell,
                             int maxCells)
{
2041
    return virHostMemGetCellsFree(freeMems, startCell, maxCells);
2042 2043 2044 2045 2046 2047
}


static unsigned long long
openvzNodeGetFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED)
{
2048
    unsigned long long freeMem;
2049
    if (virHostMemGetInfo(NULL, &freeMem) < 0)
2050 2051
        return 0;
    return freeMem;
2052 2053 2054 2055 2056 2057 2058 2059 2060
}


static int
openvzNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
                    unsigned char **cpumap,
                    unsigned int *online,
                    unsigned int flags)
{
2061
    return virHostCPUGetMap(cpumap, online, flags);
2062 2063 2064
}


2065 2066 2067
static int
openvzConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
2068
    switch ((virDrvFeature) feature) {
2069 2070 2071
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
    case VIR_DRV_FEATURE_MIGRATION_V3:
        return 1;
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
    case VIR_DRV_FEATURE_FD_PASSING:
    case VIR_DRV_FEATURE_MIGRATE_CHANGE_PROTECTION:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
    case VIR_DRV_FEATURE_MIGRATION_OFFLINE:
    case VIR_DRV_FEATURE_MIGRATION_P2P:
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_PROGRAM_KEEPALIVE:
    case VIR_DRV_FEATURE_REMOTE:
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
    case VIR_DRV_FEATURE_TYPED_PARAM_STRING:
    case VIR_DRV_FEATURE_XML_MIGRATABLE:
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
    default:
        return 0;
    }
}


static char *
openvzDomainMigrateBegin3Params(virDomainPtr domain,
                                virTypedParameterPtr params,
                                int nparams,
                                char **cookieout ATTRIBUTE_UNUSED,
                                int *cookieoutlen ATTRIBUTE_UNUSED,
                                unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    struct openvz_driver *driver = domain->conn->privateData;
    char *xml = NULL;
    int status;

    virCheckFlags(OPENVZ_MIGRATION_FLAGS, NULL);
    if (virTypedParamsValidate(params, nparams, OPENVZ_MIGRATION_PARAMETERS) < 0)
        return NULL;

2108 2109
    if (!(vm = openvzDomObjFromDomain(driver, domain->uuid)))
        return NULL;
2110

2111
    if (virDomainObjCheckActive(vm) < 0)
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
        goto cleanup;

    if (openvzGetVEStatus(vm, &status, NULL) == -1)
        goto cleanup;

    if (status != VIR_DOMAIN_RUNNING) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not in running state"));
        goto cleanup;
    }

2123 2124
    xml = virDomainDefFormat(vm->def, driver->caps,
                             VIR_DOMAIN_DEF_FORMAT_SECURE);
2125 2126

 cleanup:
2127
    virDomainObjEndAPI(&vm);
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
    return xml;
}

static int
openvzDomainMigratePrepare3Params(virConnectPtr dconn,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  const char *cookiein ATTRIBUTE_UNUSED,
                                  int cookieinlen ATTRIBUTE_UNUSED,
                                  char **cookieout ATTRIBUTE_UNUSED,
                                  int *cookieoutlen ATTRIBUTE_UNUSED,
                                  char **uri_out,
                                  unsigned int fflags ATTRIBUTE_UNUSED)
{
    struct openvz_driver *driver = dconn->privateData;
    const char *dom_xml = NULL;
    const char *uri_in = NULL;
    virDomainDefPtr def = NULL;
    virDomainObjPtr vm = NULL;
2147 2148
    char *my_hostname = NULL;
    const char *hostname = NULL;
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
    virURIPtr uri = NULL;
    int ret = -1;

    if (virTypedParamsValidate(params, nparams, OPENVZ_MIGRATION_PARAMETERS) < 0)
        goto error;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_XML,
                                &dom_xml) < 0 ||
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI,
                                &uri_in) < 0)
        goto error;

    if (!dom_xml) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no domain XML passed"));
        goto error;
    }

    if (!(def = virDomainDefParseString(dom_xml, driver->caps, driver->xmlopt,
2170
                                        NULL,
2171 2172
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE |
                                        VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE)))
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
        goto error;

    if (!(vm = virDomainObjListAdd(driver->domains, def,
                                   driver->xmlopt,
                                   VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
        goto error;
    def = NULL;

    if (!uri_in) {
2184
        if ((my_hostname = virGetHostname()) == NULL)
2185 2186
            goto error;

2187
        if (STRPREFIX(my_hostname, "localhost")) {
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("hostname on destination resolved to localhost,"
                             " but migration requires an FQDN"));
            goto error;
        }
    } else {
        uri = virURIParse(uri_in);

        if (uri == NULL) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("unable to parse URI: %s"),
                           uri_in);
            goto error;
        }

        if (uri->server == NULL) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("missing host in migration URI: %s"),
                           uri_in);
            goto error;
        } else {
            hostname = uri->server;
        }
    }

    if (virAsprintf(uri_out, "ssh://%s", hostname) < 0)
        goto error;

    ret = 0;
    goto done;

 error:
    virDomainDefFree(def);
2221
    if (vm)
2222 2223 2224
        virDomainObjListRemove(driver->domains, vm);

 done:
2225
    VIR_FREE(my_hostname);
2226
    virURIFree(uri);
2227
    virDomainObjEndAPI(&vm);
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
    return ret;
}

static int
openvzDomainMigratePerform3Params(virDomainPtr domain,
                                  const char *dconnuri ATTRIBUTE_UNUSED,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  const char *cookiein ATTRIBUTE_UNUSED,
                                  int cookieinlen ATTRIBUTE_UNUSED,
                                  char **cookieout ATTRIBUTE_UNUSED,
                                  int *cookieoutlen ATTRIBUTE_UNUSED,
                                  unsigned int flags)
{
    struct openvz_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    const char *uri_str = NULL;
    virURIPtr uri = NULL;
2246
    virCommandPtr cmd = NULL;
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
    int ret = -1;

    virCheckFlags(OPENVZ_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, OPENVZ_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI,
                                &uri_str) < 0)
        goto cleanup;

2258
    if (!(vm = openvzDomObjFromDomain(driver, domain->uuid)))
2259 2260 2261 2262 2263 2264 2265
        goto cleanup;

    /* parse dst host:port from uri */
    uri = virURIParse(uri_str);
    if (uri == NULL || uri->server == NULL)
        goto cleanup;

2266
    cmd = virCommandNew(VZMIGRATE);
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
    if (flags & VIR_MIGRATE_LIVE)
        virCommandAddArg(cmd, "--live");
    virCommandAddArg(cmd, uri->server);
    virCommandAddArg(cmd, vm->def->name);

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

    ret = 0;

 cleanup:
    virCommandFree(cmd);
    virURIFree(uri);
2280
    virDomainObjEndAPI(&vm);
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
    return ret;
}

static virDomainPtr
openvzDomainMigrateFinish3Params(virConnectPtr dconn,
                                 virTypedParameterPtr params,
                                 int nparams,
                                 const char *cookiein ATTRIBUTE_UNUSED,
                                 int cookieinlen ATTRIBUTE_UNUSED,
                                 char **cookieout ATTRIBUTE_UNUSED,
                                 int *cookieoutlen ATTRIBUTE_UNUSED,
                                 unsigned int flags,
                                 int cancelled)
{
    struct openvz_driver *driver = dconn->privateData;
    virDomainObjPtr vm = NULL;
    const char *dname = NULL;
    virDomainPtr dom = NULL;
    int status;

    if (cancelled)
        goto cleanup;

    virCheckFlags(OPENVZ_MIGRATION_FLAGS, NULL);
    if (virTypedParamsValidate(params, nparams, OPENVZ_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

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

    if (!dname ||
        !(vm = virDomainObjListFindByName(driver->domains, dname))) {
        /* Migration obviously failed if the domain doesn't exist */
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Migration failed. No domain on destination host "
                         "with matching name '%s'"),
                       NULLSTR(dname));
        goto cleanup;
    }

    if (openvzGetVEStatus(vm, &status, NULL) == -1)
        goto cleanup;

    if (status != VIR_DOMAIN_RUNNING) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("domain is not running on destination host"));
        goto cleanup;
    }

    vm->def->id = strtoI(vm->def->name);
    virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_MIGRATED);

2335
    dom = virGetDomain(dconn, vm->def->name, vm->def->uuid, vm->def->id);
2336 2337

 cleanup:
2338
    virDomainObjEndAPI(&vm);
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
    return dom;
}

static int
openvzDomainMigrateConfirm3Params(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  const char *cookiein ATTRIBUTE_UNUSED,
                                  int cookieinlen ATTRIBUTE_UNUSED,
                                  unsigned int flags,
                                  int cancelled)
{
    struct openvz_driver *driver = domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    int status;
    int ret = -1;

    virCheckFlags(OPENVZ_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, OPENVZ_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

2360
    if (!(vm = openvzDomObjFromDomain(driver, domain->uuid)))
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
        goto cleanup;

    if (cancelled) {
        if (openvzGetVEStatus(vm, &status, NULL) == -1)
            goto cleanup;

        if (status == VIR_DOMAIN_RUNNING) {
            ret = 0;
        } else {
            VIR_DEBUG("Domain '%s' does not recover after failed migration",
                      vm->def->name);
        }

        goto cleanup;
    }

    vm->def->id = -1;

    VIR_DEBUG("Domain '%s' successfully migrated", vm->def->name);

    virDomainObjListRemove(driver->domains, vm);

    ret = 0;

 cleanup:
2386
    virDomainObjEndAPI(&vm);
2387 2388 2389
    return ret;
}

2390 2391 2392 2393 2394 2395 2396 2397 2398
static int
openvzDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    struct openvz_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    virCheckFlags(0, -1);

2399 2400 2401
    if (!(obj = openvzDomObjFromDomain(driver, dom->uuid)))
        return -1;

2402 2403
    ret = 0;

2404
    virDomainObjEndAPI(&obj);
2405 2406 2407 2408
    return ret;
}


2409

2410
static virHypervisorDriver openvzHypervisorDriver = {
2411
    .name = "OPENVZ",
2412
    .connectURIProbe = openvzConnectURIProbe,
2413 2414 2415 2416
    .connectOpen = openvzConnectOpen, /* 0.3.1 */
    .connectClose = openvzConnectClose, /* 0.3.1 */
    .connectGetType = openvzConnectGetType, /* 0.3.1 */
    .connectGetVersion = openvzConnectGetVersion, /* 0.5.0 */
2417
    .connectGetHostname = openvzConnectGetHostname, /* 0.9.12 */
2418
    .connectGetMaxVcpus = openvzConnectGetMaxVcpus, /* 0.4.6 */
2419 2420 2421 2422 2423 2424
    .nodeGetInfo = openvzNodeGetInfo, /* 0.3.2 */
    .nodeGetCPUStats = openvzNodeGetCPUStats, /* 0.9.12 */
    .nodeGetMemoryStats = openvzNodeGetMemoryStats, /* 0.9.12 */
    .nodeGetCellsFreeMemory = openvzNodeGetCellsFreeMemory, /* 0.9.12 */
    .nodeGetFreeMemory = openvzNodeGetFreeMemory, /* 0.9.12 */
    .nodeGetCPUMap = openvzNodeGetCPUMap, /* 1.0.0 */
2425 2426 2427 2428
    .connectGetCapabilities = openvzConnectGetCapabilities, /* 0.4.6 */
    .connectListDomains = openvzConnectListDomains, /* 0.3.1 */
    .connectNumOfDomains = openvzConnectNumOfDomains, /* 0.3.1 */
    .connectListAllDomains = openvzConnectListAllDomains, /* 0.9.13 */
2429 2430 2431 2432 2433 2434 2435
    .domainCreateXML = openvzDomainCreateXML, /* 0.3.3 */
    .domainLookupByID = openvzDomainLookupByID, /* 0.3.1 */
    .domainLookupByUUID = openvzDomainLookupByUUID, /* 0.3.1 */
    .domainLookupByName = openvzDomainLookupByName, /* 0.3.1 */
    .domainSuspend = openvzDomainSuspend, /* 0.8.3 */
    .domainResume = openvzDomainResume, /* 0.8.3 */
    .domainShutdown = openvzDomainShutdown, /* 0.3.1 */
2436
    .domainShutdownFlags = openvzDomainShutdownFlags, /* 0.9.10 */
2437
    .domainReboot = openvzDomainReboot, /* 0.3.1 */
2438 2439 2440
    .domainDestroy = openvzDomainDestroy, /* 0.3.1 */
    .domainDestroyFlags = openvzDomainDestroyFlags, /* 0.9.4 */
    .domainGetOSType = openvzDomainGetOSType, /* 0.3.1 */
2441 2442
    .domainGetMemoryParameters = openvzDomainGetMemoryParameters, /* 0.9.12 */
    .domainSetMemoryParameters = openvzDomainSetMemoryParameters, /* 0.9.12 */
2443 2444 2445 2446 2447 2448 2449
    .domainGetInfo = openvzDomainGetInfo, /* 0.3.1 */
    .domainGetState = openvzDomainGetState, /* 0.9.2 */
    .domainSetVcpus = openvzDomainSetVcpus, /* 0.4.6 */
    .domainSetVcpusFlags = openvzDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = openvzDomainGetVcpusFlags, /* 0.8.5 */
    .domainGetMaxVcpus = openvzDomainGetMaxVcpus, /* 0.4.6 */
    .domainGetXMLDesc = openvzDomainGetXMLDesc, /* 0.4.6 */
2450 2451
    .connectListDefinedDomains = openvzConnectListDefinedDomains, /* 0.3.1 */
    .connectNumOfDefinedDomains = openvzConnectNumOfDefinedDomains, /* 0.3.1 */
2452 2453 2454
    .domainCreate = openvzDomainCreate, /* 0.3.1 */
    .domainCreateWithFlags = openvzDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = openvzDomainDefineXML, /* 0.3.3 */
2455
    .domainDefineXMLFlags = openvzDomainDefineXMLFlags, /* 1.2.12 */
2456
    .domainUndefine = openvzDomainUndefine, /* 0.3.3 */
2457
    .domainUndefineFlags = openvzDomainUndefineFlags, /* 0.9.4 */
2458 2459
    .domainGetAutostart = openvzDomainGetAutostart, /* 0.4.6 */
    .domainSetAutostart = openvzDomainSetAutostart, /* 0.4.6 */
2460
    .domainInterfaceStats = openvzDomainInterfaceStats, /* 0.9.12 */
2461 2462
    .connectIsEncrypted = openvzConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = openvzConnectIsSecure, /* 0.7.3 */
2463 2464 2465
    .domainIsActive = openvzDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = openvzDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = openvzDomainIsUpdated, /* 0.8.6 */
2466
    .connectIsAlive = openvzConnectIsAlive, /* 0.9.8 */
2467
    .domainUpdateDeviceFlags = openvzDomainUpdateDeviceFlags, /* 0.9.13 */
2468
    .domainGetHostname = openvzDomainGetHostname, /* 0.10.0 */
2469 2470 2471 2472 2473 2474
    .connectSupportsFeature = openvzConnectSupportsFeature, /* 1.2.8 */
    .domainMigrateBegin3Params = openvzDomainMigrateBegin3Params, /* 1.2.8 */
    .domainMigratePrepare3Params = openvzDomainMigratePrepare3Params, /* 1.2.8 */
    .domainMigratePerform3Params = openvzDomainMigratePerform3Params, /* 1.2.8 */
    .domainMigrateFinish3Params = openvzDomainMigrateFinish3Params, /* 1.2.8 */
    .domainMigrateConfirm3Params = openvzDomainMigrateConfirm3Params, /* 1.2.8 */
2475
    .domainHasManagedSaveImage = openvzDomainHasManagedSaveImage, /* 1.2.13 */
2476 2477
};

2478
static virConnectDriver openvzConnectDriver = {
2479
    .localOnly = true,
2480
    .uriSchemes = (const char *[]){ "openvz", NULL },
2481 2482 2483
    .hypervisorDriver = &openvzHypervisorDriver,
};

2484 2485
int openvzRegister(void)
{
2486 2487
    return virRegisterConnectDriver(&openvzConnectDriver,
                                    false);
2488
}