lxc_driver.c 35.3 KB
Newer Older
D
Daniel Veillard 已提交
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
/*
 * Copyright IBM Corp. 2008
 *
 * lxc_driver.c: linux container driver functions
 *
 * Authors:
 *  David L. Leskovec <dlesko at linux.vnet.ibm.com>
 *
 * 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
 */

#include <config.h>

#ifdef WITH_LXC

28
#include <fcntl.h>
D
David L. Leskovec 已提交
29
#include <sys/epoll.h>
D
Daniel Veillard 已提交
30 31
#include <sched.h>
#include <sys/utsname.h>
D
David L. Leskovec 已提交
32
#include <stdbool.h>
D
Daniel Veillard 已提交
33 34
#include <string.h>
#include <sys/types.h>
35
#include <termios.h>
D
Daniel Veillard 已提交
36 37 38 39
#include <unistd.h>
#include <wait.h>

#include "lxc_conf.h"
40
#include "lxc_container.h"
D
Daniel Veillard 已提交
41 42 43
#include "lxc_driver.h"
#include "driver.h"
#include "internal.h"
44
#include "memory.h"
45
#include "util.h"
46
#include "memory.h"
47 48 49
#include "bridge.h"
#include "qemu_conf.h"
#include "veth.h"
D
Daniel Veillard 已提交
50 51 52 53 54

/* debug macros */
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__)
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/*
 * GLibc headers are behind the kernel, so we define these
 * constants if they're not present already.
 */

#ifndef CLONE_NEWPID
#define CLONE_NEWPID  0x20000000
#endif
#ifndef CLONE_NEWUTS
#define CLONE_NEWUTS  0x04000000
#endif
#ifndef CLONE_NEWUSER
#define CLONE_NEWUSER 0x10000000
#endif
#ifndef CLONE_NEWIPC
#define CLONE_NEWIPC  0x08000000
#endif
72 73 74
#ifndef CLONE_NEWNET
#define CLONE_NEWNET  0x40000000 /* New network namespace */
#endif
75 76 77

static int lxcStartup(void);
static int lxcShutdown(void);
78
static lxc_driver_t *lxc_driver = NULL;
D
Daniel Veillard 已提交
79 80 81 82 83 84 85

/* Functions */
static int lxcDummyChild( void *argv ATTRIBUTE_UNUSED )
{
    exit(0);
}

86
static int lxcCheckContainerSupport(int extra_flags)
D
Daniel Veillard 已提交
87 88 89
{
    int rc = 0;
    int flags = CLONE_NEWPID|CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWUSER|
90
        CLONE_NEWIPC|SIGCHLD|extra_flags;
D
Daniel Veillard 已提交
91 92 93 94 95
    int cpid;
    char *childStack;
    char *stack;
    int childStatus;

96
    if (VIR_ALLOC_N(stack, getpagesize() * 4) < 0) {
D
Daniel Veillard 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        DEBUG0("Unable to allocate stack");
        rc = -1;
        goto check_complete;
    }

    childStack = stack + (getpagesize() * 4);

    cpid = clone(lxcDummyChild, childStack, flags, NULL);
    if ((0 > cpid) && (EINVAL == errno)) {
        DEBUG0("clone call returned EINVAL, container support is not enabled");
        rc = -1;
    } else {
        waitpid(cpid, &childStatus, 0);
    }

112
    VIR_FREE(stack);
D
Daniel Veillard 已提交
113 114 115 116 117 118 119 120

check_complete:
    return rc;
}

static const char *lxcProbe(void)
{
#ifdef __linux__
121
    if (0 == lxcCheckContainerSupport(0)) {
D
Daniel Veillard 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        return("lxc:///");
    }
#endif
    return(NULL);
}

static virDrvOpenStatus lxcOpen(virConnectPtr conn,
                                xmlURIPtr uri,
                                virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                int flags ATTRIBUTE_UNUSED)
{
    uid_t uid = getuid();

    /* Check that the user is root */
    if (0 != uid) {
        goto declineConnection;
    }

140 141 142
    if (lxc_driver == NULL)
        goto declineConnection;

D
Daniel Veillard 已提交
143 144 145 146 147 148 149 150 151 152
    /* Verify uri was specified */
    if ((NULL == uri) || (NULL == uri->scheme)) {
        goto declineConnection;
    }

    /* Check that the uri scheme is lxc */
    if (STRNEQ(uri->scheme, "lxc")) {
        goto declineConnection;
    }

153
    conn->privateData = lxc_driver;
D
Daniel Veillard 已提交
154 155 156 157 158 159 160 161 162

    return VIR_DRV_OPEN_SUCCESS;

declineConnection:
    return VIR_DRV_OPEN_DECLINED;
}

static int lxcClose(virConnectPtr conn)
{
163 164
    conn->privateData = NULL;
    return 0;
D
Daniel Veillard 已提交
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
}

static virDomainPtr lxcDomainLookupByID(virConnectPtr conn,
                                        int id)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    lxc_vm_t *vm = lxcFindVMByID(driver, id);
    virDomainPtr dom;

    if (!vm) {
        lxcError(conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
        return NULL;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) {
        dom->id = vm->def->id;
    }

    return dom;
}

