lxc_container.c 25.3 KB
Newer Older
1
/*
2 3
 * Copyright (C) 2008-2010 Red Hat, Inc.
 * Copyright (C) 2008 IBM Corp.
4 5 6 7 8
 *
 * lxc_container.c: file description
 *
 * Authors:
 *  David L. Leskovec <dlesko at linux.vnet.ibm.com>
9
 *  Daniel P. Berrange <berrange@redhat.com>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * 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>

#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
31
#include <stdio.h>
32 33
#include <sys/ioctl.h>
#include <sys/mount.h>
34
#include <sys/wait.h>
35
#include <sys/stat.h>
36
#include <unistd.h>
37 38 39 40 41 42 43
#include <mntent.h>

/* Yes, we want linux private one, for _syscall2() macro */
#include <linux/unistd.h>

/* For MS_MOVE */
#include <linux/fs.h>
44

D
Daniel P. Berrange 已提交
45
#if HAVE_CAPNG
46
# include <cap-ng.h>
D
Daniel P. Berrange 已提交
47
#endif
48

49
#include "virterror_internal.h"
50
#include "logging.h"
51 52
#include "lxc_container.h"
#include "util.h"
53
#include "memory.h"
54
#include "veth.h"
55
#include "uuid.h"
56
#include "files.h"
57
#include "command.h"
58

59 60
#define VIR_FROM_THIS VIR_FROM_LXC

61 62 63 64 65 66
/*
 * GLibc headers are behind the kernel, so we define these
 * constants if they're not present already.
 */

#ifndef CLONE_NEWPID
67
# define CLONE_NEWPID  0x20000000
68 69
#endif
#ifndef CLONE_NEWUTS
70
# define CLONE_NEWUTS  0x04000000
71 72
#endif
#ifndef CLONE_NEWUSER
73
# define CLONE_NEWUSER 0x10000000
74 75
#endif
#ifndef CLONE_NEWIPC
76
# define CLONE_NEWIPC  0x08000000
77 78
#endif
#ifndef CLONE_NEWNET
79
# define CLONE_NEWNET  0x40000000 /* New network namespace */
80 81 82 83 84 85 86 87
#endif

/* messages between parent and container */
typedef char lxc_message_t;
#define LXC_CONTINUE_MSG 'c'

typedef struct __lxc_child_argv lxc_child_argv_t;
struct __lxc_child_argv {
88
    virDomainDefPtr config;
89 90
    unsigned int nveths;
    char **veths;
91 92 93 94 95
    int monitor;
    char *ttyPath;
};


96
/**
97
 * lxcContainerBuildInitCmd:
98
 * @vmDef: pointer to vm definition structure
99
 *
100
 * Build a virCommandPtr for launching the container 'init' process
101
 *
102
 * Returns a virCommandPtr
103
 */
104
static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
105
{
106
    char uuidstr[VIR_UUID_STRING_BUFLEN];
107
    virCommandPtr cmd;
108 109 110

    virUUIDFormat(vmDef->uuid, uuidstr);

111 112 113 114 115 116 117
    cmd = virCommandNew(vmDef->os.init);

    virCommandAddEnvString(cmd, "PATH=/bin:/sbin");
    virCommandAddEnvString(cmd, "TERM=linux");
    virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr);
    virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);

118
    return cmd;
119 120 121
}

/**
122
 * lxcContainerSetStdio:
123 124
 * @control: control FD from parent
 * @ttyfd: FD of tty to set as the container console
125 126 127 128 129 130
 *
 * Sets the given tty as the primary conosole for the container as well as
 * stdout, stdin and stderr.
 *
 * Returns 0 on success or -1 in case of error
 */
