openvz_driver.c 29.4 KB
Newer Older
1 2 3 4 5
/*
 * openvz_driver.c: core driver methods for managing OpenVZ VEs
 *
 * Copyright (C) 2006, 2007 Binary Karma
 * Copyright (C) 2006 Shuveb Hussain
6
 * Copyright (C) 2007 Anoop Joe Cyriac
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
22
 * Authors:
23 24 25
 * Shuveb Hussain <shuveb@binarykarma.com>
 * Anoop Joe Cyriac <anoop@binarykarma.com>
 *
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <strings.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <paths.h>
#include <pwd.h>
#include <stdio.h>
#include <sys/wait.h>

50
#include "internal.h"
51
#include "openvz_driver.h"
52 53
#include "event.h"
#include "buf.h"
54
#include "util.h"
55
#include "openvz_conf.h"
56
#include "nodeinfo.h"
57
#include "memory.h"
58

59 60 61
#define OPENVZ_MAX_ARG 28
#define CMDBUF_LEN 1488
#define CMDOP_LEN 288
62

D
Daniel Veillard 已提交
63
static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid);
64 65 66
static int openvzGetMaxVCPUs(virConnectPtr conn, const char *type);
static int openvzDomainGetMaxVcpus(virDomainPtr dom);
static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus);
D
Daniel Veillard 已提交
67

68 69
struct openvz_driver ovz_driver;

70
static void cmdExecFree(const char *cmdExec[])
71 72 73 74
{
    int i=-1;
    while(cmdExec[++i])
    {
75
        VIR_FREE(cmdExec[i]);
76 77 78
    }
}

79 80 81 82 83
/* generate arguments to create OpenVZ container
   return -1 - error
           0 - OK
*/
static int openvzDomainDefineCmd(virConnectPtr conn,
84
                                 const char *args[],
85
                                 int maxarg,
86
                                 virDomainDefPtr vmdef)
87 88 89 90 91 92 93 94
{
    int narg;

    for (narg = 0; narg < maxarg; narg++)
        args[narg] = NULL;

    if (vmdef == NULL){
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
95
                   "%s", _("Container is not defined"));
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        return -1;
    }

#define ADD_ARG(thisarg)                                                \
    do {                                                                \
        if (narg >= maxarg)                                             \
                 goto no_memory;                                        \
        args[narg++] = thisarg;                                         \
    } while (0)

#define ADD_ARG_LIT(thisarg)                                            \
    do {                                                                \
        if (narg >= maxarg)                                             \
                 goto no_memory;                                        \
        if ((args[narg++] = strdup(thisarg)) == NULL)                   \
            goto no_memory;                                             \
    } while (0)

    narg = 0;
    ADD_ARG_LIT(VZCTL);
    ADD_ARG_LIT("--quiet");
    ADD_ARG_LIT("create");
    ADD_ARG_LIT(vmdef->name);
119

120 121
    if (vmdef->nfss) {
        if (vmdef->fss[0]->type != VIR_DOMAIN_FS_TYPE_TEMPLATE) {
122 123 124 125
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                        "%s", _("only filesystem templates are supported"));
            return -1;
        }
126

127
        if (vmdef->nfss > 1) {
128 129 130 131
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                        "%s", _("only one filesystem supported"));
            return -1;
        }
132 133

        ADD_ARG_LIT("--ostemplate");
134
        ADD_ARG_LIT(vmdef->fss[0]->src);
135
    }
136
#if 0
137 138 139 140
    if ((vmdef->profile && *(vmdef->profile))) {
        ADD_ARG_LIT("--config");
        ADD_ARG_LIT(vmdef->profile);
    }
141
#endif
142 143 144 145 146 147 148 149 150 151 152 153

    ADD_ARG(NULL);
    return 0;
 no_memory:
    openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not put argument to %s"), VZCTL);
    return -1;
#undef ADD_ARG
#undef ADD_ARG_LIT
}


