lxc_conf.c 21.9 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * Copyright IBM Corp. 2008
 *
 * lxc_conf.c: config functions for managing linux containers
 *
 * 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
 *
 */

/* includes */
#include <config.h>

#ifdef WITH_LXC

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/uri.h>
#include <libxml/xpath.h>

#include "buf.h"
#include "util.h"
#include "uuid.h"
44
#include "xml.h"
D
Daniel Veillard 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

#include "lxc_conf.h"

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

/* Functions */
void lxcError(virConnectPtr conn, virDomainPtr dom, int code,
              const char *fmt, ...)
{
    va_list args;
    char errorMessage[LXC_MAX_ERROR_LEN];
    const char *codeErrorMessage;

    if (fmt) {
        va_start(args, fmt);
        vsnprintf(errorMessage, LXC_MAX_ERROR_LEN-1, fmt, args);
        va_end(args);
    } else {
        errorMessage[0] = '\0';
    }

    codeErrorMessage = __virErrorMsg(code, fmt);
    __virRaiseError(conn, dom, NULL, VIR_FROM_LXC, code, VIR_ERR_ERROR,
                    codeErrorMessage, errorMessage, NULL, 0, 0,
                    codeErrorMessage, errorMessage);
}

static int lxcParseMountXML(virConnectPtr conn, xmlNodePtr nodePtr,
                            lxc_mount_t *lxcMount)
{
    xmlChar *fsType = NULL;
    xmlNodePtr curNode;
    xmlChar *mountSource = NULL;
    xmlChar *mountTarget = NULL;
    int strLen;
    int rc = -1;

84 85
    fsType = xmlGetProp(nodePtr, BAD_CAST "type");
    if (NULL == fsType) {
D
Daniel Veillard 已提交
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
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("missing filesystem type"));
        goto error;
    }

    if (xmlStrEqual(fsType, BAD_CAST "mount") == 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("invalid filesystem type"));
        goto error;
    }

    for (curNode = nodePtr->children;
         NULL != curNode;
         curNode = curNode->next) {
        if (curNode->type != XML_ELEMENT_NODE) {
            continue;
        }

        if ((mountSource == NULL) &&
            (xmlStrEqual(curNode->name, BAD_CAST "source"))) {
            mountSource = xmlGetProp(curNode, BAD_CAST "dir");
        } else if ((mountTarget == NULL) &&
                  (xmlStrEqual(curNode->name, BAD_CAST "target"))) {
            mountTarget = xmlGetProp(curNode, BAD_CAST "dir");
        }
    }

    if (mountSource == NULL) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("missing mount source"));
        goto error;
    }

    strLen = xmlStrlen(mountSource);
    if ((strLen > (PATH_MAX-1)) || (0 == strLen)) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("empty or invalid mount source"));
        goto error;
    }

    strncpy(lxcMount->source, (char *)mountSource, strLen);
    lxcMount->source[strLen] = '\0';

    if (mountTarget == NULL) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("missing mount target"));
        goto error;
    }

    strLen = xmlStrlen(mountTarget);
    if ((strLen > (PATH_MAX-1)) || (0 == strLen)) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("empty or invalid mount target"));
        goto error;
    }

    strncpy(lxcMount->target, (char *)mountTarget, strLen);
    lxcMount->target[strLen] = '\0';

    rc = 0;

error:
148
    xmlFree(fsType);
D
Daniel Veillard 已提交
149 150 151 152 153 154 155 156 157
    xmlFree(mountSource);
    xmlFree(mountTarget);

    return rc;
}

static int lxcParseDomainName(virConnectPtr conn, char **name,
                              xmlXPathContextPtr contextPtr)
{
158
    char *res;
D
Daniel Veillard 已提交
159

160 161
    res = virXPathString("string(/domain/name[1])", contextPtr);
    if (res == NULL) {
D
Daniel Veillard 已提交
162
        lxcError(conn, NULL, VIR_ERR_NO_NAME, NULL);
163
        return(-1);
D
Daniel Veillard 已提交
164 165
    }

166 167
    *name = res;
    return(0);
D
Daniel Veillard 已提交
168 169 170 171 172
}

