lxc_native.c 26.1 KB
Newer Older
1 2 3
/*
 * lxc_native.c: LXC native configuration import
 *
4
 * Copyright (c) 2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2013 SUSE LINUX Products GmbH, Nuernberg, Germany.
 *
 * 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, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Cedric Bosdonnat <cbosdonnat@suse.com>
 */

#include <config.h>
25
#include <stdio.h>
26 27

#include "internal.h"
28
#include "lxc_container.h"
29 30
#include "lxc_native.h"
#include "util/viralloc.h"
31
#include "util/virfile.h"
32 33 34 35 36 37
#include "util/virlog.h"
#include "util/virstring.h"
#include "util/virconf.h"

#define VIR_FROM_THIS VIR_FROM_LXC

38
VIR_LOG_INIT("lxc.lxc_native");
39 40 41 42

static virDomainFSDefPtr
lxcCreateFSDef(int type,
               const char *src,
43 44 45
               const char* dst,
               bool readonly,
               unsigned long long usage)
46 47 48 49 50 51 52 53 54 55 56 57
{
    virDomainFSDefPtr def;

    if (VIR_ALLOC(def) < 0)
        return NULL;

    def->type = type;
    def->accessmode = VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH;
    if (src && VIR_STRDUP(def->src, src) < 0)
        goto error;
    if (VIR_STRDUP(def->dst, dst) < 0)
        goto error;
58 59
    def->readonly = readonly;
    def->usage = usage;
60 61 62 63 64 65 66 67

    return def;

 error:
    virDomainFSDefFree(def);
    return NULL;
}

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
typedef struct _lxcFstab lxcFstab;
typedef lxcFstab *lxcFstabPtr;
struct _lxcFstab {
    lxcFstabPtr next;
    char *src;
    char *dst;
    char *type;
    char *options;
};

static void
lxcFstabFree(lxcFstabPtr fstab)
{
    while (fstab) {
        lxcFstabPtr next = NULL;
        next = fstab->next;

        VIR_FREE(fstab->src);
        VIR_FREE(fstab->dst);
        VIR_FREE(fstab->type);
        VIR_FREE(fstab->options);
        VIR_FREE(fstab);

        fstab = next;
    }
}

static char ** lxcStringSplit(const char *string)
{
    char *tmp;
    size_t i;
    size_t ntokens = 0;
    char **parts;
    char **result = NULL;

    if (VIR_STRDUP(tmp, string) < 0)
        return NULL;

    /* Replace potential \t by a space */
    for (i = 0; tmp[i]; i++) {
        if (tmp[i] == '\t')
            tmp[i] = ' ';
    }

    if (!(parts = virStringSplit(tmp, " ", 0)))
        goto error;

    /* Append NULL element */
    if (VIR_EXPAND_N(result, ntokens, 1) < 0)
        goto error;

    for (i = 0; parts[i]; i++) {
        if (STREQ(parts[i], ""))
            continue;

        if (VIR_EXPAND_N(result, ntokens, 1) < 0)
            goto error;

        if (VIR_STRDUP(result[ntokens-2], parts[i]) < 0)
            goto error;
    }

    VIR_FREE(tmp);
    virStringFreeList(parts);
    return result;

134
 error:
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
    VIR_FREE(tmp);
    virStringFreeList(parts);
    virStringFreeList(result);
    return NULL;
}

static lxcFstabPtr
lxcParseFstabLine(char *fstabLine)
{
    lxcFstabPtr fstab = NULL;
    char **parts;

    if (!fstabLine || VIR_ALLOC(fstab) < 0)
        return NULL;

    if (!(parts = lxcStringSplit(fstabLine)))
        goto error;

    if (!parts[0] || !parts[1] || !parts[2] || !parts[3])
        goto error;

    if (VIR_STRDUP(fstab->src, parts[0]) < 0 ||
            VIR_STRDUP(fstab->dst, parts[1]) < 0 ||
            VIR_STRDUP(fstab->type, parts[2]) < 0 ||
            VIR_STRDUP(fstab->options, parts[3]) < 0)
        goto error;

    virStringFreeList(parts);

    return fstab;

166
 error:
167 168 169 170 171
    lxcFstabFree(fstab);
    virStringFreeList(parts);
    return NULL;
}

172 173 174 175
static int
lxcAddFSDef(virDomainDefPtr def,
            int type,
            const char *src,
176 177 178
            const char *dst,
            bool readonly,
            unsigned long long usage)
179 180 181
{
    virDomainFSDefPtr fsDef = NULL;

182
    if (!(fsDef = lxcCreateFSDef(type, src, dst, readonly, usage)))
183 184 185 186 187 188 189 190
        goto error;

    if (VIR_EXPAND_N(def->fss, def->nfss, 1) < 0)
        goto error;
    def->fss[def->nfss - 1] = fsDef;

    return 0;

191
 error:
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    virDomainFSDefFree(fsDef);
    return -1;
}

static int
lxcSetRootfs(virDomainDefPtr def,
             virConfPtr properties)
{
    int type = VIR_DOMAIN_FS_TYPE_MOUNT;
    virConfValuePtr value;

    if (!(value = virConfGetValue(properties, "lxc.rootfs")) ||
        !value->str) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Missing lxc.rootfs configuration"));
        return -1;
    }

    if (STRPREFIX(value->str, "/dev/"))
        type = VIR_DOMAIN_FS_TYPE_BLOCK;

213
    if (lxcAddFSDef(def, type, value->str, "/", false, 0) < 0)
214 215 216 217 218
        return -1;

    return 0;
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static int
lxcConvertSize(const char *size, unsigned long long *value)
{
    char *unit = NULL;

    /* Split the string into value and unit */
    if (virStrToLong_ull(size, &unit, 10, value) < 0)
        goto error;

    if (STREQ(unit, "%")) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("can't convert relative size: '%s'"),
                       size);
        return -1;
    } else {
        if (virScaleInteger(value, unit, 1, ULLONG_MAX) < 0)
            goto error;
    }

    return 0;

240
 error:
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 290 291 292 293 294 295
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("failed to convert size: '%s'"),
                   size);
    return -1;
}

static int
lxcAddFstabLine(virDomainDefPtr def, lxcFstabPtr fstab)
{
    const char *src = NULL;
    char *dst = NULL;
    char **options = virStringSplit(fstab->options, ",", 0);
    bool readonly;
    int type = VIR_DOMAIN_FS_TYPE_MOUNT;
    unsigned long long usage = 0;
    int ret = -1;

    if (!options)
        return -1;

    if (fstab->dst[0] != '/') {
        if (virAsprintf(&dst, "/%s", fstab->dst) < 0)
            goto cleanup;
    } else {
        if (VIR_STRDUP(dst, fstab->dst) < 0)
            goto cleanup;
    }

    /* Check that we don't add basic mounts */
    if (lxcIsBasicMountLocation(dst)) {
        ret = 0;
        goto cleanup;
    }

    if (STREQ(fstab->type, "tmpfs")) {
        char *sizeStr = NULL;
        size_t i;
        type = VIR_DOMAIN_FS_TYPE_RAM;

        for (i = 0; options[i]; i++) {
            if ((sizeStr = STRSKIP(options[i], "size="))) {
                if (lxcConvertSize(sizeStr, &usage) < 0)
                    goto cleanup;
                break;
            }
        }
        if (!sizeStr) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("missing tmpfs size, set the size option"));
            goto cleanup;
        }
    } else {
        src = fstab->src;
    }

296 297 298 299
    /* Is it a block device that needs special favor? */
    if (STRPREFIX(fstab->src, "/dev/"))
        type = VIR_DOMAIN_FS_TYPE_BLOCK;

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 332 333 334 335 336
    /* Do we have ro in options? */
    readonly = virStringArrayHasString(options, "ro");

    if (lxcAddFSDef(def, type, src, dst, readonly, usage) < 0)
        goto cleanup;

    ret = 1;

 cleanup:
    VIR_FREE(dst);
    virStringFreeList(options);
    return ret;
}

static int
lxcFstabWalkCallback(const char* name, virConfValuePtr value, void * data)
{
    int ret = 0;
    lxcFstabPtr fstabLine;
    virDomainDefPtr def = data;

    /* We only care about lxc.mount.entry lines */
    if (STRNEQ(name, "lxc.mount.entry"))
        return 0;

    fstabLine = lxcParseFstabLine(value->str);

    if (!fstabLine)
        return -1;

    if (lxcAddFstabLine(def, fstabLine) < 0)
        ret = -1;

    lxcFstabFree(fstabLine);
    return ret;
}

