openvz_driver.c 24.1 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
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) {
D
Daniel Veillard 已提交
135
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, _("no domain with matching id"));
136 137 138 139 140
        return NULL;
    }

    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (!dom) {
D
Daniel Veillard 已提交
141
        openvzError(conn, VIR_ERR_NO_MEMORY, _("virDomainPtr"));
142 143 144 145 146 147 148
        return NULL;
    }

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

149
static char *openvzGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED)
150 151
{
    /* OpenVZ runs on Linux and runs only Linux */
152
    return strdup("linux");
153 154 155 156 157 158 159 160 161 162
}


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) {
D
Daniel Veillard 已提交
163
        openvzError(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid"));
164 165 166 167 168
        return NULL;
    }

    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (!dom) {
D
Daniel Veillard 已提交
169
        openvzError(conn, VIR_ERR_NO_MEMORY, _("virDomainPtr"));
170 171 172 173 174 175 176 177 178 179 180 181 182 183
        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) {
D
Daniel Veillard 已提交
184
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, _("no domain with matching name"));
185 186 187 188 189
        return NULL;
    }

    dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
    if (!dom) {
D
Daniel Veillard 已提交
190
        openvzError(conn, VIR_ERR_NO_MEMORY, _("virDomainPtr"));
191 192 193 194 195 196 197 198 199 200 201
        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);
202

203
    if (!vm) {
D
Daniel Veillard 已提交
204
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
205
              _("no domain with matching uuid"));
206 207 208 209 210 211
        return -1;
    }

    info->state = vm->status;

    /* TODO These need to be calculated differently for OpenVZ */
212
    //info->cpuTime =
213 214 215 216 217 218 219
    //info->maxMem = vm->def->maxmem;
    //info->memory = vm->def->memory;
    //info->nrVirtCpu = vm->def->vcpus;
    return 0;
}

static int openvzDomainShutdown(virDomainPtr dom) {
220
    char cmdbuf[CMDBUF_LEN];
221
    int ret;
222 223
    char *cmdExec[OPENVZ_MAX_ARG];
    int pid, outfd, errfd;
224 225 226 227
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
    struct openvz_vm *vm = openvzFindVMByID(driver, dom->id);

    if (!vm) {
D
Daniel Veillard 已提交
228
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
229
              _("no domain with matching id"));
230 231
        return -1;
    }
232

233
    if (vm->status != VIR_DOMAIN_RUNNING) {
D
Daniel Veillard 已提交
234
        openvzError(dom->conn, VIR_ERR_OPERATION_DENIED,
235
              _("domain is not in running state"));
236 237
        return -1;
    }
238
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " stop %d ", dom->id);
239 240

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
241
    {
242
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
243
        goto bail_out;
244 245
    }

246 247
    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
D
Daniel Veillard 已提交
248
        openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
249
              _("Could not exec %s"), VZCTL);
250 251
        return -1;
    }
252

253 254 255 256
    vm->vpsid = -1;
    vm->status = VIR_DOMAIN_SHUTOFF;
    ovz_driver.num_inactive ++;
    ovz_driver.num_active --;
257

258 259 260
bail_out:
    cmdExecFree(cmdExec);

261 262 263
    return ret;
}

264 265
static int openvzDomainReboot(virDomainPtr dom,
                              unsigned int flags ATTRIBUTE_UNUSED) {
266
    char cmdbuf[CMDBUF_LEN];
267
    int ret;
268 269
    char *cmdExec[OPENVZ_MAX_ARG];
    int pid, outfd, errfd;
270 271 272 273
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
    struct openvz_vm *vm = openvzFindVMByID(driver, dom->id);

    if (!vm) {
D
Daniel Veillard 已提交
274
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
275
              _("no domain with matching id"));
276 277
        return -1;
    }
278

279
    if (vm->status != VIR_DOMAIN_RUNNING) {
D
Daniel Veillard 已提交
280
        openvzError(dom->conn, VIR_ERR_OPERATION_DENIED,
281
              _("domain is not in running state"));
282 283
        return -1;
    }
284
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " restart %d ", dom->id);
285 286

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
287
    {
288
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
289 290 291 292
        goto bail_out1;
    }
    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
D
Daniel Veillard 已提交
293
        openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
294
               _("Could not exec %s"), VZCTL);
295 296
        return -1;
    }
297 298

bail_out1:
299
    cmdExecFree(cmdExec);
300

301 302 303
    return ret;
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
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) {
319 320
        openvzLog(OPENVZ_ERR, _("Already an OPENVZ VM active with the id '%s'"),
                  vmdef->name);
321 322 323 324
        goto bail_out2;
    }
    if (!(vm = openvzAssignVMDef(conn, driver, vmdef))) {
        openvzFreeVMDef(vmdef);
325
        openvzLog(OPENVZ_ERR, "%s", _("Error creating OPENVZ VM"));
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    }

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

346
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
347
    {
348
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
349 350 351 352
        goto bail_out2;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
D
Daniel Veillard 已提交
353
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
354
               _("Could not exec %s"), VZCTL);
