File:  [NetBSD Developer Wiki] / wikisrc / guide / rc.mdwn
Revision 1.1: download - view: text, annotated - select for diffs
Fri Mar 1 16:47:02 2013 UTC (5 years, 7 months ago) by jdf
Branches: MAIN
CVS tags: HEAD
Added the rc chapter from the Guide. Added a section about rc.d scripts
from additional services.

# The rc.d System

NetBSD uses individual scripts for controlling system and service startup, 
similar to System V, but without runlevels.
Nowadays, most Linux distributions switched to systemd, which is a different 
approach: It does not use scripts anymore, but tries to have everything binary. 
It also serves for several other purposes, while NetBSD's rc.d system just 
serves for starting up services at system startup or by user intervention.

This chapter is an overview of NetBSD's rc.d system and its configuration.

## Basics

The system startup files reside in the `/etc` directory. They are:

 * `/etc/rc`
 * `/etc/rc.conf`
 * `/etc/rc.d/*`
 * `/etc/rc.lkm`
 * `/etc/rc.local`
 * `/etc/rc.shutdown`
 * `/etc/rc.subr`
 * `/etc/defaults/*`
 * `/etc/rc.conf.d/*`

First, a look at controlling and supporting scripts (also documented in 

 * After the kernel has initialized all devices at startup, it starts 
   which in turn runs `/etc/rc`.
 * `/etc/rc` sorts the scripts in `/etc/rc.d` using 
   and then runs them in that order. See below and the manpage for details of
   how the order of rc.d scripts is determined.
 * `/etc/rc.subr` contains common functions used by `/etc/rc` and various rc.d 
 * When shutting down the system with 
   `/etc/rc.shutdown` is run, which runs the scripts in `/etc/rc.d` in
   reverse order (as defined by 
   *Note*: If you shut down the system using the 
   command, these scripts will not be run.

There are some special scripts outside the `rc.d` directory, which are also

 * `/etc/rc.lkm` loads or unloads Loadable Kernel Modules (LKMs). See 
   and `/etc/rc.d/lkm[123]`.
 * `/etc/rc.local` is almost the last script called at boot up. This script can 
   be edited by the administrator to start local daemons that don't fit the
   rc.d model, or do maintenance that should be done only once at startup.

rc.d scripts are controlled by a central configuration file, `/etc/rc.conf`, 
which loads its default settings from `/etc/defaults/rc.conf`. If you want to 
change a default setting, do not edit `/etc/defaults/rc.conf`; instead, override 
the setting in `/etc/rc.conf`.

It is a good idea to read the 
man page to learn about the services that are by default available to you.

The following example shows how to enable the SSH daemon, which is disabled by 

    # cd /etc; grep ssh defaults/rc.conf
    sshd=NO                 sshd_flags=""
    # echo "sshd=YES" >> rc.conf

Now [sshd(8)]( 
will be started automatically at system startup. The next section describes how 
to start and stop services at any time.

Last but not least, files can be created in the `/etc/rc.conf.d/` directory to 
override the behavior of a given rc.d script without editing the script itself.

## The rc.d scripts

The actual scripts that control services are in `/etc/rc.d`. These scripts are 
automatically run at boot time, but they can be called manually if necessary. 
The following example shows how to start the SSH daemon that we enabled in the 
previous section:

    # /etc/rc.d/sshd start
    Starting sshd.

Later, if you wish to stop the SSH daemon, run the following command:

    # /etc/rc.d/sshd stop
    Stopping sshd.
    Waiting for PIDS: 123.

The rc.d scripts take one of the following arguments:

 * `start`
 * `stop`
 * `restart`
 * `status`

Some scripts may support other arguments (e.g., `reload`), but every script will 
support at least the above commands.

As an example, after adding a new record to a 
database, the daemon can be told to reload its configuration files with the 
following command:

    # /etc/rc.d/named reload
    Reloading named config files.

Note that all of the commands discussed above will only take action if the 
particular service is enabled in `/etc/rc.conf`. It is possible to bypass this 
requirement by prepending `one` to the command, as in:

    # /etc/rc.d/httpd start
    $httpd is not enabled - see rc.conf(5).
    Use the following if you wish to perform the operation:
      /etc/rc.d/httpd onestart
    # /etc/rc.d/httpd onestart
    Starting httpd.

The above command will allow you to start the 
service one time. To stop a service that has been started in this manner, pass 
`onestop` to the script.

## Order/dependencies of start determined by rcorder

The startup system of every Unix system determines, in one way or another, the 
order in which services are started. On some Unix systems this is done by 
numbering the files and/or putting them in separate run level directories. 
Solaris relies on wildcards like `/etc/rc[23].d/S*` being sorted numerically 
when expanded. Some simply put all the commands that should be started into a 
single monolithic script (this is the traditional BSD method, and is what NetBSD 
did before the rc.d system). On modern NetBSD this is done by the rc.d scripts 
and their contents. Please note that NetBSD does not have multiple runlevels as 
found in SysV-style systems like Solaris and Linux.

At the beginning of each rc.d script there is a series of commented out lines 
that have one of the following items in them:


These describe the dependencies of that particular script and allow rcorder to 
easily work either `up` or `down` as the situation requires. As an example, here 
is the ordering information contained in `/etc/rc.d/nfsd`:

     PROVIDE: nfsd
     REQUIRE: rpcbind mountd

Here we can see that this script provides the `nfsd` service and that it 
requires `rpcbind` and `mountd` to be running first. The 
utility is used at system startup time to read through all the rc.d scripts and 
determine the order in which they should be run.

## rc.d scripts of additional services

Packages you install additionally won't be listed in the rc.conf(5) manpage.
Packages installing services which can be started with an rc.d script tell you
so after they are installed, along with the variable that is used for starting
them (usually, it has the same name as the service itself).

Then, you usually have to copy them from `/usr/pkg/share/examples/rc.d` to
`/etc/rc.d` for `rc` to automatically find them, or add the line

    rc_directories="/etc/rc.d /usr/pkg/share/examples/rc.d"

to your `/etc/rc.conf`.

If you forgot the name of the service variable, you can have a look at the rc.d
script itself. The variable `rcvar` (usually set to `$name`) will tell you.
E.g., to find the name of variable to start fscd, run

    $ grep ^rcvar /etc/rc.d/fscd
	$ grep ^name /etc/rc.d/fscd   # Aaaw, no direct rcvar.

Thus, you need in your `/etc/rc.conf` the entry:


## Additional Reading

Luke Mewburn, one of the principal designers of the rc.d system, gave a 
presentation on the system at USENIX 2001. It is available in 
[PDF]( format.

CVSweb for NetBSD wikisrc <> software: FreeBSD-CVSweb