OpenRC Runlevels
OpenRC Runlevels define system states in Alpine Linux, managing services startup and shutdown through predefined init levels.
OpenRC Runlevels represent a mechanism within the OpenRC init system used to group and manage sets of services according to different system states or modes. Analogous to traditional Unix runlevels, OpenRC runlevels define collections of services that are started or stopped together, allowing the system to transition between various operational states such as booting, shutting down, or entering multi-user modes. Each runlevel is essentially a directory containing symbolic links to service scripts, and the contents of these directories determine which services should be active when that runlevel is entered.
Default and Special Runlevels
OpenRC provides several predefined runlevels to cover common system states:
- default: The standard multi-user runlevel where services required during normal operation are started. This is the runlevel the system enters after boot.
- boot: Used during the early boot process to start essential services required before the system transitions to the default runlevel.
- shutdown: Activated during system shutdown, stopping services in an orderly fashion.
- sysinit: Used for system initialization tasks, such as mounting filesystems and setting up devices.
- nonetwork: A runlevel for multi-user mode without networking services enabled.
- single: A minimal runlevel typically used for administrative or recovery tasks; usually starts only essential services.
- manual: A runlevel that is empty by default and intended for manual service management.
Each of these runlevels corresponds to a directory under /etc/runlevels/ (or /etc/init.d/runlevels/ depending on configuration), where service symlinks are stored. When switching runlevels, OpenRC starts and stops services according to the differences between the current and target runlevel directories.
Custom and Stacked Runlevels
OpenRC allows administrators to create custom runlevels by simply creating new directories under /etc/runlevels/ and populating them with symlinks to the desired services. This flexibility enables tailoring system behavior to specific needs, such as specialized server roles, testing environments, or minimal service sets.
Additionally, OpenRC supports stacked runlevels, where multiple runlevels can be active simultaneously. This is achieved by stacking the service symlinks of each active runlevel, allowing services from each to run concurrently without conflict. For example, one could activate both a "default" runlevel and a custom "backup" runlevel to run backup-specific services alongside the normal system services.
Stacked runlevels are managed by manipulating symbolic links and service dependencies, and OpenRC provides commands to add or remove runlevels from the current stack without a full system reboot, thus enhancing flexibility and uptime.
Managing Runlevels
Runlevels in OpenRC are manipulated using the rc command, which allows starting, stopping, adding, or removing services and runlevels. Common operations include:
-
Changing the active runlevel:
rc defaultThis command switches to the "default" runlevel, starting and stopping services accordingly.
-
Adding a runlevel to the current stack:
rc add backup -
Removing a runlevel from the current stack:
rc del backup -
Listing services within a runlevel:
rc -l default
This management model enables granular control over which services run in which contexts and allows dynamic adjustments without rebooting.
Internal Structure and Operation
Each runlevel directory contains symbolic links to service scripts typically found in /etc/init.d/. The symlinks determine which services are enabled for that runlevel and in what order they start or stop. The naming convention of symlinks often includes a priority prefix to control service start order.
During system initialization or runlevel switching, OpenRC compares the services in the current runlevel to those in the target runlevel, starting new services and stopping those no longer needed. Dependency resolution ensures that services start in the correct order and that prerequisites are met.
The runlevel mechanism thus provides a modular, dependency-aware approach to service management that is simpler and more flexible than traditional SysV init scripts while retaining compatibility with existing init scripts.
Practical Example
To create a custom runlevel named maintenance designed to run only essential services plus a maintenance script, an administrator would:
-
Create the runlevel directory:
mkdir /etc/runlevels/maintenance -
Add symlinks to essential services and the maintenance script:
ln -s /etc/init.d/networking /etc/runlevels/maintenance/ ln -s /etc/init.d/sshd /etc/runlevels/maintenance/ ln -s /etc/init.d/maintenance_script /etc/runlevels/maintenance/ -
Switch to the new runlevel:
rc maintenance
This activates only the services linked in the maintenance runlevel, isolating maintenance activities from normal operation services.
Summary of Key Points
- OpenRC runlevels group services for specific system states.
- Default runlevels cover boot, normal operation, shutdown, and recovery.
- Custom runlevels can be created for tailored service sets.
- Stacked runlevels allow multiple runlevels to be active simultaneously.
- Runlevel directories contain symlinks to service scripts controlling which services start or stop.
- The
rcutility manages runlevels dynamically, handling dependencies and service order. - This system provides flexible, modular service management in Alpine Linux and other OpenRC-based distributions.