Archive

Run Levels


Introduction

This page is intended for people to get an overview of what happens when your computer boots up, and why there is a need for different run levels. Essentially, the information contained in here is based on an e-mail I sent to SussexLUG.

Run levels

After the kernel has sorted out the hardware, etc, “init” is spawned. Init is the top level process. It’s where everything starts. So any programs you run, will either be children of init or have init as their ancester. As soon as init loads, it looks in the /etc/inittab file. Now, this file has two uses. One is for init to set its initial state, the other is for when we login (more about that later)…

So, init’s first job is to look in /etc/inittab to determine which runlevel it is going to load. It determines this from the line:

id:2:initdefault:

Although before it does this, it has to look and see what the system-wide runlevel is. People often get this confused. Although the default inittab is the one that people associate as having the common applications that load up, there is also a mandatory set which are loaded, this comes from the system-wide inittab, typically “S” (for Single usermode). In inittab terms, this looks like:

si::sysinit:/etc/init.d/rcS

This is actually a shell script which (amongst other things) calls various files in /etc/rcS.d/. Typically this runlevel sets up the clock, mounts one’s partitions, etc. It is also the runlevel which is used for recovery if your computer fails, or one you can use yourself to correct mistakes in boot sequences (or to run ‘fsck’). You see, the init structure is ordered thus (note the paths vary between distros, but the concept is the same):

/etc/init.d/ -- this holds all of the actual scripts that all runlevels {{{                 can access.

/etc/rc?.d/ — where “?” is either S, or 0->6 inclusive. These

               directories hold symlinks which then point to the various                 scripts that the runlevel needs.

}}}

And the symlinks provide the entire key to getting the runlevel working. Take a look in /etc/rc2.d/ under your distribution, and you’ll see lots of them in the form:

SNNscriptname KNNscriptname

To break this down:

S == Start. When /etc/init.d/rc is called (more about this later) the symlinks that start with S are invoked.  NN == This is a number assignage. How these are assigned depends upon the distribution's policy, and indeed, they do differ. The numbers are allocated in the range 10->99. These scripts are then started in numerical order until all of them have been processed.  K == Kill. The reverse of "S", basically, and these are run when one changes between runlevels.

You may well be wondering just how all of these runlevels are called, or indeed how they’re kicked off. Once init has determined the default runlevel, it’ll look for the line:

l2:2:wait:/etc/init.d/rc 2

(or whatever line corresponds to the default inittab setting).

/etc/init.d/rc is the *controlling* script. It is what init uses when starting the default runlevel, and switching between levels. It is this script which is responsible for starting and stopping all of the services in a particular runlevel (see section above on the symlinks).

You may well be thinking “I don’t ever change run-levels…”. Oh yes you do. :) When ever you shutdown or restart your machine, you’re changing to runlevels 0 and 6 respectively. Again, this demonstrates how /etc/init.d/rc works, when, at the change of a runlevel, killing off services, and if applicable, starting up those needed for the specific run level.

And that’s all there is to it, really. :) The last thing that init does is spawn a getty process.

1:2345:respawn:/sbin/getty 38400 tty1 2:23:respawn:/sbin/getty 38400 tty2 3:23:respawn:/sbin/getty 38400 tty3 4:23:respawn:/sbin/getty 38400 tty4 5:23:respawn:/sbin/getty 38400 tty5 6:23:respawn:/sbin/getty 38400 tty6

So the above tells you that at your disposal, at the console there have been defined six gettys (effectively, what you would consider the “login: ” prompt, although it is more complex than this). It is as this point that init has really done its job, and this is waiting for you to login and spawn a login shell.

Differences between runlevels

Historically, SysV runlevels for Linux have been defined as:

0   -- Shutdown. 1/S -- Single user mode. 2   -- Multi-user mode (no networking). 3   -- Multi-user mode (networking). 4   -- Reserved. 5   -- XDM (or some equivalent DM). 6   -- Reboot.

Redhat, SuSE and Mandrake all use this structure. Debian however does not, essentially run levels 2 -> 5 are all the same.


This page was written by: ThomasAdam

Leave a Reply