static virDomainPtr lxcDomainLookupByUUID(virConnectPtr conn,
                                          const unsigned char *uuid)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    lxc_vm_t *vm = lxcFindVMByUUID(driver, uuid);
    virDomainPtr dom;

    if (!vm) {
        lxcError(conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
        return NULL;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) {
        dom->id = vm->def->id;
    }

    return dom;
}

static virDomainPtr lxcDomainLookupByName(virConnectPtr conn,
                                          const char *name)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    lxc_vm_t *vm = lxcFindVMByName(driver, name);
    virDomainPtr dom;

    if (!vm) {
        lxcError(conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
        return NULL;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) {
        dom->id = vm->def->id;
    }

    return dom;
}

static int lxcListDomains(virConnectPtr conn, int *ids, int nids)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    lxc_vm_t *vm;
    int numDoms = 0;

    for (vm = driver->vms; vm && (numDoms < nids); vm = vm->next) {
        if (lxcIsActiveVM(vm)) {
            ids[numDoms] = vm->def->id;
            numDoms++;
        }
    }

    return numDoms;
}

static int lxcNumDomains(virConnectPtr conn)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    return driver->nactivevms;
}

static int lxcListDefinedDomains(virConnectPtr conn,
                                 char **const names, int nnames)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    lxc_vm_t *vm;
    int numDoms = 0;
    int i;

    for (vm = driver->vms; vm && (numDoms < nnames); vm = vm->next) {
        if (!lxcIsActiveVM(vm)) {
            if (!(names[numDoms] = strdup(vm->def->name))) {
                lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "names");
                goto cleanup;
            }

            numDoms++;
        }

    }

    return numDoms;

 cleanup:
    for (i = 0 ; i < numDoms ; i++) {
273
        VIR_FREE(names[i]);
D
Daniel Veillard 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    }

    return -1;
}


static int lxcNumDefinedDomains(virConnectPtr conn)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    return driver->ninactivevms;
}

static virDomainPtr lxcDomainDefine(virConnectPtr conn, const char *xml)
{
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    lxc_vm_def_t *def;
    lxc_vm_t *vm;
    virDomainPtr dom;

    if (!(def = lxcParseVMDef(conn, xml, NULL))) {
        return NULL;
    }

297 298 299 300 301 302 303
    if ((def->nets != NULL) && !(driver->have_netns)) {
        lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
                 _("System lacks NETNS support"));
        lxcFreeVMDef(def);
        return NULL;
    }

D
Daniel Veillard 已提交
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 332 333 334 335 336 337 338 339 340 341 342 343 344
    if (!(vm = lxcAssignVMDef(conn, driver, def))) {
        lxcFreeVMDef(def);
        return NULL;
    }

    if (lxcSaveVMDef(conn, driver, vm, def) < 0) {
        lxcRemoveInactiveVM(driver, vm);
        return NULL;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) {
        dom->id = vm->def->id;
    }

    return dom;
}

static int lxcDomainUndefine(virDomainPtr dom)
{
    lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
    lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid);

    if (!vm) {
        lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
                 _("no domain with matching uuid"));
        return -1;
    }

    if (lxcIsActiveVM(vm)) {
        lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
                 _("cannot delete active domain"));
        return -1;
    }

    if (lxcDeleteConfig(dom->conn, driver, vm->configFile, vm->def->name) < 0) {
        return -1;
    }

    vm->configFile[0] = '\0';

345 346
    lxcDeleteTtyPidFile(vm);

D
Daniel Veillard 已提交
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
    lxcRemoveInactiveVM(driver, vm);

    return 0;
}

static int lxcDomainGetInfo(virDomainPtr dom,
                            virDomainInfoPtr info)
{
    lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
    lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid);

    if (!vm) {
        lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
                 _("no domain with matching uuid"));
        return -1;
    }

    info->state = vm->state;

    if (!lxcIsActiveVM(vm)) {
        info->cpuTime = 0;
    } else {
        info->cpuTime = 0;
    }

    info->maxMem = vm->def->maxMemory;
    info->memory = vm->def->maxMemory;
    info->nrVirtCpu = 1;

    return 0;
}

static char *lxcGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    /* Linux containers only run on Linux */
    return strdup("linux");
}

