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

+25 -6
View File
@@ -79,6 +79,10 @@ BKROOT="${PKGCACHE_BACKUP_ROOT:-/root}"
update_ca() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || update-ca-trust; }
dnf_clean() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || dnf clean metadata -q || true; }
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
rewrite() { # $1 file $2 cache url
@@ -122,7 +126,13 @@ for line in open(f).read().split('\n'):
break
sec.append(line)
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
}
@@ -152,10 +162,13 @@ case "$MODE" in
cp -a "$REPOS"/*.repo "$B/yum.repos.d/"
[ -e "$ANCHOR" ] && cp -a "$ANCHOR" "$B/" || touch "$B/.no-anchor-before"
install -m 0644 "$CA_SRC" "$ANCHOR" && update_ca
for f in "$REPOS"/almalinux*.repo "$REPOS"/epel*.repo "$REPOS"/remi*.repo; do
[ -f "$f" ] || continue
grep -q -E '^#?\s*baseurl\s*=\s*https?://(repo\.almalinux\.org|download\.example|rpms\.remirepo\.net)/' "$f" && rewrite "$f" "$C"
done
rewrite_all_repos() {
for f in "$REPOS"/almalinux*.repo "$REPOS"/epel*.repo "$REPOS"/remi*.repo; do
[ -f "$f" ] || continue
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
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
@@ -164,7 +177,13 @@ case "$MODE" in
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)}"
[ -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
dnf_clean
echo "reverted from $B"