diff --git a/.forgejo/workflows/vm-nightly.yml b/.forgejo/workflows/vm-nightly.yml index 0cf876d33..32f67b6cd 100644 --- a/.forgejo/workflows/vm-nightly.yml +++ b/.forgejo/workflows/vm-nightly.yml @@ -51,6 +51,7 @@ jobs: - hive-egress-antispoof - swarm-archive - swarm-builder-offline + - tuwunel-fallocate-probe - full-setup - semi-spread steps: diff --git a/docs/2-handbook/swarm/matrix.md b/docs/2-handbook/swarm/matrix.md index ec2f73f18..28cf6d8e4 100644 --- a/docs/2-handbook/swarm/matrix.md +++ b/docs/2-handbook/swarm/matrix.md @@ -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` diff --git a/nix/host-modules/hive-matrix.nix b/nix/host-modules/hive-matrix.nix index 10b3d85cf..f003389cf 100644 --- a/nix/host-modules/hive-matrix.nix +++ b/nix/host-modules/hive-matrix.nix @@ -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. diff --git a/nix/host-modules/lib/tuwunel-fallocate-probe.nix b/nix/host-modules/lib/tuwunel-fallocate-probe.nix new file mode 100644 index 000000000..bc27b0aae --- /dev/null +++ b/nix/host-modules/lib/tuwunel-fallocate-probe.nix @@ -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}" ]; + }; +} diff --git a/nix/module-eval/swarm-services-switch.nix b/nix/module-eval/swarm-services-switch.nix index 839ef60c4..9ce145ee6 100644 --- a/nix/module-eval/swarm-services-switch.nix +++ b/nix/module-eval/swarm-services-switch.nix @@ -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; } diff --git a/nix/vm-test-typecheck.nix b/nix/vm-test-typecheck.nix index 6c116dbef..2c5c5395a 100644 --- a/nix/vm-test-typecheck.nix +++ b/nix/vm-test-typecheck.nix @@ -135,6 +135,7 @@ let full-setup semi-spread swarm-builder-offline + tuwunel-fallocate-probe ; }; in diff --git a/nix/vm-tests.nix b/nix/vm-tests.nix index 98be9f84a..80110be19 100644 --- a/nix/vm-tests.nix +++ b/nix/vm-tests.nix @@ -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; }; } diff --git a/nix/vm-tests/tuwunel-fallocate-probe.nix b/nix/vm-tests/tuwunel-fallocate-probe.nix new file mode 100644 index 000000000..92681f78f --- /dev/null +++ b/nix/vm-tests/tuwunel-fallocate-probe.nix @@ -0,0 +1,98 @@ +# `vmTests..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 + ''; +}