openvz_driver.c 22.7 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 50 51
 */

#ifdef WITH_OPENVZ

#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>

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

60 61 62
#define OPENVZ_MAX_ARG 28
#define CMDBUF_LEN 1488
#define CMDOP_LEN 288
63 64 65

static virDomainPtr openvzDomainLookupByID(virConnectPtr conn, int id);
static char *openvzGetOSType(virDomainPtr dom);
66
static int openvzGetNodeInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo);
67 68 69 70 71 72
static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid);
static virDomainPtr openvzDomainLookupByName(virConnectPtr conn, const char *name);
static int openvzDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info);
static int openvzDomainShutdown(virDomainPtr dom);
static int openvzDomainReboot(virDomainPtr dom, unsigned int flags);
static int openvzDomainCreate(virDomainPtr dom);
73 74 75 76
static virDrvOpenStatus openvzOpen(virConnectPtr conn,
                                 xmlURIPtr uri,
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                 int flags ATTRIBUTE_UNUSED);
77 78 79 80 81 82 83 84 85 86
static int openvzClose(virConnectPtr conn);
static const char *openvzGetType(virConnectPtr conn ATTRIBUTE_UNUSED);
static int openvzListDomains(virConnectPtr conn, int *ids, int nids);
static int openvzNumDomains(virConnectPtr conn);
static int openvzListDefinedDomains(virConnectPtr conn, char **const names, int nnames);
static int openvzNumDefinedDomains(virConnectPtr conn);
static int openvzStartup(void);
static int openvzShutdown(void);
static int openvzReload(void);
static int openvzActive(void);
87 88

static virDomainPtr openvzDomainDefineXML(virConnectPtr conn, const char *xml);
89
static virDomainPtr openvzDomainCreateLinux(virConnectPtr conn, const char *xml,
90 91 92 93 94 95
        unsigned int flags ATTRIBUTE_UNUSED);

static int openvzDomainUndefine(virDomainPtr dom);
static int convCmdbufExec(char cmdbuf[], char *cmdExec[]);
static void cmdExecFree(char *cmdExec[]);

96 97
struct openvz_driver ovz_driver;

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static int convCmdbufExec(char cmdbuf[], char *cmdExec[])
{
    int i=0, limit = OPENVZ_MAX_ARG - 1;
    char cmdWord[CMDOP_LEN];
    while(*cmdbuf)
    {
        if(i >= limit)
        {
            cmdExec[i] = NULL;
            return -1;
        }
        sscanf(cmdbuf, "%s", cmdWord);
        cmdbuf += strlen(cmdWord);
        while(*cmdbuf == ' ') cmdbuf++;
        cmdExec[i++] = strdup(cmdWord);
    }
    cmdExec[i] = NULL;
    return i;
}

static void cmdExecFree(char *cmdExec[])
{
    int i=-1;
    while(cmdExec[++i])
    {
        free(cmdExec[i]);
        cmdExec[i] = NULL;
    }
}

128 129 130 131 132 133 134
/* For errors internal to this library. */
static void
error (virConnectPtr conn, virErrorNumber code, const char *info)
{
    const char *errmsg;

    errmsg = __virErrorMsg (code, info);
135
    __virRaiseError (conn, NULL, NULL, VIR_FROM_OPENVZ,
136 137 138 139 140 141 142 143 144 145 146
                     code, VIR_ERR_ERROR, errmsg, info, NULL, 0, 0,
                     errmsg, info);
}

static virDomainPtr openvzDomainLookupByID(virConnectPtr conn,
                                   int id) {
    struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;
    struct openvz_vm *vm = openvzFindVMByID(driver, id);
    virDomainPtr dom;

    if (!vm) {
147
        error(conn, VIR_ERR_INTERNAL_ERROR, _("no domain with matching id"));
148 149 150 151 152 153 154 155 156 157 158 159 160
        return NULL;
    }

    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (!dom) {
        error(conn, VIR_ERR_NO_MEMORY, "virDomainPtr");
        return NULL;
    }

    dom->id = vm->vpsid;
    return dom;
}

161
static char *openvzGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED)
162 163
{
    /* OpenVZ runs on Linux and runs only Linux */
164
    return strdup("linux");
165 166 167 168 169 170 171 172 173 174
}


