ostbuild: Update user-chroot to bind mount /proc and /dev

This commit is contained in:
Colin Walters 2011-12-06 10:43:01 -05:00
parent ce7345d51e
commit e68d0d25c0

View File

@ -24,10 +24,15 @@
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/mount.h>
#include <linux/securebits.h>
#include <sched.h>
typedef unsigned int bool;
static void
fatal_errno (const char *message) __attribute__ ((noreturn));
@ -39,6 +44,22 @@ fatal_errno (const char *message)
exit (1);
}
static void
initialize_chroot (const char *path)
{
char *subpath;
asprintf (&subpath, "%s/proc", path);
if (mount ("/proc", subpath, NULL, MS_BIND, NULL) < 0)
fatal_errno ("bind mounting proc");
free (subpath);
asprintf (&subpath, "%s/dev", path);
if (mount ("/dev", subpath, NULL, MS_BIND, NULL) < 0)
fatal_errno ("bind mounting dev");
free (subpath);
}
int
main (int argc,
char **argv)
@ -74,6 +95,11 @@ main (int argc,
SECBIT_NOROOT | SECBIT_NOROOT_LOCKED) < 0)
fatal_errno ("prctl");
if (unshare (CLONE_NEWNS) < 0)
fatal_errno ("unshare (CLONE_NEWNS)");
initialize_chroot (chroot_dir);
if (chroot (chroot_dir) < 0)
fatal_errno ("chroot");
if (chdir ("/") < 0)