Created
May 14, 2021 21:43
-
-
Save digetx/698af095d432d597d0835ab9f7b95f55 to your computer and use it in GitHub Desktop.
init.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/mount.h> | |
#define ROOT_DEVICE "/dev/mmcblk0p8" | |
#define ROOT_DIR "/linux" | |
// armv7a-hardfloat-linux-gnueabi-gcc init.c -static -o init | |
int main(int argc, char *argv[]) | |
{ | |
int ret; | |
printf("INIT: mount /dev\n"); | |
ret = mount(NULL, "/dev", "devtmpfs", 0, "mode=0755"); | |
if (ret != 0) | |
goto error; | |
printf("INIT: mount /root\n"); | |
ret = mount(ROOT_DEVICE, "/root", "ext4", 0, NULL); | |
if (ret != 0) | |
goto error; | |
printf("INIT: umount /dev\n"); | |
ret = umount("/dev"); | |
if (ret != 0) | |
goto error; | |
printf("INIT: chrooting\n"); | |
ret = chroot("/root" ROOT_DIR); | |
if (ret != 0) | |
goto error; | |
printf("INIT: mount /\n"); | |
ret = mount("/", "/", "rootfs", MS_BIND, NULL); | |
if (ret != 0) | |
goto error; | |
printf("INIT: run rootfs init\n"); | |
ret = execv("/sbin/init", argv); | |
error: | |
fprintf(stderr, "INIT ERROR: %s\n", strerror(errno)); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment