Compare commits

...

2 Commits

Author SHA1 Message Date
32b94bfbd2 Set AMD X3D V-cache mode to cache via udev
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Has been cancelled
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Has been cancelled
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Has been cancelled
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Has been cancelled
Python tests / test (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
Shell tests / shell-tests (push) Has been cancelled
Shell tests / lib-tests (push) Has been cancelled
Shell tests / arch-tests (push) Has been cancelled
amd_x3d_mode defaulted to frequency, so the scheduler preferred the
non-V-cache CCD. kuhy chose cache: games prefer CPUs 0-5,12-17; background
jobs are already pinned to 6-11,18-23.

A udev rule matches DRIVER amd_x3d_vcache on add (boot coldplug) and bind
(any rebind), so it hard-codes no device name and survives a rebind,
unlike a tmpfiles w line. fixes/fix_amd_x3d_vcache.sh installs it, fires
a synthetic bind event and checks the mode ends on cache. Same layout as
fix_webcam_c270.sh, with a bats test.

Host: mode frequency -> cache, prefcore ranking now favours 0-5,12-17.
vmbox has no X3D device: only the install step was proven there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYWDKuhbBAoNETfoB9
2026-11-12 13:03:12 +01:00
b57d5f9064 Install live scripts by atomic rename
The periodic setup wrote /usr/local/bin scripts with sed > target,
truncating the file in place. bash reads scripts lazily, so the running
hosts-file-monitor could execute a half-new file.

New lib/atomic_install.sh renders into a temp file beside the target,
syntax-checks it, sets mode/owner and renames it over the target (new
inode). A bad template fails closed and leaves the live file untouched.
Applied to the maintenance script, hosts monitor, browser wrapper and
auto-update script; the hosts monitor service is restarted if running.

Test: test_services_atomic_install.sh (inode changes, old inode keeps
old bytes, syntax error and missing template leave target untouched).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYWDKuhbBAoNETfoB9
2026-11-12 12:59:59 +01:00
13 changed files with 423 additions and 86 deletions

View File

@ -0,0 +1,11 @@
{
"title": "AMD X3D V-cache mode set to cache, persistently",
"objective": "amd_x3d_mode reads cache after boot and after a driver rebind, installed by a repo script using a driver-matching udev rule.",
"acceptance_criteria": [
"amd_x3d_mode reads cache on the host after install",
"prefcore ranking of CPUs 0-5,12-17 exceeds 6-11,18-23",
"The rule hard-codes no device name and survives unbind/bind"
],
"out_of_scope": ["Pinning background jobs", "Other CPU tuning"],
"verifier": "bats linux_configuration/tests/test_fix_amd_x3d_vcache.bats plus host sysfs reads"
}

View File

@ -0,0 +1,11 @@
{
"title": "Atomic install of live periodic-setup scripts",
"objective": "Installing the periodic system scripts never truncates a file a service is running; the target gets a new inode and a bad template leaves the live file untouched.",
"acceptance_criteria": [
"Target inode changes and the old inode keeps its bytes",
"A template failing bash -n is not installed and leaves no temp file",
"hosts-file-monitor.service is restarted only if it was running"
],
"out_of_scope": ["Re-running the installer on the host", "Other installers"],
"verifier": "lib/tests/test_services_atomic_install.sh, lib/tests/run_all.sh, vmbox guest check"
}

View File

@ -0,0 +1,38 @@
{
"intent": "AMD X3D V-cache mode is cache on every boot and driver rebind, so the scheduler prefers the V-cache CCD for games.",
"scope": [
"linux_configuration/fixes/60-amd-x3d-vcache-mode.rules",
"linux_configuration/fixes/fix_amd_x3d_vcache.sh",
"linux_configuration/tests/test_fix_amd_x3d_vcache.bats",
"Non-goal: CPU pinning of background jobs (already done)"
],
"changes": [
"udev rule matching DRIVER amd_x3d_vcache on add and bind, no device name",
"Installer with install, --remove and --status, same pattern as fix_webcam_c270.sh"
],
"verification": [
{
"command": "bats linux_configuration/tests/test_fix_amd_x3d_vcache.bats",
"result": "pass",
"evidence": "red while the script was missing; 8 ok after"
},
{
"command": "fixes/fix_amd_x3d_vcache.sh on the host",
"result": "pass",
"evidence": "amd_x3d_mode frequency -> cache; prefcore ranking CPUs 0-5,12-17 now 196-216 vs 6-11,18-23 166-191 (before 166-191 vs 196-216); unbind/bind keeps cache"
},
{
"command": "vm run atomichosts fix_amd_x3d_vcache.sh",
"result": "pass",
"evidence": "installs the rule, udevadm verify clean; no X3D device in vmbox so the mode itself is host-only"
}
],
"risks": [
"Games on the V-cache CCD may boost lower than on the frequency CCD",
"Background load spills onto the V-cache CCD if the pinning is lifted"
],
"rollback": [
"fixes/fix_amd_x3d_vcache.sh --remove, then write frequency to amd_x3d_mode or reboot",
"Check cat /sys/bus/platform/drivers/amd_x3d_vcache/*/amd_x3d_mode"
]
}

View File

@ -0,0 +1,40 @@
{
"intent": "The periodic setup replaces live /usr/local/bin scripts by atomic rename, so a running hosts-file-monitor never executes a half-written file.",
"scope": [
"linux_configuration/periodic_background/lib/atomic_install.sh",
"linux_configuration/periodic_background/setup_periodic_system.sh",
"linux_configuration/periodic_background/lib/periodic_browser.sh",
"linux_configuration/periodic_background/lib/tests/test_services_atomic_install.sh",
"Non-goal: re-running the installer on the host"
],
"changes": [
"install_rendered_script renders to a temp file beside the target, bash -n, chmod/chown, mv -f",
"Maintenance script, hosts monitor, browser wrapper and auto-update script use it",
"restart_if_active restarts hosts-file-monitor.service after install"
],
"verification": [
{
"command": "lib/tests/test_services_atomic_install.sh",
"result": "pass",
"evidence": "red against in-place write (inode kept, old fd saw new bytes), 12 passed after"
},
{
"command": "vm run atomichosts guest_check.sh",
"result": "pass",
"evidence": "real hosts-file-monitor template under systemd: inode 85520 -> 85524, MainPID 745 -> 768, service active"
},
{
"command": "lib/tests/run_all.sh",
"result": "pass",
"evidence": "all check_and_enable_services test files passed"
}
],
"risks": [
"Temp file left in /usr/local/bin if the installer is killed between mktemp and mv",
"Restart briefly interrupts the hosts monitor"
],
"rollback": [
"git revert the commit; the old installer overwrites in place again",
"Check systemctl is-active hosts-file-monitor.service"
]
}

View File

@ -1,30 +0,0 @@
{
"intent": "The i3 gaming-budget block shows the new earned_time 0.3.0 automation earner (anki-guard's Automation-deck quota) as ⚙ instead of its text label.",
"scope": [
"linux_configuration/i3blocks/gaming_budget.sh",
"linux_configuration/tests/test_i3blocks_gaming_budget.sh",
"Non-goal: no change to how the budget is computed or rendered; only the icon map grows"
],
"changes": [
"Icon map gains automation: ⚙ beside workout 💪, leetcode 🧩, reading 📖, anki 🗂",
"The .rules.earners test case lists an automation earner and expects '+⚙30m' in registry order"
],
"verification": [
{
"command": "bash linux_configuration/i3blocks/gaming_budget.sh (live enforcer API, after restarting on earned-time 0.3.0)",
"result": "pass",
"evidence": "'🎮 1h19 left · 5h44/7h03 +📖1h +🗂30m +⚙30m'; /api/budget lists automation 0/1800"
},
{
"command": "bash linux_configuration/tests/test_i3blocks_gaming_budget.sh",
"result": "pass",
"evidence": "All gaming_budget checks passed, incl. '+🧩1h +📖1h +🗂30m +⚙30m +Piano1h30'"
}
],
"risks": [
"⚙ (U+2699) without a variation selector may render as text-style in some fonts; it is still one glyph wide."
],
"rollback": [
"git revert this commit; automation then shows as its label 'Automation30m'"
]
}

View File

@ -1,41 +0,0 @@
{
"intent": "A firewall apply (setup_wireguard_ssh.sh allow-*) no longer bounces Docker and its containers, and the DoT rule (tcp/853 on wg0) survives every re-apply instead of vanishing.",
"scope": [
"linux_configuration/features/lib/wg_firewall.sh: ruleset replaces only 'table inet filter'; restore_docker_rules restarts docker only when its nat DOCKER chain is gone; ALLOW_DOT renders 853 on wg0; allow_dot()",
"linux_configuration/features/setup_wireguard_ssh.sh, lib/wg_sshd.sh: ALLOW_DOT default/save_config, allow-dot subcommand and usage",
"linux_configuration/features/lib/dot_resolver_install.sh: wg-bound branch goes through 'setup_wireguard_ssh.sh allow-dot' instead of a runtime 'nft add rule'",
"Tests: new lib/tests/test_wg_firewall.sh; test_dot_resolver_install.sh wg-branch cases rewritten",
"Non-goal: the publicly bound DoT branch still adds its rate-limited rule at runtime"
],
"changes": [
"'flush ruleset' replaced by 'table inet filter' + 'delete table inet filter' before the definition: atomic, leaves Docker's (iptables-nft) and Tailscale's tables alone, and loads on a first boot",
"restore_docker_rules checks 'iptables -t nat -S DOCKER' and only restarts docker when that chain is missing",
"sshd-death rollback deletes only inet filter instead of flushing the whole ruleset",
"ALLOW_DOT persisted in .wireguard_ssh.conf; open_firewall calls allow-dot so the rule is rendered on every apply"
],
"verification": [
{
"command": "vmbox guest dotfw (iptables-nft, docker 29 + busybox probe, an extra 'inet sentinel' table): old HEAD allow-homeassistant vs new allow-dot",
"result": "pass",
"evidence": "old: all non-inet-filter tables deleted, docker + probe restarted; new re-apply from healthy state: docker ActiveEnterTimestamp and probe StartedAt unchanged, sentinel and docker tables kept; verify clean; systemctl restart nftables and first-boot load (no table) rc=0"
},
{
"command": "sudo setup_wireguard_ssh.sh allow-dot (host, iptables legacy)",
"result": "pass",
"evidence": "'docker's own firewall rules are intact; containers untouched'; docker since 2026-09-28 12:41:15 and gitea-caddy StartedAt unchanged; live input chain gains iifname wg0 tcp dport 853 accept; verify: nftables.conf matches; anki/gitea HTTPS 200"
},
{
"command": "lib/tests/test_wg_firewall.sh (root in vmbox) ; shell_coverage_jail.sh --subject lib/tests/run_all.sh <jail_args> --min 0 --fail-on-case-error",
"result": "pass",
"evidence": "12 passed incl. real nft -c; jailed suite rc=0"
}
],
"risks": [
"Tables left over from an older ruleset outside inet filter are no longer flushed by an apply (none exist on the host).",
"If docker's rules live somewhere other than the nat DOCKER chain, a missing-rules state would not trigger the rebuild."
],
"rollback": [
"git revert this commit, then sudo setup_wireguard_ssh.sh allow-web to re-render (old code restarts docker once)",
"After rollback: setup_wireguard_ssh.sh verify, and confirm containers reach the network"
]
}

View File

@ -0,0 +1,12 @@
# AMD X3D (dual-CCD V-cache) CPU: prefer the V-cache CCD for games.
# amd_x3d_mode=cache makes the scheduler rank the V-cache CCD (CPUs 0-5,12-17)
# above the frequency CCD; the firmware default is "frequency". Background jobs
# are already pinned to the other CCD (6-11,18-23).
#
# Why udev and not tmpfiles.d: a tmpfiles `w` line needs the exact device path
# (a firmware-assigned ACPI name) and runs once at boot, so it
# misses a driver rebind or module reload. This rule matches the DRIVER, hard-
# codes no device name, and fires on "add" (boot coldplug, driver already
# bound) and on "bind" (every later driver (re)bind, e.g. modprobe -r/+).
# Installed by fixes/fix_amd_x3d_vcache.sh.
ACTION=="add|bind", SUBSYSTEM=="platform", DRIVER=="amd_x3d_vcache", ATTR{amd_x3d_mode}="cache"

View File

@ -0,0 +1,85 @@
#!/bin/bash
# ============================================================================
# AMD X3D: set the V-cache scheduling mode to "cache" (udev rule)
#
# The scheduler then prefers the V-cache CCD for games. The rule re-applies the
# setting on every boot and driver rebind; rationale lives in
# 60-amd-x3d-vcache-mode.rules.
#
# fix_amd_x3d_vcache.sh install the rule and apply it now
# fix_amd_x3d_vcache.sh --remove remove the rule (firmware default returns on reboot)
# fix_amd_x3d_vcache.sh --status show rule state and the live mode
# ============================================================================
set -euo pipefail
SCRIPT_NAME="$(basename "$0")"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_NAME SCRIPT_DIR
readonly RULE_NAME="60-amd-x3d-vcache-mode.rules"
readonly RULE_SRC="$SCRIPT_DIR/$RULE_NAME"
# Both overridable so the tests can point them at a scratch tree.
readonly RULE_DST="${X3D_RULES_DIR:-/etc/udev/rules.d}/$RULE_NAME"
readonly DRIVER_SYSFS="${X3D_DRIVER_SYSFS:-/sys/bus/platform/drivers/amd_x3d_vcache}"
log() { echo "[$SCRIPT_NAME] $*"; }
# Live mode of every bound X3D device, one "<device>=<mode>" per line.
live_modes() {
local f
for f in "$DRIVER_SYSFS"/*/amd_x3d_mode; do
[[ -e $f ]] || continue
echo "$(basename "$(dirname "$f")")=$(cat "$f")"
done
}
install_rule() {
sudo install -m 644 "$RULE_SRC" "$RULE_DST"
sudo udevadm control --reload
log "installed $RULE_DST"
# No driver, or a driver with no device (a VM): nothing to apply yet.
if [[ -z "$(live_modes)" ]]; then
log "no X3D device; rule applies when the driver binds"
return
fi
# A synthetic bind event runs the rule against the live device by driver,
# so no device name is needed here either.
sudo udevadm trigger --action=bind --subsystem-match=platform \
--property-match=DRIVER=amd_x3d_vcache
sudo udevadm settle --timeout=10
local modes
modes="$(live_modes)"
if [[ -n $modes ]] && ! grep -qv '=cache$' <<<"$modes"; then
log "applied: $modes"
else
log "mode is not cache after trigger: ${modes:-no device}"
exit 1
fi
}
remove_rule() {
sudo rm -f "$RULE_DST"
sudo udevadm control --reload
log "removed $RULE_DST"
}
show_status() {
if [[ -f $RULE_DST ]]; then
log "rule: installed"
else
log "rule: not installed"
fi
log "mode: $(live_modes | tr '\n' ' ')"
}
case "${1:-}" in
"") install_rule ;;
--remove) remove_rule ;;
--status) show_status ;;
-h | --help) sed -n '4,12p' "$0" | sed 's/^# \{0,3\}//' ;;
*)
log "unknown option: $1"
exit 2
;;
esac

View File

@ -0,0 +1,50 @@
#!/bin/bash
# Atomic install of live /usr/local/bin scripts for the periodic system setup.
#
# Sourced by setup_periodic_system.sh and periodic_browser.sh.
#
# Why not `sed ... > /usr/local/bin/x.sh`: that truncates the file in place.
# bash reads a script lazily, so a service already running it (the hosts
# monitor loops forever) can execute a half-new file. Rendering beside the
# target and renaming over it gives the target a NEW inode; a running instance
# keeps reading the old one until it exits or is restarted.
# Fail-closed: a template that is missing or fails `bash -n` leaves the live
# file untouched and returns non-zero.
# install_rendered_script <template> <dest> [sed args...]
# Renders <template> through sed with the given args into a temp file in
# dirname(<dest>) (same filesystem, so mv is a rename), syntax-checks it, sets
# mode 0755 and root ownership when run as root, then renames it over <dest>.
install_rendered_script() {
local template="$1" dest="$2"
shift 2
local tmp
[[ -f $template ]] || {
echo "Error: template not found: $template" >&2
return 1
}
tmp="$(mktemp "${dest}.XXXXXX")" || return 1
# `-e ''` keeps sed valid when the caller passes no expressions.
if sed -e '' "$@" "$template" >"$tmp" &&
bash -n "$tmp" &&
chmod 0755 "$tmp" &&
{ [[ $EUID -ne 0 ]] || chown root:root "$tmp"; } &&
mv -f "$tmp" "$dest"; then
return 0
fi
rm -f "$tmp"
echo "Error: not installing $dest (render or syntax check failed)" >&2
return 1
}
# restart_if_active <unit>
# Restarts a unit onto a freshly installed script, but only if it is running:
# the installer must not start a service the user stopped on purpose.
restart_if_active() {
local unit="$1"
if systemctl is-active "$unit" >/dev/null 2>&1; then
systemctl restart "$unit"
echo "✓ Restarted $unit onto the new script"
fi
return 0
}

View File

@ -2,6 +2,7 @@
# Browser wrapper install, desktop-entry redirect, auto-update units and # Browser wrapper install, desktop-entry redirect, auto-update units and
# service enablement for the periodic system setup. # service enablement for the periodic system setup.
# #
# Needs install_rendered_script from lib/atomic_install.sh (sourced by the caller).
# Sourced by setup_periodic_system.sh; split out to keep it under the # Sourced by setup_periodic_system.sh; split out to keep it under the
# 250-line cap. Sourced rather than run, so it inherits the caller's strict # 250-line cap. Sourced rather than run, so it inherits the caller's strict
# mode and the variables defined above the source line. # mode and the variables defined above the source line.
@ -13,9 +14,8 @@ install_browser_preexec_wrapper() {
echo "=========================================" echo "========================================="
local wrapper="/usr/local/bin/browser-preexec-wrapper" local wrapper="/usr/local/bin/browser-preexec-wrapper"
sed -e "s|__HOSTS_INSTALL_SCRIPT__|$HOSTS_INSTALL_SCRIPT|g" \ install_rendered_script "$TEMPLATE_BROWSER_WRAPPER" "$wrapper" \
"$TEMPLATE_BROWSER_WRAPPER" >"$wrapper" -e "s|__HOSTS_INSTALL_SCRIPT__|$HOSTS_INSTALL_SCRIPT|g"
chmod +x "$wrapper"
echo "✓ Installed wrapper: $wrapper" echo "✓ Installed wrapper: $wrapper"
# Allow passwordless execution of hosts installer for root-only actions # Allow passwordless execution of hosts installer for root-only actions
@ -88,9 +88,8 @@ install_auto_update() {
# Install script from template with user substitution # Install script from template with user substitution
local actual_user="${SUDO_USER:-$USER}" local actual_user="${SUDO_USER:-$USER}"
sed -e "s|__ACTUAL_USER__|$actual_user|g" \ install_rendered_script "$TEMPLATE_AUTO_UPDATE" "$update_script" \
"$TEMPLATE_AUTO_UPDATE" >"$update_script" -e "s|__ACTUAL_USER__|$actual_user|g"
chmod +x "$update_script"
echo "✓ Installed auto-update script: $update_script (user: $actual_user)" echo "✓ Installed auto-update script: $update_script (user: $actual_user)"
# Install systemd service and timer from templates # Install systemd service and timer from templates

View File

@ -0,0 +1,70 @@
#!/usr/bin/env bash
# Tests for lib/atomic_install.sh — installing a live /usr/local/bin script.
#
# A running bash reads its script lazily, so truncating the file in place
# (`sed ... > target`) lets a running instance execute a half-new file. The
# installer must render beside the target and rename over it: the target gets
# a NEW inode and a process holding the old one keeps reading the old bytes.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=services_harness.sh
. "${SCRIPT_DIR}/services_harness.sh"
# shellcheck source=../atomic_install.sh
. "${SCRIPT_DIR}/../atomic_install.sh"
BIN="${SYSROOT_DIR}/usr/local/bin"
TEMPLATE="${TEST_TMPDIR}/template.sh"
printf '#!/bin/bash\necho __NAME__ new\n' >"$TEMPLATE"
echo "== install_rendered_script: replaces the live file by rename =="
printf '#!/bin/bash\necho old\n' >"${BIN}/live.sh"
old_inode="$(stat -c %i "${BIN}/live.sh")"
exec 9<"${BIN}/live.sh" # a "running instance" holding the old inode open
install_rendered_script "$TEMPLATE" "${BIN}/live.sh" -e 's|__NAME__|hosts|g'
new_inode="$(stat -c %i "${BIN}/live.sh")"
if [[ $new_inode != "$old_inode" ]]; then
_t_pass "the target has a new inode"
else
_t_fail "the target kept inode $old_inode (written in place)"
fi
_t_eq $'#!/bin/bash\necho old' "$(cat <&9)" "the old inode's content is untouched for the running instance"
exec 9<&-
_t_eq $'#!/bin/bash\necho hosts new' "$(cat "${BIN}/live.sh")" "the target holds the rendered template"
if [[ -x "${BIN}/live.sh" ]]; then
_t_pass "the target is executable"
else
_t_fail "the target is not executable"
fi
_t_eq "live.sh" "$(ls "$BIN")" "no temp file is left beside the target"
echo "== install_rendered_script: a script that fails bash -n never replaces the target =="
printf '#!/bin/bash\nif then fi (\n' >"${TEST_TMPDIR}/broken.sh"
before="$(stat -c %i "${BIN}/live.sh")"
if install_rendered_script "${TEST_TMPDIR}/broken.sh" "${BIN}/live.sh" 2>/dev/null; then
_t_fail "a syntax error was installed"
else
_t_pass "a syntax error returns non-zero"
fi
_t_eq "$before" "$(stat -c %i "${BIN}/live.sh")" "the live file is untouched after a syntax error"
_t_eq "live.sh" "$(ls "$BIN")" "no temp file is left after a syntax error"
echo "== install_rendered_script: a missing template fails closed =="
if install_rendered_script "${TEST_TMPDIR}/nope.sh" "${BIN}/live.sh" 2>/dev/null; then
_t_fail "a missing template reported success"
else
_t_pass "a missing template returns non-zero"
fi
_t_eq "live.sh" "$(ls "$BIN")" "no temp file is left after a missing template"
echo "== restart_if_active: restarts only a running unit =="
reset_state
: >"${DEV}/active"
restart_if_active hosts-file-monitor.service >/dev/null
_t_not_called 'systemctl restart hosts-file-monitor.service' "a stopped unit is not started by the installer"
printf 'hosts-file-monitor.service\n' >"${DEV}/active"
restart_if_active hosts-file-monitor.service >/dev/null
_t_called 'systemctl restart hosts-file-monitor.service' "a running unit is restarted onto the new file"
_t_summary

View File

@ -122,13 +122,10 @@ create_execution_script() {
local exec_script="/usr/local/bin/periodic-system-maintenance.sh" local exec_script="/usr/local/bin/periodic-system-maintenance.sh"
# Install from template with path substitutions # Install from template with path substitutions
sed \ install_rendered_script "$TEMPLATE_MAINT_SCRIPT" "$exec_script" \
-e "s|__PACMAN_WRAPPER_INSTALL__|$PACMAN_WRAPPER_INSTALL|g" \ -e "s|__PACMAN_WRAPPER_INSTALL__|$PACMAN_WRAPPER_INSTALL|g" \
-e "s|__MAKEPKG_WRAPPER_INSTALL__|$MAKEPKG_WRAPPER_INSTALL|g" \ -e "s|__MAKEPKG_WRAPPER_INSTALL__|$MAKEPKG_WRAPPER_INSTALL|g" \
-e "s|__HOSTS_INSTALL_SCRIPT__|$HOSTS_INSTALL_SCRIPT|g" \ -e "s|__HOSTS_INSTALL_SCRIPT__|$HOSTS_INSTALL_SCRIPT|g"
"$TEMPLATE_MAINT_SCRIPT" >"$exec_script"
chmod +x "$exec_script"
echo "✓ Installed execution script from template: $exec_script" echo "✓ Installed execution script from template: $exec_script"
} }
@ -174,17 +171,22 @@ create_hosts_monitor_service() {
local monitor_script="/usr/local/bin/hosts-file-monitor.sh" local monitor_script="/usr/local/bin/hosts-file-monitor.sh"
local monitor_service="/etc/systemd/system/hosts-file-monitor.service" local monitor_service="/etc/systemd/system/hosts-file-monitor.service"
# Install the monitor script from template with substitution # Install the monitor script from template with substitution. The service
sed -e "s|__HOSTS_INSTALL_SCRIPT__|$HOSTS_INSTALL_SCRIPT|g" \ # runs it in a loop, so it is replaced by rename (see lib/atomic_install.sh).
"$TEMPLATE_HOSTS_MONITOR" >"$monitor_script" install_rendered_script "$TEMPLATE_HOSTS_MONITOR" "$monitor_script" \
chmod +x "$monitor_script" -e "s|__HOSTS_INSTALL_SCRIPT__|$HOSTS_INSTALL_SCRIPT|g"
echo "✓ Installed hosts monitor script from template: $monitor_script" echo "✓ Installed hosts monitor script from template: $monitor_script"
# Install the systemd service from template # Install the systemd service from template
install -m 0644 "$TEMPLATE_HOSTS_SVC" "$monitor_service" install -m 0644 "$TEMPLATE_HOSTS_SVC" "$monitor_service"
echo "✓ Installed hosts monitor service from template: $monitor_service" echo "✓ Installed hosts monitor service from template: $monitor_service"
# The running instance still holds the old inode; move it onto the new one.
restart_if_active hosts-file-monitor.service
} }
# shellcheck source=lib/atomic_install.sh
source "$SCRIPT_DIR/lib/atomic_install.sh"
# shellcheck source=lib/periodic_browser.sh # shellcheck source=lib/periodic_browser.sh
source "$SCRIPT_DIR/lib/periodic_browser.sh" source "$SCRIPT_DIR/lib/periodic_browser.sh"

View File

@ -0,0 +1,90 @@
#!/usr/bin/env bats
# Tests for fixes/fix_amd_x3d_vcache.sh against a fake driver sysfs tree and
# stubbed sudo/udevadm, so nothing touches /etc or the real X3D driver.
#
# Requires: bats (extra/bats). Run with:
# bats linux_configuration/tests/test_fix_amd_x3d_vcache.bats
bats_require_minimum_version 1.5.0
setup() {
REPO_DIR="$(cd -- "$(dirname -- "$BATS_TEST_FILENAME")/.." && pwd)"
SCRIPT="$REPO_DIR/fixes/fix_amd_x3d_vcache.sh"
export X3D_DRIVER_SYSFS="$BATS_TEST_TMPDIR/drivers/amd_x3d_vcache"
export X3D_RULES_DIR="$BATS_TEST_TMPDIR/rules.d"
mkdir -p "$X3D_DRIVER_SYSFS/AMDI0101:00" "$X3D_RULES_DIR" "$BATS_TEST_TMPDIR/bin"
echo frequency >"$X3D_DRIVER_SYSFS/AMDI0101:00/amd_x3d_mode"
export CALLS="$BATS_TEST_TMPDIR/calls"
# sudo runs its command; udevadm "trigger" stands in for the kernel bind
# event by flipping the fake attribute the way the installed rule would.
cat >"$BATS_TEST_TMPDIR/bin/sudo" <<'STUB'
#!/bin/bash
echo "sudo $*" >>"$CALLS"
exec "$@"
STUB
cat >"$BATS_TEST_TMPDIR/bin/udevadm" <<'STUB'
#!/bin/bash
echo "udevadm $*" >>"$CALLS"
if [[ "$1" == trigger && "${FAKE_RULE_WORKS:-1}" == 1 ]]; then
echo cache >"$X3D_DRIVER_SYSFS/AMDI0101:00/amd_x3d_mode"
fi
STUB
chmod +x "$BATS_TEST_TMPDIR/bin/sudo" "$BATS_TEST_TMPDIR/bin/udevadm"
export PATH="$BATS_TEST_TMPDIR/bin:$PATH"
}
@test "install puts the rule in place, triggers a bind event and ends on cache" {
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -f "$X3D_RULES_DIR/60-amd-x3d-vcache-mode.rules" ]
grep -q 'DRIVER=="amd_x3d_vcache"' "$X3D_RULES_DIR/60-amd-x3d-vcache-mode.rules"
grep -q 'ATTR{amd_x3d_mode}="cache"' "$X3D_RULES_DIR/60-amd-x3d-vcache-mode.rules"
grep -q '^udevadm control --reload' "$CALLS"
grep -q '^udevadm trigger --action=bind' "$CALLS"
[ "$(cat "$X3D_DRIVER_SYSFS/AMDI0101:00/amd_x3d_mode")" = cache ]
}
@test "the rule hard-codes no device name" {
run ! grep -q 'AMDI0101' "$REPO_DIR/fixes/60-amd-x3d-vcache-mode.rules"
}
@test "install fails loudly when the mode does not end on cache" {
FAKE_RULE_WORKS=0 run "$SCRIPT"
[ "$status" -eq 1 ]
[[ "$output" == *"frequency"* ]]
}
@test "install without the driver installs the rule and skips the trigger" {
rm -rf "$X3D_DRIVER_SYSFS"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -f "$X3D_RULES_DIR/60-amd-x3d-vcache-mode.rules" ]
run ! grep -q '^udevadm trigger' "$CALLS"
}
@test "install with the driver loaded but no X3D device installs the rule and succeeds" {
rm -rf "${X3D_DRIVER_SYSFS:?}/AMDI0101:00"
run "$SCRIPT"
[ "$status" -eq 0 ]
[[ "$output" == *"no X3D device"* ]]
[ -f "$X3D_RULES_DIR/60-amd-x3d-vcache-mode.rules" ]
}
@test "--remove deletes the rule and reloads udev" {
touch "$X3D_RULES_DIR/60-amd-x3d-vcache-mode.rules"
run "$SCRIPT" --remove
[ "$status" -eq 0 ]
[ ! -e "$X3D_RULES_DIR/60-amd-x3d-vcache-mode.rules" ]
grep -q '^udevadm control --reload' "$CALLS"
}
@test "--status reports rule and live mode" {
run "$SCRIPT" --status
[ "$status" -eq 0 ]
[[ "$output" == *"frequency"* ]]
}
@test "unknown option exits 2" {
run "$SCRIPT" --bogus
[ "$status" -eq 2 ]
}