OpenRC

From Gentoo Wiki
Jump to: navigation, search
Article status
IconClipboardTask.png This article has some todo items:

OpenRC is a dependency based init system that works with the system provided init program, normally /sbin/init. It is not a replacement for /sbin/init. OpenRC is 100% compatible with Gentoo init scripts, which means you can probably find one for the daemons you want to start in the Gentoo Portage Tree. OpenRC, however, is not exclusively used by Gentoo Linux and can be used on different Linux and BSD systems.

Features

OpenRC provides a number of features touted as innovative by recent init systems like systemd or upstart, such as hardware initiated initscript run or cgroups support, without requiring large layout changes to accommodate radically different designs and dependencies. See also Comparison of init systems.

OpenRC Busybox Integration

Busybox can be used to replace most of the userspace needed by OpenRC (init, shell, awk and other posix tools), by using a complete busybox as shell for OpenRC all the calls that normally would cause a fork/exec would be spared, improving the overall speed. This process isn't yet streamlined.

Please note that there are currently many busybox applets that are incompatible with OpenRC. See bug #529086 for details.

Replacing init

The sysvinit inittab provided by Gentoo is not compatible to the busybox init.

File/etc/inittabExample inittab compatible with busybox init

::sysinit:/sbin/rc sysinit
::wait:/sbin/rc boot
::wait:/sbin/rc

In order to set a specific runlevel from the bootloader the variable `softlevel=` should be used.

Busybox specific init.d files

TODO: busybox provides a number of applets that could be used to replace third party software like acpid or dhcp/dhcpcd.

Replacing udev with mdev

See mdev.

Using runlevels

OpenRC can be controlled and configured using rc, rc-update and rc-status.

Named runlevels

OpenRC runlevels are directories living in /etc/runlevels to create additional runlevels is enough to issue

root # install -d /etc/runlevels/$runlevel

Stacked runlevels

Is possible manage variants using rc-update -s

An usage example for using stacked runlevel on laptop to group networking services based on location is at OpenRC/StackedRunlevel.

Select specific runlevel at boot

OpenRC reads the kernel commandline used at boot time, and will start the runlevel specified by the "softlevel" parameter if provided, instead of 'default'.

For instance, you can choose whether to boot into the 'default' or 'nonetwork' runlevels with the following example grub.conf configuration:

File/boot/grub/grub.confExample grub.conf (grub legacy)

...

title=Regular Start-up
...
kernel (hd0,0)/boot/kernel-3.7.10-gentoo-r1 root=/dev/sda3

title=Start without Networking
...
kernel (hd0,0)/boot/kernel-3.7.10-gentoo-r1 root=/dev/sda3 softlevel=nonetwork

Dependency behaviour

Changing the default dependencies of init scripts, might be needed to fit more complex setups. See /etc/rc.conf for how to change the default behaviour; notice the rc_depend_strict option. In addition, next networking examples show how flexible OpenRC can be.

Multiple network interfaces (example)

The SSH service must come up with the internal network, for instance eth0 and never wlan0.

Overrule the "net" dependency from /etc/init.d/sshd, and refine it to depend on "net.eth0":

File/etc/conf.d/sshd

rc_need="!net net.eth0"
Multiple network interfaces in multiple runlevels (example)

The SSH service must start with eth0 (not wlan0) in "default" runlevel, but in "office" runlevel it must start with wlan0 (not eth0).

Keep the default:

File/etc/rc.conf

#rc_depend_strict="YES"

Make additional symlinks to sshd with the network interface names:

root # ln -s sshd /etc/init.d/sshd.eth0
root #
ln -s sshd /etc/init.d/sshd.wlan0

Settings are read from /etc/conf.d/sshd.eth0 and /etc/conf.d/sshd.wlan0 now:

root # cp /etc/conf.d/sshd /etc/conf.d/sshd.eth0
root #
cp /etc/conf.d/sshd /etc/conf.d/sshd.wlan0

Add the dependencies:

root # echo 'rc_need="!net net.eth0"' >> /etc/conf.d/sshd.eth0
root #
echo 'rc_need="!net net.wlan0"' >> /etc/conf.d/sshd.wlan0

In this example net.eth0 and net.wlan0 read their settings from /etc/conf.d/net, or /etc/conf.d/net.office depending on the active runlevel. Add all runscripts to the different runlevels:

root # rc-update add sshd.eth0 default
root #
rc-update add sshd.wlan0 office
root #
rc-update add net.eth0 default office
root #
rc-update add net.wlan0 default office

To switch between "default" runlevel and "office" runlevel without rebooting the computer, change to "nonetwork" runlevel in between. The network interfaces will be stopped this way, and re-read their runlevel specific configuration.

default runlevel <---> nonetwork runlevel <---> office runlevel
root # rc nonetwork && rc office
root #
rc nonetwork && rc default

Prefix usage

Gentoo Prefix installs Gentoo within an offset, known as a prefix, allowing users to install Gentoo in another location in the filesystem hierarchy, hence avoiding conflicts. Next to this offset, Gentoo Prefix runs unprivileged, meaning no root user or rights are required to use it.

By using an offset (the "prefix" location), it is possible for many "alternative" user groups to benefit from a large part of the packages in the Gentoo Linux Portage tree. Currently users of the following systems successfully run Gentoo Prefix: Mac OS X on PPC and x86, Linux on x86, x86_64 and ia64, Solaris 10 on Sparc, Sparc/64, x86 and x86_64, FreeBSD on x86, AIX on PPC, Interix on x86, Windows on x86 (with the help of Interix), HP-UX on PARISC and ia64.

OpenRC runscript already support prefix-installed daemons, during the SummerOfCode2012 work will be done to implement full secondary/session daemon behaviour to complete the overall feature set provided by Prefix.

OpenRC/Prefix, a tutorial for trying it out.

Hotplug usage

OpenRC can be trigger by external events, such as new hardware from udev. See OpenRC/Event Driven for details.

Automatic respawning crashed services

OpenRC can return state of services to runlevel setting state. To provide stateful init scripts and automatic respawning. What do your need is run "rc" (for default runlevel). Crashed services start and manual run services will stop. To prevet this you can run "rc -n" (--not-stop)

By default rc will attempt just to start crashed services, not restart. This сontrolled by rc_crashed_stop (default NO) and rc_crashed_start (default YES) options in /etc/rc.conf.

CGroups support

OpenRC starting with version 0.12 has extended cgroups support. See OpenRC/CGroups for details.

Chroot support

TODO: add info about chroot support

Additional resources