static int lxcParseDomainUUID(virConnectPtr conn, unsigned char *uuid,
                              xmlXPathContextPtr contextPtr)
{
173
    char *res;
D
Daniel Veillard 已提交
174

175 176 177
    res = virXPathString("string(/domain/uuid[1])", contextPtr);
    if (res == NULL) {
        if (virUUIDGenerate(uuid)) {
D
Daniel Veillard 已提交
178
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
179 180
                     _("failed to generate uuid"));
            return(-1);
D
Daniel Veillard 已提交
181 182
        }
    } else {
183
        if (virUUIDParse(res, uuid) < 0) {
D
Daniel Veillard 已提交
184 185
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("invalid uuid element"));
186 187
	    free(res);
            return(-1);
D
Daniel Veillard 已提交
188
        }
189
	free(res);
D
Daniel Veillard 已提交
190
    }
191
    return(0);
D
Daniel Veillard 已提交
192 193 194 195 196 197 198 199 200 201 202
}

static int lxcParseDomainMounts(virConnectPtr conn,
                                lxc_mount_t **mounts,
                                xmlXPathContextPtr contextPtr)
{
    int rc = -1;
    int i;
    lxc_mount_t *mountObj;
    lxc_mount_t *prevObj = NULL;
    int nmounts = 0;
203 204
    xmlNodePtr *list;
    int res;
D
Daniel Veillard 已提交
205

206 207 208
    res = virXPathNodeSet("/domain/devices/filesystem", contextPtr, &list);
    if (res > 0) {
        for (i = 0; i < res; ++i) {
D
Daniel Veillard 已提交
209 210 211 212 213 214
            mountObj = calloc(1, sizeof(lxc_mount_t));
            if (NULL == mountObj) {
                lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "mount");
                goto parse_complete;
            }

215
            rc = lxcParseMountXML(conn, list[i], mountObj);
D
Daniel Veillard 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
            if (0 > rc) {
                free(mountObj);
                goto parse_complete;
            }

            /* set the linked list pointers */
            nmounts++;
            mountObj->next = NULL;
            if (0 == i) {
                *mounts = mountObj;
            } else {
                prevObj->next = mountObj;
            }
            prevObj = mountObj;
        }
231
	free(list);
D
Daniel Veillard 已提交
232 233 234 235 236 237 238 239
    }

    rc = nmounts;

parse_complete:
    return rc;
}

240 241
static int lxcParseDomainInit(virConnectPtr conn, char** init,
                              xmlXPathContextPtr contextPtr)
D
Daniel Veillard 已提交
242
{
243
    char *res;
D
Daniel Veillard 已提交
244

245 246
    res = virXPathString("string(/domain/os/init[1])", contextPtr);
    if (res == NULL) {
D
Daniel Veillard 已提交
247 248
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("invalid or missing init element"));
249
        return(-1);
D
Daniel Veillard 已提交
250 251
    }

252
    if (strlen(res) >= PATH_MAX - 1) {
D
Daniel Veillard 已提交
253 254
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("init string too long"));
255 256
        free(res);
	return(-1);
D
Daniel Veillard 已提交
257 258
    }

259
    *init = res;
D
Daniel Veillard 已提交
260

261
    return(0);
D
Daniel Veillard 已提交
262 263 264
}


265
static int lxcParseDomainTty(virConnectPtr conn, char **tty, xmlXPathContextPtr contextPtr)
D
Daniel Veillard 已提交
266
{
267
    char *res;
D
Daniel Veillard 已提交
268

269 270
    res = virXPathString("string(/domain/devices/console[1]/@tty)", contextPtr);
    if (res == NULL) {
D
Daniel Veillard 已提交
271
        /* make sure the tty string is empty */
272 273 274 275 276
        *tty = strdup("");
	if (*tty == NULL) {
	    lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
	    return(-1);
	}
D
Daniel Veillard 已提交
277
    } else {
278
        *tty = res;
D
Daniel Veillard 已提交
279 280
    }

281
    return(0);
D
Daniel Veillard 已提交
282 283 284 285
}

