openvz_driver.c 22.5 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 23 24 25
 * Authors: 
 * 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 52
 */

#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 <ctype.h>
#include <pwd.h>
#include <stdio.h>
#include <sys/wait.h>

53
#include "libvirt/virterror.h"
54

55
#include "openvz_driver.h"
56 57
#include "event.h"
#include "buf.h"
58
#include "util.h"
59
#include "openvz_conf.h"
60
#include "nodeinfo.h"
61

62 63 64
#define OPENVZ_MAX_ARG 28
#define CMDBUF_LEN 1488
#define CMDOP_LEN 288
65 66 67

static virDomainPtr openvzDomainLookupByID(virConnectPtr conn, int id);
static char *openvzGetOSType(virDomainPtr dom);
68
static int openvzGetNodeInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo);
69 70 71 72 73 74
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);
75 76 77 78
static virDrvOpenStatus openvzOpen(virConnectPtr conn,
                                 xmlURIPtr uri,
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                 int flags ATTRIBUTE_UNUSED);
79 80 81 82 83 84 85 86 87 88
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);
89 90 91 92 93 94 95 96 97

static virDomainPtr openvzDomainDefineXML(virConnectPtr conn, const char *xml);
static virDomainPtr openvzDomainCreateLinux(virConnectPtr conn, const char *xml, 
        unsigned int flags ATTRIBUTE_UNUSED);

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

98 99
struct openvz_driver ovz_driver;

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 128 129
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;
    }
}

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

    errmsg = __virErrorMsg (code, info);
137
    __virRaiseError (conn, NULL, NULL, VIR_FROM_OPENVZ,
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                     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) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "no domain with matching id");
        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;
}

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


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) {
177
        error(conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        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) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "no domain with matching name");
        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);
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    if (!vm) {
        error(dom->conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
        return -1;
    }

    info->state = vm->status;

    /* TODO These need to be calculated differently for OpenVZ */
    //info->cpuTime = 
    //info->maxMem = vm->def->maxmem;
    //info->memory = vm->def->memory;
    //info->nrVirtCpu = vm->def->vcpus;
    return 0;
}

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

    if (!vm) {
        error(dom->conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching id");
        return -1;
    }
    
    if (vm->status != VIR_DOMAIN_RUNNING) {
        error(dom->conn, VIR_ERR_OPERATION_DENIED, "domain is not in running state");
        return -1;
    }
249 250 251 252 253 254 255 256 257 258 259
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " stop %d ", dom->id);
    
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1) 
    {
        openvzLog(OPENVZ_ERR, "Error in parsing Options to OPENVZ");
        goto bail_out;
    }    
        
    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
        error(dom->conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
260 261
        return -1;
    }
262
    
263 264 265 266 267
    vm->vpsid = -1;
    vm->status = VIR_DOMAIN_SHUTOFF;
    ovz_driver.num_inactive ++;
    ovz_driver.num_active --;
    
268 269 270
bail_out:
    cmdExecFree(cmdExec);

271 272 273
    return ret;
}

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

    if (!vm) {
        error(dom->conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching id");
        return -1;
    }
    
    if (vm->status != VIR_DOMAIN_RUNNING) {
        error(dom->conn, VIR_ERR_OPERATION_DENIED, "domain is not in running state");
        return -1;
    }
292 293 294 295 296 297 298 299 300 301
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " restart %d ", dom->id);
    
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1) 
    {
        openvzLog(OPENVZ_ERR, "Error in parsing Options to OPENVZ");
        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);
302 303
        return -1;
    }
304 305 306
   
bail_out1:    
    cmdExecFree(cmdExec);
307 308 309 310
    
    return ret;
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
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) {
        openvzLog(OPENVZ_ERR, "Already an OPENVZ VM active with the id '%s'", 
                vmdef->name);
        goto bail_out2;
    }
    if (!(vm = openvzAssignVMDef(conn, driver, vmdef))) {
        openvzFreeVMDef(vmdef);
        openvzLog(OPENVZ_ERR, "Error creating OPENVZ VM");
    }

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

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1) 
    {
        openvzLog(OPENVZ_ERR, "Error in parsing Options to OPENVZ");
        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;
    }
    
    waitpid(pid, NULL, 0);
    cmdExecFree(cmdExec);
    
    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);
        openvzLog(OPENVZ_ERR, "Already an OPENVZ VM defined with the id '%d'", 
                strtoI(vmdef->name));
        return NULL;
    }
    if (!(vm = openvzAssignVMDef(conn, driver, vmdef))) {
        openvzLog(OPENVZ_ERR, "Error creating OPENVZ VM");
        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);
    }

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1) 
    {
        openvzLog(OPENVZ_ERR, "Error in parsing Options to OPENVZ");
        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;
    }
    
    waitpid(pid, NULL, 0);
    cmdExecFree(cmdExec);

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

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1) 
    {
        openvzLog(OPENVZ_ERR, "Error in parsing Options to OPENVZ");
        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;
    }
    
    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:
    cmdExecFree(cmdExec);    
    return NULL;
}

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

    if (!vm) {
        error(dom->conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching id");
        return -1;
    }
    
    if (vm->status != VIR_DOMAIN_SHUTOFF) {
        error(dom->conn, VIR_ERR_OPERATION_DENIED, "domain is not in shutoff state");
        return -1;
    }

    vmdef = vm->vmdef;
486 487 488 489 490 491 492 493 494 495
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " start %s ", vmdef->name);
    
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1) 
    {
        openvzLog(OPENVZ_ERR, "Error in parsing Options to OPENVZ");
        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);
