In this article we’ll talk a bit about Linux booting process and we’ll describe some of the key elements involved in this process. You should know by now that the kernel of an Operating System is the “brain” of any computer because it handles resources usage (CPU, memory). The kernel is a program that manages System processes, I/O and device drivers. Once the kernel is discovered by the boot loader, it’s loaded into memory to initialize hardware resources and offers all means for the OS to boot. After the kernel finishes its operation it loads the init program.
The kernel has control over system resources. A computer with one CPU will be able to run one program at a time, but in memory several programs can be stored simultaneously. As a summary, the kernel performs the following operations.
- The kernel controls CPU access, several processes are stored in System memory
- It will select a process and will “resume” its last operation
- Kernel will give CPU control to the designated process
- The process will perform its operation for a number of microseconds
- Once the process finishes its operation, a clock interrupt signal is sent to stop the process and the kernel receives control over CPU
- If other operations such as reading and writing to system devices is needed, the kernel will take care of it
- The kernel will select another process from memory and so on…
With CentOS distributions, the kernel image is stored under /boot directory and usually starts with /boot/vmlinuz…
The boot process of a Linux machine consists of the following:
- Power-on-self test and the boot loader are executed
- A boot loader is responsible for discovering the kernel image, it will load and start the kernel into System memory. If there are multiple OS stored on the machine, the boot loader will prompt the user to select on of the available distributions.We’ll talk about boot loaders in a future article
- The kernel will initialize system devices and drivers
- The kernel will mount the root filesystem. Read previous Linux articles from IT training day for further information regarding Linux filesystems
- The kernel starts the init program
- init is responsible for starting the rest of the processes
- Once init finishes its operation, the user will be able to login to the server.
init – is a program responsible for starting and stopping certain processes in a particular order. init can be found in /sbin/init. The configuration file for init is stored in /etc/inittab. At a single point in time, any Linux machine runs a certain pool of processes. These system states are called runlevels and are identified by a number from 0 to 6. If you execute cat /etc/ininttab, you will be able to view all runlevels available on a Linux machine:
# 0 – halt (Do NOT set initdefault to this) – shutdowns the machine
# 1 – Single user mode – the OS will boot into a single superuser without networking
# 2 – Multiuser, without NFS (The same as 3, if you do not have networking) – multiuser without networking
# 3 – Full multiuser mode – multiuser with networking
# 4 – unused
# 5 – X11 – boots the OS in GUI mode
# 6 – reboot (Do NOT set initdefault to this) – reboots the machine
From the output of this command you can see that the init default runlevel is set to 3 which is full multiuser mode. On CentOS systems, runlevels are started by /etc/init/rc.conf file. If we open this file we can see that it executes /etc/init/rc.conf and the selected runlevel (exec /etc/rc.d/rc $RUNLEVEL):
Now execute cat /etc/rc.d/rc. This is a long script but as a summary, it does the following:
- the script is executed with an argument which represents the runlevel. It will verify if the runlevel exists for the specified number
- it will find out the current and the previous runlevel
- retrieves the first argument of the script and sets the runlevel to this argument
- /etc/rc$runlevel.d is where the scripts for each runlevel are stored. The script will verify if there is a rc folder directory for the new runlevel
- sets language and vc settings
- runs the KILL scripts for that particular runlevel
- verifies if each service for that particular run level has been started
- informs the kernel that the particular service is stopped
- runs the START scripts for each service
- If you run in user confirmation mode, it will prompt the user to confirm for each service (subsystem)
- In the end, it brings all the subsystem up
When the above script executes START/STOP scripts for a particular runlevel, it will execute one of the symbolic links from /etc:
The symbolic link will redirect to one of the runlevel locations which is something like /rc.d/rc[ runlevel number].d. Here you will be able to see each START (S…) and KILL (K…) script:
Whenever you install something on a Linux machine, the START script for that particular runlevel must be added here so that the service is started upon reboot.
Init has several actions that modifies its behavior when running runlevel scripts:
initdefault – sets default runlevel
wait – tells init to run the particular run level when it’s booted then wait for those to finish before executing other things
respawn – will force init to run a specified script twice
ctrlaltdel – tells the system what to do when CTL+ALT+DELETE is pressed
sysinit – forces init to run a particular thing before entering a runlevel
Let’s select the start scripts for runlevel 3 by executing ls -al | grep S/*:
There may exist certain dependencies between different programs this is why scripts are executed in the order indicated by the numbers contained in their names:
For example, S08iptables will be executed before the network daemon is started (S10network).
If you want to add a new service to a Linux machine, you will need to add a script in the /init.d directory and also create a symlink in /etc directory similar to those that already exist.
CentOS distributions include a tool that offers you the possibility to interact with System services. chkconfig command can be used to enable or disable services on different runlevels. You can simply type chkconfig –list to view all services:
To enable/disable a service in runlevels 2, 3, 4, and 5, type the following:
chkconfig service on/off
ex: chkconfig iptables on
To enable or disable a service for specific runlevels, type the following:
chkconfig service_name on/off level_number
chkconfig iptables on 345
Because init is the first process that is executed on a Linux machine, its process ID will always be 1. To view the current runlevel, execute the following command: runlevel
At any point in time you can force init to re-read its inittab file and change the run level. To achieve this result, use the telinit runlevel command just like in the following example:
You’ll see that the runlevel command will display two numbers, The first one is the previous runlevel while the second one is the current runlevel. When you switch the runlevels, init will kill all processes that are not part of the inittab file for the new runlevel.
Telinit supports multiple options, as follows:
q – to reload its configuration
s – can be used to change to single mode
init also controls the way the System enters shutdown state. You can use the shutdown -h now command to force a System shutdown instantly. To reboot a Linux machine you can use shutdown -r now command. You can also execute init 6 or init 0 to enter that particular runlevel.
When the System is set to runlevel 0 or 6, the following things will happen:
- init will try to kill all processes by executing the scripts from rc0.d
- When the first commands run, the system will be locked so that no further modification can be done
- All filesystems except the root will be unmounted
- The root filesystem in mounted in read-only mode
- Sync command is executed to force changed blocks to disk
- The final commands will tell the kernel to reboot or stop
That’s about it for the initialization of a Linux machine, we’ve talked a bit about the kernel, the initializaiton steps, the init program and runlevels.