static char *lxcDomainDumpXML(virDomainPtr dom,
                              int flags ATTRIBUTE_UNUSED)
{
    lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
    lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid);

    if (!vm) {
        lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
                 _("no domain with matching uuid"));
        return NULL;
    }

    return lxcGenerateXML(dom->conn, driver, vm, vm->def);
}

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 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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
/**
 * lxcSetupInterfaces:
 * @conn: pointer to connection
 * @vm: pointer to virtual machine structure
 *
 * Sets up the container interfaces by creating the veth device pairs and
 * attaching the parent end to the appropriate bridge.  The container end
 * will moved into the container namespace later after clone has been called.
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcSetupInterfaces(virConnectPtr conn,
                              lxc_vm_t *vm)
{
    int rc = -1;
    lxc_driver_t *driver = conn->privateData;
    struct qemud_driver *networkDriver =
        (struct qemud_driver *)(conn->networkPrivateData);
    lxc_net_def_t *net = vm->def->nets;
    char* bridge;
    char parentVeth[PATH_MAX] = "";
    char containerVeth[PATH_MAX] = "";

    if ((vm->def->nets != NULL) && (driver->have_netns == 0)) {
        lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
                 _("System lacks NETNS support"));
        return -1;
    }

    for (net = vm->def->nets; net; net = net->next) {
        if (LXC_NET_NETWORK == net->type) {
            virNetworkPtr network = virNetworkLookupByName(conn, net->txName);
            if (!network) {
                goto error_exit;
            }

            bridge = virNetworkGetBridgeName(network);

            virNetworkFree(network);

        } else {
            bridge = net->txName;
        }

        DEBUG("bridge: %s", bridge);
        if (NULL == bridge) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("failed to get bridge for interface"));
            goto error_exit;
        }

        DEBUG0("calling vethCreate()");
        if (NULL != net->parentVeth) {
            strcpy(parentVeth, net->parentVeth);
        }
        if (NULL != net->containerVeth) {
            strcpy(containerVeth, net->containerVeth);
        }
        DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth);
        if (0 != (rc = vethCreate(parentVeth, PATH_MAX, containerVeth, PATH_MAX))) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("failed to create veth device pair: %d"), rc);
            goto error_exit;
        }
        if (NULL == net->parentVeth) {
            net->parentVeth = strdup(parentVeth);
        }
        if (NULL == net->containerVeth) {
            net->containerVeth = strdup(containerVeth);
        }

        if ((NULL == net->parentVeth) || (NULL == net->containerVeth)) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("failed to allocate veth names"));
            goto error_exit;
        }

        if (!(networkDriver->brctl) && (rc = brInit(&(networkDriver->brctl)))) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("cannot initialize bridge support: %s"),
                     strerror(rc));
            goto error_exit;
        }

        if (0 != (rc = brAddInterface(networkDriver->brctl, bridge, parentVeth))) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("failed to add %s device to %s: %s"),
                     parentVeth,
                     bridge,
                     strerror(rc));
            goto error_exit;
        }

        if (0 != (rc = vethInterfaceUpOrDown(parentVeth, 1))) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("failed to enable parent ns veth device: %d"), rc);
            goto error_exit;
        }

    }

    rc = 0;

error_exit:
    return rc;
}

/**
 * lxcMoveInterfacesToNetNs:
 * @conn: pointer to connection
 * @vm: pointer to virtual machine structure
 *
 * Starts a container process by calling clone() with the namespace flags
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcMoveInterfacesToNetNs(virConnectPtr conn,
                                    const lxc_vm_t *vm)
{
    int rc = -1;
    lxc_net_def_t *net;

    for (net = vm->def->nets; net; net = net->next) {
        if (0 != moveInterfaceToNetNs(net->containerVeth, vm->def->id)) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("failed to move interface %s to ns %d"),
                     net->containerVeth, vm->def->id);
            goto error_exit;
        }
    }

    rc = 0;

error_exit:
    return rc;
}

/**
 * lxcCleanupInterfaces:
 * @conn: pointer to connection
 * @vm: pointer to virtual machine structure
 *
 * Cleans up the container interfaces by deleting the veth device pairs.
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcCleanupInterfaces(const lxc_vm_t *vm)
{
    int rc = -1;
    lxc_net_def_t *net;

    for (net = vm->def->nets; net; net = net->next) {
        if (0 != (rc = vethDelete(net->parentVeth))) {
            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("failed to delete veth: %s"), net->parentVeth);
            /* will continue to try to cleanup any other interfaces */
        }
    }

    return 0;
}

/**
 * lxcSendContainerContinue:
564
 * @monitor: FD for communicating with child
565 566 567 568 569 570
 *
 * Sends the continue message via the socket pair stored in the vm
 * structure.
 *
 * Returns 0 on success or -1 in case of error
 */
571 572
static int lxcSendContainerContinue(virConnectPtr conn,
                                    int monitor)
573 574 575 576 577
{
    int rc = -1;
    lxc_message_t msg = LXC_CONTINUE_MSG;
    int writeCount = 0;

578
    writeCount = safewrite(monitor, &msg, sizeof(msg));
579
    if (writeCount != sizeof(msg)) {
580
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
581 582 583 584 585 586 587 588 589 590 591
                 _("unable to send container continue message: %s"),
                 strerror(errno));
        goto error_out;
    }

    rc = 0;

error_out:
    return rc;
}