static int lxcParseDomainMemory(virConnectPtr conn, int* memory, xmlXPathContextPtr contextPtr)
{
286 287 288 289 290 291 292 293 294
    long res;
    int rc;

    rc = virXPathLong("string(/domain/memory[1])", contextPtr, &res);
    if ((rc == -2) || ((rc == 0) && (res <= 0))) {
	*memory = -1;
	lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
	         _("invalid memory value"));
    } else if (rc < 0) {
D
Daniel Veillard 已提交
295
        /* not an error, default to an invalid value so it's not used */
296
	*memory = -1;
D
Daniel Veillard 已提交
297
    } else {
298
	*memory = (int) res;
D
Daniel Veillard 已提交
299
    }
300
    return(0);
D
Daniel Veillard 已提交
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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
}

static lxc_vm_def_t * lxcParseXML(virConnectPtr conn, xmlDocPtr docPtr)
{
    xmlNodePtr rootNodePtr = NULL;
    xmlXPathContextPtr contextPtr = NULL;
    xmlChar *xmlProp = NULL;
    lxc_vm_def_t *containerDef;

    if (!(containerDef = calloc(1, sizeof(*containerDef)))) {
        lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "containerDef");
        return NULL;
    }

    /* Prepare parser / xpath context */
    rootNodePtr = xmlDocGetRootElement(docPtr);
    if ((rootNodePtr == NULL) ||
       (!xmlStrEqual(rootNodePtr->name, BAD_CAST "domain"))) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("invalid root element"));
        goto error;
    }

    contextPtr = xmlXPathNewContext(docPtr);
    if (contextPtr == NULL) {
        lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "context");
        goto error;
    }

    /* Verify the domain type is linuxcontainer */
    if (!(xmlProp = xmlGetProp(rootNodePtr, BAD_CAST "type"))) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("missing domain type"));
        goto error;
    }

    if (!(xmlStrEqual(xmlProp, BAD_CAST LXC_DOMAIN_TYPE))) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("invalid domain type"));
        goto error;
    }
    free(xmlProp);
    xmlProp = NULL;

    if ((xmlProp = xmlGetProp(rootNodePtr, BAD_CAST "id"))) {
        if (0 > virStrToLong_i((char*)xmlProp, NULL, 10, &(containerDef->id))) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
348
                     _("invalid domain id"));
D
Daniel Veillard 已提交
349 350 351 352 353 354 355 356 357 358 359 360
            goto error;
        }
    } else {
        containerDef->id = -1;
    }
    free(xmlProp);
    xmlProp = NULL;

    if (lxcParseDomainName(conn, &(containerDef->name), contextPtr) < 0) {
        goto error;
    }

361
    if (lxcParseDomainInit(conn, &(containerDef->init), contextPtr) < 0) {
D
Daniel Veillard 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374
        goto error;
    }

    if (lxcParseDomainUUID(conn, containerDef->uuid, contextPtr) < 0) {
        goto error;
    }

    containerDef->nmounts = lxcParseDomainMounts(conn, &(containerDef->mounts),
                                                 contextPtr);
    if (0 > containerDef->nmounts) {
        goto error;
    }

375
    if (lxcParseDomainTty(conn, &(containerDef->tty), contextPtr) < 0) {
D
Daniel Veillard 已提交
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 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
        goto error;
    }

    if (lxcParseDomainMemory(conn, &(containerDef->maxMemory), contextPtr) < 0) {
        goto error;
    }

    xmlXPathFreeContext(contextPtr);

    return containerDef;

 error:
    free(xmlProp);
    xmlXPathFreeContext(contextPtr);
    lxcFreeVMDef(containerDef);

    return NULL;
}


lxc_vm_def_t * lxcParseVMDef(virConnectPtr conn,
                             const char* xmlString,
                             const char* fileName)
{
    xmlDocPtr xml;
    lxc_vm_def_t *containerDef;

    xml = xmlReadDoc(BAD_CAST xmlString,
                     fileName ? fileName : "domain.xml",
                     NULL, XML_PARSE_NOENT |
                     XML_PARSE_NONET | XML_PARSE_NOERROR |
                     XML_PARSE_NOWARNING);
    if (!xml) {
        lxcError(conn, NULL, VIR_ERR_XML_ERROR, NULL);
        return NULL;
    }

    containerDef = lxcParseXML(conn, xml);

    xmlFreeDoc(xml);

    return containerDef;
}