static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn,
                                     const unsigned char *uuid) {
    struct  openvz_driver *driver = (struct openvz_driver *)conn->privateData;
    struct openvz_vm *vm = openvzFindVMByUUID(driver, uuid);
    virDomainPtr dom;

    if (!vm) {
175
        error(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid"));
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        return NULL;
    }

    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (!dom) {
        error(conn, VIR_ERR_NO_MEMORY, "virDomainPtr");
        return NULL;
    }

    dom->id = vm->vpsid;
    return dom;
}

static virDomainPtr openvzDomainLookupByName(virConnectPtr conn,
                                     const char *name) {
    struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;
    struct openvz_vm *vm = openvzFindVMByName(driver, name);
    virDomainPtr dom;

    if (!vm) {
196
        error(conn, VIR_ERR_INTERNAL_ERROR, _("no domain with matching name"));
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        return NULL;
    }

    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (!dom) {
        error(conn, VIR_ERR_NO_MEMORY, "virDomainPtr");
        return NULL;
    }

    dom->id = vm->vpsid;
    return dom;
}

static int openvzDomainGetInfo(virDomainPtr dom,
                       virDomainInfoPtr info) {
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
    struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid);
214

215
    if (!vm) {
216 217
        error(dom->conn, VIR_ERR_INVALID_DOMAIN,
              _("no domain with matching uuid"));
218 219 220 221 222 223
        return -1;
    }

    info->state = vm->status;

    /* TODO These need to be calculated differently for OpenVZ */
224
    //info->cpuTime =
225 226 227 228 229 230 231
    //info->maxMem = vm->def->maxmem;
    //info->memory = vm->def->memory;
    //info->nrVirtCpu = vm->def->vcpus;
    return 0;
}

static int openvzDomainShutdown(virDomainPtr dom) {
232
    char cmdbuf[CMDBUF_LEN];
233
    int ret;
234 235
    char *cmdExec[OPENVZ_MAX_ARG];
    int pid, outfd, errfd;
236 237 238 239
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
    struct openvz_vm *vm = openvzFindVMByID(driver, dom->id);

    if (!vm) {
240 241
        error(dom->conn, VIR_ERR_INVALID_DOMAIN,
              _("no domain with matching id"));
242 243
        return -1;
    }
244

245
    if (vm->status != VIR_DOMAIN_RUNNING) {
246 247
        error(dom->conn, VIR_ERR_OPERATION_DENIED,
              _("domain is not in running state"));
248 249
        return -1;
    }
250
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " stop %d ", dom->id);
251 252

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
253
    {
254
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
255
        goto bail_out;
256 257
    }

258 259 260
    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(dom->conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
261 262
        return -1;
    }
263

264 265 266 267
    vm->vpsid = -1;
    vm->status = VIR_DOMAIN_SHUTOFF;
    ovz_driver.num_inactive ++;
    ovz_driver.num_active --;
268

269 270 271
bail_out:
    cmdExecFree(cmdExec);

272 273 274
    return ret;
}

275 276
static int openvzDomainReboot(virDomainPtr dom,
                              unsigned int flags ATTRIBUTE_UNUSED) {
277
    char cmdbuf[CMDBUF_LEN];
278
    int ret;
279 280
    char *cmdExec[OPENVZ_MAX_ARG];
    int pid, outfd, errfd;
281 282 283 284
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
    struct openvz_vm *vm = openvzFindVMByID(driver, dom->id);

    if (!vm) {
285 286
        error(dom->conn, VIR_ERR_INVALID_DOMAIN,
              _("no domain with matching id"));
287 288
        return -1;
    }
289

290
    if (vm->status != VIR_DOMAIN_RUNNING) {
291 292
        error(dom->conn, VIR_ERR_OPERATION_DENIED,
              _("domain is not in running state"));
293 294
        return -1;
    }
295
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " restart %d ", dom->id);
296 297

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
298
    {
299
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
300 301 302 303 304
        goto bail_out1;
    }
    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(dom->conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
305 306
        return -1;
    }
307 308

bail_out1:
309
    cmdExecFree(cmdExec);
310

311 312 313
    return ret;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static virDomainPtr
