Linux boot up process - sysvinit
by Wenwei Weng
Booting a Linux installation involves multiple stages and software components, including firmware initialization, execution of a boot loader, loading and startup of a Linux kernel image, and execution of various startup scripts and daemons. For each of these stages and components there are different variations and approaches based on various hardware; for example, GRUB, LILO, SYSLINUX or Loadlin can be used as boot loaders, while the startup scripts can be either traditional init-style, or the system configuration can be performed through modern alternatives such as systemd or Upstart.
The following diagram shows a typical x86 based linux boot steps.
In this post, I will go throught sysvinit based boot up process using a yocto based linux system running inside qemu.
What version of init in the system?
root@qemux86:/etc# ls -l /sbin/init
lrwxrwxrwx 1 root root 19 Sep 28 03:00 /sbin/init -> /sbin/init.sysvinit
root@qemux86:/etc# /sbin/init --help
/sbin/init: invalid option -- '-'
Usage: init {-e VAR[=VAL] | [-t SECONDS] {0|1|2|3|4|5|6|S|s|Q|q|A|a|B|b|C|c|U|u}}
sysvinit based init File structure
- /etc/initab : top level set up
- /etc/init.d/: directory contains all init scripts for initial boot setup, including "/etc/init.d/rc" which will kicks in the right runlevel, e.g. " /etc/init.d/rc 5"
- /etc/rc[0-6].d. under each directory, it can have script name starts with "K[0-9][0-9]*.sh", which is to stop the service, and name starts with "S[0-9][0-9]*.sh", to start service.
root@qemux86:/etc# ls -l inittab
-rw-r--r-- 1 root root 1147 Sep 28 01:30 inittab
root@qemux86:/etc#
root@qemux86:/etc# ls -l init.d/
total 82
-rwxr-xr-x 1 root root 553 Sep 28 02:11 acpid
-rwxr-xr-x 1 root root 483 Sep 28 02:37 atd
-rwxr-xr-x 1 root root 492 Sep 28 02:19 banner.sh
-rwxr-xr-x 1 root root 1529 Sep 28 02:19 bootlogd
-rwxr-xr-x 1 root root 1584 Sep 28 02:19 bootmisc.sh
-rwxr-xr-x 1 root root 3229 Sep 28 02:19 checkroot.sh
-rwxr-xr-x 1 root root 1533 Sep 28 02:38 crond
-rwxr-xr-x 1 root root 2774 Sep 28 02:40 dbus-1
-rwxr-xr-x 1 root root 526 Sep 28 02:19 devpts.sh
-rwxr-xr-x 1 root root 988 Sep 28 02:44 dhcp-relay
-rwxr-xr-x 1 root root 35 Sep 28 02:23 fbsetup
lrwxrwxrwx 1 root root 33 Sep 28 02:58 functions -> /etc/init.d/functions.initscripts
-rw-r--r-- 1 root root 1408 Sep 28 02:19 functions.initscripts
-rwxr-xr-x 1 root root 510 Sep 28 02:19 halt
-rwxr-xr-x 1 root root 270 Sep 28 02:19 hostname.sh
-rwxr-xr-x 1 root root 2541 Sep 28 02:22 hwclock.sh
-rwxr-xr-x 1 root root 632 Sep 28 02:35 lighttpd
-rwxr-xr-x 1 root root 1223 Sep 28 02:10 modutils.sh
-rwxr-xr-x 1 root root 859 Sep 28 02:19 mountall.sh
-rwxr-xr-x 1 root root 1542 Sep 28 02:19 mountnfs.sh
-rwxr-xr-x 1 root root 1909 Sep 28 02:10 networking
-rwxr-xr-x 1 root root 3766 Sep 28 02:40 nfsserver
-rwxr-xr-x 1 root root 1446 Sep 28 02:44 ntpd
-rwxr-xr-x 1 root root 6347 Sep 28 02:19 populate-volatile.sh
-rwxr-xr-x 1 root root 521 Sep 28 02:22 psplash.sh
-rwxr-xr-x 1 root root 4358 Sep 28 02:19 rc
-rwxr-xr-x 1 root root 525 Sep 28 02:19 rcS
-rwxr-xr-x 1 root root 278 Sep 28 02:19 read-only-rootfs-hook.sh
-rwxr-xr-x 1 root root 289 Sep 28 02:19 reboot
-rwxr-xr-x 1 root root 585 Sep 28 02:19 rmnologin.sh
-rwxr-xr-x 1 root root 1798 Sep 28 02:37 rpcbind
-rwxr-xr-x 1 root root 321 Sep 28 02:19 save-rtc.sh
-rwxr-xr-x 1 root root 438 Sep 28 02:19 sendsigs
-rwxr-xr-x 1 root root 578 Sep 28 02:19 single
-rwxr-xr-x 1 root root 2143 Sep 28 02:40 sshd
lrwxrwxrwx 1 root root 8 Sep 28 03:00 stop-bootlogd -> bootlogd
-rwxr-xr-x 1 root root 660 Sep 28 02:19 sysfs.sh
lrwxrwxrwx 1 root root 27 Sep 28 02:58 syslog -> /etc/init.d/syslog.sysklogd
-rwxr-xr-x 1 root root 1711 Sep 28 02:22 syslog.busybox
-rwxr-xr-x 1 root root 3564 Sep 28 02:19 syslog.sysklogd
-rwxr-xr-x 1 root root 1525 Sep 28 02:10 tpm
-rwxr-xr-x 1 root root 3172 Sep 28 02:37 udev
-rwxr-xr-x 1 root root 516 Sep 28 02:19 umountfs
-rwxr-xr-x 1 root root 711 Sep 28 02:19 umountnfs.sh
-rwxr-xr-x 1 root root 1349 Sep 28 02:19 urandom
root@qemux86:/etc#
root@qemux86:/etc# ls -ld rc*
drwxr-xr-x 2 root root 1024 Sep 28 03:01 rc0.d
drwxr-xr-x 2 root root 1024 Sep 28 03:01 rc1.d
drwxr-xr-x 2 root root 1024 Sep 28 03:01 rc2.d
drwxr-xr-x 2 root root 1024 Sep 28 03:01 rc3.d
drwxr-xr-x 2 root root 1024 Sep 28 03:01 rc4.d
drwxr-xr-x 2 root root 1024 Sep 28 03:01 rc5.d
drwxr-xr-x 2 root root 1024 Sep 28 03:01 rc6.d
drwxr-xr-x 2 root root 1024 Oct 15 04:39 rcS.d
root@qemux86:/etc#
root@qemux86:/etc# ls -l rc5.d
total 0
lrwxrwxrwx 1 root root 16 Sep 28 03:01 S02dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S09sshd -> ../init.d/sshd
lrwxrwxrwx 1 root root 15 Sep 28 02:58 S20acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 20 Sep 28 02:58 S20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 19 Sep 28 03:01 S20nfsserver -> ../init.d/nfsserver
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S20ntpd -> ../init.d/ntpd
lrwxrwxrwx 1 root root 16 Sep 28 02:58 S20syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 18 Sep 28 03:01 S70lighttpd -> ../init.d/lighttpd
lrwxrwxrwx 1 root root 15 Sep 28 03:01 S90crond -> ../init.d/crond
lrwxrwxrwx 1 root root 22 Sep 28 02:58 S99rmnologin.sh -> ../init.d/rmnologin.sh
lrwxrwxrwx 1 root root 23 Sep 28 03:00 S99stop-bootlogd -> ../init.d/stop-bootlogd
root@qemux86:/etc#
root@qemux86:/etc# ls -l rc6.d/
total 0
lrwxrwxrwx 1 root root 14 Sep 28 03:01 K09sshd -> ../init.d/sshd
lrwxrwxrwx 1 root root 15 Sep 28 02:58 K20acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 16 Sep 28 03:01 K20dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 root root 20 Sep 28 02:58 K20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 19 Sep 28 03:01 K20nfsserver -> ../init.d/nfsserver
lrwxrwxrwx 1 root root 14 Sep 28 03:01 K20ntpd -> ../init.d/ntpd
lrwxrwxrwx 1 root root 20 Sep 28 02:58 K20psplash.sh -> ../init.d/psplash.sh
lrwxrwxrwx 1 root root 16 Sep 28 02:58 K20syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 20 Sep 28 02:58 K40networking -> ../init.d/networking
lrwxrwxrwx 1 root root 15 Sep 28 03:01 K60crond -> ../init.d/crond
lrwxrwxrwx 1 root root 18 Sep 28 03:01 K70lighttpd -> ../init.d/lighttpd
lrwxrwxrwx 1 root root 18 Sep 28 02:58 S20sendsigs -> ../init.d/sendsigs
lrwxrwxrwx 1 root root 21 Sep 28 02:58 S25save-rtc.sh -> ../init.d/save-rtc.sh
lrwxrwxrwx 1 root root 17 Sep 28 02:58 S30urandom -> ../init.d/urandom
lrwxrwxrwx 1 root root 22 Sep 28 02:58 S31umountnfs.sh -> ../init.d/umountnfs.sh
lrwxrwxrwx 1 root root 17 Sep 28 02:58 S32rpcbind -> ../init.d/rpcbind
lrwxrwxrwx 1 root root 18 Sep 28 02:58 S40umountfs -> ../init.d/umountfs
lrwxrwxrwx 1 root root 16 Sep 28 02:58 S90reboot -> ../init.d/reboot
root@qemux86:/etc#
what is runlevel?
What is runlevel?
Runlevel is a mode of linux system operation, possible value:
- 0: Halt, Shuts down the system.
- 1: Single-user mode, Mode for administrative tasks.
- 2: Multi-user mode, Does not configure network interfaces and does not export networks services.
- 3: Multi-user mode with networking Starts the system normally.
- 4: Not used/user-definable For special purposes.
- 5: Start the system normally with appropriate display manager (with GUI) Same as runlevel 3 + display manager.
- 6: Reboot, Reboots the system.
How to know what runlevel the system is in:
root@qemux86:/etc# runlevel
N 5
root@qemux86:/etc# who -r
run-level 5 Oct 15 05:34 last=S
root@qemux86:/etc#
switch runlevel, use the following command:
init [0-6] or telinit [0-6]
e.g. often we will do “init 6” to reboot box, after command is entered, the scripts under “rc6.d” will be executed, the last one is S90reboot, which is a softlink to “reboot” script, which eventually runs /sbin/reboot.sysvinit.
root@qemux86:/etc# ls -l rc6.d/
total 0
lrwxrwxrwx 1 root root 14 Sep 28 03:01 K09sshd -> ../init.d/sshd
lrwxrwxrwx 1 root root 15 Sep 28 02:58 K20acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 16 Sep 28 03:01 K20dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 root root 20 Sep 28 02:58 K20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 19 Sep 28 03:01 K20nfsserver -> ../init.d/nfsserver
lrwxrwxrwx 1 root root 14 Sep 28 03:01 K20ntpd -> ../init.d/ntpd
lrwxrwxrwx 1 root root 20 Sep 28 02:58 K20psplash.sh -> ../init.d/psplash.sh
lrwxrwxrwx 1 root root 16 Sep 28 02:58 K20syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 20 Sep 28 02:58 K40networking -> ../init.d/networking
lrwxrwxrwx 1 root root 15 Sep 28 03:01 K60crond -> ../init.d/crond
lrwxrwxrwx 1 root root 18 Sep 28 03:01 K70lighttpd -> ../init.d/lighttpd
lrwxrwxrwx 1 root root 18 Sep 28 02:58 S20sendsigs -> ../init.d/sendsigs
lrwxrwxrwx 1 root root 21 Sep 28 02:58 S25save-rtc.sh -> ../init.d/save-rtc.sh
lrwxrwxrwx 1 root root 17 Sep 28 02:58 S30urandom -> ../init.d/urandom
lrwxrwxrwx 1 root root 22 Sep 28 02:58 S31umountnfs.sh -> ../init.d/umountnfs.sh
lrwxrwxrwx 1 root root 17 Sep 28 02:58 S32rpcbind -> ../init.d/rpcbind
lrwxrwxrwx 1 root root 18 Sep 28 02:58 S40umountfs -> ../init.d/umountfs
lrwxrwxrwx 1 root root 16 Sep 28 02:58 S90reboot -> ../init.d/reboot
root@qemux86:/etc#
root@qemux86:/etc# cat init.d/reboot
#!/bin/sh
### BEGIN INIT INFO
# Provides: reboot
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop: 6
# Short-Description: Execute the reboot command.
# Description:
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
echo -n "Rebooting... "
reboot -d -f
root@qemux86:/etc# which reboot
/sbin/reboot
root@qemux86:/etc# file /sbin/reboot
/sbin/reboot: symbolic link to `/sbin/reboot.sysvinit'
root@qemux86:/etc# file /sbin/reboot.sysvinit
/sbin/reboot.sysvinit: symbolic link to `halt.sysvinit'
root@qemux86:/etc# file /sbin/halt.sysvinit
/sbin/halt.sysvinit: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, BuildID[sha1]=638a764335d30a864f9b9c0d9321c8f1de7afbd8, stripped
root@qemux86:/etc#
understanding inittab
The inittab file describes which processes are started at bootup and during normal operation (e.g. /etc/init.d/boot, /etc/init.d/rc, gettys…). Init distinguishes multiple runlevels, each of which can have its own set of processes that are started. Valid runlevels are 0-6 plus A, B, and C for ondemand entries. An entry in the inittab file has the following format:
id:runlevels:action:process
Lines beginning with ‘#’ are ignored.
- id: is a unique sequence of 1-4 characters which identifies an entry in inittab (for versions of sysvinit compiled with the old libc5 (< 5.2.18) or a.out libraries the limit is 2 characters). (Note: traditionally, for getty and other login processes, the value of the id field is kept the same as the suffix of the corresponding tty, e.g. 1 for tty1. Some ancient login accounting programs might expect this, though I can’t think of any.)
- runlevels: lists the runlevels for which the specified action should be taken. The runlevels field may contain multiple characters for different runlevels. For example, 123 specifies that the process should be started in runlevels 1, 2, and 3. The runlevels for ondemand entries may contain an A, B, or C. The runlevels field of sysinit, boot, and bootwait entries are ignored. When the system runlevel is changed, any running processes that are not specified for the new runlevel are killed, first with SIGTERM , then with SIGKILL.
- action: describes which action should be taken. Valid actions for the action field are:
- respawn: The process will be restarted whenever it terminates (e.g. getty).
- wait:: The process will be started once when the specified runlevel is entered and init will wait for its termination.
- once: The process will be executed once when the specified runlevel is entered.
- boot : The process will be executed during system boot. The runlevels field is ignored.
- bootwait: The process will be executed during system boot, while init waits for its termination (e.g. /etc/rc). The runlevels field is ignored.
- off : This does nothing.
- ondemand: A process marked with an ondemand runlevel will be executed whenever the specified ondemand runlevel is called. However, no runlevel change will occur (ondemand runlevels are ‘a’, ‘b’, and ‘c’).
- initdefault: An initdefault entry specifies the runlevel which should be entered after system boot. If none exists, init will ask for a runlevel on the console. The process field is ignored.
- sysinit: The process will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
- powerwait: The process will be executed when the power goes down. Init is usually informed about this by a process talking to a UPS connected to the computer. Init will wait for the process to finish before continuing.
- powerfail : As for powerwait, except that init does not wait for the process’s completion.
- powerokwait: This process will be executed as soon as init is informormed that the power has been restored.
- powerfailnow: This process will be executed when init is told that the battery of the external UPS is almost empty and the power is failing (provided that the external UPS and the monitoring process are able to detect this condition).
- ctrlaltdel: The process will be executed when init receives the SIGINT signal. This means that someone on the system console has pressed the CTRL-ALT-DEL key combination. Typically one wants to execute some sort of shutdown either to get into single-user level or to reboot the machine.
- kbrequest: The process will be executed when init receives a signal from the keyboard handler that a special key combination was pressed on the console keyboard.
- process: specifies the process to be executed. If the process field starts with a ‘+’ character, init will not do utmp and wtmp accounting for that process. This is needed for gettys that insist on doing their own utmp/wtmp housekeeping. This is also a historic bug.
inittab example
root@qemux86:/etc# cat -n /etc/inittab
1 # /etc/inittab: init(8) configuration.
2 # $Id: inittab,v 1.91 2002/01/25 13:35:21 miquels Exp $
3
4 # The default runlevel.
5 id:5:initdefault:
6
7 # Boot-time system configuration/initialization script.
8 # This is run first except when booting in emergency (-b) mode.
9 si::sysinit:/etc/init.d/rcS
10
11 # What to do in single-user mode.
12 ~~:S:wait:/sbin/sulogin
13
14 # /etc/init.d executes the S and K scripts upon change
15 # of runlevel.
16 #
17 # Runlevel 0 is halt.
18 # Runlevel 1 is single-user.
19 # Runlevels 2-5 are multi-user.
20 # Runlevel 6 is reboot.
21
22 l0:0:wait:/etc/init.d/rc 0
23 l1:1:wait:/etc/init.d/rc 1
24 l2:2:wait:/etc/init.d/rc 2
25 l3:3:wait:/etc/init.d/rc 3
26 l4:4:wait:/etc/init.d/rc 4
27 l5:5:wait:/etc/init.d/rc 5
28 l6:6:wait:/etc/init.d/rc 6
29 # Normally not reached, but fallthrough in case of emergency.
30 z6:6:respawn:/sbin/sulogin
31 tpmc:5:respawn:python /home/root/fap/tpmc.py
32 mon:5:respawn:/usr/bin/monit
33 S:2345:respawn:/sbin/getty 115200 ttyS0
34 # /sbin/getty invocations for the runlevels.
35 #
36 # The "id" field MUST be the same as the last
37 # characters of the device (after "tty").
38 #
39 # Format:
40 # <id>:<runlevels>:<action>:<process>
41 #
42
43 1:2345:respawn:/sbin/getty 38400 tty1
44
root@qemux86:/etc#
explanation:
- line#5 tell system goes to runlevel 5 after system boot.
- line#9 and 12: runs when it boots in single user mode.
- line#22 - 28: invoke set up for each runlevel. In the above setup, this is what will occur: system goes into runlevel 5, which will trigger all scripts under "/etc/rc5.d/" to be executed.
root@qemux86:/etc# ls -l rc5.d
total 0
lrwxrwxrwx 1 root root 16 Sep 28 03:01 S02dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S09sshd -> ../init.d/sshd
lrwxrwxrwx 1 root root 15 Sep 28 02:58 S20acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 20 Sep 28 02:58 S20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 19 Sep 28 03:01 S20nfsserver -> ../init.d/nfsserver
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S20ntpd -> ../init.d/ntpd
lrwxrwxrwx 1 root root 16 Sep 28 02:58 S20syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 18 Sep 28 03:01 S70lighttpd -> ../init.d/lighttpd
lrwxrwxrwx 1 root root 15 Sep 28 03:01 S90crond -> ../init.d/crond
lrwxrwxrwx 1 root root 22 Sep 28 02:58 S99rmnologin.sh -> ../init.d/rmnologin.sh
lrwxrwxrwx 1 root root 23 Sep 28 03:00 S99stop-bootlogd -> ../init.d/stop-bootlogd
root@qemux86:/etc#
root@qemux86:~# cat /etc/init.d/test-sysvinit
#!/bin/bash
# do whatever needed for the service here
# for testing, simply drop a file named as /tmp/test-sysvinit
touch /tmp/test-sysvinit
root@qemux86:~#
root@qemux86:/etc/rc5.d# ls -l
total 0
lrwxrwxrwx 1 root root 16 Sep 28 03:01 S02dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S09sshd -> ../init.d/sshd
lrwxrwxrwx 1 root root 15 Sep 28 02:58 S20acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 20 Sep 28 02:58 S20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 19 Sep 28 03:01 S20nfsserver -> ../init.d/nfsserver
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S20ntpd -> ../init.d/ntpd
lrwxrwxrwx 1 root root 16 Sep 28 02:58 S20syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 18 Sep 28 03:01 S70lighttpd -> ../init.d/lighttpd
lrwxrwxrwx 1 root root 15 Sep 28 03:01 S90crond -> ../init.d/crond
lrwxrwxrwx 1 root root 22 Sep 28 02:58 S99rmnologin.sh -> ../init.d/rmnologin.sh
lrwxrwxrwx 1 root root 23 Sep 28 03:00 S99stop-bootlogd -> ../init.d/stop-bootlogd
root@qemux86:/etc/rc5.d# ln -s ../init.d/test-sysvinit S98-test-sysvinit
root@qemux86:/etc/rc5.d# ls -l
total 0
lrwxrwxrwx 1 root root 16 Sep 28 03:01 S02dbus-1 -> ../init.d/dbus-1
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S09sshd -> ../init.d/sshd
lrwxrwxrwx 1 root root 15 Sep 28 02:58 S20acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 20 Sep 28 02:58 S20hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 19 Sep 28 03:01 S20nfsserver -> ../init.d/nfsserver
lrwxrwxrwx 1 root root 14 Sep 28 03:01 S20ntpd -> ../init.d/ntpd
lrwxrwxrwx 1 root root 16 Sep 28 02:58 S20syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 18 Sep 28 03:01 S70lighttpd -> ../init.d/lighttpd
lrwxrwxrwx 1 root root 15 Sep 28 03:01 S90crond -> ../init.d/crond
lrwxrwxrwx 1 root root 23 Oct 31 02:22 S98-test-sysvinit -> ../init.d/test-sysvinit
lrwxrwxrwx 1 root root 22 Sep 28 02:58 S99rmnologin.sh -> ../init.d/rmnologin.sh
lrwxrwxrwx 1 root root 23 Sep 28 03:00 S99stop-bootlogd -> ../init.d/stop-bootlogd
root@qemux86:/etc/rc5.d# sync
root@qemux86:/etc/rc5.d#
root@qemux86:/etc/rc5.d# reboot
Poky 9.0 (Yocto Project 1.4 Reference Distro) 1.4.1 qemux86 ttyS0
qemux86 login: root
root@qemux86:~# ls /tmp
test-sysvinit
root@qemux86:~#
Subscribe via RSS