Add fix_tmp_space.sh: 30G /tmp, /var/disktmp
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

/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
This commit is contained in:
Krzysztof Rudnicki 2026-11-22 11:37:47 +01:00
parent 339790efab
commit 70189149b2
12 changed files with 1256 additions and 36 deletions

View File

@ -0,0 +1,17 @@
{
"title": "fix_tmp_space.sh: 30G /tmp tmpfs, live user cap, 64G /var/disktmp",
"objective": "Commands stop failing with EDQUOT on /tmp: the tmpfs grows to 30G without an unmount, each logged-in user's cap rises live to 80% of it, and a 64G disk-backed temp area exists at /var/disktmp for heavy writers to use later through TMPDIR.",
"acceptance_criteria": [
"`sudo fix_tmp_space.sh` on a stock machine writes the tmp.mount drop-in, the fstab line and the tmpfiles entry, reloads systemd once, remounts /tmp at size=30G and keeps usrquota, files and X11 sockets in /tmp.",
"Each non-root uid under /run/user gets a cap of 80% of 30G live (25165824 KiB); a cap of 0 or one already within 1 MiB is left alone.",
"/var/disktmp is a mounted 64G sparse ext4 image, mode 1777; a file written there by a user lands in the image.",
"A second run changes nothing and prints one line; --verify-only changes nothing, works without root and exits non-zero on drift.",
"After a reboot /tmp is 30G from the drop-in, systemd applies its own 80% cap at login, and /var/disktmp is mounted and emptied by tmpfiles."
],
"out_of_scope": [
"Pointing heavy writers at /var/disktmp through TMPDIR.",
"/dev/shm and its quota.",
"Running the script on the host (the coordinator does that after review)."
],
"verifier": "test_tmp_space_apply.sh (42 passed), test_tmp_space_verify.sh (36 passed), test_tmp_quota.py (11 passed); every phase of vmbox_tmp_space.sh OK in guest tmpspace, including the reboot and a fail-open boot without the image."
}

View File

@ -1,36 +0,0 @@
{
"intent": "The automation tutor is now just Tutor (own repo ~/src/tutor, data ~/.local/share/tutor, earned_time 0.9.0 earner 'tutor'); the i3 bar follows the rename.",
"scope": [
"linux_configuration/i3blocks/tutor.sh (was automation_tutor.sh), the [tutor] stanza in linux_configuration/i3blocks/config, linux_configuration/tests/test_i3blocks_tutor.sh",
"gaming_budget.sh and shutdown_countdown.sh icon maps: earner 'tutor' gets the same gear as 'automation'",
"Non-goal: no change to what the block counts (still today's credit rows / 4)"
],
"changes": [
"Ledger default ~/.local/share/tutor/ledger.json (override TUTOR_LEDGER); click runs ~/src/tutor/run.sh (override TUTOR_RUN)",
"Block and test renamed; ~/.config/i3blocks/tutor.sh symlink replaces automation_tutor.sh",
"tutor: \"⚙\" added beside automation in the two daily-limits renderers"
],
"verification": [
{
"command": "bash linux_configuration/tests/test_i3blocks_tutor.sh",
"result": "pass",
"evidence": "tutor block: all checks passed"
},
{
"command": "bash linux_configuration/tests/test_i3blocks_gaming_budget.sh",
"result": "pass",
"evidence": "All gaming_budget checks passed."
},
{
"command": "~/.config/i3blocks/tutor.sh",
"result": "pass",
"evidence": "⚙ tutor 0/4 (live ledger at the new path)"
}
],
"risks": [
"The i3blocks config is symlinked, so the [tutor] stanza is live on the next i3blocks reload; until then the old stanza points at a removed symlink"
],
"rollback": [
"git revert the commit and re-link ~/.config/i3blocks/automation_tutor.sh"
]
}

View File

@ -0,0 +1,45 @@
{
"intent": "Stop EDQUOT on /tmp: grow the /tmp tmpfs to 30G live, raise each logged-in user's /tmp cap live to 80% of it, and add a 64G disk-backed temp area at /var/disktmp for heavy writers.",
"scope": [
"linux_configuration/fixes/fix_tmp_space.sh (entry), lib/tmp_space.sh (apply), lib/tmp_space_verify.sh (--verify-only)",
"linux_configuration/fixes/tmp_quota.py: quotactl_fd get/set helper run by /usr/bin/python3",
"Tests: fixes/lib/tests/tmp_space_harness.sh, test_tmp_space_apply.sh, test_tmp_space_verify.sh; linux_configuration/tests/test_tmp_quota.py; guest check fixes/lib/tests/vmbox_tmp_space.sh",
"Non-goal: pointing TMPDIR at /var/disktmp; /dev/shm is left alone; the script was not run on the host"
],
"changes": [
"tmp.mount drop-in with the stock options and size=30G, one daemon-reload, live remount (never unmount/restart; X11 sockets live in /tmp)",
"Raise-only live cap for every non-root uid under /run/user via quotactl_fd (soft = hard, QIF_BLIMITS only); systemd re-applies its own 80% at login",
"Sparse 64G ext4 image (0600, no reserved blocks) loop-mounted at /var/disktmp through one fstab line, ordered before systemd-tmpfiles-setup; tmpfiles D entry empties it at boot and ages it 10d"
],
"verification": [
{
"command": "bash linux_configuration/fixes/lib/tests/test_tmp_space_apply.sh; bash linux_configuration/fixes/lib/tests/test_tmp_space_verify.sh",
"result": "pass",
"evidence": "red before the lib existed (18/41 and 4/36 passing); green after: 42 passed 0 failed, 36 passed 0 failed"
},
{
"command": "python3 -m pytest -q linux_configuration/tests/test_tmp_quota.py",
"result": "pass",
"evidence": "red: ImportError at collection; green: 11 passed"
},
{
"command": "vmbox guest tmpspace: sudo bash vmbox_tmp_space.sh before|apply|quota|disk|again, reboot, rebooted",
"result": "pass",
"evidence": "before: --verify-only exit 1 (red). apply: findmnt size=31457280k usrquota kept, /tmp canary and /tmp/.X11-unix/X0 survive, xset q answers, uid 1000 cap 25165824 KiB. quota: 60M write under 50M cap 'Disk quota exceeded', 100M after raising to 200M ok. disk: /dev/loop0 ext4, 1777, 64G apparent / 517M used, user file visible via debugfs. again: one line 'already applied, nothing changed', --verify-only 0 as root and as the user. rebooted: /tmp 30G, systemd-user-runtime-dir 'on /tmp to 24G', disk area mounted and canary gone. Image removed + reboot: boot continues, only var-disktmp.mount failed, /var/disktmp a 1777 dir, --verify-only exit 1. Passed in vmbox, not verified on the host."
},
{
"command": "pre-commit run --hook-stage pre-commit --files <changed files>",
"result": "pass",
"evidence": "ruff, ruff format, mypy, pylint, bandit, shellcheck, shell coverage ratchet, file-length cap all Passed"
}
],
"risks": [
"A 30G tmpfs equals the host's RAM: a full /tmp now pushes into swap or OOM instead of failing with EDQUOT at 12G",
"The image is sparse: if the root filesystem fills, writes into /var/disktmp fail with I/O errors rather than ENOSPC",
"If the image fails to mount, /var/disktmp is an uncapped 1777 directory on the root filesystem (fail-open on purpose)"
],
"rollback": [
"rm /etc/systemd/system/tmp.mount.d/size.conf /etc/tmpfiles.d/disktmp.conf; drop the disktmp line from /etc/fstab; systemctl daemon-reload; mount -o remount,size=50% /tmp; umount /var/disktmp; rm /var/lib/disktmp/disktmp.img",
"Validate with findmnt /tmp and the next login's systemd-user-runtime-dir journal line"
]
}