154
static virDomainPtr openvzDomainLookupByID(virConnectPtr conn,
155
                                           int id) {
156
    struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;
157
    virDomainObjPtr vm;
158 159
    virDomainPtr dom;

160
    vm = virDomainFindByID(&driver->domains, id);
161

162
    if (!vm) {
163
        openvzError(conn, VIR_ERR_NO_DOMAIN, NULL);
164 165 166
        return NULL;
    }

167 168
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (!dom)
169 170
        return NULL;

171
    dom->id = vm->def->id;
172 173 174
    return dom;
}

175
static char *openvzGetOSType(virDomainPtr dom)
176
{
177
    struct  openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
178
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
179 180 181 182 183 184 185 186 187 188 189
    char *ret;

    if (!vm) {
        openvzError(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        return NULL;
    }

    if (!(ret = strdup(vm->def->os.type)))
        openvzError(dom->conn, VIR_ERR_NO_MEMORY, NULL);

    return ret;
190 191 192 193
}


static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn,
194
                                             const unsigned char *uuid) {
195
    struct  openvz_driver *driver = (struct openvz_driver *)conn->privateData;
196
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, uuid);
197 198 199
    virDomainPtr dom;

    if (!vm) {
200
        openvzError(conn, VIR_ERR_NO_DOMAIN, NULL);
201 202 203
        return NULL;
    }

204 205
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (!dom)
206 207
        return NULL;

208
    dom->id = vm->def->id;
209 210 211 212 213 214
    return dom;
}

static virDomainPtr openvzDomainLookupByName(virConnectPtr conn,
                                     const char *name) {
    struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;
215
    virDomainObjPtr vm = virDomainFindByName(&driver->domains, name);
216 217 218
    virDomainPtr dom;

    if (!vm) {
219
        openvzError(conn, VIR_ERR_NO_DOMAIN, NULL);
220 221 222
        return NULL;
    }

223 224
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (!dom)
225
        return NULL;
226

227
    dom->id = vm->def->id;
228 229 230 231
    return dom;
}

static int openvzDomainGetInfo(virDomainPtr dom,
232
                               virDomainInfoPtr info) {
233
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
234
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
235

236
    if (!vm) {
D
Daniel Veillard 已提交
237
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
238
                    "%s", _("no domain with matching uuid"));
239 240 241
        return -1;
    }

242
    info->state = vm->state;
243

244
    if (!virDomainIsActive(vm)) {
D
Daniel Veillard 已提交
245 246 247 248
        info->cpuTime = 0;
    } else {
        if (openvzGetProcessInfo(&(info->cpuTime), dom->id) < 0) {
            openvzError(dom->conn, VIR_ERR_OPERATION_FAILED,
249
                        _("cannot read cputime for domain %d"), dom->id);
D
Daniel Veillard 已提交
250 251 252 253
            return -1;
        }
    }

254 255 256
    info->maxMem = vm->def->maxmem;
    info->memory = vm->def->memory;
    info->nrVirtCpu = vm->def->vcpus;
257 258 259 260
    return 0;
}


261 262
static char *openvzDomainDumpXML(virDomainPtr dom, int flags) {
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
263
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
264

265
    if (!vm) {
D
Daniel Veillard 已提交
266
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
267
                    "%s", _("no domain with matching uuid"));
268
        return NULL;
269
    }
270

271 272
    return virDomainDefFormat(dom->conn, vm->def, flags);
}
273

274 275 276 277


static int openvzDomainShutdown(virDomainPtr dom) {
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
278
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
279
    const char *prog[] = {VZCTL, "--quiet", "stop", vm ? vm->def->name : NULL, NULL};
280 281 282

    if (!vm) {
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
283
                    "%s", _("no domain with matching uuid"));
284 285
        return -1;
    }
286

287
    if (vm->state != VIR_DOMAIN_RUNNING) {
D
Daniel Veillard 已提交
288
        openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
289
                    "%s", _("domain is not in running state"));
290 291
        return -1;
    }
292

293 294
    if (virRun(dom->conn, prog, NULL) < 0)
        return -1;
295

296 297
    vm->def->id = -1;
    vm->state = VIR_DOMAIN_SHUTOFF;
298

299
    return 0;
300 301
}

