How Linux Boots


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.
  1. The kernel controls CPU access, several processes are stored in System memory
  2. It will select a process and will “resume” its last operation
  3. Kernel will give CPU control to the designated process
  4. The process will perform its operation for a number of microseconds
  5. Once the process finishes its operation, a clock interrupt signal is sent to stop the process and the kernel receives control over CPU
  6. If other operations such as reading and writing to system devices is needed, the kernel will take care of it
  7. 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…
Linux kernel
The boot process of a Linux machine consists of the following:
  1. Power-on-self test and the boot loader are executed
  2. 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
  3. The kernel will initialize system devices and drivers
  4. The kernel will mount the root filesystem. Read previous Linux articles from IT training day for further information regarding Linux filesystems
  5. The kernel starts the init program
  6. init is responsible for starting the rest of the processes
  7. 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:
Linux runlevel
#   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):
Linux runlevels
Now execute cat /etc/rc.d/rc. This is a long script but as a summary, it does the following:
  1. the script is executed with an argument which represents the runlevel. It will verify if the runlevel exists for the specified number
  2. it will find out the current and the previous runlevel
  3. retrieves the first argument of the script and sets the runlevel to this argument
  4. /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
  5. sets language and vc settings
  6. runs the KILL scripts for that particular runlevel
  7. verifies if each service for that particular run level has been started
  8. informs the kernel that the particular service is stopped
  9. runs the START scripts for each service
  10. If you run in user confirmation mode, it will prompt the user to confirm for each service (subsystem)
  11. 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:
Linux boot process
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:
Linux initialization process
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:
Linux start scripts
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:
Linux 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
Linux 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:
telinit 3
Changing Linux runlevel
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:
  1. init will try to kill all processes by executing the scripts from rc0.dLinux init
  2. When the first commands run, the system will be locked so that no further modification can be done
  3. All filesystems except the root will be unmounted
  4. The root filesystem in mounted in read-only mode
  5. Sync command is executed to force changed blocks to disk
  6. 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.
Advertisements

One thought on “How Linux Boots

  1. Pingback: Linux processes | ITtrainingday

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s