Annotation of wikisrc/guide/rc.mdwn, revision 1.3

1.1       jdf         1: # The rc.d System
                      2: 
1.3     ! jdf         3: NetBSD uses individual scripts for controlling system and service startup,
1.1       jdf         4: similar to System V, but without runlevels.
1.3     ! jdf         5: Nowadays, most Linux distributions switched to systemd, which is a different
        !             6: approach: It does not use scripts anymore, but tries to have everything binary.
        !             7: It also serves for several other purposes, while NetBSD's rc.d system just
1.1       jdf         8: serves for starting up services at system startup or by user intervention.
                      9: 
                     10: This chapter is an overview of NetBSD's rc.d system and its configuration.
                     11: 
                     12: ## Basics
                     13: 
                     14: The system startup files reside in the `/etc` directory. They are:
                     15: 
                     16:  * `/etc/rc`
                     17:  * `/etc/rc.conf`
                     18:  * `/etc/rc.d/*`
                     19:  * `/etc/rc.lkm`
                     20:  * `/etc/rc.local`
                     21:  * `/etc/rc.shutdown`
                     22:  * `/etc/rc.subr`
                     23:  * `/etc/defaults/*`
                     24:  * `/etc/rc.conf.d/*`
                     25: 
1.3     ! jdf        26: First, a look at controlling and supporting scripts (also documented in
1.1       jdf        27: [rc(8)](http://netbsd.gw.com/cgi-bin/man-cgi?rc+8+NetBSD-5.0.1+i386)):
                     28: 
1.3     ! jdf        29:  * After the kernel has initialized all devices at startup, it starts
1.1       jdf        30:    [init(8)](http://netbsd.gw.com/cgi-bin/man-cgi?init+8+NetBSD-5.0.1+i386),
                     31:    which in turn runs `/etc/rc`.
1.3     ! jdf        32: 
        !            33:  * `/etc/rc` sorts the scripts in `/etc/rc.d` using
        !            34:    [rcorder(8)](http://netbsd.gw.com/cgi-bin/man-cgi?rcorder+8+NetBSD-5.0.1+i386)
1.1       jdf        35:    and then runs them in that order. See below and the manpage for details of
                     36:    how the order of rc.d scripts is determined.
1.3     ! jdf        37: 
        !            38:  * `/etc/rc.subr` contains common functions used by `/etc/rc` and various rc.d
1.1       jdf        39:    scripts.
1.3     ! jdf        40: 
        !            41:  * When shutting down the system with
        !            42:    [shutdown(8)](http://netbsd.gw.com/cgi-bin/man-cgi?shutdown+8+NetBSD-5.0.1+i386),
1.1       jdf        43:    `/etc/rc.shutdown` is run, which runs the scripts in `/etc/rc.d` in
1.3     ! jdf        44:    reverse order (as defined by
        !            45:    [rcorder(8)](http://netbsd.gw.com/cgi-bin/man-cgi?rcorder+8+NetBSD-5.0.1+i386)).
        !            46: 
        !            47:    *Note*: If you shut down the system using the
        !            48:    [halt(8)](http://netbsd.gw.com/cgi-bin/man-cgi?halt+8+NetBSD-5.0.1+i386)
1.1       jdf        49:    command, these scripts will not be run.
                     50: 
                     51: There are some special scripts outside the `rc.d` directory, which are also
                     52: run:
                     53: 
1.3     ! jdf        54:  * `/etc/rc.lkm` loads or unloads Loadable Kernel Modules (LKMs). See
        !            55:    [modload(8)](http://netbsd.gw.com/cgi-bin/man-cgi?modload+8+NetBSD-5.0.1+i386)
1.1       jdf        56:    and `/etc/rc.d/lkm[123]`.
1.3     ! jdf        57: 
        !            58:  * `/etc/rc.local` is almost the last script called at boot up. This script can
1.1       jdf        59:    be edited by the administrator to start local daemons that don't fit the
                     60:    rc.d model, or do maintenance that should be done only once at startup.
                     61: 
1.3     ! jdf        62: rc.d scripts are controlled by a central configuration file, `/etc/rc.conf`,
        !            63: which loads its default settings from `/etc/defaults/rc.conf`. If you want to
        !            64: change a default setting, do not edit `/etc/defaults/rc.conf`; instead, override
1.1       jdf        65: the setting in `/etc/rc.conf`.
                     66: 
1.3     ! jdf        67: It is a good idea to read the
        !            68: [rc.conf(5)](http://netbsd.gw.com/cgi-bin/man-cgi?rc.conf+5+NetBSD-5.0.1+i386)
1.1       jdf        69: man page to learn about the services that are by default available to you.
                     70: 
1.3     ! jdf        71: The following example shows how to enable the SSH daemon, which is disabled by
1.1       jdf        72: default:
                     73: 
                     74:     # cd /etc; grep ssh defaults/rc.conf
                     75:     sshd=NO                 sshd_flags=""
                     76:     # echo "sshd=YES" >> rc.conf
                     77: 
1.3     ! jdf        78: Now [sshd(8)](http://netbsd.gw.com/cgi-bin/man-cgi?sshd+8+NetBSD-5.0.1+i386)
        !            79: will be started automatically at system startup. The next section describes how
1.1       jdf        80: to start and stop services at any time.
                     81: 
1.3     ! jdf        82: Last but not least, files can be created in the `/etc/rc.conf.d/` directory to
1.1       jdf        83: override the behavior of a given rc.d script without editing the script itself.
                     84: 
                     85: ## The rc.d scripts
                     86: 
1.3     ! jdf        87: The actual scripts that control services are in `/etc/rc.d`. These scripts are
        !            88: automatically run at boot time, but they can be called manually if necessary.
        !            89: The following example shows how to start the SSH daemon that we enabled in the
1.1       jdf        90: previous section:
                     91: 
                     92:     # /etc/rc.d/sshd start
                     93:     Starting sshd.
                     94: 
                     95: Later, if you wish to stop the SSH daemon, run the following command:
                     96: 
                     97:     # /etc/rc.d/sshd stop
                     98:     Stopping sshd.
                     99:     Waiting for PIDS: 123.
                    100: 
                    101: The rc.d scripts take one of the following arguments:
                    102: 
                    103:  * `start`
                    104:  * `stop`
                    105:  * `restart`
                    106:  * `status`
                    107: 
1.3     ! jdf       108: Some scripts may support other arguments (e.g., `reload`), but every script will
1.1       jdf       109: support at least the above commands.
                    110: 
1.3     ! jdf       111: As an example, after adding a new record to a
        !           112: [named(8)](http://netbsd.gw.com/cgi-bin/man-cgi?named+8+NetBSD-5.0.1+i386)
        !           113: database, the daemon can be told to reload its configuration files with the
1.1       jdf       114: following command:
                    115: 
                    116:     # /etc/rc.d/named reload
                    117:     Reloading named config files.
                    118: 
1.3     ! jdf       119: Note that all of the commands discussed above will only take action if the
        !           120: particular service is enabled in `/etc/rc.conf`. It is possible to bypass this
1.1       jdf       121: requirement by prepending `one` to the command, as in:
                    122: 
                    123:     # /etc/rc.d/httpd start
                    124:     $httpd is not enabled - see rc.conf(5).
                    125:     Use the following if you wish to perform the operation:
                    126:       /etc/rc.d/httpd onestart
                    127:     # /etc/rc.d/httpd onestart
                    128:     Starting httpd.
                    129: 
1.3     ! jdf       130: The above command will allow you to start the
        !           131: [httpd(8)](http://netbsd.gw.com/cgi-bin/man-cgi?httpd+8+NetBSD-5.0.1+i386)
        !           132: service one time. To stop a service that has been started in this manner, pass
1.1       jdf       133: `onestop` to the script.
                    134: 
                    135: ## Order/dependencies of start determined by rcorder
                    136: 
1.3     ! jdf       137: The startup system of every Unix system determines, in one way or another, the
        !           138: order in which services are started. On some Unix systems this is done by
        !           139: numbering the files and/or putting them in separate run level directories.
        !           140: Solaris relies on wildcards like `/etc/rc[23].d/S*` being sorted numerically
        !           141: when expanded. Some simply put all the commands that should be started into a
        !           142: single monolithic script (this is the traditional BSD method, and is what NetBSD
        !           143: did before the rc.d system). On modern NetBSD this is done by the rc.d scripts
        !           144: and their contents. Please note that NetBSD does not have multiple runlevels as
1.1       jdf       145: found in SysV-style systems like Solaris and Linux.
                    146: 
1.3     ! jdf       147: At the beginning of each rc.d script there is a series of commented out lines
1.1       jdf       148: that have one of the following items in them:
                    149: 
                    150:  * `REQUIRE`
                    151:  * `PROVIDE`
                    152:  * `BEFORE`
                    153:  * `KEYWORD`
                    154: 
1.3     ! jdf       155: These describe the dependencies of that particular script and allow rcorder to
        !           156: easily work either `up` or `down` as the situation requires. As an example, here
1.1       jdf       157: is the ordering information contained in `/etc/rc.d/nfsd`:
                    158: 
                    159:     ...
1.2       jdf       160:     # PROVIDE: nfsd
                    161:     # REQUIRE: rpcbind mountd
1.1       jdf       162:     ...
                    163: 
1.3     ! jdf       164: Here we can see that this script provides the `nfsd` service and that it
        !           165: requires `rpcbind` and `mountd` to be running first. The
        !           166: [rcorder(8)](http://netbsd.gw.com/cgi-bin/man-cgi?rcorder+8+NetBSD-5.0.1+i386)
        !           167: utility is used at system startup time to read through all the rc.d scripts and
1.1       jdf       168: determine the order in which they should be run.
                    169: 
                    170: ## rc.d scripts of additional services
                    171: 
                    172: Packages you install additionally won't be listed in the rc.conf(5) manpage.
                    173: Packages installing services which can be started with an rc.d script tell you
                    174: so after they are installed, along with the variable that is used for starting
                    175: them (usually, it has the same name as the service itself).
                    176: 
                    177: Then, you usually have to copy them from `/usr/pkg/share/examples/rc.d` to
                    178: `/etc/rc.d` for `rc` to automatically find them, or add the line
                    179: 
                    180:     rc_directories="/etc/rc.d /usr/pkg/share/examples/rc.d"
                    181: 
                    182: to your `/etc/rc.conf`.
                    183: 
                    184: If you forgot the name of the service variable, you can have a look at the rc.d
                    185: script itself. The variable `rcvar` (usually set to `$name`) will tell you.
                    186: E.g., to find the name of variable to start fscd, run
                    187: 
                    188:     $ grep ^rcvar /etc/rc.d/fscd
                    189:        rcvar=${name}
                    190:        $ grep ^name /etc/rc.d/fscd   # Aaaw, no direct rcvar.
                    191:        name="fscd"
                    192: 
                    193: Thus, you need in your `/etc/rc.conf` the entry:
                    194: 
                    195:     fscd="YES"
                    196: 
                    197: ## Additional Reading
                    198: 
1.3     ! jdf       199: Luke Mewburn, one of the principal designers of the rc.d system, gave a
        !           200: presentation on the system at USENIX 2001. It is available in
1.1       jdf       201: [PDF](http://www.mewburn.net/luke/papers/rc.d.pdf) format.
                    202: 

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb