The way Linux starts (and stops) all its subsystems is very simple and modular. Lets you define initialization order, runlevels etc
Lets review what happens when we boot Linux:
The BIOS or a bootloader (lilo, zlilo, grub, etc) loads Linux Kernel from disk to memory, with some parameters defined in the bootloader configuration. We can see this process watching the dots that appear in the screen. Kernel file stays in the /boot
directory, and is accessed only at this moment.
In memory, Kernel code starts to run, detecting a series of vital devices, disk partitions etc.
On of the last things Kernel does is to mount the /
(root) filesystem, that obrigatoriamente must contain the /etc
, /sbin
, /bin
and /lib
directories.
Immediately behind, calls the program called init (/sbin/init
) and passes the control to him.
The init command will read his configuration file (/etc/inittab
) which defines the system runlevel, and some Shell scripts to be run.
These scripts will continue the setup of system's minimal infrastructure, mounting other filesystems (according to /etc/fstab
), activating swap space (virtual memory), etc.
The last step, and most interesting for you, is the execution of the special script called /etc/rc.d/rc
, which initializes the subsystems according to a directory structure under /etc/rc.d
. The name rc comes from run commands.
The runlevels mechanism lets Linux initialize itself in different ways. And also lets us change from one profile (runlevel) to another without rebooting.
The default runlevel is defined in /etc/inittab
with a line like this:
Runlevels are numbers from 0 to 6 and each one of them is used following this standard:
Halts the system. Turning to this runlevel, all subsystems are softly deactivated before the shutdown. Don't use it in the initdefault line of /etc/inittab
.
Mono-user mode. Only vital subsystems are initialized because it is used for system maintenance. No user authentication (login) is required in this runlevel. A command line is directly returned to the user.
3 is used when a system is in full production. Take it as the runlevel your software will run. 2 is historical and is like 3, but without NFS.
Not used. You can define it as you want, but is uncommon.
Like 3 plus a graphical login. It is ideal for a desktop workstation. Use 3 if the machine will be used as a server, for security and performance reasons.
Like runlevel 0, but after complete stop, the machine is rebooted. Don't use it in the initdefault line of /etc/inittab
.
You can switch from one runlevel to another using the telinit command. And you can see the current runlevel and the last one with the runlevel command. See bellow how we switched from runlevel 3 to 5.
bash#
runlevel N 3bash#
telinit 5bash#
runlevel 3 5bash#
Subsystems examples are a web-server, data base server, OS network layer etc. We'll not consider a user oriented application (like a text editor) as a subsystem.
Linux provides an elegant and modular way to organize the subsystems initialization. An important fact to think is about subsystems interdependencies. For instance, it makes no sense to start a web-server before basic networking subsystem is active.
Subsystems are organized under the /etc/init.d
and /etc/rc.d/rc
directories: N
.d
/etc/init.d
All installed Subsystems put in this directory a control program, which is a script that follows a simple standard described bellow. This is a simplified listing of this directory:
Example 4. Subsystems installed in /etc/init.d
bash:/etc/init.d#
ls -l
-rwxr-xr-x 1 root root 9284 Aug 13 2001 functions
-rwxr-xr-x 1 root root 4984 Sep 5 00:18 halt
-rwxr-xr-x 1 root root 5528 Nov 5 09:44 firewall
-rwxr-xr-x 1 root root 1277 Sep 5 21:09 keytable
-rwxr-xr-x 1 root root 487 Jan 30 2001 killall
-rwxr-xr-x 1 root root 7958 Aug 15 17:20 network
-rwxr-xr-x 1 root root 1490 Sep 5 07:54 ntpd
-rwxr-xr-x 1 root root 2295 Jan 30 2001 rawdevices
-rwxr-xr-x 1 root root 1830 Aug 31 09:29 httpd
-rwxr-xr-x 1 root root 1311 Aug 15 14:18 syslog
/etc/rc.d/rcN
.d
(N
is the runlevel indicator)These directories must contain only special symbolic links to the scripts in /etc/init.d
. This is how it looks:
Example 5. /etc/rc3.d
listing
bash:/etc/rc3.d#
ls -l
lrwxrwxrwx 1 root root 18 Jan 14 11:59 K92firewall -> ../init.d/firewall
lrwxrwxrwx 1 root root 17 Jan 14 11:59 S10network -> ../init.d/network
lrwxrwxrwx 1 root root 16 Jan 14 11:59 S12syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 18 Jan 14 11:59 S17keytable -> ../init.d/keytable
lrwxrwxrwx 1 root root 20 Jan 14 11:59 S56rawdevices -> ../init.d/rawdevices
lrwxrwxrwx 1 root root 16 Jan 14 11:59 S56xinetd -> ../init.d/xinetd
lrwxrwxrwx 1 root root 18 Jan 14 11:59 S75httpd -> ../init.d/httpd
lrwxrwxrwx 1 root root 11 Jan 13 21:45 S99local -> ../rc.local
Pay attention that all link names has a prefix starting with letter K (from Kill, to deactivate) or S (from Start, to activate), and a 2 digit number that defines the boot activation priority. In our example we have HTTPd (priority 75) starting after the Network (priority 10) subsystem. And the Firewalling subsystem will be deactivated (K) in this runlevel.
So to make your Software start automatically in the boot process, it must be a subsystem, and we'll see how to do it in the following section.
Your Software's files will spread across the filesystems, but you'll want to provide a simple and consistent interface to let the user at least start and stop it. Subsystems architecture promotes this ease-of-use, also providing a way (non obrigatoria) to be automatically started on system initialization. You just have to create your /etc/init.d
script following a standard to make it functional.
Example 6. Skeleton of a Subsystem control program in /etc/init.d
#!/bin/sh # # /etc/init.d/mysystem # Subsystem file for "MySystem" server # # chkconfig: 2345 95 05 # description: MySystem server daemon # # processname: MySystem # config: /etc/MySystem/mySystem.conf # config: /etc/sysconfig/mySystem # pidfile: /var/run/MySystem.pid # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/mySystem ] && . /etc/sysconfig/mySystem RETVAL=0 prog="MySystem" . . . start() { echo -n $"Starting $prog:" . . . RETVAL=$? [ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog echo } stop() { echo -n $"Stopping $prog:" . . . killproc $prog -TERM RETVAL=$? [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog echo } reload() { echo -n $"Reloading $prog:" killproc $prog -HUP RETVAL=$? echo } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; reload) reload ;; condrestart) if [ -f /var/lock/subsys/$prog ] ; then stop # avoid race sleep 3 start fi ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}" RETVAL=1 esac exit $RETVAL
Although these are comments, they are used by chkconfig command and must be present. This particular line defines that on runlevels 2,3,4 and 5, this subsystem will be activated with priority 95 (one of the lasts), and deactivated with priority 05 (one of the firsts). | |
Besides your Software's own configuration, this script can also have a configuration file. The standard place for it is under | |
Your script can have many functions, but it is obrigatorios the implementation of start and stop methods, because they are responsible for (de)activation of your Subsystem on boot. Other methods can be called from the command line, and you can define as much as you want. | |
After defining the script actions, the command line is analyzed and the requested method (action) is called. | |
If this script is executed without any parameter, it will return a help message like this:
| |
Here you put your Software's specific command. |
The mysystem subsystem methods you implemented will be called by users with the service command like this example:
Example 7. service command usage
bash#
service mysystem start Starting MySystem: [ OK ]bash#
service mysystem status Subsysten MySystem is active with pid 1234bash#
service mysystem reload Reloading MySystem: [ OK ]bash#
service mysystem stop Stopping MySystem: [ OK ]bash#
You don't have to worry about managing the symbolic links in /etc/rc.d/rc
. The chkconfig command makes it for you, based on the control comments defined in the beginning of your script.N
.d
Example 8. Using the chkconfig command
bash#
chkconfig --add mysystembash#
chkconfig --del mysystem
Read the chkconfig manual page to see what more it can do for you.
When you'll create the RPM, put your Subsystem script in /etc/init.d
and do not include any /etc/rc.d/rc
link, because it is a user decision to make your subsystem automatic or not. If you include them and the user makes any change, the RPM file inventory will become inconsistent.N
.d
The symbolic links must be created and removed dynamically by the post-installation and pre-uninstallation process of your package, using the chkconfig command. This approach guarantees 100% package and filesystem consistency.