Watch
0
0
Fork
You've already forked hyperhive
0

hive-matrix: the fallocate probe works inside tuwunel's sandbox

Refs #6885
This commit is contained in:
atlas 2026-11-27 18:39:03 +01:00
commit 05e59c1e8c
2 changed files with 181 additions and 28 deletions

View file

@ -158,24 +158,9 @@ let
# the database directory under /var/lib.
stateName = "tuwunel";
# tuwunel's `rocksdb_allow_fallocate` is off only where its database sits on
# a filesystem that cannot honour RocksDB's log preallocation. A swarm's
# filesystem is not known at evaluation, so the unit's `ExecStartPre` reads
# it at each start, from the same superblock magics tuwunel itself tests for
# its warning (btrfs `9123683e`, ZFS `2fc12fc1`), and writes the override
# to an environment file. tuwunel applies `TUWUNEL_*` variables over its
# config file. Anywhere else, or if `stat` fails, the file is empty and the
# default (`true`) stands.
tuwunelRuntimeDir = "/run/tuwunel";
fallocateEnvFile = "${tuwunelRuntimeDir}/fallocate.env";
fallocateProbe = pkgs.writeShellScript "tuwunel-fallocate-probe" ''
set -u
: > ${fallocateEnvFile}
magic=$(${pkgs.coreutils}/bin/stat -f -c %t /var/lib/${stateName}) || exit 0
case "$magic" in
9123683e | 2fc12fc1) echo TUWUNEL_ROCKSDB_ALLOW_FALLOCATE=false > ${fallocateEnvFile} ;;
esac
'';
# `rocksdb_allow_fallocate` is off only where the database is on btrfs or ZFS:
# a probe at each start decides, see the file.
fallocateProbe = import ./lib/tuwunel-fallocate-probe.nix { inherit pkgs; };
# ── the swarm's own appservice ──────────────────────────────────────
#
@ -1117,11 +1102,11 @@ in
# Upstream's `ProtectSystem = "strict"` leaves `/run` read-only.
systemd.services.tuwunel.serviceConfig.ReadWritePaths = [ socketDir ];
# The `RuntimeDirectory` is upstream's. The file is read before each
# command of the unit runs, so `ExecStartPre` writes what `ExecStart`
# sees; the `-` keeps a missing file from failing the start.
systemd.services.tuwunel.serviceConfig.ExecStartPre = [ fallocateProbe ];
systemd.services.tuwunel.serviceConfig.EnvironmentFile = [ "-${fallocateEnvFile}" ];
# The probe's file lives in upstream's `RuntimeDirectory`.
systemd.services.tuwunel.serviceConfig.ExecStartPre =
(fallocateProbe.serviceConfig "/var/lib/${stateName}").ExecStartPre;
systemd.services.tuwunel.serviceConfig.EnvironmentFile =
(fallocateProbe.serviceConfig "/var/lib/${stateName}").EnvironmentFile;
# Keeps DynamicUser=true + PrivateUsers=true intact — no
# host-side chown :tuwunel / GID-pin gymnastics needed.

View file

@ -0,0 +1,53 @@
# The `ExecStartPre` that turns tuwunel's `rocksdb_allow_fallocate` off where
# its database sits on btrfs or ZFS, and the unit settings that carry the
# result to `ExecStart`. Shared by ../hive-matrix.nix and the VM test
# ../../vm-tests/tuwunel-fallocate-probe.nix, which runs it under tuwunel's own
# sandbox.
#
# tuwunel's unit (nixpkgs' `services.matrix-tuwunel`) filters system calls with
# `~@ipc`, which denies `pipe`: the probe cannot use a pipe, a command
# substitution (`$(…)`) or a here-document. It writes `stat`'s answer to a file
# in the unit's `RuntimeDirectory` and reads it back with the `read` builtin.
#
# The superblock types are the two tuwunel tests for its own warning (btrfs
# `9123683e`, ZFS `2fc12fc1`; `filesystem_from_path`). tuwunel applies
# `TUWUNEL_*` variables over its config file. When the probe cannot decide it
# says why on stderr and leaves the file empty: tuwunel still starts, with its
# default (`true`).
{ pkgs }:
rec {
runtimeDir = "/run/tuwunel";
envFile = "${runtimeDir}/fallocate.env";
# `$1` is the database directory.
probe = pkgs.writeShellScript "tuwunel-fallocate-probe" ''
set -u
name=tuwunel-fallocate-probe
typeFile=${runtimeDir}/fallocate.fstype
: > ${envFile}
if ! ${pkgs.coreutils}/bin/stat -f -c %t "$1" > "$typeFile"; then
echo "$name: stat -f of $1 failed; rocksdb_allow_fallocate stays at tuwunel's default" >&2
exit 0
fi
if ! read -r magic < "$typeFile"; then
echo "$name: no filesystem type read back from $typeFile; rocksdb_allow_fallocate stays at tuwunel's default" >&2
exit 0
fi
echo "$name: $1 is on filesystem type $magic"
case "$magic" in
9123683e) fs=btrfs ;;
2fc12fc1) fs=zfs ;;
*) exit 0 ;;
esac
echo "$name: $fs, so rocksdb_allow_fallocate is set to false"
echo TUWUNEL_ROCKSDB_ALLOW_FALLOCATE=false > ${envFile}
'';
# What a tuwunel unit takes for a database in `dir`. The file is read before
# each command of the unit runs, so `ExecStartPre` writes what `ExecStart`
# sees; the `-` keeps a missing file from failing the start.
serviceConfig = dir: {
ExecStartPre = [ "${probe} ${dir}" ];
EnvironmentFile = [ "-${envFile}" ];
};
}