openvz_driver.c 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 53 54
/*
 * openvz_driver.c: core driver methods for managing OpenVZ VEs
 *
 * Copyright (C) 2006, 2007 Binary Karma
 * Copyright (C) 2006 Shuveb Hussain
 *
 * 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
 *
 * Author: Shuveb Hussain <shuveb@binarykarma.com>
 */

#ifdef WITH_OPENVZ

#include <config.h>

#define _GNU_SOURCE /* for asprintf */

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

#include <libvirt/virterror.h>

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


#define openvzLog(level, msg...) fprintf(stderr, msg)

static virDomainPtr openvzDomainLookupByID(virConnectPtr conn, int id);
static char *openvzGetOSType(virDomainPtr dom);
65
static int openvzGetNodeInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 128 129 130 131 132 133 134 135 136 137 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
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);
static virDrvOpenStatus openvzOpen(virConnectPtr conn, const char *name,
                           int flags ATTRIBUTE_UNUSED);
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);
static int openvzCloseNetwork(virConnectPtr conn);
static virDrvOpenStatus openvzOpenNetwork(virConnectPtr conn, const char *name ATTRIBUTE_UNUSED,
                                         int flags ATTRIBUTE_UNUSED);
struct openvz_driver ovz_driver;

/* For errors internal to this library. */
static void
error (virConnectPtr conn, virErrorNumber code, const char *info)
{
    const char *errmsg;

    errmsg = __virErrorMsg (code, info);
    __virRaiseError (conn, NULL, NULL, VIR_FROM_REMOTE,
                     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;
}

static char *openvzGetOSType(virDomainPtr dom)
{
    /* OpenVZ runs on Linux and runs only Linux */
    return strdup("Linux");
}


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) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "no domain with matching uuid");
        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);
    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) {
    char cmdbuf[1024];
    int ret;
    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;
    }

    snprintf(cmdbuf, 1024, VZCTL " stop %d >/dev/null 2>&1", dom->id);
    ret = system(cmdbuf);
    if(WEXITSTATUS(ret)) {
        error(dom->conn, VIR_ERR_OPERATION_FAILED, "could not shutdown domain");
        return -1;
    }
    vm->vpsid = -1;
    vm->status = VIR_DOMAIN_SHUTOFF;
    ovz_driver.num_inactive ++;
    ovz_driver.num_active --;
    
    return ret;
}

static int openvzDomainReboot(virDomainPtr dom, unsigned int flags) {
    char cmdbuf[1024];
    int ret;
    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;
    }

    snprintf(cmdbuf, 1024, VZCTL " restart %d >/dev/null 2>&1", dom->id);
    ret = system(cmdbuf);
    if(WEXITSTATUS(ret)) {
        error(dom->conn, VIR_ERR_OPERATION_FAILED, "could not reboot domain");
        return -1;
    }
    
    return ret;
}

static int openvzDomainCreate(virDomainPtr dom) {
    char cmdbuf[1024];
    int ret;
    struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
    struct openvz_vm *vm = openvzFindVMByID(driver, dom->id);
    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;
    snprintf(cmdbuf, 1024, VZCTL " start %s >/dev/null 2>&1", vmdef->name);
    ret = system(cmdbuf);
    if(WEXITSTATUS(ret)) {
        error(dom->conn, VIR_ERR_OPERATION_FAILED, "could not start domain");
        return -1;
    }
    sscanf(vmdef->name, "%d", &vm->vpsid); 
    vm->status = VIR_DOMAIN_RUNNING;
    ovz_driver.num_inactive --;
    ovz_driver.num_active ++;

    return ret;
}

static virDrvOpenStatus openvzOpen(virConnectPtr conn,
                           const char *name,
                           int flags ATTRIBUTE_UNUSED) {
    struct openvz_vm *vms;

    /* Just check if the guy 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 (strcmp(name, "openvz:///system")) 
            return VIR_DRV_OPEN_DECLINED;
    }
290 291 292 293 294
    /* See if we are running an OpenVZ enabled kernel */
    if(access("/proc/vz/veinfo", F_OK) == -1 || 
                access("/proc/user_beancounters", F_OK) == -1) {
        return VIR_DRV_OPEN_DECLINED;
    }
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

    conn->privateData = &ovz_driver;

    virStateInitialize();
    vms = openvzGetVPSInfo(conn);
    ovz_driver.vms = vms;

    return VIR_DRV_OPEN_SUCCESS;
}

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