openvzDomainDefineXML(virConnectPtr conn, const char *xml)
{
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
    struct openvz_vm_def *vmdef = NULL;
    struct openvz_vm *vm = NULL;
    virDomainPtr dom;
    char cmdbuf[CMDBUF_LEN], cmdOption[CMDOP_LEN], *cmdExec[OPENVZ_MAX_ARG];
    int ret, pid, outfd, errfd;

    if (!(vmdef = openvzParseVMDef(conn, xml, NULL)))
        goto bail_out2;

    vm = openvzFindVMByID(driver, strtoI(vmdef->name));
    if (vm) {
329 330
        openvzLog(OPENVZ_ERR, _("Already an OPENVZ VM active with the id '%s'"),
                  vmdef->name);
331 332 333 334
        goto bail_out2;
    }
    if (!(vm = openvzAssignVMDef(conn, driver, vmdef))) {
        openvzFreeVMDef(vmdef);
335
        openvzLog(OPENVZ_ERR, "%s", _("Error creating OPENVZ VM"));
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    }

    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " create %s", vmdef->name);
    if ((vmdef->fs.tmpl && *(vmdef->fs.tmpl))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --ostemplate %s", vmdef->fs.tmpl);
        strcat(cmdbuf, cmdOption);
    }
    if ((vmdef->profile && *(vmdef->profile))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --config %s", vmdef->profile);
        strcat(cmdbuf, cmdOption);
    }
    if ((vmdef->net.ips->ip && *(vmdef->net.ips->ip))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --ipadd %s", vmdef->net.ips->ip);
        strcat(cmdbuf, cmdOption);
    }
    if ((vmdef->net.hostname && *(vmdef->net.hostname))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --hostname %s", vmdef->net.hostname);
        strcat(cmdbuf, cmdOption);
    }

356
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
357
    {
358
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
359 360 361 362 363 364 365
        goto bail_out2;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
        goto bail_out2;
    }
366

367 368
    waitpid(pid, NULL, 0);
    cmdExecFree(cmdExec);
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (dom)
        dom->id = vm->vpsid;
    return dom;
bail_out2:
    cmdExecFree(cmdExec);
    return NULL;
}

static virDomainPtr
openvzDomainCreateLinux(virConnectPtr conn, const char *xml,
                        unsigned int flags ATTRIBUTE_UNUSED)
{
    struct openvz_vm_def *vmdef = NULL;
    struct openvz_vm *vm = NULL;
    virDomainPtr dom;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
    char cmdbuf[CMDBUF_LEN], cmdOption[CMDOP_LEN], *cmdExec[OPENVZ_MAX_ARG];
    int ret, pid, outfd, errfd;

    if (!(vmdef = openvzParseVMDef(conn, xml, NULL)))
        return NULL;

    vm = openvzFindVMByID(driver, strtoI(vmdef->name));
    if (vm) {
        openvzFreeVMDef(vmdef);
396 397
        openvzLog(OPENVZ_ERR,
                  _("Already an OPENVZ VM defined with the id '%d'"),
398 399 400 401
                strtoI(vmdef->name));
        return NULL;
    }
    if (!(vm = openvzAssignVMDef(conn, driver, vmdef))) {
402
        openvzLog(OPENVZ_ERR, "%s", _("Error creating OPENVZ VM"));
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
        return NULL;
    }

    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " create %s", vmdef->name);
    if ((vmdef->fs.tmpl && *(vmdef->fs.tmpl))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --ostemplate %s", vmdef->fs.tmpl);
        strcat(cmdbuf, cmdOption);
    }
    if ((vmdef->profile && *(vmdef->profile))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --config %s", vmdef->profile);
        strcat(cmdbuf, cmdOption);
    }
    if ((vmdef->net.ips->ip && *(vmdef->net.ips->ip))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --ipadd %s", vmdef->net.ips->ip);
        strcat(cmdbuf, cmdOption);
    }
    if ((vmdef->net.hostname && *(vmdef->net.hostname))) {
        snprintf(cmdOption, CMDOP_LEN - 1, " --hostname %s", vmdef->net.hostname);
        strcat(cmdbuf, cmdOption);
    }

424
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
425
    {
426
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
427 428 429 430 431 432 433
        goto bail_out3;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
        return NULL;
    }
434

435 436 437 438 439
    waitpid(pid, NULL, 0);
    cmdExecFree(cmdExec);

    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " start %s ", vmdef->name);

440
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
441
    {
442
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
443 444 445 446 447 448 449
        goto bail_out3;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
        return NULL;
    }
450

451 452 453 454 455 456 457 458 459 460 461 462 463
    sscanf(vmdef->name, "%d", &vm->vpsid);
    vm->status = VIR_DOMAIN_RUNNING;
    ovz_driver.num_inactive--;
    ovz_driver.num_active++;

    waitpid(pid, NULL, 0);
    cmdExecFree(cmdExec);

    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (dom)
        dom->id = vm->vpsid;
    return dom;
