OpenRC

来自 阿尔派 Linux

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

请参考 阿尔派 Linux 文档项目中关于 working with OpenRC 的优秀指南 以快速学习基础知识。有关更高级的信息,请参考 Writing Init ScriptsMultiple instances of services 页面。

快速入门

操作 命令 备注
管理服务 - 启动、停止和重启
立即启动 <服务名称> 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。它还在可用时将 /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 或 “统一” 是默认设置。您可以通过编辑 /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: Start services after login prompt 中的建议进行补救。此解决方案利用了 堆叠运行级别

警告: 如果错误地编辑文件 /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 # Set up a couple of getty's 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


参见