2016-02-10 05:30:46 +08:00
|
|
|
# Purpose and description
|
|
|
|
|
|
|
|
OpenRC is an init system for Unixoid operating systems. It takes care of
|
|
|
|
startup and shutdown of the whole system, including services.
|
|
|
|
|
|
|
|
It evolved out of the Gentoo "Baselayout" package which was a custom pure-shell
|
|
|
|
startup solution. (This was both hard to maintain and debug, and not very
|
|
|
|
performant)
|
|
|
|
|
|
|
|
Most of the core parts are written in C99 for performance and flexibility
|
|
|
|
reasons, while everything else is posix sh.
|
|
|
|
The License is 2-clause BSD
|
|
|
|
|
|
|
|
Current size is about 10k LoC C, and about 4k LoC shell.
|
|
|
|
|
|
|
|
OpenRC is known to work on Linux, many BSDs (FreeBSD, OpenBSD, DragonFlyBSD at
|
|
|
|
least) and HURD.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
Services are stateful (i.e. `start`; `start` will lead to "it's already started")
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
# Startup
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
Usually PID1 (aka. `init`) calls the OpenRC binary (`/sbin/openrc` by default).
|
2016-02-10 05:30:46 +08:00
|
|
|
(The default setup assumes sysvinit for this)
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
openrc scans the runlevels (default: `/etc/runlevels`) and builds a dependency
|
2016-02-10 05:30:46 +08:00
|
|
|
graph, then starts the needed service scripts, either serialized (default) or in
|
|
|
|
parallel.
|
|
|
|
|
|
|
|
When all the init scripts are started openrc terminates. There is no persistent
|
|
|
|
daemon. (Integration with tools like monit, runit or s6 can be done)
|
|
|
|
|
|
|
|
# Shutdown
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
On change to runlevel 0/6 or running `reboot`, `halt` etc., openrc stops all
|
|
|
|
services that are started and runs the services in the `shutdown` runlevel.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
# Modifying Service Scripts
|
|
|
|
|
|
|
|
Any service can, at any time, be started/stopped/restarted by executing
|
2016-09-02 20:10:05 +08:00
|
|
|
`rc-service someservice start`, `rc-service someservice stop`, etc.
|
2016-02-10 05:30:46 +08:00
|
|
|
Another, less preferred method, is to run the service script directly,
|
2016-09-02 20:10:05 +08:00
|
|
|
e.g. `/etc/init.d/service start`, `/etc/init.d/service stop`, etc.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
OpenRC will take care of dependencies, e.g starting apache will start network
|
|
|
|
first, and stopping network will stop apache first.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
There is a special command `zap` that makes OpenRC 'forget' that a service is
|
2016-02-10 05:30:46 +08:00
|
|
|
started; this is mostly useful to reset a crashed service to stopped state
|
|
|
|
without invoking the (possibly broken) stop function of the service script.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
Calling `openrc` without any arguments will try to reset all services so
|
2016-02-10 05:30:46 +08:00
|
|
|
that the current runlevel is satisfied; if you manually started apache it will be
|
|
|
|
stopped, and if squid died but is in the current runlevel it'll be restarted.
|
|
|
|
|
|
|
|
# Runlevels
|
|
|
|
|
|
|
|
OpenRC has a concept of runlevels, similar to what sysvinit historically
|
|
|
|
offered. A runlevel is basically a collection of services that needs to be
|
|
|
|
started. Instead of random numbers they are named, and users can create their
|
|
|
|
own if needed. This allows, for example, to have a default runlevel with
|
|
|
|
"everything" enabled, and a "powersaving" runlevel where some services are
|
|
|
|
disabled.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
The `rc-status` helper will print all currently active runlevels and the state
|
2016-02-10 05:30:46 +08:00
|
|
|
of init scripts in them:
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
# rc-status
|
|
|
|
* Caching service dependencies ... [ ok ]
|
|
|
|
Runlevel: default
|
|
|
|
modules [ started ]
|
|
|
|
lvm [ started ]
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
All runlevels are represented as folders in `/etc/runlevels/` with symlinks to
|
2016-02-10 05:30:46 +08:00
|
|
|
the actual init scripts.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
Calling openrc with an argument (`openrc default`) will switch to that
|
2016-02-10 05:30:46 +08:00
|
|
|
runlevel; this will start and stop services as needed.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
Managing runlevels is usually done through the `rc-update` helper, but could of
|
2016-02-10 05:30:46 +08:00
|
|
|
course be done by hand if desired.
|
2016-09-02 20:10:05 +08:00
|
|
|
e.g. `rc-update add nginx default` - add nginx to the default runlevel
|
|
|
|
Note: This will not auto-start nginx! You'd still have to trigger `rc` or run
|
2016-02-10 05:30:46 +08:00
|
|
|
the initscript by hand.
|
|
|
|
|
|
|
|
FIXME: Document stacked runlevels
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
The default startup uses the runlevels `boot`, `sysinit` and `default`, in that
|
|
|
|
order. Shutdown uses the `shutdown` runlevel.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Syntax of Service Scripts
|
|
|
|
|
|
|
|
Service scripts are shell scripts. OpenRC aims at using only the standardized
|
|
|
|
POSIX sh subset for portability reasons. The default interpreter (build-time
|
2016-09-02 20:10:05 +08:00
|
|
|
toggle) is `/bin/sh`, so using for example mksh is not a problem.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
OpenRC has been tested with busybox sh, ash, dash, bash, mksh, zsh and possibly
|
|
|
|
others. Using busybox sh has been difficult as it replaces commands with
|
|
|
|
builtins that don't offer the expected features.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
The interpreter for initscripts is `#!/sbin/openrc-run`.
|
2016-02-10 05:30:46 +08:00
|
|
|
Not using this interpreter will break the use of dependencies and is not
|
2016-09-02 20:10:05 +08:00
|
|
|
supported. (iow: if you insist on using `#!/bin/sh` you're on your own)
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
A `depend` function declares the dependencies of this service script.
|
2016-02-10 05:30:46 +08:00
|
|
|
All scripts must have start/stop/status functions, but defaults are provided.
|
|
|
|
Extra functions can be added easily:
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
extra_commands="checkconfig"
|
|
|
|
checkconfig() {
|
|
|
|
doSomething
|
|
|
|
}
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
This exports the checkconfig function so that `/etc/init.d/someservice
|
|
|
|
checkconfig` will be available, and it "just" runs this function.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
While commands defined in `extra_commands` are always available, commands
|
|
|
|
defined in `extra_started_commands` will only work when the service is started
|
|
|
|
and those defined in `extra_stopped_commands` will only work when the service is
|
2016-02-10 05:30:46 +08:00
|
|
|
stopped. This can be used for implementing graceful reload and similar
|
|
|
|
behaviour.
|
|
|
|
|
|
|
|
Adding a restart function will not work, this is a design decision within
|
|
|
|
OpenRC. Since there may be dependencies involved (e.g. network -> apache) a
|
|
|
|
restart function is in general not going to work.
|
2016-09-02 20:10:05 +08:00
|
|
|
restart is internally mapped to `stop()` + `start()` (plus handling dependencies).
|
2016-02-10 05:30:46 +08:00
|
|
|
If a service needs to behave differently when it is being restarted vs
|
2016-09-02 20:10:05 +08:00
|
|
|
started or stopped, it should test the `$RC_CMD` variable, for example:
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
|
|
|
[ "$RC_CMD" = restart ] && do_something
|
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
# The Depend Function
|
|
|
|
|
|
|
|
This function declares the dependencies for a service script. This
|
|
|
|
determines the order the service scripts start.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
depend() {
|
|
|
|
need net
|
|
|
|
use dns logger netmount
|
|
|
|
want coolservice
|
|
|
|
}
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
`need` declares a hard dependency - net always needs to be started before this
|
2016-02-10 05:30:46 +08:00
|
|
|
service does
|
2016-09-02 20:10:05 +08:00
|
|
|
|
|
|
|
`use` is a soft dependency - if dns, logger or netmount is in this runlevel
|
2016-02-10 05:30:46 +08:00
|
|
|
start it before, but we don't care if it's not in this runlevel.
|
2016-09-02 20:10:05 +08:00
|
|
|
`want` is between need and use - try to start coolservice if it is
|
2016-02-10 05:30:46 +08:00
|
|
|
installed on the system, regardless of whether it is in the
|
|
|
|
runlevel, but we don't care if it starts.
|
2016-09-02 20:10:05 +08:00
|
|
|
|
|
|
|
`before` declares that we need to be started before another service
|
|
|
|
|
|
|
|
`after` declares that we need to be started after another service, without
|
2016-02-10 05:30:46 +08:00
|
|
|
creating a dependency (so on calling stop the two are independent)
|
2016-09-02 20:10:05 +08:00
|
|
|
|
|
|
|
`provide` allows multiple implementations to provide one service type, e.g.:
|
|
|
|
`provide cron` is set in all cron-daemons, so any one of them started
|
2016-02-10 05:30:46 +08:00
|
|
|
satisfies a cron dependency
|
2016-09-02 20:10:05 +08:00
|
|
|
|
|
|
|
`keyword` allows platform-specific overrides, e.g. `keyword -lxc` makes this
|
2016-02-10 05:30:46 +08:00
|
|
|
service script a noop in lxc containers. Useful for things like keymaps,
|
|
|
|
module loading etc. that are either platform-specific or not available
|
|
|
|
in containers/virtualization/...
|
|
|
|
|
|
|
|
FIXME: Anything missing in this list?
|
|
|
|
|
|
|
|
# The Default Functions
|
|
|
|
|
|
|
|
All service scripts are assumed to have the following functions:
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
start()
|
|
|
|
stop()
|
|
|
|
status()
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
There are default implementations in `lib/rc/sh/openrc-run.sh` - this allows very
|
2016-02-10 05:30:46 +08:00
|
|
|
compact service scripts. These functions can be overridden per service script as
|
|
|
|
needed.
|
|
|
|
|
|
|
|
The default functions assume the following variables to be set in the service
|
|
|
|
script:
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
command=
|
|
|
|
command_args=
|
|
|
|
pidfile=
|
2017-01-27 05:01:43 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
Thus the 'smallest' service scripts can be half a dozen lines long
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
# The Magic of `conf.d`
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
Most service scripts need default values. It would be fragile to
|
2016-09-02 20:10:05 +08:00
|
|
|
explicitly source some arbitrary files. By convention `openrc-run` will source
|
|
|
|
the matching file in `/etc/conf.d/` for any script in `/etc/init.d/`
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
This allows you to set random startup-related things easily. Example:
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
conf.d/foo:
|
|
|
|
START_OPTS="--extraparameter sausage"
|
|
|
|
|
|
|
|
init.d/foo:
|
|
|
|
start() {
|
|
|
|
/usr/sbin/foo-daemon ${STARTOPTS}
|
|
|
|
}
|
2016-09-02 20:10:05 +08:00
|
|
|
```
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
The big advantage of this split is that most of the time editing of the init
|
|
|
|
script can be avoided.
|
|
|
|
|
|
|
|
# Start-Stop-Daemon
|
|
|
|
|
|
|
|
OpenRC has its own modified version of s-s-d, which is historically related and
|
|
|
|
mostly syntax-compatible to Debian's s-s-d, but has been rewritten from scratch.
|
|
|
|
|
|
|
|
It helps with starting daemons, backgrounding, creating PID files and many
|
|
|
|
other convenience functions related to managing daemons.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
# `/etc/rc.conf`
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
This file manages the default configuration for OpenRC, and it has examples of
|
|
|
|
per-service-script variables.
|
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
Among these are `rc_parallel` (for parallelized startup), `rc_log` (logs all boot
|
2016-02-10 05:30:46 +08:00
|
|
|
messages to a file), and a few others.
|
|
|
|
|
|
|
|
# ulimit and CGroups
|
|
|
|
|
2017-09-14 23:39:38 +08:00
|
|
|
Setting `ulimit` and `nice` values per service can be done through the
|
|
|
|
`rc_ulimit` variable.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2017-09-14 23:39:38 +08:00
|
|
|
Under Linux, OpenRC can use cgroups for process management as well. Once
|
|
|
|
the kernel is configured appropriately, the `rc_cgroup_mode` setting in
|
|
|
|
/etc/rc.conf should be used to control whether cgroups version one,,
|
|
|
|
two, or both are used. The default is to use both if they are available.
|
|
|
|
|
|
|
|
By changing certain settings in the service's `conf.d` file limits can be
|
|
|
|
enforced per service. These settings are documented in detail in the
|
|
|
|
default /etc/rc.conf under `LINUX CGROUPS RESOURCE MANAGEMENT`.
|
|
|
|
|
|
|
|
# Dealing with Orphaned Processes
|
|
|
|
|
|
|
|
It is possible to get into a state where there are orphaned processes
|
|
|
|
running which were part of a service. For example, if you are monitoring
|
|
|
|
a service with supervise-daemon and supervise-daemon dies for an unknown
|
|
|
|
reason. The way to deal with this will be different for each system.
|
|
|
|
|
|
|
|
On Linux systems with cgroups enabled, the cgroup_cleanup command is
|
|
|
|
added to all services. You can run it manually, when the service is
|
|
|
|
stopped, by using:
|
|
|
|
|
|
|
|
```
|
|
|
|
# rc-service someservice cgroup_cleanup
|
|
|
|
```
|
|
|
|
|
|
|
|
The `rc_cgroup_cleanup` setting can be changed to yes to make this
|
|
|
|
happen automatically when the service is stopped.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Caching
|
|
|
|
|
|
|
|
For performance reasons OpenRC keeps a cache of pre-parsed initscript metadata
|
2016-09-02 20:10:05 +08:00
|
|
|
(e.g. `depend`). The default location for this is `/${RC_SVCDIR}/cache`.
|
2016-02-10 05:30:46 +08:00
|
|
|
|
2016-09-02 20:10:05 +08:00
|
|
|
The cache uses `mtime` to check for file staleness. Should any service script
|
2016-02-10 05:30:46 +08:00
|
|
|
change it'll re-source the relevant files and update the cache
|
|
|
|
|
|
|
|
# Convenience functions
|
|
|
|
|
|
|
|
OpenRC has wrappers for many common output tasks in libeinfo.
|
|
|
|
This allows to print colour-coded status notices and other things.
|
|
|
|
To make the output consistent the bundled initscripts all use ebegin/eend to
|
|
|
|
print nice messages.
|