Opkg extras

  • This instruction extends Opkg functionality.
  • Follow Hotplug extras to extend Hotplug functionality.
  • Follow UCI extras to extend UCI functionality.
  • Follow the automated section for quick setup.
  • Save, restore and roll back Opkg profiles.
  • Support automatic and custom profiles.
  • Identify user-removed/installed packages.
  • Restore automatically after firmware upgrade.
  • Upgrade all upgradable packages.
  • Find new configurations.
  • Wrap Opkg calls to provide a seamless invocation method.
  • Rely on UCI and backup defaults to store and manage profiles.
  • Identify package types with grep and find, and overlay with mount.
  • Support importing user-installed packages from Sysupgrade.
  • Use Hotplug to detect WAN connectivity and trigger profile restore.
  • Utilize lockfiles with lock to avoid race conditions and loops.
  • Perform reboot to apply changes after automatic profile restore.
  • Write and read non-interactive logs with Syslog for troubleshooting.
  • Fetch the default profile to restore and roll back from UCI, unless specified manually.
  • Take precedence for remove over install to avoid package conflicts while restoring profile.
  • Limit the operation scope to save time: remove installed, install missing, upgrade upgradable.
  • Process packages one by one to minimize resource consumption and avoid transaction failure.
Opkg sub-command Description
help Show Opkg extras
ls [<pkg_type>] List packages by type:
help - show package types,
ai or none - all installed,
ro - ROM-installed,
ov - overlay-installed,
ur - user-removed,
ui - user-installed,
au - all upgradable
upgr [<pkg_type>] Upgrade packages by type:
help - show package types,
au or none - all upgradable,
ov - overlay-installed
proc <cmd> <pkgs> Process packages one by one with the given sub-command
reinstall <pkgs> Reinstall specific packages
newconf Find new configurations
prof <cmd> [<prof>] Get Opkg profile for the given sub-command
uci [<prof>] < <prof_uci> Initialize Opkg profile from stdin or file
import [<prof_bak>] Import Opkg profile from Sysupgrade backup to UCI
save [<prof>] Save the current Opkg profile to UCI
restore [<prof>] Restore Opkg profile:
custom - custom profile configured manually,
auto or none - automatic profile
rollback [<prof>] Roll back Opkg profile:
remove user-installed packages,
install user-removed packages,
skip upgraded packages
# Configure profile
mkdir -p /etc/profile.d
cat << "EOF" > /etc/profile.d/opkg.sh
opkg() {
local OPKG_CMD="${1}"
if ! type opkg_"${OPKG_CMD}" &> /dev/null
then command opkg "${@}"
else shift
opkg_"${OPKG_CMD}" "${@}"
fi
}
 
opkg_help() {
cat << EOI
opkg [help|ls|upgr|proc|reinstall|newconf|prof|uci|import|save|restore|rollback]
EOI
}
 
opkg_ls() {
local OPKG_ARG="${1:-ai}"
if ! type opkg_"${OPKG_CMD}_${OPKG_ARG}" &> /dev/null
then opkg_"${OPKG_CMD}_help"
else shift
opkg_"${OPKG_CMD}_${OPKG_ARG}" "${@}"
fi
}
 
opkg_ls_help() {
cat << EOI
opkg ${OPKG_CMD} [help|ai|ro|ov|ur|ui|au]
EOI
}
 
opkg_ls_ai() {
opkg list-installed "${@}"
}
 
opkg_ls_ro() {
local OPKG_CONF="/etc/opkg.conf"
local OPKG_ROCONF="$(mktemp -t opkg.XXXXXX)"
sed -e "/^dest\sroot\s/s/$/rom/" "${OPKG_CONF}" > "${OPKG_ROCONF}"
opkg ls ai "${@}" -f "${OPKG_ROCONF}"
rm -f "${OPKG_ROCONF}"
}
 
opkg_ls_ov() {
local OPKG_OVINFO="/overlay/upper/usr/lib/opkg/info"
find "${OPKG_OVINFO}" -name "*.control" -type "f" 2> /dev/null \
| sed -r -e "s/^.*\/(.*)\.control$/\1/"
}
 
