diff --git a/pkgs/preinit/preinit.c b/pkgs/preinit/preinit.c index 4b5f3f1..c9a1821 100644 --- a/pkgs/preinit/preinit.c +++ b/pkgs/preinit/preinit.c @@ -4,9 +4,13 @@ #include #include #include +#include #include #include #include +#include + +#include #include /* for COMMAND_LINE_SIZE */ @@ -44,6 +48,25 @@ static int fork_exec(char * command, char *args[]) return execve(command, args, NULL); } +static void debug_listdir(const char * path) +{ + DIR *mydir; + struct dirent *myfile; + struct stat mystat; + + char buf[512]; + mydir = opendir(path); + while((myfile = readdir(mydir)) != NULL) + { + sprintf(buf, "%s/%s", path, myfile->d_name); + stat(buf, &mystat); + printf("%llu", mystat.st_size); + printf(" %s\n", myfile->d_name); + } + closedir(mydir); + +} + char banner[] = "Running pre-init...\n"; char buf[COMMAND_LINE_SIZE]; @@ -108,7 +131,12 @@ int main(int argc, char *argv[], char *envp[]) "bind", MS_BIND, NULL)); char *exec_args[] = { "activate", "/target", NULL }; - AVER(fork_exec("/target/persist/activate", exec_args)); + if (fork_exec("/target/persist/activate", exec_args) < 0) { + ERR("failed to activate the system\n"); + pr_u32(errno); ERR ( " - "); ERR(strerror(errno)); ERR("\n"); + goto failsafe; + } + AVER(chdir("/target")); AVER(mount("/target", "/", "bind", MS_BIND | MS_REC, NULL)); @@ -120,13 +148,22 @@ int main(int argc, char *argv[], char *envp[]) AVER(execve("/persist/init", argv, envp)); } +failsafe: + debug_listdir("/"); + debug_listdir("/target"); + ERR("failed to mount the rootfs\n"); ERR("final stand using the failsafe initialization method\n"); ERR("the boot process is manual from now on\n"); argv[0] = "init"; argv[1] = NULL; + // Attempt to unmount the /target mount-bind. + AVER(umount("/target")); AVER(execve("/failsafe-init", argv, envp)); + debug_listdir("/"); + debug_listdir("/target"); + die(); }