131
static int lxcContainerSetStdio(int control, int ttyfd)
132 133
{
    int rc = -1;
134
    int open_max, i;
135 136

    if (setsid() < 0) {
137
        virReportSystemError(errno, "%s",
138
                             _("setsid failed"));
139
        goto cleanup;
140 141 142
    }

    if (ioctl(ttyfd, TIOCSCTTY, NULL) < 0) {
143
        virReportSystemError(errno, "%s",
144
                             _("ioctl(TIOCSTTY) failed"));
145 146 147
        goto cleanup;
    }

148 149 150 151
    /* Just in case someone forget to set FD_CLOEXEC, explicitly
     * close all FDs before executing the container */
    open_max = sysconf (_SC_OPEN_MAX);
    for (i = 0; i < open_max; i++)
152 153 154 155
        if (i != ttyfd && i != control) {
            int tmpfd = i;
            VIR_FORCE_CLOSE(tmpfd);
        }
156 157

    if (dup2(ttyfd, 0) < 0) {
158
        virReportSystemError(errno, "%s",
159
                             _("dup2(stdin) failed"));
160 161 162 163
        goto cleanup;
    }

    if (dup2(ttyfd, 1) < 0) {
164
        virReportSystemError(errno, "%s",
165
                             _("dup2(stdout) failed"));
166 167 168 169
        goto cleanup;
    }

    if (dup2(ttyfd, 2) < 0) {
170
        virReportSystemError(errno, "%s",
171
                             _("dup2(stderr) failed"));
172 173 174 175 176 177 178 179 180 181
        goto cleanup;
    }

    rc = 0;

cleanup:
    return rc;
}

/**
182
 * lxcContainerSendContinue:
183
 * @control: control FD to child
184
 *
185 186
 * Sends the continue message via the socket pair stored in the vm
 * structure.
187 188 189
 *
 * Returns 0 on success or -1 in case of error
 */
190
int lxcContainerSendContinue(int control)
191 192
{
    int rc = -1;
193 194
    lxc_message_t msg = LXC_CONTINUE_MSG;
    int writeCount = 0;
195

196 197 198
    writeCount = safewrite(control, &msg, sizeof(msg));
    if (writeCount != sizeof(msg)) {
        goto error_out;
199 200
    }

201 202 203
    rc = 0;
error_out:
    return rc;
204 205
}

206
/**
207
 * lxcContainerWaitForContinue:
208
 * @control: Control FD from parent
209 210 211 212 213 214 215
 *
 * This function will wait for the container continue message from the
 * parent process.  It will send this message on the socket pair stored in
 * the vm structure once it has completed the post clone container setup.
 *
 * Returns 0 on success or -1 in case of error
 */
216
int lxcContainerWaitForContinue(int control)
217 218 219 220
{
    lxc_message_t msg;
    int readLen;

221
    readLen = saferead(control, &msg, sizeof(msg));
222 223 224
    if (readLen != sizeof(msg) ||
        msg != LXC_CONTINUE_MSG) {
        return -1;
225 226
    }

227
    return 0;
228 229
}

230

231
/**
232
 * lxcContainerRenameAndEnableInterfaces:
233 234
 * @nveths: number of interfaces
 * @veths: interface names
235
 *
236 237 238
 * This function will rename the interfaces to ethN
 * with id ascending order from zero and enable the
 * renamed interfaces for this container.
239 240 241
 *
 * Returns 0 on success or nonzero in case of error
 */
242 243
static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths,
                                                 char **veths)
244 245
{
    int rc = 0;
246
    unsigned int i;
247
    char *newname = NULL;
248

249
    for (i = 0 ; i < nveths ; i++) {
250 251 252
        if (virAsprintf(&newname, "eth%d", i) < 0) {
            virReportOOMError();
            rc = -1;
253
            goto error_out;
254
        }
255

256
        VIR_DEBUG("Renaming %s to %s", veths[i], newname);
257
        rc = setInterfaceName(veths[i], newname);
258
        if (rc < 0)
259 260
            goto error_out;

261
        VIR_DEBUG("Enabling %s", newname);
262
        rc = vethInterfaceUpOrDown(newname, 1);
263
        if (rc < 0)
264
            goto error_out;
265

266
        VIR_FREE(newname);
267 268 269
    }

    /* enable lo device only if there were other net devices */
270
    if (veths)
271 272 273
        rc = vethInterfaceUpOrDown("lo", 1);

error_out:
274
    VIR_FREE(newname);
275 276 277
    return rc;
}

