Skip to content

Instantly share code, notes, and snippets.

@digetx
Created May 14, 2021 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digetx/698af095d432d597d0835ab9f7b95f55 to your computer and use it in GitHub Desktop.
Save digetx/698af095d432d597d0835ab9f7b95f55 to your computer and use it in GitHub Desktop.
init.c
#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