384. Init and runlevels
KDOS boots with System V init, sysvinit’s, as process 1. This page covers the file init reads, /etc/inittab, the runlevels it defines, and the tasks that go with them: finding the current runlevel, reloading the table after an edit, entering single-user and emergency mode, and shutting the machine down. Starting and stopping individual services is on Services. Every command on this page runs on a running KDOS machine, from a shell on a console or in the desktop’s terminal.
Installation
Init is part of every KDOS image and is never enabled or disabled; there is nothing to install. The pieces come from these ports and from the base filesystem:
- sysvinit installs
initand its other names,telinit,halt(also asrebootandpoweroff),shutdown,runlevelandkillall5, all in/sbin:
make -C src init halt shutdown runlevel killall5 VERSION=$version \
CFLAGS="$CFLAGS -I/usr/include/utmps" LDLIBS="-lutmps -lskarnet"
install -Dm755 src/init src/halt src/shutdown src/runlevel src/killall5 -t "$PKG/sbin"
ln -s init "$PKG/sbin/telinit"
ln -s halt "$PKG/sbin/reboot"
ln -s halt "$PKG/sbin/poweroff"
ports/core/base/sysvinit/build.sh, lines 12–18
- util-linux installs
agetty, which runs every console login, andsulogin, the single-user login. - kdos-tools installs
kdos-getty, a second name ofksvc, which thetty2entry runs first. - The base filesystem supplies
/usr/local/sbin/kdos-dm, which thetty1entry runs to start the display manager, SDDM, or a password prompt when SDDM cannot run; see Login and autologin. - utmps keeps the login records init writes; see Login records.
- The base filesystem,
fs/in the tree, supplies/etc/inittaband the scripts it names under/etc/init.d:rcS,rcK,killprocs,killpowerandserial-getty. The first bootstrap step copies them into the root; see The file system.
The live medium and an installed system run the same init with the same table, with one difference: the installer removes --autologin root from the serial console’s entry in the installed copy of /etc/inittab, so an installed system asks for a login on ttyS0 where the live medium logs root in with no password.
Configuration
/etc/inittab
The whole table, as the image ships it, from fs/etc/inittab:
# Runlevel 3 is the running system. S is single-user: sulogin on the
# console, no consoles from this file. 0 powers off and 6 reboots.
id:3:initdefault:
si::sysinit:/etc/init.d/rcS
# sulogin asks for root's password on the console. Leaving its shell
# continues to runlevel 3. The initramfs passes `single` from the kernel
# command line, and `telinit S` reaches it from a running system. -p makes
# the shell a login shell, which reads /etc/profile: init's own PATH does not
# reach /usr/local, where `kdos` and the other KDOS commands live.
~~:S:wait:/sbin/sulogin -p
# Local Console (QEMU Window / Physical) — the display manager.
#
# kdos-dm execs SDDM, whose greeter compositor takes tty1. It is started here
# and never from rcS: init reaches this line only once rcS has returned, and the
# splash stops drawing synchronously at its end (`kdos-splash handoff`), its
# settled frame left on screen for the compositor's first frame to replace, so
# the compositor's modeset never races the splash for the DRM master.
#
# A MISSING OR FAILING SDDM STILL GIVES A CONSOLE. sddm stays up across every
# login and logout, so a respawn here is sddm having exited; kdos-dm turns a
# missing binary, a missing account or three exits in a minute into a password
# prompt on tty1 through kdos-getty, never an autologin. Autologin is SDDM's
# alone, in /etc/sddm.conf.d/kde_settings.conf.
1:2345:respawn:/usr/local/sbin/kdos-dm
# Second console, a login prompt for any account. THE RECOVERY CONSOLE: it
# is a plain agetty with no --autologin, and nothing above may be allowed to
# make it one too, because it is what is left when the desktop on tty1 does
# not come up — a greeter SDDM keeps restarting included, which no respawn
# here ever sees. kdos-getty loads the console font and palette after fbcon's
# deferred take-over; doing that in rcS is futile, because the take-over resets
# every VT's font. agetty hands the name to /bin/login, util-linux's PAM login
# (/etc/pam.d/login, through system-login), so a console login is a session
# elogind knows and starts the user's services like any other.
#
# EVERY AGETTY HERE NAMES /etc/issue. agetty built against musl, which has no
# scandirat(), reads no issue file of its own accord, and without
# --issue-file the banner /etc/issue holds is never printed.
2:2345:respawn:/usr/local/sbin/kdos-getty tty2 /sbin/agetty --noclear --issue-file /etc/issue tty2 38400 linux
# Serial console. The live image logs root in with no password; the
# installer removes `--autologin root` from the installed copy, which then
# asks for a login. --keep-baud stays at the rate the kernel console set.
# serial-getty passes its arguments to agetty, and sleeps instead when no
# UART answers at ttyS0, where agetty would fail at every respawn.
S0:2345:respawn:/etc/init.d/serial-getty --autologin root --keep-baud --noclear --issue-file /etc/issue -L ttyS0 115200,38400,9600 vt102
ca::ctrlaltdel:/sbin/shutdown -r now
# Runlevels 0 and 6. init first sends TERM, and KILL 3 s later, to every
# entry above that the runlevel does not list (the consoles, and with tty1
# SDDM and every session under it), then runs these in order, each to
# completion. kc clears the console the screen shows and hides its cursor,
# so sysvinit's `shutdown`, which always broadcasts that the system is going
# down, leaves no text on the screen the desktop gave back. rcK stops the services while the filesystems they save to are
# writable; killprocs signals every process still alive; killpower may tell a
# UPS to cut the power, so every filesystem is read-only before it runs.
#
# k3 makes the live medium's persistence store read-only by name, before
# umount reaches it. The store is the root overlay's upper layer, and the
# overlay holds its superblock: umount -a -r detaches /mnt/persist without
# making it read-only, the overlay root refuses a read-only remount, and the
# store would be left writable at reboot with its journal to replay. With no
# store mounted there it does nothing.
kc:06:wait:/bin/sh -c 'printf "\033[H\033[2J\033[?25l" > /dev/tty0'
k0:06:wait:/etc/init.d/rcK
k1:06:wait:/etc/init.d/killprocs
k2:06:wait:/sbin/swapoff -a
k3:06:wait:/bin/sh -c '/bin/mountpoint -q /mnt/persist && exec /bin/mount -o remount,ro /mnt/persist'
k4:06:wait:/bin/umount -a -r
k5:06:wait:/etc/init.d/killpower
h0:0:wait:/sbin/halt -d -f -p
h6:6:wait:/sbin/reboot -d -f
Each line is id:runlevels:action:process. What each entry starts is tabulated in /etc/inittab; the actions and the order of a boot are explained in rcS and the service scripts. inittab(5), installed by sysvinit, is the format’s reference.
/etc/init.d/rcS
rcS is the sysinit entry. After mounting the filesystems it runs every numbered service script, and this is the loop that does it:
INIT_DIR="/etc/init.d"
DISABLED_DIR="/etc/service.disabled"
for _script in "${INIT_DIR}"/[0-9][0-9]_*.sh; do
[ -x "$_script" ] || continue
_name=$(basename "$_script" | sed 's/^[0-9][0-9]_//;s/\.sh$//')
if [ -f "${DISABLED_DIR}/${_name}" ]; then
echo "[KDOS] Skipping disabled service: ${_name}"
continue
fi
echo "[KDOS] Running: $(basename $_script) start"
splash step "$(echo "$_name" | tr '[:lower:]' '[:upper:]')"
# Redirect to a file and print it AFTERWARDS, never through a pipe. A
# `supervise` points the supervisor and the daemon at its own log
# forwarder, so a supervised service lets go of the script's output; any
# other process a script leaves running with that output open would hold
# a pipe's write end, a `| tee` would never see EOF, and the boot would
# stop at that service. A file has no reader to wait on.
#
# Every message still reaches the console and the serial line, which is
# the only way to debug a machine that dies behind a splash — just after
# the step rather than streamed during it.
_log="/run/kdos-init.${_name}.log"
"$_script" start >"$_log" 2>&1
_rc=$?
cat "$_log"
_verdict=$(stage_verdict "$_rc" "$_log")
if [ "$_verdict" != fail ]; then
splash "$_verdict"
else
splash fail
# The reason, not just the verdict. On the live medium this output
# goes to the serial line, and the screen shows only the splash.
while IFS= read -r _l; do
splash detail "$_l"
done < <(tail -n 6 "$_log" 2>/dev/null)
unset _l
fi
unset _rc _verdict
done
fs/etc/init.d/rcS, lines 192–232
The mark each stage gets on the splash comes from stage_verdict, defined near the top of rcS: fail for a non-zero exit, skip for a script that printed a [SKIP] line and started no supervised service, and ok otherwise. A failed stage also puts the last six lines of its log under the bar.
/etc/init.d/rcK
rcK is the first entry of runlevels 0 and 6. It stops the same set of services in reverse order, leaving the firewall loaded:
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
INIT_DIR="/etc/init.d"
DISABLED_DIR="/etc/service.disabled"
_scripts=()
for _script in "${INIT_DIR}"/[0-9][0-9]_*.sh; do
[ -x "$_script" ] || continue
_name=$(basename "$_script" | sed 's/^[0-9][0-9]_//;s/\.sh$//')
[ -f "${DISABLED_DIR}/${_name}" ] && continue
[ "$_name" = nftables ] && continue
_scripts+=("$_script")
done
for ((_i = ${#_scripts[@]} - 1; _i >= 0; _i--)); do
_script=${_scripts[_i]}
echo "[KDOS] Running: $(basename "$_script") stop"
"$_script" stop || true
done
sync
echo "KDOS services stopped."
fs/etc/init.d/rcK, lines 41–62
/etc/init.d/serial-getty
The serial console’s entry runs this wrapper, which sleeps when no UART answers at ttyS0:
# The serial console's getty, the S0 entry in /etc/inittab. Its arguments
# are agetty's.
#
# The kernel registers ttyS0 whether or not a UART answers there, so the
# device node always exists. With no UART the 8250 driver reports the port
# type as 0 (unknown), agetty fails on it at once, and init, after ten
# respawns in two minutes, disables the entry for five minutes and logs
# "respawning too fast", over and over. On such a port this sleeps instead,
# so the entry stays quiet until the runlevel changes.
#
# The name has no NN_ prefix, so rcS and rcK never run it.
export PATH=/sbin:/usr/sbin:/bin:/usr/bin
_type=
read -r _type < /sys/class/tty/ttyS0/type 2>/dev/null
if [ "$_type" = 0 ]; then
exec sleep infinity
fi
exec /sbin/agetty "$@"
The kernel command line
Two words on the kernel command line change what init does at boot: single and emergency. The initramfs passes both on to init. Their meaning is in Command-line parameters KDOS reads.
Usage
Finding the runlevel
Print the previous and the current runlevel:
runlevelOn a running system this prints
N 3: no previous runlevel, and runlevel 3.Print the time of the last boot from the login records
utmpdkeeps:who -bIt prints a line such as
system boot Oct 21 19:11.who -rprints the runlevel from the same records, with the time init entered it, as a line such asrun-level 3 Oct 21 19:11 last=S;whoshows theNof “no previous runlevel” asS. After a change of runlevel the line names the new one, andlast=the one before it.runlevelreads the records too and, should they hold no runlevel, answers from/run/runlevel, the one-character file init writes at every change of runlevel.
Applying an edit to /etc/inittab
Edit
/etc/inittabas root.Tell init to read it again:
sudo telinit q
An entry whose program exits at once is restarted by init until it has respawned ten times in two minutes, and is then left alone for five minutes. Check that every program a new entry names exists before reloading.
After an upgrade of the sysvinit package, make the running init re-execute itself, so that process 1 runs the new binary:
sudo telinit u
Single-user mode
Single-user mode runs sulogin -p on the console, which asks for root’s password and opens a root login shell, with /usr/local/bin and so kdos on its PATH, and starts no consoles and no desktop. Leaving that shell continues to runlevel 3.
sulogin refuses to open a shell while root’s password is locked, which is how the installer leaves root unless it was asked to set a root password. Set one first:
sudo passwd root
Then choose one of three ways in:
From the boot menu, on an installed system. Pick the
KDOS <version> (single user)entry.rcSruns as on every boot, so every enabled service starts, andsuloginruns after it. The live medium’s menu has no such entry: its entries areKDOS Live,KDOS Live (clean session),KDOS Live (verbose)and the memory test.From a running system. This ends every console, the desktop session on
tty1and the serial console with them:sudo telinit Sinit prints
INIT: Sending processes configured via /etc/inittab the TERM signal, then theKILLone, andsuloginasksEnter root password for system maintenance (or press Control-D to continue):. A wrong password getsLogin incorrectand the same question again.Emergency mode. In the boot menu, press
Eon an entry, add the wordemergencyto the kernel command line and boot it. The initramfs stops the splash, andsuloginthen runs beforercS, on the screen and on the serial line, so only the mounts the initramfs handed over exist, and nothing from/etc/fstabis mounted. This shell keeps init’sPATH, so callkdosas/usr/local/bin/kdos.
Every way in is described in Single-user and emergency mode.
Shutting down and rebooting
From a shell, as root, any of these asks init for runlevel 0 (power off) or 6 (reboot):
sudo poweroff sudo reboot sudo shutdown -h now sudo shutdown -r nowWithout
sudo, from the session at the machine, ask elogind, which polkit allows for the active session; elogind runs/sbin/poweroffor/sbin/reboot, which take the same runlevel path:loginctl poweroff loginctl rebootThe Leave buttons of the Application Launcher and the logout screen send the same request.
At a text console,
Ctrl+Alt+Delrunsshutdown -r now, thecaentry of the table.
What happens between the request and the power going off, entry by entry, is in Shutdown.
The recovery console
tty2 always has a plain agetty login with no automatic login, whatever happens on tty1. When the desktop does not come up, press Ctrl+Alt+F2 (Alt+F2 at a text console) and log in there.
Tips
kdos why /etc/inittabreportspackage (none — provided by fs/ or 10_bootstrap, or not installed): the file comes from the base filesystem, so no package upgrade replaces an edit to it.- Every process field in the table is free of shell metacharacters, so init executes it directly rather than through
/bin/sh -c. Keep an added entry the same way, or put the command in a script and name the script. - init hands its children
PATH=/sbin:/usr/sbin:/bin:/usr/bin, which does not include/usr/local, where thekdoscommands live.rcSandrcKset a longer one for the services, but an entry of your own in/etc/inittabgets init’s: name a program under/usr/localby its full path. - Each console’s
kdos-gettyappends its messages to/run/kdos-getty.<tty>.log, such as/run/kdos-getty.tty1.log. The file exists fortty1andtty2on every boot. A line such askdos-getty: tty1 winsize -> <columns>x<rows>is written when it sets the terminal’s size to the grid the console font produced, and is not an error. A line sayingcannot exec … — falling backmeans the program the entry names could not be run and a plainagettytook its place. The log also says how the console setup went:waiting for tty2 to be on screenuntil the terminal is first shown (tty2waits forCtrl+Alt+F2; its prompt is drawn only then), whether the console takeover was seen,font 16x32 after <n> triesor the size the font stayed at, and the exit status ofsetvtrgbandloadkeys. The tools’ own error messages are discarded; their outcome is what the log keeps. - A script in
/etc/init.dwithout theNN_prefix, such asserial-getty, is never run byrcSorrcK. That is the place for a helper aninittabentry calls.
Troubleshooting
The splash stops on a stage and marks it failed. The stage is named after the service script in capitals. Log in on tty2 and read the script’s output from that boot:
cat /run/kdos-init.<name>.log
The table in When a boot stops says where to look for every other stage. The files under /run are gone after the next reboot.
Single-user mode prints “Cannot open access to console, the root account is locked”. Root has no password. Press Enter; init continues to runlevel 3. Set a root password with sudo passwd root and try again.
The system log shows init “respawning too fast” for an entry. The program the entry names exits at once. Run the entry’s process field by hand as root to see why, correct the entry, and run sudo telinit q.
telinit reports timeout opening/writing control channel /run/initctl. init is not reading its request channel, and shutdown, reboot and poweroff go through the same channel. A SIGUSR2 sent to init closes it for the rest of the boot. Save your work, then:
sync
sudo reboot -f
-f calls the kernel’s reboot directly and skips rcK, so no service stops cleanly.
The screen is black after the splash. The session did not start. Log in on tty2 and follow Desktop troubleshooting; kdos doctor checks the session’s processes.
Known gaps
- The live image’s serial console is a root shell with no password: see Security
See also
- rcS and the service scripts: how a boot runs, from init to the login
- Shutdown: every step from the request to the power going off
- /etc/inittab: each entry and what it starts
- The console:
kdos-gettyand the console font - Services, Writing a service, Logs, The initramfs and the rescue shell, Login and autologin, Boot menu and kernel parameters