332 333 334 335 336 337
static int openvzGetNodeInfo(virConnectPtr conn,
                             virNodeInfoPtr nodeinfo) {
    return virNodeInfoPopulate(conn, nodeinfo);
}


338 339
static int openvzListDomains(virConnectPtr conn, int *ids, int nids) {
    int got = 0;
340 341 342 343
    int veid, pid, outfd, errfd;
    int ret;
    char buf[32];
    const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
344

345
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
346 347
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
348 349 350
        return (int)NULL;
    }

351 352 353 354
    while(got < nids){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d", &veid);
355 356 357
        ids[got] = veid;
        got ++;
    }
358
    waitpid(pid, NULL, 0);
359 360 361 362 363 364 365 366 367 368 369

    return got;
}

static int openvzNumDomains(virConnectPtr conn) {
    return ovz_driver.num_active;
}

static int openvzListDefinedDomains(virConnectPtr conn,
                            char **const names, int nnames) {
    int got = 0;
370
    int veid, pid, outfd, errfd, ret;
371
    char vpsname[OPENVZ_NAME_MAX];
372 373
    char buf[32];
    const char *cmd[] = {VZLIST, "-ovpsid", "-H", NULL};
374 375

    /* the -S options lists only stopped domains */
376
    ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
377 378
    if(ret == -1) {
        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
379 380 381
        return (int)NULL;
    }

382 383 384 385
    while(got < nnames){
        ret = openvz_readline(outfd, buf, 32);
        if(!ret) break;
        sscanf(buf, "%d\n", &veid);
386 387 388 389
        sprintf(vpsname, "%d", veid);
        names[got] = strdup(vpsname);
        got ++;
    }
390
    waitpid(pid, NULL, 0);
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
    return got;
}


static int openvzNumDefinedDomains(virConnectPtr conn) {
    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 int openvzCloseNetwork(virConnectPtr conn) {
    return 0;
}

static virDrvOpenStatus openvzOpenNetwork(virConnectPtr conn,
                                         const char *name ATTRIBUTE_UNUSED,
                                         int flags ATTRIBUTE_UNUSED) {
    return VIR_DRV_OPEN_SUCCESS;
}


static virDriver openvzDriver = {
    VIR_DRV_OPENVZ,
    "OPENVZ",
    LIBVIR_VERSION_NUMBER,
    openvzOpen, /* open */
    openvzClose, /* close */
    openvzGetType, /* type */
    NULL, /* version */
    NULL, /* hostname */
    NULL, /* uri */
    NULL, /* getMaxVcpus */
442
    openvzGetNodeInfo, /* nodeGetInfo */
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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    NULL, /* getCapabilities */
    openvzListDomains, /* listDomains */
    openvzNumDomains, /* numOfDomains */
    NULL, /* domainCreateLinux */
    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 */
    NULL, /* domainDefineXML */
    NULL, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
};

static virNetworkDriver openvzNetworkDriver = {
    openvzOpenNetwork, /* open */
    openvzCloseNetwork, /* close */
    NULL, /* numOfNetworks */
    NULL, /* listNetworks */
    NULL, /* numOfDefinedNetworks */
    NULL, /* listDefinedNetworks */
    NULL, /* networkLookupByUUID */
    NULL, /* networkLookupByName */
    NULL, /* networkCreateXML */
    NULL, /* networkDefineXML */
    NULL, /* networkUndefine */
    NULL, /* networkCreate */
    NULL, /* networkDestroy */
    NULL, /* networkDumpXML */
    NULL, /* networkGetBridgeName */
    NULL, /* networkGetAutostart */
    NULL, /* networkSetAutostart */
};

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

int openvzRegister(void) {
    virRegisterDriver(&openvzDriver);
    virRegisterNetworkDriver(&openvzNetworkDriver);
    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:
 */