355 356
        goto bail_out2;
    }
357

358 359
    waitpid(pid, NULL, 0);
    cmdExecFree(cmdExec);
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
    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);
387 388
        openvzLog(OPENVZ_ERR,
                  _("Already an OPENVZ VM defined with the id '%d'"),
389 390 391 392
                strtoI(vmdef->name));
        return NULL;
    }
    if (!(vm = openvzAssignVMDef(conn, driver, vmdef))) {
393
        openvzLog(OPENVZ_ERR, "%s", _("Error creating OPENVZ VM"));
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
        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);
    }

415
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
416
    {
417
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
418 419 420 421
        goto bail_out3;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
D
Daniel Veillard 已提交
422
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
423
               _("Could not exec %s"), VZCTL);
424 425
        return NULL;
    }
426

427 428 429 430 431
    waitpid(pid, NULL, 0);
    cmdExecFree(cmdExec);

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

432
    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
433
    {
434
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
435 436 437 438
        goto bail_out3;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
D
Daniel Veillard 已提交
439
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
440
               _("Could not exec %s"), VZCTL);
441 442
        return NULL;
    }
443

444 445 446 447 448 449 450 451 452 453 454 455 456
    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:
457
    cmdExecFree(cmdExec);
458 459 460 461 462 463 464
    return NULL;
}

static int
openvzDomainCreate(virDomainPtr dom)
{
    char cmdbuf[CMDBUF_LEN];
465
    int ret;
466 467
    char *cmdExec[OPENVZ_MAX_ARG] ;
    int pid, outfd, errfd;
468
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
469
    struct openvz_vm *vm = openvzFindVMByName(driver, dom->name);
470 471 472
    struct openvz_vm_def *vmdef;

    if (!vm) {
D
Daniel Veillard 已提交
473
        openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
474
              _("no domain with matching id"));
475 476
        return -1;
    }
477

478
    if (vm->status != VIR_DOMAIN_SHUTOFF) {
D
Daniel Veillard 已提交
479
        openvzError(dom->conn, VIR_ERR_OPERATION_DENIED,
480
              _("domain is not in shutoff state"));
481 482 483 484
        return -1;
    }

    vmdef = vm->vmdef;
485
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " start %s ", vmdef->name);
486 487

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
488
    {
489
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
490 491 492 493
        goto bail_out4;
    }
    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
D
Daniel Veillard 已提交
494
        openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
495
               _("Could not exec %s"), VZCTL);
496 497
        return -1;
    }
498 499

    sscanf(vmdef->name, "%d", &vm->vpsid);
500 501 502
    vm->status = VIR_DOMAIN_RUNNING;
    ovz_driver.num_inactive --;
    ovz_driver.num_active ++;
503

504
    waitpid(pid, NULL, 0);
505
bail_out4:
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    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) {
D
Daniel Veillard 已提交
521
        openvzError(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid"));
522 523
        return -1;
    }
524

525
    if (openvzIsActiveVM(vm)) {
D
Daniel Veillard 已提交
526
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, _("cannot delete active domain"));
527 528 529
        return -1;
    }
    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " destroy %s ", vm->vmdef->name);
530 531

    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
532
    {
533
        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
534 535 536 537
        goto bail_out5;
    }
    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
    if(ret == -1) {
D
Daniel Veillard 已提交
538
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
539
               _("Could not exec %s"), VZCTL);
540 541
        return -1;
    }
542

543 544
    waitpid(pid, NULL, 0);
    openvzRemoveInactiveVM(driver, vm);
545
bail_out5:
546
    cmdExecFree(cmdExec);
547 548 549
    return ret;
}

550 551 552 553 554 555
static int
openvzDomainSetAutostart(virDomainPtr dom, int autostart)
{
    virConnectPtr conn= dom->conn;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
    struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid);
D
Daniel Veillard 已提交
556 557
    const char *prog[] = { VZCTL, "set", vm->vmdef->name,
                           "--onboot", autostart ? "yes" : "no",
558 559 560
                           "--save", NULL };

    if (!vm) {
D
Daniel Veillard 已提交
561
        openvzError(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid"));
562 563 564 565
        return -1;
    }

    if (virRun(conn, (char **)prog, NULL) < 0) {
D
Daniel Veillard 已提交
566
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, _("Could not exec %s"), VZCTL);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        return -1;
    }

    return 0;
}

static int
openvzDomainGetAutostart(virDomainPtr dom, int *autostart)
{
    virConnectPtr conn= dom->conn;
    struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
    struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid);
    char value[1024];

    if (!vm) {
D
Daniel Veillard 已提交
582
        openvzError(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid"));
583 584 585 586
        return -1;
    }

    if (openvzReadConfigParam(vm->vpsid , "ONBOOT", value, sizeof(value)) < 0) {
D
Daniel Veillard 已提交
587
        openvzError(conn, VIR_ERR_INTERNAL_ERROR, _("Cound not read container config"));
588 589 590 591 592
        return -1;
    }

    *autostart = 0;
    if (STREQ(value,"yes"))
D
Daniel Veillard 已提交
593
        *autostart = 1;
594 595 596 597

    return 0;
}