592 593 594 595 596 597 598 599 600 601 602 603
/**
 * lxcStartContainer:
 * @conn: pointer to connection
 * @driver: pointer to driver structure
 * @vm: pointer to virtual machine structure
 *
 * Starts a container process by calling clone() with the namespace flags
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcStartContainer(virConnectPtr conn,
                             lxc_driver_t* driver,
604 605 606
                             lxc_vm_t *vm,
                             int monitor,
                             char *ttyPath)
607 608 609 610
{
    int rc = -1;
    int flags;
    int stacksize = getpagesize() * 4;
611
    char *stack, *stacktop;
612
    lxc_child_argv_t args = { vm->def, monitor, ttyPath };
613 614

    /* allocate a stack for the container */
615
    if (VIR_ALLOC_N(stack, stacksize) < 0) {
616 617 618 619
        lxcError(conn, NULL, VIR_ERR_NO_MEMORY,
                 _("unable to allocate container stack"));
        goto error_exit;
    }
620
    stacktop = stack + stacksize;
621 622 623

    flags = CLONE_NEWPID|CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWUSER|CLONE_NEWIPC|SIGCHLD;

624 625 626
    if (vm->def->nets != NULL)
        flags |= CLONE_NEWNET;

627
    vm->def->id = clone(lxcChild, stacktop, flags, &args);
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

    DEBUG("clone() returned, %d", vm->def->id);

    if (vm->def->id < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("clone() failed, %s"), strerror(errno));
        goto error_exit;
    }

    lxcSaveConfig(NULL, driver, vm, vm->def);

    rc = 0;

error_exit:
    return rc;
}


/**
647
 * lxcOpenTty:
648 649 650 651 652 653 654 655 656
 * @conn: pointer to connection
 * @ttymaster: pointer to int.  On success, set to fd for master end
 * @ttyName: On success, will point to string slave end of tty.  Caller
 * must free when done (such as in lxcFreeVM).
 *
 * Opens and configures container tty.
 *
 * Returns 0 on success or -1 in case of error
 */
657 658 659 660
static int lxcOpenTty(virConnectPtr conn,
                      int *ttymaster,
                      char **ttyName,
                      int rawmode)
661 662 663
{
    int rc = -1;

D
David L. Leskovec 已提交
664
    *ttymaster = posix_openpt(O_RDWR|O_NOCTTY|O_NONBLOCK);
665 666 667 668 669 670 671 672 673 674 675 676
    if (*ttymaster < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("posix_openpt failed: %s"), strerror(errno));
        goto cleanup;
    }

    if (unlockpt(*ttymaster) < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("unlockpt failed: %s"), strerror(errno));
        goto cleanup;
    }

677 678 679 680 681 682 683
    if (rawmode) {
        struct termios ttyAttr;
        if (tcgetattr(*ttymaster, &ttyAttr) < 0) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     "tcgetattr() failed: %s", strerror(errno));
            goto cleanup;
        }
684

685 686 687 688 689 690 691
        cfmakeraw(&ttyAttr);

        if (tcsetattr(*ttymaster, TCSADRAIN, &ttyAttr) < 0) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     "tcsetattr failed: %s", strerror(errno));
            goto cleanup;
        }
692 693
    }

694 695 696 697 698 699 700 701 702 703 704 705 706
    if (ttyName) {
        char tempTtyName[PATH_MAX];
        if (0 != ptsname_r(*ttymaster, tempTtyName, sizeof(tempTtyName))) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("ptsname_r failed: %s"), strerror(errno));
            goto cleanup;
        }

        if ((*ttyName = strdup(tempTtyName)) == NULL) {
            lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
            goto cleanup;
        }
    }
707 708 709 710

    rc = 0;

cleanup:
711 712 713
    if (rc != 0 &&
        *ttymaster != -1) {
        close(*ttymaster);
714 715 716 717 718
    }

    return rc;
}

D
David L. Leskovec 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
/**
 * lxcFdForward:
 * @readFd: file descriptor to read
 * @writeFd: file desriptor to write
 *
 * Reads 1 byte of data from readFd and writes to writeFd.
 *
 * Returns 0 on success, EAGAIN if returned on read, or -1 in case of error
 */
static int lxcFdForward(int readFd, int writeFd)
{
    int rc = -1;
    char buf[2];

    if (1 != (saferead(readFd, buf, 1))) {
        if (EAGAIN == errno) {
            rc = EAGAIN;
            goto cleanup;
        }

        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("read of fd %d failed: %s"), readFd, strerror(errno));
        goto cleanup;
    }

    if (1 != (safewrite(writeFd, buf, 1))) {
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("write to fd %d failed: %s"), writeFd, strerror(errno));
        goto cleanup;
    }

    rc = 0;

cleanup:
    return rc;
}

typedef struct _lxcTtyForwardFd_t {
    int fd;
    bool active;
} lxcTtyForwardFd_t;

