Last active
October 16, 2022 16:50
-
-
Save fcicq/a277fa51386d7468efd94ca7a9dc0e31 to your computer and use it in GitHub Desktop.
openwrt: revive 4MB flash devices! updated for 22.03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir -p openwrt-rootfs/lib/preinit | |
[ ! -f openwrt-rootfs/lib/preinit/80_mount_root ] && cat <<'EOF' > openwrt-rootfs/lib/preinit/80_mount_root | |
# Copyright (C) 2006 OpenWrt.org | |
# Copyright (C) 2010 Vertical Communications | |
ram_mount_root() { | |
echo "- ram overlay -" | |
rdev=$(find_mtd_part rootfs_data) | |
if [ -z "$rdev" ]; then | |
echo "- bad rdev -" | |
return | |
fi | |
mount $rdev /overlay -o noatime -t jffs2 || { | |
echo "- overlay failed, please try jffs2reset -" | |
return | |
} | |
[ -f /overlay/sysupgrade.tgz ] && ln -s /overlay/sysupgrade.tgz /sysupgrade.tgz | |
mkdir -p /overlay/upper/etc | |
# note: custom init script in /etc/rc.local only | |
rm -rf /overlay/upper/etc/init.d 2>/dev/null | |
mkdir -p /overlay/work | |
mkdir -p /tmp/etc-init.d/upper | |
mkdir -p /tmp/etc-init.d/work | |
/bin/mount -o noatime,lowerdir=/rom/etc,upperdir=/overlay/upper/etc,workdir=/overlay/work -t overlay "overlayfs:/etc" /etc | |
/bin/mount -o noatime,lowerdir=/rom/etc/init.d,upperdir=/tmp/etc-init.d/upper,workdir=/tmp/etc-init.d/work -t overlay "overlayfs:/etc/init.d" /etc/init.d | |
} | |
missing_lines() { | |
local file1 file2 line | |
file1="$1" | |
file2="$2" | |
oIFS="$IFS" | |
IFS=":" | |
while read line; do | |
set -- $line | |
grep -q "^$1:" "$file2" || echo "$*" | |
done < "$file1" | |
IFS="$oIFS" | |
} | |
do_mount_root() { | |
mount_root ram | |
ram_mount_root || true | |
boot_run_hook preinit_mount_root | |
[ -f /sysupgrade.tgz -o -f /tmp/sysupgrade.tar ] && { | |
echo "- config restore -" | |
cp /etc/passwd /etc/group /etc/shadow /tmp | |
cd / | |
[ -f /sysupgrade.tgz ] && tar xzf /sysupgrade.tgz | |
[ -f /tmp/sysupgrade.tar ] && tar xf /tmp/sysupgrade.tar | |
missing_lines /tmp/passwd /etc/passwd >> /etc/passwd | |
missing_lines /tmp/group /etc/group >> /etc/group | |
missing_lines /tmp/shadow /etc/shadow >> /etc/shadow | |
rm /tmp/passwd /tmp/group /tmp/shadow | |
# Prevent configuration corruption on a power loss | |
sync | |
} | |
} | |
[ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_mount_root | |
EOF | |
mkdir -p openwrt-rootfs/etc/init.d | |
[ ! -f openwrt-rootfs/etc/init.d/done ] && cat <<'EOF' > openwrt-rootfs/etc/init.d/done | |
#!/bin/sh /etc/rc.common | |
# Copyright (C) 2006 OpenWrt.org | |
START=95 | |
boot() { | |
mount_root done | |
rm -f /sysupgrade.tgz | |
rm -f /overlay/sysupgrade.tgz | |
# process user commands | |
[ -f /etc/rc.local ] && { | |
sh /etc/rc.local | |
} | |
# set leds to normal state | |
. /etc/diag.sh | |
set_state done | |
} | |
EOF | |
chmod +x openwrt-rootfs/etc/init.d/done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example usage: