| 1 |
swift |
1.10 |
<?xml version='1.0' encoding='UTF-8'?> |
| 2 |
|
|
<!DOCTYPE sections SYSTEM "/dtd/book.dtd"> |
| 3 |
|
|
|
| 4 |
swift |
1.1 |
<!-- The content of this document is licensed under the CC-BY-SA license --> |
| 5 |
neysx |
1.25 |
<!-- See http://creativecommons.org/licenses/by-sa/2.5 --> |
| 6 |
swift |
1.1 |
|
| 7 |
nightmorph |
1.30 |
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml,v 1.29 2007/03/08 06:52:27 nightmorph Exp $ --> |
| 8 |
swift |
1.1 |
|
| 9 |
|
|
<sections> |
| 10 |
swift |
1.18 |
|
| 11 |
neysx |
1.27 |
<abstract> |
| 12 |
|
|
Gentoo uses a special initscript format which, amongst other features, allows |
| 13 |
|
|
dependency-driven decisions and virtual initscripts. This chapter explains all |
| 14 |
|
|
these aspects and explains how to deal with these scripts. |
| 15 |
|
|
</abstract> |
| 16 |
|
|
|
| 17 |
nightmorph |
1.30 |
<version>1.24</version> |
| 18 |
|
|
<date>2007-07-07</date> |
| 19 |
swift |
1.18 |
|
| 20 |
swift |
1.1 |
<section> |
| 21 |
|
|
<title>Runlevels</title> |
| 22 |
|
|
<subsection> |
| 23 |
swift |
1.4 |
<title>Booting your System</title> |
| 24 |
|
|
<body> |
| 25 |
|
|
|
| 26 |
|
|
<p> |
| 27 |
|
|
When you boot your system, you will notice lots of text floating by. If you pay |
| 28 |
|
|
close attention, you will notice this text is the same every time you reboot |
| 29 |
|
|
your system. The sequence of all these actions is called the <e>boot |
| 30 |
|
|
sequence</e> and is (more or less) statically defined. |
| 31 |
|
|
</p> |
| 32 |
|
|
|
| 33 |
|
|
<p> |
| 34 |
|
|
First, your boot loader will load the kernel image you have defined in the |
| 35 |
|
|
boot loader configuration into memory after which it tells the CPU to run the |
| 36 |
swift |
1.6 |
kernel. When the kernel is loaded and run, it initializes all kernel-specific |
| 37 |
swift |
1.4 |
structures and tasks and starts the <c>init</c> process. |
| 38 |
|
|
</p> |
| 39 |
|
|
|
| 40 |
|
|
<p> |
| 41 |
|
|
This process then makes sure that all filesystems (defined in |
| 42 |
|
|
<path>/etc/fstab</path>) are mounted and ready to be used. Then it executes |
| 43 |
|
|
several scripts located in <path>/etc/init.d</path>, which will start the |
| 44 |
swift |
1.8 |
services you need in order to have a successfully booted system. |
| 45 |
swift |
1.4 |
</p> |
| 46 |
|
|
|
| 47 |
|
|
<p> |
| 48 |
|
|
Finally, when all scripts are executed, <c>init</c> activates the terminals |
| 49 |
swift |
1.14 |
(in most cases just the virtual consoles which are hidden beneath <c>Alt-F1</c>, |
| 50 |
swift |
1.4 |
<c>Alt-F2</c>, etc.) attaching a special process called <c>agetty</c> to it. |
| 51 |
|
|
This process will then make sure you are able to log on through these terminals |
| 52 |
|
|
by running <c>login</c>. |
| 53 |
|
|
</p> |
| 54 |
|
|
|
| 55 |
|
|
</body> |
| 56 |
|
|
</subsection> |
| 57 |
|
|
<subsection> |
| 58 |
|
|
<title>Init Scripts</title> |
| 59 |
swift |
1.1 |
<body> |
| 60 |
|
|
|
| 61 |
swift |
1.2 |
<p> |
| 62 |
swift |
1.4 |
Now <c>init</c> doesn't just execute the scripts in <path>/etc/init.d</path> |
| 63 |
|
|
randomly. Even more, it doesn't run all scripts in <path>/etc/init.d</path>, |
| 64 |
swift |
1.7 |
only the scripts it is told to execute. It decides which scripts to execute by |
| 65 |
swift |
1.4 |
looking into <path>/etc/runlevels</path>. |
| 66 |
|
|
</p> |
| 67 |
|
|
|
| 68 |
|
|
<p> |
| 69 |
|
|
First, <c>init</c> runs all scripts from <path>/etc/init.d</path> that have |
| 70 |
|
|
symbolic links inside <path>/etc/runlevels/boot</path>. Usually, it will |
| 71 |
|
|
start the scripts in alphabetical order, but some scripts have dependency |
| 72 |
|
|
information in them, telling the system that another script must be run before |
| 73 |
|
|
they can be started. |
| 74 |
|
|
</p> |
| 75 |
|
|
|
| 76 |
|
|
<p> |
| 77 |
|
|
When all <path>/etc/runlevels/boot</path> referenced scripts are executed, |
| 78 |
|
|
<c>init</c> continues with running the scripts that have a symbolic link to them |
| 79 |
|
|
in <path>/etc/runlevels/default</path>. Again, it will use the alphabetical |
| 80 |
|
|
order to decide what script to run first, unless a script has dependency |
| 81 |
swift |
1.7 |
information in it, in which case the order is changed to provide a valid |
| 82 |
|
|
start-up sequence. |
| 83 |
swift |
1.2 |
</p> |
| 84 |
|
|
|
| 85 |
|
|
</body> |
| 86 |
|
|
</subsection> |
| 87 |
|
|
<subsection> |
| 88 |
swift |
1.4 |
<title>How Init Works</title> |
| 89 |
swift |
1.2 |
<body> |
| 90 |
|
|
|
| 91 |
|
|
<p> |
| 92 |
swift |
1.4 |
Of course <c>init</c> doesn't decide all that by itself. It needs a |
| 93 |
|
|
configuration file that specifies what actions need to be taken. This |
| 94 |
|
|
configuration file is <path>/etc/inittab</path>. |
| 95 |
swift |
1.2 |
</p> |
| 96 |
|
|
|
| 97 |
swift |
1.4 |
<p> |
| 98 |
neysx |
1.16 |
If you remember the boot sequence we have just described, you will remember |
| 99 |
|
|
that <c>init</c>'s first action is to mount all filesystems. This is defined in |
| 100 |
|
|
the following line from <path>/etc/inittab</path>: |
| 101 |
swift |
1.4 |
</p> |
| 102 |
|
|
|
| 103 |
|
|
<pre caption="The system initialisation line in /etc/inittab"> |
| 104 |
swift |
1.2 |
si::sysinit:/sbin/rc sysinit |
| 105 |
|
|
</pre> |
| 106 |
|
|
|
| 107 |
|
|
<p> |
| 108 |
swift |
1.4 |
This line tells <c>init</c> that it must run <c>/sbin/rc sysinit</c> to |
| 109 |
swift |
1.6 |
initialize the system. The <path>/sbin/rc</path> script takes care of the |
| 110 |
swift |
1.4 |
initialisation, so you might say that <c>init</c> doesn't do much -- it |
| 111 |
|
|
delegates the task of initialising the system to another process. |
| 112 |
swift |
1.2 |
</p> |
| 113 |
|
|
|
| 114 |
|
|
<p> |
| 115 |
swift |
1.4 |
Second, <c>init</c> executed all scripts that had symbolic links in |
| 116 |
|
|
<path>/etc/runlevels/boot</path>. This is defined in the following line: |
| 117 |
swift |
1.2 |
</p> |
| 118 |
|
|
|
| 119 |
swift |
1.4 |
<pre caption="The system initialisation, continued"> |
| 120 |
|
|
rc::bootwait:/sbin/rc boot |
| 121 |
swift |
1.2 |
</pre> |
| 122 |
|
|
|
| 123 |
|
|
<p> |
| 124 |
swift |
1.4 |
Again the <c>rc</c> script performs the necessary tasks. Note that the option |
| 125 |
|
|
given to <c>rc</c> (<e>boot</e>) is the same as the subdirectory of |
| 126 |
|
|
<path>/etc/runlevels</path> that is used. |
| 127 |
swift |
1.2 |
</p> |
| 128 |
|
|
|
| 129 |
|
|
<p> |
| 130 |
swift |
1.4 |
Now <c>init</c> checks its configuration file to see what <e>runlevel</e> it |
| 131 |
|
|
should run. To decide this, it reads the following line from |
| 132 |
|
|
<path>/etc/inittab</path>: |
| 133 |
swift |
1.2 |
</p> |
| 134 |
|
|
|
| 135 |
swift |
1.4 |
<pre caption="The initdefault line"> |
| 136 |
|
|
id:3:initdefault: |
| 137 |
|
|
</pre> |
| 138 |
swift |
1.2 |
|
| 139 |
|
|
<p> |
| 140 |
swift |
1.4 |
In this case (which the majority of Gentoo users will use), the <e>runlevel</e> |
| 141 |
|
|
id is 3. Using this information, <c>init</c> checks what it must run to start |
| 142 |
|
|
<e>runlevel 3</e>: |
| 143 |
swift |
1.2 |
</p> |
| 144 |
|
|
|
| 145 |
swift |
1.4 |
<pre caption="The runlevel definitions"> |
| 146 |
swift |
1.2 |
l0:0:wait:/sbin/rc shutdown |
| 147 |
|
|
l1:S1:wait:/sbin/rc single |
| 148 |
|
|
l2:2:wait:/sbin/rc nonetwork |
| 149 |
|
|
l3:3:wait:/sbin/rc default |
| 150 |
|
|
l4:4:wait:/sbin/rc default |
| 151 |
|
|
l5:5:wait:/sbin/rc default |
| 152 |
|
|
l6:6:wait:/sbin/rc reboot |
| 153 |
|
|
</pre> |
| 154 |
|
|
|
| 155 |
|
|
<p> |
| 156 |
swift |
1.4 |
The line that defines level 3, again, uses the <c>rc</c> script to start the |
| 157 |
|
|
services (now with argument <e>default</e>). Again note that the argument of |
| 158 |
|
|
<c>rc</c> is the same as the subdirectory from <path>/etc/runlevels</path>. |
| 159 |
swift |
1.2 |
</p> |
| 160 |
|
|
|
| 161 |
|
|
<p> |
| 162 |
swift |
1.4 |
When <c>rc</c> has finished, <c>init</c> decides what virtual consoles it should |
| 163 |
|
|
activate and what commands need to be run at each console: |
| 164 |
swift |
1.2 |
</p> |
| 165 |
|
|
|
| 166 |
swift |
1.4 |
<pre caption="The virtual consoles definition"> |
| 167 |
|
|
c1:12345:respawn:/sbin/agetty 38400 tty1 linux |
| 168 |
|
|
c2:12345:respawn:/sbin/agetty 38400 tty2 linux |
| 169 |
|
|
c3:12345:respawn:/sbin/agetty 38400 tty3 linux |
| 170 |
|
|
c4:12345:respawn:/sbin/agetty 38400 tty4 linux |
| 171 |
|
|
c5:12345:respawn:/sbin/agetty 38400 tty5 linux |
| 172 |
|
|
c6:12345:respawn:/sbin/agetty 38400 tty6 linux |
| 173 |
|
|
</pre> |
| 174 |
|
|
|
| 175 |
swift |
1.1 |
|
| 176 |
|
|
</body> |
| 177 |
|
|
</subsection> |
| 178 |
|
|
<subsection> |
| 179 |
swift |
1.4 |
<title>What is a runlevel?</title> |
| 180 |
swift |
1.1 |
<body> |
| 181 |
|
|
|
| 182 |
swift |
1.2 |
<p> |
| 183 |
swift |
1.4 |
You have seen that <c>init</c> uses a numbering scheme to decide what |
| 184 |
|
|
<e>runlevel</e> it should activate. A <e>runlevel</e> is a state in which |
| 185 |
|
|
your system is running and contains a collection of scripts (runlevel scripts or |
| 186 |
|
|
<e>initscripts</e>) that must be executed when you enter or leave a runlevel. |
| 187 |
|
|
</p> |
| 188 |
|
|
|
| 189 |
|
|
<p> |
| 190 |
|
|
In Gentoo, there are seven runlevels defined: three internal runlevels, and four |
| 191 |
|
|
user-defined runlevels. The internal runlevels are called <e>sysinit</e>, |
| 192 |
|
|
<e>shutdown</e> and <e>reboot</e> and do exactly what their names imply: |
| 193 |
swift |
1.6 |
initialize the system, powering off the system and rebooting the system. |
| 194 |
swift |
1.4 |
</p> |
| 195 |
|
|
|
| 196 |
|
|
<p> |
| 197 |
|
|
The user-defined runlevels are those with an accompanying |
| 198 |
|
|
<path>/etc/runlevels</path> subdirectory: <path>boot</path>, |
| 199 |
|
|
<path>default</path>, <path>nonetwork</path> and <path>single</path>. The |
| 200 |
|
|
<path>boot</path> runlevel starts all system-necessary services which all other |
| 201 |
|
|
runlevels use. The remaining three runlevels differ in what services they start: |
| 202 |
|
|
<path>default</path> is used for day-to-day operations, <path>nonetwork</path> |
| 203 |
|
|
is used in case no network connectivity is required, and <path>single</path> is |
| 204 |
|
|
used when you need to fix the system. |
| 205 |
swift |
1.2 |
</p> |
| 206 |
|
|
|
| 207 |
swift |
1.4 |
</body> |
| 208 |
|
|
</subsection> |
| 209 |
|
|
<subsection> |
| 210 |
|
|
<title>Working with the Init Scripts</title> |
| 211 |
|
|
<body> |
| 212 |
|
|
|
| 213 |
swift |
1.2 |
<p> |
| 214 |
swift |
1.4 |
The scripts that the <c>rc</c> process starts are called <e>init scripts</e>. |
| 215 |
swift |
1.2 |
Each script in <path>/etc/init.d</path> can be executed with the arguments |
| 216 |
|
|
<e>start</e>, <e>stop</e>, <e>restart</e>, <e>pause</e>, <e>zap</e>, |
| 217 |
|
|
<e>status</e>, <e>ineed</e>, <e>iuse</e>, <e>needsme</e>, <e>usesme</e> or |
| 218 |
|
|
<e>broken</e>. |
| 219 |
|
|
</p> |
| 220 |
|
|
|
| 221 |
|
|
<p> |
| 222 |
|
|
To start, stop or restart a service (and all depending services), <c>start</c>, |
| 223 |
|
|
<c>stop</c> and <c>restart</c> should be used: |
| 224 |
|
|
</p> |
| 225 |
|
|
|
| 226 |
|
|
<pre caption="Starting Postfix"> |
| 227 |
|
|
# <i>/etc/init.d/postfix start</i> |
| 228 |
|
|
</pre> |
| 229 |
swift |
1.4 |
|
| 230 |
|
|
<note> |
| 231 |
|
|
Only the services that <e>need</e> the given service are stopped or restarted. |
| 232 |
|
|
The other depending services (those that <e>use</e> the service but don't need |
| 233 |
|
|
it) are left untouched. |
| 234 |
|
|
</note> |
| 235 |
swift |
1.2 |
|
| 236 |
|
|
<p> |
| 237 |
|
|
If you want to stop a service, but not the services that depend on it, you can |
| 238 |
|
|
use the <c>pause</c> argument: |
| 239 |
|
|
</p> |
| 240 |
|
|
|
| 241 |
|
|
<pre caption="Stopping Postfix but keep the depending services running"> |
| 242 |
|
|
# <i>/etc/init.d/postfix pause</i> |
| 243 |
|
|
</pre> |
| 244 |
|
|
|
| 245 |
|
|
<p> |
| 246 |
|
|
If you want to see what status a service has (started, stopped, paused, ...) you |
| 247 |
|
|
can use the <c>status</c> argument: |
| 248 |
|
|
</p> |
| 249 |
|
|
|
| 250 |
|
|
<pre caption="Status information for postfix"> |
| 251 |
|
|
# <i>/etc/init.d/postfix status</i> |
| 252 |
|
|
</pre> |
| 253 |
|
|
|
| 254 |
|
|
<p> |
| 255 |
swift |
1.3 |
If the status information tells you that the service is running, but you know |
| 256 |
|
|
that it is not, then you can reset the status information to "stopped" with the |
| 257 |
|
|
<c>zap</c> argument: |
| 258 |
swift |
1.2 |
</p> |
| 259 |
|
|
|
| 260 |
|
|
<pre caption="Resetting status information for postfix"> |
| 261 |
|
|
# <i>/etc/init.d/postfix zap</i> |
| 262 |
|
|
</pre> |
| 263 |
|
|
|
| 264 |
|
|
<p> |
| 265 |
|
|
To also ask what dependencies the service has, you can use <c>iuse</c> or |
| 266 |
|
|
<c>ineed</c>. With <c>ineed</c> you can see the services that are really |
| 267 |
|
|
necessary for the correct functioning of the service. <c>iuse</c> on the other |
| 268 |
|
|
hand shows the services that can be used by the service, but are not necessary |
| 269 |
|
|
for the correct functioning. |
| 270 |
|
|
</p> |
| 271 |
|
|
|
| 272 |
|
|
<pre caption="Requesting a list of all necessary services on which Postfix depends"> |
| 273 |
|
|
# <i>/etc/init.d/postfix ineed</i> |
| 274 |
|
|
</pre> |
| 275 |
|
|
|
| 276 |
|
|
<p> |
| 277 |
|
|
Similarly, you can ask what services require the service (<c>needsme</c>) or can |
| 278 |
|
|
use it (<c>usesme</c>): |
| 279 |
|
|
</p> |
| 280 |
|
|
|
| 281 |
|
|
<pre caption="Requesting a list of all services that require Postfix"> |
| 282 |
|
|
# <i>/etc/init.d/postfix needsme</i> |
| 283 |
|
|
</pre> |
| 284 |
|
|
|
| 285 |
|
|
<p> |
| 286 |
neysx |
1.16 |
Finally, you can ask what dependencies the service requires that are missing: |
| 287 |
swift |
1.2 |
</p> |
| 288 |
|
|
|
| 289 |
|
|
<pre caption="Requesting a list of missing dependencies for Postfix"> |
| 290 |
|
|
# <i>/etc/init.d/postfix broken</i> |
| 291 |
|
|
</pre> |
| 292 |
swift |
1.1 |
|
| 293 |
|
|
</body> |
| 294 |
|
|
</subsection> |
| 295 |
|
|
</section> |
| 296 |
|
|
<section> |
| 297 |
|
|
<title>Working with rc-update</title> |
| 298 |
|
|
<subsection> |
| 299 |
|
|
<title>What is rc-update?</title> |
| 300 |
|
|
<body> |
| 301 |
|
|
|
| 302 |
swift |
1.2 |
<p> |
| 303 |
|
|
Gentoo's init system uses a dependency-tree to decide what service needs to be |
| 304 |
neysx |
1.16 |
started first. As this is a tedious task that we wouldn't want our users to |
| 305 |
|
|
have to do manually, we have created tools that ease the administration of the |
| 306 |
|
|
runlevels and init scripts. |
| 307 |
swift |
1.2 |
</p> |
| 308 |
|
|
|
| 309 |
|
|
<p> |
| 310 |
|
|
With <c>rc-update</c> you can add and remove init scripts to a runlevel. The |
| 311 |
|
|
<c>rc-update</c> tool will then automatically ask the <c>depscan.sh</c> script |
| 312 |
|
|
to rebuild the dependency tree. |
| 313 |
|
|
</p> |
| 314 |
|
|
|
| 315 |
swift |
1.1 |
</body> |
| 316 |
|
|
</subsection> |
| 317 |
|
|
<subsection> |
| 318 |
|
|
<title>Adding and Removing Services</title> |
| 319 |
|
|
<body> |
| 320 |
|
|
|
| 321 |
swift |
1.2 |
<p> |
| 322 |
|
|
You have already added init scripts to the "default" runlevel during the |
| 323 |
swift |
1.7 |
installation of Gentoo. At that time you might not have had a clue what the |
| 324 |
swift |
1.2 |
"default" is for, but now you should. The <c>rc-update</c> script requires a |
| 325 |
|
|
second argument that defines the action: <e>add</e>, <e>del</e> or <e>show</e>. |
| 326 |
|
|
</p> |
| 327 |
|
|
|
| 328 |
|
|
<p> |
| 329 |
|
|
To add or remove an init script, just give <c>rc-update</c> the <c>add</c> or |
| 330 |
|
|
<c>del</c> argument, followed by the init script and the runlevel. For instance: |
| 331 |
|
|
</p> |
| 332 |
|
|
|
| 333 |
|
|
<pre caption="Removing Postfix from the default runlevel"> |
| 334 |
|
|
# <i>rc-update del postfix default</i> |
| 335 |
|
|
</pre> |
| 336 |
|
|
|
| 337 |
|
|
<p> |
| 338 |
nightmorph |
1.30 |
The <c>rc-update -v show</c> command will show all the available init scripts and |
| 339 |
swift |
1.2 |
list at which runlevels they will execute: |
| 340 |
|
|
</p> |
| 341 |
|
|
|
| 342 |
|
|
<pre caption="Receiving init script information"> |
| 343 |
nightmorph |
1.30 |
# <i>rc-update -v show</i> |
| 344 |
swift |
1.2 |
</pre> |
| 345 |
swift |
1.1 |
|
| 346 |
nightmorph |
1.30 |
<p> |
| 347 |
|
|
You can also run <c>rc-update show</c> (without <c>-v</c>) to just view enabled |
| 348 |
|
|
init scripts and their runlevels. |
| 349 |
|
|
</p> |
| 350 |
|
|
|
| 351 |
swift |
1.1 |
</body> |
| 352 |
|
|
</subsection> |
| 353 |
|
|
</section> |
| 354 |
|
|
<section> |
| 355 |
|
|
<title>Configuring Services</title> |
| 356 |
|
|
<subsection> |
| 357 |
swift |
1.2 |
<title>Why the Need for Extra Configuration?</title> |
| 358 |
swift |
1.1 |
<body> |
| 359 |
|
|
|
| 360 |
swift |
1.2 |
<p> |
| 361 |
neysx |
1.16 |
Init scripts can be quite complex. It is therefore not really desirable to |
| 362 |
|
|
have the users edit the init script directly, as it would make it more |
| 363 |
swift |
1.5 |
error-prone. It is however important to be able to configure such a service. For |
| 364 |
|
|
instance, you might want to give more options to the service itself. |
| 365 |
swift |
1.2 |
</p> |
| 366 |
|
|
|
| 367 |
|
|
<p> |
| 368 |
neysx |
1.16 |
A second reason to have this configuration outside the init script is to be |
| 369 |
|
|
able to update the init scripts without the fear that your configuration |
| 370 |
|
|
changes will be undone. |
| 371 |
swift |
1.2 |
</p> |
| 372 |
|
|
|
| 373 |
swift |
1.1 |
</body> |
| 374 |
|
|
</subsection> |
| 375 |
|
|
<subsection> |
| 376 |
|
|
<title>The /etc/conf.d Directory</title> |
| 377 |
|
|
<body> |
| 378 |
|
|
|
| 379 |
swift |
1.2 |
<p> |
| 380 |
|
|
Gentoo provides an easy way to configure such a service: every init script that |
| 381 |
|
|
can be configured has a file in <path>/etc/conf.d</path>. For instance, the |
| 382 |
|
|
apache2 initscript (called <path>/etc/init.d/apache2</path>) has a |
| 383 |
|
|
configuration file called <path>/etc/conf.d/apache2</path>, which can contain |
| 384 |
|
|
the options you want to give to the Apache 2 server when it is started: |
| 385 |
|
|
</p> |
| 386 |
|
|
|
| 387 |
|
|
<pre caption="Variable defined in /etc/conf.d/apache2"> |
| 388 |
|
|
APACHE2_OPTS="-D PHP4" |
| 389 |
|
|
</pre> |
| 390 |
|
|
|
| 391 |
|
|
<p> |
| 392 |
|
|
Such a configuration file contains variables and variables alone (just like |
| 393 |
|
|
<path>/etc/make.conf</path>), making it very easy to configure services. It also |
| 394 |
|
|
allows us to provide more information about the variables (as comments). |
| 395 |
|
|
</p> |
| 396 |
|
|
|
| 397 |
swift |
1.1 |
</body> |
| 398 |
|
|
</subsection> |
| 399 |
|
|
</section> |
| 400 |
|
|
<section> |
| 401 |
swift |
1.2 |
<title>Writing Init Scripts</title> |
| 402 |
swift |
1.1 |
<subsection> |
| 403 |
|
|
<title>Do I Have To?</title> |
| 404 |
|
|
<body> |
| 405 |
|
|
|
| 406 |
swift |
1.2 |
<p> |
| 407 |
neysx |
1.16 |
No, writing an init script is usually not necessary as Gentoo provides |
| 408 |
swift |
1.2 |
ready-to-use init scripts for all provided services. However, you might have |
| 409 |
|
|
installed a service without using Portage, in which case you will most likely |
| 410 |
|
|
have to create an init script. |
| 411 |
|
|
</p> |
| 412 |
|
|
|
| 413 |
|
|
<p> |
| 414 |
|
|
Do not use the init script provided by the service if it isn't explicitly |
| 415 |
|
|
written for Gentoo: Gentoo's init scripts are not compatible with the init |
| 416 |
|
|
scripts used by other distributions! |
| 417 |
|
|
</p> |
| 418 |
|
|
|
| 419 |
swift |
1.1 |
</body> |
| 420 |
|
|
</subsection> |
| 421 |
|
|
<subsection> |
| 422 |
|
|
<title>Layout</title> |
| 423 |
|
|
<body> |
| 424 |
|
|
|
| 425 |
swift |
1.2 |
<p> |
| 426 |
|
|
The basic layout of an init script is shown below. |
| 427 |
|
|
</p> |
| 428 |
|
|
|
| 429 |
|
|
<pre caption="Basic layout of an init script"> |
| 430 |
|
|
#!/sbin/runscript |
| 431 |
|
|
|
| 432 |
|
|
depend() { |
| 433 |
|
|
<comment>(Dependency information)</comment> |
| 434 |
|
|
} |
| 435 |
|
|
|
| 436 |
|
|
start() { |
| 437 |
|
|
<comment>(Commands necessary to start the service)</comment> |
| 438 |
|
|
} |
| 439 |
|
|
|
| 440 |
|
|
stop() { |
| 441 |
|
|
<comment>(Commands necessary to stop the service)</comment> |
| 442 |
|
|
} |
| 443 |
|
|
|
| 444 |
|
|
restart() { |
| 445 |
|
|
<comment>(Commands necessary to restart the service)</comment> |
| 446 |
|
|
} |
| 447 |
|
|
</pre> |
| 448 |
|
|
|
| 449 |
|
|
<p> |
| 450 |
|
|
Any init script <e>requires</e> the <c>start()</c> function to be defined. All |
| 451 |
|
|
other sections are optional. |
| 452 |
|
|
</p> |
| 453 |
|
|
|
| 454 |
swift |
1.1 |
</body> |
| 455 |
|
|
</subsection> |
| 456 |
|
|
<subsection> |
| 457 |
|
|
<title>Dependencies</title> |
| 458 |
|
|
<body> |
| 459 |
|
|
|
| 460 |
swift |
1.2 |
<p> |
| 461 |
|
|
There are two dependencies you can define: <c>use</c> and <c>need</c>. As we |
| 462 |
|
|
have mentioned before, the <c>need</c> dependency is more strict than the |
| 463 |
|
|
<c>use</c> dependency. Following this dependency type you enter the service |
| 464 |
|
|
you depend on, or the <e>virtual</e> dependency. |
| 465 |
|
|
</p> |
| 466 |
|
|
|
| 467 |
|
|
<p> |
| 468 |
|
|
A <e>virtual</e> dependency is a dependency that a service provides, but that is |
| 469 |
|
|
not provided solely by that service. Your init script can depend on a system |
| 470 |
|
|
logger, but there are many system loggers available (metalogd, syslog-ng, |
| 471 |
|
|
sysklogd, ...). As you cannot <c>need</c> every single one of them (no sensible |
| 472 |
|
|
system has all these system loggers installed and running) we made sure that |
| 473 |
|
|
all these services <c>provide</c> a virtual dependency. |
| 474 |
|
|
</p> |
| 475 |
|
|
|
| 476 |
|
|
<p> |
| 477 |
|
|
Let us take a look at the dependency information for the postfix service. |
| 478 |
|
|
</p> |
| 479 |
|
|
|
| 480 |
|
|
<pre caption="Dependency information for Postfix"> |
| 481 |
|
|
depend() { |
| 482 |
|
|
need net |
| 483 |
|
|
use logger dns |
| 484 |
|
|
provide mta |
| 485 |
|
|
} |
| 486 |
|
|
</pre> |
| 487 |
|
|
|
| 488 |
|
|
<p> |
| 489 |
|
|
As you can see, the postfix service: |
| 490 |
|
|
</p> |
| 491 |
|
|
|
| 492 |
|
|
<ul> |
| 493 |
|
|
<li> |
| 494 |
|
|
requires the (virtual) <c>net</c> dependency (which is provided by, for |
| 495 |
|
|
instance, <path>/etc/init.d/net.eth0</path>) |
| 496 |
|
|
</li> |
| 497 |
|
|
<li> |
| 498 |
|
|
uses the (virtual) <c>logger</c> dependency (which is provided by, for |
| 499 |
|
|
instance, <path>/etc/init.d/syslog-ng</path>) |
| 500 |
|
|
</li> |
| 501 |
|
|
<li> |
| 502 |
|
|
uses the (virtual) <c>dns</c> dependency (which is provided by, for |
| 503 |
|
|
instance, <path>/etc/init.d/named</path>) |
| 504 |
|
|
</li> |
| 505 |
|
|
<li> |
| 506 |
|
|
provides the (virtual) <c>mta</c> dependency (which is common for all mail |
| 507 |
|
|
servers) |
| 508 |
|
|
</li> |
| 509 |
|
|
</ul> |
| 510 |
|
|
|
| 511 |
swift |
1.1 |
</body> |
| 512 |
|
|
</subsection> |
| 513 |
|
|
<subsection> |
| 514 |
|
|
<title>Controlling the Order</title> |
| 515 |
|
|
<body> |
| 516 |
|
|
|
| 517 |
swift |
1.2 |
<p> |
| 518 |
|
|
In some cases you might not require a service, but want your service to be |
| 519 |
|
|
started <c>before</c> (or <c>after</c>) another service <e>if</e> it is |
| 520 |
|
|
available on the system (note the conditional - this is no dependency anymore) |
| 521 |
swift |
1.17 |
<e>and</e> run in the same runlevel (note the conditional - only services in the |
| 522 |
swift |
1.2 |
same runlevel are involved). You can provide this information using the |
| 523 |
|
|
<c>before</c> or <c>after</c> settings. |
| 524 |
|
|
</p> |
| 525 |
|
|
|
| 526 |
|
|
<p> |
| 527 |
|
|
As an example we view the settings of the Portmap service: |
| 528 |
|
|
</p> |
| 529 |
|
|
|
| 530 |
|
|
<pre caption="The depend() function in the Portmap service"> |
| 531 |
|
|
depend() { |
| 532 |
|
|
need net |
| 533 |
|
|
before inetd |
| 534 |
|
|
before xinetd |
| 535 |
|
|
} |
| 536 |
|
|
</pre> |
| 537 |
|
|
|
| 538 |
|
|
<p> |
| 539 |
|
|
You can also use the "*" glob to catch all services in the same runlevel, |
| 540 |
neysx |
1.16 |
although this isn't advisable. |
| 541 |
swift |
1.2 |
</p> |
| 542 |
|
|
|
| 543 |
|
|
<pre caption="Running an init script as first script in the runlevel"> |
| 544 |
|
|
depend() { |
| 545 |
|
|
before * |
| 546 |
|
|
} |
| 547 |
|
|
</pre> |
| 548 |
swift |
1.1 |
|
| 549 |
nightmorph |
1.26 |
<p> |
| 550 |
|
|
If your service must write to local disks, it should need <c>localmount</c>. If |
| 551 |
nightmorph |
1.28 |
it places anything in <path>/var/run</path> such as a pidfile, then it should |
| 552 |
nightmorph |
1.26 |
start after <c>bootmisc</c>: |
| 553 |
|
|
</p> |
| 554 |
|
|
|
| 555 |
|
|
<pre caption="Example depend() function"> |
| 556 |
|
|
depend() { |
| 557 |
|
|
need localmount |
| 558 |
|
|
after bootmisc |
| 559 |
|
|
} |
| 560 |
|
|
</pre> |
| 561 |
|
|
|
| 562 |
swift |
1.1 |
</body> |
| 563 |
|
|
</subsection> |
| 564 |
|
|
<subsection> |
| 565 |
|
|
<title>Standard Functions</title> |
| 566 |
|
|
<body> |
| 567 |
|
|
|
| 568 |
swift |
1.2 |
<p> |
| 569 |
|
|
Next to the <c>depend()</c> functionality, you also need to define the |
| 570 |
|
|
<c>start()</c> function. This one contains all the commands necessary to |
| 571 |
neysx |
1.16 |
initialize your service. It is advisable to use the <c>ebegin</c> and |
| 572 |
swift |
1.2 |
<c>eend</c> functions to inform the user about what is happening: |
| 573 |
|
|
</p> |
| 574 |
|
|
|
| 575 |
|
|
<pre caption="Example start() function"> |
| 576 |
|
|
start() { |
| 577 |
|
|
ebegin "Starting my_service" |
| 578 |
nightmorph |
1.26 |
start-stop-daemon --start --exec /path/to/my_service \ |
| 579 |
|
|
--pidfile /path/to/my_pidfile |
| 580 |
swift |
1.2 |
eend $? |
| 581 |
|
|
} |
| 582 |
|
|
</pre> |
| 583 |
|
|
|
| 584 |
|
|
<p> |
| 585 |
nightmorph |
1.26 |
Both <c>--exec</c> and <c>--pidfile</c> should be used in start and stop |
| 586 |
|
|
functions. If the service does not create a pidfile, then use |
| 587 |
|
|
<c>--make-pidfile</c> if possible, though you should test this to be sure. |
| 588 |
|
|
Otherwise, don't use pidfiles. You can also add <c>--quiet</c> to the |
| 589 |
|
|
<c>start-stop-daemon</c> options, but this is not recommended unless the |
| 590 |
|
|
service is extremely verbose. Using <c>--quiet</c> may hinder debugging if the |
| 591 |
|
|
service fails to start. |
| 592 |
swift |
1.2 |
</p> |
| 593 |
|
|
|
| 594 |
nightmorph |
1.26 |
<note> |
| 595 |
|
|
Make sure that <c>--exec</c> actually calls a service and not just a shell |
| 596 |
|
|
script that launches services and exits -- that's what the init script is |
| 597 |
|
|
supposed to do. |
| 598 |
|
|
</note> |
| 599 |
|
|
|
| 600 |
|
|
<p> |
| 601 |
|
|
If you need more examples of the <c>start()</c> function, please read the |
| 602 |
|
|
source code of the available init scripts in your <path>/etc/init.d</path> |
| 603 |
|
|
directory. |
| 604 |
|
|
</p> |
| 605 |
swift |
1.2 |
|
| 606 |
|
|
<p> |
| 607 |
|
|
Other functions you can define are: <c>stop()</c> and <c>restart()</c>. You are |
| 608 |
|
|
not obliged to define these functions! Our init system is intelligent enough to |
| 609 |
swift |
1.9 |
fill these functions by itself if you use <c>start-stop-daemon</c>. |
| 610 |
swift |
1.2 |
</p> |
| 611 |
|
|
|
| 612 |
swift |
1.21 |
<p> |
| 613 |
nightmorph |
1.26 |
Although you do not <e>have</e> to create a <c>stop()</c> function, here is an |
| 614 |
|
|
example: |
| 615 |
|
|
</p> |
| 616 |
|
|
|
| 617 |
|
|
<pre caption="Example stop() function"> |
| 618 |
|
|
stop() { |
| 619 |
|
|
ebegin "Stopping my_service" |
| 620 |
|
|
start-stop-daemon --stop --exec /path/to/my_service \ |
| 621 |
|
|
--pidfile /path/to/my_pidfile |
| 622 |
|
|
eend $? |
| 623 |
|
|
} |
| 624 |
|
|
</pre> |
| 625 |
|
|
|
| 626 |
|
|
<p> |
| 627 |
|
|
If your service runs some other script (for example, bash, python, or perl), |
| 628 |
|
|
and this script later changes names (for example, <c>foo.py</c> to <c>foo</c>), |
| 629 |
|
|
then you will need to add <c>--name</c> to <c>start-stop-daemon</c>. You must |
| 630 |
|
|
specify the name that your script will be changed to. In this example, a |
| 631 |
|
|
service starts <c>foo.py</c>, which changes names to <c>foo</c>: |
| 632 |
|
|
</p> |
| 633 |
|
|
|
| 634 |
|
|
<pre caption="A service that starts the foo script"> |
| 635 |
|
|
start() { |
| 636 |
|
|
ebegin "Starting my_script" |
| 637 |
|
|
start-stop-daemon --start --exec /path/to/my_script \ |
| 638 |
|
|
--pidfile /path/to/my_pidfile --name foo |
| 639 |
|
|
eend $? |
| 640 |
|
|
} |
| 641 |
|
|
</pre> |
| 642 |
|
|
|
| 643 |
|
|
<p> |
| 644 |
|
|
<c>start-stop-daemon</c> has an excellent man page available if you need more |
| 645 |
|
|
information: |
| 646 |
|
|
</p> |
| 647 |
|
|
|
| 648 |
|
|
<pre caption="Getting the man page for start-stop-daemon"> |
| 649 |
|
|
$ <i>man start-stop-daemon</i> |
| 650 |
|
|
</pre> |
| 651 |
|
|
|
| 652 |
|
|
<p> |
| 653 |
swift |
1.21 |
Gentoo's init script syntax is based on the Bourne Again Shell (bash) so you are |
| 654 |
|
|
free to use bash-compatible constructs inside your init script. |
| 655 |
|
|
</p> |
| 656 |
|
|
|
| 657 |
swift |
1.1 |
</body> |
| 658 |
|
|
</subsection> |
| 659 |
|
|
<subsection> |
| 660 |
|
|
<title>Adding Custom Options</title> |
| 661 |
|
|
<body> |
| 662 |
|
|
|
| 663 |
swift |
1.2 |
<p> |
| 664 |
|
|
If you want your init script to support more options than the ones we have |
| 665 |
|
|
already encountered, you should add the option to the <c>opts</c> variable, and |
| 666 |
|
|
create a function with the same name as the option. For instance, to support an |
| 667 |
|
|
option called <c>restartdelay</c>: |
| 668 |
|
|
</p> |
| 669 |
|
|
|
| 670 |
|
|
<pre caption="Supporting the restartdelay option"> |
| 671 |
|
|
opts="${opts} restartdelay" |
| 672 |
|
|
|
| 673 |
|
|
restartdelay() { |
| 674 |
swift |
1.24 |
stop |
| 675 |
swift |
1.2 |
sleep 3 <comment># Wait 3 seconds before starting again</comment> |
| 676 |
swift |
1.24 |
start |
| 677 |
swift |
1.2 |
} |
| 678 |
|
|
</pre> |
| 679 |
|
|
|
| 680 |
swift |
1.1 |
</body> |
| 681 |
|
|
</subsection> |
| 682 |
|
|
<subsection> |
| 683 |
|
|
<title>Service Configuration Variables</title> |
| 684 |
|
|
<body> |
| 685 |
swift |
1.2 |
|
| 686 |
|
|
<p> |
| 687 |
|
|
You don't have to do anything to support a configuration file in |
| 688 |
|
|
<path>/etc/conf.d</path>: if your init script is executed, the following files |
| 689 |
|
|
are automatically sourced (i.e. the variables are available to use): |
| 690 |
|
|
</p> |
| 691 |
|
|
|
| 692 |
|
|
<ul> |
| 693 |
|
|
<li><path>/etc/conf.d/<your init script></path></li> |
| 694 |
|
|
<li><path>/etc/conf.d/basic</path></li> |
| 695 |
|
|
<li><path>/etc/rc.conf</path></li> |
| 696 |
|
|
</ul> |
| 697 |
|
|
|
| 698 |
|
|
<p> |
| 699 |
|
|
Also, if your init script provides a virtual dependency (such as <c>net</c>), |
| 700 |
|
|
the file associated with that dependency (such as <path>/etc/conf.d/net</path>) |
| 701 |
|
|
will be sourced too. |
| 702 |
|
|
</p> |
| 703 |
swift |
1.1 |
|
| 704 |
|
|
</body> |
| 705 |
|
|
</subsection> |
| 706 |
|
|
</section> |
| 707 |
swift |
1.12 |
<section> |
| 708 |
|
|
<title>Changing the Runlevel Behaviour</title> |
| 709 |
|
|
<subsection> |
| 710 |
|
|
<title>Who might benefit from this?</title> |
| 711 |
|
|
<body> |
| 712 |
|
|
|
| 713 |
|
|
<p> |
| 714 |
|
|
Many laptop users know the situation: at home you need to start <c>net.eth0</c> |
| 715 |
|
|
while you don't want to start <c>net.eth0</c> while you're on the road (as |
| 716 |
|
|
there is no network available). With Gentoo you can alter the runlevel behaviour |
| 717 |
|
|
to your own will. |
| 718 |
|
|
</p> |
| 719 |
|
|
|
| 720 |
|
|
<p> |
| 721 |
|
|
For instance you can create a second "default" runlevel which you can boot that |
| 722 |
|
|
has other init scripts assigned to it. You can then select at boottime what |
| 723 |
dertobi123 |
1.15 |
default runlevel you want to use. |
| 724 |
swift |
1.12 |
</p> |
| 725 |
|
|
|
| 726 |
|
|
</body> |
| 727 |
|
|
</subsection> |
| 728 |
|
|
<subsection> |
| 729 |
neysx |
1.22 |
<title>Using softlevel</title> |
| 730 |
swift |
1.12 |
<body> |
| 731 |
|
|
|
| 732 |
|
|
<p> |
| 733 |
|
|
First of all, create the runlevel directory for your second "default" runlevel. |
| 734 |
|
|
As an example we create the <path>offline</path> runlevel: |
| 735 |
|
|
</p> |
| 736 |
|
|
|
| 737 |
|
|
<pre caption="Creating a runlevel directory"> |
| 738 |
|
|
# <i>mkdir /etc/runlevels/offline</i> |
| 739 |
|
|
</pre> |
| 740 |
|
|
|
| 741 |
|
|
<p> |
| 742 |
|
|
Add the necessary init scripts to the newly created runlevels. For instance, if |
| 743 |
|
|
you want to have an exact copy of your current <c>default</c> runlevel but |
| 744 |
|
|
without <c>net.eth0</c>: |
| 745 |
|
|
</p> |
| 746 |
|
|
|
| 747 |
|
|
<pre caption="Adding the necessary init scripts"> |
| 748 |
neysx |
1.22 |
<comment>(Copy all services from default runlevel to offline runlevel)</comment> |
| 749 |
neysx |
1.23 |
# <i>cd /etc/runlevels/default</i> |
| 750 |
|
|
# <i>for service in *; do rc-update add $service offline; done</i> |
| 751 |
neysx |
1.22 |
<comment>(Remove unwanted service from offline runlevel)</comment> |
| 752 |
|
|
# <i>rc-update del net.eth0 offline</i> |
| 753 |
|
|
<comment>(Display active services for offline runlevel)</comment> |
| 754 |
|
|
# <i>rc-update show offline</i> |
| 755 |
|
|
<comment>(Partial sample Output)</comment> |
| 756 |
|
|
acpid | offline |
| 757 |
|
|
domainname | offline |
| 758 |
|
|
local | offline |
| 759 |
|
|
net.eth0 | |
| 760 |
swift |
1.12 |
</pre> |
| 761 |
|
|
|
| 762 |
|
|
<p> |
| 763 |
nightmorph |
1.29 |
Even though <c>net.eth0</c> has been removed from the offline runlevel, |
| 764 |
|
|
<c>udev</c> will still attempt to start any devices it detects and launch the |
| 765 |
|
|
appropriate services. Therefore, you will need to add each network service you |
| 766 |
|
|
do not want started (as well as services for any other devices that may be |
| 767 |
|
|
started by udev) to <path>/etc/conf.d/rc</path> as shown. |
| 768 |
|
|
</p> |
| 769 |
|
|
|
| 770 |
|
|
<pre caption="Disabling device initiated services in /etc/conf.d/rc"> |
| 771 |
|
|
RC_COLDPLUG="yes" |
| 772 |
|
|
<comment>(Next, specify the services you do not want automatically started)</comment> |
| 773 |
|
|
RC_PLUG_SERVICES="!net.eth0" |
| 774 |
|
|
</pre> |
| 775 |
|
|
|
| 776 |
|
|
<note> |
| 777 |
|
|
For more information on device initiated services, please see the comments |
| 778 |
|
|
inside <path>/etc/conf.d/rc</path>. |
| 779 |
|
|
</note> |
| 780 |
|
|
|
| 781 |
|
|
<p> |
| 782 |
swift |
1.12 |
Now edit your bootloader configuration and add a new entry for the |
| 783 |
|
|
<c>offline</c> runlevel. For instance, in <path>/boot/grub/grub.conf</path>: |
| 784 |
|
|
</p> |
| 785 |
|
|
|
| 786 |
|
|
<pre caption="Adding an entry for the offline runlevel"> |
| 787 |
|
|
title Gentoo Linux Offline Usage |
| 788 |
|
|
root (hd0,0) |
| 789 |
|
|
kernel (hd0,0)/kernel-2.4.25 root=/dev/hda3 <i>softlevel=offline</i> |
| 790 |
|
|
</pre> |
| 791 |
|
|
|
| 792 |
|
|
<p> |
| 793 |
neysx |
1.22 |
VoilĂ , you're all set now. If you boot your system and select the newly added |
| 794 |
swift |
1.12 |
entry at boot, the <c>offline</c> runlevel will be used instead of the |
| 795 |
|
|
<c>default</c> one. |
| 796 |
|
|
</p> |
| 797 |
|
|
|
| 798 |
|
|
</body> |
| 799 |
|
|
</subsection> |
| 800 |
|
|
<subsection> |
| 801 |
neysx |
1.22 |
<title>Using bootlevel</title> |
| 802 |
swift |
1.12 |
<body> |
| 803 |
|
|
|
| 804 |
|
|
<p> |
| 805 |
|
|
Using <c>bootlevel</c> is completely analogous to <c>softlevel</c>. The only |
| 806 |
|
|
difference here is that you define a second "boot" runlevel instead of a second |
| 807 |
|
|
"default" runlevel. |
| 808 |
|
|
</p> |
| 809 |
|
|
|
| 810 |
|
|
</body> |
| 811 |
|
|
</subsection> |
| 812 |
|
|
</section> |
| 813 |
swift |
1.1 |
</sections> |