337 338
static virDomainNetDefPtr
lxcCreateNetDef(const char *type,
339
                const char *linkdev,
340
                const char *mac,
341
                const char *flag,
342 343
                const char *macvlanmode,
                const char *name)
344 345
{
    virDomainNetDefPtr net = NULL;
346
    virMacAddr macAddr;
347 348 349 350 351 352 353 354 355 356 357

    if (VIR_ALLOC(net) < 0)
        goto error;

    if (flag) {
        if (STREQ(flag, "up"))
            net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP;
        else
            net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN;
    }

358 359
    if (VIR_STRDUP(net->ifname_guest, name) < 0)
        goto error;
360

361 362 363 364
    if (mac && virMacAddrParse(mac, &macAddr) == 0)
        net->mac = macAddr;

    if (STREQ(type, "veth")) {
365
        if (!linkdev)
366 367 368 369
            goto error;

        net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;

370
        if (VIR_STRDUP(net->data.bridge.brname, linkdev) < 0)
371 372
            goto error;

373 374
    } else if (STREQ(type, "macvlan")) {
        net->type = VIR_DOMAIN_NET_TYPE_DIRECT;
375

376
        if (!linkdev || VIR_STRDUP(net->data.direct.linkdev, linkdev) < 0)
377 378 379 380 381 382 383 384 385 386
            goto error;

        if (!macvlanmode || STREQ(macvlanmode, "private"))
            net->data.direct.mode = VIR_NETDEV_MACVLAN_MODE_PRIVATE;
        else if (STREQ(macvlanmode, "vepa"))
            net->data.direct.mode = VIR_NETDEV_MACVLAN_MODE_VEPA;
        else if (STREQ(macvlanmode, "bridge"))
            net->data.direct.mode = VIR_NETDEV_MACVLAN_MODE_BRIDGE;
        else
            VIR_WARN("Unknown macvlan type: %s", macvlanmode);
387 388 389 390
    }

    return net;

391
 error:
392 393 394 395
    virDomainNetDefFree(net);
    return NULL;
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static virDomainHostdevDefPtr
lxcCreateHostdevDef(int mode, int type, const char *data)
{
    virDomainHostdevDefPtr hostdev = virDomainHostdevDefAlloc();

    if (!hostdev)
        return NULL;

    hostdev->mode = mode;
    hostdev->source.caps.type = type;

    if (type == VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET &&
        VIR_STRDUP(hostdev->source.caps.u.net.iface, data) < 0) {
        virDomainHostdevDefFree(hostdev);
        hostdev = NULL;
    }

    return hostdev;
}

416 417 418
static int
lxcAddNetworkDefinition(virDomainDefPtr def,
                        const char *type,
419
                        const char *linkdev,
420
                        const char *mac,
421
                        const char *flag,
422
                        const char *macvlanmode,
423 424
                        const char *vlanid,
                        const char *name)
425 426
{
    virDomainNetDefPtr net = NULL;
427
    virDomainHostdevDefPtr hostdev = NULL;
428
    bool isPhys, isVlan = false;
429 430 431 432 433

    if ((type == NULL) || STREQ(type, "empty") || STREQ(type, "") ||
            STREQ(type, "none"))
        return 0;

434 435 436
    isPhys = STREQ(type, "phys");
    isVlan = STREQ(type, "vlan");
    if (type != NULL && (isPhys || isVlan)) {
437
        if (!linkdev) {
438 439 440 441 442
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Missing 'link' attribute for NIC"));
            goto error;
        }
        if (!(hostdev = lxcCreateHostdevDef(VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES,
443
                                            VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET,
444
                                            linkdev)))
445
            goto error;
446

447 448 449 450 451
        /* This still requires the user to manually setup the vlan interface
         * on the host */
        if (isVlan && vlanid) {
            VIR_FREE(hostdev->source.caps.u.net.iface);
            if (virAsprintf(&hostdev->source.caps.u.net.iface,
452
                            "%s.%s", linkdev, vlanid) < 0)
453 454 455
                goto error;
        }

456 457 458 459
        if (VIR_EXPAND_N(def->hostdevs, def->nhostdevs, 1) < 0)
            goto error;
        def->hostdevs[def->nhostdevs - 1] = hostdev;
    } else {
460
        if (!(net = lxcCreateNetDef(type, linkdev, mac, flag, macvlanmode, name)))
461 462 463 464 465 466
            goto error;

        if (VIR_EXPAND_N(def->nets, def->nnets, 1) < 0)
            goto error;
        def->nets[def->nnets - 1] = net;
    }
467 468 469

    return 1;

470
 error:
471
    virDomainNetDefFree(net);
472
    virDomainHostdevDefFree(hostdev);
473 474 475
    return -1;
}

476
typedef struct {
477
    virDomainDefPtr def;
478
    char *type;
479 480 481
    char *link;
    char *mac;
    char *flag;
482
    char *macvlanmode;
483
    char *vlanid;
484
    char *name;
485 486 487 488 489 490 491 492
    bool privnet;
    size_t networks;
} lxcNetworkParseData;

static int
lxcNetworkWalkCallback(const char *name, virConfValuePtr value, void *data)
{
    lxcNetworkParseData *parseData = data;
493
    int status;
494 495

    if (STREQ(name, "lxc.network.type")) {
496 497 498
        /* Store the previous NIC */
        status = lxcAddNetworkDefinition(parseData->def, parseData->type,
                                         parseData->link, parseData->mac,
499
                                         parseData->flag,
500
                                         parseData->macvlanmode,
501 502
                                         parseData->vlanid,
                                         parseData->name);
503

504 505 506
        if (status < 0)
            return -1;
        else if (status > 0)
507
            parseData->networks++;
508 509
        else if (parseData->type != NULL && STREQ(parseData->type, "none"))
            parseData->privnet = false;
510 511 512

        /* Start a new network interface config */
        parseData->type = NULL;
513 514 515
        parseData->link = NULL;
        parseData->mac = NULL;
        parseData->flag = NULL;
516
        parseData->macvlanmode = NULL;
517
        parseData->vlanid = NULL;
518
        parseData->name = NULL;
519 520 521 522

        /* Keep the new value */
        parseData->type = value->str;
    }
523 524 525 526 527 528
    else if (STREQ(name, "lxc.network.link"))
        parseData->link = value->str;
    else if (STREQ(name, "lxc.network.hwaddr"))
        parseData->mac = value->str;
    else if (STREQ(name, "lxc.network.flags"))
        parseData->flag = value->str;
529 530
    else if (STREQ(name, "lxc.network.macvlan.mode"))
        parseData->macvlanmode = value->str;
531 532
    else if (STREQ(name, "lxc.network.vlan.id"))
        parseData->vlanid = value->str;
533 534
    else if (STREQ(name, "lxc.network.name"))
        parseData->name = value->str;
535 536 537 538
    else if (STRPREFIX(name, "lxc.network"))
        VIR_WARN("Unhandled network property: %s = %s",
                 name,
                 value->str);
539

540 541 542 543 544 545
    return 0;
}

static int
lxcConvertNetworkSettings(virDomainDefPtr def, virConfPtr properties)
{
546
    int status;
547
    lxcNetworkParseData data = {def, NULL, NULL, NULL, NULL,
548
                                NULL, NULL, NULL, true, 0};
549 550 551

    virConfWalk(properties, lxcNetworkWalkCallback, &data);

552 553
    /* Add the last network definition found */
    status = lxcAddNetworkDefinition(def, data.type, data.link,
554
                                     data.mac, data.flag,
555
                                     data.macvlanmode,
556 557
                                     data.vlanid,
                                     data.name);
558 559 560
    if (status < 0)
        return -1;
    else if (status > 0)
561
        data.networks++;
562 563
    else if (data.type != NULL && STREQ(data.type, "none"))
        data.privnet = false;
564 565 566 567 568 569 570 571 572

    if (data.networks == 0 && data.privnet) {
        /* When no network type is provided LXC only adds loopback */
        def->features[VIR_DOMAIN_FEATURE_PRIVNET] = VIR_DOMAIN_FEATURE_STATE_ON;
    }

    return 0;
}

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static int
lxcCreateConsoles(virDomainDefPtr def, virConfPtr properties)
{
    virConfValuePtr value;
    int nbttys = 0;
    virDomainChrDefPtr console;
    size_t i;

    if (!(value = virConfGetValue(properties, "lxc.tty")) || !value->str)
        return 0;

    if (virStrToLong_i(value->str, NULL, 10, &nbttys) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to parse int: '%s'"),
                       value->str);
        return -1;
    }

    if (VIR_ALLOC_N(def->consoles, nbttys) < 0)
        return -1;

    def->nconsoles = nbttys;
    for (i = 0; i < nbttys; i++) {
        if (!(console = virDomainChrDefNew()))
            goto error;

        console->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
        console->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_LXC;
        console->target.port = i;
        console->source.type = VIR_DOMAIN_CHR_TYPE_PTY;

        def->consoles[i] = console;
    }

    return 0;

608
 error:
609 610 611 612
    virDomainChrDefFree(console);
    return -1;
}