bail_out3:
464
    cmdExecFree(cmdExec);
465 466 467 468 469 470 471
    return NULL;
}

static int
openvzDomainCreate(virDomainPtr dom)
{
    char cmdbuf[CMDBUF_LEN];
472
    int ret;
473 474
    char *cmdExec[OPENVZ_MAX_ARG] ;
    int pid, outfd, errfd;
475
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
476
    struct openvz_vm *vm = openvzFindVMByName(driver, dom->name);
477 478 479
    struct openvz_vm_def *vmdef;

    if (!vm) {
480 481
        error(dom->conn, VIR_ERR_INVALID_DOMAIN,
              _("no domain with matching id"));
482 483
        return -1;
    }
484

485
    if (vm->status != VIR_DOMAIN_SHUTOFF) {
486 487
        error(dom->conn, VIR_ERR_OPERATION_DENIED,
              _("domain is not in shutoff state"));
488 489 490 491
        return -1;
    }

    vmdef = vm->vmdef;
492
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " start %s ", vmdef->name);
493 494

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
495
    {
496
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
497 498 499 500 501
        goto bail_out4;
    }
    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(dom->conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
502 503
        return -1;
    }
504 505

    sscanf(vmdef->name, "%d", &vm->vpsid);
506 507 508
    vm->status = VIR_DOMAIN_RUNNING;
    ovz_driver.num_inactive --;
    ovz_driver.num_active ++;
509

510
    waitpid(pid, NULL, 0);
511
bail_out4:
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
    cmdExecFree(cmdExec);

    return ret;
}

static int
openvzDomainUndefine(virDomainPtr dom)
{
    char cmdbuf[CMDBUF_LEN], *cmdExec[OPENVZ_MAX_ARG];
    int ret, pid, outfd, errfd;
    virConnectPtr conn= dom->conn;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
    struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid);

    if (!vm) {
527
        error(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid"));
528 529
        return -1;
    }
530

531
    if (openvzIsActiveVM(vm)) {
532
        error(conn, VIR_ERR_INTERNAL_ERROR, _("cannot delete active domain"));
533 534 535
        return -1;
    }
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " destroy %s ", vm->vmdef->name);
536 537

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
538
    {
539
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
540 541 542 543 544 545 546
        goto bail_out5;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
        return -1;
    }
547

548 549
    waitpid(pid, NULL, 0);
    openvzRemoveInactiveVM(driver, vm);
550
bail_out5:
551
    cmdExecFree(cmdExec);
552 553 554
    return ret;
}

555 556 557 558 559 560 561 562 563
static const char *openvzProbe(void)
{
#ifdef __linux__
    if ((getuid() == 0) && (virFileExists("/proc/vz")))
        return("openvz:///");
#endif
    return(NULL);
}

564
static virDrvOpenStatus openvzOpen(virConnectPtr conn,
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
                                 xmlURIPtr uri,
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                 int flags ATTRIBUTE_UNUSED)
{
   struct openvz_vm *vms;

    /*Just check if the user is root. Nothing really to open for OpenVZ */
   if (getuid()) { // OpenVZ tools can only be used by r00t
           return VIR_DRV_OPEN_DECLINED;
   } else {
       if (uri == NULL || uri->scheme == NULL || uri->path == NULL)
                   return VIR_DRV_OPEN_DECLINED;
       if (STRNEQ (uri->scheme, "openvz"))
                   return VIR_DRV_OPEN_DECLINED;
       if (STRNEQ (uri->path, "/system"))
                   return VIR_DRV_OPEN_DECLINED;
   }
582
    /* See if we are running an OpenVZ enabled kernel */
583 584 585 586
   if(access("/proc/vz/veinfo", F_OK) == -1 ||
               access("/proc/user_beancounters", F_OK) == -1) {
       return VIR_DRV_OPEN_DECLINED;
   }
587

588
   conn->privateData = &ovz_driver;
589

590 591 592
   virStateInitialize();
   vms = openvzGetVPSInfo(conn);
   ovz_driver.vms = vms;
593

594 595
   return VIR_DRV_OPEN_SUCCESS;
};
596 597

