OpenRC

出自Alpine Linux
(重定向自Alpine Linux Init System

Alpine Linux 使用 openrc 作为其 init 系统。init 系统管理计算机的服务、启动和关机。

请参考 Alpine Linux 文档项目中出色的 OpenRC 使用指南,以快速学习基础知识。更多高级信息,请参考编写 Init 脚本服务的多个实例页面。

快速入门

操作 命令 备注
管理服务 - 启动、停止和重启
立即启动 <服务名称> rc-service <服务名称> start
立即停止 <服务名称> rc-service <服务名称> stop
立即重启 <服务名称> rc-service <服务名称> restart
在运行级别中添加和删除服务
将 <服务名称> 添加到 <运行级别> rc-update add <服务名称> <运行级别>
从 <运行级别> 中删除 <服务名称> rc-update del <服务名称> <运行级别>
检查运行级别中的服务及其状态
检查 <服务名称> 的状态 rc-service <服务名称> status
查看在 <运行级别> 中配置的服务 rc-update show <运行级别>
查看当前活动的运行级别和服务状态 rc-status
检查和管理运行级别
查看可用的运行级别 rc-status -l
切换到不同的 <运行级别> openrc <运行级别>
将 <堆叠运行级别> 添加为堆叠的 <运行级别> rc-update add -s <堆叠运行级别> <运行级别>

运行级别

运行级别基本上是需要启动的服务集合。它们使用名称而不是随机数字,用户可以根据需要创建自己的运行级别。默认启动按顺序使用 sysinit、boot 和 default 运行级别。关机使用 shutdown 运行级别。

可用的运行级别有

  • default - 如果未指定运行级别,则使用此级别。(通常,您希望将服务添加到此运行级别。)
  • hotplugged
  • manual

特殊的运行级别有

  • sysinit - 启动系统特定的内容,例如 Linux 系统的 /dev/proc,以及可选的 /sys。它还在 tmpfs 可用时将 /lib/rc/init.d 挂载为 ramdisk,除非 / 在启动时以 rw 方式挂载。rc 使用 /lib/rc/init.d 来保存其运行服务的状态信息。sysinit 始终在主机首次启动时运行,不应再次运行。
  • boot - 通常,您应该添加到 boot 运行级别的服务是那些处理文件系统挂载、设置连接外围设备的初始状态和日志记录的服务。热插拔服务由系统添加到 boot 运行级别。boot 和 sysinit 运行级别中的所有服务都自动包含在所有其他运行级别中,但此处列出的运行级别除外。
  • single - 停止除 sysinit 运行级别中的服务之外的所有服务。
  • reboot - 更改为 shutdown 运行级别,然后重启主机。
  • shutdown - 更改为 shutdown 运行级别,然后停止主机。

堆叠运行级别

运行级别“继承”是通过运行级别堆叠实现的。有关更多详细信息,请参考Gentoo wiki

配置

OpenRC 的系统级配置文件是 /etc/rc.conf

如果配置了 rc_parallel="YES" 设置,OpenRC 系统会尝试并行启动服务,以略微提高速度。但是,此设置附带来自 OpenRC 开发人员的消息

警告:虽然我们改进了并行处理,但它仍然可能锁定启动过程。请勿提交关于此问题的错误报告。


为了提高启动时间,请考虑防止慢速服务延迟启动部分中建议的想法。

cgroups v2

openrc 0.51 起,cgroups v2 或“unified”是默认设置。您可以通过编辑 /etc/rc.conf 并设置 rc_cgroup_mode="hybrid" 来启用混合 cgroups v1 和 v2。

然后您应该运行

# rc-service cgroups start

以使其生效,并运行

# rc-update add cgroups

以在启动时自动挂载 cgroup 文件系统。

用户服务

注意:当前用户服务仅在 edge 版本中可用

OpenRC 支持管理用户服务。必须在调用 openrc --user 之前设置 XDG_RUNTIME_DIR 变量,因为它用于存储 openrc 自身及其运行服务的状态。

对于许多 wayland 用户服务,需要创建一个自定义的 GUI 运行级别。以下示例展示了如何为 wlsunset 软件包完成此操作。wlsunset 提供 OpenRC 用户服务。为了使用此服务,需要执行以下步骤

为了创建此运行级别,请运行以下命令

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

通过将 wlsunset 服务添加到此运行级别来启用它

$ rc-update add -U wlsunset gui

接下来,通过将以下行添加到文件 ${XDG_CONFIG_HOME:-$HOME/.config}/rc/rc.conf 来允许 WAYLAND_DISPLAY 环境变量的传播,如下所示

内容 rc.conf

rc_env_allow="WAYLAND_DISPLAY"

防止慢速服务延迟启动

启动时间较长的服务会阻止启动过程,直到它们完成。例如:iwdnetworking 可能会延迟交互系统的启动,而不是在后台启动。

可以按照 Patrycja 的博客文章 OpenRC:在登录提示后启动服务 中的方法来解决此问题。此解决方案利用了堆叠运行级别

警告:如果错误地编辑了文件 /etc/inittab,系统可能无法启动。在继续操作之前,请备份并学习如何使用救援磁盘还原。


  • 创建一个自定义运行级别(此处名称为“async”,但无关紧要)

    # mkdir /etc/runlevels/async

  • 将 default 添加为堆叠运行级别

    # rc-update add -s default async

  • 从 default 中删除慢速服务,并将它们添加到 async

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

  • 通过将行 ::once:/sbin/openrc async 添加到 /etc/inittab 文件中,将运行级别更改为 async,如下所示

    内容 /etc/inittab

    ... ::wait:/sbin/openrc default ::once:/sbin/openrc async -q # 设置几个 getty tty1::respawn:/sbin/getty 38400 tty1 ...

重启后,来自 async 的服务将单独启动。此更改不影响从 Default 运行级别启动的其他服务,并且由于 wait 标签,它们可能仍然阻止 agetty 运行。

命令用法

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) 

重启/停止/关机:(以及来自传统 GNU/Linux 系统的等效命令)

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


参见