761 762 763 764 765 766
/**
 * lxcTtyForward:
 * @fd1: Open fd
 * @fd1: Open fd
 *
 * Forwards traffic between fds.  Data read from fd1 will be written to fd2
D
David L. Leskovec 已提交
767 768 769
 * This process loops forever.
 * This uses epoll in edge triggered mode to avoid a hard loop on POLLHUP
 * events when the user disconnects the virsh console via ctrl-]
770 771 772 773 774 775
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcTtyForward(int fd1, int fd2)
{
    int rc = -1;
D
David L. Leskovec 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    int epollFd;
    struct epoll_event epollEvent;
    int numEvents;
    int numActive = 0;
    lxcTtyForwardFd_t fdArray[2];
    int timeout = -1;
    int curFdOff = 0;
    int writeFdOff = 0;

    fdArray[0].fd = fd1;
    fdArray[0].active = false;
    fdArray[1].fd = fd2;
    fdArray[1].active = false;

    /* create the epoll fild descriptor */
    epollFd = epoll_create(2);
    if (0 > epollFd) {
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("epoll_create(2) failed: %s"), strerror(errno));
        goto cleanup;
796 797
    }

D
David L. Leskovec 已提交
798 799 800 801 802 803 804 805 806
    /* add the file descriptors the epoll fd */
    memset(&epollEvent, 0x00, sizeof(epollEvent));
    epollEvent.events = EPOLLIN|EPOLLET;    /* edge triggered */
    epollEvent.data.fd = fd1;
    epollEvent.data.u32 = 0;                /* fdArray position */
    if (0 > epoll_ctl(epollFd, EPOLL_CTL_ADD, fd1, &epollEvent)) {
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("epoll_ctl(fd1) failed: %s"), strerror(errno));
        goto cleanup;
807
    }
D
David L. Leskovec 已提交
808 809 810 811 812
    epollEvent.data.fd = fd2;
    epollEvent.data.u32 = 1;                /* fdArray position */
    if (0 > epoll_ctl(epollFd, EPOLL_CTL_ADD, fd2, &epollEvent)) {
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("epoll_ctl(fd2) failed: %s"), strerror(errno));
813 814 815 816
        goto cleanup;
    }

    while (1) {
D
David L. Leskovec 已提交
817 818 819 820 821 822 823 824 825 826
        /* if active fd's, return if no events, else wait forever */
        timeout = (numActive > 0) ? 0 : -1;
        numEvents = epoll_wait(epollFd, &epollEvent, 1, timeout);
        if (0 < numEvents) {
            if (epollEvent.events & EPOLLIN) {
                curFdOff = epollEvent.data.u32;
                if (!fdArray[curFdOff].active) {
                    fdArray[curFdOff].active = true;
                    ++numActive;
                }
827

D
David L. Leskovec 已提交
828 829
            } else if (epollEvent.events & EPOLLHUP) {
                DEBUG("EPOLLHUP from fd %d", epollEvent.data.fd);
830
                continue;
D
David L. Leskovec 已提交
831 832 833 834
            } else {
                lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         _("error event %d"), epollEvent.events);
                goto cleanup;
835 836
            }

D
David L. Leskovec 已提交
837 838 839 840 841 842 843 844 845
        } else if (0 == numEvents) {
            if (2 == numActive) {
                /* both fds active, toggle between the two */
                curFdOff ^= 1;
            } else {
                /* only one active, if current is active, use it, else it */
                /* must be the other one (ie. curFd just went inactive) */
                curFdOff = fdArray[curFdOff].active ? curFdOff : curFdOff ^ 1;
            }
846

D
David L. Leskovec 已提交
847 848
        } else  {
            if (EINTR == errno) {
849 850 851
                continue;
            }

D
David L. Leskovec 已提交
852 853 854 855
            /* error */
            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("epoll_wait() failed: %s"), strerror(errno));
            goto cleanup;
856

D
David L. Leskovec 已提交
857
        }
858

D
David L. Leskovec 已提交
859 860 861
        if (0 < numActive) {
            writeFdOff = curFdOff ^ 1;
            rc = lxcFdForward(fdArray[curFdOff].fd, fdArray[writeFdOff].fd);
862

D
David L. Leskovec 已提交
863 864 865 866 867 868
            if (EAGAIN == rc) {
                /* this fd no longer has data, set it as inactive */
                --numActive;
                fdArray[curFdOff].active = false;
            } else if (-1 == rc) {
                goto cleanup;
869 870 871 872 873 874 875 876 877
            }

        }

    }

    rc = 0;

cleanup:
D
David L. Leskovec 已提交
878 879 880 881
    close(fd1);
    close(fd2);
    close(epollFd);
    exit(rc);
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
}

/**
 * lxcVmStart:
 * @conn: pointer to connection
 * @driver: pointer to driver structure
 * @vm: pointer to virtual machine structure
 *
 * Starts a vm
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcVmStart(virConnectPtr conn,
                      lxc_driver_t * driver,
                      lxc_vm_t * vm)
{
    int rc = -1;
899 900 901
    int sockpair[2] = { -1, -1 };
    int containerTty, parentTty;
    char *containerTtyPath = NULL;
902 903

    /* open parent tty */
904 905
    VIR_FREE(vm->def->tty);
    if (lxcOpenTty(conn, &parentTty, &vm->def->tty, 1) < 0) {
906 907 908 909
        goto cleanup;
    }

    /* open container tty */
