When you boot the computer, the many messages scrolling by on the console display many automatic initializations and configurations that are being executed. Sometimes you may wish to slightly alter how this stage works, which means that you need to understand it well. That is the purpose of this section.
First, the BIOS takes control of the computer, detects the disks, loads the Master Boot Record, and executes the bootloader. The bootloader takes over, finds the kernel on the disk, loads and executes it. The kernel is then initialized, and starts to search for and mount the partition containing the root filesystem, and finally executes the first program — init
. Frequently, this “root partition” and this init
are, in fact, located in a virtual filesystem that only exists in RAM (hence its name, “initramfs”, formerly called “initrd” for “initialization RAM disk”). This filesystem is loaded in memory by the bootloader, often from a file on a hard drive or from the network. It contains the bare minimum required by the kernel to load the “true” root filesystem: this may be driver modules for the hard drive, or other devices without which the system can not boot, or, more frequently, initialization scripts and modules for assembling RAID arrays, opening encrypted partitions, activating LVM volumes, etc. Once the root partition is mounted, the initramfs hands over control to the real init, and the machine goes back to the standard boot process.
The “real init” is currently provided by sysv-rc (“System V”) and this section documents this init system.
Init executes several processes, following instructions from the
/etc/inittab
file. The first program that is executed (which corresponds to the
sysinit step) is
/etc/init.d/rcS
, a script that executes all of the programs in the
/etc/rcS.d/
directory.
Among these, you will find successively programs in charge of:
configuring the console's keyboard;
loading drivers: most of the kernel modules are loaded by the kernel itself as the hardware is detected; extra drivers are then loaded automatically when the corresponding modules are listed in /etc/modules
;
checking the integrity of filesystems;
mounting local partitions;
configuring the network;
mounting network filesystems (NFS).
After this stage, init
takes over and starts the programs enabled in the default runlevel (which is usually runlevel 2). It executes /etc/init.d/rc 2
, a script that starts all services which are listed in /etc/rc2.d/
and whose name start with the “S” letter. The two-figures number that follows had historically been used to define the order in which services had to be started, but nowadays the default boot system uses insserv
, which schedules everything automatically based on the scripts' dependencies. Each boot script thus declares the conditions that must be met to start or stop the service (for example, if it must start before or after another service); init
then launches them in the order that meets these conditions. The static numbering of scripts is therefore no longer taken into consideration (but they must always have a name beginning with “S” followed by two digits and the actual name of the script used for the dependencies). Generally, base services (such as logging with rsyslog
, or port assignment with portmap
) are started first, followed by standard services and the graphical interface (gdm
).
This dependency-based boot system makes it possible to automate re-numbering, which could be rather tedious if it had to be done manually, and it limits the risks of human error, since scheduling is conducted according to the parameters that are indicated. Another benefit is that services can be started in parallel when they are independent from one another, which can accelerate the boot process.
init
distinguishes several runlevels, so it can switch from one to another with the telinit new-level
command. Immediately, init
executes /etc/init.d/rc
again with the new runlevel. This script will then start the missing services and stop those that are no longer desired. To do this, it refers to the content of the /etc/rcX.d
(where X represents the new runlevel). Scripts starting with “S” (as in “Start”) are services to be started; those starting with “K” (as in “Kill”) are the services to be stopped. The script does not start any service that was already active in the previous runlevel.
By default, Debian uses four different runlevels:
Level 0 is only used temporarily, while the computer is powering down. As such, it only contains many “K” scripts.
Level 1, also known as single-user mode, corresponds to the system in degraded mode; it includes only basic services, and is intended for maintenance operations where interactions with ordinary users are not desired.
Level 2 is the level for normal operation, which includes networking services, a graphical interface, user logins, etc.
Level 6 is similar to level 0, except that it is used during the shutdown phase that precedes a reboot.
Other levels exist, especially 3 to 5. By default they are configured to operate the same way as level 2, but the administrator can modify them (by adding or deleting scripts in the corresponding /etc/rcX.d
directories) to adapt them to particular needs.
All the scripts contained in the various /etc/rcX.d
directories are really only symbolic links — created upon package installation by the update-rc.d
program — pointing to the actual scripts which are stored in /etc/init.d/
. The administrator can fine tune the services available in each runlevel by re-running update-rc.d
with adjusted parameters. The update-rc.d(1) manual page describes the syntax in detail. Please note that removing all symbolic links (with the remove
parameter) is not a good method to disable a service. Instead you should simply configure it to not start in the desired runlevel (while preserving the corresponding calls to stop it in the event that the service runs in the previous runlevel). Since update-rc.d
has a somewhat convoluted interface, you may prefer using rcconf
(from the rcconf package) which provides a more user-friendly interface.
Finally, init
starts control programs for various virtual consoles (getty
). It displays a prompt, waiting for a username, then executes login user
to initiate a session.