lxc_vm_t * lxcAssignVMDef(virConnectPtr conn,
                          lxc_driver_t *driver,
                          lxc_vm_def_t *def)
{
    lxc_vm_t *vm = NULL;

    if ((vm = lxcFindVMByName(driver, def->name))) {
        if (!lxcIsActiveVM(vm)) {
            lxcFreeVMDef(vm->def);
            vm->def = def;
        } else {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("Can't redefine active VM with name %s"), def->name);
            return NULL;
        }

        return vm;
    }

    if (!(vm = calloc(1, sizeof(lxc_vm_t)))) {
        lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "vm");
        return NULL;
    }

    vm->pid = -1;
    vm->def = def;
    vm->next = driver->vms;

    driver->vms = vm;

    if (lxcIsActiveVM(vm)) {
        vm->state = VIR_DOMAIN_RUNNING;
        driver->nactivevms++;
    } else {
        vm->state = VIR_DOMAIN_SHUTOFF;
        driver->ninactivevms++;
    }

    return vm;
}

void lxcRemoveInactiveVM(lxc_driver_t *driver,
                         lxc_vm_t *vm)
{
    lxc_vm_t *prevVm = NULL;
    lxc_vm_t *curVm;

    for (curVm = driver->vms;
         (curVm != vm) && (NULL != curVm);
         curVm = curVm->next) {
        prevVm = curVm;
    }

    if (curVm) {
        if (prevVm) {
            prevVm->next = curVm->next;
        } else {
            driver->vms = curVm->next;
        }

        driver->ninactivevms--;
    }

    lxcFreeVM(vm);
}

/* Save a container's config data into a persistent file */
int lxcSaveConfig(virConnectPtr conn,
                  lxc_driver_t *driver,
                  lxc_vm_t *vm,
                  lxc_vm_def_t *def)
{
    int rc = -1;
    char *xmlDef;
    int fd = -1;
    int amtToWrite;

    if (!(xmlDef = lxcGenerateXML(conn, driver, vm, def))) {
        return -1;
    }

    if ((fd = open(vm->configFile,
                  O_WRONLY | O_CREAT | O_TRUNC,
                  S_IRUSR | S_IWUSR )) < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("cannot create config file %s: %s"),
                  vm->configFile, strerror(errno));
        goto cleanup;
    }

    amtToWrite = strlen(xmlDef);
    if (safewrite(fd, xmlDef, amtToWrite) < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("cannot write config file %s: %s"),
                 vm->configFile, strerror(errno));
        goto cleanup;
    }

    if (close(fd) < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("cannot save config file %s: %s"),
                 vm->configFile, strerror(errno));
        goto cleanup;
    }

    rc = 0;

 cleanup:
    if (fd != -1) {
        close(fd);
    }

    free(xmlDef);

    return rc;
}

int lxcSaveVMDef(virConnectPtr conn,
                 lxc_driver_t *driver,
                 lxc_vm_t *vm,
                 lxc_vm_def_t *def)
{
    int rc = -1;

    if (vm->configFile[0] == '\0') {
        if ((rc = virFileMakePath(driver->configDir))) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("cannot create config directory %s: %s"),
                     driver->configDir, strerror(rc));
            goto save_complete;
        }

552
        if (virFileBuildPath(driver->configDir, vm->def->name, ".xml",
D
Daniel Veillard 已提交
553 554 555 556 557 558
                            vm->configFile, PATH_MAX) < 0) {
            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                     _("cannot construct config file path"));
            goto save_complete;
        }

559 560
        strncpy(vm->configFileBase, vm->def->name, PATH_MAX-1);
        strncat(vm->configFileBase, ".xml", PATH_MAX - strlen(vm->def->name)-1);
D
Daniel Veillard 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

    }

    rc = lxcSaveConfig(conn, driver, vm, def);

save_complete:
    return rc;
}



