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
8 changed files with 181 additions and 28 deletions

View file

@ -51,6 +51,7 @@ jobs:
- hive-egress-antispoof
- swarm-archive
- swarm-builder-offline
- tuwunel-fallocate-probe
- full-setup
- semi-spread
steps:

View file

@ -35,7 +35,8 @@ Same shape as [`gateway.md::hive-forge container shape`](../../4-implementation/
On btrfs or ZFS it sets `rocksdb_allow_fallocate = false` (through
`TUWUNEL_ROCKSDB_ALLOW_FALLOCATE`), because RocksDB's preallocated
write-ahead logs pin far more disk than they hold there. On any other
filesystem tuwunel's default stays.
filesystem tuwunel's default stays. A probe that can't decide says why in
the journal (`tuwunel-fallocate-probe:`) and leaves the default.
## Identity vs API listener: `serverName` vs `gatewayHost`

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}" ];
};
}

View file

@ -960,16 +960,29 @@ let
c = host.containers.hive-matrix;
global = c.config.services.matrix-tuwunel.settings.global;
unit = c.config.systemd.services.tuwunel;
probe = lib.head unit.serviceConfig.ExecStartPre;
probeLib = import ../host-modules/lib/tuwunel-fallocate-probe.nix { inherit pkgs; };
text = probeLib.probe.text;
in
global ? allow_registration
&& global ? unix_socket_perms
&& !(global ? rocksdb_allow_fallocate)
&& lib.length unit.serviceConfig.ExecStartPre == 1
&& unit.serviceConfig.ExecStartPre == [ "${probeLib.probe} /var/lib/tuwunel" ]
&& unit.serviceConfig.EnvironmentFile == [ "-/run/tuwunel/fallocate.env" ]
&& unit.serviceConfig.RuntimeDirectory == "tuwunel"
&& lib.hasInfix "stat -f -c %t /var/lib/tuwunel" probe.text
&& lib.hasInfix "9123683e | 2fc12fc1) echo TUWUNEL_ROCKSDB_ALLOW_FALLOCATE=false > /run/tuwunel/fallocate.env" probe.text
# The sandbox denies the `@ipc` group, which has `pipe`: the probe makes no pipe.
# The VM test of the probe runs it under that sandbox. These are the
# presence arms of the absences below.
&& lib.any (f: lib.hasPrefix "~" f && lib.hasInfix " @ipc" f) unit.serviceConfig.SystemCallFilter
&& lib.hasInfix "stat -f -c %t" text
&& lib.hasInfix "read -r magic < " text
&& lib.hasInfix "stat -f of $1 failed; rocksdb_allow_fallocate stays at tuwunel's default\" >&2" text
&& lib.hasInfix "no filesystem type read back" text
&& lib.hasInfix "9123683e) fs=btrfs" text
&& lib.hasInfix "2fc12fc1) fs=zfs" text
&& lib.hasInfix "TUWUNEL_ROCKSDB_ALLOW_FALLOCATE=false > /run/tuwunel/fallocate.env" text
&& !(lib.hasInfix "$(" text)
&& !(lib.hasInfix "`" text)
&& !(lib.hasInfix "<<" text)
&& c.timeoutStartSec == "1min"
&& startLimitOf "hive-matrix" == 60;
}

View file

@ -135,6 +135,7 @@ let
full-setup
semi-spread
swarm-builder-offline
tuwunel-fallocate-probe
;
};
in

View file

@ -15,5 +15,6 @@
swarm-archive = import ./vm-tests/swarm-archive.nix { inherit pkgs; };
swarm-builder-offline = import ./vm-tests/swarm-builder-offline.nix { inherit pkgs self; };
full-setup = import ./vm-tests/full-setup.nix { inherit pkgs self; };
tuwunel-fallocate-probe = import ./vm-tests/tuwunel-fallocate-probe.nix { inherit pkgs; };
semi-spread = import ./vm-tests/semi-spread.nix { inherit pkgs self; };
}

View file

