From 9fcfae3eae0e3343d50ef711567cfc233254a0cd Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 23:28:50 +0200 Subject: [PATCH] feat(recovery): strengthen debugging capabilities of preinit Signed-off-by: Raito Bezarius --- pkgs/preinit/preinit.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) 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(); }