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

81 lines
2.1 KiB
Bash
Executable File

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