598 599 600 601
static const char *openvzProbe(void)
{
#ifdef __linux__
    if ((getuid() == 0) && (virFileExists("/proc/vz")))
D
Daniel Veillard 已提交
602
        return("openvz:///system");
603 604 605 606
#endif
    return(NULL);
}

607
static virDrvOpenStatus openvzOpen(virConnectPtr conn,
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
                                 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;
   }
625
    /* See if we are running an OpenVZ enabled kernel */
626 627 628 629
   if(access("/proc/vz/veinfo", F_OK) == -1 ||
               access("/proc/user_beancounters", F_OK) == -1) {
       return VIR_DRV_OPEN_DECLINED;
   }
630

631
   conn->privateData = &ovz_driver;
632

633 634 635
   virStateInitialize();
   vms = openvzGetVPSInfo(conn);
   ovz_driver.vms = vms;
636

637 638
   return VIR_DRV_OPEN_SUCCESS;
};
639 640

static int openvzClose(virConnectPtr conn) {
641

642 643 644 645 646 647 648 649 650 651 652 653 654
    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);
    }
655

656 657 658 659 660 661 662 663 664
    conn->privateData = NULL;

    return 0;
}

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

665 666 667 668 669
static int openvzGetNodeInfo(virConnectPtr conn,
                             virNodeInfoPtr nodeinfo) {
    return virNodeInfoPopulate(conn, nodeinfo);
}

670 671
static int openvzListDomains(virConnectPtr conn, int *ids, int nids) {
    int got = 0;
672 673 674 675
    int veid, pid, outfd, errfd;
    int ret;
    char buf[32];
    const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
676

677
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
678
    if(ret == -1) {
D
Daniel Veillard 已提交
679
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
680
               _("Could not exec %s"), VZLIST);
D
Daniel P. Berrange 已提交
681
        return -1;
682 683
    }

684 685 686 687
    while(got < nids){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d", &veid);
688 689 690
        ids[got] = veid;
        got ++;
    }
691
    waitpid(pid, NULL, 0);
692 693 694 695

    return got;
}

696
static int openvzNumDomains(virConnectPtr conn ATTRIBUTE_UNUSED) {
697 698 699 700 701 702
    return ovz_driver.num_active;
}

static int openvzListDefinedDomains(virConnectPtr conn,
                            char **const names, int nnames) {
    int got = 0;
703
    int veid, pid, outfd, errfd, ret;
704
    char vpsname[OPENVZ_NAME_MAX];
705
    char buf[32];
706
    const char *cmd[] = {VZLIST, "-ovpsid", "-H", "-S", NULL};
707 708

    /* the -S options lists only stopped domains */
709
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
710
    if(ret == -1) {
D
Daniel Veillard 已提交
711
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
712
               _("Could not exec %s"), VZLIST);
D
Daniel P. Berrange 已提交
713
        return -1;
714 715
    }

716 717 718 719
    while(got < nnames){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d\n", &veid);
720 721 722 723
        sprintf(vpsname, "%d", veid);
        names[got] = strdup(vpsname);
        got ++;
    }
724
    waitpid(pid, NULL, 0);
725 726 727
    return got;
}

728
static int openvzNumDefinedDomains(virConnectPtr conn ATTRIBUTE_UNUSED) {
729
    return ovz_driver.num_inactive;
730 731 732 733
}

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

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    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,
757
    openvzProbe, /* probe */
758 759
    openvzOpen, /* open */
    openvzClose, /* close */
760
    NULL, /* supports_feature */
761 762 763 764 765
    openvzGetType, /* type */
    NULL, /* version */
    NULL, /* hostname */
    NULL, /* uri */
    NULL, /* getMaxVcpus */
766
    openvzGetNodeInfo, /* nodeGetInfo */
767 768 769
    NULL, /* getCapabilities */
    openvzListDomains, /* listDomains */
    openvzNumDomains, /* numOfDomains */
770
    openvzDomainCreateLinux, /* domainCreateLinux */
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    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 */
795 796
    openvzDomainDefineXML, /* domainDefineXML */
    openvzDomainUndefine, /* domainUndefine */
797 798
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
799 800
    openvzDomainGetAutostart, /* domainGetAutostart */
    openvzDomainSetAutostart, /* domainSetAutostart */
801 802 803
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
804 805 806 807 808
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
D
Daniel P. Berrange 已提交
809 810
    NULL, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
811
    NULL, /* nodeGetCellsFreeMemory */
812
    NULL, /* nodeGetFreeMemory */
813 814 815 816 817 818 819
};

static virStateDriver openvzStateDriver = {
    openvzStartup,
    openvzShutdown,
    openvzReload,
    openvzActive,
D
Daniel P. Berrange 已提交
820
    NULL, /* sigHandler */
821 822 823 824 825 826 827 828 829
};

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

#endif /* WITH_OPENVZ */