910
    if (lxcOpenTty(conn, &containerTty, &containerTtyPath, 0) < 0) {
911 912 913 914 915 916 917 918 919 920 921 922 923
        goto cleanup;
    }

    /* fork process to handle the tty io forwarding */
    if ((vm->pid = fork()) < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("unable to fork tty forwarding process: %s"),
                 strerror(errno));
        goto cleanup;
    }

    if (vm->pid  == 0) {
        /* child process calls forward routine */
924
        lxcTtyForward(parentTty, containerTty);
925 926
    }

927 928 929 930
    if (lxcStoreTtyPid(driver, vm)) {
        DEBUG0("unable to store tty pid");
    }

931 932
    close(parentTty);
    close(containerTty);
933

934 935 936
    if (0 != (rc = lxcSetupInterfaces(conn, vm))) {
        goto cleanup;
    }
937

938 939
    /* create a socket pair to send continue message to the container once */
    /* we've completed the post clone configuration */
940
    if (0 != socketpair(PF_UNIX, SOCK_STREAM, 0, sockpair)) {
941 942 943
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("sockpair failed: %s"), strerror(errno));
        goto cleanup;
944 945
    }

946 947
    /* check this rc */

948 949 950
    rc = lxcStartContainer(conn, driver, vm,
                           sockpair[1],
                           containerTtyPath);
951 952 953 954 955 956 957
    if (rc != 0)
        goto cleanup;

    rc = lxcMoveInterfacesToNetNs(conn, vm);
    if (rc != 0)
        goto cleanup;

958
    rc = lxcSendContainerContinue(conn, sockpair[0]);
959 960 961 962 963 964 965
    if (rc != 0)
        goto cleanup;

    vm->state = VIR_DOMAIN_RUNNING;
    driver->ninactivevms--;
    driver->nactivevms++;

966
cleanup:
967 968 969
    if (sockpair[0] != -1) close(sockpair[0]);
    if (sockpair[1] != -1) close(sockpair[1]);
    VIR_FREE(containerTtyPath);
970

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    return rc;
}

/**
 * lxcDomainStart:
 * @dom: domain to start
 *
 * Looks up domain and starts it.
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcDomainStart(virDomainPtr dom)
{
    int rc = -1;
    virConnectPtr conn = dom->conn;
    lxc_driver_t *driver = (lxc_driver_t *)(conn->privateData);
    lxc_vm_t *vm = lxcFindVMByName(driver, dom->name);

    if (!vm) {
        lxcError(conn, dom, VIR_ERR_INVALID_DOMAIN,
                 "no domain with uuid");
        goto cleanup;
    }

    rc = lxcVmStart(conn, driver, vm);

cleanup:
    return rc;
}

/**
 * lxcDomainCreateAndStart:
 * @conn: pointer to connection
 * @xml: XML definition of domain
 * @flags: Unused
 *
 * Creates a domain based on xml and starts it
 *
 * Returns 0 on success or -1 in case of error
 */
static virDomainPtr
lxcDomainCreateAndStart(virConnectPtr conn,
                        const char *xml,
                        unsigned int flags ATTRIBUTE_UNUSED) {
    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
    lxc_vm_t *vm;
    lxc_vm_def_t *def;
    virDomainPtr dom = NULL;

    if (!(def = lxcParseVMDef(conn, xml, NULL))) {
        goto return_point;
    }

    if (!(vm = lxcAssignVMDef(conn, driver, def))) {
        lxcFreeVMDef(def);
        goto return_point;
    }

    if (lxcSaveVMDef(conn, driver, vm, def) < 0) {
        lxcRemoveInactiveVM(driver, vm);
        return NULL;
    }

    if (lxcVmStart(conn, driver, vm) < 0) {
        lxcRemoveInactiveVM(driver, vm);
        goto return_point;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) {
        dom->id = vm->def->id;
    }

return_point:
    return dom;
}

/**
 * lxcDomainShutdown:
 * @dom: Ptr to domain to shutdown
 *
 * Sends SIGINT to container root process to request it to shutdown
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcDomainShutdown(virDomainPtr dom)
{
    int rc = -1;
    lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
    lxc_vm_t *vm = lxcFindVMByID(driver, dom->id);

    if (!vm) {
        lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
                 _("no domain with id %d"), dom->id);
        goto error_out;
    }

    if (0 > (kill(vm->def->id, SIGINT))) {
        if (ESRCH != errno) {
            lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
                     _("sending SIGTERM failed: %s"), strerror(errno));

            goto error_out;
        }
    }

    vm->state = VIR_DOMAIN_SHUTDOWN;

    rc = 0;

error_out:
    return rc;
}

/**
1086 1087
 * lxcVmCleanup:
 * @vm: Ptr to VM to clean up
1088
 *
1089 1090 1091
 * waitpid() on the container process.  kill and wait the tty process
 * This is called by boh lxcDomainDestroy and lxcSigHandler when a
 * container exits.
1092 1093 1094
 *
 * Returns 0 on success or -1 in case of error
 */