278

279
/*_syscall2(int, pivot_root, char *, newroot, const char *, oldroot)*/
280 281 282 283 284 285 286 287 288 289 290 291
extern int pivot_root(const char * new_root,const char * put_old);

static int lxcContainerChildMountSort(const void *a, const void *b)
{
  const char **sa = (const char**)a;
  const char **sb = (const char**)b;

  /* Delibrately reversed args - we need to unmount deepest
     children first */
  return strcmp(*sb, *sa);
}

292
#ifndef MS_REC
293
# define MS_REC          16384
294 295 296
#endif

#ifndef MNT_DETACH
297
# define MNT_DETACH      0x00000002
298 299 300
#endif

#ifndef MS_PRIVATE
301
# define MS_PRIVATE              (1<<18)
302 303 304
#endif

#ifndef MS_SLAVE
305
# define MS_SLAVE                (1<<19)
306 307
#endif

308 309
static int lxcContainerPivotRoot(virDomainFSDefPtr root)
{
M
Mark McLoughlin 已提交
310
    int rc, ret;
311
    char *oldroot = NULL, *newroot = NULL;
312

M
Mark McLoughlin 已提交
313 314
    ret = -1;

315 316
    /* root->parent must be private, so make / private. */
    if (mount("", "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
317
        virReportSystemError(errno, "%s",
318
                             _("Failed to make root private"));
319
        goto err;
320 321
    }

322
    if (virAsprintf(&oldroot, "%s/.oldroot", root->src) < 0) {
323
        virReportOOMError();
324
        goto err;
325 326
    }

L
Laine Stump 已提交
327
    if ((rc = virFileMakePath(oldroot)) != 0) {
328
        virReportSystemError(rc,
329
                             _("Failed to create %s"),
330
                             oldroot);
331 332 333 334 335
        goto err;
    }

    /* Create a tmpfs root since old and new roots must be
     * on separate filesystems */
336
    if (mount("tmprootfs", oldroot, "tmpfs", 0, NULL) < 0) {
337
        virReportSystemError(errno,
338
                             _("Failed to mount empty tmpfs at %s"),
339 340 341
                             oldroot);
        goto err;
    }
M
Mark McLoughlin 已提交
342

343 344
    /* Create a directory called 'new' in tmpfs */
    if (virAsprintf(&newroot, "%s/new", oldroot) < 0) {
345
        virReportOOMError();
346 347 348
        goto err;
    }

L
Laine Stump 已提交
349
    if ((rc = virFileMakePath(newroot)) != 0) {
350
        virReportSystemError(rc,
351
                             _("Failed to create %s"),
352 353 354 355 356 357
                             newroot);
        goto err;
    }

    /* ... and mount our root onto it */
    if (mount(root->src, newroot, NULL, MS_BIND|MS_REC, NULL) < 0) {
358
        virReportSystemError(errno,
359
                             _("Failed to bind new root %s into tmpfs"),
360 361 362 363 364 365
                             root->src);
        goto err;
    }

    /* Now we chroot into the tmpfs, then pivot into the
     * root->src bind-mounted onto '/new' */
366
    if (chdir(newroot) < 0) {
367
        virReportSystemError(errno,
368
                             _("Failed to chroot into %s"), newroot);
369
        goto err;
370 371 372 373
    }

    /* The old root directory will live at /.oldroot after
     * this and will soon be unmounted completely */
374
    if (pivot_root(".", ".oldroot") < 0) {
375
        virReportSystemError(errno, "%s",
376
                             _("Failed to pivot root"));
377
        goto err;
378 379 380
    }

    /* CWD is undefined after pivot_root, so go to / */
381 382 383
    if (chdir("/") < 0)
        goto err;

M
Mark McLoughlin 已提交
384 385 386
    ret = 0;

err:
387 388 389
    VIR_FREE(oldroot);
    VIR_FREE(newroot);

M
Mark McLoughlin 已提交
390
    return ret;
391 392
}

393 394

static int lxcContainerMountBasicFS(virDomainFSDefPtr root)
395 396
{
    const struct {
397 398 399 400 401 402 403 404 405 406
        const char *src;
        const char *dst;
        const char *type;
    } mnts[] = {
        { "/dev", "/dev", "tmpfs" },
        { "/proc", "/proc", "proc" },
        { "/sys", "/sys", "sysfs" },
#if WITH_SELINUX
        { "none", "/selinux", "selinuxfs" },
#endif
407
    };
408
    int i, rc = -1;
409
    char *devpts;
410

411
    if (virAsprintf(&devpts, "/.oldroot%s/dev/pts", root->src) < 0) {
412
        virReportOOMError();
413
        return rc;
414
    }
415 416

    for (i = 0 ; i < ARRAY_CARDINALITY(mnts) ; i++) {
L
Laine Stump 已提交
417
        if (virFileMakePath(mnts[i].dst) != 0) {
418
            virReportSystemError(errno,
419
                                 _("Failed to mkdir %s"),
420
                                 mnts[i].src);
421
            goto cleanup;
422 423
        }
        if (mount(mnts[i].src, mnts[i].dst, mnts[i].type, 0, NULL) < 0) {
424
            virReportSystemError(errno,
425
                                 _("Failed to mount %s on %s"),
426
                                 mnts[i].type, mnts[i].type);
427
            goto cleanup;
428
        }
429
    }
430

L
Laine Stump 已提交
431
    if ((rc = virFileMakePath("/dev/pts") != 0)) {
432
        virReportSystemError(rc, "%s",
433
                             _("Cannot create /dev/pts"));
434
        goto cleanup;
435
    }
436 437 438

    VIR_DEBUG("Trying to move %s to %s", devpts, "/dev/pts");
    if ((rc = mount(devpts, "/dev/pts", NULL, MS_MOVE, NULL)) < 0) {
439
        virReportSystemError(errno, "%s",
440
                             _("Failed to mount /dev/pts in container"));
441
        goto cleanup;
442
    }
443 444 445 446

    rc = 0;

 cleanup:
447 448
    VIR_FREE(devpts);

449
    return rc;
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
}

static int lxcContainerPopulateDevices(void)
{
    int i;
    const struct {
        int maj;
        int min;
        mode_t mode;
        const char *path;
    } devs[] = {
        { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_NULL, 0666, "/dev/null" },
        { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_ZERO, 0666, "/dev/zero" },
        { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_FULL, 0666, "/dev/full" },
        { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_RANDOM, 0666, "/dev/random" },
        { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_URANDOM, 0666, "/dev/urandom" },
    };
467 468 469 470

    /* Populate /dev/ with a few important bits */
    for (i = 0 ; i < ARRAY_CARDINALITY(devs) ; i++) {
        dev_t dev = makedev(devs[i].maj, devs[i].min);
471
        if (mknod(devs[i].path, S_IFCHR, dev) < 0 ||
472
            chmod(devs[i].path, devs[i].mode)) {
473
            virReportSystemError(errno,
474
                                 _("Failed to make device %s"),
475
                                 devs[i].path);
476 477 478 479
            return -1;
        }
    }

480 481
    if (access("/dev/pts/ptmx", W_OK) == 0) {
        if (symlink("/dev/pts/ptmx", "/dev/ptmx") < 0) {
482
            virReportSystemError(errno, "%s",
483
                                 _("Failed to create symlink /dev/ptmx to /dev/pts/ptmx"));
484 485 486 487
            return -1;
        }
    } else {
        dev_t dev = makedev(LXC_DEV_MAJ_TTY, LXC_DEV_MIN_PTMX);
488
        if (mknod("/dev/ptmx", S_IFCHR, dev) < 0 ||
489
            chmod("/dev/ptmx", 0666)) {
490
            virReportSystemError(errno, "%s",
491
                                 _("Failed to make device /dev/ptmx"));
492 493 494 495
            return -1;
        }
    }

496 497 498 499 500 501 502 503 504
    /* XXX we should allow multiple consoles per container
     * for tty2, tty3, etc, but the domain XML does not
     * handle this yet
     */
    if (symlink("/dev/pts/0", "/dev/tty1") < 0) {
        virReportSystemError(errno, "%s",
                             _("Failed to symlink /dev/pts/0 to /dev/tty1"));
        return -1;
    }
505 506 507 508 509
    if (symlink("/dev/pts/0", "/dev/console") < 0) {
        virReportSystemError(errno, "%s",
                             _("Failed to symlink /dev/pts/0 to /dev/console"));
        return -1;
    }
510

511 512 513 514 515 516
    return 0;
}


static int lxcContainerMountNewFS(virDomainDefPtr vmDef)
{
517
    int i;
518 519

    /* Pull in rest of container's mounts */
520
    for (i = 0 ; i < vmDef->nfss ; i++) {
521
        char *src;
522
        if (STREQ(vmDef->fss[i]->dst, "/"))
523
            continue;
524
        /* XXX fix */
525
        if (vmDef->fss[i]->type != VIR_DOMAIN_FS_TYPE_MOUNT)
526 527
            continue;

528
        if (virAsprintf(&src, "/.oldroot/%s", vmDef->fss[i]->src) < 0) {
529
            virReportOOMError();
530 531 532
            return -1;
        }

L
Laine Stump 已提交
533
        if (virFileMakePath(vmDef->fss[i]->dst) != 0) {
534
            virReportSystemError(errno,
535
                                 _("Failed to create %s"),
536
                                 vmDef->fss[i]->dst);
537
            VIR_FREE(src);
538 539 540
            return -1;
        }
        if (mount(src, vmDef->fss[i]->dst, NULL, MS_BIND, NULL) < 0) {
541
            virReportSystemError(errno,
542 543 544
                                 _("Failed to mount %s at %s"),
                                 src, vmDef->fss[i]->dst);
            VIR_FREE(src);
545 546 547 548 549 550 551 552 553 554 555
            return -1;
        }
        VIR_FREE(src);
    }

    return 0;
}


static int lxcContainerUnmountOldFS(void)
{
556
    struct mntent mntent;
557 558 559 560
    char **mounts = NULL;
    int nmounts = 0;
    FILE *procmnt;
    int i;
561
    char mntbuf[1024];
562 563

    if (!(procmnt = setmntent("/proc/mounts", "r"))) {
564
        virReportSystemError(errno, "%s",
565
                             _("Failed to read /proc/mounts"));
566 567
        return -1;
    }
568
    while (getmntent_r(procmnt, &mntent, mntbuf, sizeof(mntbuf)) != NULL) {
569
        VIR_DEBUG("Got %s", mntent.mnt_dir);
570
        if (!STRPREFIX(mntent.mnt_dir, "/.oldroot"))
571 572 573 574
            continue;

        if (VIR_REALLOC_N(mounts, nmounts+1) < 0) {
            endmntent(procmnt);
575
            virReportOOMError();
576 577
            return -1;
        }
578
        if (!(mounts[nmounts++] = strdup(mntent.mnt_dir))) {
579
            endmntent(procmnt);
580
            virReportOOMError();
581 582 583 584 585
            return -1;
        }
    }
    endmntent(procmnt);

586 587 588
    if (mounts)
        qsort(mounts, nmounts, sizeof(mounts[0]),
              lxcContainerChildMountSort);
589 590

    for (i = 0 ; i < nmounts ; i++) {
591
        VIR_DEBUG("Umount %s", mounts[i]);
592
        if (umount(mounts[i]) < 0) {
593
            virReportSystemError(errno,
594
                                 _("Failed to unmount '%s'"),
595
                                 mounts[i]);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
            return -1;
        }
        VIR_FREE(mounts[i]);
    }
    VIR_FREE(mounts);

    return 0;
}


/* Got a FS mapped to /, we're going the pivot_root
 * approach to do a better-chroot-than-chroot
 * this is based on this thread http://lkml.org/lkml/2008/3/5/29
 */
static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
                                      virDomainFSDefPtr root)
{
613
    /* Gives us a private root, leaving all parent OS mounts on /.oldroot */
614 615 616
    if (lxcContainerPivotRoot(root) < 0)
        return -1;

617 618
    /* Mounts the core /proc, /sys, /dev, /dev/pts filesystems */
    if (lxcContainerMountBasicFS(root) < 0)
619 620
        return -1;

621
    /* Populates device nodes in /dev/ */
622 623 624
    if (lxcContainerPopulateDevices() < 0)
        return -1;

625
    /* Sets up any non-root mounts from guest config */
626 627 628
    if (lxcContainerMountNewFS(vmDef) < 0)
        return -1;

629
    /* Gets rid of all remaining mounts from host OS, including /.oldroot itself */
630 631 632 633 634 635 636 637 638 639
    if (lxcContainerUnmountOldFS() < 0)
        return -1;

    return 0;
}

