



























#define _GNU_SOURCE
#include <unistd.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define NEWROOT "/newroot"
#define OLDROOT "/newroot/oldroot"
#define DEV "/dev"
#define PROC "/proc"
#define SYS "/sys"
#define RUN "/run"
static void die(const char *msg) {
perror(msg);
_exit(1);
}
int main(void) {
mount("proc", PROC, "proc", 0, NULL);
mount("sysfs", SYS, "sysfs", 0, NULL);
mount("devtmpfs", DEV, "devtmpfs", 0, NULL);
mount("tmpfs", RUN, "tmpfs", 0, "mode=0755");
if (mount("/dev/mmcblk1p1", NEWROOT, "ext4", 0, NULL) != 0) die("mount newroot");
mkdir(OLDROOT, 0755);
if (syscall(SYS_pivot_root, NEWROOT, OLDROOT) != 0) die("pivot_root");
if (chdir("/") != 0) die("chdir /");
mount("/dev", DEV, NULL, MS_MOVE, NULL);
mount("/proc", PROC, NULL, MS_MOVE, NULL);
mount("/sys", SYS, NULL, MS_MOVE, NULL);
mount("/run", RUN, NULL, MS_MOVE, NULL);
umount2("/oldroot", MNT_DETACH);
rmdir("/oldroot");
execl("/sbin/init.real", "init", NULL);
die("exec init failed");
return 0;
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。