302 303
static int openvzDomainReboot(virDomainPtr dom,
                              unsigned int flags ATTRIBUTE_UNUSED) {
304
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
305
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
306
    const char *prog[] = {VZCTL, "--quiet", "restart", vm ? vm->def->name : NULL, NULL};
307 308

    if (!vm) {
D
Daniel Veillard 已提交
309
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
310
                    "%s", _("no domain with matching uuid"));
311 312
        return -1;
    }
313

314 315
    if (vm->state != VIR_DOMAIN_RUNNING) {
        openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
316
                    "%s", _("domain is not in running state"));
317 318
        return -1;
    }
319

320
    if (virRun(dom->conn, prog, NULL) < 0)
321
        return -1;
322

323
    return 0;
324 325
}

D
Daniel Veillard 已提交
326 327 328 329 330
static int
openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
                        virDomainNetDefPtr net)
{
    int rc = 0, narg;
331
    const char *prog[OPENVZ_MAX_ARG];
D
Daniel Veillard 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    char *mac = NULL;

#define ADD_ARG_LIT(thisarg)                                            \
    do {                                                                \
        if (narg >= OPENVZ_MAX_ARG)                                             \
                 goto no_memory;                                        \
        if ((prog[narg++] = strdup(thisarg)) == NULL)                   \
            goto no_memory;                                             \
    } while (0)


    if (net == NULL)
       return 0;
    if (vpsid == NULL) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
347
                    "%s", _("Container ID is not specified"));
D
Daniel Veillard 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        return -1;
    }

    for (narg = 0; narg < OPENVZ_MAX_ARG; narg++)
        prog[narg] = NULL;

    narg = 0;

    if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE ||
        net->type == VIR_DOMAIN_NET_TYPE_ETHERNET) {
        ADD_ARG_LIT(VZCTL);
        ADD_ARG_LIT("--quiet");
        ADD_ARG_LIT("set");
        ADD_ARG_LIT(vpsid);
    }

    if (openvzCheckEmptyMac(net->mac) > 0)
          mac = openvzMacToString(net->mac);

    if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE &&
           net->data.bridge.brname != NULL) {
        char opt[1024];
        //--netif_add ifname[,mac,host_ifname,host_mac]
        ADD_ARG_LIT("--netif_add") ;
        strncpy(opt, net->data.bridge.brname, 256);
        if (mac != NULL) {
            strcat(opt, ",");
            strcat(opt, mac);
        }
        ADD_ARG_LIT(opt) ;
    }else if (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
              net->data.ethernet.ipaddr != NULL) {
        //--ipadd ip
        ADD_ARG_LIT("--ipadd") ;
        ADD_ARG_LIT(net->data.ethernet.ipaddr) ;
    }

    //TODO: processing NAT and physical device

    if (prog[0] != NULL){
        ADD_ARG_LIT("--save");
389
        if (virRun(conn, prog, NULL) < 0) {
D
Daniel Veillard 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
           openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                    _("Could not exec %s"), VZCTL);
           rc = -1;
           goto exit;
        }
    }

 exit:
    cmdExecFree(prog);
    VIR_FREE(mac);
    return rc;

 no_memory:
    openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not put argument to %s"), VZCTL);
    cmdExecFree(prog);
    VIR_FREE(mac);
    return -1;

#undef ADD_ARG_LIT
}

412 413 414 415
static virDomainPtr
openvzDomainDefineXML(virConnectPtr conn, const char *xml)
{
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
416 417
    virDomainDefPtr vmdef = NULL;
    virDomainObjPtr vm = NULL;
418
    virDomainPtr dom = NULL;
419
    int i;
420
    const char *prog[OPENVZ_MAX_ARG];
421
    prog[0] = NULL;
422

423
    if ((vmdef = virDomainDefParseString(conn, driver->caps, xml)) == NULL)
424
        return NULL;
425

426 427 428 429 430 431
    if (vmdef->os.init == NULL &&
        !(vmdef->os.init = strdup("/sbin/init"))) {
        virDomainDefFree(vmdef);
        return NULL;
    }

432
    vm = virDomainFindByName(&driver->domains, vmdef->name);
433
    if (vm) {
434 435
        openvzError(conn, VIR_ERR_OPERATION_FAILED,
                  _("Already an OPENVZ VM active with the id '%s'"),
436
                  vmdef->name);
437
        virDomainDefFree(vmdef);
438
        return NULL;
439
    }
440 441 442
    if (!(vm = virDomainAssignDef(conn, &driver->domains, vmdef))) {
        virDomainDefFree(vmdef);
        return NULL;
443 444
    }

445 446
    if (openvzDomainDefineCmd(conn, prog, OPENVZ_MAX_ARG, vmdef) < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
447
                "%s", _("Error creating command for container"));
448
        goto exit;
449 450
    }

