OpenRC: Difference between revisions

From Alpine Linux
(Clarify status of user services, add `-p` flag to mkdir.)
 
(43 intermediate revisions by 18 users not shown)
Line 1: Line 1:
= Alpine Linux Init System =
Alpine Linux uses [https://github.com/OpenRC/ openrc] for its init system.  The init system manages the services, startup and shutdown of your computer.  
Alpine Linux uses [http://roy.marples.name/projects/openrc OpenRC] for its init system.


== Quick-Start Information ==
Refer to the excellent guide [https://docs.alpinelinux.org/user-handbook/0.1a/Working/openrc.html working with OpenRC] from Alpine Linux documentation project to learn the basics quickly. Refer [[Writing Init Scripts]] and [[Multiple Instances of Services|Multiple instances of services]] pages for more advanced information.
Alpine Linux has the following commands for managing the init system:
* <code>rc-update add service <runlevel></code>
* <code>rc-update del service <runlevel></code>
* <code>rc-service <service> <start | stop | restart></code><br />''(Equivalent to <code>/etc/init.d/service <start | stop | restart></code>)''
* <code>rc-status</code><br />''(To check services and their set runlevels)''
* <code>rc <runlevel></code><br />''(To change to a different runlevel)''
* <code>reboot</code><br />''(Equivalent to <code>shutdown -r now</code> from traditional GNU/Linux systems)''
* <code>halt</code><br />''(Equivalent to <code>shutdown -h now</code> from traditional GNU/Linux systems)''


{{Tip|Prior to Alpine Linux 2.0.0, you might need to use the following commands instead: ''rc_add, rc_delete,'' and ''rc_status''.}}
== Quickstart  ==
 
{|align="center" style="width:100%; border:1px #0771a6 solid; background:#f9f9f9; text-align:left; border-collapse:collapse;"
|- style="background:#333333; color:#ffffff; font-size: 0.9em; text-align:center;"
| width="50%" |Action
|            |Command
|-
|- style="background:#333333; color:#ffffff; font-size: 0.9em; text-align:center;"
| colspan="3" | '''Managing a service - start,stop and restart'''
|-
| Start <serviceName> now || {{ic|rc-service <serviceName> start}}
|-
| Stop <serviceName> now || {{ic|rc-service <serviceName> stop}}
|-
| Restart <serviceName> now || {{ic|rc-service <serviceName> restart}}
|-
|- style="background:#333333; color:#ffffff; font-size: 0.9em; text-align:center;"
| colspan="3" | '''Adding and removing service from runlevels'''
|-
| Add <serviceName> to <runlevel> || {{ic|rc-update add <serviceName> <runlevel>}}
|-
| Remove <serviceName> from <runlevel> || {{ic|rc-update del <serviceName> <runlevel>}}
|-
|- style="background:#333333; color:#ffffff; font-size: 0.9em; text-align:center;"
| colspan="3" | ''' Check services in a runlevel and their status'''
|- style="background:#333333; color:#ffffff; font-size: 0.9em; text-align:center;"
|-
| To check status of  <serviceName> || {{ic|rc-service <serviceName> status}}
|-
| To view services configured at <runlevel>  || {{ic|rc-update show <runlevel>}}
|-
| To view currently active runlevels and state of services  || {{ic|rc-status}}
|-
|- style="background:#333333; color:#ffffff; font-size: 0.9em; text-align:center;"
| colspan="3" | ''' Check and manage runlevels'''
|- style="background:#333333; color:#ffffff; font-size: 0.9em; text-align:center;"
|-
| To view available runlevels || {{ic|rc-status -l}}  
|-
| To change to a different <runlevel> || {{ic|openrc <runlevel>}}
|-
| To add <s-runlevel> as a stacked <runlevel>  || {{ic|rc-update add -s <s-runlevel> <runlevel>}}
|}
 
== 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.


== Available Runlevels ==
The available runlevels are:
The available runlevels are:
* '''default''' - Used if no runlevel is specified. (This is generally the runlevel you want to add services to.)
* '''default''' - Used if no runlevel is specified. (This is generally the runlevel you want to add services to.)
* '''hotplugged'''
* '''hotplugged'''
* '''needed'''
* '''manual'''
* '''manual'''


The special runlevels are:
The special runlevels are:
* '''sysinit''' - Brings up any system specific stuff such as <code>/dev</code>, <code>/proc</code> and optionally <code>/sys</code> for Linux based systems. It also mounts <code>/lib/rc/init.d</code> as a ramdisk using tmpfs where available unless <code>/</code> is mounted rw at boot. <code>'''rc'''</code> uses <code>/lib/rc/init.d</code> to hold state information about the services it runs. sysinit always runs when the host first starts and should not be run again.
 
* '''sysinit''' - Brings up system specific stuff such as <code>/dev</code>, <code>/proc</code> and optionally <code>/sys</code> for Linux based systems. It also mounts <code>/lib/rc/init.d</code> as a ramdisk using tmpfs where available unless <code>/</code> is mounted rw at boot. <code>'''rc'''</code> uses <code>/lib/rc/init.d</code> 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.
* '''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.
* '''single''' - Stops all services except for those in the sysinit runlevel.
Line 28: Line 64:
* '''shutdown''' - Changes to the shutdown runlevel and then halts the host.
* '''shutdown''' - Changes to the shutdown runlevel and then halts the host.


== rc-update usage ==
=== Stacked runlevels ===
  Usage: rc-update [options] add service <runlevel>
 
        rc-update [options] del service <runlevel>
Runlevel "inheritance" is acheived through [https://docs.alpinelinux.org/user-handbook/0.1a/Working/openrc.html#_runlevel_stacking runlevel stacking]. For more detailed information, refer [https://wiki.gentoo.org/wiki/OpenRC/Stacked%20runlevel Gentoo wiki].
        rc-update [options] show
 
== Configuration ==
  Options: [suChqv]
 
  -s, --stack                      Stack a runlevel instead of a service
System-wide configuration file for OpenRC is {{Path|/etc/rc.conf}}.
  -u, --update                      Force an update of the dependency tree
 
If the setting {{Codeline|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|preventing slow services]] from delaying boot section.
 
=== cgroups v2 ===
 
Since {{pkg|openrc}} 0.51 cgroups v2, or "unified", is the default. You can enable hybrid cgroups v1 & v2 by editing {{Path|/etc/rc.conf}} and setting <code>rc_cgroup_mode="hybrid"</code>.
 
Then you should run {{Cmd|# rc-service cgroups start}}
to take effect and {{Cmd|# rc-update add cgroups}}
to auto mount the cgroup filesystem on boot.
 
== User services ==
 
{{Note|Currently User services are experimental and available in [[Repositories#Edge|edge]] version only}}
 
OpenRC supports managing services for users. The [[XDG_RUNTIME_DIR]] variable must be set before calling {{Codeline|openrc --user}}, as it's used to store state for openrc itself and the services it runs.
 
For many [[wayland]] user services, a custom GUI runlevel needs to be created.
The below example shows how this is done for {{pkg|wlsunset}} package. wlsunset provides an OpenRC user services.
In order to use this service, the following steps need to be performed:
 
In order to create this runlevel,run the following commands:{{Cmd|$ mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/rc/runlevels/gui
$ rc-update --user --stack add default gui}}
 
Enable the wlsunset service by adding it to this runlevel:{{Cmd|$ rc-update add -U wlsunset gui}}
 
Next, allow propagation of the WAYLAND_DISPLAY environment variable by adding the following lines to file {{path|${XDG_CONFIG_HOME:-$HOME/.config}/rc/rc.conf}} as follows:{{Cat|rc.conf|<nowiki>rc_env_allow="WAYLAND_DISPLAY"</nowiki>}}
 
Finally, make sure to start services from the GUI runlevel from your Wayland compositor in its configuration file (i.e. after WAYLAND_DISPLAY is set) by
adding the following lines to the startup file of your compositor.
openrc --user gui
 
== Preventing slow services from delaying boot ==
 
Services that take a while to start will block the boot process until they complete. E.g.: <code>iwd</code> and <code>networking</code> might delay startup of an interactive system rather than start in the background.
 
This can be remedied as per Patrycja's blog post titled [https://ptrcnull.me/posts/openrc-async-services/ OpenRC: Start services after login prompt]. This solution makes use of [[#Stacked runlevels|stacked runlevels]].
 
{{Warning|If the file {{Path|'''/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) {{Cmd|# mkdir /etc/runlevels/async}}
 
* Add default as a stacked runlevel {{Cmd|# rc-update add -s default async}}
 
* Remove slow services from default and add them to async {{Cmd|<nowiki># rc-update del chronyd
# rc-update add chronyd async </nowiki>}}
 
* Add changing of runlevel to async by adding the line '''::once:/sbin/openrc async''' to {{Path|/etc/inittab}} file as follows: {{Cat|/etc/inittab|<nowiki>...
::wait:/sbin/openrc default
::once:/sbin/openrc async -q
 
# Set up a couple of getty's
tty1::respawn:/sbin/getty 38400 tty1
...</nowiki>}}
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 ==
 
  Usage: rc-update [options] add <service> [<runlevel>...]
  or: rc-update [options] del <service> [<runlevel>...]
  or: rc-update [options] [show [<runlevel>...]]
  Options: [ asuChqVv ]
  -a, --all                        Process all runlevels
  -s, --stack                      Stack a runlevel instead of a service
  -u, --update                      Force an update of the dependency tree
  -h, --help                        Display this help output
  -C, --nocolor                    Disable color output
  -V, --version                    Display software version
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly (repeat to suppress errors)
 
Usage: rc-status [options] [-f ini] <runlevel>...
  or: rc-status [options] [-f ini] [-a | -c | -l | -m | -r | -s | -u]
Options: [ acf:lmrsSuChqVv ]
  -a, --all                        Show services from all run levels
  -c, --crashed                    Show crashed services
  -f, --format <arg>                format status to be parsable (currently arg must be ini)
  -l, --list                        Show list of run levels
  -m, --manual                      Show manually started services
  -r, --runlevel                    Show the name of the current runlevel
  -s, --servicelist                Show service list
  -S, --supervised                  show supervised services
  -u, --unused                      Show services not assigned to any runlevel
  -h, --help                        Display this help output
  -C, --nocolor                    Disable color output
  -V, --version                    Display software version
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly (repeat to suppress errors)
 
Usage: rc-service [options] [-i] <service> <cmd>...
  or: rc-service [options] -e <service>
  or: rc-service [options] -l
  or: rc-service [options] -r <service>
Options: [ cdDe:ilr:INsSZChqVv ]
  -d, --debug                      set xtrace when running the command
  -D, --nodeps                      ignore dependencies
  -e, --exists <arg>                tests if the service exists or not
  -c, --ifcrashed                  if the service is crashed run the command
  -i, --ifexists                    if the service exists run the command
  -I, --ifinactive                  if the service is inactive run the command
  -N, --ifnotstarted                if the service is not started run the command
  -s, --ifstarted                  if the service is started run the command
  -S, --ifstopped                  if the service is stopped run the command
  -l, --list                        list all available services
  -r, --resolve <arg>              resolve the service name to an init script
  -Z, --dry-run                    dry run (show what would happen)
  -h, --help                        Display this help output
  -C, --nocolor                    Disable color output
  -V, --version                    Display software version
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly (repeat to suppress errors)
 
Usage: openrc [options] [<runlevel>]
Options: [ a:no:s:SChqVv ]
  -n, --no-stop                    do not stop any services
  -o, --override <arg>              override the next runlevel to change into
                                    when leaving single user or boot runlevels
  -s, --service <arg>              runs the service specified with the rest
                                    of the arguments
  -S, --sys                        output the RC system type, if any
   -h, --help                        Display this help output
   -h, --help                        Display this help output
   -C, --nocolor                    Disable color output
   -C, --nocolor                    Disable color output
  -V, --version                    Display software version
   -v, --verbose                    Run verbosely
   -v, --verbose                    Run verbosely
   -q, --quiet                      Run quietly
   -q, --quiet                      Run quietly (repeat to suppress errors)
 
  Reboot/Halt/Poweroff: (And their equivalent from traditional GNU/Linux systems):{{Cmd|<nowiki># reboot  # ⇔ shutdown now -r
# halt    # ⇔ shutdown now -H
# poweroff # ⇔ shutdown now -P</nowiki>}}
 
== Troubleshooting ==
 
Whenever a openRC service fails to run, to troubleshoot enable debugging option and run the command.
 
For example, if running the command {{ic|rc-service greetd start}} causes greetd service to immediately crash, then alter the command to enable and view the debug:{{Cmd|# rc-service -d greetd restart}}
 
== See also ==


== rc-status usage ==
* [[Writing Init Scripts]]
Usage: rc-status [options] [runlevel1] [runlevel2] ...
* [[Multiple Instances of Services]]
* [https://github.com/OpenRC/openrc/blob/master/user-guide.md OpenRC user Guide]
Options: [aclrsuChqv]
* [https://github.com/OpenRC/openrc/blob/master/service-script-guide.md OpenRC Service Script Writing Guide]
  -a, --all                        Show services from all run levels
* [https://wiki.gentoo.org/wiki/OpenRC Gentoo Wiki]
  -c, --crashed                    Show crashed services
* [https://wiki.archlinux.org/title/OpenRC ArchWiki]
  -l, --list                        Show list of run levels
* [https://wiki.postmarketos.org/wiki/OpenRC PostmarketOS Wiki]
  -r, --runlevel                    Show the name of the current runlevel
* [https://ptrcnull.me/posts/openrc-async-services/ Start services after login prompt]
  -s, --servicelist                Show service list
 
  -u, --unused                      Show services not assigned to any runlevel
[[Category:Booting]]
  -h, --help                        Display this help output
[[Category:System Administration]]
  -C, --nocolor                    Disable color output
[[Category:Services]]
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly

Latest revision as of 17:39, 25 March 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>
To add <s-runlevel> as a stacked <runlevel> rc-update add -s <s-runlevel> <runlevel>

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 v2

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

Note: Currently User services are experimental and available in edge version only

OpenRC supports managing services for users. The XDG_RUNTIME_DIR variable must be set before calling openrc --user, as it's used to store state for openrc itself and the services it runs.

For many wayland user services, a custom GUI runlevel needs to be created. The below example shows how this is done for wlsunset package. wlsunset provides an OpenRC user services. In order to use this service, the following steps need to be performed:

In order to create this runlevel,run the following commands:

$ mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/rc/runlevels/gui $ rc-update --user --stack add default gui

Enable the wlsunset service by adding it to this runlevel:

$ rc-update add -U wlsunset gui

Next, 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"

Finally, make sure to start services from the GUI runlevel from your Wayland compositor in its configuration file (i.e. after WAYLAND_DISPLAY is set) by adding the following lines to the startup file of your compositor. openrc --user gui

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

Usage: rc-update [options] add <service> [<runlevel>...]
  or: rc-update [options] del <service> [<runlevel>...]
  or: rc-update [options] [show [<runlevel>...]]
Options: [ asuChqVv ]
 -a, --all                         Process all runlevels
 -s, --stack                       Stack a runlevel instead of a service
 -u, --update                      Force an update of the dependency tree
 -h, --help                        Display this help output
 -C, --nocolor                     Disable color output
 -V, --version                     Display software version
 -v, --verbose                     Run verbosely
 -q, --quiet                       Run quietly (repeat to suppress errors)
Usage: rc-status [options] [-f ini] <runlevel>...
  or: rc-status [options] [-f ini] [-a | -c | -l | -m | -r | -s | -u]
Options: [ acf:lmrsSuChqVv ]
 -a, --all                         Show services from all run levels
 -c, --crashed                     Show crashed services
 -f, --format <arg>                format status to be parsable (currently arg must be ini)
 -l, --list                        Show list of run levels
 -m, --manual                      Show manually started services
 -r, --runlevel                    Show the name of the current runlevel
 -s, --servicelist                 Show service list
 -S, --supervised                  show supervised services
 -u, --unused                      Show services not assigned to any runlevel
 -h, --help                        Display this help output
 -C, --nocolor                     Disable color output
 -V, --version                     Display software version
 -v, --verbose                     Run verbosely
 -q, --quiet                       Run quietly (repeat to suppress errors)
Usage: rc-service [options] [-i] <service> <cmd>...
  or: rc-service [options] -e <service>
  or: rc-service [options] -l
  or: rc-service [options] -r <service>
Options: [ cdDe:ilr:INsSZChqVv ]
 -d, --debug                       set xtrace when running the command
 -D, --nodeps                      ignore dependencies
 -e, --exists <arg>                tests if the service exists or not
 -c, --ifcrashed                   if the service is crashed run the command
 -i, --ifexists                    if the service exists run the command
 -I, --ifinactive                  if the service is inactive run the command
 -N, --ifnotstarted                if the service is not started run the command
 -s, --ifstarted                   if the service is started run the command
 -S, --ifstopped                   if the service is stopped run the command
 -l, --list                        list all available services
 -r, --resolve <arg>               resolve the service name to an init script
 -Z, --dry-run                     dry run (show what would happen)
 -h, --help                        Display this help output
 -C, --nocolor                     Disable color output
 -V, --version                     Display software version
 -v, --verbose                     Run verbosely
 -q, --quiet                       Run quietly (repeat to suppress errors)
Usage: openrc [options] [<runlevel>]
Options: [ a:no:s:SChqVv ]
  -n, --no-stop                     do not stop any services
  -o, --override <arg>              override the next runlevel to change into
                                   when leaving single user or boot runlevels
  -s, --service <arg>               runs the service specified with the rest
                                   of the arguments
  -S, --sys                         output the RC system type, if any
  -h, --help                        Display this help output
  -C, --nocolor                     Disable color output
  -V, --version                     Display software version
  -v, --verbose                     Run verbosely
  -q, --quiet                       Run quietly (repeat to suppress errors) 

Reboot/Halt/Poweroff: (And their equivalent from traditional GNU/Linux systems):

# 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

See also