static lxc_vm_t * lxcLoadConfig(lxc_driver_t *driver,
                                const char *file,
                                const char *fullFilePath,
                                const char *xmlData)
{
    lxc_vm_def_t *containerDef;
    lxc_vm_t * vm;

    containerDef = lxcParseVMDef(NULL, xmlData, file);
    if (NULL == containerDef) {
        DEBUG0("Error parsing container config");
        return NULL;
    }

586 587
    if (!virFileMatchesNameSuffix(file, containerDef->name, ".xml")) {
        DEBUG0("Container name does not match config file name");
D
Daniel Veillard 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
        lxcFreeVMDef(containerDef);
        return NULL;
    }

    vm = lxcAssignVMDef(NULL, driver, containerDef);
    if (NULL == vm) {
        DEBUG0("Failed to load container config");
        lxcFreeVMDef(containerDef);
        return NULL;
    }

    strncpy(vm->configFile, fullFilePath, PATH_MAX);
    vm->configFile[PATH_MAX-1] = '\0';

    strncpy(vm->configFileBase, file, PATH_MAX);
    vm->configFile[PATH_MAX-1] = '\0';

    return vm;
}

608
int lxcLoadDriverConfig(lxc_driver_t *driver)
D
Daniel Veillard 已提交
609 610
{
    /* Set the container configuration directory */
611 612 613
    driver->configDir = strdup(SYSCONF_DIR "/libvirt/lxc");
    if (NULL == driver->configDir) {
        lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, "configDir");
D
Daniel Veillard 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
        return -1;
    }

    return 0;
}

int lxcLoadContainerConfigFile(lxc_driver_t *driver,
                               const char *file)
{
    int rc = -1;
    char tempPath[PATH_MAX];
    char* xmlData;

    rc = virFileBuildPath(driver->configDir, file, NULL, tempPath,
                          PATH_MAX);
    if (0 > rc) {
        DEBUG0("config file name too long");
        goto load_complete;
    }

    if ((rc = virFileReadAll(tempPath, LXC_MAX_XML_LENGTH, &xmlData)) < 0) {
        goto load_complete;
    }

    lxcLoadConfig(driver, file, tempPath, xmlData);

    free(xmlData);

load_complete:
    return rc;
}

646
int lxcLoadContainerInfo(lxc_driver_t *driver)
D
Daniel Veillard 已提交
647 648 649 650 651
{
    int rc = -1;
    DIR *dir;
    struct dirent *dirEntry;

652
    if (!(dir = opendir(driver->configDir))) {
D
Daniel Veillard 已提交
653 654 655 656
        if (ENOENT == errno) {
            /* no config dir => no containers */
            rc = 0;
        } else {
657
            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
658 659
                     _("failed to open config directory %s: %s"),
		     driver->configDir, strerror(errno));
D
Daniel Veillard 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673
        }

        goto load_complete;
    }

    while ((dirEntry = readdir(dir))) {
        if (dirEntry->d_name[0] == '.') {
            continue;
        }

        if (!virFileHasSuffix(dirEntry->d_name, ".xml")) {
            continue;
        }

674
        lxcLoadContainerConfigFile(driver, dirEntry->d_name);
D
Daniel Veillard 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
    }

    closedir(dir);

    rc = 0;

load_complete:
    return rc;
}

