[ << ]
[ < ]
[ Home ]
[ > ]
[ >> ]
4. Initscripts
Content:
4.a. Runlevels
Booting your System
When you boot your system, you will notice lots of text floating by. If you pay
close attention, you will notice this text is the same every time you reboot
your system. The sequence of all these actions is called the boot
sequence and is (more or less) statically defined.
First, your boot loader will load the kernel image you have defined in the
boot loader configuration into memory after which it tells the CPU to run the
kernel. When the kernel is loaded and run, it initializes all kernel-specific
structures and tasks and starts the init process.
This process then makes sure that all filesystems (defined in
/etc/fstab) are mounted and ready to be used. Then it executes
several scripts located in /etc/init.d, which will start the
services you need in order to have a successfully booted system.
Finally, when all scripts are executed, init activates the terminals
(in most cases just the virtual consoles which are hidden beneath Alt-F1,
Alt-F2, etc.) attaching a special process called agetty to it.
This process will then make sure you are able to log on through these terminals
by running login.
Init Scripts
Now init doesn't just execute the scripts in /etc/init.d
randomly. Even more, it doesn't run all scripts in /etc/init.d,
only the scripts it is told to execute. It decides which scripts to execute by
looking into /etc/runlevels.
First, init runs all scripts from /etc/init.d that have
symbolic links inside /etc/runlevels/boot. Usually, it will
start the scripts in alphabetical order, but some scripts have dependency
information in them, telling the system that another script must be run before
they can be started.
When all /etc/runlevels/boot referenced scripts are executed,
init continues with running the scripts that have a symbolic link to them
in /etc/runlevels/default. Again, it will use the alphabetical
order to decide what script to run first, unless a script has dependency
information in it, in which case the order is changed to provide a valid
start-up sequence.
How Init Works
Of course init doesn't decide all that by itself. It needs a
configuration file that specifies what actions need to be taken. This
configuration file is /etc/inittab.
If you remember the boot sequence we have just described, you will remember
that init's first action is to mount all filesystems. This is defined in
the following line from /etc/inittab:
Code Listing 1.1: The system initialisation line in /etc/inittab |
si::sysinit:/sbin/rc sysinit
|
This line tells init that it must run /sbin/rc sysinit to
initialize the system. The /sbin/rc script takes care of the
initialisation, so you might say that init doesn't do much -- it
delegates the task of initialising the system to another process.
Second, init executed all scripts that had symbolic links in
/etc/runlevels/boot. This is defined in the following line:
Code Listing 1.2: The system initialisation, continued |
rc::bootwait:/sbin/rc boot
|
Again the rc script performs the necessary tasks. Note that the option
given to rc (boot) is the same as the subdirectory of
/etc/runlevels that is used.
Now init checks its configuration file to see what runlevel it
should run. To decide this, it reads the following line from
/etc/inittab:
Code Listing 1.3: The initdefault line |
id:3:initdefault:
|
In this case (which the majority of Gentoo users will use), the runlevel
id is 3. Using this information, init checks what it must run to start
runlevel 3:
Code Listing 1.4: The runlevel definitions |
l0:0:wait:/sbin/rc shutdown
l1:S1:wait:/sbin/rc single
l2:2:wait:/sbin/rc nonetwork
l3:3:wait:/sbin/rc default
l4:4:wait:/sbin/rc default
l5:5:wait:/sbin/rc default
l6:6:wait:/sbin/rc reboot
|
The line that defines level 3, again, uses the rc script to start the
services (now with argument default). Again note that the argument of
rc is the same as the subdirectory from /etc/runlevels.
When rc has finished, init decides what virtual consoles it should
activate and what commands need to be run at each console:
Code Listing 1.5: The virtual consoles definition |
c1:12345:respawn:/sbin/agetty 38400 tty1 linux
c2:12345:respawn:/sbin/agetty 38400 tty2 linux
c3:12345:respawn:/sbin/agetty 38400 tty3 linux
c4:12345:respawn:/sbin/agetty 38400 tty4 linux
c5:12345:respawn:/sbin/agetty 38400 tty5 linux
c6:12345:respawn:/sbin/agetty 38400 tty6 linux
|
What is a runlevel?
You have seen that init uses a numbering scheme to decide what
runlevel it should activate. A runlevel is a state in which
your system is running and contains a collection of scripts (runlevel scripts or
initscripts) that must be executed when you enter or leave a runlevel.
In Gentoo, there are seven runlevels defined: three internal runlevels, and four
user-defined runlevels. The internal runlevels are called sysinit,
shutdown and reboot and do exactly what their names imply:
initialize the system, powering off the system and rebooting the system.
The user-defined runlevels are those with an accompanying
/etc/runlevels subdirectory: boot,
default, nonetwork and single. The
boot runlevel starts all system-necessary services which all other
runlevels use. The remaining three runlevels differ in what services they start:
default is used for day-to-day operations, nonetwork
is used in case no network connectivity is required, and single is
used when you need to fix the system.
Working with the Init Scripts
The scripts that the rc process starts are called init scripts.
Each script in /etc/init.d can be executed with the arguments
start, stop, restart, pause, zap,
status, ineed, iuse, needsme, usesme or
broken.
To start, stop or restart a service (and all depending services), start,
stop and restart should be used:
Code Listing 1.6: Starting Postfix |
# /etc/init.d/postfix start
|
Note:
Only the services that need the given service are stopped or restarted.
The other depending services (those that use the service but don't need
it) are left untouched.
|
If you want to stop a service, but not the services that depend on it, you can
use the pause argument:
Code Listing 1.7: Stopping Postfix but keep the depending services running |
# /etc/init.d/postfix pause
|
If you want to see what status a service has (started, stopped, paused, ...) you
can use the status argument:
Code Listing 1.8: Status information for postfix |
# /etc/init.d/postfix status
|
If the status information tells you that the service is running, but you know
that it is not, then you can reset the status information to "stopped" with the
zap argument:
Code Listing 1.9: Resetting status information for postfix |
# /etc/init.d/postfix zap
|
To also ask what dependencies the service has, you can use iuse or
ineed. With ineed you can see the services that are really
necessary for the correct functioning of the service. iuse on the other
hand shows the services that can be used by the service, but are not necessary
for the correct functioning.
Code Listing 1.10: Requesting a list of all necessary services on which Postfix depends |
# /etc/init.d/postfix ineed
|
Similarly, you can ask what services require the service (needsme) or can
use it (usesme):
Code Listing 1.11: Requesting a list of all services that require Postfix |
# /etc/init.d/postfix needsme
|
Finally, you can ask what dependencies the service requires that are missing:
Code Listing 1.12: Requesting a list of missing dependencies for Postfix |
# /etc/init.d/postfix broken
|
4.b. Working with rc-update
What is rc-update?
Gentoo's init system uses a dependency-tree to decide what service needs to be
started first. As this is a tedious task that we wouldn't want our users to
have to do manually, we have created tools that ease the administration of the
runlevels and init scripts.
With rc-update you can add and remove init scripts to a runlevel. The
rc-update tool will then automatically ask the depscan.sh script
to rebuild the dependency tree.
Adding and Removing Services
You have already added init scripts to the "default" runlevel during the
installation of Gentoo. At that time you might not have had a clue what the
"default" is for, but now you should. The rc-update script requires a
second argument that defines the action: add, del or show.
To add or remove an init script, just give rc-update the add or
del argument, followed by the init script and the runlevel. For instance:
Code Listing 2.1: Removing Postfix from the default runlevel |
# rc-update del postfix default
|
The rc-update -v show command will show all the available init scripts and
list at which runlevels they will execute:
Code Listing 2.2: Receiving init script information |
# rc-update -v show
|
You can also run rc-update show (without -v) to just view enabled
init scripts and their runlevels.
4.c. Configuring Services
Why the Need for Extra Configuration?
Init scripts can be quite complex. It is therefore not really desirable to
have the users edit the init script directly, as it would make it more
error-prone. It is however important to be able to configure such a service. For
instance, you might want to give more options to the service itself.
A second reason to have this configuration outside the init script is to be
able to update the init scripts without the fear that your configuration
changes will be undone.
The /etc/conf.d Directory
Gentoo provides an easy way to configure such a service: every init script that
can be configured has a file in /etc/conf.d. For instance, the
apache2 initscript (called /etc/init.d/apache2) has a
configuration file called /etc/conf.d/apache2, which can contain
the options you want to give to the Apache 2 server when it is started:
Code Listing 3.1: Variable defined in /etc/conf.d/apache2 |
APACHE2_OPTS="-D PHP5"
|
Such a configuration file contains variables and variables alone (just like
/etc/portage/make.conf), making it very easy to configure services.
It also allows us to provide more information about the variables (as comments).
4.d. Writing Init Scripts
Do I Have To?
No, writing an init script is usually not necessary as Gentoo provides
ready-to-use init scripts for all provided services. However, you might have
installed a service without using Portage, in which case you will most likely
have to create an init script.
Do not use the init script provided by the service if it isn't explicitly
written for Gentoo: Gentoo's init scripts are not compatible with the init
scripts used by other distributions!
Layout
The basic layout of an init script is shown below.
Code Listing 4.1: Basic layout of an init script |
#!/sbin/runscript
depend() {
}
start() {
}
stop() {
}
|
Any init script requires the start() function to be defined. All
other sections are optional.
Dependencies
There are two dependency-alike settings you can define that influence the
start-up or sequencing of init scripts: use and need. Next to
these two, there are also two order-influencing methods called before and
after. These last two are no dependencies per se - they do not make the
original init script fail if the selected one isn't scheduled to start (or fails
to start).
-
The use settings informs the init system that this script uses
functionality offered by the selected script, but does not directly depend
on it. A good example would be use logger or use dns. If those
services are available, they will be put in good use, but if you do not have
a logger or DNS server the services will still work. If the services exist,
then they are started before the script that use's them.
-
The need setting is a hard dependency. It means that the script that
is need'ing another script will not start before the other script is
launched successfully. Also, if that other script is restarted, then this
one will be restarted as well.
-
When using before, then the given script is launched before the
selected one if the selected one is part of the init level. So an
init script xdm that defines before alsasound will start
before the alsasound script, but only if alsasound
is scheduled to start as well in the same init level. If
alsasound is not scheduled to start too, then this particular
setting has no effect and xdm will be started when the init
system deems it most appropriate.
-
Similarly, after informs the init system that the given script should
be launched after the selected one if the selected one is part of the
init level. If not, then the setting has no effect and the script will be
launched by the init system when it deems it most appropriate.
It should be clear from the above that need is the only "true" dependency
setting as it affects if the script will be started or not. All the others are
merely pointers towards the init system to clarify in which order scripts can be
(or should be) launched.
Now, if you look at many of Gentoo's available init scripts, you will notice
that some have dependencies on things that are no init scripts. These "things"
we call virtuals.
A virtual dependency is a dependency that a service provides, but that is
not provided solely by that service. Your init script can depend on a system
logger, but there are many system loggers available (metalogd, syslog-ng,
sysklogd, ...). As you cannot need every single one of them (no sensible
system has all these system loggers installed and running) we made sure that
all these services provide a virtual dependency.
Let us take a look at the dependency information for the postfix service.
Code Listing 4.2: Dependency information for Postfix |
depend() {
need net
use logger dns
provide mta
}
|
As you can see, the postfix service:
-
requires the (virtual) net dependency (which is provided by, for
instance, /etc/init.d/net.eth0)
-
uses the (virtual) logger dependency (which is provided by, for
instance, /etc/init.d/syslog-ng)
-
uses the (virtual) dns dependency (which is provided by, for
instance, /etc/init.d/named)
-
provides the (virtual) mta dependency (which is common for all mail
servers)
Controlling the Order
As we described in the previous section, you can tell the init system what order
it should use for starting (or stopping) scripts. This ordering is handled both
through the dependency settings use and need, but also through the
order settings before and after. As we have described these
earlier already, let's take a look at the Portmap service as an example of such
init script.
Code Listing 4.3: The depend() function in the Portmap service |
depend() {
need net
before inetd
before xinetd
}
|
You can also use the "*" glob to catch all services in the same runlevel,
although this isn't advisable.
Code Listing 4.4: Running an init script as first script in the runlevel |
depend() {
before *
}
|
If your service must write to local disks, it should need localmount. If
it places anything in /var/run such as a pidfile, then it should
start after bootmisc:
Code Listing 4.5: Example depend() function |
depend() {
need localmount
after bootmisc
}
|
Standard Functions
Next to the depend() functionality, you also need to define the
start() function. This one contains all the commands necessary to
initialize your service. It is advisable to use the ebegin and
eend functions to inform the user about what is happening:
Code Listing 4.6: Example start() function |
start() {
if [ "${RC_CMD}" = "restart" ];
then
fi
ebegin "Starting my_service"
start-stop-daemon --start --exec /path/to/my_service \
--pidfile /path/to/my_pidfile
eend $?
}
|
Both --exec and --pidfile should be used in start and stop
functions. If the service does not create a pidfile, then use
--make-pidfile if possible, though you should test this to be sure.
Otherwise, don't use pidfiles. You can also add --quiet to the
start-stop-daemon options, but this is not recommended unless the
service is extremely verbose. Using --quiet may hinder debugging if the
service fails to start.
Another notable setting used in the above example is to check the contents of
the RC_CMD variable. Unlike the previous init script system, the newer
openrc system does not support script-specific restart functionality.
Instead, the script needs to check the contents of the RC_CMD variable
to see if a function (be it start() or stop()) is called as part
of a restart or not.
Note:
Make sure that --exec actually calls a service and not just a shell
script that launches services and exits -- that's what the init script is
supposed to do.
|
If you need more examples of the start() function, please read the
source code of the available init scripts in your /etc/init.d
directory.
Another function you can define is stop(). You are not obliged to define
this function though! Our init system is intelligent enough to fill in this
function by itself if you use start-stop-daemon.
Here is an example of a stop() function:
Code Listing 4.7: Example stop() function |
stop() {
ebegin "Stopping my_service"
start-stop-daemon --stop --exec /path/to/my_service \
--pidfile /path/to/my_pidfile
eend $?
}
|
If your service runs some other script (for example, bash, python, or perl),
and this script later changes names (for example, foo.py to foo),
then you will need to add --name to start-stop-daemon. You must
specify the name that your script will be changed to. In this example, a
service starts foo.py, which changes names to foo:
Code Listing 4.8: A service that starts the foo script |
start() {
ebegin "Starting my_script"
start-stop-daemon --start --exec /path/to/my_script \
--pidfile /path/to/my_pidfile --name foo
eend $?
}
|
start-stop-daemon has an excellent man page available if you need more
information:
Code Listing 4.9: Getting the man page for start-stop-daemon |
$ man start-stop-daemon
|
Gentoo's init script syntax is based on the POSIX Shell so you are
free to use sh-compatible constructs inside your init script. Keep other
constructs, like bash-specific ones, out of the init scripts to ensure that the
scripts remain functional regardless of the change Gentoo might do on its init
system.
Adding Custom Options
If you want your init script to support more options than the ones we have
already encountered, you should add the option to the extra_commands
variable, and create a function with the same name as the option. For instance,
to support an option called restartdelay:
Code Listing 4.10: Supporting the restartdelay option |
extra_commands="restartdelay"
restartdelay() {
stop
sleep 3
start
}
|
Important:
The function restart() cannot be overridden in openrc!
|
Service Configuration Variables
You don't have to do anything to support a configuration file in
/etc/conf.d: if your init script is executed, the following files
are automatically sourced (i.e. the variables are available to use):
- /etc/conf.d/<your init script>
- /etc/conf.d/basic
- /etc/rc.conf
Also, if your init script provides a virtual dependency (such as net),
the file associated with that dependency (such as /etc/conf.d/net)
will be sourced too.
4.e. Changing the Runlevel Behaviour
Who might benefit from this?
Many laptop users know the situation: at home you need to start net.eth0
while you don't want to start net.eth0 while you're on the road (as
there is no network available). With Gentoo you can alter the runlevel behaviour
to your own will.
For instance you can create a second "default" runlevel which you can boot that
has other init scripts assigned to it. You can then select at boottime what
default runlevel you want to use.
Using softlevel
First of all, create the runlevel directory for your second "default" runlevel.
As an example we create the offline runlevel:
Code Listing 5.1: Creating a runlevel directory |
# mkdir /etc/runlevels/offline
|
Add the necessary init scripts to the newly created runlevels. For instance, if
you want to have an exact copy of your current default runlevel but
without net.eth0:
Code Listing 5.2: Adding the necessary init scripts |
# cd /etc/runlevels/default
# for service in *; do rc-update add $service offline; done
# rc-update del net.eth0 offline
# rc-update show offline
acpid | offline
domainname | offline
local | offline
net.eth0 |
|
Even though net.eth0 has been removed from the offline runlevel,
udev might want to attempt to start any devices it detects and launch the
appropriate services, a functionality that is called hotplugging. By
default, Gentoo does not enable hotplugging.
If you do want to enable hotplugging, but only for a selected set of scripts,
use the rc_hotplug variable in /etc/rc.conf:
Code Listing 5.3: Disabling device initiated services in /etc/rc.conf |
rc_hotplug="net.wlan !net.*"
|
Note:
For more information on device initiated services, please see the comments
inside /etc/rc.conf.
|
Now edit your bootloader configuration and add a new entry for the
offline runlevel. For instance, in /boot/grub/grub.conf:
Code Listing 5.4: Adding an entry for the offline runlevel |
title Gentoo Linux Offline Usage
root (hd0,0)
kernel (hd0,0)/kernel-2.4.25 root=/dev/hda3 softlevel=offline
|
Voilà, you're all set now. If you boot your system and select the newly added
entry at boot, the offline runlevel will be used instead of the
default one.
Using bootlevel
Using bootlevel is completely analogous to softlevel. The only
difference here is that you define a second "boot" runlevel instead of a second
"default" runlevel.
[ << ]
[ < ]
[ Home ]
[ > ]
[ >> ]
The contents of this document, unless otherwise expressly stated, are licensed under the CC-BY-SA-2.5 license. The Gentoo Name and Logo Usage Guidelines apply.
|