OpenRC: Difference between revisions

From Alpine Linux
(replaced command usage as this keeps changing with openrc versions and can easily outdated)
(updated instructions for xorg)
Line 119: Line 119:
#:The above option ensures settings in {{path|${XDG_CONFIG_HOME:-$HOME/.config}/rc/rc.conf}} override {{path|/etc/rc.conf}}. Similarly, configuration and service files in {{path|${XDG_CONFIG_HOME:-$HOME/.config}/rc/conf.d/}} and {{path|${XDG_CONFIG_HOME:-$HOME/.config}/rc/init.d/}} override {{path|/etc/user/conf.d/}} and {{path|/etc/user/init.d/}} respectively.
#:The above option ensures settings in {{path|${XDG_CONFIG_HOME:-$HOME/.config}/rc/rc.conf}} override {{path|/etc/rc.conf}}. Similarly, configuration and service files in {{path|${XDG_CONFIG_HOME:-$HOME/.config}/rc/conf.d/}} and {{path|${XDG_CONFIG_HOME:-$HOME/.config}/rc/init.d/}} override {{path|/etc/user/conf.d/}} and {{path|/etc/user/init.d/}} respectively.
# For many [[wayland]] user services, a custom GUI runlevel needs to be created. To create this runlevel,run the following command:{{Cmd|$ mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/rc/runlevels/gui"}}
# For many [[wayland]] user services, a custom GUI runlevel needs to be created. To create this runlevel,run the following command:{{Cmd|$ mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/rc/runlevels/gui"}}
#To start user services from the above '''gui'''  runlevel, add the line {{ic|openrc --user gui}} to the startup file of your compositor after {{ic|$WAYLAND_DISPLAY}} is set.  
#:To start user services from the above '''gui'''  runlevel, add the line {{ic|openrc --user gui}} to the startup file of your compositor after {{ic|$WAYLAND_DISPLAY}} is set.  
#: For eg, for [[Sway]], add the following line to sway user config file {{Path|~/.config/sway/config}} as follows {{Cat|~/.config/sway/config|...
#: For eg, for [[Sway]], add the following line to sway user config file {{Path|~/.config/sway/config}} as follows {{Cat|~/.config/sway/config|...
exec openrc --user gui
exec openrc --user gui
...}}
...}}
# Enable [[PipeWire]] User service by adding it to '''gui''' runlevel:{{Cmd|$ rc-update -U add pipewire gui}}
#:Enable [[PipeWire]] User service by adding it to '''gui''' runlevel:{{Cmd|$ rc-update -U add pipewire gui}}
#: If {{pkg|wlsunset}} package is installed, to enable the wlsunset service replace {{codeline|pipewire}} by {{codeline|wlsunset}} in the above command.
# For [[Xorg]], custom GUI runlevels are not needed. If [[Elogind]] is not used, ensure that [[Wayland#Creating_and_exporting_XDG_RUNTIME_DIR_manually|XDG_RUNTIME_DIR]] is set manually in {{path|.xinitrc}} before the following entry:{{Cat|.xinitrc|...
openrc -U default}}  
#:Enable [[PipeWire]] User service by adding it to '''default''' runlevel:{{Cmd|$ rc-update -U add pipewire default}}
#Logout and login for the user services to be lauched.  
# Remove the startup of above services either from {{Path|/etc/xdg/autostart}} folder or from the startup/config file, i.e wherever they were started/mentioned earlier.
# Remove the startup of above services either from {{Path|/etc/xdg/autostart}} folder or from the startup/config file, i.e wherever they were started/mentioned earlier.
# Logout and login to your [[wayland]] compositor for the user services to be lauched.
# Issue the command {{ic|$ rc-status -U}} to view the OpenRC user services.
# Issuing the command {{ic|$ rc-status -U}} will produce an output similar to below, if the above instructions have been followed:
<Pre>
Runlevel: gui
pipewire                                                [  started 04:29:55 (0) ]
wlsunset                                                [  started 04:29:55 (0) ]
mako                                                    [  started 04:29:53 (1) ]
Dynamic Runlevel: hotplugged
Dynamic Runlevel: needed/wanted
dbus                                                    [  started 04:29:55 (0) ]
wireplumber                                              [  started 04:29:53 (1) ]
Dynamic Runlevel: manual
</Pre>
{{Todo|
[[KDE]] plasma users are requested to test the below instructions and [[Help:Editing|edit]] this page directly or the [[Talk:OpenRC|Discussion]] page.
# Enable [[#PAM support|PAM support]] for openrc user services.
# In the above instructions, skip steps 2 & 3. In step 4, skip the word "gui", as [[SDDM]] automatically drops the user in "default" user runlevel on logging in.}}


=== PAM support ===
=== PAM support ===

Revision as of 17:37, 27 July 2025

Alpine Linux uses openrc for its init system. The init system manages the services, startup and shutdown of your computer.

Refer to the excellent guide working with OpenRC from Alpine Linux documentation project to learn the basics quickly. Refer Writing Init Scripts and Multiple instances of services pages for more advanced information.

Quickstart

Action Command
Managing a service - start,stop and restart
Start <serviceName> now # rc-service <serviceName> start
Stop <serviceName> now # rc-service <serviceName> stop
Restart <serviceName> now # rc-service <serviceName> restart
Adding and removing service from runlevels
Add <serviceName> to <runlevel> # rc-update add <serviceName> <runlevel>
Remove <serviceName> from <runlevel> # rc-update del <serviceName> <runlevel>
Check services in a runlevel and their status
To check status of <serviceName> $ rc-service <serviceName> status
To view services configured at <runlevel> $ rc-update show <runlevel>
To view currently active runlevels and state of services $ rc-status
Check and manage runlevels
To view available runlevels $ rc-status -l
To change to a different <runlevel> $ openrc <runlevel>
Stacked runlevels
To add <s-runlevel> as a stacked <runlevel> # rc-update add -s <s-runlevel> <runlevel>
User Services - adding -U or --user to most of above commands should work
To view currently active User runlevels and state of User services $ rc-status -U
To change to a different user <runlevel> $ openrc -U <runlevel>
Add User <serviceName> to user <runlevel> $ rc-update -U add <serviceName> <runlevel>
Command Usage - add -h or --help to all commands
To view command usage for all openrc commands $ rc-status -h or $ rc-status --help

Runlevels

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. The default startup uses the runlevels sysinit, boot, and default, in that order. Shutdown uses the shutdown runlevel.

The available runlevels are:

  • default - Used if no runlevel is specified. (This is generally the runlevel you want to add services to.)
  • hotplugged
  • manual

The special runlevels are:

  • sysinit - Brings up system specific stuff such as /dev, /proc and optionally /sys for Linux based systems. It also mounts /lib/rc/init.d as a ramdisk using tmpfs where available unless / is mounted rw at boot. rc uses /lib/rc/init.d to hold state information about the services it runs. sysinit always runs when the host first starts and should not be run again.
  • boot - Generally the only services you should add to the boot runlevel are those which deal with the mounting of filesystems, set the initial state of attached peripherals and logging. Hotplugged services are added to the boot runlevel by the system. All services in the boot and sysinit runlevels are automatically included in all other runlevels except for those listed here.
  • single - Stops all services except for those in the sysinit runlevel.
  • reboot - Changes to the shutdown runlevel and then reboots the host.
  • shutdown - Changes to the shutdown runlevel and then halts the host.

Stacked runlevels

Runlevel "inheritance" is acheived through runlevel stacking. For more detailed information, refer Gentoo wiki.

Configuration

System-wide configuration file for OpenRC is /etc/rc.conf.

If the setting rc_parallel="YES" is configured, the OpenRC system tries to start services in parallel for a slight speed improvement. This setting however comes with a message from openRC developers:

Warning: whilst we have improved parallel, it can still potentially lock the boot process. Don't file bugs about this.


To improve boot times, consider the idea suggested in the preventing slow services from delaying boot section.

Cgroups

Since openrc 0.51 cgroups v2, or "unified", is the default. You can enable hybrid cgroups v1 & v2 by editing /etc/rc.conf and setting rc_cgroup_mode="hybrid".

Then you should run

# rc-service cgroups start

to take effect and

# rc-update add cgroups

to auto mount the cgroup filesystem on boot.

User services

OpenRC supports managing services for users. User services are currently experimental and available from v3.22 for the following:

Prerequisites

Configuration

Follow the below configuration steps to install user service:

  1. Allow propagation of the WAYLAND_DISPLAY environment variable by adding the following lines to file ${XDG_CONFIG_HOME:-$HOME/.config}/rc/rc.conf as follows:

    Contents of rc.conf

    rc_env_allow="WAYLAND_DISPLAY"
    The above option ensures settings in ${XDG_CONFIG_HOME:-$HOME/.config}/rc/rc.conf override /etc/rc.conf. Similarly, configuration and service files in ${XDG_CONFIG_HOME:-$HOME/.config}/rc/conf.d/ and ${XDG_CONFIG_HOME:-$HOME/.config}/rc/init.d/ override /etc/user/conf.d/ and /etc/user/init.d/ respectively.
  2. For many wayland user services, a custom GUI runlevel needs to be created. To create this runlevel,run the following command:

    $ mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/rc/runlevels/gui"

    To start user services from the above gui runlevel, add the line openrc --user gui to the startup file of your compositor after $WAYLAND_DISPLAY is set.
    For eg, for Sway, add the following line to sway user config file ~/.config/sway/config as follows

    Contents of ~/.config/sway/config

    ... exec openrc --user gui ...
    Enable PipeWire User service by adding it to gui runlevel:

    $ rc-update -U add pipewire gui

  3. For Xorg, custom GUI runlevels are not needed. If Elogind is not used, ensure that XDG_RUNTIME_DIR is set manually in .xinitrc before the following entry:

    Contents of .xinitrc

    ... openrc -U default
    Enable PipeWire User service by adding it to default runlevel:

    $ rc-update -U add pipewire default

  4. Logout and login for the user services to be lauched.
  5. Remove the startup of above services either from /etc/xdg/autostart folder or from the startup/config file, i.e wherever they were started/mentioned earlier.
  6. Issue the command $ rc-status -U to view the OpenRC user services.

PAM support

The default installation of OpenRC user services is without PAM support. To ensure that user services do not linger on logout, enable PAM support for OpenRC user services by installing the openrc-user-pam package.

# apk add openrc-user-pam

If greetd is used, a harmless failed error message appears for user.greetd service as per !81612#note_492385.

Preventing slow services from delaying boot

Services that take a while to start will block the boot process until they complete. E.g.: iwd and networking might delay startup of an interactive system rather than start in the background.

This can be remedied as per Patrycja's blog post titled OpenRC: Start services after login prompt. This solution makes use of stacked runlevels.

Warning: If the file /etc/inittab is edited wrongly, the system may not boot. Take backup and learn how to restore using rescue disk before proceeding.


  • Create a custom runlevel (name is “async” here, but it doesn’t matter)

    # mkdir /etc/runlevels/async

  • Add default as a stacked runlevel

    # rc-update add -s default async

  • Remove slow services from default and add them to async

    # rc-update del chronyd # rc-update add chronyd async

  • Add changing of runlevel to async by adding the line ::once:/sbin/openrc async to /etc/inittab file as follows:

    Contents of /etc/inittab

    ... ::wait:/sbin/openrc default ::once:/sbin/openrc async -q # Set up a couple of getty's tty1::respawn:/sbin/getty 38400 tty1 ...

After rebooting, services from async will start separately. This change does not affect other services that start from Default runlevel and they may still block agetty from running due to the wait label.

Command usage

To view the command usage for all OpenRC commands (rc-update, rc-service, rc-status, openrc etc.) either use --help or (-h) flag. For eg:$ rc-update --help or $ rc-update -h will show usage information for rc-update.

The busybox command equivalent for traditional GNU/Linux systems is as follows:

# reboot # ⇔ shutdown now -r # halt # ⇔ shutdown now -H # poweroff # ⇔ shutdown now -P

Troubleshooting

Whenever a openRC service fails to run, to troubleshoot enable debugging option and run the command.

For example, if running the command rc-service greetd start causes greetd service to immediately crash, then alter the command to enable and view the debug:

# rc-service -d greetd restart

XDG_RUNTIME_DIR unset

While configuring user services, running the command $ doas rc-update add -U pipewire gui will generate the above error XDG_RUNTIME_DIR unset. Always issue the command as normal user $ rc-update add -U pipewire gui and do NOT use doas.

See also