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.
Imediatly 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 intialize 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:
0
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.
1
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, 2
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.
4
Not used. You can define it as you want, but is uncommon.
5
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.
6
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.
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/rcN.d directories:
/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:
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 thru 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 obrigatória) to be automatically started on system initialization. You just have to create your /etc/init.d script following a standard to make it functional.
Besides being 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 /etc/sysconfig directory, and in our case we call it mySystem. This code line reads this configuration file.
Your script can have many functions, but it is obrigatórios 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:
bash# service mysystem start
Starting MySystem: [ OK ]
bash# service mysystem status
Subsysten MySystem is active with pid 1234
bash# 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/rcN.d. The chkconfig command makes it for you, based on the control comments defined in the begining of your script.
When you'll create the RPM, put your Subsystem script in /etc/init.d and do not include any /etc/rc.d/rcN.d 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.
The symbolic links must be created and removed dynamically by the post-instalation and pre-uninstallation process of your package, using the chkconfig command. This aproach guaratees 100% package and filesystem consistency.