@ -0,0 +1,98 @@
# `vmTests.<system>.tuwunel-fallocate-probe`: runs the hive's real tuwunel
# fallocate probe (../host-modules/lib/tuwunel-fallocate-probe.nix) in a VM,
# under the sandbox of nixpkgs' tuwunel unit, which the module-eval suite can
# only read. That sandbox denies the `@ipc` group (`SystemCallFilter`), which
# has `pipe`, so a probe that makes a pipe (a command substitution) fails there
# and says nothing.
#
# `tuwunel-probe-check` is tuwunel's own `serviceConfig`, with the probe and
# the environment file the hive adds, and `env` as the command: its journal
# shows what `ExecStart` would be given. `tuwunel-pipe-control` is the same
# sandbox around a command substitution, and has to log that bash cannot make
# a pipe (bash still exits 0): it shows the sandbox in this VM does filter
# pipes. Two nodes: `cow` has a btrfs at `/mnt/cow`, which the probe answers
# with the override; `plain` is probed at `/var/lib` on the root filesystem and
# gets none.
{ pkgs }:
let
probeLib = import ../host-modules/lib/tuwunel-fallocate-probe.nix { inherit pkgs; };
pipeControl = pkgs.writeShellScript "pipe-control" ''
x=$(true)
'';
node =
dir:
{ config, lib, ... }:
let
sandbox = removeAttrs config.systemd.services.tuwunel.serviceConfig [
"ExecStart"
"ExecStartPre"
"Restart"
"RestartSec"
];
in
{
services.matrix-tuwunel = {
enable = true;
settings.global.server_name = "t.local";
};
# The real unit stays stopped: only the sandbox is wanted from it.
systemd.services.tuwunel.wantedBy = lib.mkForce [ ];
systemd.services.tuwunel-probe-check.serviceConfig =
sandbox
// {
Type = "oneshot";
ExecStart = "${pkgs.coreutils}/bin/env";
}
// probeLib.serviceConfig dir;
systemd.services.tuwunel-pipe-control.serviceConfig = sandbox // {
Type = "oneshot";
ExecStart = "${pipeControl}";
};
};
in
pkgs.testers.runNixOSTest {
name = "tuwunel-fallocate-probe";
# Without this the driver allows an hour, the runner's whole job limit.
globalTimeout = 900;
nodes.cow = {
imports = [ (node "/mnt/cow") ];
virtualisation.emptyDiskImages = [ 256 ];
boot.supportedFilesystems = [ "btrfs" ];
};
nodes.plain = node "/var/lib";
testScript = ''
start_all()
cow.wait_for_unit("multi-user.target")
plain.wait_for_unit("multi-user.target")
def journal(m, unit: str) -> str:
return m.succeed(f"journalctl -u {unit} --no-pager -o cat")
with subtest("the sandbox filters pipes: a command substitution cannot make one in it"):
# bash exits 0 after a failed pipe, so the unit starts: its journal has the verdict.
for m in (cow, plain):
m.succeed("systemctl start tuwunel-pipe-control")
out = journal(m, "tuwunel-pipe-control")
assert "cannot make pipe for command substitution: Operation not permitted" in out, out
with subtest("on btrfs the probe sets the override, and the unit's command is given it"):
cow.succeed("mkfs.btrfs -q /dev/vdb && mkdir -p /mnt/cow && mount /dev/vdb /mnt/cow")
cow.succeed("systemctl start tuwunel-probe-check")
out = journal(cow, "tuwunel-probe-check")
assert "tuwunel-fallocate-probe: /mnt/cow is on filesystem type 9123683e" in out, out
assert "TUWUNEL_ROCKSDB_ALLOW_FALLOCATE=false" in out, out
with subtest("elsewhere the probe reads the type and leaves the default"):
plain.succeed("systemctl start tuwunel-probe-check")
out = journal(plain, "tuwunel-probe-check")
assert "tuwunel-fallocate-probe: /var/lib is on filesystem type " in out, out
assert "TUWUNEL_ROCKSDB_ALLOW_FALLOCATE" not in out, out
'';
}