Monday, December 22, 2008
A look at System V initialization
welcome to infomix.blogspot.com
In the traditional BSD style of startup, there are only a few initialization scripts, which are something like the autoexec.bat and config.sys pair of DOS. Commands are executed and services are loaded up sequentially as given in the scripts.
But the system administrator may want a more flexible and convenient way of managing the system. There will be times when certain services are not required. The BSD style of startup as adopted by distributions like Slackware offered only a few system states at the disposal of the administrator. A single user state was provided for maintenance and there was a normal multi-user state.
System V Unix extended this by using more runlevels and also by separating each service into its own unique startup and shutdown script. Each script could be linked to one or more than one runlevel. It was now quite easy for the administrator to create a custom configuration. This system of runlevels and unique service scripts has been adopted by most Linux distributions and can be seen in Red Hat, SuSE, Mandrake and Debian among others. There's quite a bit of flexibility here. One runlevel can boot you into the console while another will boot into the GUI. You can also choose to start without the network having been initialized. The separation of each service also meant that it was now very easy to start, shutdown or restart any given service. To restart the web server, all you would need to do now is say "/etc/rc.d/init.d/httpd restart". The httpd script will shutdown the service and bring it back up.
There are 7 runlevels that have been defined. Of these 7 runlevels 0,1, 6 and S are reserved runlevels.
0 - Halt
1 - Single user mode for maintenance
6 - Reboot
S - Single user mode but only the root partition is mounted. Use this one for filesystem check and repair.
The single user mode is handled differently by the various distributions. Under Red Hat, runlevel "S" and "1" are the same. Runlevel S under System V was for filesystem checks and repairs where only the root filesystem was mounted. No services are running. Runlevel 1 is single user but is used for system administration tasks like adding/removing packages or configuration changes. Some basic services are running but networking is disabled. SuSE works this way and makes the distinction between the two runlevels.
(Thanks to Karl Schmitz for some good information regarding the above)
2,3,4 and 5 can be customized to your liking. Red Hat, for example, uses runlevel 5 when starting the GUI after boot. Runlevel 2 is used to startup in multi-user without NFS. SuSE on the other hand, uses runlevel 2 for multi-user with network and runlevel 3 for the multi-user GUI startup. There's nothing stopping you from changing this setup to a custom configuration of your liking.
/etc/rc.d
The startup scripts and files are placed in the directory /etc/rc.d. Some distributions do divert and do things differently here. SuSE used /sbin/init.d instead. We'll first look at Red Hat and then look at the SuSE structure in this article.
Red Hat uses /etc/rc.d. Given below is the result of an "ls -p" in this directory.
init.d/ rc.local rc0.d/ rc2.d/ rc4.d/ rc6.d/
rc rc.sysinit rc1.d/ rc3.d/ rc5.d/
init.d is a very important directory as it holds the scripts for the various services. The scripts, which are simple shell scripts, generally offer some very useful functions that are given as command line arguments.
/etc/rc.d/init.d/httpd start
Common arguments that can be supplied are
start - Start the service
stop - Stop the service
restart - Stop the service and restart it.
status - Display information regarding the status of the service, whether it is running or not.
Now, enter any runlevel directory and do a "ls -l" there. You will see something like the extract below.
lrwxrwxrwx 1 root root 14 Jan 8 17:14 K80nscd -> ../init.d/nscd
lrwxrwxrwx 1 root root 16 Jan 8 17:14 K83ypbind -> ../init.d/ypbind
lrwxrwxrwx 1 root root 17 Jan 8 17:14 S10network -> ../init.d/network
lrwxrwxrwx 1 root root 17 Jan 8 17:14 S11portmap -> ../init.d/portmap
lrwxrwxrwx 1 root root 16 Jan 8 17:14 S12syslog -> ../init.d/syslog
You can now see that the contents of the runlevel directories are merely symlinks that point to the scripts in the init.d directory. You should also take note of the naming scheme of these symlinks.
Either an "S" or a "K" prefixes the link name followed by a two-digit number. The "S" means that the service is to be started when the system enters that runlevel whereas conversely "K" would mean that the service is to be stopped when the system changes from that runlevel. The two-digit number is also of significance. The number decides at what point in the boot process the service is to be started or killed. The services are started or killed in ascending order of the number provided.
Now, say you want httpd service started in the runlevel 3, you need to create a link to it from /etc/rc.d/init.d/httpd to /etc/rc.d/rc3.d. You can do this by created a symlink.
ln -s /etc/rc.d/init.d/httpd /etc/rc.d/rc3.d/S25httpd
Similarly create the symlink for killing the service here. But this is not a very easy way of managing your services. Red Hat provides a better tool that you can use to setup the rcx.d links. Under Red Hat you can use 'ntsysv' or 'chkconfig'. The tool 'ntsysv' brings up a menu where you can select or deselect a service to stop it from starting up. The command-line tool chkconfig can be used if more control is desired. This can be found in /sbin.
chkconfig --add service will add the service specified into the default runlevels. Other options are also available here.
--list [service] - Will list all the services and the runlevels they are active on. If a particular service is specified then the pertinent details for only that service are displayed.
chkconfig nfs off - Will remove the service from the runlevels that it is run in. A 'on' here will restore it to the runlevels.
chkconfig --level 35 ypserv on
The level switch is a very useful one in case you want to specify which runlevels you want to start the service from or turn it off if so required. Ypserv is now active on runlevels 3 and 5.
While adding services to runlevels, make sure that you starting them at the correct point. You should not start up Sendmail before your network has been initialized. NFS and YP require the services of Portmap so they should be assigned a number higher than Portmap is.
The rc.sysinit, rc and rc.local files are also of interest. The file rc.sysinit is the first to be executed. The filesystem checks and other important pre-initialization tasks are executed from this file. This then transfers control to the rc script. The rc script comes into play when changing runlevels and for basic initialization after which the scripts that are linked to the startup runlevel are executed. The file rc.local is executed at the very end of the boot process, after all the services have been started. You can put any post-boot jobs in here. This is a good place for your hdparm settings so that they're set at every boot.
SuSE has a slightly different setup. SuSE places the service scripts in the directory /sbin/init.d and the various runlevel directories are created under this directory. Also, instead of rc.sysinit, the boot process starts from the script /sbin/init.d/boot. Other than these differences, the basic structure is the same.
Under SuSE to modify script runlevels, the program used is rctab. Use "rctab -l" to see the services and the runlevels they are active on. Use "rctab -e" to edit the runlevels. This will bring up the vi editor and a table of your list of services and the runlevels they are active in. To remove a service from a runlevel, delete the name and put in a "-" there instead. To add a service do the opposite but make sure that the service is started at the correct point.
Switching Runlevels
There will be times when you might want to boot into a different runlevel. There are several ways in which you can do this. You can do this at boot or off the command-line.
First, let's look at /etc/inittab, the configuration file read by init. Open it in any text editor and look for a line like the one below. It should be somewhere near the top of the file if not at the very top.
id:3:initdefault
This line defines the default runlevel that Linux should boot into. Here, the runlevel is set to 3, which is the console mode with network in Red Hat. If you want to start up in the GUI, you should set this to say 5. Save the file and reboot. The next time you boot, you will find yourself at the GUI.
You can also do the same at the LILO prompt. Sometimes, when the GUI is not working properly, you would want to boot into the console and make the changes before starting X. Enter the following at the LILO prompt. Or, check the list of runlevels in /etc/inittab and use the runlevel for the console.
LILO: linux 3
The format is, image name followed by the runlevel to boot into. To boot into single user mode you could just say "linux single".
At the command line, you can use the telinit to change runlevels. You will need to be root to use this. Let's say you are in runlevel 3 and want to switch to the GUI on runlevel 5, use the following command to switch.
telinit 5
This was a brief look at runlevels and the System V style of booting that most Linux distributions use these days. This article was aimed at the newbies and we certainly hope we cleared up a few doubts. As always, feel free to send us a mail in case of any problems.
0 Responses to "A look at System V initialization"
Post a Comment