s6-linux-init: Actions after unmounting filesystems

From: Guillermo <gdiazhartusch_at_gmail.com>
Date: Sat, 17 Aug 2019 16:14:03 -0300

For the 0.4.x.x series of s6-linux-init, the shutdown sequence model
was that s6-svscan replaced itself with a 'stage3 init' that ran as
process 1 until the machine halted, powered off, or rebooted. The
stage3 init was an execline script.

s6-linux-init- and later, among other things, has changed the
shutdown sequence model. The new one is that a supervised 'shutdown
daemon', s6-linux-init-shutdownd, performs all shutdown tasks, and
s6-svscan remains process 1 until the machine halts, powers off, or
reboots. s6-linux-init-shutdownd is a C program.

In both models, all processes are killed with a 'kill(-1, SIGTERM)'
call and then a 'kill(-1, SIGKILL)' call, and then filesystems are
unmounted. For the 0.4.x.x series, this is done by having the stage3
init run a program. Because the stage3 init runs as process 1, it
survives the SIGKILL. For s6-linux-init- and later, this is
done directly by s6-linux-init-shutdownd. Because
s6-linux-init-shutdownd is part of a supervision tree rooted in
process 1, it is respawned after the SIGKILL, and then completes the
shutdown sequence.

There are certain setups that require doing something after
filesystems are unmounted. Two examples are LVM logical volumes and
LUKS encrypted volumes, but I suppose there must be more cases. In any
such setup, the shutdown sequence would include a 'vgchange -a n'
command or 'cryptsetup close' command after filesystems are unmounted.
Runit-based Void Linux does this in /etc/runit/3:

* https://github.com/void-linux/void-runit/blob/0566391df8c9c93f75ad99d94c8a19abe379908b/3#L37

deactivate_vgs and deactivate_crypt are defined in /etc/runit/functions:

* https://github.com/void-linux/void-runit/blob/0566391df8c9c93f75ad99d94c8a19abe379908b/functions#L38

Gentoo, and I suppose that other OpenRC-based distributions as well,
leave this to the service manager. OpenRC-provided 'localmount'
service uses 'fuser -m -k' and umount commands in its stop() function
to unmount filesystems, and 'killprocs' service takes care of every
remaining process using OpenRC's internal kill_all command. Gentoo
then adds services named 'lvm' and 'dmcrypt' to OpenRC's boot
runlevel, and service ordering at shutdown, enforced by specifications
in service's depend() functions and OpenRC's runlevels model, is
localmount < root (a no-op) < fsck (a no-op generally) < {lvm,
dmcrypt} < killprocs < mount-ro, where "<" means "precedes" here.

How would something like this fit in s6-linux-init's new shutdown
model? In the old one, just like in runit's case, I suppose it could
be done in 'shutdown hooks' called by the execline stage3 init.
Something like:

foreground {
   redirfd -r 0 shutdown-hooks.conf
   forstdin shutdown_hook
   importas -u shutdown_hook shutdown_hook
   if -nX { $shutdown_hook }
      echo stage3 init: warning: shutdown hook \"${shutdown_hook}\" failed

I also looked at Adélie Linux, which currently uses
s6-linux-init- + OpenRC if it is configured to do so, and it
seems to copy Gentoo in this case. It also has lvm and dmcrypt OpenRC
service scripts:

* https://code.foxkit.us/adelie/packages/raw/b1eb50599e4db7eb4501af75cbbfa22007081ea5/system/lvm2/lvm.initd
* https://code.foxkit.us/adelie/packages/raw/49602ddb82f87067bbb945fc2a814a8eb6d6aabe/system/cryptsetup/dmcrypt.initd

I can't verify without actually installing Adélie, but I suppose these
are also boot runlevel services. However, it appears that Adélie's
/etc/s6-linux-init/skel/rc.shutdown just asks OpenRC to enter a custom
runlevel named 'empty'. Because this isn't the shutdown runlevel, if I
understand correctly this would neither stop boot runlevel services,
nor start shutdown runlevel services, so OpenRC's localmount,
killprocs and mount-ro would not interfere with s6-linux-init's
shutdown procedure. But doesn't this also mean that the vgchange and
cryptsetup invocations in lvm's and dmcrypt's stop() functions would
never happen?

Received on Sat Aug 17 2019 - 19:14:03 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:44:19 UTC