testsAndMisc/linux_configuration/fixes/lib/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

75 lines
2.1 KiB
Bash
Executable File

#!/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"
}