/* Nothing mapped to /, we're using the main root,
   but with extra stuff mapped in */
static int lxcContainerSetupExtraMounts(virDomainDefPtr vmDef)
{
640
    int i;
641

642
    if (mount("", "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) {
643
        virReportSystemError(errno, "%s",
644
                             _("Failed to make / slave"));
645 646
        return -1;
    }
647
    for (i = 0 ; i < vmDef->nfss ; i++) {
648
        /* XXX fix to support other mount types */
649
        if (vmDef->fss[i]->type != VIR_DOMAIN_FS_TYPE_MOUNT)
650 651
            continue;

652 653
        if (mount(vmDef->fss[i]->src,
                  vmDef->fss[i]->dst,
654 655 656
                  NULL,
                  MS_BIND,
                  NULL) < 0) {
657
            virReportSystemError(errno,
658
                                 _("Failed to mount %s at %s"),
659 660
                                 vmDef->fss[i]->src,
                                 vmDef->fss[i]->dst);
661 662 663 664 665 666
            return -1;
        }
    }

    /* mount /proc */
    if (mount("lxcproc", "/proc", "proc", 0, NULL) < 0) {
667
        virReportSystemError(errno, "%s",
668
                             _("Failed to mount /proc"));
669 670 671 672 673 674
        return -1;
    }

    return 0;
}

675 676
static int lxcContainerSetupMounts(virDomainDefPtr vmDef,
                                   virDomainFSDefPtr root)
677 678 679 680 681 682 683
{
    if (root)
        return lxcContainerSetupPivotRoot(vmDef, root);
    else
        return lxcContainerSetupExtraMounts(vmDef);
}

D
Daniel P. Berrange 已提交
684 685 686 687 688 689 690

/*
 * This is running as the 'init' process insid the container.
 * It removes some capabilities that could be dangerous to
 * host system, since they are not currently "containerized"
 */
static int lxcContainerDropCapabilities(void)
691
{
D
Daniel P. Berrange 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705
#if HAVE_CAPNG
    int ret;

    capng_get_caps_process();

    if ((ret = capng_updatev(CAPNG_DROP,
                             CAPNG_EFFECTIVE | CAPNG_PERMITTED |
                             CAPNG_INHERITABLE | CAPNG_BOUNDING_SET,
                             CAP_SYS_BOOT, /* No use of reboot */
                             CAP_SYS_MODULE, /* No kernel module loading */
                             CAP_SYS_TIME, /* No changing the clock */
                             CAP_AUDIT_CONTROL, /* No messing with auditing status */
                             CAP_MAC_ADMIN, /* No messing with LSM config */
                             -1 /* sentinal */)) < 0) {
706
        lxcError(VIR_ERR_INTERNAL_ERROR,
707
                 _("Failed to remove capabilities: %d"), ret);
D
Daniel P. Berrange 已提交
708 709
        return -1;
    }
710

D
Daniel P. Berrange 已提交
711
    if ((ret = capng_apply(CAPNG_SELECT_BOTH)) < 0) {
712
        lxcError(VIR_ERR_INTERNAL_ERROR,
713
                 _("Failed to apply capabilities: %d"), ret);
D
Daniel P. Berrange 已提交
714
        return -1;
715
    }
D
Daniel P. Berrange 已提交
716

717 718 719 720 721
    /* We do not need to call capng_lock() in this case. The bounding
     * set restriction will prevent them reacquiring sys_boot/module/time,
     * etc which is all that matters for the container. Once inside the
     * container it is fine for SECURE_NOROOT / SECURE_NO_SETUID_FIXUP to
     * be unmasked  - they can never escape the bounding set. */
D
Daniel P. Berrange 已提交
722 723

#else
724
    VIR_WARN("libcap-ng support not compiled in, unable to clear capabilities");
D
Daniel Veillard 已提交
725
#endif
726 727 728 729
    return 0;
}


730
/**
731 732
 * lxcContainerChild:
 * @data: pointer to container arguments
733 734 735 736 737 738 739 740 741
 *
 * This function is run in the process clone()'d in lxcStartContainer.
 * Perform a number of container setup tasks:
 *     Setup container file system
 *     mount container /proca
 * Then exec's the container init
 *
 * Returns 0 on success or -1 in case of error
 */
742
static int lxcContainerChild( void *data )
743
{
744
    lxc_child_argv_t *argv = data;
745
    virDomainDefPtr vmDef = argv->config;
746
    int ttyfd = -1;
747
    int ret = -1;
748
    char *ttyPath = NULL;
749
    virDomainFSDefPtr root;
750
    virCommandPtr cmd = NULL;
751 752

    if (NULL == vmDef) {
753
        lxcError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
754
                 "%s", _("lxcChild() passed invalid vm definition"));
755
        goto cleanup;
756 757
    }

758 759 760
    cmd = lxcContainerBuildInitCmd(vmDef);
    virCommandWriteArgLog(cmd, 1);

761
    root = virDomainGetRootFilesystem(vmDef);
762

763 764
    if (root) {
        if (virAsprintf(&ttyPath, "%s%s", root->src, argv->ttyPath) < 0) {
765
            virReportOOMError();
766
            goto cleanup;
767 768 769
        }
    } else {
        if (!(ttyPath = strdup(argv->ttyPath))) {
770
            virReportOOMError();
771
            goto cleanup;
772 773
        }
    }
774
    VIR_DEBUG("Container TTY path: %s", ttyPath);
775 776

    ttyfd = open(ttyPath, O_RDWR|O_NOCTTY);
777
    if (ttyfd < 0) {
778
        virReportSystemError(errno,
779
                             _("Failed to open tty %s"),
780
                             ttyPath);
781
        goto cleanup;
782
    }
783

784
    if (lxcContainerSetupMounts(vmDef, root) < 0)
785
        goto cleanup;
786

787
    /* Wait for interface devices to show up */
788 789 790
    if (lxcContainerWaitForContinue(argv->monitor) < 0) {
        virReportSystemError(errno, "%s",
                             _("Failed to read the container continue message"));
791
        goto cleanup;
792 793
    }
    VIR_DEBUG("Received container continue message");
794

795 796
    /* rename and enable interfaces */
    if (lxcContainerRenameAndEnableInterfaces(argv->nveths,
797
                                              argv->veths) < 0) {
798
        goto cleanup;
799
    }
800

801
    /* drop a set of root capabilities */
D
Daniel P. Berrange 已提交
802
    if (lxcContainerDropCapabilities() < 0)
803
        goto cleanup;
804

805 806 807
    if (lxcContainerSetStdio(argv->monitor, ttyfd) < 0) {
        goto cleanup;
    }
808

809
    ret = 0;
810
cleanup:
811 812
    VIR_FREE(ttyPath);
    VIR_FORCE_CLOSE(ttyfd);
813
    VIR_FORCE_CLOSE(argv->monitor);
814 815 816 817 818 819

    if (ret == 0) {
        /* this function will only return if an error occured */
        ret = virCommandExec(cmd);
    }

820 821
    virCommandFree(cmd);
    return ret;
822
}
823