View File

@ -0,0 +1,80 @@
#!/bin/bash
# ============================================================================
# Grow /tmp to a 30G tmpfs and add a 64G disk-backed temp area at /var/disktmp.
#
# 1. /etc/systemd/system/tmp.mount.d/size.conf: the stock options with
# size=30G, then one daemon-reload and a live remount (never an unmount:
# the X11 sockets live in /tmp/.X11-unix).
# 2. Each logged-in user's /tmp cap raised live to 80% of 30G through
# tmp_quota.py (quotactl_fd). systemd applies its own 80% at the next
# login, so nothing about the cap is persisted.
# 3. A sparse 64G ext4 image, /var/lib/disktmp/disktmp.img, loop-mounted at
# /var/disktmp (mode 1777) through one /etc/fstab line.
# 4. /etc/tmpfiles.d/disktmp.conf empties it at boot and ages it like /tmp.
#
# Idempotent: a second run changes nothing and says so in one line.
# ============================================================================
set -euo pipefail
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
readonly SCRIPT_DIR
SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_NAME
readonly REQUIRED_TOOLS=(findmnt stat mount systemctl truncate mkfs.ext4 blkid numfmt)
# shellcheck source=lib/tmp_space.sh
source "${SCRIPT_DIR}/lib/tmp_space.sh"
# shellcheck source=lib/tmp_space_verify.sh
source "${SCRIPT_DIR}/lib/tmp_space_verify.sh"
usage() {
echo "Usage: sudo ${SCRIPT_NAME} [--verify-only]"
echo " (no option) apply everything (root)"
echo " --verify-only change nothing; print the state, exit 1 on drift"
echo " -h, --help show this help"
}
require_tools() {
local tool
for tool in "${REQUIRED_TOOLS[@]}"; do
if ! command -v "$tool" >/dev/null; then
ts_err "${tool} not found"
exit 1
fi
done
if [[ ! -x "$TS_PYTHON" ]]; then
ts_err "${TS_PYTHON} not found"
exit 1
fi
}
main() {
local verify_only=false
while (($# > 0)); do
case "$1" in
--verify-only) verify_only=true ;;
-h | --help)
usage
exit 0
;;
*)
usage >&2
exit 2
;;
esac
shift
done
if [[ "$verify_only" == true ]]; then
require_tools
ts_verify
return
fi
if ((EUID != 0)); then
ts_err "must run as root (sudo ${SCRIPT_NAME})"
exit 1
fi
require_tools
ts_apply
}
main "$@"

View File