opkg_ls_ur() {
local OPKG_AI="$(mktemp -t opkg.XXXXXX)"
local OPKG_RO="$(mktemp -t opkg.XXXXXX)"
opkg ls ai | sed -e "s/\s.*$//" > "${OPKG_AI}"
opkg ls ro | sed -e "s/\s.*$//" > "${OPKG_RO}"
grep -v -x -f "${OPKG_AI}" "${OPKG_RO}"
rm -f "${OPKG_AI}" "${OPKG_RO}"
}
 
opkg_ls_ui() {
local OPKG_AI="$(mktemp -t opkg.XXXXXX)"
local OPKG_RO="$(mktemp -t opkg.XXXXXX)"
opkg ls ai | sed -e "s/\s.*$//" > "${OPKG_AI}"
opkg ls ro | sed -e "s/\s.*$//" > "${OPKG_RO}"
grep -v -x -f "${OPKG_RO}" "${OPKG_AI}"
rm -f "${OPKG_AI}" "${OPKG_RO}"
}
 
opkg_ls_au() {
opkg list-upgradable "${@}"
}
 
opkg_upgr() {
local OPKG_ARG="${1:-au}"
if ! type opkg_"${OPKG_CMD}_${OPKG_ARG}" &> /dev/null
then opkg_"${OPKG_CMD}_help"
else shift
opkg_"${OPKG_CMD}_${OPKG_ARG}" "${@}"
fi
}
 
opkg_upgr_help() {
cat << EOI
opkg ${OPKG_CMD} [help|au|ov]
EOI
}
 
opkg_upgr_au() {
local OPKG_AI="$(mktemp -t opkg.XXXXXX)"
local OPKG_AU="$(mktemp -t opkg.XXXXXX)"
opkg ls ai | sed -e "s/\s.*$//" > "${OPKG_AI}"
opkg ls au | sed -e "s/\s.*$//" > "${OPKG_AU}"
opkg proc upgrade $(grep -x -f "${OPKG_AI}" "${OPKG_AU}")
rm -f "${OPKG_AI}" "${OPKG_AU}"
}
 
opkg_upgr_ov() {
local OPKG_OV="$(mktemp -t opkg.XXXXXX)"
local OPKG_AU="$(mktemp -t opkg.XXXXXX)"
opkg ls ov > "${OPKG_OV}"
opkg ls au | sed -e "s/\s.*$//" > "${OPKG_AU}"
opkg proc upgrade $(grep -x -f "${OPKG_OV}" "${OPKG_AU}")
rm -f "${OPKG_OV}" "${OPKG_AU}"
}
 
opkg_proc() {
local OPKG_CMD="${1:?}"
local OPKG_PKG
shift
for OPKG_PKG in "${@}"
do
if [ "${OPKG_PKG%%-*}" = "dnsmasq" -a "${OPKG_CMD}" = "remove" ]
then /etc/init.d/dnsmasq stop
fi
opkg "${OPKG_CMD}" "${OPKG_PKG}" --force-depends
if [ "${OPKG_PKG%%-*}" = "grep" ]
then hash -r
fi
done
}
 
opkg_reinstall() {
opkg install "${@}" --force-reinstall
}
 
opkg_newconf() {
local OPKG_PATH="/etc"
find "${OPKG_PATH}" -name "*-opkg"
}
 
opkg_prof() {
local OPKG_PROF="$(uci -q get opkg.defaults."${1}")"
local OPKG_PROF="${2:-${OPKG_PROF}}"
local OPKG_PROF="${OPKG_PROF:-auto}"
cat << EOI
${OPKG_PROF}
EOI
}
 
opkg_uci() {
local OPKG_PROF="$(opkg prof "${OPKG_CMD}" "${1}")"
if ! uci -q show opkg > /dev/null
then uci import opkg < /dev/null
fi
uci -q batch << EOI
delete opkg.${OPKG_PROF}
set opkg.defaults=opkg
set opkg.custom=opkg
set opkg.${OPKG_PROF}=opkg
$(sed -r -e "s/^(.*)\s(.*)$/add_list opkg.${OPKG_PROF}.\2=\1/")
commit opkg
EOI
}
 
opkg_import() {
local OPKG_BAK="${1:-/etc/backup/installed_packages.txt}"
if grep -q -e "^overlayfs:" /etc/mtab
then local OPKG_DEST="overlay"
else local OPKG_DEST="unknown"
fi
if [ -e "${OPKG_BAK}" ]
then sed -n -e "s/\s${OPKG_DEST}$/\tipkg/p" "${OPKG_BAK}" | opkg uci
fi
}
 
