OpenRC: Difference between revisions

From Alpine Linux
mNo edit summary
(added warning and removed the content tag to avoid potential emptying of inittab file by new users)
 
(19 intermediate revisions by 15 users not shown)
Line 1: Line 1:
Alpine Linux uses {{pkg|openrc}} for its init system. 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.
== Quick-Start Information ==
== Quick-Start Information ==
Alpine Linux uses [http://roy.marples.name/projects/openrc OpenRC] for its init system.


The following commands are available to manage the init system:
The following commands are available to manage the init system:
* {{Cmd|rc-update add service <runlevel>}}
* Basics:
* {{Cmd|rc-update del service <runlevel>}}
{{Cmd|# rc-update add <service> <runlevel>}}
* {{Cmd|rc-service <service> <start stop restart>}}
{{Cmd|# rc-update del <service> <runlevel>}}
: Equivalent to <code>/etc/init.d/service <start stop restart></code>
{{Cmd|# rc-service <service> <start stop restart> # ⇔ /etc/init.d/service <start stop restart>}}
* {{Cmd|rc-status}}
 
: To check services and their set runlevels
* To check services and their set runlevels:
* {{Cmd|rc <runlevel>}}
{{Cmd|rc-status}}
: To change to a different runlevel
 
* {{Cmd|reboot}}
* To change to a different runlevel:
: Equivalent to <code>shutdown -r now</code> from traditional GNU/Linux systems
{{Cmd|# openrc <runlevel>}}
* {{Cmd|halt}}
: Equivalent to <code>shutdown -h now</code> from traditional GNU/Linux systems
* {{Cmd|poweroff}}
: To turn off the machine


{{Tip|Prior to Alpine Linux 2.0.0, you might need to use the following commands instead: ''rc_add, rc_delete,'' and ''rc_status''.}}
* Reboot/Halt/Poweroff: (And their equivalent from traditional GNU/Linux systems)
{{Cmd|# reboot  # ⇔ shutdown now -r}}
{{Cmd|# halt    # ⇔ shutdown now -H}}
{{Cmd|# poweroff # ⇔ shutdown now -P}}


== Available Runlevels ==
== Available Runlevels ==
Line 24: Line 24:
* '''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 34: Line 33:
* '''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 ==
== Preventing slow services from delaying boot ==
Usage: rc-update [options] add service <runlevel>
 
        rc-update [options] del service <runlevel>
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.
        rc-update [options] show
 
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 [https://wiki.gentoo.org/wiki/OpenRC/Stacked%20runlevel stacked runlevels].
 
<br>
{{Warning| This solution requires editing the file '''/etc/inittab'''. If this file is edited wrongly, the system may not boot. Take adequate backup and gain necessary knowledge to edit files before proceeding.}}
* Create a custom runlevel (name is “async” here, but it doesn’t matter)
   
   
Options: [suChqv]
{{Cmd|# mkdir /etc/runlevels/async}}
  -s, --stack                      Stack a runlevel instead of a service
 
  -u, --update                      Force an update of the dependency tree
* Add default as a stacked runlevel
  -h, --help                        Display this help output
  -C, --nocolor                    Disable color output
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly


== rc-status usage ==
{{Cmd|# rc-update add -s default async}}
Usage: rc-status [options] [runlevel1] [runlevel2] ...
 
* Remove slow services from default and add them to async
Options: [aclrsuChqv]
 
  -a, --all                        Show services from all run levels
{{Cmd|# rc-update del chronyd}}
  -c, --crashed                    Show crashed services
{{Cmd|# rc-update add chronyd async }}
  -l, --list                        Show list of run levels
 
  -r, --runlevel                   Show the name of the current runlevel
* Add changing of runlevel to async by adding the line '''::once:/sbin/openrc async'''  to /etc/inittab file:
  -s, --servicelist                Show service list
 
  -u, --unused                      Show services not assigned to any runlevel
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.
  -h, --help                        Display this help output
 
  -C, --nocolor                    Disable color output
== cgroups v2 ==
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly


== rc-service usage ==
Since {{pkg|openrc}} 0.51 cgroups v2, or "unified", is the default.
Usage: rc-service [options]
You can enable hybrid cgroups v1 & v2 by editing <code>/etc/rc.conf</code> and setting <code>rc_cgroup_mode="hybrid"</code>.
Options: [e:ilr:ChqVv]
  -e, --exists <arg>                tests if the service exists or not
  -i, --ifexists                    if the service exists then run the command
  -l, --list                        list all available services
  -r, --resolve <arg>               resolve the service name to an init script
  -h, --help                        Display this help output
  -C, --nocolor                    Disable color output
  -V, --version                    Display software version
  -v, --verbose                    Run verbosely
  -q, --quiet                      Run quietly


== rc usage ==
Then you should run
  Usage: rc [options]
  {{Cmd|# rc-service cgroups start}}
to take effect
Options: [a:o:s:SChqVv]
  -a, --applet <arg>                runs the applet specified by the next argument
  -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


and
{{Cmd|# rc-update add cgroups}}
to auto mount the cgroup filesystem on boot


[[Category:Booting]]
== See also ==
* [https://github.com/OpenRC/openrc/blob/master/user-guide.md OpenRC user Guide]
* [https://github.com/OpenRC/openrc/blob/master/service-script-guide.md OpenRC Service Script Writing Guide]
* [https://wiki.gentoo.org/wiki/OpenRC Gentoo Wiki]
* [https://wiki.archlinux.org/title/OpenRC ArchWiki]
* [https://wiki.postmarketos.org/wiki/OpenRC PostmarketOS Wiki]
* [https://ptrcnull.me/posts/openrc-async-services/ Start services after login prompt]
[[Category:Booting]]
[[Category:System Administration]]
[[Category:Services]]

Latest revision as of 04:34, 25 September 2024

Alpine Linux uses openrc for its init system. Refer to the excellent guide working with OpenRC from Alpine Linux documentation project to learn the basics.

Quick-Start Information

The following commands are available to manage the init system:

  • Basics:

# rc-update add <service> <runlevel>

# rc-update del <service> <runlevel>

# rc-service <service> <start stop restart> # ⇔ /etc/init.d/service <start stop restart>

  • To check services and their set runlevels:

rc-status

  • To change to a different runlevel:

# openrc <runlevel>

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

# reboot # ⇔ shutdown now -r

# halt # ⇔ shutdown now -H

# poweroff # ⇔ shutdown now -P

Available Runlevels

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.

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: This solution requires editing the file /etc/inittab. If this file is edited wrongly, the system may not boot. Take adequate backup and gain necessary knowledge to edit files 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:

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

See also