scripts/hosts/guard/install_pacman_hooks.sh

50 lines
1.3 KiB
Bash
Raw Permalink Normal View History

2025-10-13 10:21:35 +02:00
#!/usr/bin/env bash
set -euo pipefail
2025-11-01 15:36:22 +01:00
require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi; }
2025-10-13 10:21:35 +02:00
require_root "$@"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOOKS_DIR="/etc/pacman.d/hooks"
install -d -m 755 "$HOOKS_DIR"
# Pre-transaction hook
2025-11-01 15:36:22 +01:00
cat > "$HOOKS_DIR/10-unlock-etc-hosts.hook" << 'HOOK'
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Temporarily unlocking /etc/hosts for transaction
When = PreTransaction
Exec = /bin/bash /usr/local/share/hosts-guard/pacman-pre-unlock-hosts.sh
NeedsTargets
HOOK
2025-10-13 10:21:35 +02:00
# Post-transaction hook
2025-11-01 15:36:22 +01:00
cat > "$HOOKS_DIR/90-relock-etc-hosts.hook" << 'HOOK'
2025-10-13 10:21:35 +02:00
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Re-locking /etc/hosts after transaction
When = PostTransaction
Exec = /bin/bash /usr/local/share/hosts-guard/pacman-post-relock-hosts.sh
NeedsTargets
HOOK
# Place helper scripts into a shared location
install -d -m 755 /usr/local/share/hosts-guard
install -m 755 "$SCRIPT_DIR/pacman-hooks/pacman-pre-unlock-hosts.sh" /usr/local/share/hosts-guard/
2025-10-13 10:21:35 +02:00
install -m 755 "$SCRIPT_DIR/pacman-hooks/pacman-post-relock-hosts.sh" /usr/local/share/hosts-guard/
echo "Pacman hooks installed into $HOOKS_DIR."