static int openvzClose(virConnectPtr conn) {
598

599 600 601 602 603 604 605 606 607 608 609 610 611
    struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;
    struct openvz_vm *vm = driver->vms;

    while(vm) {
        openvzFreeVMDef(vm->vmdef);
        vm = vm->next;
    }
    vm = driver->vms;
    while (vm) {
        struct openvz_vm *prev = vm;
        vm = vm->next;
        free(prev);
    }
612

613 614 615 616 617 618 619 620 621
    conn->privateData = NULL;

    return 0;
}

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

622 623 624 625 626
static int openvzGetNodeInfo(virConnectPtr conn,
                             virNodeInfoPtr nodeinfo) {
    return virNodeInfoPopulate(conn, nodeinfo);
}

627 628
static int openvzListDomains(virConnectPtr conn, int *ids, int nids) {
    int got = 0;
629 630 631 632
    int veid, pid, outfd, errfd;
    int ret;
    char buf[32];
    const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
633

634
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
635 636
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
D
Daniel P. Berrange 已提交
637
        return -1;
638 639
    }

640 641 642 643
    while(got < nids){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d", &veid);
644 645 646
        ids[got] = veid;
        got ++;
    }
647
    waitpid(pid, NULL, 0);
648 649 650 651

    return got;
}

652
static int openvzNumDomains(virConnectPtr conn ATTRIBUTE_UNUSED) {
653 654 655 656 657 658
    return ovz_driver.num_active;
}

static int openvzListDefinedDomains(virConnectPtr conn,
                            char **const names, int nnames) {
    int got = 0;
659
    int veid, pid, outfd, errfd, ret;
660
    char vpsname[OPENVZ_NAME_MAX];
661
    char buf[32];
662
    const char *cmd[] = {VZLIST, "-ovpsid", "-H", "-S", NULL};
663 664

    /* the -S options lists only stopped domains */
665
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
666 667
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
D
Daniel P. Berrange 已提交
668
        return -1;
669 670
    }

671 672 673 674
    while(got < nnames){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d\n", &veid);
675 676 677 678
        sprintf(vpsname, "%d", veid);
        names[got] = strdup(vpsname);
        got ++;
    }
679
    waitpid(pid, NULL, 0);
680 681 682
    return got;
}

683
static int openvzNumDefinedDomains(virConnectPtr conn ATTRIBUTE_UNUSED) {
684
    return ovz_driver.num_inactive;
685 686 687 688
}

static int openvzStartup(void) {
    openvzAssignUUIDs();
689

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    return 0;
}

static int openvzShutdown(void) {

    return 0;
}

static int openvzReload(void) {

    return 0;
}

static int openvzActive(void) {

    return 1;
}

static virDriver openvzDriver = {
    VIR_DRV_OPENVZ,
    "OPENVZ",
    LIBVIR_VERSION_NUMBER,
712
    openvzProbe, /* probe */
713 714
    openvzOpen, /* open */
    openvzClose, /* close */
715
    NULL, /* supports_feature */
716 717 718 719 720
    openvzGetType, /* type */
    NULL, /* version */
    NULL, /* hostname */
    NULL, /* uri */
    NULL, /* getMaxVcpus */
721
    openvzGetNodeInfo, /* nodeGetInfo */
722 723 724
    NULL, /* getCapabilities */
    openvzListDomains, /* listDomains */
    openvzNumDomains, /* numOfDomains */
725
    openvzDomainCreateLinux, /* domainCreateLinux */
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    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 */
    NULL, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    NULL, /* domainGetMaxVcpus */
    NULL, /* domainDumpXML */
    openvzListDefinedDomains, /* listDomains */
    openvzNumDefinedDomains, /* numOfDomains */
    openvzDomainCreate, /* domainCreate */
750 751
    openvzDomainDefineXML, /* domainDefineXML */
    openvzDomainUndefine, /* domainUndefine */
752 753 754 755 756 757 758
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
759 760 761 762 763
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
D
Daniel P. Berrange 已提交
764 765
    NULL, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
766
    NULL, /* nodeGetCellsFreeMemory */
767
    NULL, /* nodeGetFreeMemory */
768 769 770 771 772 773 774
};

static virStateDriver openvzStateDriver = {
    openvzStartup,
    openvzShutdown,
    openvzReload,
    openvzActive,
D
Daniel P. Berrange 已提交
775
    NULL, /* sigHandler */
776 777 778 779 780 781 782 783 784
};

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

#endif /* WITH_OPENVZ */