D
Daniel Veillard 已提交
451 452
    //TODO: set quota

453
    if (virRun(conn, prog, NULL) < 0) {
D
Daniel Veillard 已提交
454
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
455
               _("Could not exec %s"), VZCTL);
456
        goto exit;
457
    }
458

459 460
    if (openvzSetDefinedUUID(strtoI(vmdef->name), vmdef->uuid) < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
461
               "%s", _("Could not set UUID"));
462 463 464
        goto exit;
    }

465
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
466
    if (dom)
467
        dom->id = -1;
D
Daniel Veillard 已提交
468

469 470 471
    for (i = 0 ; i < vmdef->nnets ; i++) {
        if (openvzDomainSetNetwork(conn, vmdef->name, vmdef->nets[i]) < 0) {
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
472
                        "%s", _("Could not configure network"));
473 474
            goto exit;
        }
D
Daniel Veillard 已提交
475 476
    }

477 478 479
    if (vmdef->vcpus > 0) {
        if (openvzDomainSetVcpus(dom, vmdef->vcpus) < 0) {
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
480
                     "%s", _("Could not set number of virtual cpu"));
481 482 483 484
             goto exit;
        }
    }

D
Daniel Veillard 已提交
485
    exit:
486
    cmdExecFree(prog);
487 488 489 490
    return dom;
}

static virDomainPtr
491
openvzDomainCreateXML(virConnectPtr conn, const char *xml,
492 493
                        unsigned int flags ATTRIBUTE_UNUSED)
{
494 495
    virDomainDefPtr vmdef = NULL;
    virDomainObjPtr vm = NULL;
496
    virDomainPtr dom = NULL;
497
    int i;
498
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
499
    const char *progstart[] = {VZCTL, "--quiet", "start", NULL, NULL};
500
    const char *progcreate[OPENVZ_MAX_ARG];
501
    progcreate[0] = NULL;
502

503 504 505 506 507 508
    if ((vmdef = virDomainDefParseString(conn, driver->caps, xml)) == NULL)
        return NULL;

    if (vmdef->os.init == NULL &&
        !(vmdef->os.init = strdup("/sbin/init"))) {
        virDomainDefFree(vmdef);
509
        return NULL;
510
    }
511

512
    vm = virDomainFindByName(&driver->domains, vmdef->name);
513
    if (vm) {
514 515 516
        openvzError(conn, VIR_ERR_OPERATION_FAILED,
                  _("Already an OPENVZ VM defined with the id '%s'"),
                  vmdef->name);
517
        virDomainDefFree(vmdef);
518 519
        return NULL;
    }
520 521
    if (!(vm = virDomainAssignDef(conn, &driver->domains, vmdef))) {
        virDomainDefFree(vmdef);
522 523 524
        return NULL;
    }

525 526
    if (openvzDomainDefineCmd(conn, progcreate, OPENVZ_MAX_ARG, vmdef) < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
527
                "%s", _("Error creating command for container"));
528
        goto exit;
529 530
    }

531
    if (virRun(conn, progcreate, NULL) < 0) {
D
Daniel Veillard 已提交
532
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
533
               _("Could not exec %s"), VZCTL);
534
        goto exit;
535
    }
536

537 538
    if (openvzSetDefinedUUID(strtoI(vmdef->name), vmdef->uuid) < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
539
               "%s", _("Could not set UUID"));
540 541 542
        goto exit;
    }