@ -0,0 +1,134 @@
#!/usr/bin/env bash
# Tests for lib/tmp_space.sh: ts_apply on a stock machine, a second run, and
# every refusal (fail-closed) path.
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"
dropin="${TMP_MOUNT_DROPIN_DIR}/size.conf"
image="${DISKTMP_IMAGE_DIR}/disktmp.img"
want_opts='Options=mode=1777,strictatime,nosuid,nodev,size=30G,nr_inodes=1m,x-systemd.graceful-option=usrquota'
# --- first run on a stock machine -------------------------------------------
ts_reset
out="$(ts_apply 2>&1)"
rc=$?
calls="$(_t_calls)"
_t_eq "0" "$rc" "apply: exits 0 on a stock machine"
_t_eq "[Mount]
${want_opts}" "$(cat "$dropin" 2>/dev/null)" \
"apply: drop-in keeps the stock options with size=30G"
_t_contains "$(cat "${FSTAB_FILE}")" "UUID=abc / ext4 rw,noatime 0 1" \
"apply: keeps the existing fstab lines"
_t_contains "$(cat "${FSTAB_FILE}")" \
"${image} ${DISKTMP_MOUNT} ext4 loop,nosuid,nodev,noatime,discard,nofail,x-systemd.before=systemd-tmpfiles-setup.service 0 0" \
"apply: appends the disktmp loop line to fstab"
_t_eq "D ${DISKTMP_MOUNT} 1777 root root 10d" \
"$(cat "${TMPFILES_CONF_DIR}/disktmp.conf" 2>/dev/null)" \
"apply: tmpfiles entry empties and ages the disk area"
_t_eq "1" "$(grep -c '^systemctl daemon-reload' <<<"$calls")" \
"apply: reloads systemd exactly once"
_t_lacks "$calls" "systemctl restart" "apply: never restarts a unit"
_t_lacks "$calls" "umount" "apply: never unmounts anything"
_t_contains "$calls" "mount -o remount,size=30G ${TMP_MOUNT_POINT}" \
"apply: remounts /tmp live at 30G"
_t_contains "$calls" "fake-interp ${TMP_QUOTA_HELPER} set --uid 1000 --mount ${TMP_MOUNT_POINT} --limit-kib ${TARGET_CAP_KIB}" \
"apply: raises uid 1000's cap to 80% of 30G"
_t_lacks "$calls" "set --uid 0" "apply: leaves root's unlimited quota alone"
_t_contains "$calls" "truncate -s 64G ${image}" "apply: creates a sparse 64G image"
_t_contains "$calls" "mkfs.ext4 -F -q -m 0 -L disktmp ${image}" \
"apply: formats the image ext4 with no reserved blocks"
_t_contains "$calls" "mount -T ${FSTAB_FILE} ${DISKTMP_MOUNT}" \
"apply: mounts the disk area through its fstab line"
_t_eq "1777" "$(/usr/bin/stat -c %a "${DISKTMP_MOUNT}")" \
"apply: the disk area is world-writable with the sticky bit"
_t_eq "600" "$(/usr/bin/stat -c %a "$image")" \
"apply: the image is readable by root only"
_t_contains "$out" "/tmp" "apply: reports what it changed"
# --- second run changes nothing and says so in one line ---------------------
before_fstab="$(cat "${FSTAB_FILE}")"
_t_reset_calls
out="$(ts_apply 2>&1)"
rc=$?
calls="$(_t_calls)"
_t_eq "0" "$rc" "apply again: exits 0"
_t_eq "1" "$(wc -l <<<"$out")" "apply again: prints exactly one line"
_t_contains "$out" "nothing changed" "apply again: says nothing changed"
_t_lacks "$calls" "systemctl" "apply again: no daemon-reload"
_t_eq "0" "$(grep -c '^mount ' <<<"$calls")" "apply again: no remount, no mount"
_t_lacks "$calls" "mkfs" "apply again: never reformats the image"
_t_lacks "$calls" " set " "apply again: no quota write"
_t_eq "$before_fstab" "$(cat "${FSTAB_FILE}")" "apply again: fstab unchanged"
# --- refusals: each fails closed and writes nothing it should not -----------
ts_reset
printf 'ext4\n' >"$(_ts_fstype_fixture "${TMP_MOUNT_POINT}")"
out="$(ts_apply 2>&1)"
_t_eq "1" "$?" "not tmpfs: apply fails"
_t_contains "$out" "not a tmpfs" "not tmpfs: says why"
_t_eq "0" "$(_t_calls | grep -vc '^findmnt')" "not tmpfs: only probes, runs nothing"
if [[ -e "$dropin" ]]; then
_t_fail "not tmpfs: wrote the drop-in"
else
_t_pass "not tmpfs: writes no drop-in"
fi
ts_reset
printf '/dev/sdb1 %s ext4 rw 0 2\n' "${DISKTMP_MOUNT}" >>"${FSTAB_FILE}"
out="$(ts_apply 2>&1)"
_t_eq "1" "$?" "foreign fstab line: apply fails"
_t_contains "$out" "already has a different line" "foreign fstab line: says why"
_t_eq "1" "$(grep -c "${DISKTMP_MOUNT}" "${FSTAB_FILE}")" \
"foreign fstab line: is left as it was"
ts_reset
mkdir -p "${DISKTMP_IMAGE_DIR}"
printf 'data' >"$image"
printf 'xfs\n' >"${DEV}/blkid_type"
out="$(ts_apply 2>&1)"
_t_eq "1" "$?" "foreign image: apply fails"
_t_contains "$out" "not ext4" "foreign image: says why"
_t_lacks "$(_t_calls)" "mkfs" "foreign image: is never reformatted"
ts_reset
mkdir -p "${DISKTMP_IMAGE_DIR}"
printf 'data' >"$image"
out="$(ts_apply 2>&1)"
_t_eq "1" "$?" "unreadable image: apply fails"
_t_lacks "$(_t_calls)" "mkfs" "unreadable image: is never reformatted"
ts_reset
: >"${DEV}/helper_fails"
out="$(ts_apply 2>&1)"
_t_eq "1" "$?" "helper failure: apply fails"
_t_contains "$out" "quotactl failed" "helper failure: passes the helper's message on"
ts_reset
printf '0 0\n' >"${DEV}/quota_1000"
ts_apply >/dev/null 2>&1
_t_lacks "$(_t_calls)" "set --uid" "unlimited user: is not given a cap"
ts_reset
printf '%s 0\n' $((TARGET_CAP_KIB + 500)) >"${DEV}/quota_1000"
ts_apply >/dev/null 2>&1
_t_lacks "$(_t_calls)" "set --uid" "cap within systemd's rounding: left alone"
ts_reset
: >"${DEV}/mount_fails"
out="$(ts_apply 2>&1)"
_t_eq "1" "$?" "remount failure: apply fails"
echo
echo "tmp_space_apply: ${PASS} passed, ${FAIL} failed"
((FAIL == 0))

View File

@ -0,0 +1,120 @@
#!/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))

View File

@ -0,0 +1,119 @@
#!/usr/bin/env bash
# lib/tests/tmp_space_harness.sh — shared setup for the tmp_space.sh tests.
#
# Sourced, not executed. Builds on lib_test_core.sh and adds a fake machine:
#
# * every path tmp_space.sh writes (the tmp.mount drop-in, fstab, the
# tmpfiles.d entry, the image, both mount points, /run/user) is pointed
# into $TEST_TMPDIR through the lib's env overrides;
# * findmnt, stat -f, mount, systemctl, mkfs.ext4 and blkid are stubs that
# read and update fixture files in $DEV, so a remount really changes what
# the next statfs reports and a second run sees the result of the first
# (truncate is logged, then real: a sparse 64G file costs nothing and
# gives verify a real apparent size);
# * the quota helper is a stub python that keeps one "<limit> <used>" file
# per uid, in KiB, like the real tmp_quota.py prints.
#
# run_all.sh runs UN-jailed in ci_mirror.sh and in CI, so these overrides are
# the only thing between the suite and the real /etc/fstab.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FIXES_DIR="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# shellcheck source=lib_test_core.sh
. "${SCRIPT_DIR}/lib_test_core.sh"
# Read by the lib under test, never by this harness; export makes it explicit.
export TMP_MOUNT_DROPIN_DIR="${TEST_TMPDIR}/etc/tmp.mount.d"
export FSTAB_FILE="${TEST_TMPDIR}/etc/fstab"
export TMPFILES_CONF_DIR="${TEST_TMPDIR}/etc/tmpfiles.d"
export DISKTMP_IMAGE_DIR="${TEST_TMPDIR}/var/lib/disktmp"
export DISKTMP_MOUNT="${TEST_TMPDIR}/var/disktmp"
export TMP_MOUNT_POINT="${TEST_TMPDIR}/tmp"
export RUN_USER_DIR="${TEST_TMPDIR}/run/user"
export TMP_SPACE_PYTHON="${FAKE_BIN}/fake-interp"
export TMP_QUOTA_HELPER="${FIXES_DIR}/tmp_quota.py"
readonly GIB=1073741824
# 16G stock tmpfs and its systemd-applied 80% cap, as on kuhy's host.
readonly STOCK_BLOCKS=$((16 * GIB / 4096))
readonly STOCK_CAP_KIB=13421772
readonly TARGET_CAP_KIB=25165824
export TARGET_CAP_KIB # read by the test files
# Fixture names: findmnt reports FSTYPE from fstype<path with / as _>.
_ts_fstype_fixture() {
printf '%s/fstype%s' "${DEV}" "${1//\//_}"
}
_ts_stubs() {
_t_stub_stdin findmnt <<'STUB'
target="${*: -1}"
f="${LIB_TEST_DEV}/fstype${target//\//_}"
[[ -f "$f" ]] || exit 1
cat "$f"
STUB
# Only `stat -f` is faked: it reports /tmp's block count. Mode reads go
# to the real stat, because the real chmod ran on the tmpdir.
_t_stub_stdin stat <<'STUB'
if [[ "$1" == "-f" ]]; then
printf '%s 4096\n' "$(cat "${LIB_TEST_DEV}/tmp_blocks")"
exit 0
fi
exec /usr/bin/stat "$@"
STUB
_t_stub_stdin mount <<'STUB'
[[ -f "${LIB_TEST_DEV}/mount_fails" ]] && exit 32
if [[ "$*" == *remount,size=30G* ]]; then
printf '%s\n' $((30 * 1073741824 / 4096)) >"${LIB_TEST_DEV}/tmp_blocks"
else
target="${*: -1}"
printf 'ext4\n' >"${LIB_TEST_DEV}/fstype${target//\//_}"
fi
STUB
_t_stub systemctl 'exit 0'
_t_stub_stdin truncate <<'STUB'
exec /usr/bin/truncate "$@"
STUB
_t_stub_stdin mkfs.ext4 <<'STUB'
printf 'ext4\n' >"${LIB_TEST_DEV}/blkid_type"
STUB
_t_stub_stdin blkid <<'STUB'
[[ -f "${LIB_TEST_DEV}/blkid_type" ]] || exit 2
cat "${LIB_TEST_DEV}/blkid_type"
STUB
_t_stub_stdin fake-interp <<'STUB'
# argv: <helper> get|set --uid N --mount M [--limit-kib K]
f="${LIB_TEST_DEV}/quota_$4"
[[ -f "${LIB_TEST_DEV}/helper_fails" ]] && {
echo "tmp_quota: quotactl failed" >&2
exit 1
}
if [[ "$2" == "get" ]]; then
cat "$f"
else
read -r _ used <"$f"
printf '%s %s\n' "$8" "$used" >"$f"
fi
STUB
}
# ts_reset — a stock machine: /tmp a 16G tmpfs, uid 1000 logged in with
# systemd's cap, uid 0 present, no drop-in, no image, fstab with one line.
ts_reset() {
_t_reset_calls
rm -rf "${TEST_TMPDIR:?}/etc" "${TEST_TMPDIR:?}/var" "${TEST_TMPDIR:?}/run" \
"${TMP_MOUNT_POINT}"
rm -f "${DEV}"/fstype* "${DEV}"/quota_* "${DEV}/blkid_type" \
"${DEV}/mount_fails" "${DEV}/helper_fails"
mkdir -p "${TEST_TMPDIR}/etc" "${RUN_USER_DIR}/1000" "${RUN_USER_DIR}/0" \
"${TMP_MOUNT_POINT}"
printf 'UUID=abc / ext4 rw,noatime 0 1\n' >"${FSTAB_FILE}"
printf '%s\n' "${STOCK_BLOCKS}" >"${DEV}/tmp_blocks"
printf 'tmpfs\n' >"$(_ts_fstype_fixture "${TMP_MOUNT_POINT}")"
printf '%s 2048\n' "${STOCK_CAP_KIB}" >"${DEV}/quota_1000"
printf '0 0\n' >"${DEV}/quota_0"
}
_ts_stubs

View File

@ -0,0 +1,150 @@
#!/usr/bin/env bash
# lib/tests/vmbox_tmp_space.sh — prove fix_tmp_space.sh inside a vmbox guest.
#
# Runs INSIDE the guest, as root, one phase per call so every call stays
# under a minute. Never run it on the host: it remounts /tmp, writes /etc
# and creates a 64G image. Not named test_*.sh, so run_all.sh (and CI) never
# picks it up. From the host:
#
# vm new tmpspace
# vm repo tmpspace ~/src/testsAndMisc --worktree # lands in ~/testsAndMisc
# C=~/testsAndMisc/linux_configuration/fixes/lib/tests/vmbox_tmp_space.sh
# vm run tmpspace "sudo bash $C before" # red: --verify-only exits 1
# vm run tmpspace "sudo bash $C apply"
# vm run tmpspace "sudo bash $C quota"
# vm run tmpspace "sudo bash $C disk"
# vm run tmpspace "sudo bash $C again"
# vm run tmpspace 'sudo systemctl reboot'; vm start tmpspace
# vm run tmpspace "sudo bash $C rebooted"
#
# Each phase prints one OK/FAIL line per check and exits non-zero on any FAIL.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly FIXES="${HERE}/../.."
readonly ENTRY="${FIXES}/fix_tmp_space.sh"
readonly HELPER="${FIXES}/tmp_quota.py"
readonly USER_NAME="${SUDO_USER:?run through sudo from the guest user}"
USER_UID="$(id -u "$USER_NAME")"
readonly USER_UID
readonly PROBE_USER=tmpqprobe
readonly TARGET_KIB=25165824 # 80% of 30G, in KiB
readonly CANARY=/tmp/vmbox-tmp-space-canary
readonly DISK_CANARY=/var/disktmp/vmbox-reboot-canary
FAILED=0
ok() { printf 'OK %s\n' "$1"; }
bad() {
printf 'FAIL %s\n' "$1"
FAILED=1
}
# check WHAT CMD... — OK when CMD exits 0.
check() {
local what="$1"
shift
if "$@" >/dev/null 2>&1; then ok "$what"; else bad "$what"; fi
}
as_user() { runuser -u "$1" -- "${@:2}"; }
cap_kib() { /usr/bin/python3 "$HELPER" get --uid "$1" --mount /tmp | cut -d' ' -f1; }
# check_cap CAP WHAT — OK when CAP is 80% of 30G within systemd's rounding.
check_cap() {
if (($1 >= TARGET_KIB - 1024 && $1 <= TARGET_KIB + 1024)); then ok "$2"; else bad "$2"; fi
}
phase_before() {
: >"$CANARY"
findmnt -no OPTIONS /tmp
if as_user "$USER_NAME" "$ENTRY" --verify-only; then
bad "stock guest: --verify-only should report drift"
else
ok "stock guest: --verify-only exits non-zero (red)"
fi
}
phase_apply() {
"$ENTRY"
local opts
opts="$(findmnt -no OPTIONS /tmp)"
printf 'findmnt /tmp: %s\n' "$opts"
check "/tmp is size=30G" grep -q 'size=31457280k' <<<"$opts"
check "/tmp kept usrquota" grep -q usrquota <<<"$opts"
check "/tmp canary survived the remount" test -e "$CANARY"
check "X11 socket survived the remount" test -S /tmp/.X11-unix/X0
check "X server still answers" as_user "$USER_NAME" env DISPLAY=:0 xset q
local cap
cap="$(cap_kib "$USER_UID")"
printf 'uid %s cap %s KiB (want %s)\n' "$USER_UID" "$cap" "$TARGET_KIB"
check_cap "$cap" "logged-in user's cap is 80% of 30G"
check "/dev/shm left alone" grep -qv 'size=31457280k' <<<"$(findmnt -no OPTIONS /dev/shm)"
}
phase_quota() {
id "$PROBE_USER" >/dev/null 2>&1 || useradd -M "$PROBE_USER"
local uid file=/tmp/vmbox-quota-probe out
uid="$(id -u "$PROBE_USER")"
/usr/bin/python3 "$HELPER" set --uid "$uid" --mount /tmp --limit-kib 51200
if out="$(as_user "$PROBE_USER" dd if=/dev/zero of="$file" bs=1M count=60 2>&1)"; then
bad "60M write under a 50M cap should fail"
else
grep -m1 'quota' <<<"$out" || true
check "60M write under a 50M cap fails with EDQUOT" grep -q 'quota exceeded' <<<"$out"
fi
rm -f "$file"
/usr/bin/python3 "$HELPER" set --uid "$uid" --mount /tmp --limit-kib 204800
check "100M write after raising the cap live to 200M succeeds" \
as_user "$PROBE_USER" dd if=/dev/zero of="$file" bs=1M count=100
/usr/bin/python3 "$HELPER" get --uid "$uid" --mount /tmp
rm -f "$file"
}
phase_disk() {
findmnt -no SOURCE,FSTYPE,OPTIONS /var/disktmp
check "/var/disktmp is an ext4 loop mount" \
grep -q '^/dev/loop[0-9]* ext4' <<<"$(findmnt -no SOURCE,FSTYPE /var/disktmp)"
check "/var/disktmp is mode 1777" test "$(stat -c %a /var/disktmp)" = 1777
check "image is 64G sparse" test "$(stat -c %s /var/lib/disktmp/disktmp.img)" = 68719476736
du -h /var/lib/disktmp/disktmp.img
as_user "$USER_NAME" sh -c 'echo hello >/var/disktmp/vmbox-user-file'
sync
check "user's file landed inside the image" \
grep -q 'vmbox-user-file' <<<"$(debugfs -R 'ls -l /' /var/lib/disktmp/disktmp.img 2>/dev/null)"
as_user "$USER_NAME" sh -c ": >$DISK_CANARY"
}
phase_again() {
local out
out="$("$ENTRY")"
printf 'second run: %s\n' "$out"
check "second run prints one line" test "$(wc -l <<<"$out")" = 1
check "second run says nothing changed" grep -q 'nothing changed' <<<"$out"
check "--verify-only exits 0 as root" "$ENTRY" --verify-only
as_user "$USER_NAME" "$ENTRY" --verify-only || true
check "--verify-only exits 0 as the user" as_user "$USER_NAME" "$ENTRY" --verify-only
}
phase_rebooted() {
findmnt -no OPTIONS /tmp
check "/tmp is 30G from the drop-in" grep -q 'size=31457280k' <<<"$(findmnt -no OPTIONS /tmp)"
journalctl -b -u "user-runtime-dir@${USER_UID}" --no-pager | grep 'quota.*/tmp' || true
local cap
cap="$(cap_kib "$USER_UID")"
printf 'uid %s cap %s KiB at login\n' "$USER_UID" "$cap"
check_cap "$cap" "login cap is systemd's 80% of 30G"
check "/var/disktmp is mounted" findmnt -M /var/disktmp
check "/var/disktmp was emptied at boot" test ! -e "$DISK_CANARY"
check "--verify-only exits 0 after reboot" "$ENTRY" --verify-only
}
case "${1:-}" in
before) phase_before ;;
apply) phase_apply ;;
quota) phase_quota ;;
disk) phase_disk ;;
again) phase_again ;;
rebooted) phase_rebooted ;;
*)
echo "Usage: $0 before|apply|quota|disk|again|rebooted" >&2
exit 2
;;
esac
exit "$FAILED"

