Repository navigation
11 init
Language: English | 中文
Goal: starting from the moment the kernel loads the first process, fully understand how userland is built up layer by layer; understand the
.rdboot scripts — all the way until a command-line prompt appears in front of you.
Recall Ch. 10: at the end of kernel startup, load_init_proc() is called
to read /sbin/init's ELF straight off the SD card and create a process.
This process has a few special traits:
- pid = 0 (EwokOS's process numbering starts from 0);
- uid = -1 (a special mark: loaded by the kernel, holding the highest trust);
- It is the ancestor of all processes, responsible for adopting orphans (Ch. 07).
Look at main in init.c:
int main(int argc, char** argv) {
if(((int16_t)getuid()) >= 0) { // not loaded by the kernel? refuse to run
klog("process 'init' can only loaded by kernel!\n");
return -1;
}
if(getpid() != 0) // not pid 0? then you're a core's idle process
idle();
klog("\n[init process started]\n");
run_before_vfs("/sbin/core"); // ① name service + system coordination
run_before_vfs("/sbin/vfsd"); // ② the virtual filesystem
run_before_vfs("/sbin/sdfsd"); // ③ the SD card ext2 filesystem
switch_root(); // ④ run the boot scripts
while(true)
usleep(100000); // init never exits
}Note the timing of ①②③: at this point the VFS doesn't exist yet and no
filesystem is mounted! So these three processes cannot be started by
"loading from a file" — they use run_before_vfs:
static void run_before_vfs(const char* cmd) {
int pid = fork();
if(pid == 0) {
exec_from_sd(cmd); // use the kernel-mode SD driver to read the ELF
// from ext2 directly, then load it via SYS_EXEC_ELF
}
else
ipc_wait_ready(pid); // the parent waits until the service is truly ready
}This is the classic "chicken and egg" solution: the filesystem services themselves must be started by unconventional means first.
With the three core services in place, the filesystem is usable, and init
moves on to the normal flow — executing /etc/init0.rd ~ /etc/init7.rd
in order (only those that exist), and finally /etc/init.rd:
static void switch_root(void) {
char initfile[32];
uint8_t i = 0;
while(i < 8) {
snprintf(initfile, 31, "/etc/init%d.rd", i);
if(run_init(initfile) != 0)
break;
i++;
}
run_init("/etc/init.rd");
}
// inside run_init: fork() + proc_exec("/bin/shell <initfile>")In other words: the boot scripts are interpreted and executed by the shell.
Take the Pi's basic boot script etc/basic/init.rd as an example:
@/bin/ipcserv /drivers/raspix/uartd /dev/tty0 # UART driver (the terminal)
@/bin/ipcserv /drivers/raspix/cpud /dev/cpu # CPU info driver
@set_stdio /dev/tty0 # hook stdio up to the UART
@/bin/ipcserv /drivers/timerd # the timer device
@/bin/ipcserv /drivers/piped /dev/pipe0 # pipes
@/bin/ipcserv /drivers/ramfsd /tmp # the RAM filesystem
@/bin/ipcserv /drivers/nulld /dev/null
@/bin/ipcserv /sbin/sessiond # session management
@/bin/bgrun /bin/session -r -t /dev/tty0 # start a terminal session → the prompt appears!Lines starting with @ are special shell directives:
| Directive | Effect |
|---|---|
@/bin/ipcserv <program> [mount point] |
start a program as a background service (can register with core) |
@/bin/bgrun <command> |
run in the background |
@set_stdio <device> |
switch the standard input/output device |
@export VAR=value |
set an environment variable (e.g. TZ timezone, XTHEME theme) |
@echo ... |
The full (graphical) system's script is at
etc/xwin/init.rd; it
starts the display driver, fonts, the splash screen, the X server, and so
on in sequence. Every @/bin/splash -m "..." -p xx line advances the boot
progress bar once.
Summary of the configuration file system: the kernel config is
/etc/kernel/kernel.conf(simple key-value pairs); each service uses JSON config (e.g./etc/console.json); boot orchestration uses.rdscripts. All three follow "whoever uses it reads it" — there is no central configuration manager.
The boot script's last line, /bin/session -r -t /dev/tty0, starts a
terminal session:
session ──► opens /dev/tty0 (the serial device file, backed by the uartd process)
│
└──► fork + exec /bin/shell
│
└──► infinite loop: read a line → parse → fork+exec the command
→ wait → print the prompt
shell (system/basic/bin/shell/) is the
program you interact with behind the prompt. What it does is familiar to
every Unix veteran:
- Read a line from standard input;
- Parse the command and arguments (supporting
|pipes,>redirection,&background); -
fork()a child,exec()the target program; - Without
&,waitpidfor the child to finish; - Print the next prompt.
Pipes are implemented with /dev/pipe0 (the piped process), and
redirection is just opening a file and replacing standard input/output —
all built on the mechanisms of chapters 09 and 10.
Stringing this chapter together with the previous ones:
kernel startup complete
│
├─ load_init_proc(): read /sbin/init off the SD card, create process 0
▼
init (pid 0):
├─ exec_from_sd /sbin/core (the name service)
├─ exec_from_sd /sbin/vfsd (the VFS mount table is set up)
├─ exec_from_sd /sbin/sdfsd (mounts /, the SD card filesystem)
├─ the shell executes /etc/init0.rd ... /etc/init.rd
│ ├─ uartd → /dev/tty0
│ ├─ timerd → /dev/timer
│ ├─ ramfsd → /tmp
│ ├─ logd → /dev/log
│ ├─ (graphical targets also have) displayd / fontd / xserverd ...
│ └─ session → shell
▼
the prompt appears, waiting for your command
A very satisfying experiment — modifying the boot flow:
> cd hardwares/raspix/system/etc/basic
> edit init.rd, adding a line:
@echo Hello from my init script!
> cd hardwares/raspix/system
> make && make sd # rebuild and repack the image
> make runYou'll see your own words appear in the boot log. That is the entire process of "configuring the system's boot behavior" — not a single line of C code needed.
- After booting, run
psand check every process against the sequence diagram in §11.4, in boot order; - Modify
init.rd: comment outtimerdand observe what happens to the system (hint:sleepandps's time statistics will be affected); - Read the shell source directory and
find the code segment that "parses the pipe character
|".
-
init(pid 0) is the only "loaded directly by the kernel" process; afterwards it is responsible for bringing up everything; - Before the VFS is ready, the core services are loaded straight off the SD
card with
exec_from_sd; - Boot orchestration is done by the
/etc/init*.rdscripts, interpreted by the shell, with@-prefixed directives; -
session + shellturns the serial port into the command line in front of you.
Next chapter: no longer just booting other people's programs — write a command of your own.
EwokOS OS Tutorial / EwokOS 操作系统教学
Part 1: Getting Started / 第 1 部分:起步
Part 2: The Kernel / 第 2 部分:内核
Part 3: Userland / 第 3 部分:用户态
Part 4: The Full System / 第 4 部分:完整系统
Part 5: The ewokos Series (Libraries) / 第 5 部分:ewokos 系列(库生态)
Part 6: GPU & Compute / 第 6 部分:GPU 与计算
Appendix / 附录