Init (WAS: Re:[Sussex]login/logout/reboot/shutdown question (Gentoo)?)
Thomas Adam
thomas at edulinux.homeunix.org
Mon Nov 15 20:06:44 UTC 2004
On Mon, Nov 15, 2004 at 06:47:16PM -0500, John D. wrote:
> As Thomas Adam mentioned, "It appears that your entire init sequence
> is ........" etc etc. I think he'd hit the nail on the head. The
> difficulty being, that for things like that, there's no real examples
> to follow or explainations of things that can be "tweaked" to improve
> this or that.
OK. You want to know how this all works, eh? :) Here goes....
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 distributions 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.
So, to answer your question about why certain things are not working, I
would say that it is because the symlinks in /etc.rc?.d (whatever your
default runlevel is) are broken somehow. So what I would do is check
that directory, and follow the symlinks. Look at the order of them and
map it out for yourself. Usually, the very last program spawned is a
display manager of some kind (XDM, in my case). It should just be a case
of adding new symlinks in, essentially. There are a number of tools
which try and do this with ease, but I find a brain and ln(1) to be the
Best Way (tm).
I hope this helps. I apologise for the vast amount of information, too.
If you have any questions, do let me know.
-- Thomas Adam
--
I know nothing, and understand even less.
More information about the Sussex
mailing list