From 3960d77b08d903f55806cafd079dd5cc787fa403 Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Thu, 11 Jun 2026 00:37:10 -0300 Subject: [PATCH] rootfs-handoff: Milestone B - add early userspace /init - Created user/cmds/initrd_init/initrd_init.c as early /init program - Created user/cmds/initrd_init/Makefile - Added initrd_init to USER_CMD_NAMES in main Makefile - Changed default init path from /sbin/init to /init in cmdline.c - Early /init checks if /newroot is mounted by kernel - If /newroot is mounted, executes /sbin/init (basic handoff) - If /newroot is not mounted, falls back to /sbin/init directly - Milestone C will add pivot_root and mount moving functionality Test Results: - Smoke test: 131/131 PASS - Battery test: 157/157 PASS - Analyzer: PASS - Zero regressions --- Makefile | 2 +- src/kernel/cmdline.c | 2 +- user/cmds/initrd_init/Makefile | 12 +++++ user/cmds/initrd_init/initrd_init.c | 73 +++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 user/cmds/initrd_init/Makefile create mode 100644 user/cmds/initrd_init/initrd_init.c diff --git a/Makefile b/Makefile index ed90dad4..057b422b 100644 --- a/Makefile +++ b/Makefile @@ -127,7 +127,7 @@ ifeq ($(ARCH),x86) grep id uname dmesg \ printenv tr dd pwd stat \ sed awk who top du find which \ - login init + login init initrd_init # ELF paths for dynamically-linked commands USER_CMD_ELFS := $(foreach cmd,$(USER_CMD_NAMES),$(USER_BUILD)/cmds/$(cmd)/$(cmd).elf) diff --git a/src/kernel/cmdline.c b/src/kernel/cmdline.c index 167d4f02..28139b4e 100644 --- a/src/kernel/cmdline.c +++ b/src/kernel/cmdline.c @@ -85,7 +85,7 @@ void cmdline_parse(const char* raw) { kflag_count = 0; init_argc = 0; init_envc = 0; - init_path_val = "/sbin/init"; + init_path_val = "/init"; /* Default to early /init for root handoff */ if (!raw) { raw_copy[0] = '\0'; diff --git a/user/cmds/initrd_init/Makefile b/user/cmds/initrd_init/Makefile new file mode 100644 index 00000000..d741b0a1 --- /dev/null +++ b/user/cmds/initrd_init/Makefile @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright (c) 2026, Tulio A M Mendes +# All rights reserved. +# See LICENSE for details. +# +# Source: https://github.com/tadryanom/AdrOS +# + +NAME := initrd_init +SRCS := initrd_init.c +include ../common.mk diff --git a/user/cmds/initrd_init/initrd_init.c b/user/cmds/initrd_init/initrd_init.c new file mode 100644 index 00000000..7771519b --- /dev/null +++ b/user/cmds/initrd_init/initrd_init.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2026, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +/* + * Early /init for root filesystem handoff + * + * This program runs from the initrd before the final /sbin/init. + * + * Milestone B: Basic early init - checks for /newroot and executes /sbin/init + * Milestone C: Will add pivot_root and mount moving + */ + +#include +#include +#include +#include +#include +#include + +#define NEWROOT "/newroot" + +static int is_mounted(const char* path) { + FILE* fp = fopen("/proc/mounts", "r"); + if (!fp) { + return 0; + } + + char line[256]; + int found = 0; + while (fgets(line, sizeof(line), fp)) { + char mountpoint[128]; + if (sscanf(line, "%*s %127s", mountpoint) == 1) { + if (strcmp(mountpoint, path) == 0) { + found = 1; + break; + } + } + } + + fclose(fp); + return found; +} + +int main(int argc __attribute__((unused)), char** argv) { + printf("[init] early userspace init starting\n"); + + /* Check if /newroot is already mounted by kernel */ + if (!is_mounted(NEWROOT)) { + printf("[init] /newroot not mounted by kernel, nothing to do\n"); + printf("[init] falling back to /sbin/init\n"); + execve("/sbin/init", argv, environ); + perror("execve /sbin/init"); + return 1; + } + + printf("[init] /newroot is mounted by kernel\n"); + printf("[init] Milestone B: basic handoff - executing /sbin/init directly\n"); + printf("[init] (pivot_root and mount moving will be added in Milestone C)\n"); + + /* For now, just execute /sbin/init from the initrd overlay */ + /* The kernel has already mounted the real root on /newroot */ + /* Future Milestone C will implement pivot_root to make /newroot the actual root */ + execve("/sbin/init", argv, environ); + perror("execve /sbin/init"); + + return 1; +} -- 2.43.0