opkg_save() {
local OPKG_PROF="$(opkg prof "${OPKG_CMD}" "${1}")"
{
opkg ls ur | sed -e "s/$/\trpkg/"
opkg ls ui | sed -e "s/$/\tipkg/"
} | opkg uci "${OPKG_PROF}"
}
 
opkg_restore() {
local OPKG_PROF="$(opkg prof "${OPKG_CMD}" "${1}")"
local OPKG_AI="$(mktemp -t opkg.XXXXXX)"
local OPKG_PR="$(mktemp -t opkg.XXXXXX)"
local OPKG_PI="$(mktemp -t opkg.XXXXXX)"
opkg ls ai | sed -e "s/\s.*$//" > "${OPKG_AI}"
uci -q get opkg."${OPKG_PROF}".rpkg | sed -e "s/\s/\n/g" > "${OPKG_PR}"
uci -q get opkg."${OPKG_PROF}".ipkg | sed -e "s/\s/\n/g" > "${OPKG_PI}"
opkg proc remove $(grep -x -f "${OPKG_AI}" "${OPKG_PR}")
opkg proc install $(grep -v -x -f "${OPKG_AI}" "${OPKG_PI}")
rm -f "${OPKG_AI}" "${OPKG_PR}" "${OPKG_PI}"
}
 
opkg_rollback() {
local OPKG_PROF="$(opkg prof "${OPKG_CMD}" "${1}")"
local OPKG_UR="$(mktemp -t opkg.XXXXXX)"
local OPKG_UI="$(mktemp -t opkg.XXXXXX)"
local OPKG_PR="$(mktemp -t opkg.XXXXXX)"
local OPKG_PI="$(mktemp -t opkg.XXXXXX)"
opkg restore "${OPKG_PROF}"
opkg ls ur > "${OPKG_UR}"
opkg ls ui > "${OPKG_UI}"
uci -q get opkg."${OPKG_PROF}".rpkg | sed -e "s/\s/\n/g" > "${OPKG_PR}"
uci -q get opkg."${OPKG_PROF}".ipkg | sed -e "s/\s/\n/g" > "${OPKG_PI}"
opkg proc remove $(grep -v -x -f "${OPKG_PI}" "${OPKG_UI}")
opkg proc install $(grep -v -x -f "${OPKG_PR}" "${OPKG_UR}")
rm -f "${OPKG_UR}" "${OPKG_UI}" "${OPKG_PR}" "${OPKG_PI}"
}
EOF
. /etc/profile.d/opkg.sh
 
# Restore packages automatically
mkdir -p /etc/hotplug.d/online
cat << "EOF" > /etc/hotplug.d/online/50-opkg-restore
if [ ! -e /etc/opkg-restore ] \
&& lock -n /var/lock/opkg-restore \
&& opkg update
then
. /etc/profile.d/opkg.sh
opkg restore
touch /etc/opkg-restore
lock -u /var/lock/opkg-restore
reboot
fi
EOF
cat << "EOF" >> /etc/sysupgrade.conf
/etc/hotplug.d/online/50-opkg-restore
EOF
# Save Opkg profile
opkg save
 
# Restore Opkg profile
opkg update
opkg restore
 
# Roll back Opkg profile
opkg rollback
 
# Set up a custom Opkg profile
uci set opkg.defaults.restore="custom"
uci -q delete opkg.custom.rpkg
uci add_list opkg.custom.rpkg="dnsmasq"
uci add_list opkg.custom.rpkg="ppp"
uci -q delete opkg.custom.ipkg
uci add_list opkg.custom.ipkg="curl"
uci add_list opkg.custom.ipkg="diffutils"
uci add_list opkg.custom.ipkg="dnsmasq-full"
uci commit opkg
 
# Check hotplug log
logread -e hotplug
 
# Upgrade packages
opkg update
opkg upgr
opkg update
opkg install libustream-mbedtls
uclient-fetch -O opkg-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/opkg_extras?codeblock=0"
. ./opkg-extras.sh
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2021/04/16 11:08
  • by vgaetera