613 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 646 647 648 649
static int
lxcIdmapWalkCallback(const char *name, virConfValuePtr value, void *data)
{
    virDomainDefPtr def = data;
    virDomainIdMapEntryPtr idmap = NULL;
    char type;
    unsigned long start, target, count;

    if (STRNEQ(name, "lxc.id_map") || !value->str)
        return 0;

    if (sscanf(value->str, "%c %lu %lu %lu", &type,
               &target, &start, &count) != 4) {
        virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid lxc.id_map: '%s'"),
                       value->str);
        return -1;
    }

    if (type == 'u') {
        if (VIR_EXPAND_N(def->idmap.uidmap, def->idmap.nuidmap, 1) < 0)
            return -1;
        idmap = &def->idmap.uidmap[def->idmap.nuidmap - 1];
    } else if (type == 'g') {
        if (VIR_EXPAND_N(def->idmap.gidmap, def->idmap.ngidmap, 1) < 0)
            return -1;
        idmap = &def->idmap.gidmap[def->idmap.ngidmap - 1];
    } else {
        return -1;
    }

    idmap->start = start;
    idmap->target = target;
    idmap->count = count;

    return 0;
}

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
static int
lxcSetMemTune(virDomainDefPtr def, virConfPtr properties)
{
    virConfValuePtr value;
    unsigned long long size = 0;

    if ((value = virConfGetValue(properties,
                "lxc.cgroup.memory.limit_in_bytes")) &&
            value->str && STRNEQ(value->str, "-1")) {
        if (lxcConvertSize(value->str, &size) < 0)
            return -1;
        size = size / 1024;
        def->mem.max_balloon = size;
        def->mem.hard_limit = size;
    }

    if ((value = virConfGetValue(properties,
                "lxc.cgroup.memory.soft_limit_in_bytes")) &&
            value->str && STRNEQ(value->str, "-1")) {
        if (lxcConvertSize(value->str, &size) < 0)
            return -1;

        def->mem.soft_limit = size / 1024;
    }

    if ((value = virConfGetValue(properties,
                "lxc.cgroup.memory.memsw.limit_in_bytes")) &&
            value->str && STRNEQ(value->str, "-1")) {
        if (lxcConvertSize(value->str, &size) < 0)
            return -1;

       def->mem.swap_hard_limit = size / 1024;
    }
    return 0;
}