543 544 545
    for (i = 0 ; i < vmdef->nnets ; i++) {
        if (openvzDomainSetNetwork(conn, vmdef->name, vmdef->nets[i]) < 0) {
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
546
                        "%s", _("Could not configure network"));
547 548
            goto exit;
        }
D
Daniel Veillard 已提交
549 550
    }

551
    progstart[3] = vmdef->name;
552

553
    if (virRun(conn, progstart, NULL) < 0) {
D
Daniel Veillard 已提交
554
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
555
               _("Could not exec %s"), VZCTL);
556
        goto exit;
557
    }
558

559 560 561
    vm->pid = strtoI(vmdef->name);
    vm->def->id = vm->pid;
    vm->state = VIR_DOMAIN_RUNNING;
562

563
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
564
    if (dom)
565
        dom->id = vm->def->id;
566 567 568 569

    if (vmdef->vcpus > 0) {
        if (openvzDomainSetVcpus(dom, vmdef->vcpus) < 0) {
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
570
                        "%s", _("Could not set number of virtual cpu"));
571
            goto exit;
572 573 574
        }
    }

575 576
 exit:
    cmdExecFree(progcreate);
577 578 579 580 581 582
    return dom;
}

static int
openvzDomainCreate(virDomainPtr dom)
{
583
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
584
    virDomainObjPtr vm = virDomainFindByName(&driver->domains, dom->name);
585
    const char *prog[] = {VZCTL, "--quiet", "start", vm ? vm->def->name : NULL, NULL };
586 587

    if (!vm) {
D
Daniel Veillard 已提交
588
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
589
              "%s", _("no domain with matching id"));
590 591
        return -1;
    }
592

593
    if (vm->state != VIR_DOMAIN_SHUTOFF) {
D
Daniel Veillard 已提交
594
        openvzError(dom->conn, VIR_ERR_OPERATION_DENIED,
J
Jim Meyering 已提交
595
              "%s", _("domain is not in shutoff state"));
596 597 598
        return -1;
    }

599
    if (virRun(dom->conn, prog, NULL) < 0) {
D
Daniel Veillard 已提交
600
        openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
601
               _("Could not exec %s"), VZCTL);
602 603
        return -1;
    }
604

605 606 607
    vm->pid = strtoI(vm->def->name);
    vm->def->id = vm->pid;
    vm->state = VIR_DOMAIN_RUNNING;
608

609
    return 0;
610 611 612 613 614 615 616
}

static int
openvzDomainUndefine(virDomainPtr dom)
{
    virConnectPtr conn= dom->conn;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
617
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
618
    const char *prog[] = { VZCTL, "--quiet", "destroy", vm ? vm->def->name : NULL, NULL };
619 620

    if (!vm) {
J
Jim Meyering 已提交
621
        openvzError(conn, VIR_ERR_INVALID_DOMAIN, "%s", _("no domain with matching uuid"));
622 623
        return -1;
    }
624

625
    if (virDomainIsActive(vm)) {
J
Jim Meyering 已提交
626
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, "%s", _("cannot delete active domain"));
627 628
        return -1;
    }
629

630
    if (virRun(conn, prog, NULL) < 0) {
D
Daniel Veillard 已提交
631
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
632
               _("Could not exec %s"), VZCTL);
633 634
        return -1;
    }
635

636 637
    virDomainRemoveInactive(&driver->domains, vm);

638
    return 0;
639 640
}

641 642 643 644 645
static int
openvzDomainSetAutostart(virDomainPtr dom, int autostart)
{
    virConnectPtr conn= dom->conn;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
646
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
647
    const char *prog[] = { VZCTL, "--quiet", "set", vm ? vm->def->name : NULL,
D
Daniel Veillard 已提交
648
                           "--onboot", autostart ? "yes" : "no",
649 650 651
                           "--save", NULL };

    if (!vm) {
J
Jim Meyering 已提交
652
        openvzError(conn, VIR_ERR_INVALID_DOMAIN, "%s", _("no domain with matching uuid"));
653 654 655
        return -1;
    }

656
    if (virRun(conn, prog, NULL) < 0) {
D
Daniel Veillard 已提交
657
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, _("Could not exec %s"), VZCTL);
658 659 660 661 662 663 664 665 666 667 668
        return -1;
    }

    return 0;
}

