Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/host-modules/lib/tuwunel-fallocate-probe.nix

53 lines
2.2 KiB
Nix

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