686 687 688 689 690 691
static int
lxcSetCpuTune(virDomainDefPtr def, virConfPtr properties)
{
    virConfValuePtr value;

    if ((value = virConfGetValue(properties, "lxc.cgroup.cpu.shares")) &&
692 693 694 695 696
            value->str) {
        if (virStrToLong_ul(value->str, NULL, 10, &def->cputune.shares) < 0)
            goto error;
        def->cputune.sharesSpecified = true;
    }
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

    if ((value = virConfGetValue(properties,
                                 "lxc.cgroup.cpu.cfs_quota_us")) &&
            value->str && virStrToLong_ll(value->str, NULL, 10,
                                          &def->cputune.quota) < 0)
        goto error;

    if ((value = virConfGetValue(properties,
                                 "lxc.cgroup.cpu.cfs_period_us")) &&
            value->str && virStrToLong_ull(value->str, NULL, 10,
                                           &def->cputune.period) < 0)
        goto error;

    return 0;

712
 error:
713 714 715 716 717
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("failed to parse integer: '%s'"), value->str);
    return -1;
}

718 719 720 721
static int
lxcSetCpusetTune(virDomainDefPtr def, virConfPtr properties)
{
    virConfValuePtr value;
722
    virBitmapPtr nodeset = NULL;
723 724 725 726 727 728 729 730 731 732 733

    if ((value = virConfGetValue(properties, "lxc.cgroup.cpuset.cpus")) &&
            value->str) {
        if (virBitmapParse(value->str, 0, &def->cpumask,
                           VIR_DOMAIN_CPUMASK_LEN) < 0)
            return -1;

        def->placement_mode = VIR_DOMAIN_CPU_PLACEMENT_MODE_STATIC;
    }

    if ((value = virConfGetValue(properties, "lxc.cgroup.cpuset.mems")) &&
734 735
        value->str) {
        if (virBitmapParse(value->str, 0, &nodeset, VIR_DOMAIN_CPUMASK_LEN) < 0)
736
            return -1;
737 738 739 740 741 742
        if (virDomainNumatuneSet(def, VIR_DOMAIN_NUMATUNE_PLACEMENT_STATIC,
                                 VIR_DOMAIN_NUMATUNE_MEM_STRICT, nodeset) < 0) {
            virBitmapFree(nodeset);
            return -1;
        }
        virBitmapFree(nodeset);
743 744 745 746 747
    }

    return 0;
}