496 497
        return -1;
    }
498
    
499 500 501 502
    sscanf(vmdef->name, "%d", &vm->vpsid); 
    vm->status = VIR_DOMAIN_RUNNING;
    ovz_driver.num_inactive --;
    ovz_driver.num_active ++;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    
    waitpid(pid, NULL, 0);
bail_out4:    
    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) {
        error(conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
        return -1;
    }
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    if (openvzIsActiveVM(vm)) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "cannot delete active domain");
        return -1;
    }
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " destroy %s ", vm->vmdef->name);
    
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1) 
    {
        openvzLog(OPENVZ_ERR, "Error in parsing Options to OPENVZ");
        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;
    }
    
    waitpid(pid, NULL, 0);
    openvzRemoveInactiveVM(driver, vm);
bail_out5:    
    cmdExecFree(cmdExec);
546 547 548 549
    return ret;
}

static virDrvOpenStatus openvzOpen(virConnectPtr conn,
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
                                 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;
   }
567
    /* See if we are running an OpenVZ enabled kernel */
568 569 570 571
   if(access("/proc/vz/veinfo", F_OK) == -1 ||
               access("/proc/user_beancounters", F_OK) == -1) {
       return VIR_DRV_OPEN_DECLINED;
   }
572

573
   conn->privateData = &ovz_driver;
574

575 576 577
   virStateInitialize();
   vms = openvzGetVPSInfo(conn);
   ovz_driver.vms = vms;
578

579 580
   return VIR_DRV_OPEN_SUCCESS;
};
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

static int openvzClose(virConnectPtr conn) {
    
    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);
    }
    
    conn->privateData = NULL;

    return 0;
}

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

607 608 609 610 611
static int openvzGetNodeInfo(virConnectPtr conn,
                             virNodeInfoPtr nodeinfo) {
    return virNodeInfoPopulate(conn, nodeinfo);
}

612 613
static int openvzListDomains(virConnectPtr conn, int *ids, int nids) {
    int got = 0;
614 615 616 617
    int veid, pid, outfd, errfd;
    int ret;
    char buf[32];
    const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
618

619
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
620 621
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
622 623 624
        return (int)NULL;
    }

625 626 627 628
    while(got < nids){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d", &veid);
629 630 631
        ids[got] = veid;
        got ++;
    }
632
    waitpid(pid, NULL, 0);
633 634 635 636

    return got;
}

637
static int openvzNumDomains(virConnectPtr conn ATTRIBUTE_UNUSED) {
638 639 640 641 642 643
    return ovz_driver.num_active;
}

static int openvzListDefinedDomains(virConnectPtr conn,
                            char **const names, int nnames) {
    int got = 0;
644
    int veid, pid, outfd, errfd, ret;
645
    char vpsname[OPENVZ_NAME_MAX];
646
    char buf[32];
647
    const char *cmd[] = {VZLIST, "-ovpsid", "-H", "-S", NULL};
648 649

    /* the -S options lists only stopped domains */
650
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
651 652
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
653 654 655
        return (int)NULL;
    }

656 657 658 659
    while(got < nnames){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d\n", &veid);
660 661 662 663
        sprintf(vpsname, "%d", veid);
        names[got] = strdup(vpsname);
        got ++;
    }
664
    waitpid(pid, NULL, 0);
665 666 667
    return got;
}

668
static int openvzNumDefinedDomains(virConnectPtr conn ATTRIBUTE_UNUSED) {
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
    return ovz_driver.num_inactive; 
}

static int openvzStartup(void) {
    openvzAssignUUIDs();
    
    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,
    openvzOpen, /* open */
    openvzClose, /* close */
699
    NULL, /* supports_feature */
700 701 702 703 704
    openvzGetType, /* type */
    NULL, /* version */
    NULL, /* hostname */
    NULL, /* uri */
    NULL, /* getMaxVcpus */
705
    openvzGetNodeInfo, /* nodeGetInfo */
706 707 708
    NULL, /* getCapabilities */
    openvzListDomains, /* listDomains */
    openvzNumDomains, /* numOfDomains */
709
    openvzDomainCreateLinux, /* domainCreateLinux */
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    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 */
734 735
    openvzDomainDefineXML, /* domainDefineXML */
    openvzDomainUndefine, /* domainUndefine */
736 737 738 739 740 741 742
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
743 744 745 746 747
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
748
    NULL, /* nodeGetCellsFreeMemory */
749
    NULL, /* nodeGetFreeMemory */
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
};

static virStateDriver openvzStateDriver = {
    openvzStartup,
    openvzShutdown,
    openvzReload,
    openvzActive,
};

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

#endif /* WITH_OPENVZ */

/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */