|
#!/usr/bin/env bash |
|
set -euo pipefail |
|
|
|
require_root() { |
|
if [[ $EUID -ne 0 ]]; then |
|
printf "%s\n" "Please run as root." |
|
exit 1 |
|
fi |
|
} |
|
|
|
mask_sleep_targets() { |
|
printf "%s\n" "Masking sleep and hibernate targets." |
|
systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target |
|
} |
|
|
|
configure_logind() { |
|
printf "%s\n" "Configuring logind to ignore lid and sleep keys." |
|
mkdir -p /etc/systemd/logind.conf.d |
|
cat > /etc/systemd/logind.conf.d/disable-suspend.conf << 'EOF' |
|
[Login] |
|
HandleLidSwitch=ignore |
|
HandleLidSwitchDocked=ignore |
|
HandleSuspendKey=ignore |
|
HandleHibernateKey=ignore |
|
EOF |
|
systemctl restart systemd-logind |
|
} |
|
|
|
install_polkit_rule() { |
|
printf "%s\n" "Installing polkit rule to remove suspend and hibernate options." |
|
mkdir -p /etc/polkit-1/rules.d |
|
cat > /etc/polkit-1/rules.d/disable-suspend.rules << 'EOF' |
|
polkit.addRule(function(action, subject) { |
|
if (action.id == "org.freedesktop.login1.suspend" || |
|
action.id == "org.freedesktop.login1.suspend-multiple-sessions" || |
|
action.id == "org.freedesktop.login1.hibernate" || |
|
action.id == "org.freedesktop.login1.hibernate-multiple-sessions") { |
|
return polkit.Result.NO; |
|
} |
|
}); |
|
EOF |
|
systemctl restart polkit 2>/dev/null || systemctl restart polkit.service 2>/dev/null || true |
|
} |
|
|
|
disable_graphical_boot() { |
|
printf "%s\n" "Setting default boot target to multi-user." |
|
systemctl set-default multi-user.target |
|
|
|
# Stop and disable common display managers if present |
|
local dms=(gdm gdm3 lightdm sddm lxdm slim) |
|
for dm in "${dms[@]}"; do |
|
if systemctl list-unit-files | grep -q "^${dm}\.service"; then |
|
printf "%s\n" "Disabling ${dm}.service." |
|
systemctl disable --now "${dm}.service" || true |
|
fi |
|
done |
|
} |
|
|
|
verify() { |
|
printf "%s\n" "Verification." |
|
systemctl is-enabled sleep.target && printf "%s\n" "sleep.target enabled." || printf "%s\n" "sleep.target is masked." |
|
systemctl is-enabled suspend.target && printf "%s\n" "suspend.target enabled." || printf "%s\n" "suspend.target is masked." |
|
systemctl is-enabled hibernate.target && printf "%s\n" "hibernate.target enabled." || printf "%s\n" "hibernate.target is masked." |
|
systemctl is-enabled hybrid-sleep.target && printf "%s\n" "hybrid-sleep.target enabled." || printf "%s\n" "hybrid-sleep.target is masked." |
|
local current_target |
|
current_target=$(systemctl get-default) |
|
printf "%s\n" "Current default target: ${current_target}." |
|
} |
|
|
|
main() { |
|
require_root |
|
mask_sleep_targets |
|
configure_logind |
|
install_polkit_rule |
|
disable_graphical_boot |
|
verify |
|
printf "%s\n" "Done. Reboot to apply graphical target change." |
|
} |
|
|
|
main "$@" |