1095
static int lxcVMCleanup(lxc_driver_t *driver, lxc_vm_t * vm)
1096 1097 1098
{
    int rc = -1;
    int waitRc;
1099
    int childStatus = -1;
1100

1101 1102 1103
    /* if this fails, we'll continue.  it will report any errors */
    lxcCleanupInterfaces(vm);

1104 1105 1106
    while (((waitRc = waitpid(vm->def->id, &childStatus, 0)) == -1) &&
           errno == EINTR);

1107
    if ((waitRc != vm->def->id) && (errno != ECHILD)) {
1108
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1109 1110 1111 1112 1113
                 _("waitpid failed to wait for container %d: %d %s"),
                 vm->def->id, waitRc, strerror(errno));
        goto kill_tty;
    }

1114 1115 1116 1117 1118 1119
    rc = 0;

    if (WIFEXITED(childStatus)) {
        rc = WEXITSTATUS(childStatus);
        DEBUG("container exited with rc: %d", rc);
    }
1120 1121

kill_tty:
1122 1123 1124 1125 1126
    if (2 > vm->pid) {
        DEBUG("not killing tty process with pid %d", vm->pid);
        goto tty_error_out;
    }

1127 1128
    if (0 > (kill(vm->pid, SIGKILL))) {
        if (ESRCH != errno) {
1129
            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
                     _("sending SIGKILL to tty process failed: %s"),
                     strerror(errno));

            goto tty_error_out;
        }
    }

    while (((waitRc = waitpid(vm->pid, &childStatus, 0)) == -1) &&
           errno == EINTR);

1140
    if ((waitRc != vm->pid) && (errno != ECHILD)) {
1141
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1142 1143 1144 1145 1146 1147 1148
                 _("waitpid failed to wait for tty %d: %d %s"),
                 vm->pid, waitRc, strerror(errno));
    }

tty_error_out:
    vm->state = VIR_DOMAIN_SHUTOFF;
    vm->pid = -1;
1149
    lxcDeleteTtyPidFile(vm);
1150 1151 1152
    vm->def->id = -1;
    driver->nactivevms--;
    driver->ninactivevms++;
1153
    lxcSaveConfig(NULL, driver, vm, vm->def);
1154

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    return rc;
 }

/**
 * lxcDomainDestroy:
 * @dom: Ptr to domain to destroy
 *
 * Sends SIGKILL to container root process to terminate the container
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcDomainDestroy(virDomainPtr dom)
{
    int rc = -1;
    lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
    lxc_vm_t *vm = lxcFindVMByID(driver, dom->id);

    if (!vm) {
        lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
                 _("no domain with id %d"), dom->id);
        goto error_out;
    }

    if (0 > (kill(vm->def->id, SIGKILL))) {
        if (ESRCH != errno) {
            lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
                     _("sending SIGKILL failed: %s"), strerror(errno));

            goto error_out;
        }
    }

    vm->state = VIR_DOMAIN_SHUTDOWN;

    rc = lxcVMCleanup(driver, vm);
1190 1191 1192 1193

error_out:
    return rc;
}
1194

1195 1196 1197 1198 1199 1200 1201
static int lxcCheckNetNsSupport(void)
{
    const char *argv[] = {"ip", "link", "set", "lo", "netns", "-1", NULL};
    int ip_rc;
    int user_netns = 0;
    int kern_netns = 0;

1202
    if (virRun(NULL, argv, &ip_rc) == 0)
1203 1204 1205 1206 1207 1208 1209 1210
        user_netns = WIFEXITED(ip_rc) && (WEXITSTATUS(ip_rc) != 255);

    if (lxcCheckContainerSupport(CLONE_NEWNET) == 0)
        kern_netns = 1;

    return kern_netns && user_netns;
}

1211
static int lxcStartup(void)
D
Daniel Veillard 已提交
1212
{
1213 1214 1215 1216 1217 1218 1219
    uid_t uid = getuid();

    /* Check that the user is root */
    if (0 != uid) {
        return -1;
    }

1220
    if (VIR_ALLOC(lxc_driver) < 0) {
1221 1222
        return -1;
    }
D
Daniel Veillard 已提交
1223

1224
    /* Check that this is a container enabled kernel */
1225
    if(0 != lxcCheckContainerSupport(0)) {
D
Daniel Veillard 已提交
1226 1227 1228
        return -1;
    }

1229
    lxc_driver->have_netns = lxcCheckNetNsSupport();
D
Daniel Veillard 已提交
1230 1231

    /* Call function to load lxc driver configuration information */
1232 1233
    if (lxcLoadDriverConfig(lxc_driver) < 0) {
        lxcShutdown();
D
Daniel Veillard 已提交
1234 1235 1236 1237
        return -1;
    }

    /* Call function to load the container configuration files */
1238 1239
    if (lxcLoadContainerInfo(lxc_driver) < 0) {
        lxcShutdown();
D
Daniel Veillard 已提交
1240 1241 1242 1243 1244 1245 1246 1247
        return -1;
    }

    return 0;
}

static void lxcFreeDriver(lxc_driver_t *driver)
{
1248 1249 1250
    VIR_FREE(driver->configDir);
    VIR_FREE(driver->stateDir);
    VIR_FREE(driver);
D
Daniel Veillard 已提交
1251 1252
}

