torvalds/linux
Init
Purpose
init/ is the boot-time entry point of the kernel proper. It contains start_kernel() (the function called from architecture-specific assembly after the MMU and stack are set up), the initramfs unpacker that materializes the first user-space root file system, and a small amount of bookkeeping (version stamping, calibration loops).
When this directory is done, the system is running PID 1 in user space.
Directory layout
init/
├── main.c # start_kernel(), do_initcalls(), kernel_init()
├── calibrate.c # BogoMIPS / loops_per_jiffy calibration
├── do_mounts.c # Root filesystem mount logic
├── do_mounts_initrd.c # Old-style initrd handling
├── do_mounts_rd.c # ramdisk image handling
├── initramfs.c # cpio-archive unpacker
├── initramfs_internal.h
├── initramfs_test.c # KUnit tests
├── init_task.c # The very first task_struct (PID 0)
├── noinitramfs.c # Stub when no initramfs is built in
├── version.c # UTS string, banner
├── version-timestamp.c
└── KconfigKey abstractions
| Symbol | File | Purpose |
|---|---|---|
start_kernel() |
init/main.c |
The single C entry point of the kernel. Calls every early subsystem initializer in order. |
kernel_init() |
init/main.c |
The kernel thread that runs do_initcalls() and then execs /init. |
do_initcalls() |
init/main.c |
Walks the initcall sections in the linker script and invokes each registered initializer. |
init_task |
init/init_task.c |
The first task_struct (PID 0, the swapper/idle task). |
unpack_to_rootfs() |
init/initramfs.c |
Decodes a cpio image into the root rootfs. |
How it works
graph TD
A["arch/<arch>/kernel/head_*.S"] -->|calls| B["start_kernel()"]
B --> C["setup_arch()"]
C --> D["mm_init(), sched_init()"]
D --> E["rcu_init()"]
E --> F["init_IRQ(), time_init()"]
F --> G["rest_init()"]
G -->|spawns| H["kernel_init thread"]
G -->|enters| I["cpu_idle_loop()"]
H --> J["do_initcalls() across<br/>all initcall levels"]
J --> K["prepare_namespace()<br/>mount initramfs"]
K --> L["run_init_process('/init')"]
L -->|execs| M["PID 1 in user space"]The initcall mechanism is a key piece. Macros like subsys_initcall(foo_init) in subsystem code put a function pointer into a section in the kernel image (e.g. .initcall4.init). At boot, do_initcalls() walks each section in order:
early_initcall— runs before SMP is brought up.core_initcall,postcore_initcall,arch_initcall,subsys_initcall,fs_initcall,device_initcall,late_initcall— run in increasing order during normal boot.
This ordering is how the kernel guarantees, e.g., that the slab allocator is up before file systems try to allocate.
Integration points
- arch/: every architecture's boot stub eventually jumps to
start_kernel(). - mm/:
mm_init()brings up the page allocator and slab. - kernel/sched/:
sched_init()andinit_idle()create the idle task and put it on every CPU. - kernel/rcu/:
rcu_init()brings up RCU before any subsystem can use it. - drivers/base/: most drivers register through
device_initcall(). - fs/: file systems register through
fs_initcall()so they exist by the time the root is mounted. - usr/: the initramfs cpio image is generated by
usr/and embedded intovmlinux.init/initramfs.cdecodes it.
Key source files
| File | Purpose |
|---|---|
init/main.c |
start_kernel(), the initcall walker, the kernel_init thread. |
init/init_task.c |
Static definition of the first task. |
init/initramfs.c |
cpio unpacker for the embedded or loaded initramfs. |
init/do_mounts.c |
The prepare_namespace() machinery that picks and mounts the root. |
init/calibrate.c |
loops_per_jiffy calibration ("BogoMIPS"). |
init/version.c |
UTS namespace strings (uname output). |
Entry points for modification
- To add a boot-time message: insert
pr_info()ininit/main.c. - To add an early kernel command-line option: use
early_param()and add a parser in your subsystem; it will be called beforestart_kernel()finishes. - To add a new initcall level: rare. Look at
include/linux/init.hand the linker scripts ininclude/asm-generic/vmlinux.lds.h. - To change the root-mount logic:
init/do_mounts.c. This is sensitive — many user-space setups depend on it.
Related pages
- Architecture support — what runs before
start_kernel(). - Kernel core — what
start_kernel()brings up. - usr — how the initramfs is generated and embedded.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.