diff --git a/docs/2-handbook/swarm/matrix.md b/docs/2-handbook/swarm/matrix.md index 4d3b949ef..ec2f73f18 100644 --- a/docs/2-handbook/swarm/matrix.md +++ b/docs/2-handbook/swarm/matrix.md @@ -31,6 +31,11 @@ Same shape as [`gateway.md::hive-forge container shape`](../../4-implementation/ `/var/lib/nixos-containers/hive-matrix/var/lib/matrix-tuwunel/` survives container restart / host reboot. To wipe, destroy the container. +- At each start tuwunel's unit checks which filesystem holds its database. + 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. ## Identity vs API listener: `serverName` vs `gatewayHost` diff --git a/nix/host-modules/hive-matrix.nix b/nix/host-modules/hive-matrix.nix index 1ed68bcc6..10b3d85cf 100644 --- a/nix/host-modules/hive-matrix.nix +++ b/nix/host-modules/hive-matrix.nix @@ -158,6 +158,25 @@ 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 + ''; + # ── the swarm's own appservice ────────────────────────────────────── # # The homeserver's one appservice registration, and the swarm's identity on @@ -1098,6 +1117,12 @@ 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}" ]; + # Keeps DynamicUser=true + PrivateUsers=true intact — no # host-side chown :tuwunel / GID-pin gymnastics needed. # See `man systemd.exec` → LoadCredential. diff --git a/nix/module-eval/swarm-services-switch.nix b/nix/module-eval/swarm-services-switch.nix index 2c349fe94..839ef60c4 100644 --- a/nix/module-eval/swarm-services-switch.nix +++ b/nix/module-eval/swarm-services-switch.nix @@ -945,6 +945,34 @@ let && unit.serviceConfig.Group == "gateway-nginx" && lib.elem dir unit.serviceConfig.ReadWritePaths; } + { + # tuwunel preallocates its logs unless told otherwise, which is only right + # off btrfs and ZFS. The setting is decided when the unit starts: the + # settings carry no `rocksdb_allow_fallocate`, a probe runs before + # `ExecStart` and writes the override to a file the unit reads. The + # container keeps the default start limit: a slow database open is not + # answered with a longer one. `allow_registration` and `unix_socket_perms` + # are the presence arms: settings read through the same path. + name = "the homeserver turns log preallocation off at start, by a probe of its filesystem, and keeps the default start limit"; + ok = + let + host = serviceContainersWithCi; + 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; + in + global ? allow_registration + && global ? unix_socket_perms + && !(global ? rocksdb_allow_fallocate) + && lib.length unit.serviceConfig.ExecStartPre == 1 + && 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 + && c.timeoutStartSec == "1min" + && startLimitOf "hive-matrix" == 60; + } { # On the bridge by DHCP, with all three listeners on sockets: nothing # the bridge can dial. Every vhost location that forwards goes through