[openwrt/openwrt] base-files: board_detect: make resilient against power-cuts

LEDE Commits lede-commits at lists.infradead.org
Tue Nov 25 12:53:45 PST 2025


hauke pushed a commit to openwrt/openwrt.git, branch main:
https://git.openwrt.org/26ba9b609b95886dd7028fbd343ad079a1ef4445

commit 26ba9b609b95886dd7028fbd343ad079a1ef4445
Author: Andreas Gnau <andreas.gnau at iopsys.eu>
AuthorDate: Wed Oct 8 23:00:44 2025 +0200

    base-files: board_detect: make resilient against power-cuts
    
    If board_detect is interrupted by cutting power on first boot,
    board.json might only be half-way written and the file will not be
    written again correctly on subsequent boots.
    
    Write to a temporary file first, then rename. Since a rename on the same
    file system is an atomic operation, it ensures that either
    /etc/board.json does not exist or that the complete file exists.
    
    Signed-off-by: Andreas Gnau <andreas.gnau at iopsys.eu>
    Link: https://github.com/openwrt/openwrt/pull/20831
    Signed-off-by: Hauke Mehrtens <hauke at hauke-m.de>
---
 package/base-files/files/bin/board_detect | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/package/base-files/files/bin/board_detect b/package/base-files/files/bin/board_detect
index 4c50a6057d..c4910cb47c 100755
--- a/package/base-files/files/bin/board_detect
+++ b/package/base-files/files/bin/board_detect
@@ -1,14 +1,22 @@
 #!/bin/sh
 
-CFG=$1
+REAL_CFG=$1
 
-[ -n "$CFG" ] || CFG=/etc/board.json
+[ -n "$REAL_CFG" ] || REAL_CFG=/etc/board.json
 
-if [ -d "/etc/board.d/" ] && [ ! -s "$CFG" ]; then
+if [ -d "/etc/board.d/" ] && [ ! -s "$REAL_CFG" ]; then
+	# Use temp file to prevent incomplete file on power-cut, CFG is used by the sourced script to read/write the file
+	CFG="$(dirname "$REAL_CFG")/.$(basename "$REAL_CFG").tmp"
+	rm -f "$CFG" || exit
 	for a in $(ls /etc/board.d/*); do
 		[ -s "$a" ] || continue
 		(. "$a")
 	done
 fi
 
-[ -s "$CFG" ] || return 1
+if [ -s "$CFG" ]; then
+	mv "$CFG" "$REAL_CFG" || exit
+else
+	rm -f "$CFG"
+	exit 1
+fi




More information about the lede-commits mailing list