bootstrap 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. # System-level state that dotsync cannot manage.
  3. #
  4. # dotsync deals in unprivileged $HOME symlinks; this covers the things that live
  5. # outside $HOME and need root: enabled units and /etc drop-ins. Kept as a plain
  6. # script rather than using the @host/@group markers, because those only apply to
  7. # files deployed into $HOME.
  8. #
  9. # Idempotent -- safe to re-run. Run after 'dotsync packages'.
  10. #
  11. # ./bootstrap
  12. set -euo pipefail
  13. hostname_id=$(uname -n)
  14. RED='\033[0;31m'
  15. GREEN='\033[0;32m'
  16. YELLOW='\033[1;33m'
  17. NC='\033[0m'
  18. log_info() { echo -e "${GREEN}[INFO]${NC} $*"; }
  19. log_warn() { echo -e "${YELLOW}[WARN]${NC} $*" >&2; }
  20. log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
  21. if [[ $EUID -eq 0 ]]; then
  22. log_error "Run as your normal user, not root -- user units need your session."
  23. exit 1
  24. fi
  25. # Write a file only if its content differs, so re-runs stay quiet and we avoid
  26. # needlessly restarting units.
  27. install_file() {
  28. local dest="$1" content="$2"
  29. if [[ -f "$dest" ]] && [[ "$(cat "$dest")" == "$content" ]]; then
  30. return 1
  31. fi
  32. sudo mkdir -p "$(dirname "$dest")"
  33. printf '%s\n' "$content" | sudo tee "$dest" >/dev/null
  34. log_info "Wrote $dest"
  35. return 0
  36. }
  37. enable_unit() {
  38. local unit="$1"
  39. if systemctl is-enabled --quiet "$unit" 2>/dev/null; then
  40. return 0
  41. fi
  42. log_info "Enabling $unit"
  43. sudo systemctl enable "$unit"
  44. }
  45. enable_user_unit() {
  46. local unit="$1"
  47. if systemctl --user is-enabled --quiet "$unit" 2>/dev/null; then
  48. return 0
  49. fi
  50. log_info "Enabling (user) $unit"
  51. systemctl --user enable "$unit"
  52. }
  53. log_info "Bootstrapping system state for $hostname_id"
  54. reload_needed=0
  55. # ---------------------------------------------------------------------------
  56. # logind: do not kill user processes at session end.
  57. #
  58. # The default (KillUserProcesses=yes) tears down lingering processes when the
  59. # last session for a user closes. That kills etterminal, which aborts the whole
  60. # etserver -- it broke Eternal Terminal on tompot. Declared explicitly on every
  61. # host so the two machines cannot drift on it again.
  62. #
  63. # A drop-in rather than an edit to /etc/systemd/logind.conf: package upgrades
  64. # replace that file, drop-ins survive.
  65. # ---------------------------------------------------------------------------
  66. if install_file /etc/systemd/logind.conf.d/10-kill-user-processes.conf \
  67. "[Login]
  68. KillUserProcesses=no"; then
  69. reload_needed=1
  70. fi
  71. # ---------------------------------------------------------------------------
  72. # iwd: shorten the stop timeout so shutdown isn't held up for 90s.
  73. # ---------------------------------------------------------------------------
  74. if install_file /etc/systemd/system/iwd.service.d/timeout.conf \
  75. "[Service]
  76. TimeoutStopSec=5"; then
  77. reload_needed=1
  78. fi
  79. if [[ $reload_needed -eq 1 ]]; then
  80. log_info "Reloading systemd"
  81. sudo systemctl daemon-reload
  82. fi
  83. # ---------------------------------------------------------------------------
  84. # System units
  85. # ---------------------------------------------------------------------------
  86. for unit in bluetooth.service greetd.service iwd.service \
  87. systemd-timesyncd.service tlp.service; do
  88. enable_unit "$unit"
  89. done
  90. # ---------------------------------------------------------------------------
  91. # User units
  92. # ---------------------------------------------------------------------------
  93. for unit in wluma.service; do
  94. enable_user_unit "$unit"
  95. done
  96. log_info "Bootstrap complete"
  97. log_warn "logind changes only take full effect after a reboot or full logout."