| 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> |
|
|
| 14 | When you boot your system, a number of tasks need to be performed before you are |
|
|
| 15 | able to log on. This "normal" boot operation is fully defined -- it will be the |
|
|
| 16 | same every time you restart your system. A <e>runlevel</e> is a state in which |
|
|
| 17 | your 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> |
| 28 | The process that takes care of the runlevels is called <c>init</c> and is also |
14 | When you boot your system, you will notice lots of text floating by. If you pay |
| 29 | the first process started by the Linux kernel. <c>init</c>'s configuration file |
15 | close attention, you will notice this text is the same every time you reboot |
| 30 | is called <path>/etc/inittab</path> and gets read immediately after <c>init</c> |
16 | your system. The sequence of all these actions is called the <e>boot |
| 31 | is started. In this configuration file, the commands used to enter a certain |
17 | sequence</e> and is (more or less) statically defined. |
| 32 | runlevel are listed. For instance, after initialising the system (<e>si</e>), |
18 | </p> |
| 33 | the <b>boot</b> runlevel is started: |
19 | |
| 34 | </p> |
20 | <p> |
|
|
21 | First, your boot loader will load the kernel image you have defined in the |
|
|
22 | boot loader configuration into memory after which it tells the CPU to run the |
|
|
23 | kernel. When the kernel is loaded and run, it initialises all kernel-specific |
|
|
24 | structures and tasks and starts the <c>init</c> process. |
|
|
25 | </p> |
| 35 | |
26 | |
|
|
27 | <p> |
|
|
28 | This process then makes sure that all filesystems (defined in |
|
|
29 | <path>/etc/fstab</path>) are mounted and ready to be used. Then it executes |
|
|
30 | several scripts located in <path>/etc/init.d</path>, which will start the |
|
|
31 | services you need in order to have a succesfully booted system. |
|
|
32 | </p> |
|
|
33 | |
|
|
34 | <p> |
|
|
35 | Finally, 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. |
|
|
38 | This process will then make sure you are able to log on through these terminals |
|
|
39 | by running <c>login</c>. |
|
|
40 | </p> |
|
|
41 | |
|
|
42 | </body> |
|
|
43 | </subsection> |
|
|
44 | <subsection> |
|
|
45 | <title>Init Scripts</title> |
|
|
46 | <body> |
|
|
47 | |
|
|
48 | <p> |
|
|
49 | Now <c>init</c> doesn't just execute the scripts in <path>/etc/init.d</path> |
|
|
50 | randomly. Even more, it doesn't run all scripts in <path>/etc/init.d</path>, |
|
|
51 | only the scripts it is told to execute. It decides what scripts to execute by |
|
|
52 | looking into <path>/etc/runlevels</path>. |
|
|
53 | </p> |
|
|
54 | |
|
|
55 | <p> |
|
|
56 | First, <c>init</c> runs all scripts from <path>/etc/init.d</path> that have |
|
|
57 | symbolic links inside <path>/etc/runlevels/boot</path>. Usually, it will |
|
|
58 | start the scripts in alphabetical order, but some scripts have dependency |
|
|
59 | information in them, telling the system that another script must be run before |
|
|
60 | they can be started. |
|
|
61 | </p> |
|
|
62 | |
|
|
63 | <p> |
|
|
64 | When 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 |
|
|
66 | in <path>/etc/runlevels/default</path>. Again, it will use the alphabetical |
|
|
67 | order to decide what script to run first, unless a script has dependency |
|
|
68 | information in it, in which the order is changed to provide a valid start-up |
|
|
69 | sequence. |
|
|
70 | </p> |
|
|
71 | |
|
|
72 | </body> |
|
|
73 | </subsection> |
|
|
74 | <subsection> |
|
|
75 | <title>How Init Works</title> |
|
|
76 | <body> |
|
|
77 | |
|
|
78 | <p> |
|
|
79 | Of course <c>init</c> doesn't decide all that by itself. It needs a |
|
|
80 | configuration file that specifies what actions need to be taken. This |
|
|
81 | configuration file is <path>/etc/inittab</path>. |
|
|
82 | </p> |
|
|
83 | |
|
|
84 | <p> |
|
|
85 | If you remember the boot sequence we have just explained to you, you will |
|
|
86 | remember that <c>init</c>'s first action is to mount all filesystems. This is |
|
|
87 | defined 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"> |
| 37 | si::sysinit:/sbin/rc sysinit |
91 | si::sysinit:/sbin/rc sysinit |
|
|
92 | </pre> |
|
|
93 | |
|
|
94 | <p> |
|
|
95 | This line tells <c>init</c> that it must run <c>/sbin/rc sysinit</c> to |
|
|
96 | initialise the system. The <path>/sbin/rc</path> script takes care of the |
|
|
97 | initialisation, so you might say that <c>init</c> doesn't do much -- it |
|
|
98 | delegates the task of initialising the system to another process. |
|
|
99 | </p> |
|
|
100 | |
|
|
101 | <p> |
|
|
102 | Second, <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"> |
| 38 | rc::bootwait:/sbin/rc boot |
107 | rc::bootwait:/sbin/rc boot |
| 39 | </pre> |
108 | </pre> |
| 40 | |
109 | |
| 41 | <p> |
110 | <p> |
| 42 | As you can see, <c>init</c> relies on the <c>rc</c> script. When started with |
111 | Again the <c>rc</c> script performs the necessary tasks. Note that the option |
| 43 | the "boot" argument, <c>rc</c> starts all scripts in the |
112 | given 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. |
| 45 | to start the scripts -- we will explain later how Gentoo uses dependencies for |
|
|
| 46 | the init scripts. |
|
|
| 47 | </p> |
|
|
| 48 | |
|
|
| 49 | <p> |
114 | </p> |
| 50 | When the <b>boot</b> runlevel is completed (the boot runlevel is an intermediate |
115 | |
| 51 | one), <c>init</c> checks what runlevel it should start. If you have not defined |
116 | <p> |
| 52 | one as kernel parameter, it will use the one defined in |
117 | Now <c>init</c> checks its configuration file to see what <e>runlevel</e> it |
|
|
118 | should 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"> |
| 57 | id:3:initdefault: |
123 | id:3:initdefault: |
| 58 | </pre> |
124 | </pre> |
| 59 | |
125 | |
| 60 | <p> |
126 | <p> |
| 61 | In this case, the runlevel id is "3", and gets mapped to: |
127 | In this case (which the majority of Gentoo users will use), the <e>runlevel</e> |
| 62 | </p> |
128 | id 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"> |
|
|
| 65 | l3:3:wait:/sbin/rc default |
|
|
| 66 | </pre> |
|
|
| 67 | |
|
|
| 68 | <p> |
130 | </p> |
| 69 | In other words, the <c>rc</c> script is asked to activate the <b>default</b> |
|
|
| 70 | runlevel. 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> |
|
|
| 81 | When you take a look at <path>/etc/inittab</path>, you will see the following |
|
|
| 82 | section: |
|
|
| 83 | </p> |
|
|
| 84 | |
|
|
| 85 | <pre caption="Defining the runlevels in /etc/inittab"> |
|
|
| 86 | l0:0:wait:/sbin/rc shutdown |
133 | l0:0:wait:/sbin/rc shutdown |
| 87 | l1:S1:wait:/sbin/rc single |
134 | l1:S1:wait:/sbin/rc single |
| 88 | l2:2:wait:/sbin/rc nonetwork |
135 | l2:2:wait:/sbin/rc nonetwork |
| 89 | l3:3:wait:/sbin/rc default |
136 | l3:3:wait:/sbin/rc default |
| 90 | l4:4:wait:/sbin/rc default |
137 | l4:4:wait:/sbin/rc default |
| 91 | l5:5:wait:/sbin/rc default |
138 | l5:5:wait:/sbin/rc default |
| 92 | l6:6:wait:/sbin/rc reboot |
139 | l6:6:wait:/sbin/rc reboot |
| 93 | </pre> |
140 | </pre> |
| 94 | |
141 | |
| 95 | <p> |
142 | <p> |
| 96 | As you can see, there is a mapping of numbers to names (not vice versa). For |
143 | The line that defines level 3, again, uses the <c>rc</c> script to start the |
| 97 | instance, 0 maps to "shutdown", 1 to "single" etc. Vice versa is not true, as |
144 | services (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>. |
| 99 | distributions work with the numbers; Gentoo however decided to make it a bit |
|
|
| 100 | more userfriendly and continue with the naming. |
|
|
| 101 | </p> |
|
|
| 102 | |
|
|
| 103 | <p> |
146 | </p> |
| 104 | As you can see from the listings, Gentoo defines 7 runlevels. Three of them are |
147 | |
| 105 | internal 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 |
|
|
| 108 | services 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 | |
149 | When <c>rc</c> has finished, <c>init</c> decides what virtual consoles it should |
|
|
150 | activate and what commands need to be run at each console: |
| 112 | <p> |
151 | </p> |
| 113 | The 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"> |
| 115 | containing scripts that need to be started when the runlevel is activated. |
154 | c1:12345:respawn:/sbin/agetty 38400 tty1 linux |
|
|
155 | c2:12345:respawn:/sbin/agetty 38400 tty2 linux |
|
|
156 | c3:12345:respawn:/sbin/agetty 38400 tty3 linux |
|
|
157 | c4:12345:respawn:/sbin/agetty 38400 tty4 linux |
|
|
158 | c5:12345:respawn:/sbin/agetty 38400 tty5 linux |
|
|
159 | c6: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> |
|
|
170 | You 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 |
|
|
172 | your 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> |
|
|
177 | In Gentoo, there are seven runlevels defined: three internal runlevels, and four |
|
|
178 | user-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: |
|
|
180 | initialise the system, powering off the system and rebooting the system. |
|
|
181 | </p> |
|
|
182 | |
|
|
183 | <p> |
|
|
184 | The 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 |
|
|
188 | runlevels 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> |
|
|
190 | is used in case no network connectivity is required, and <path>single</path> is |
|
|
191 | used 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> |
|
|
| 125 | If you take a closer look to <path>/etc/runlevels/default</path>, you will |
|
|
| 126 | notice 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 |
|
|
| 128 | instance, <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 |
|
|
| 130 | provides a service... |
|
|
| 131 | </p> |
200 | <p> |
| 132 | |
201 | The scripts that the <c>rc</c> process starts are called <e>init scripts</e>. |
| 133 | <p> |
|
|
| 134 | Each script in <path>/etc/init.d</path> can be executed with the arguments |
202 | Each 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> |
|
|
218 | Only the services that <e>need</e> the given service are stopped or restarted. |
|
|
219 | The other depending services (those that <e>use</e> the service but don't need |
|
|
220 | it) are left untouched. |
|
|
221 | </note> |
| 148 | |
222 | |
| 149 | <p> |
223 | <p> |
| 150 | If you want to stop a service, but not the services that depend on it, you can |
224 | If you want to stop a service, but not the services that depend on it, you can |
| 151 | use the <c>pause</c> argument: |
225 | use the <c>pause</c> argument: |
| 152 | </p> |
226 | </p> |