OpenRC: Difference between revisions

From Alpine Linux
(added back Command usage section)
(updated Command usage section as both commands and options have changed)
Line 32: Line 32:
|-
|-
| To check status of  <example.service> || {{ic|rc-service <example.service> status}} ||  
| To check status of  <example.service> || {{ic|rc-service <example.service> status}} ||  
|-
| To view services configured at <runlevel>  || {{ic|rc-update show <runlevel>}} ||
|-
|-
| To view currently active runlevels and state of services  || {{ic|rc-status}} ||  
| To view currently active runlevels and state of services  || {{ic|rc-status}} ||  
Line 111: Line 113:
== Command usage ==
== Command usage ==


  Usage: rc-update [options] add service <runlevel>
  Usage: rc-update [options] add <service> [<runlevel>...]
        rc-update [options] del service <runlevel>
  or: rc-update [options] del <service> [<runlevel>...]
        rc-update [options] show
  or: rc-update [options] [show [<runlevel>...]]
  Options: [ asuChqVv ]
  Options: [suChqv]
  -a, --all                        Process all runlevels
  -s, --stack                      Stack a runlevel instead of a service
  -s, --stack                      Stack a runlevel instead of a service
  -u, --update                      Force an update of the dependency tree
  -u, --update                      Force an update of the dependency tree
  -h, --help                        Display this help output
  -h, --help                        Display this help output
  -C, --nocolor                    Disable color output
  -C, --nocolor                    Disable color output
  -v, --verbose                    Run verbosely
  -V, --version                    Display software version
  -q, --quiet                      Run quietly
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly (repeat to suppress errors)


  Usage: rc-status [options] [runlevel1] [runlevel2] ...
  Usage: rc-status [options] [-f ini] <runlevel>...
  or: rc-status [options] [-f ini] [-a | -c | -l | -m | -r | -s | -u]
  Options: [aclrsuChqv]
  Options: [ acf:lmrsSuChqVv ]
  -a, --all                        Show services from all run levels
  -a, --all                        Show services from all run levels
  -c, --crashed                    Show crashed services
  -c, --crashed                    Show crashed services
  -l, --list                        Show list of run levels
  -f, --format <arg>                format status to be parsable (currently arg must be ini)
  -r, --runlevel                    Show the name of the current runlevel
  -l, --list                        Show list of run levels
  -s, --servicelist                Show service list
  -m, --manual                      Show manually started services
  -u, --unused                      Show services not assigned to any runlevel
  -r, --runlevel                    Show the name of the current runlevel
  -h, --help                        Display this help output
  -s, --servicelist                Show service list
  -C, --nocolor                    Disable color output
  -S, --supervised                  show supervised services
  -v, --verbose                    Run verbosely
  -u, --unused                      Show services not assigned to any runlevel
  -q, --quiet                      Run quietly
  -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]
  Usage: rc-service [options] [-i] <service> <cmd>...
  or: rc-service [options] -e <service>
  Options: [e:ilr:ChqVv]
  or: rc-service [options] -l
  -e, --exists <arg>                tests if the service exists or not
  or: rc-service [options] -r <service>
  -i, --ifexists                    if the service exists then run the command
  Options: [ cdDe:ilr:INsSZChqVv ]
  -l, --list                        list all available services
  -d, --debug                      set xtrace when running the command
  -r, --resolve <arg>              resolve the service name to an init script
  -D, --nodeps                      ignore dependencies
  -h, --help                        Display this help output
  -e, --exists <arg>                tests if the service exists or not
  -C, --nocolor                    Disable color output
  -c, --ifcrashed                  if the service is crashed run the command
  -V, --version                    Display software version
  -i, --ifexists                    if the service exists run the command
  -v, --verbose                    Run verbosely
  -I, --ifinactive                  if the service is inactive run the command
  -q, --quiet                      Run quietly
  -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: rc [options]
  Usage: openrc [options] [<runlevel>]
  Options: [ a:no:s:SChqVv ]
  Options: [a:o:s:SChqVv]
   -n, --no-stop                    do not stop any services
   -a, --applet <arg>                runs the applet specified by the next argument
   -o, --override <arg>              override the next runlevel to change into
   -o, --override <arg>              override the next runlevel to change into
                                    when leaving single user or boot runlevels
                                    when leaving single user or boot runlevels
   -s, --service <arg>              runs the service specified with the rest
   -s, --service <arg>              runs the service specified with the rest
                                    of the arguments
                                    of the arguments
   -S, --sys                        output the RC system type, if any
   -S, --sys                        output the RC system type, if any
   -h, --help                        Display this help output
   -h, --help                        Display this help output
Line 163: Line 179:
   -V, --version                    Display software version
   -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)


== See also ==
== See also ==

Revision as of 17:01, 15 February 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 Remarks
Managing services - start,stop and restart
Start <example.service> now rc-service <example.service> start
Start <example.service> now rc-service <example.service> stop
Restart <example.service> now rc-service <example.service> restart
Adding and removing services from runlevels
Add <example.service> to <runlevel> rc-update add <example.service> <runlevel>
Remove <example.service> from <runlevel> rc-update del <example.service> <runlevel>
check services and their status
To check status of <example.service> rc-service <example.service> 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>


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

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

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.

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 runlevel.

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.

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

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.

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) 

See also