softoff: add softoff utility
authorChristian Lamparter <chunkeey@gmail.com>
Mon, 19 Aug 2019 15:45:42 +0000 (17:45 +0200)
committerChristian Lamparter <chunkeey@gmail.com>
Sat, 19 Oct 2019 16:50:44 +0000 (18:50 +0200)
The softoff package replaces the poweroff command with a
scripts that utilize the existing sysupgrade code to perform
a "soft" poweroff (in a way that the device is no longer
responsive). So this makes it safer to disconnect and
be moved.

By design, sysupgrade already stops services and closes
any running application, as well as unmounts any active
partition.
This scripts piggybacks on what's provided by sysupgrade,
but also puts all active SATA/SCSI/UAS/USB-attached HDDs
into standby by telling the kernel they are no longer
needed (and the kernel will do the rest) and tries to
switch all available LEDs to the off state (the trigger
"none" will only switch off managed LEDs) before causes
the device to enter a infinite loop.

This works especially well with embedded devices without a
real poweroff method (many SoC platform drivers just map
the shutdown() to reset()) or suffer because the platform
has a no-way-out watchdog that can trigger a hardware
reboot which can bring the device out of its "poweroff"
state.

Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
package/utils/softoff/Makefile [new file with mode: 0644]
package/utils/softoff/files/softoff [new file with mode: 0644]

diff --git a/package/utils/softoff/Makefile b/package/utils/softoff/Makefile
new file mode 100644 (file)
index 0000000..28930e9
--- /dev/null
@@ -0,0 +1,36 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=softoff
+PKG_RELEASE:=1
+PKG_LICENSE:=GPL-2.0
+PKG_MAINTAINER:=Christian Lamparter <chunkeey@gmail.com>
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/softoff
+       PKGARCH:=all
+       SECTION:=utils
+       CATEGORY:=Utilities
+       TITLE:=softoff utility
+endef
+
+define Package/softoff/description
+  the softoff package replaces the poweroff command with scripts that
+  utilize the sysupgrade code to perform a "softer" poweroff.
+  The script will try to close any running application, unmount any active
+  partition as well as putting any connected HDD to standby (for shutdown).
+  This allows embedded devices with out a working poweroff method to become
+  unavailable.
+endef
+
+define Build/Compile
+       true
+endef
+
+define Package/softoff/install-overlay
+       $(INSTALL_DIR)  $(1)/sbin
+       $(INSTALL_BIN)  ./files/softoff $(1)/sbin/softoff
+       $(LN) softoff $(1)/sbin/poweroff
+endef
+
+$(eval $(call BuildPackage,softoff))
diff --git a/package/utils/softoff/files/softoff b/package/utils/softoff/files/softoff
new file mode 100644 (file)
index 0000000..e25bdba
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+. /lib/functions.sh
+. /lib/functions/system.sh
+. /lib/upgrade/common.sh
+
+case "$1" in
+stage2)
+       for dev in /sys/block/sd*[a-z]; do
+               [ -d "$dev" ] && echo "1" > "$dev/device/delete"
+       done
+
+       for led in /sys/class/leds/*; do
+               [ -d "$led" ] && {
+                       echo "none" > "$led/trigger"
+                       echo "0" > "$led/brightness"
+               }
+       done
+
+       while :; do
+               sleep 10000;
+       done
+       ;;
+*)
+       install_bin /sbin/upgraded
+       install_bin "$0"
+
+       ifdown -a
+
+       ubus call system sysupgrade "{
+               \"prefix\": $(json_string "$RAM_ROOT"),
+               \"path\": $(json_string "badfile"),
+               \"command\": $(json_string "$0 stage2")
+       }"
+       ;;
+esac