/[gentoo]/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml
Gentoo

Diff of /xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.3 Revision 1.4
1<!-- The content of this document is licensed under the CC-BY-SA license --> 1<!-- The content of this document is licensed under the CC-BY-SA license -->
2<!-- See http://creativecommons.org/licenses/by-sa/1.0 --> 2<!-- See http://creativecommons.org/licenses/by-sa/1.0 -->
3 3
4<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml,v 1.3 2003/12/03 13:42:05 swift Exp $ --> 4<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml,v 1.4 2003/12/03 15:03:41 swift Exp $ -->
5 5
6<sections> 6<sections>
7<section> 7<section>
8<title>Runlevels</title> 8<title>Runlevels</title>
9<subsection> 9<subsection>
10<title>What is a runlevel?</title>
11<body>
12
13<p>
14When you boot your system, a number of tasks need to be performed before you are
15able to log on. This "normal" boot operation is fully defined -- it will be the
16same every time you restart your system. A <e>runlevel</e> is a state in which
17your system is running and contains a collection of scripts (runlevel scripts or
18<e>initscripts</e>) that must be executed when you enter or leave a runlevel.
19</p>
20
21</body>
22</subsection>
23<subsection>
24<title>Booting your System</title> 10<title>Booting your System</title>
25<body> 11<body>
26 12
27<p> 13<p>
28The process that takes care of the runlevels is called <c>init</c> and is also 14When you boot your system, you will notice lots of text floating by. If you pay
29the first process started by the Linux kernel. <c>init</c>'s configuration file 15close attention, you will notice this text is the same every time you reboot
30is called <path>/etc/inittab</path> and gets read immediately after <c>init</c> 16your system. The sequence of all these actions is called the <e>boot
31is started. In this configuration file, the commands used to enter a certain 17sequence</e> and is (more or less) statically defined.
32runlevel are listed. For instance, after initialising the system (<e>si</e>), 18</p>
33the <b>boot</b> runlevel is started: 19
34</p> 20<p>
21First, your boot loader will load the kernel image you have defined in the
22boot loader configuration into memory after which it tells the CPU to run the
23kernel. When the kernel is loaded and run, it initialises all kernel-specific
24structures and tasks and starts the <c>init</c> process.
25</p>
35 26
27<p>
28This process then makes sure that all filesystems (defined in
29<path>/etc/fstab</path>) are mounted and ready to be used. Then it executes
30several scripts located in <path>/etc/init.d</path>, which will start the
31services you need in order to have a succesfully booted system.
32</p>
33
34<p>
35Finally, when all scripts are executed, <c>init</c> activates the terminals
36(in most cases just the virtual consoles which are hidden beneith <c>Alt-F1</c>,
37<c>Alt-F2</c>, etc.) attaching a special process called <c>agetty</c> to it.
38This process will then make sure you are able to log on through these terminals
39by running <c>login</c>.
40</p>
41
42</body>
43</subsection>
44<subsection>
45<title>Init Scripts</title>
46<body>
47
48<p>
49Now <c>init</c> doesn't just execute the scripts in <path>/etc/init.d</path>
50randomly. Even more, it doesn't run all scripts in <path>/etc/init.d</path>,
51only the scripts it is told to execute. It decides what scripts to execute by
52looking into <path>/etc/runlevels</path>.
53</p>
54
55<p>
56First, <c>init</c> runs all scripts from <path>/etc/init.d</path> that have
57symbolic links inside <path>/etc/runlevels/boot</path>. Usually, it will
58start the scripts in alphabetical order, but some scripts have dependency
59information in them, telling the system that another script must be run before
60they can be started.
61</p>
62
63<p>
64When all <path>/etc/runlevels/boot</path> referenced scripts are executed,
65<c>init</c> continues with running the scripts that have a symbolic link to them
66in <path>/etc/runlevels/default</path>. Again, it will use the alphabetical
67order to decide what script to run first, unless a script has dependency
68information in it, in which the order is changed to provide a valid start-up
69sequence.
70</p>
71
72</body>
73</subsection>
74<subsection>
75<title>How Init Works</title>
76<body>
77
78<p>
79Of course <c>init</c> doesn't decide all that by itself. It needs a
80configuration file that specifies what actions need to be taken. This
81configuration file is <path>/etc/inittab</path>.
82</p>
83
84<p>
85If you remember the boot sequence we have just explained to you, you will
86remember that <c>init</c>'s first action is to mount all filesystems. This is
87defined in the following line from <path>/etc/inittab</path>:
88</p>
89
36<pre caption="System initialisation lines in /etc/inittab"> 90<pre caption="The system initialisation line in /etc/inittab">
37si::sysinit:/sbin/rc sysinit 91si::sysinit:/sbin/rc sysinit
92</pre>
93
94<p>
95This line tells <c>init</c> that it must run <c>/sbin/rc sysinit</c> to
96initialise the system. The <path>/sbin/rc</path> script takes care of the
97initialisation, so you might say that <c>init</c> doesn't do much -- it
98delegates the task of initialising the system to another process.
99</p>
100
101<p>
102Second, <c>init</c> executed all scripts that had symbolic links in
103<path>/etc/runlevels/boot</path>. This is defined in the following line:
104</p>
105
106<pre caption="The system initialisation, continued">
38rc::bootwait:/sbin/rc boot 107rc::bootwait:/sbin/rc boot
39</pre> 108</pre>
40 109
41<p> 110<p>
42As you can see, <c>init</c> relies on the <c>rc</c> script. When started with 111Again the <c>rc</c> script performs the necessary tasks. Note that the option
43the "boot" argument, <c>rc</c> starts all scripts in the 112given to <c>rc</c> (<e>boot</e>) is the same as the subdirectory of
44<path>/etc/runlevels/boot</path> directory. Don't think about the sequence used 113<path>/etc/runlevels</path> that is used.
45to start the scripts -- we will explain later how Gentoo uses dependencies for
46the init scripts.
47</p>
48
49<p> 114</p>
50When the <b>boot</b> runlevel is completed (the boot runlevel is an intermediate 115
51one), <c>init</c> checks what runlevel it should start. If you have not defined 116<p>
52one as kernel parameter, it will use the one defined in 117Now <c>init</c> checks its configuration file to see what <e>runlevel</e> it
118should run. To decide this, it reads the following line from
53<path>/etc/inittab</path>: 119<path>/etc/inittab</path>:
54</p> 120</p>
55 121
56<pre caption="Default runlevel"> 122<pre caption="The initdefault line">
57id:3:initdefault: 123id:3:initdefault:
58</pre> 124</pre>
59 125
60<p> 126<p>
61In this case, the runlevel id is "3", and gets mapped to: 127In this case (which the majority of Gentoo users will use), the <e>runlevel</e>
62</p> 128id is 3. Using this information, <c>init</c> checks what it must run to start
63 129<e>runlevel 3</e>:
64<pre caption="Mapping from number to readable commands in /etc/inittab">
65l3:3:wait:/sbin/rc default
66</pre>
67
68<p> 130</p>
69In other words, the <c>rc</c> script is asked to activate the <b>default</b>
70runlevel. Again, this results in executing all
71<path>/etc/runlevels/default</path> scripts.
72</p>
73 131
74</body> 132<pre caption="The runlevel definitions">
75</subsection>
76<subsection>
77<title>Numbers and Names</title>
78<body>
79
80<p>
81When you take a look at <path>/etc/inittab</path>, you will see the following
82section:
83</p>
84
85<pre caption="Defining the runlevels in /etc/inittab">
86l0:0:wait:/sbin/rc shutdown 133l0:0:wait:/sbin/rc shutdown
87l1:S1:wait:/sbin/rc single 134l1:S1:wait:/sbin/rc single
88l2:2:wait:/sbin/rc nonetwork 135l2:2:wait:/sbin/rc nonetwork
89l3:3:wait:/sbin/rc default 136l3:3:wait:/sbin/rc default
90l4:4:wait:/sbin/rc default 137l4:4:wait:/sbin/rc default
91l5:5:wait:/sbin/rc default 138l5:5:wait:/sbin/rc default
92l6:6:wait:/sbin/rc reboot 139l6:6:wait:/sbin/rc reboot
93</pre> 140</pre>
94 141
95<p> 142<p>
96As you can see, there is a mapping of numbers to names (not vice versa). For 143The line that defines level 3, again, uses the <c>rc</c> script to start the
97instance, 0 maps to "shutdown", 1 to "single" etc. Vice versa is not true, as 144services (now with argument <e>default</e>). Again note that the argument of
98"default" is used by 3, 4 and 5. These numbers are the runlevel numbers. Most 145<c>rc</c> is the same as the subdirectory from <path>/etc/runlevels</path>.
99distributions work with the numbers; Gentoo however decided to make it a bit
100more userfriendly and continue with the naming.
101</p>
102
103<p> 146</p>
104As you can see from the listings, Gentoo defines 7 runlevels. Three of them are 147
105internal runlevels: <e>sysinit</e>, <e>shutdown</e> and <e>reboot</e>. The
106<b>sysinit</b> runlevel mounts all necessary filesystems as defined in
107<path>/etc/fstab</path>. The <b>shutdown</b> runlevel shuts down all running
108services and powers down the system. The <b>reboot</b> runlevel acts like the
109<e>shutdown</e> runlevel, but reboots the system instead of powering down.
110</p> 148<p>
111 149When <c>rc</c> has finished, <c>init</c> decides what virtual consoles it should
150activate and what commands need to be run at each console:
112<p> 151</p>
113The other four runlevels are <e>boot</e>, <e>default</e>, <e>nonetwork</e> and 152
114<e>single</e>. Each of them has a subdirectory in <path>/etc/runlevels</path> 153<pre caption="The virtual consoles definition">
115containing scripts that need to be started when the runlevel is activated. 154c1:12345:respawn:/sbin/agetty 38400 tty1 linux
155c2:12345:respawn:/sbin/agetty 38400 tty2 linux
156c3:12345:respawn:/sbin/agetty 38400 tty3 linux
157c4:12345:respawn:/sbin/agetty 38400 tty4 linux
158c5:12345:respawn:/sbin/agetty 38400 tty5 linux
159c6:12345:respawn:/sbin/agetty 38400 tty6 linux
116</p> 160</pre>
117 161
162
118</body> 163</body>
119</subsection>
120<subsection> 164</subsection>
165<subsection>
166<title>What is a runlevel?</title>
167<body>
168
169<p>
170You have seen that <c>init</c> uses a numbering scheme to decide what
171<e>runlevel</e> it should activate. A <e>runlevel</e> is a state in which
172your system is running and contains a collection of scripts (runlevel scripts or
173<e>initscripts</e>) that must be executed when you enter or leave a runlevel.
174</p>
175
176<p>
177In Gentoo, there are seven runlevels defined: three internal runlevels, and four
178user-defined runlevels. The internal runlevels are called <e>sysinit</e>,
179<e>shutdown</e> and <e>reboot</e> and do exactly what their names imply:
180initialise the system, powering off the system and rebooting the system.
181</p>
182
183<p>
184The user-defined runlevels are those with an accompanying
185<path>/etc/runlevels</path> subdirectory: <path>boot</path>,
186<path>default</path>, <path>nonetwork</path> and <path>single</path>. The
187<path>boot</path> runlevel starts all system-necessary services which all other
188runlevels use. The remaining three runlevels differ in what services they start:
189<path>default</path> is used for day-to-day operations, <path>nonetwork</path>
190is used in case no network connectivity is required, and <path>single</path> is
191used when you need to fix the system.
192</p>
193
194</body>
195</subsection>
196<subsection>
121<title>Working with the initscripts</title> 197<title>Working with the Init Scripts</title>
122<body> 198<body>
123 199
124<p>
125If you take a closer look to <path>/etc/runlevels/default</path>, you will
126notice that it contains symbolic links to identically named scripts located in
127<path>/etc/init.d</path> and not just scripts as we mentioned previously. For
128instance, <path>/etc/runlevels/default/postfix</path> is a symbolic link to
129<path>/etc/init.d/postfix</path>. In general we can say that such a script
130provides a service...
131</p> 200<p>
132 201The scripts that the <c>rc</c> process starts are called <e>init scripts</e>.
133<p>
134Each script in <path>/etc/init.d</path> can be executed with the arguments 202Each script in <path>/etc/init.d</path> can be executed with the arguments
135<e>start</e>, <e>stop</e>, <e>restart</e>, <e>pause</e>, <e>zap</e>, 203<e>start</e>, <e>stop</e>, <e>restart</e>, <e>pause</e>, <e>zap</e>,
136<e>status</e>, <e>ineed</e>, <e>iuse</e>, <e>needsme</e>, <e>usesme</e> or 204<e>status</e>, <e>ineed</e>, <e>iuse</e>, <e>needsme</e>, <e>usesme</e> or
137<e>broken</e>. 205<e>broken</e>.
138</p> 206</p>
143</p> 211</p>
144 212
145<pre caption="Starting Postfix"> 213<pre caption="Starting Postfix">
146# <i>/etc/init.d/postfix start</i> 214# <i>/etc/init.d/postfix start</i>
147</pre> 215</pre>
216
217<note>
218Only the services that <e>need</e> the given service are stopped or restarted.
219The other depending services (those that <e>use</e> the service but don't need
220it) are left untouched.
221</note>
148 222
149<p> 223<p>
150If you want to stop a service, but not the services that depend on it, you can 224If you want to stop a service, but not the services that depend on it, you can
151use the <c>pause</c> argument: 225use the <c>pause</c> argument:
152</p> 226</p>

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.20