824 825
static int userns_supported(void)
{
826 827 828 829 830 831
#if 1
    /*
     * put off using userns until uid mapping is implemented
     */
    return 0;
#else
832
    return lxcContainerAvailable(LXC_CONTAINER_FEATURE_USER) == 0;
833
#endif
834 835
}

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
const char *lxcContainerGetAlt32bitArch(const char *arch)
{
    /* Any Linux 64bit arch which has a 32bit
     * personality available should be listed here */
    if (STREQ(arch, "x86_64"))
        return "i686";
    if (STREQ(arch, "s390x"))
        return "s390";
    if (STREQ(arch, "ppc64"))
        return "ppc";
    if (STREQ(arch, "parisc64"))
        return "parisc";
    if (STREQ(arch, "sparc64"))
        return "sparc";
    if (STREQ(arch, "mips64"))
        return "mips";

    return NULL;
}


857 858
/**
 * lxcContainerStart:
859 860 861 862 863
 * @def: pointer to virtual machine structure
 * @nveths: number of interfaces
 * @veths: interface names
 * @control: control FD to the container
 * @ttyPath: path of tty to set as the container console
864 865 866 867 868
 *
 * Starts a container process by calling clone() with the namespace flags
 *
 * Returns PID of container on success or -1 in case of error
 */