748 749 750 751 752 753
static int
lxcBlkioDeviceWalkCallback(const char *name, virConfValuePtr value, void *data)
{
    char **parts = NULL;
    virBlkioDevicePtr device = NULL;
    virDomainDefPtr def = data;
754 755 756
    size_t i = 0;
    char *path = NULL;
    int ret = -1;
757

758 759
    if (!STRPREFIX(name, "lxc.cgroup.blkio.") ||
            STREQ(name, "lxc.cgroup.blkio.weight")|| !value->str)
760 761 762 763 764 765 766
        return 0;

    if (!(parts = lxcStringSplit(value->str)))
        return -1;

    if (!parts[0] || !parts[1]) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
767 768 769
                       _("invalid %s value: '%s'"),
                       name, value->str);
        goto cleanup;
770 771
    }

772 773
    if (virAsprintf(&path, "/dev/block/%s", parts[0]) < 0)
        goto cleanup;
774

775 776 777 778 779 780 781 782 783 784 785 786 787
    /* Do we already have a device definition for this path?
     * Get that device or create a new one */
    for (i = 0; !device && i < def->blkio.ndevices; i++) {
        if (STREQ(def->blkio.devices[i].path, path))
            device = &def->blkio.devices[i];
    }
    if (!device) {
        if (VIR_EXPAND_N(def->blkio.devices, def->blkio.ndevices, 1) < 0)
            goto cleanup;
        device = &def->blkio.devices[def->blkio.ndevices - 1];
        device->path = path;
        path = NULL;
    }
788

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
    /* Set the value */
    if (STREQ(name, "lxc.cgroup.blkio.device_weight")) {
        if (virStrToLong_ui(parts[1], NULL, 10, &device->weight) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse device weight: '%s'"), parts[1]);
            goto cleanup;
        }
    } else if (STREQ(name, "lxc.cgroup.blkio.throttle.read_bps_device")) {
        if (virStrToLong_ull(parts[1], NULL, 10, &device->rbps) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse read_bps_device: '%s'"),
                           parts[1]);
            goto cleanup;
        }
    } else if (STREQ(name, "lxc.cgroup.blkio.throttle.write_bps_device")) {
        if (virStrToLong_ull(parts[1], NULL, 10, &device->wbps) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse write_bps_device: '%s'"),
                           parts[1]);
            goto cleanup;
        }
    } else if (STREQ(name, "lxc.cgroup.blkio.throttle.read_iops_device")) {
        if (virStrToLong_ui(parts[1], NULL, 10, &device->riops) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse read_iops_device: '%s'"),
                           parts[1]);
            goto cleanup;
        }
    } else if (STREQ(name, "lxc.cgroup.blkio.throttle.write_iops_device")) {
        if (virStrToLong_ui(parts[1], NULL, 10, &device->wiops) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse write_iops_device: '%s'"),
                           parts[1]);
            goto cleanup;
        }
    } else {
        VIR_WARN("Unhandled blkio tune config: %s", name);
826 827
    }

828
    ret = 0;
829

830 831 832
 cleanup:
    virStringFreeList(parts);
    VIR_FREE(path);
833

834
    return ret;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
}