static int
openvzDomainGetAutostart(virDomainPtr dom, int *autostart)
{
    virConnectPtr conn= dom->conn;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
669
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
670 671 672
    char value[1024];

    if (!vm) {
J
Jim Meyering 已提交
673
        openvzError(conn, VIR_ERR_INVALID_DOMAIN, "%s", _("no domain with matching uuid"));
674 675 676
        return -1;
    }

677
    if (openvzReadConfigParam(strtoI(vm->def->name), "ONBOOT", value, sizeof(value)) < 0) {
J
Jim Meyering 已提交
678
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, "%s", _("Could not read container config"));
679 680 681 682 683
        return -1;
    }

    *autostart = 0;
    if (STREQ(value,"yes"))
D
Daniel Veillard 已提交
684
        *autostart = 1;
685 686 687 688

    return 0;
}

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
static int openvzGetMaxVCPUs(virConnectPtr conn, const char *type) {
    if (STRCASEEQ(type, "openvz"))
        return 1028; //OpenVZ has no limitation

    openvzError(conn, VIR_ERR_INVALID_ARG,
                     _("unknown type '%s'"), type);
    return -1;
}


static int openvzDomainGetMaxVcpus(virDomainPtr dom) {
    return openvzGetMaxVCPUs(dom->conn, "openvz");
}

static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
    virConnectPtr conn= dom->conn;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
706
    virDomainObjPtr vm = virDomainFindByUUID(&driver->domains, dom->uuid);
707
    char   str_vcpus[32];
708
    const char *prog[] = { VZCTL, "--quiet", "set", vm ? vm->def->name : NULL,
709 710 711 712 713
                           "--cpus", str_vcpus, "--save", NULL };


    if (!vm) {
        openvzError(conn, VIR_ERR_INVALID_DOMAIN,
J
Jim Meyering 已提交
714
                    "%s", _("no domain with matching uuid"));
715 716 717
        return -1;
    }

718 719
    if (nvcpus <= 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
720
                    "%s", _("VCPUs should be >= 1"));
721 722 723 724 725 726
        return -1;
    }

    snprintf(str_vcpus, 31, "%d", nvcpus);
    str_vcpus[31] = '\0';

727 728
    if (virRun(conn, prog, NULL) < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
729
                    _("Could not exec %s"), VZCTL);
730 731 732
        return -1;
    }

733
    vm->def->vcpus = nvcpus;
734 735 736
    return 0;
}

737 738 739
static const char *openvzProbe(void)
{
#ifdef __linux__
740
    if ((geteuid() == 0) && (virFileExists("/proc/vz")))
D
Daniel Veillard 已提交
741
        return("openvz:///system");
742 743 744 745
#endif
    return(NULL);
}

746
static virDrvOpenStatus openvzOpen(virConnectPtr conn,
747 748 749 750
                                 xmlURIPtr uri,
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                 int flags ATTRIBUTE_UNUSED)
{
751
    struct openvz_driver *driver;
752
    /*Just check if the user is root. Nothing really to open for OpenVZ */
753 754 755 756 757 758 759 760 761 762
    if (geteuid()) { // OpenVZ tools can only be used by r00t
        return VIR_DRV_OPEN_DECLINED;
    } else {
        if (uri == NULL ||
            uri->scheme == NULL ||
            uri->path == NULL ||
            STRNEQ (uri->scheme, "openvz") ||
            STRNEQ (uri->path, "/system"))
            return VIR_DRV_OPEN_DECLINED;
    }
763
    /* See if we are running an OpenVZ enabled kernel */
764 765 766 767 768 769 770 771 772 773 774 775
    if(access("/proc/vz/veinfo", F_OK) == -1 ||
       access("/proc/user_beancounters", F_OK) == -1) {
        return VIR_DRV_OPEN_DECLINED;
    }

    if (VIR_ALLOC(driver) < 0) {
        openvzError(conn, VIR_ERR_NO_MEMORY, NULL);
        return VIR_DRV_OPEN_ERROR;
    }

    if (!(driver->caps = openvzCapsInit()))
        goto cleanup;
776

777 778
    if (openvzLoadDomains(driver) < 0)
        goto cleanup;
779

780
    conn->privateData = driver;
781

782
    return VIR_DRV_OPEN_SUCCESS;
783

784 785 786
cleanup:
    openvzFreeDriver(driver);
    return VIR_DRV_OPEN_ERROR;
787
};
788 789 790