869
int lxcContainerStart(virDomainDefPtr def,
870 871
                      unsigned int nveths,
                      char **veths,
872 873 874 875 876 877 878
                      int control,
                      char *ttyPath)
{
    pid_t pid;
    int flags;
    int stacksize = getpagesize() * 4;
    char *stack, *stacktop;
879
    lxc_child_argv_t args = { def, nveths, veths, control, ttyPath };
880 881 882

    /* allocate a stack for the container */
    if (VIR_ALLOC_N(stack, stacksize) < 0) {
883
        virReportOOMError();
884 885 886 887
        return -1;
    }
    stacktop = stack + stacksize;

888 889
    flags = CLONE_NEWPID|CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|SIGCHLD;

890
    if (userns_supported()) {
891
        VIR_DEBUG("Enable user namespaces");
892
        flags |= CLONE_NEWUSER;
893
    }
894

895
    if (def->nets != NULL) {
896
        VIR_DEBUG("Enable network namespaces");
897
        flags |= CLONE_NEWNET;
898
    }
899 900 901

    pid = clone(lxcContainerChild, stacktop, flags, &args);
    VIR_FREE(stack);
902
    VIR_DEBUG("clone() completed, new container PID is %d", pid);
903 904

    if (pid < 0) {
905
        virReportSystemError(errno, "%s",
906
                             _("Failed to run clone container"));
907 908 909 910 911 912
        return -1;
    }

    return pid;
}

