Skip to content

Instantly share code, notes, and snippets.

@caelor
Created May 1, 2015 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelor/e98dc3d30d1ea17e34f5 to your computer and use it in GitHub Desktop.
Save caelor/e98dc3d30d1ea17e34f5 to your computer and use it in GitHub Desktop.
OpenWRT mr3020 Switchable configs on boot
#!/bin/sh /etc/rc.common
# filename: /etc/init.d/modeswitch
START=15
boot() {
# process user commands
[ -f /root/switchable_config.sh ] && {
sh /root/switchable_config.sh
}
}
1) Create the files /root/switchable_config.sh and /etc/init.d/modeswitch
2) /etc/init.d/modeswitch enable to enable the config
#!/bin/sh
# filename: /root/switchable_config.sh
# Mode GPIO18 GPIO20
# sw1 sw2
# 3G 1 0
# WISP 0 1
# AP 1 1
dolog() {
echo "$1"
logger -t modeswitch "$1"
}
MODE="3g"
if grep -qe "sw2.*in hi" /sys/kernel/debug/gpio ; then
# sw2 is hi, so either WISP or AP mode
if grep -qe "sw1.*in hi" /sys/kernel/debug/gpio ; then
MODE="ap"
else
MODE="wisp"
fi
fi
dolog "Current switch mode: $MODE"
OLDMODE="unknown"
if [ -f /root/boot.switch_position ] ; then
OLDMODE=$(cat /root/boot.switch_position)
fi
if [ "$OLDMODE" == "$MODE" ] ; then
dolog "Mode is unchanged. Leaving configs unmodified."
else
dolog "Mode has changed $OLDMODE -> $MODE"
echo "$MODE" > /root/boot.switch_position
DESTFILE="/root/boot.backup_$OLDMODE.tgz"
dolog "Backing up old config to $DESTFILE"
sysupgrade -b $DESTFILE
ORIGINFILE="/root/boot.backup_$MODE.tgz"
if [ -f "$ORIGINFILE" ] ; then
dolog "Restoring previously saved config for $MODE ($ORIGINFILE)"
sysupgrade -r $ORIGINFILE
# needed to reapply potentially changed system level
/etc/init.d/system restart
else
dolog "No previously saved config for $MODE. Inheriting config from $OLDMODE"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment