testsAndMisc/linux_configuration/fixes/lib/tests/test_tmp_space_verify.sh
Krzysztof kuhy Rudnicki 70189149b2
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
Python tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Add fix_tmp_space.sh: 30G /tmp, /var/disktmp
/tmp is the stock tmp.mount tmpfs (size=50%, usrquota) and systemd caps
each user at 80% of it at login; a heavy build hit that cap with EDQUOT.

fix_tmp_space.sh writes a tmp.mount drop-in with the stock options and
size=30G, reloads systemd once and remounts /tmp live (never an unmount:
the X11 sockets live there). tmp_quota.py raises each logged-in user's
cap live to 80% of 30G through quotactl_fd, since systemd sets it only
at login. A sparse 64G ext4 image is loop-mounted at /var/disktmp (1777)
through one fstab line; tmpfiles.d empties it at boot and ages it 10d.

The fstab line adds x-systemd.before=systemd-tmpfiles-setup.service:
nofail drops the ordering, and in vmbox tmpfiles started 22 ms before
the mount finished, so the boot-time emptying could miss the image.

Failure paths:
- /tmp not a tmpfs: fail-closed, nothing written.
- another fstab line owns /var/disktmp: fail-closed.
- image exists but is not ext4: fail-closed, never reformatted.
- quotactl or remount fails: fail-closed, non-zero exit.
- image fails to mount at boot: fail-open on purpose. nofail keeps the
  boot going and the 1777 directory underneath takes writes uncapped,
  instead of every command that uses it failing. --verify-only exits 1.

Passed in vmbox, not verified on the host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWW9EDj5WPLpxuWXv29
2026-11-22 11:55:06 +01:00

121 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Tests for lib/tmp_space.sh's ts_verify and the fix_tmp_space.sh entry
# script's argument and root handling.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=tmp_space_harness.sh
. "${SCRIPT_DIR}/tmp_space_harness.sh"
# shellcheck source=../tmp_space.sh
. "${FIXES_DIR}/lib/tmp_space.sh"
# shellcheck source=../tmp_space_verify.sh
. "${FIXES_DIR}/lib/tmp_space_verify.sh"
entry="${FIXES_DIR}/fix_tmp_space.sh"
# --- a stock machine is drift on every item ---------------------------------
ts_reset
out="$(ts_verify 2>&1)"
_t_eq "1" "$?" "verify stock: exits non-zero"
_t_contains "$out" "/tmp size 16G" "verify stock: prints the /tmp size"
_t_contains "$out" "want 30G" "verify stock: names the wanted size"
_t_contains "$out" "uid 1000 cap 13G" "verify stock: prints the user's cap"
_t_contains "$out" "not mounted" "verify stock: says the disk area is not mounted"
_t_contains "$out" "DRIFT" "verify stock: marks drift"
_t_eq "" "$(_t_calls | grep -v -e '^findmnt' -e '^stat' -e ' get ')" \
"verify stock: only reads, never writes"
# --- after apply, verify is clean -------------------------------------------
ts_reset
ts_apply >/dev/null 2>&1
_t_reset_calls
out="$(ts_verify 2>&1)"
_t_eq "0" "$?" "verify applied: exits 0"
_t_contains "$out" "/tmp size 30G" "verify applied: /tmp is 30G"
_t_contains "$out" "uid 1000 cap 24G" "verify applied: cap is 80% of 30G"
_t_contains "$out" "used 2.0M" "verify applied: prints the user's use"
_t_contains "$out" "64G image, mounted ext4" "verify applied: disk area mounted"
_t_lacks "$out" "DRIFT" "verify applied: no drift"
_t_eq "0" "$(_t_calls | grep -c -e '^mount' -e '^systemctl' -e ' set ')" \
"verify applied: runs no write"
# --- each single drift is caught --------------------------------------------
ts_reset
ts_apply >/dev/null 2>&1
printf '[Mount]\nOptions=size=50%%\n' >"${TMP_MOUNT_DROPIN_DIR}/size.conf"
out="$(ts_verify 2>&1)"
_t_eq "1" "$?" "edited drop-in: drift"
_t_contains "$out" "size.conf DRIFT" "edited drop-in: names the file"
ts_reset
ts_apply >/dev/null 2>&1
printf 'UUID=abc / ext4 rw,noatime 0 1\n' >"${FSTAB_FILE}"
out="$(ts_verify 2>&1)"
_t_eq "1" "$?" "missing fstab line: drift"
_t_contains "$out" "fstab DRIFT" "missing fstab line: names fstab"
ts_reset
ts_apply >/dev/null 2>&1
rm -f "${TMPFILES_CONF_DIR}/disktmp.conf"
out="$(ts_verify 2>&1)"
_t_eq "1" "$?" "missing tmpfiles entry: drift"
ts_reset
ts_apply >/dev/null 2>&1
printf '1000 0\n' >"${DEV}/quota_1000"
out="$(ts_verify 2>&1)"
_t_eq "1" "$?" "low cap: drift"
_t_contains "$out" "uid 1000 cap 1000K" "low cap: prints the low cap"
ts_reset
ts_apply >/dev/null 2>&1
printf '0 0\n' >"${DEV}/quota_1000"
out="$(ts_verify 2>&1)"
_t_eq "0" "$?" "unlimited user: not drift"
_t_contains "$out" "uid 1000 cap unlimited" "unlimited user: says so"
ts_reset
ts_apply >/dev/null 2>&1
chmod 0755 "${DISKTMP_MOUNT}"
out="$(ts_verify 2>&1)"
_t_eq "1" "$?" "wrong mode: drift"
_t_contains "$out" "mode 755" "wrong mode: prints the mode"
ts_reset
ts_apply >/dev/null 2>&1
: >"${DEV}/helper_fails"
out="$(ts_verify 2>&1)"
_t_eq "1" "$?" "helper failure: verify fails"
_t_contains "$out" "quotactl failed" "helper failure: passes the message on"
# --- entry script -----------------------------------------------------------
ts_reset
out="$("$entry" --bogus 2>&1)"
_t_eq "2" "$?" "entry: unknown option exits 2"
_t_contains "$out" "Usage:" "entry: unknown option prints usage"
out="$("$entry" --help 2>&1)"
_t_eq "0" "$?" "entry: --help exits 0"
_t_contains "$out" "--verify-only" "entry: --help documents --verify-only"
out="$("$entry" --verify-only 2>&1)"
_t_eq "1" "$?" "entry: --verify-only on a stock machine exits 1 without root"
_t_contains "$out" "DRIFT" "entry: --verify-only prints the drift"
if ((EUID != 0)); then
_t_reset_calls
out="$("$entry" 2>&1)"
_t_eq "1" "$?" "entry: apply without root exits 1"
_t_contains "$out" "must run as root" "entry: apply without root says why"
_t_eq "" "$(_t_calls)" "entry: apply without root runs nothing"
fi
echo
echo "tmp_space_verify: ${PASS} passed, ${FAIL} failed"
((FAIL == 0))