Skip to content

Commit 6cb0ddc

Browse files
mingzhangqunigorpecovnik
authored andcommitted
armbian-firstrun: use atomic write for armbianEnv.txt MAC randomization
Replace non-atomic sed -i with atomic write pattern (sed to temp file -> chmod -> sync -> mv). Chain all steps with && so any failure triggers cleanup of the temp file instead of silently keeping old MAC values. Generate both MAC addresses before any I/O to avoid partial state.
1 parent fbfc3ea commit 6cb0ddc

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

packages/bsp/common/usr/lib/armbian/armbian-firstrun

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ case "$1" in
6060
# get rid of purple background color in newt apps whiptail, partimage, debconf ... Reverting to Debian look.
6161
[[ -f /etc/newt/palette ]] && sed -e 's/magenta/blue/g' -i /etc/newt/palette
6262

63-
# randomize mac in armbianEnv.txt
63+
# randomize mac in armbianEnv.txt (atomic write to survive power loss)
6464
if [[ -f /boot/armbianEnv.txt ]]; then
65-
generate_random_mac
66-
sed -i "s/^ethaddr=.*/ethaddr=$MACADDR/" /boot/armbianEnv.txt
67-
generate_random_mac
68-
sed -i "s/^eth1addr=.*/eth1addr=$MACADDR/" /boot/armbianEnv.txt
65+
generate_random_mac; mac0="$MACADDR"
66+
generate_random_mac; mac1="$MACADDR"
67+
if sed -e "s/^ethaddr=.*/ethaddr=${mac0}/" \
68+
-e "s/^eth1addr=.*/eth1addr=${mac1}/" \
69+
/boot/armbianEnv.txt > /boot/armbianEnv.txt.tmp \
70+
&& chmod --reference=/boot/armbianEnv.txt /boot/armbianEnv.txt.tmp \
71+
&& sync /boot/armbianEnv.txt.tmp \
72+
&& mv /boot/armbianEnv.txt.tmp /boot/armbianEnv.txt; then
73+
:
74+
else
75+
rm -f /boot/armbianEnv.txt.tmp
76+
fi
6977
fi
7078

7179
# hardware workarounds per family

0 commit comments

Comments
 (0)