static int
lxcSetBlkioTune(virDomainDefPtr def, virConfPtr properties)
{
    virConfValuePtr value;

    if ((value = virConfGetValue(properties, "lxc.cgroup.blkio.weight")) &&
            value->str && virStrToLong_ui(value->str, NULL, 10,
                                          &def->blkio.weight) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to parse integer: '%s'"), value->str);
        return -1;
    }

    if (virConfWalk(properties, lxcBlkioDeviceWalkCallback, def) < 0)
        return -1;

    return 0;
}

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
virDomainDefPtr
lxcParseConfigString(const char *config)
{
    virDomainDefPtr vmdef = NULL;
    virConfPtr properties = NULL;
    virConfValuePtr value;

    if (!(properties = virConfReadMem(config, 0, VIR_CONF_FLAG_LXC_FORMAT)))
        return NULL;

    if (VIR_ALLOC(vmdef) < 0)
        goto error;

    if (virUUIDGenerate(vmdef->uuid) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to generate uuid"));
        goto error;
    }
874

875 876 877 878 879 880 881 882 883 884 885
    vmdef->id = -1;
    vmdef->mem.max_balloon = 64 * 1024;

    vmdef->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
    vmdef->onCrash = VIR_DOMAIN_LIFECYCLE_DESTROY;
    vmdef->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
    vmdef->virtType = VIR_DOMAIN_VIRT_LXC;

    /* Value not handled by the LXC driver, setting to
     * minimum required to make XML parsing pass */
    vmdef->maxvcpus = 1;
886
    vmdef->vcpus = 1;
887

888 889
    vmdef->nfss = 0;

890 891 892
    if (VIR_STRDUP(vmdef->os.type, "exe") < 0)
        goto error;

893 894 895 896 897 898 899 900 901
    if ((value = virConfGetValue(properties, "lxc.arch")) && value->str) {
        virArch arch = virArchFromString(value->str);
        if (arch == VIR_ARCH_NONE && STREQ(value->str, "x86"))
            arch = VIR_ARCH_I686;
        else if (arch == VIR_ARCH_NONE && STREQ(value->str, "amd64"))
            arch = VIR_ARCH_X86_64;
        vmdef->os.arch = arch;
    }

902 903 904 905 906 907 908 909 910
    if (VIR_STRDUP(vmdef->os.init, "/sbin/init") < 0)
        goto error;

    if (!(value = virConfGetValue(properties, "lxc.utsname")) ||
            !value->str || (VIR_STRDUP(vmdef->name, value->str) < 0))
        goto error;
    if (!vmdef->name && (VIR_STRDUP(vmdef->name, "unnamed") < 0))
        goto error;

911 912 913
    if (lxcSetRootfs(vmdef, properties) < 0)
        goto error;

914 915 916 917 918 919 920 921 922 923 924
    /* Look for fstab: we shouldn't have it */
    if (virConfGetValue(properties, "lxc.mount")) {
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("lxc.mount found, use lxc.mount.entry lines instead"));
        goto error;
    }

    /* Loop over lxc.mount.entry to add filesystem devices for them */
    if (virConfWalk(properties, lxcFstabWalkCallback, vmdef) < 0)
        goto error;

925 926 927 928
    /* Network configuration */
    if (lxcConvertNetworkSettings(vmdef, properties) < 0)
        goto error;

929 930 931 932
    /* Consoles */
    if (lxcCreateConsoles(vmdef, properties) < 0)
        goto error;

933 934 935 936
    /* lxc.id_map */
    if (virConfWalk(properties, lxcIdmapWalkCallback, vmdef) < 0)
        goto error;

937 938 939 940
    /* lxc.cgroup.memory.* */
    if (lxcSetMemTune(vmdef, properties) < 0)
        goto error;

941 942 943 944
    /* lxc.cgroup.cpu.* */
    if (lxcSetCpuTune(vmdef, properties) < 0)
        goto error;

945 946 947 948
    /* lxc.cgroup.cpuset.* */
    if (lxcSetCpusetTune(vmdef, properties) < 0)
        goto error;

949 950 951 952
    /* lxc.cgroup.blkio.* */
    if (lxcSetBlkioTune(vmdef, properties) < 0)
        goto error;

953 954
    goto cleanup;

955
 error:
956 957 958
    virDomainDefFree(vmdef);
    vmdef = NULL;

959
 cleanup:
960 961 962 963
    virConfFree(properties);

    return vmdef;
}