pkgcache host: lock-free 'mode', atomic apply/revert file writes, shared repo lock

- health 'mode' took the 30 s flock and mkdir'd the state dir; a held lock gave an empty
  answer, which sync (OFF) read as 'not off' and reverted from the newest backup. mode is
  now read-only and lock-free.
- apply's rewrite() truncated repo files in place and revert used cp -a over the file;
  both now write a temp file and rename(2), so a concurrent dnf never sees half a file.
- apply/revert take the same lock as the health timer so a health run cannot rewrite
  repo files while they edit them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-29 07:31:56 -08:00
1 parent b28e561387
commit ebe635501d
3 files changed
+37 -6

No files matched your search

+8
View File
@@ -33,6 +33,14 @@ MARK='#pkgcache-cachefirst '
MANAGED_RE='^baseurl=https://[^ /]+(:[0-9]+)?/(almalinux|epel|remi)/[^ ]+ https://[^ ]+$' MANAGED_RE='^baseurl=https://[^ /]+(:[0-9]+)?/(almalinux|epel|remi)/[^ ]+ https://[^ ]+$'
log() { logger -t pkgcache-host-health -p "daemon.$1" -- "${*:2}" 2>/dev/null || true; echo "$1: ${*:2}" >&2; } log() { logger -t pkgcache-host-health -p "daemon.$1" -- "${*:2}" 2>/dev/null || true; echo "$1: ${*:2}" >&2; }
# "mode" is read-only and is used by repos.sh sync/status: no lock (a 30 s wait that ends in an
# empty answer would make `sync` think the host is not OFF and revert it), no state dir needed.
if [ "${1:-mode}" = mode ]; then
if grep -q -s -F "$MARK" "$REPO_DIR"/*.repo 2>/dev/null; then echo origin-only
elif grep -q -s -E "$MANAGED_RE" "$REPO_DIR"/*.repo 2>/dev/null; then echo cache-first
else echo off; fi
exit 0
fi
mkdir -p "$STATE_DIR" mkdir -p "$STATE_DIR"
exec 9>"$STATE_DIR/lock"; flock -w 30 9 || { echo "lock busy" >&2; exit 0; } exec 9>"$STATE_DIR/lock"; flock -w 30 9 || { echo "lock busy" >&2; exit 0; }
SF="$STATE_DIR/state" SF="$STATE_DIR/state"
+25 -6
View File
@@ -79,6 +79,10 @@ BKROOT="${PKGCACHE_BACKUP_ROOT:-/root}"
update_ca() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || update-ca-trust; } update_ca() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || update-ca-trust; }
dnf_clean() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || dnf clean metadata -q || true; } dnf_clean() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || dnf clean metadata -q || true; }
die() { echo "FATAL(pkgcache-host-repos): $*" >&2; exit 1; } die() { echo "FATAL(pkgcache-host-repos): $*" >&2; exit 1; }
# Same lock as pkgcache-host-health.sh, so a health run can never rewrite repo files while
# apply/revert do (child processes that take the lock themselves must NOT be called inside).
with_lock() { mkdir -p "$LOCKDIR"; ( flock -w 60 8 || die "repo lock busy"; "$@" ) 8>"$LOCKDIR/lock"; }
LOCKDIR="${PKGCACHE_STATE_DIR:-/var/lib/pkgcache-host}"
same() { [ "$(sha256sum < "$1")" = "$(sha256sum < "$2")" ]; } # cmp/diffutils is not on minimal hosts same() { [ "$(sha256sum < "$1")" = "$(sha256sum < "$2")" ]; } # cmp/diffutils is not on minimal hosts
rewrite() { # $1 file $2 cache url rewrite() { # $1 file $2 cache url
@@ -122,7 +126,13 @@ for line in open(f).read().split('\n'):
break break
sec.append(line) sec.append(line)
flush() flush()
open(f, 'w').write('\n'.join(out)) import os, tempfile
st = os.stat(f)
fd, tmp = tempfile.mkstemp(dir=os.path.dirname(os.path.abspath(f)), prefix='.pkgcache-', suffix='.tmp')
with os.fdopen(fd, 'w') as fh:
fh.write('\n'.join(out)); fh.flush(); os.fsync(fh.fileno())
os.chmod(tmp, st.st_mode & 0o7777); os.chown(tmp, st.st_uid, st.st_gid)
os.rename(tmp, f) # atomic: a concurrent dnf sees the old or the new file, never half of one
PY PY
} }
@@ -152,10 +162,13 @@ case "$MODE" in
cp -a "$REPOS"/*.repo "$B/yum.repos.d/" cp -a "$REPOS"/*.repo "$B/yum.repos.d/"
[ -e "$ANCHOR" ] && cp -a "$ANCHOR" "$B/" || touch "$B/.no-anchor-before" [ -e "$ANCHOR" ] && cp -a "$ANCHOR" "$B/" || touch "$B/.no-anchor-before"
install -m 0644 "$CA_SRC" "$ANCHOR" && update_ca install -m 0644 "$CA_SRC" "$ANCHOR" && update_ca
for f in "$REPOS"/almalinux*.repo "$REPOS"/epel*.repo "$REPOS"/remi*.repo; do rewrite_all_repos() {
[ -f "$f" ] || continue for f in "$REPOS"/almalinux*.repo "$REPOS"/epel*.repo "$REPOS"/remi*.repo; do
grep -q -E '^#?\s*baseurl\s*=\s*https?://(repo\.almalinux\.org|download\.example|rpms\.remirepo\.net)/' "$f" && rewrite "$f" "$C" [ -f "$f" ] || continue
done if grep -q -E '^#?\s*baseurl\s*=\s*https?://(repo\.almalinux\.org|download\.example|rpms\.remirepo\.net)/' "$f"; then rewrite "$f" "$C"; fi
done
}
with_lock rewrite_all_repos
install_timer install_timer
echo "backup: $B" echo "backup: $B"
echo "changed files:"; for f in "$B"/yum.repos.d/*.repo; do same "$f" "$REPOS/$(basename "$f")" || echo " $REPOS/$(basename "$f")"; done echo "changed files:"; for f in "$B"/yum.repos.d/*.repo; do same "$f" "$REPOS/$(basename "$f")" || echo " $REPOS/$(basename "$f")"; done
@@ -164,7 +177,13 @@ case "$MODE" in
remove_timer; rm -f "${PKGCACHE_STATE_DIR:-/var/lib/pkgcache-host}/state" remove_timer; rm -f "${PKGCACHE_STATE_DIR:-/var/lib/pkgcache-host}/state"
B="${2:-$(ls -d "$BKROOT"/pkgcache-repo-backup-* 2>/dev/null | sort | tail -1)}" B="${2:-$(ls -d "$BKROOT"/pkgcache-repo-backup-* 2>/dev/null | sort | tail -1)}"
[ -d "$B/yum.repos.d" ] || die "no backup dir ($B)" [ -d "$B/yum.repos.d" ] || die "no backup dir ($B)"
for f in "$B"/yum.repos.d/*.repo; do same "$f" "$REPOS/$(basename "$f")" || { cp -a "$f" "$REPOS/"; echo "restored $REPOS/$(basename "$f")"; }; done restore_repos() {
for f in "$B"/yum.repos.d/*.repo; do
d="$REPOS/$(basename "$f")"
same "$f" "$d" || { cp -a "$f" "$d.pkgcache-restore" && mv -f "$d.pkgcache-restore" "$d"; echo "restored $d"; } # atomic for a concurrent dnf
done
}
with_lock restore_repos
if [ -e "$B/.no-anchor-before" ]; then rm -f "$ANCHOR"; update_ca; echo "removed $ANCHOR"; fi if [ -e "$B/.no-anchor-before" ]; then rm -f "$ANCHOR"; update_ca; echo "removed $ANCHOR"; fi
dnf_clean dnf_clean
echo "reverted from $B" echo "reverted from $B"
+4
View File
@@ -82,6 +82,10 @@ FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null; FAKE_CACHE=refused FA
[ "$("$HL" mode)" = cache-first ] && ok "cache+origin both down: no fallback" || bad "network outage caused a fallback" [ "$("$HL" mode)" = cache-first ] && ok "cache+origin both down: no fallback" || bad "network outage caused a fallback"
FAKE_CACHE=refused "$HL" run 2>/dev/null; FAKE_CACHE=refused "$HL" run 2>/dev/null; [ "$("$HL" mode)" = origin-only ] && ok "connection refused counts as failure" || bad "refused not counted" FAKE_CACHE=refused "$HL" run 2>/dev/null; FAKE_CACHE=refused "$HL" run 2>/dev/null; [ "$("$HL" mode)" = origin-only ] && ok "connection refused counts as failure" || bad "refused not counted"
# mode must not wait for the health lock (a held lock once turned `mode` into an empty answer)
( flock 9; sleep 4 ) 9>"$T/state/lock" & sleep 0.5
m=$(timeout 3 "$HL" mode || true); [ "$m" = origin-only ] && ok "mode answers while the health lock is held" || bad "mode blocked on the lock (got '$m')"
wait
# ---- OFF via setting reverts cleanly, even from origin-only ---- # ---- OFF via setting reverts cleanly, even from origin-only ----
echo "PKGCACHE_ENABLED=0" >"$T/pkgcache-host.conf"; printf 'PKGCACHE_CA=%s\n' "$T/ca.crt" >>"$T/pkgcache-host.conf" echo "PKGCACHE_ENABLED=0" >"$T/pkgcache-host.conf"; printf 'PKGCACHE_CA=%s\n' "$T/ca.crt" >>"$T/pkgcache-host.conf"
"$R" sync >/dev/null "$R" sync >/dev/null