[OpenWrt-Devel] [RFC PATCH 1/2] softoff: add softoff utility

Christian Lamparter chunkeey at gmail.com
Fri Aug 23 17:06:30 EDT 2019


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 at gmail.com>
---
 package/utils/softoff/Makefile      | 36 +++++++++++++++++++++++++++++
 package/utils/softoff/files/softoff | 36 +++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 package/utils/softoff/Makefile
 create mode 100644 package/utils/softoff/files/softoff

diff --git a/package/utils/softoff/Makefile b/package/utils/softoff/Makefile
new file mode 100644
index 0000000000..28930e9bde
--- /dev/null
+++ b/package/utils/softoff/Makefile
@@ -0,0 +1,36 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=softoff
+PKG_RELEASE:=1
+PKG_LICENSE:=GPL-2.0
+PKG_MAINTAINER:=Christian Lamparter <chunkeey at 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
index 0000000000..e25bdba8f2
--- /dev/null
+++ b/package/utils/softoff/files/softoff
@@ -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
-- 
2.23.0


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list