View File

@ -0,0 +1,202 @@
#!/bin/bash
# Grow the /tmp tmpfs to 30G and add a capped, disk-backed temp area.
#
# Sourced by fix_tmp_space.sh; the caller sets strict mode. Every function
# returns non-zero instead of exiting, so the tests can call them directly.
# Every path is env-overridable so tests/tmp_space_harness.sh can point it
# into a tmpdir (run_all.sh runs un-jailed in CI).
#
# Why: the stock tmp.mount is size=50% with usrquota, and systemd caps each
# user at 80% of it at login (systemd-user-runtime-dir). A heavy build hit
# that cap with EDQUOT on 2026-11-22.
TS_DROPIN_DIR="${TMP_MOUNT_DROPIN_DIR:-/etc/systemd/system/tmp.mount.d}"
TS_FSTAB="${FSTAB_FILE:-/etc/fstab}"
TS_TMPFILES_DIR="${TMPFILES_CONF_DIR:-/etc/tmpfiles.d}"
TS_IMAGE_DIR="${DISKTMP_IMAGE_DIR:-/var/lib/disktmp}"
TS_MOUNT="${DISKTMP_MOUNT:-/var/disktmp}"
TS_TMP="${TMP_MOUNT_POINT:-/tmp}"
TS_RUN_USER="${RUN_USER_DIR:-/run/user}"
# System python on purpose: the helper needs nothing outside the stdlib and
# must not depend on whichever venv is first on root's PATH.
TS_PYTHON="${TMP_SPACE_PYTHON:-/usr/bin/python3}"
TS_HELPER="${TMP_QUOTA_HELPER:-$(dirname "${BASH_SOURCE[0]}")/../tmp_quota.py}"
TS_IMAGE="${TS_IMAGE_DIR}/disktmp.img"
readonly TS_TMP_BYTES=$((30 * 1024 * 1024 * 1024))
# systemd's own login cap is 80% of the mount, computed from statfs with
# its own rounding; 1 MiB of slack lets its value count as "already right".
readonly TS_CAP_PERCENT=80
readonly TS_CAP_SLACK_KIB=1024
TS_CHANGED=0 # set by any write; the second run reports "nothing changed"
ts_say() { printf 'tmp-space: %s\n' "$*"; }
ts_err() { printf 'tmp-space: ERROR: %s\n' "$*" >&2; }
ts_human() { numfmt --to=iec "$1"; }
ts_dropin_body() {
cat <<'DROPIN'
[Mount]
Options=mode=1777,strictatime,nosuid,nodev,size=30G,nr_inodes=1m,x-systemd.graceful-option=usrquota
DROPIN
}
# nofail drops the mount's ordering before local-fs.target, and with it the
# ordering before systemd-tmpfiles-setup: in the vmbox guest tmpfiles started
# 22 ms before the mount finished, so its boot-time emptying could hit the
# directory underneath instead of the image. x-systemd.before restores it.
ts_fstab_line() {
printf '%s %s ext4 %s 0 0\n' "$TS_IMAGE" "$TS_MOUNT" \
loop,nosuid,nodev,noatime,discard,nofail,x-systemd.before=systemd-tmpfiles-setup.service
}
ts_tmpfiles_body() { printf 'D %s 1777 root root 10d\n' "$TS_MOUNT"; }
# ts_fstype MOUNTPOINT — the filesystem type mounted exactly there, or "".
ts_fstype() { findmnt -no FSTYPE --mountpoint "$1" 2>/dev/null || true; }
ts_tmp_bytes() {
local blocks bsize
read -r blocks bsize < <(stat -f -c '%b %S' "$TS_TMP")
printf '%s\n' $((blocks * bsize))
}
# ts_write FILE BODY — write BODY to FILE unless it already holds exactly it.
ts_write() {
local file="$1" body="$2"
[[ -f "$file" && "$(cat "$file")" == "$body" ]] && return 0
mkdir -p "$(dirname "$file")"
printf '%s\n' "$body" >"$file"
ts_say "wrote ${file}"
TS_CHANGED=1
}
# ts_fstab_state — "ours", "foreign" (another line owns the mount point) or
# "absent".
ts_fstab_state() {
local want line
want="$(ts_fstab_line)"
while IFS= read -r line; do
[[ "$line" == "$want" ]] && {
echo ours
return 0
}
done <"$TS_FSTAB"
if awk -v m="$TS_MOUNT" '$1 !~ /^#/ && $2 == m { f = 1 } END { exit !f }' \
"$TS_FSTAB"; then
echo foreign
else
echo absent
fi
}
ts_target_kib() { echo $(($(ts_tmp_bytes) * TS_CAP_PERCENT / 100 / 1024)); }
# ts_quota UID — "<limit_kib> <used_kib>" from the helper; fails with its
# message when quotactl does.
ts_quota() {
"$TS_PYTHON" "$TS_HELPER" get --uid "$1" --mount "$TS_TMP"
}
# ts_uids — every logged-in non-root uid: the set systemd capped at login.
ts_uids() {
local dir uid
for dir in "$TS_RUN_USER"/*/; do
uid="$(basename "$dir")"
[[ "$uid" =~ ^[0-9]+$ ]] && ((uid != 0)) && echo "$uid"
done
return 0
}
ts_apply_files() {
if [[ "$(ts_fstype "$TS_TMP")" != tmpfs ]]; then
ts_err "${TS_TMP} is not a tmpfs; refusing to touch it"
return 1
fi
local state
state="$(ts_fstab_state)"
if [[ "$state" == foreign ]]; then
ts_err "${TS_FSTAB} already has a different line for ${TS_MOUNT}"
return 1
fi
local before="$TS_CHANGED"
ts_write "${TS_DROPIN_DIR}/size.conf" "$(ts_dropin_body)"
if [[ "$state" == absent ]]; then
# Fail-open on purpose: nofail lets the machine boot without the
# image, and the 1777 directory underneath then takes the writes
# uncapped instead of every command that uses it failing.
printf '# Disk-backed temp area (fix_tmp_space.sh)\n%s\n' \
"$(ts_fstab_line)" >>"$TS_FSTAB"
ts_say "added ${TS_MOUNT} to ${TS_FSTAB}"
TS_CHANGED=1
fi
# One reload covers the drop-in and the fstab line.
if ((TS_CHANGED != before)); then
systemctl daemon-reload || return 1
fi
ts_write "${TS_TMPFILES_DIR}/disktmp.conf" "$(ts_tmpfiles_body)"
}
# Remount live: never unmount or restart tmp.mount, /tmp/.X11-unix is there.
ts_apply_tmp() {
(($(ts_tmp_bytes) == TS_TMP_BYTES)) && return 0
mount -o remount,size=30G "$TS_TMP" || return 1
ts_say "remounted ${TS_TMP} at 30G"
TS_CHANGED=1
}
# Raise each logged-in user's cap live; systemd only sets it at login.
# Restarting user-runtime-dir@ would end the session, so never do that.
ts_apply_caps() {
local target uid out limit
target="$(ts_target_kib)"
for uid in $(ts_uids); do
out="$(ts_quota "$uid")" || return 1
read -r limit _ <<<"$out"
# 0 is "no limit": raising it to 80% would be a cut, not a raise.
((limit == 0 || limit >= target - TS_CAP_SLACK_KIB)) && continue
"$TS_PYTHON" "$TS_HELPER" set --uid "$uid" --mount "$TS_TMP" \
--limit-kib "$target" >/dev/null || return 1
ts_say "raised uid ${uid}'s ${TS_TMP} cap to $(ts_human $((target * 1024)))"
TS_CHANGED=1
done
}
ts_apply_disk() {
# The directory stays searchable so --verify-only can stat the image as a
# user; the image itself is 0600, it holds every user's temp files.
mkdir -p "$TS_IMAGE_DIR"
if [[ ! -e "$TS_IMAGE" ]]; then
truncate -s 64G "$TS_IMAGE" || return 1
chmod 0600 "$TS_IMAGE"
mkfs.ext4 -F -q -m 0 -L disktmp "$TS_IMAGE" || return 1
ts_say "created ${TS_IMAGE}"
TS_CHANGED=1
elif [[ "$(blkid -o value -s TYPE "$TS_IMAGE" 2>/dev/null)" != ext4 ]]; then
# Fail-closed: never reformat a file this script did not just make.
ts_err "${TS_IMAGE} exists but is not ext4; refusing to format it"
return 1
fi
if [[ "$(ts_fstype "$TS_MOUNT")" != ext4 ]]; then
mkdir -p "$TS_MOUNT"
chmod 1777 "$TS_MOUNT" # the fail-open directory under the mount
mount -T "$TS_FSTAB" "$TS_MOUNT" || return 1
ts_say "mounted ${TS_MOUNT}"
TS_CHANGED=1
fi
if [[ "$(stat -c %a "$TS_MOUNT")" != 1777 ]]; then
chmod 1777 "$TS_MOUNT"
TS_CHANGED=1
fi
}
ts_apply() {
TS_CHANGED=0
ts_apply_files || return 1
ts_apply_tmp || return 1
ts_apply_caps || return 1
ts_apply_disk || return 1
((TS_CHANGED == 0)) && ts_say "already applied, nothing changed"
return 0
}

View File

@ -0,0 +1,74 @@
#!/bin/bash
# fix_tmp_space.sh --verify-only: read-only report of every item ts_apply
# writes; sets TS_DRIFT and returns non-zero on any mismatch.
#
# Sourced after tmp_space.sh (split out for the 250-line cap); uses its paths,
# constants and probes. Changes nothing, so it runs without root: a user may
# read its own quota.
readonly TS_IMAGE_BYTES=$((64 * 1024 * 1024 * 1024))
TS_DRIFT=0 # set on any mismatch
# ts_report WHAT OK — one verify line; OK=0 marks drift.
ts_report() {
if (($2)); then
ts_say "$1"
else
ts_say "$1 DRIFT"
TS_DRIFT=1
fi
}
# ts_report_file FILE BODY — drift unless FILE holds exactly BODY.
ts_report_file() {
local ok=0
[[ -f "$1" && "$(cat "$1")" == "$2" ]] && ok=1
ts_report "$1" "$ok"
}
ts_verify_caps() {
local target uid out limit used
target="$(ts_target_kib)"
for uid in $(ts_uids); do
if ! out="$(ts_quota "$uid")"; then
TS_DRIFT=1
continue
fi
read -r limit used <<<"$out"
if ((limit == 0)); then
ts_report "uid ${uid} cap unlimited, used $(ts_human $((used * 1024)))" 1
continue
fi
ts_report "uid ${uid} cap $(ts_human $((limit * 1024))), used $(ts_human $((used * 1024))) (want $(ts_human $((target * 1024))))" \
$((limit >= target - TS_CAP_SLACK_KIB))
done
}
ts_verify_disk() {
local size=0 mode
[[ -f "$TS_IMAGE" ]] && size="$(stat -c %s "$TS_IMAGE")"
if [[ "$(ts_fstype "$TS_MOUNT")" != ext4 ]]; then
ts_report "${TS_MOUNT} $(ts_human "$size") image, not mounted" 0
return 0
fi
mode="$(stat -c %a "$TS_MOUNT")"
ts_report "${TS_MOUNT} $(ts_human "$size") image, mounted ext4, mode ${mode}" \
$((size == TS_IMAGE_BYTES && mode == 1777))
}
# ts_verify — read-only; works without root (a user may read its own quota).
ts_verify() {
TS_DRIFT=0
local bytes
bytes="$(ts_tmp_bytes)"
ts_report "${TS_TMP} size $(ts_human "$bytes") (want 30G)" \
$((bytes == TS_TMP_BYTES))
ts_report_file "${TS_DROPIN_DIR}/size.conf" "$(ts_dropin_body)"
local fstab_ok=0
[[ "$(ts_fstab_state)" == ours ]] && fstab_ok=1
ts_report "${TS_MOUNT} line in ${TS_FSTAB}" "$fstab_ok"
ts_report_file "${TS_TMPFILES_DIR}/disktmp.conf" "$(ts_tmpfiles_body)"
ts_verify_caps
ts_verify_disk
return "$TS_DRIFT"
}

View File

@ -0,0 +1,160 @@
#!/usr/bin/env python3
"""Get or set one user's block quota on a mounted filesystem via quotactl_fd.
Used by fix_tmp_space.sh to raise a logged-in user's cap on the /tmp tmpfs
live: systemd sets that cap only at login, and restarting
user-runtime-dir@<uid> to re-apply it would end the session.
tmp_quota.py get --uid 1000 --mount /tmp
tmp_quota.py set --uid 1000 --mount /tmp --limit-kib 25165824
Both print "<limit_kib> <used_kib>" (the limit after the change, for set).
A limit of 0 means "no limit". Exits 1 with one line on stderr on failure.
Only the block limits are written; inode limits and usage are left alone.
"""
from __future__ import annotations
import argparse
import ctypes
from dataclasses import dataclass
import os
import sys
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable, Sequence
Syscall = Callable[[int, int, int, int, int], int]
SYS_QUOTACTL_FD = 443 # x86_64 (asm/unistd_64.h)
Q_GETQUOTA = 0x800007
Q_SETQUOTA = 0x800008
USRQUOTA = 0
SUBCMDSHIFT = 8
QIF_BLIMITS = 1
class IfDqblk(ctypes.Structure):
"""``struct if_dqblk`` from linux/quota.h; limits in KiB, space in bytes."""
_fields_ = (
("dqb_bhardlimit", ctypes.c_uint64),
("dqb_bsoftlimit", ctypes.c_uint64),
("dqb_curspace", ctypes.c_uint64),
("dqb_ihardlimit", ctypes.c_uint64),
("dqb_isoftlimit", ctypes.c_uint64),
("dqb_curinodes", ctypes.c_uint64),
("dqb_btime", ctypes.c_uint64),
("dqb_itime", ctypes.c_uint64),
("dqb_valid", ctypes.c_uint32),
)
class QuotaError(Exception):
"""quotactl_fd failed, or the kernel did not keep the requested limit."""
@dataclass(frozen=True)
class Quota:
"""One user's block quota: hard limit and current use, both in KiB."""
limit_kib: int
used_kib: int
def qcmd(cmd: int) -> int:
"""Encode ``QCMD(cmd, USRQUOTA)``."""
return (cmd << SUBCMDSHIFT) | USRQUOTA
def libc_syscall() -> ctypes._FuncPointer:
"""Return libc's ``syscall`` with explicit types (cmd exceeds INT_MAX)."""
fn = ctypes.CDLL(None, use_errno=True).syscall
fn.argtypes = [
ctypes.c_long,
ctypes.c_int,
ctypes.c_uint,
ctypes.c_uint,
ctypes.c_void_p,
]
fn.restype = ctypes.c_long
return fn
def _call(mount: str, cmd: int, uid: int, block: IfDqblk, syscall: Syscall) -> None:
"""Run one quotactl_fd command against ``mount`` for ``uid``."""
try:
fd = os.open(mount, os.O_RDONLY | os.O_DIRECTORY)
except OSError as exc:
msg = f"cannot open {mount}: {exc.strerror}"
raise QuotaError(msg) from exc
try:
if syscall(SYS_QUOTACTL_FD, fd, qcmd(cmd), uid, ctypes.addressof(block)):
err = ctypes.get_errno()
msg = f"quotactl_fd on {mount} for uid {uid}: {os.strerror(err)}"
raise QuotaError(msg)
finally:
os.close(fd)
def get_quota(mount: str, uid: int, syscall: Syscall) -> Quota:
"""Read ``uid``'s block quota on ``mount``."""
block = IfDqblk()
_call(mount, Q_GETQUOTA, uid, block, syscall)
return Quota(limit_kib=block.dqb_bhardlimit, used_kib=block.dqb_curspace // 1024)
def set_limit(mount: str, uid: int, limit_kib: int, syscall: Syscall) -> Quota:
"""Set soft = hard = ``limit_kib`` (as systemd does), then read it back."""
block = IfDqblk(
dqb_bhardlimit=limit_kib, dqb_bsoftlimit=limit_kib, dqb_valid=QIF_BLIMITS
)
_call(mount, Q_SETQUOTA, uid, block, syscall)
quota = get_quota(mount, uid, syscall)
if quota.limit_kib != limit_kib:
msg = f"uid {uid} on {mount}: set {limit_kib} KiB, reads back {quota.limit_kib}"
raise QuotaError(msg)
return quota
def _non_negative(text: str) -> int:
"""Parse an argument that must be an integer >= 0."""
value = int(text)
if value < 0:
msg = f"must be >= 0: {text}"
raise argparse.ArgumentTypeError(msg)
return value
def _parser() -> argparse.ArgumentParser:
"""Build the get/set command line."""
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
sub = parser.add_subparsers(dest="action", required=True)
for name in ("get", "set"):
cmd = sub.add_parser(name)
cmd.add_argument("--uid", type=_non_negative, required=True)
cmd.add_argument("--mount", required=True)
if name == "set":
cmd.add_argument("--limit-kib", type=_non_negative, required=True)
return parser
def main(argv: Sequence[str] | None = None, syscall: Syscall | None = None) -> int:
"""Run the command line; return the exit code."""
args = _parser().parse_args(argv)
call = syscall if syscall is not None else libc_syscall()
try:
if args.action == "set":
quota = set_limit(args.mount, args.uid, args.limit_kib, call)
else:
quota = get_quota(args.mount, args.uid, call)
except QuotaError as exc:
sys.stderr.write(f"tmp_quota: {exc}\n")
return 1
sys.stdout.write(f"{quota.limit_kib} {quota.used_kib}\n")
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@ -0,0 +1,155 @@
"""Tests for linux_configuration/fixes/tmp_quota.py (quotactl_fd helper).
The kernel is never called: every test passes a fake ``syscall`` that reads
or fills the ``if_dqblk`` struct at the address it is given, the way
quotactl_fd(2) does.
"""
from __future__ import annotations
import ctypes
import errno
from typing import TYPE_CHECKING
import pytest
import tmp_quota as tq
if TYPE_CHECKING:
from pathlib import Path
LIMIT_KIB = 25165824
USED_BYTES = 3 * 1024 * 1024
UID = 1000
class FakeKernel:
"""Holds one user's quota and answers Q_GETQUOTA / Q_SETQUOTA."""
def __init__(self, limit_kib: int = 0, *, fail: int = 0) -> None:
"""Start with ``limit_kib`` and fail every call with ``fail`` if set."""
self.limit_kib = limit_kib
self.fail = fail
self.ignore_set = False
self.calls: list[tuple[int, int, int]] = []
self.set_blocks: list[tq.IfDqblk] = []
def __call__(self, nr: int, fd: int, cmd: int, qid: int, addr: int) -> int:
"""Act as syscall(2) for quotactl_fd."""
assert fd >= 0
self.calls.append((nr, cmd, qid))
if self.fail:
ctypes.set_errno(self.fail)
return -1
block = tq.IfDqblk.from_address(addr)
if cmd == tq.qcmd(tq.Q_GETQUOTA):
block.dqb_bhardlimit = self.limit_kib
block.dqb_bsoftlimit = self.limit_kib
block.dqb_curspace = USED_BYTES
block.dqb_valid = 63
else:
copy = tq.IfDqblk.from_buffer_copy(block)
self.set_blocks.append(copy)
if not self.ignore_set:
self.limit_kib = block.dqb_bhardlimit
return 0
def test_qcmd_matches_the_kernel_encoding() -> None:
"""QCMD(Q_GETQUOTA, USRQUOTA) is 0x80000700."""
assert tq.qcmd(tq.Q_GETQUOTA) == 0x80000700
assert tq.qcmd(tq.Q_SETQUOTA) == 0x80000800
def test_get_reads_limit_and_use(tmp_path: Path) -> None:
"""GET returns the hard limit in KiB and the use converted to KiB."""
kernel = FakeKernel(LIMIT_KIB)
quota = tq.get_quota(str(tmp_path), UID, kernel)
assert quota == tq.Quota(limit_kib=LIMIT_KIB, used_kib=3072)
assert kernel.calls == [(443, 0x80000700, UID)]
def test_get_reports_the_kernel_error(tmp_path: Path) -> None:
"""A failing syscall becomes a QuotaError naming the mount, uid and errno."""
kernel = FakeKernel(fail=errno.ESRCH)
with pytest.raises(tq.QuotaError, match="No such process") as caught:
tq.get_quota(str(tmp_path), UID, kernel)
assert str(tmp_path) in str(caught.value)
assert "uid 1000" in str(caught.value)
def test_get_reports_an_unopenable_mount(tmp_path: Path) -> None:
"""A mount point that cannot be opened is a QuotaError, not a traceback."""
with pytest.raises(tq.QuotaError, match="cannot open"):
tq.get_quota(str(tmp_path / "missing"), UID, FakeKernel())
def test_set_writes_only_the_block_limits(tmp_path: Path) -> None:
"""SET sends soft = hard = limit and QIF_BLIMITS, nothing else."""
kernel = FakeKernel(1600201)
quota = tq.set_limit(str(tmp_path), UID, LIMIT_KIB, kernel)
(sent,) = kernel.set_blocks
assert (sent.dqb_bhardlimit, sent.dqb_bsoftlimit) == (LIMIT_KIB, LIMIT_KIB)
assert sent.dqb_valid == tq.QIF_BLIMITS
assert quota.limit_kib == LIMIT_KIB
def test_set_fails_when_the_limit_does_not_stick(tmp_path: Path) -> None:
"""A read-back that differs from the request is a QuotaError."""
kernel = FakeKernel(1600201)
kernel.ignore_set = True
with pytest.raises(tq.QuotaError, match="reads back 1600201"):
tq.set_limit(str(tmp_path), UID, LIMIT_KIB, kernel)
def test_main_get_prints_two_numbers(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""``get`` prints "<limit_kib> <used_kib>" for the shell to read."""
argv = ["get", "--uid", "1000", "--mount", str(tmp_path)]
assert tq.main(argv, FakeKernel(LIMIT_KIB)) == 0
assert capsys.readouterr().out == f"{LIMIT_KIB} 3072\n"
def test_main_set_prints_the_read_back(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""``set`` prints the quota as the kernel reports it afterwards."""
argv = ["set", "--uid", "1000", "--mount", str(tmp_path)]
argv += ["--limit-kib", str(LIMIT_KIB)]
assert tq.main(argv, FakeKernel(1)) == 0
assert capsys.readouterr().out == f"{LIMIT_KIB} 3072\n"
def test_main_reports_failure_on_stderr(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A QuotaError exits 1 with one clear line on stderr."""
argv = ["get", "--uid", "1000", "--mount", str(tmp_path)]
assert tq.main(argv, FakeKernel(fail=errno.EPERM)) == 1
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err.startswith("tmp_quota: ")
assert "Operation not permitted" in captured.err
def test_main_rejects_a_negative_limit(tmp_path: Path) -> None:
"""A negative limit is an argument error, never sent to the kernel."""
kernel = FakeKernel()
argv = ["set", "--uid", "1000", "--mount", str(tmp_path), "--limit-kib", "-1"]
with pytest.raises(SystemExit) as caught:
tq.main(argv, kernel)
assert caught.value.code == 2
assert kernel.calls == []
def test_libc_syscall_is_typed() -> None:
"""The real syscall entry point has explicit argument and return types."""
fn = tq.libc_syscall()
assert fn.restype is ctypes.c_long
assert fn.argtypes == [
ctypes.c_long,
ctypes.c_int,
ctypes.c_uint,
ctypes.c_uint,
ctypes.c_void_p,
]