amend gitignore
This commit is contained in:
+89
@@ -0,0 +1,89 @@
|
||||
#include "container.h"
|
||||
#include "mount.h"
|
||||
|
||||
static int pivot_root(const char *new_root, const char *put_old)
|
||||
{
|
||||
return syscall(SYS_pivot_root, new_root, put_old);
|
||||
}
|
||||
|
||||
int mounts(struct child_config *config)
|
||||
{
|
||||
fprintf(stderr, "=> remounting everything with MS_PRIVATE...");
|
||||
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
|
||||
fprintf(stderr, "failed! %m\n");
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, "remounted.\n");
|
||||
|
||||
fprintf(stderr, "=> making a temp directory and a bind mount there...");
|
||||
char mount_dir[] = "/tmp/tmp.XXXXXX";
|
||||
if (!mkdtemp(mount_dir)) {
|
||||
fprintf(stderr, "failed making a directory!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mount(config->mount_dir, mount_dir, NULL, MS_BIND | MS_PRIVATE, NULL)) {
|
||||
fprintf(stderr, "bind mount failed!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char inner_mount_dir[] = "/tmp/tmp.XXXXXX/oldroot.XXXXXX";
|
||||
memcpy(inner_mount_dir, mount_dir, sizeof(mount_dir) - 1);
|
||||
if (!mkdtemp(inner_mount_dir)) {
|
||||
fprintf(stderr, "failed making the inner directory!\n");
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, "done.\n");
|
||||
|
||||
fprintf(stderr, "=> pivoting root...");
|
||||
if (pivot_root(mount_dir, inner_mount_dir)) {
|
||||
fprintf(stderr, "failed!\n");
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, "done.\n");
|
||||
|
||||
char *old_root_dir = basename(inner_mount_dir);
|
||||
char old_root[sizeof(inner_mount_dir) + 1] = { "/" };
|
||||
strcpy(&old_root[1], old_root_dir);
|
||||
|
||||
fprintf(stderr, "=> unmounting %s...", old_root);
|
||||
if (chdir("/")) {
|
||||
fprintf(stderr, "chdir failed! %m\n");
|
||||
return -1;
|
||||
}
|
||||
if (umount2(old_root, MNT_DETACH)) {
|
||||
fprintf(stderr, "umount failed! %m\n");
|
||||
return -1;
|
||||
}
|
||||
if (rmdir(old_root)) {
|
||||
fprintf(stderr, "rmdir failed! %m\n");
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, "done.\n");
|
||||
|
||||
fprintf(stderr, "=> mounting /proc...");
|
||||
if (mount("proc", "/proc", "proc", 0, NULL)) {
|
||||
fprintf(stderr, "failed: %m\n");
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, "done.\n");
|
||||
|
||||
fprintf(stderr, "=> mounting /dev...");
|
||||
if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID | MS_STRICTATIME, "mode=755,size=65536k")) {
|
||||
fprintf(stderr, "failed: %m\n");
|
||||
return -1;
|
||||
}
|
||||
umask(0);
|
||||
if (mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3))) {
|
||||
fprintf(stderr, "mknod null failed: %m\n");
|
||||
}
|
||||
if (mknod("/dev/zero", S_IFCHR | 0666, makedev(1, 5))) {
|
||||
fprintf(stderr, "mknod zero failed: %m\n");
|
||||
}
|
||||
if (mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9))) {
|
||||
fprintf(stderr, "mknod urandom failed: %m\n");
|
||||
}
|
||||
fprintf(stderr, "done.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user