1253
static int lxcShutdown(void)
D
Daniel Veillard 已提交
1254
{
1255
    if (lxc_driver == NULL)
1256
        return(-1);
1257
    lxcFreeVMs(lxc_driver->vms);
1258 1259
    lxc_driver->vms = NULL;
    lxcFreeDriver(lxc_driver);
1260
    lxc_driver = NULL;
1261 1262 1263

    return 0;
}
D
Daniel Veillard 已提交
1264

1265 1266 1267 1268 1269 1270 1271 1272 1273
/**
 * lxcActive:
 *
 * Checks if the LXC daemon is active, i.e. has an active domain
 *
 * Returns 1 if active, 0 otherwise
 */
static int
lxcActive(void) {
1274 1275
    if (lxc_driver == NULL)
        return(0);
1276 1277 1278 1279 1280 1281 1282
    /* If we've any active networks or guests, then we
     * mark this driver as active
     */
    if (lxc_driver->nactivevms)
        return 1;

    /* Otherwise we're happy to deal with a shutdown */
D
Daniel Veillard 已提交
1283 1284 1285
    return 0;
}

1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
/**
 * lxcSigHandler:
 * @siginfo: Pointer to siginfo_t structure
 *
 * Handles signals received by libvirtd.  Currently this is used to
 * catch SIGCHLD from an exiting container.
 *
 * Returns 0 on success or -1 in case of error
 */
static int lxcSigHandler(siginfo_t *siginfo)
{
    int rc = -1;
    lxc_vm_t *vm;

    if (siginfo->si_signo == SIGCHLD) {
        vm = lxcFindVMByID(lxc_driver, siginfo->si_pid);

        if (NULL == vm) {
            DEBUG("Ignoring SIGCHLD from non-container process %d\n",
                  siginfo->si_pid);
            goto cleanup;
        }

        rc = lxcVMCleanup(lxc_driver, vm);

    }

cleanup:
    return rc;
}

1317

D
Daniel Veillard 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
/* Function Tables */
static virDriver lxcDriver = {
    VIR_DRV_LXC, /* the number virDrvNo */
    "LXC", /* the name of the driver */
    LIBVIR_VERSION_NUMBER, /* the version of the backend */
    lxcProbe, /* probe */
    lxcOpen, /* open */
    lxcClose, /* close */
    NULL, /* supports_feature */
    NULL, /* type */
    NULL, /* version */
    NULL, /* getHostname */
    NULL, /* getURI */
    NULL, /* getMaxVcpus */
    NULL, /* nodeGetInfo */
    NULL, /* getCapabilities */
    lxcListDomains, /* listDomains */
    lxcNumDomains, /* numOfDomains */
1336
    lxcDomainCreateAndStart, /* domainCreateLinux */
D
Daniel Veillard 已提交
1337 1338 1339 1340 1341
    lxcDomainLookupByID, /* domainLookupByID */
    lxcDomainLookupByUUID, /* domainLookupByUUID */
    lxcDomainLookupByName, /* domainLookupByName */
    NULL, /* domainSuspend */
    NULL, /* domainResume */
1342
    lxcDomainShutdown, /* domainShutdown */
D
Daniel Veillard 已提交
1343
    NULL, /* domainReboot */
1344
    lxcDomainDestroy, /* domainDestroy */
D
Daniel Veillard 已提交
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    lxcGetOSType, /* domainGetOSType */
    NULL, /* domainGetMaxMemory */
    NULL, /* domainSetMaxMemory */
    NULL, /* domainSetMemory */
    lxcDomainGetInfo, /* domainGetInfo */
    NULL, /* domainSave */
    NULL, /* domainRestore */
    NULL, /* domainCoreDump */
    NULL, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    NULL, /* domainGetMaxVcpus */
    lxcDomainDumpXML, /* domainDumpXML */
    lxcListDefinedDomains, /* listDefinedDomains */
    lxcNumDefinedDomains, /* numOfDefinedDomains */
1360
    lxcDomainStart, /* domainCreate */
D
Daniel Veillard 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
    lxcDomainDefine, /* domainDefineXML */
    lxcDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
D
Daniel P. Berrange 已提交
1375 1376
    NULL, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
D
Daniel Veillard 已提交
1377 1378 1379 1380
    NULL, /* nodeGetCellsFreeMemory */
    NULL, /* getFreeMemory */
};

1381 1382 1383 1384 1385 1386

static virStateDriver lxcStateDriver = {
    lxcStartup,
    lxcShutdown,
    NULL, /* reload */
    lxcActive,
1387
    lxcSigHandler
1388 1389
};

D
Daniel Veillard 已提交
1390 1391 1392
int lxcRegister(void)
{
    virRegisterDriver(&lxcDriver);
1393
    virRegisterStateDriver(&lxcStateDriver);
D
Daniel Veillard 已提交
1394 1395 1396 1397
    return 0;
}

#endif /* WITH_LXC */