Thursday, June 14, 2012

Changing RunLevel



Two sets of init scripts are involved in switching runlevels: K-scripts from the runlevel on which we are currently running and S-scripts from a new (target) runlevel. It is important to understand that there are two runlevels involved in switching:
  • runlevel from which we switch. Let's call it l_from
  • runlevel to which we switch. Let's call it l_to
Simplifying the rc script which is responsible for changing runlevels performs the following two operations.
  1. All K-scripts that exist on the current runlevel l_from are processed. For each K-script for which S-script does not exists at the target runlevel l_to, the  K-script froml_from is executed.
  2. Each S script that exist at target runlevel l_to is executed
In other words when you change the runlevel first stop scripts of the current runlevel are launched, closing down some daemons running on the current runlevel that are not necessary on a new runlevel (do not have S-script defined for them). Then all start scripts of the new runlevel are run in the order defined by their numeric priorities (lexographic order).
For example, the following occurs when changing from runlevel 3 to 5:
  1. The administrator (root) enter the command init 5 which tells init to change the current runlevel to 5. 
  2. The init consults its configuration file (/etc/inittab) and determines it should start /etc/init.d/rc with the new runlevel as a parameter.
  3. Now rc calls all the stop scripts of the current runlevel, but only those for which there is no start script in the new runlevel. In this example, these are all the scripts that reside in /etc/init.d/rc3.d (old runlevel was 3) and start with a K. The number following K specifies the order to start, because there are some dependencies to consider.
  4. The last things to start are the start scripts of the new runlevel. These are, in this example, in /etc/init.d/rc5.d and begin with an S. The same procedure regarding the order in which they are started is applied here.
When changing into the same runlevel as the current runlevel, init only checks /etc/inittab for changes and starts the appropriate scripts. 

No comments:

Post a Comment