913 914
ATTRIBUTE_NORETURN static int
lxcContainerDummyChild(void *argv ATTRIBUTE_UNUSED)
915 916 917 918 919 920
{
    _exit(0);
}

int lxcContainerAvailable(int features)
{
921
    int flags = CLONE_NEWPID|CLONE_NEWNS|CLONE_NEWUTS|
922 923 924 925 926 927
        CLONE_NEWIPC|SIGCHLD;
    int cpid;
    char *childStack;
    char *stack;
    int childStatus;

928 929 930
    if (features & LXC_CONTAINER_FEATURE_USER)
        flags |= CLONE_NEWUSER;

931 932 933 934
    if (features & LXC_CONTAINER_FEATURE_NET)
        flags |= CLONE_NEWNET;

    if (VIR_ALLOC_N(stack, getpagesize() * 4) < 0) {
935
        VIR_DEBUG("Unable to allocate stack");
936 937 938 939 940 941 942 943
        return -1;
    }

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

    cpid = clone(lxcContainerDummyChild, childStack, flags, NULL);
    VIR_FREE(stack);
    if (cpid < 0) {
944
        char ebuf[1024];
945
        VIR_DEBUG("clone call returned %s, container support is not enabled",
946
              virStrerror(errno, ebuf, sizeof ebuf));
947 948 949 950 951 952
        return -1;
    } else {
        waitpid(cpid, &childStatus, 0);
    }

    return 0;
953
}