/* Generate an XML document describing the vm's configuration */
char *lxcGenerateXML(virConnectPtr conn,
                     lxc_driver_t *driver ATTRIBUTE_UNUSED,
                     lxc_vm_t *vm,
                     lxc_vm_def_t *def)
{
    virBufferPtr buf = 0;
    unsigned char *uuid;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    lxc_mount_t *mount;

    buf = virBufferNew(LXC_MAX_XML_LENGTH);
    if (!buf) {
        goto no_memory;
    }

    if (lxcIsActiveVM(vm)) {
702 703
        if (virBufferVSprintf(buf, "<domain type='%s' id='%d'>\n",
			      LXC_DOMAIN_TYPE, vm->def->id) < 0) {
D
Daniel Veillard 已提交
704 705 706
            goto no_memory;
        }
    } else {
707 708
	if (virBufferVSprintf(buf, "<domain type='%s'>\n",
			      LXC_DOMAIN_TYPE) < 0) {
D
Daniel Veillard 已提交
709 710 711 712 713 714 715 716 717 718 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 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
            goto no_memory;
        }
    }

    if (virBufferVSprintf(buf, "    <name>%s</name>\n", def->name) < 0) {
        goto no_memory;
    }

    uuid = def->uuid;
    virUUIDFormat(uuid, uuidstr);
    if (virBufferVSprintf(buf, "    <uuid>%s</uuid>\n", uuidstr) < 0) {
        goto no_memory;
    }

    if (virBufferAddLit(buf, "    <os>\n") < 0) {
        goto no_memory;
    }

    if (virBufferVSprintf(buf, "        <init>%s</init>\n", def->init) < 0) {
        goto no_memory;
    }

    if (virBufferAddLit(buf, "    </os>\n") < 0) {
        goto no_memory;
    }

    if (virBufferVSprintf(buf, "    <memory>%d</memory>\n", def->maxMemory) < 0) {
        goto no_memory;
    }

    if (virBufferAddLit(buf, "    <devices>\n") < 0) {
        goto no_memory;
    }

    /* loop adding mounts */
    for (mount = def->mounts; mount; mount = mount->next) {
        if (virBufferAddLit(buf, "        <filesystem type='mount'>\n") < 0) {
            goto no_memory;
        }

        if (virBufferVSprintf(buf, "            <source dir='%s'/>\n",
                              mount->source) < 0) {
            goto no_memory;
        }

        if (virBufferVSprintf(buf, "            <target dir='%s'/>\n",
                              mount->target) < 0) {
            goto no_memory;
        }

        if (virBufferAddLit(buf, "        </filesystem>\n") < 0) {
            goto no_memory;
        }

    }

    if (virBufferVSprintf(buf, "        <console tty='%s'/>\n", def->tty) < 0) {
        goto no_memory;
    }

    if (virBufferAddLit(buf, "    </devices>\n") < 0) {
        goto no_memory;
    }

    if (virBufferAddLit(buf, "</domain>\n") < 0) {
        goto no_memory;
    }

    return virBufferContentAndFree(buf);

no_memory:
    lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "generateXml");
    virBufferFree(buf);

    return NULL;
}

void lxcFreeVMDef(lxc_vm_def_t *vmdef)
{
788
    lxc_mount_t *curMount;
D
Daniel Veillard 已提交
789 790
    lxc_mount_t *nextMount;

791 792 793 794
    if (vmdef == NULL)
        return;

    curMount = vmdef->mounts;
D
Daniel Veillard 已提交
795 796 797 798 799 800 801
    while (curMount) {
        nextMount = curMount->next;
        free(curMount);
        curMount = nextMount;
    }

    free(vmdef->name);
802 803 804
    free(vmdef->init);
    free(vmdef->tty);
    free(vmdef);
D
Daniel Veillard 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
}

void lxcFreeVMs(lxc_vm_t *vms)
{
    lxc_vm_t *curVm = vms;
    lxc_vm_t *nextVm;

    while (curVm) {
        lxcFreeVM(curVm);
        nextVm = curVm->next;
        curVm = nextVm;
    }
}

void lxcFreeVM(lxc_vm_t *vm)
{
    lxcFreeVMDef(vm->def);
822
    free(vm);
D
Daniel Veillard 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
}

lxc_vm_t *lxcFindVMByID(const lxc_driver_t *driver, int id)
{
    lxc_vm_t *vm;

    for (vm = driver->vms; vm; vm = vm->next) {
        if (lxcIsActiveVM(vm) && (vm->def->id == id)) {
            return vm;
        }

    }

    return NULL;
}

lxc_vm_t *lxcFindVMByUUID(const lxc_driver_t *driver,
                          const unsigned char *uuid)
{
    lxc_vm_t *vm;

    for (vm = driver->vms; vm; vm = vm->next) {
        if (!memcmp(vm->def->uuid, uuid, VIR_UUID_BUFLEN)) {
            return vm;
        }
    }

    return NULL;
}

lxc_vm_t *lxcFindVMByName(const lxc_driver_t *driver,
                          const char *name)
{
    lxc_vm_t *vm;

    for (vm = driver->vms; vm; vm = vm->next) {
        if (STREQ(vm->def->name, name)) {
            return vm;
        }
    }

    return NULL;
}

int lxcDeleteConfig(virConnectPtr conn,
                    lxc_driver_t *driver ATTRIBUTE_UNUSED,
                    const char *configFile,
                    const char *name)
{
    if (!configFile[0]) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("no config file for %s"), name);
        return -1;
    }

    if (unlink(configFile) < 0) {
        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                 _("cannot remove config for %s"), name);
        return -1;
    }

    return 0;
}

#endif /* WITH_LXC */

889 890 891 892 893 894 895 896 897

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