From: Michal Sojka Date: Mon, 25 Apr 2022 20:21:32 +0000 (+0200) Subject: server: Refactor delayed power-off logic X-Git-Tag: 20220425~1 X-Git-Url: https://rtime.felk.cvut.cz/gitweb/novaboot.git/commitdiff_plain/6fbfa776759891549832817602092d629f48acb7 server: Refactor delayed power-off logic From time to time, I'm facing problems with delayed power off not working. I'm not sure what is the exact reason, but a potential reason could be that the board was powered on but the systemd service novaboot-delayed-power-off.service, which is responsible for powering off, was not started. To prevent that, we change the logic so that powering on happens as a start job of the same service. --- diff --git a/server/novaboot-shell b/server/novaboot-shell index 6c72250..5e8621a 100755 --- a/server/novaboot-shell +++ b/server/novaboot-shell @@ -98,8 +98,10 @@ power() { esac if [ "$PPID" -ne 1 ] && systemctl --user is-enabled --quiet novaboot-delayed-power-off.service; then - sudo novaboot-power "$1" - if [ "$1" = "on" ]; then systemctl --user start novaboot-delayed-power-off.service; fi + case "$1" in + "on") systemctl --user start novaboot-delayed-power-off.service;; + "off") sudo novaboot-power off;; + esac else eval "$cmd" fi diff --git a/server/novaboot-shell.pod b/server/novaboot-shell.pod index 5684c6e..8442ef9 100644 --- a/server/novaboot-shell.pod +++ b/server/novaboot-shell.pod @@ -225,6 +225,41 @@ following into the spawned editor: [Timer] OnActiveSec=30min +=head2 Implementation/Debugging + +Implementation of delayed power off is a bit tricky because it uses +C in combination with several systemd services and timers. Here +is a summary of the call chains in various situations. This might be +useful for debugging. + +Powering on (via I or I subcommands): + + novaboot-shell -c on + systemctl --user start novaboot-delayed-power-off.service + sudo novaboot-power on + systemctl stop "novaboot-power-off@${SUDO_USER}.timer" + systemctl start "novaboot-power@${SUDO_USER}.service" + novaboot-shell -c on (with $PPID = 1) + eval "$on_cmd" + +Powering off via explicit I subcommand: + + novaboot-shell -c off + sudo novaboot-power off + systemctl stop "novaboot-power@${SUDO_USER}.service" + novaboot-shell -c off (with $PPID = 1) + eval $off_cmd + +Delayed off (executed automatically as a stop job of +novaboot-delayed-power-off.service at session end): + + sudo novaboot-power delayed-off + systemctl start "novaboot-power-off@${SUDO_USER}.timer" + ... + novaboot-power-off@${SUDO_USER}.service conflicts novaboot-power@${SUDO_USER}.service + novaboot-shell -c off (with $PPID = 1) + eval $off_cmd + =head1 AUTHORS Michal Sojka diff --git a/server/systemd/novaboot-delayed-power-off.service b/server/systemd/novaboot-delayed-power-off.service index cf750c3..c469c12 100644 --- a/server/systemd/novaboot-delayed-power-off.service +++ b/server/systemd/novaboot-delayed-power-off.service @@ -5,6 +5,7 @@ ConditionFileNotEmpty=%h/.novaboot-shell [Service] Type=oneshot +ExecStart=/usr/bin/sudo novaboot-power on ExecStop=/usr/bin/sudo novaboot-power delayed-off RemainAfterExit=true