static int openvzClose(virConnectPtr conn) {
    struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;
791

792
    openvzFreeDriver(driver);
793 794 795 796 797 798 799 800 801
    conn->privateData = NULL;

    return 0;
}

static const char *openvzGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
    return strdup("OpenVZ");
}

802 803 804 805 806
static int openvzGetNodeInfo(virConnectPtr conn,
                             virNodeInfoPtr nodeinfo) {
    return virNodeInfoPopulate(conn, nodeinfo);
}

807 808 809 810 811 812
static char *openvzGetCapabilities(virConnectPtr conn) {
    struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;

    return virCapabilitiesFormatXML(driver->caps);
}

813 814
static int openvzListDomains(virConnectPtr conn, int *ids, int nids) {
    int got = 0;
815 816 817
    int veid, pid;
    int outfd = -1;
    int errfd = -1;
818 819
    int ret;
    char buf[32];
820
    char *endptr;
821
    const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
822

823 824
    ret = virExec(conn, cmd, NULL, NULL,
                  &pid, -1, &outfd, &errfd, VIR_EXEC_NONE);
825
    if(ret == -1) {
D
Daniel Veillard 已提交
826
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
827
               _("Could not exec %s"), VZLIST);
D
Daniel P. Berrange 已提交
828
        return -1;
829 830
    }

831 832 833
    while(got < nids){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
834 835 836 837 838
        if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) {
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                    _("Could not parse VPS ID %s"), buf);
            continue;
        }
839 840 841
        ids[got] = veid;
        got ++;
    }
842
    waitpid(pid, NULL, 0);
843 844 845 846

    return got;
}

847
static int openvzNumDomains(virConnectPtr conn) {
848
    struct openvz_driver *driver = conn->privateData;
849 850 851 852
    int nactive = 0, i;

    for (i = 0 ; i < driver->domains.count ; i++)
        if (virDomainIsActive(driver->domains.objs[i]))
853
            nactive++;
854

855
    return nactive;
856 857 858
}

static int openvzListDefinedDomains(virConnectPtr conn,
859
                                    char **const names, int nnames) {
860
    int got = 0;
861
    int veid, pid, outfd = -1, errfd = -1, ret;
862
    char vpsname[32];
863
    char buf[32];
864
    char *endptr;
865
    const char *cmd[] = {VZLIST, "-ovpsid", "-H", "-S", NULL};
866 867

    /* the -S options lists only stopped domains */
868 869
    ret = virExec(conn, cmd, NULL, NULL,
                  &pid, -1, &outfd, &errfd, VIR_EXEC_NONE);
870
    if(ret == -1) {
D
Daniel Veillard 已提交
871
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
872
               _("Could not exec %s"), VZLIST);
D
Daniel P. Berrange 已提交
873
        return -1;
874 875
    }

876 877 878
    while(got < nnames){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
879 880 881 882 883
        if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) {
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                    _("Could not parse VPS ID %s"), buf);
            continue;
        }
884 885 886
        snprintf(vpsname, sizeof(vpsname), "%d", veid);
        if (!(names[got] = strdup(vpsname)))
            goto no_memory;
887 888
        got ++;
    }
889
    waitpid(pid, NULL, 0);
890
    return got;
891 892 893 894 895 896

no_memory:
    openvzError(conn, VIR_ERR_NO_MEMORY, NULL);
    for ( ; got >= 0 ; got--)
        VIR_FREE(names[got]);
    return -1;
897 898
}

