In this lesson, you will learn how to orchestrate the transition from a running kernel to a fully functional user-space environment by writing custom initialization scripts.
Once the kernel finishes mounting the root filesystem (which we previously built using BusyBox), it executes the first user-space process: /sbin/init. In most embedded systems, this is a symlink to the init binary within BusyBox. This process is responsible for reading configuration files to determine which services to start and how to configure the system state.
The Init Sequence
The initialization process typically follows a structured path. BusyBox init looks for a configuration file, usually located at /etc/inittab. This file defines the system's behavior during different "runlevels." For a simple embedded system, the primary instruction is to execute a script, often /etc/init.d/rcS.
Creating a Custom Service
To launch a background process—such as a custom data-collection daemon—you create a shell script in /etc/init.d/. An example script, /etc/init.d/S99mydaemon, might look like this:
#!/bin/sh
case "$1" in
start)
echo "Starting my-daemon..."
/usr/bin/my-daemon --config /etc/daemon.conf &
;;
stop)
killall my-daemon
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
When the system boots, the rcS script iterates through the /etc/init.d/ directory. It executes every script prefixed with 'S' (for Start) followed by a two-digit number to determine the execution order. By naming our script S99..., we ensure it runs after essential services like network configuration and logging have already been initialized.
The & operator is critical here; it forces the daemon to run in the background. Without it, the initialization script would wait for the daemon to terminate, effectively hanging the entire boot process. By properly configuring these scripts, you gain full control over the hardware-software handshake, ensuring your application environment is ready for user interaction before the system finishes booting.
Key Takeaway: System initialization relies on the init process to execute ordered scripts in /etc/init.d/, where proper naming conventions and the use of background execution (&) ensure that services start in the correct sequence without blocking the boot process.