Setting up OpenRC init system
Overview
Section titled “Overview”The most popular init system on Linux distributions today is systemd. Besides being just an init system, systemd provides many additional components, which are intended to replace various common Linux services, such as syslog, cron, etc. This is why some people consider systemd design as too complex and incompatible with the Unix philosophy.
Alpaquita Linux, in contrast, uses OpenRC, a more lightweight init system that works with the init program /usr/sbin/init provided by BusyBox. Unlike systemd, OpenRC does not tend to replace common Linux services. In Alpaquita, some functionality that is normally covered by systemd in systemd-based distributions is covered by separate utilities.
The document provides a quick overview of some OpenRC init system features in Alpaquita Linux and includes examples of how to perform the tasks that you perform in a systemd-based OS distribution.
Basic commands
Section titled “Basic commands”You can manage the OpenRC service using one of the following commands:
rc-service <service> <cmd>/etc/init.d/<service> <cmd>
For example:
rc-service sshd reload/etc/init.d/sshd reloadThe following table describes basic OpenRC commands and their systemd alternatives:
| Command | OpenRC | systemd |
|---|---|---|
| Start service | rc-service <service> start | systemctl start <service> |
| Stop service | rc-service <service> stop | systemctl stop <service> |
| Restart service | rc-service <service> restart | systemctl restart <service> |
| Reload service | rc-service <service> reload | systemctl reload <service> |
| Show service status | rc-service <service> status | systemctl status <service> |
| Enable service at startup | rc-update add <service> | systemctl enable <service> |
| Disable service at startup | rc-update del <service> | systemctl disable <service> |
| List enabled services | rc-update | systemctl list-unit-files --state enabled |
| List all services | rc-status -s | systemctl list-unit-files |
For more information, see the following documentation:
Configuration files
Section titled “Configuration files”/etc/inittab
Section titled “/etc/inittab”This is a configuration file for the /usr/sbin/init program from BusyBox. It is basically used to start OpenRC or getty processes on specific ttys depending on the requested action.
For more information, see /etc/inittab init(8) configuration for BusyBox.
/etc/rc.conf
Section titled “/etc/rc.conf”This is the global OpenRC configuration file. In OpenRC, configuration files are simply shell scripts, therefore you do not need to learn a special syntax as in systemd.
/etc/conf.d
Section titled “/etc/conf.d”This directory contains configuration files for individual OpenRC init scripts. It somewhat corresponds to /etc/systemd/ in systemd-based distributions.
Logging
Section titled “Logging”Messages that you normally observe by invoking journalctl under systemd, you can find in the following files when using OpenRC:
/var/log/rc.log/var/log/dmesg/var/log/messages
/var/log/rc.log
Section titled “/var/log/rc.log”This file is for OpenRC specific logging that is disabled by default. To enable it, add the following lines to /etc/rc.conf:
rc_logger="YES" # enable loggingrc_quiet_openrc="NO" # disable quiet mode/var/log/dmesg
Section titled “/var/log/dmesg”This file contains the initial dmesg log. It is populated by the OpenRC service bootmisc. This log is useful in case of long uptime of your system when initial kernel messages have been flushed.
/var/log/messages
Section titled “/var/log/messages”This file contains system log messages. Unlike systemd, OpenRC does not provide any utilities for system logging. In Alpaquita, BusyBox services syslogd and klogd are used for system logging.
Logging kernel messages
Section titled “Logging kernel messages”By default, klogd is disabled and kernel messages are not logged to /var/log/messages. To enable and start it by running the following commands:
rc-update add klogd bootrc-service klogd startLog rotation
Section titled “Log rotation”systemd provides the systemd-journald service for system logging and a wide range of options for log rotation. syslogd provides the following two options:
-s SIZE Max size (KB) before rotation (default 200KB, 0=off)-b N N rotated logs to keep (default 1, max 99, 0=purge)The options can be adjusted in /etc/conf.d/syslog file. For example, you can set the following options for syslogd.
SYSLOGD_OPTS="-s 1024 -b 4"For more sophisticated log rotation, consider installing the logrotate package.
Log rules
Section titled “Log rules”journalctl helps you filter logs in many different ways (by service, priority, facility, and so on). Such powerful filtering is possible, because the systemd-journald service stores logs in binary format. syslogd stores logs as plain text. For plain text files, you can use grep, but you can also set up log rules in /etc/syslog.conf for different types of messages to be logged in different files as in the following example.
cron.* /var/log/cron #all messages of cron facility*.err /var/log/error #all messages with priorities less than or equal to errFor more information, see syslog.conf.txt.
Writing init scripts
Section titled “Writing init scripts”OpenRC uses init shell scripts to run services rather than special service configuration files. An init script must start with a shebang #!/usr/sbin/openrc-run. The init script code is expected to be compatible with POSIX shell.
Below you can see an example of an init script that moves all tasks to a cgroup with specific CPUs and memory nodes assigned to it:
#!/usr/sbin/openrc-run
: ${CPUS:="0"}: ${MEMS:="0"}# $RC_SVCNAME is a name of the service. By default, it equals a script name.: ${CGROUP_NAME:=$RC_SVCNAME}: ${CGROUP_CPUSET_PATH="/sys/fs/cgroup/cpuset"}
# This function specifies dependency information which is needed to start and# stop the service in the right order in relation to other services.depend(){ # The service must start after the 'cgroups' service. after cgroups # Don't stop the service when shutting the system down. keyword -shutdown}
# This function contains commands necessary to start the service. Functions# 'ebegin', 'eend', 'einfo' are defined by openrc-run and used for logging.start(){ cd "$CGROUP_CPUSET_PATH"
ebegin "Creating cgroup $CGROUP_NAME" mkdir "$CGROUP_NAME" eend $? || return $?
echo "$MEMS" > "$CGROUP_NAME/cpuset.mems" echo "$CPUS" > "$CGROUP_NAME/cpuset.cpus"
einfo "Moving all tasks to cgroup $CGROUP_NAME" cat tasks | xargs -n1 echo >"$CGROUP_NAME/tasks" 2>/dev/null
return 0}
# This function contains commands necessary to stop the service.stop(){ cd "$CGROUP_CPUSET_PATH"
einfo "Removing all tasks from cgroup $CGROUP_NAME" cat "$CGROUP_NAME/tasks" | xargs -n1 echo >tasks 2>/dev/null
ebegin "Removing cgroup $CGROUP_NAME" rmdir "$CGROUP_NAME" eend $? || return $?}If you save this script to /etc/init.d/isoltasks, you can use it as a regular service.
# /etc/init.d/isoltasks start * Creating cgroup isoltasks ... [ ok ] * Moving all tasks to cgroup isoltasks# /etc/init.d/isoltasks stop * Removing all tasks from cgroup isoltasks * Removing cgroup isoltasks ... [ ok ]For more information about built-in functions and environment variables used in init scripts, see man openrc-run (provided by package openrc-doc).
See also:
Mounting filesystems
Section titled “Mounting filesystems”To mount nfs targets, install the nfs-utils package first.
apk add nfs-utilsOpenRC does not automatically mount nfs targets specified in /etc/fstab. Enable the netmount service for that.
rc-update add netmount defaultUnder systemd, /tmp is automatically mounted as tmpfs. In Alpaquita, to mount /tmp as tmpfs, configure the /etc/fstab as in the following example:
tmpfs /tmp tmpfs rw,nosuid,noatime,nodev,size=1G,mode=1777 0 0Console font
Section titled “Console font”A console font is set by the OpenRC service consolefont. Before using it, install some fonts first, for example:
apk add terminus-fontTo change the default font, set the consolefont variable in /etc/conf.d/consolefont to the name of a file found in /usr/share/consolefonts/, for instance:
consolefont="ter-v20b.psf.gz"The consolefont service is not enabled by default. To enable and start it, run the following commands:
rc-update add consolefont bootrc-service consolefont startTo preview the font, use utility setfont.
setfont ter-v20b.psf.gzgetty on the serial console
Section titled “getty on the serial console”Under systemd, it is enough to specify console=ttyS0 in the kernel command line and systemd automatically starts a getty for it. In Alpaquita, additional configuration is needed. Use /usr/sbin/init or OpenRC for it.
/usr/sbin/init
Section titled “/usr/sbin/init”To enable getty on the serial console using /usr/sbin/init, uncomment or add this line to /etc/inittab:
ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100For the changes to take effect without a reboot, reload the init process /etc/inittab. Use the following command:
kill -HUP 1OpenRC
Section titled “OpenRC”To enable and start getty on the serial console using OpenRC, set up the agetty.ttyS0 service.
apk add agettyln -s /etc/init.d/agetty /etc/init.d/agetty.ttyS0rc-update add agetty.ttyS0 bootrc-service agetty.ttyS0 startYou can edit agetty options in the /etc/conf.d/agetty.ttyS0 file. See the /etc/conf.d/agetty file for option description and examples.
Single-user mode
Section titled “Single-user mode”If you need to do something that is not possible at the default OpenRC runlevel, you can try to do it in the single-user mode (also known as the rescue mode). You can enter the single-user mode at different stages of a booting process, namely:
- initramfs (dracut)
- /usr/sbin/init
- OpenRC
initramfs (dracut)
Section titled “initramfs (dracut)”Alpaquita uses dracut for creating initramfs images. dracut provides the rd.break kernel command line option that lets you drop to a shell right before switching to a root filesystem mounted on /sysroot. If you exit from the shell, the booting process continues as usual.
It is also possible to drop to a shell at earlier stages. For more information, see man dracut.cmdline (provided by the dracut-doc package).
/usr/sbin/init
Section titled “/usr/sbin/init”To enable single-user mode provided by /usr/sbin/init, add single to the kernel command line. In this case, /etc/inittab configuration file is ignored and you drop to a shell right after switching to a root filesystem, but before any OpenRC service starts.
If you want to get to the default runlevel from this stage, run the following commands:
openrc sysinitopenrc defaultkill -HUP 1 && exitOpenRC
Section titled “OpenRC”Single-user mode provided by OpenRC is a special runlevel called single. When you start it, all services except those in the sysinit runlevel are stopped.
To get to the single runlevel from the default runlevel, run the following command:
openrc singleTo get back to the default runlevel, run the command as in the next example:
openrc defaultFor more information, see man openrc (provided by the openrc-doc package).