D
Daniel Veillard 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 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
static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid) {
    int fd;
    char line[1024] ;
    unsigned long long usertime, systime, nicetime;
    int readvps = 0, ret;

/* read statistic from /proc/vz/vestat.
sample:
Version: 2.2
      VEID     user      nice     system     uptime                 idle   other..
        33       78         0       1330   59454597      142650441835148   other..
        55      178         0       5340   59424597      542650441835148   other..
*/

    if ((fd = open("/proc/vz/vestat", O_RDONLY)) == -1)
        return -1;

    /*search line with VEID=vpsid*/
    while(1) {
        ret = openvz_readline(fd, line, sizeof(line));
        if(ret <= 0)
            break;

        if (sscanf(line, "%d %llu %llu %llu",
                          &readvps, &usertime, &nicetime, &systime) != 4)
            continue;

        if (readvps == vpsid)
            break; /*found vpsid*/
    }

    close(fd);
    if (ret < 0)
        return -1;

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

    /* convert jiffies to nanoseconds */
    *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + nicetime  + systime)
                                     / (unsigned long long)sysconf(_SC_CLK_TCK);

    return 0;
}

944 945
static int openvzNumDefinedDomains(virConnectPtr conn) {
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
946 947 948 949
    int ninactive = 0, i;

    for (i = 0 ; i < driver->domains.count ; i++)
        if (!virDomainIsActive(driver->domains.objs[i]))
950
            ninactive++;
951

952
    return ninactive;
953 954 955 956 957 958
}

static virDriver openvzDriver = {
    VIR_DRV_OPENVZ,
    "OPENVZ",
    LIBVIR_VERSION_NUMBER,
959
    openvzProbe, /* probe */
960 961
    openvzOpen, /* open */
    openvzClose, /* close */
962
    NULL, /* supports_feature */
963 964 965 966
    openvzGetType, /* type */
    NULL, /* version */
    NULL, /* hostname */
    NULL, /* uri */
967
    openvzGetMaxVCPUs, /* getMaxVcpus */
968
    openvzGetNodeInfo, /* nodeGetInfo */
969
    openvzGetCapabilities, /* getCapabilities */
970 971
    openvzListDomains, /* listDomains */
    openvzNumDomains, /* numOfDomains */
972
    openvzDomainCreateXML, /* domainCreateXML */
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
    openvzDomainLookupByID, /* domainLookupByID */
    openvzDomainLookupByUUID, /* domainLookupByUUID */
    openvzDomainLookupByName, /* domainLookupByName */
    NULL, /* domainSuspend */
    NULL, /* domainResume */
    openvzDomainShutdown, /* domainShutdown */
    openvzDomainReboot, /* domainReboot */
    openvzDomainShutdown, /* domainDestroy */
    openvzGetOSType, /* domainGetOSType */
    NULL, /* domainGetMaxMemory */
    NULL, /* domainSetMaxMemory */
    NULL, /* domainSetMemory */
    openvzDomainGetInfo, /* domainGetInfo */
    NULL, /* domainSave */
    NULL, /* domainRestore */
    NULL, /* domainCoreDump */
989
    openvzDomainSetVcpus, /* domainSetVcpus */
990 991
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
992
    openvzDomainGetMaxVcpus, /* domainGetMaxVcpus */
993
    openvzDomainDumpXML, /* domainDumpXML */
994 995 996
    openvzListDefinedDomains, /* listDomains */
    openvzNumDefinedDomains, /* numOfDomains */
    openvzDomainCreate, /* domainCreate */
997 998
    openvzDomainDefineXML, /* domainDefineXML */
    openvzDomainUndefine, /* domainUndefine */
999 1000
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
1001 1002
    openvzDomainGetAutostart, /* domainGetAutostart */
    openvzDomainSetAutostart, /* domainSetAutostart */
1003 1004 1005
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
1006 1007 1008 1009 1010
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
D
Daniel P. Berrange 已提交
1011 1012
    NULL, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
1013
    NULL, /* nodeGetCellsFreeMemory */
1014
    NULL, /* nodeGetFreeMemory */
1015 1016
    NULL, /* domainEventRegister */
    NULL, /* domainEventDeregister */
1017 1018 1019 1020 1021 1022 1023
};

int openvzRegister(void) {
    virRegisterDriver(&openvzDriver);
    return 0;
}