78 lines
2.6 KiB
Bash
78 lines
2.6 KiB
Bash
#!/bin/bash
|
|
# Disposable-VM acceptance: kill a real sys package transaction during emerge,
|
|
# recover its stale mutation lock, undo the pre-update snapshot, and reboot.
|
|
set -uo pipefail
|
|
|
|
if [[ $(id -u) != 0 ]]; then
|
|
echo '===INTERRUPT-NEEDS-ROOT==='
|
|
exit 1
|
|
fi
|
|
|
|
SYS=/usr/local/bin/sys
|
|
WORK=/var/tmp/phoebus-interrupted-update
|
|
mkdir -p "$WORK"
|
|
if [[ ! -x "$SYS" ]] || compgen -G '/var/db/pkg/app-misc/hello-*' >/dev/null; then
|
|
echo '===INTERRUPT-PRECONDITION-FAILED==='
|
|
exit 1
|
|
fi
|
|
|
|
pkg_fingerprint() {
|
|
find /var/db/pkg -mindepth 2 -maxdepth 2 -type d 2>/dev/null |
|
|
sort | sha256sum | awk '{print $1}'
|
|
}
|
|
|
|
before=$(pkg_fingerprint)
|
|
marker="interrupted-update-$(date +%s)-$$"
|
|
printf '%s\n' "$marker" > /home/phoebus/interrupted-update-marker.txt
|
|
echo "===INTERRUPT-BEFORE pkgs=$before home=$marker==="
|
|
|
|
"$SYS" -y snapshot 'interrupted update acceptance' --legacy > "$WORK/snapshot.log" 2>&1
|
|
snapshot=$(sed -n 's/^protective snapshot: //p' "$WORK/snapshot.log" | tail -1)
|
|
if [[ -z $snapshot ]]; then
|
|
echo '===INTERRUPT-SNAPSHOT-FAILED==='
|
|
exit 1
|
|
fi
|
|
echo "===INTERRUPT-SNAPSHOT id=$snapshot==="
|
|
|
|
# `setsid` keeps the whole sys/emerge process tree in a private process group,
|
|
# so SIGKILL cannot hit the driver shell. Follow that group until the mutating
|
|
# `emerge --ask=n` process exists; CLI output may be buffered until it exits,
|
|
# and a small package can finish before a log-based poll sees it.
|
|
setsid "$SYS" -y install app-misc/hello --legacy --portage > "$WORK/install.log" 2>&1 &
|
|
leader=$!
|
|
started=0
|
|
for _ in $(seq 1 2400); do
|
|
if ! kill -0 "$leader" 2>/dev/null; then
|
|
break
|
|
fi
|
|
if ps -eo pgid=,args= | awk -v group="$leader" \
|
|
'$1 == group && /emerge/ && /--ask=n/ { found = 1 } END { exit !found }'; then
|
|
started=1
|
|
break
|
|
fi
|
|
sleep 0.05
|
|
done
|
|
if [[ $started != 1 ]] || ! kill -0 "$leader" 2>/dev/null; then
|
|
wait "$leader" 2>/dev/null || true
|
|
echo '===INTERRUPT-NO-ACTIVE-UPDATE==='
|
|
sed -n '1,25p' "$WORK/install.log"
|
|
exit 1
|
|
fi
|
|
kill -KILL -- -"$leader"
|
|
wait "$leader" 2>/dev/null || true
|
|
echo '===INTERRUPT-SIGKILL-DURING-EMERGE==='
|
|
|
|
# The killed sys owner must not leave a permanent lock. A deterministic read
|
|
# and undo must still work without a model or a network connection.
|
|
"$SYS" history --legacy > "$WORK/history.log" 2>&1
|
|
history_rc=$?
|
|
"$SYS" -y undo "$snapshot" --legacy > "$WORK/undo.log" 2>&1
|
|
undo_rc=$?
|
|
echo "===INTERRUPT-RECOVERY history_rc=$history_rc undo_rc=$undo_rc==="
|
|
if [[ $history_rc != 0 || $undo_rc != 0 ]]; then
|
|
sed -n '1,25p' "$WORK/undo.log"
|
|
exit 1
|
|
fi
|
|
sync
|
|
echo '===INTERRUPT-POWEROFF==='
|
|
poweroff -f
|