Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:advanced:opkg_extras [2021/09/26 06:24] – [Instructions] save rwm when it is defined and mounted vgaeteradocs:guide-user:advanced:opkg_extras [2023/10/03 18:19] – [Instructions] delay for networking to become ready vgaetera
Line 1: Line 1:
 ====== Opkg extras ====== ====== Opkg extras ======
-{{section>meta:infobox:howto_links#cli_skills&noheader&nofooter&noeditbutton}}+{{section>meta:infobox:howto_links#basic_skills&noheader&nofooter&noeditbutton}}
  
 ===== Introduction ===== ===== Introduction =====
-  * This instruction extends [[docs:guide-user:additional-software:opkg|Opkg]] functionality. +  * This instruction extends the functionality of [[docs:guide-user:additional-software:opkg|Opkg]].
-  * Follow [[docs:guide-user:advanced:hotplug_extras|Hotplug extras]] to extend Hotplug functionality. +
-  * Follow [[docs:guide-user:advanced:uci_extras|UCI extras]] to extend UCI functionality.+
   * Follow the [[docs:guide-user:advanced:opkg_extras#automated|automated]] section for quick setup.   * Follow the [[docs:guide-user:advanced:opkg_extras#automated|automated]] section for quick setup.
  
Line 15: Line 13:
   * Restore automatically after firmware upgrade.   * Restore automatically after firmware upgrade.
   * Upgrade all upgradable packages.   * Upgrade all upgradable packages.
-  * Find new configurations.+  * Fix symbolic links for installed alternatives. 
 +  * Find new configurations for upgraded packages.
  
 ===== Implementation ===== ===== Implementation =====
Line 21: Line 20:
   * Rely on [[docs:guide-user:base-system:uci|UCI]] and [[https://github.com/openwrt/openwrt/blob/master/package/base-files/Makefile#L47|backup defaults]] to store and manage profiles.   * Rely on [[docs:guide-user:base-system:uci|UCI]] and [[https://github.com/openwrt/openwrt/blob/master/package/base-files/Makefile#L47|backup defaults]] to store and manage profiles.
   * Identify package types with [[man>grep(1)|grep]] and [[man>find(1)|find]], and RWM with [[man>mount(8)|mount]].   * Identify package types with [[man>grep(1)|grep]] and [[man>find(1)|find]], and RWM with [[man>mount(8)|mount]].
-  * Support importing [[https://github.com/openwrt/openwrt/blob/master/package/base-files/files/sbin/sysupgrade#L245-L251|user-installed]] packages from [[docs:techref:sysupgrade|Sysupgrade]]. +  * Support importing [[https://github.com/openwrt/openwrt/blob/master/package/base-files/files/sbin/sysupgrade#L249-L255|user-installed]] packages from [[docs:techref:sysupgrade|Sysupgrade]]. 
-  * Use [[docs:guide-user:base-system:hotplug|Hotplug]] to detect WAN connectivity and trigger profile restore.+  * Use [[docs:guide-user:advanced:hotplug_extras|Hotplug extras]] to detect WAN connectivity and trigger profile restore.
   * Utilize lockfiles with [[https://github.com/openwrt/openwrt/blob/master/package/utils/busybox/patches/220-add_lock_util.patch|lock]] to avoid race conditions and loops.   * Utilize lockfiles with [[https://github.com/openwrt/openwrt/blob/master/package/utils/busybox/patches/220-add_lock_util.patch|lock]] to avoid race conditions and loops.
   * Perform [[man>reboot(8)|reboot]] to apply changes after automatic profile restore.   * Perform [[man>reboot(8)|reboot]] to apply changes after automatic profile restore.
-  * Write and read non-interactive logs with [[docs:guide-user:base-system:log.essentials|Syslog]] for troubleshooting. 
   * Fetch the default profile to restore and roll back from UCI, unless specified manually.   * 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.   * Take precedence for remove over install to avoid package conflicts while restoring profile.
Line 32: Line 30:
  
 ===== Commands ===== ===== Commands =====
-Opkg sub-command ^ Description ^+Sub-command ^ Description ^
 | ''**init**'' | Initialize Opkg configuration. | | ''**init**'' | Initialize Opkg configuration. |
 | ''**uci** [<prof>] < <uciprof>'' | Import Opkg profile from stdin or file to UCI. | | ''**uci** [<prof>] < <uciprof>'' | Import Opkg profile from stdin or file to UCI. |
 | ''**import** [<bakprof>]'' | Import Opkg profile from Sysupgrade backup to UCI. | | ''**import** [<bakprof>]'' | Import Opkg profile from Sysupgrade backup to UCI. |
-| ''**save** [<prof>]'' | Save the current Opkg profile to UCI. | +| ''**save**'' | Save the current Opkg profile to UCI. | 
-| ''**restore** [<prof>]'' | Restore Opkg profile:\\ ''auto'' or none - default profile,\\ ''rwm'' - RWM profile,\\ ''custom'' - custom profile. |+| ''**restore** [<prof>]'' | Restore Opkg profile:\\ ''main'' or none - default profile,\\ ''init'' - RWM/minimal profile,\\ ''custom'' - custom profile. |
 | ''**rollback** [<prof>]'' | Roll back Opkg profile:\\ remove user-installed packages,\\ install user-removed packages,\\ skip upgraded packages. | | ''**rollback** [<prof>]'' | Roll back Opkg profile:\\ remove user-installed packages,\\ install user-removed packages,\\ skip upgraded packages. |
 | ''**upgr** [<pkgtype>]'' | Upgrade packages by type:\\ ''ai'' or none - all installed,\\ ''oi'' - overlay-installed. | | ''**upgr** [<pkgtype>]'' | Upgrade packages by type:\\ ''ai'' or none - all installed,\\ ''oi'' - overlay-installed. |
Line 43: Line 41:
 | ''**proc** <cmd> < <pkgs>'' | Process packages from stdin with the given sub-command. | | ''**proc** <cmd> < <pkgs>'' | Process packages from stdin with the given sub-command. |
 | ''**reinstall** <pkgs>'' | Reinstall specific packages. | | ''**reinstall** <pkgs>'' | Reinstall specific packages. |
-| ''**newconf** [<path>]'' | Find new configurations. |+| ''**altlink**'' | Fix symbolic links for installed alternatives. | 
 +| ''**newconf** [<path>]'' | Find new configurations for upgraded packages. |
  
 ===== Instructions ===== ===== Instructions =====
 +Copy the following text and paste it in a SSH or serial console of the device
 <code bash> <code bash>
 # Configure profile # Configure profile
Line 54: Line 54:
 local OPKG_UCI="$(uci -q get opkg.defaults."${OPKG_CMD}")" local OPKG_UCI="$(uci -q get opkg.defaults."${OPKG_CMD}")"
 case "${OPKG_CMD}" in case "${OPKG_CMD}" in
-(init|uci|import|save|restore|rollback\ +(init|uci|import|save|restore|rollback|upgr
-|upgr|export|newconf|proc|reinstall) opkg_"${@}" ;;+|export|proc|reinstall|altlink|newconf) opkg_"${@}" ;;
 (*) command opkg "${@}" ;; (*) command opkg "${@}" ;;
 esac esac
Line 63: Line 63:
 uci import opkg < /dev/null uci import opkg < /dev/null
 uci -q batch << EOI uci -q batch << EOI
-set opkg.defaults=opkg +set opkg.defaults='opkg' 
-set opkg.defaults.import=/etc/backup/installed_packages.txt +set opkg.defaults.import='/etc/backup/installed_packages.txt' 
-set opkg.defaults.save=auto +set opkg.defaults.restore='init main' 
-set opkg.defaults.restore=auto +set opkg.defaults.rollback='main' 
-set opkg.defaults.rollback=auto +set opkg.defaults.upgr='ai' 
-set opkg.defaults.upgr=ai +set opkg.defaults.export='ai' 
-set opkg.defaults.export=ai +set opkg.defaults.proc='--force-depends' 
-set opkg.defaults.proc=--force-depends +set opkg.defaults.reinstall='--force-reinstall' 
-set opkg.defaults.reinstall=--force-reinstall +set opkg.defaults.newconf='/etc'
-set opkg.defaults.newconf=/etc+
 EOI EOI
 } }
Line 78: Line 77:
 opkg_uci() { opkg_uci() {
 local OPKG_OPT="${1:-${OPKG_UCI}}" local OPKG_OPT="${1:-${OPKG_UCI}}"
-local OPKG_OPT="${OPKG_OPT:-auto}"+local OPKG_OPT="${OPKG_OPT:-main}"
 if ! uci -q get opkg > /dev/null if ! uci -q get opkg > /dev/null
 then opkg init then opkg init
 fi fi
 uci -q batch << EOI uci -q batch << EOI
-delete opkg.${OPKG_OPT} +delete opkg.'${OPKG_OPT}' 
-set opkg.${OPKG_OPT}=opkg+set opkg.'${OPKG_OPT}'='opkg'
 $(sed -r -e "s/^(.*)\s(.*)$/\ $(sed -r -e "s/^(.*)\s(.*)$/\
-del_list opkg.${OPKG_OPT}.\2=\1\n\ +del_list opkg.'${OPKG_OPT}'.'\2'='\1'\n\ 
-add_list opkg.${OPKG_OPT}.\2=\1/")+add_list opkg.'${OPKG_OPT}'.'\2'='\1'/")
 commit opkg commit opkg
 EOI EOI
Line 97: Line 96:
 then sed -n -r -e "s/\s(overlay|unknown)$/\ then sed -n -r -e "s/\s(overlay|unknown)$/\
 \tipkg/p" "${OPKG_OPT}" \ \tipkg/p" "${OPKG_OPT}" \
-| opkg uci auto+| opkg uci main
 fi fi
 } }
  
 opkg_save() { opkg_save() {
-local OPKG_OPT="${1:-${OPKG_UCI}}" 
 local OPKG_WR="$(opkg export wr)" local OPKG_WR="$(opkg export wr)"
 local OPKG_WI="$(opkg export wi)" local OPKG_WI="$(opkg export wi)"
Line 112: Line 110:
 sed -e "s/$/\trpkg/" "${OPKG_WR}" sed -e "s/$/\trpkg/" "${OPKG_WR}"
 sed -e "s/$/\tipkg/" "${OPKG_WI}" sed -e "s/$/\tipkg/" "${OPKG_WI}"
-} | opkg uci rwm+} | opkg uci init
 fi fi
 { {
 sed -e "s/$/\trpkg/" "${OPKG_UR}" sed -e "s/$/\trpkg/" "${OPKG_UR}"
 sed -e "s/$/\tipkg/" "${OPKG_UI}" sed -e "s/$/\tipkg/" "${OPKG_UI}"
-} | opkg uci "${OPKG_OPT}"+} | opkg uci main
 rm -f "${OPKG_WR}" "${OPKG_WI}" "${OPKG_UR}" "${OPKG_UI}" rm -f "${OPKG_WR}" "${OPKG_WI}" "${OPKG_UR}" "${OPKG_UI}"
 } }
Line 123: Line 121:
 opkg_restore() { opkg_restore() {
 local OPKG_OPT="${1:-${OPKG_UCI}}" local OPKG_OPT="${1:-${OPKG_UCI}}"
-local OPKG_CFG="${OPKG_OPT}"+local OPKG_CONF="${OPKG_OPT}" 
 +for OPKG_CONF in ${OPKG_CONF} 
 +do
 local OPKG_AI="$(opkg export ai)" local OPKG_AI="$(opkg export ai)"
 local OPKG_PR="$(opkg export pr)" local OPKG_PR="$(opkg export pr)"
Line 132: Line 132:
 | opkg proc install | opkg proc install
 rm -f "${OPKG_AI}" "${OPKG_PR}" "${OPKG_PI}" rm -f "${OPKG_AI}" "${OPKG_PR}" "${OPKG_PI}"
 +done
 } }
  
 opkg_rollback() { opkg_rollback() {
 local OPKG_OPT="${1:-${OPKG_UCI}}" local OPKG_OPT="${1:-${OPKG_UCI}}"
-local OPKG_CFG="${OPKG_OPT}"+local OPKG_CONF="${OPKG_OPT}"
 local OPKG_UR="$(opkg export ur)" local OPKG_UR="$(opkg export ur)"
 local OPKG_UI="$(opkg export ui)" local OPKG_UI="$(opkg export ui)"
 local OPKG_PR="$(opkg export pr)" local OPKG_PR="$(opkg export pr)"
 local OPKG_PI="$(opkg export pi)" local OPKG_PI="$(opkg export pi)"
-if uci -q get opkg > /dev/null +if uci -q get opkg."${OPKG_CONF}" > /dev/null 
-then opkg restore "${OPKG_OPT}"+then opkg restore "${OPKG_CONF}"
 grep -v -x -f "${OPKG_PI}" "${OPKG_UI}" \ grep -v -x -f "${OPKG_PI}" "${OPKG_UI}" \
 | opkg proc remove | opkg proc remove
Line 171: Line 172:
 opkg_export() { opkg_export() {
 local OPKG_OPT="${1:-${OPKG_UCI}}" local OPKG_OPT="${1:-${OPKG_UCI}}"
-local OPKG_EXP="$(mktemp -t opkg.XXXXXX)"+local OPKG_TEMP="$(mktemp -t opkg.XXXXXX)"
 case "${OPKG_OPT}" in case "${OPKG_OPT}" in
 (ai|au) opkg_"${OPKG_CMD}"_cmd ;; (ai|au) opkg_"${OPKG_CMD}"_cmd ;;
Line 177: Line 178:
 (ur|ui) opkg_"${OPKG_CMD}"_run ;; (ur|ui) opkg_"${OPKG_CMD}"_run ;;
 (pr|pi) opkg_"${OPKG_CMD}"_uci ;; (pr|pi) opkg_"${OPKG_CMD}"_uci ;;
-esac > "${OPKG_EXP}" +esac > "${OPKG_TEMP}" 
-echo "${OPKG_EXP}"+echo "${OPKG_TEMP}"
 } }
  
Line 224: Line 225:
 (i) OPKG_TYPE="ipkg" ;; (i) OPKG_TYPE="ipkg" ;;
 esac esac
-uci -q get opkg."${OPKG_CFG}"."${OPKG_TYPE}" \+uci -q get opkg."${OPKG_CONF}"."${OPKG_TYPE}" \
 | sed -e "s/\s/\n/g" | sed -e "s/\s/\n/g"
 } }
Line 240: Line 241:
 local OPKG_OPT="${OPKG_UCI}" local OPKG_OPT="${OPKG_UCI}"
 opkg install "${@}" ${OPKG_OPT} opkg install "${@}" ${OPKG_OPT}
 +}
 +
 +opkg_altlink() {
 +sed -n -e "/^Alternatives:/\
 +{s/^\S*\s*//;s/,\s/\n/gp}" /usr/lib/opkg/status \
 +| sort -n -t ":" \
 +| while IFS=":" read -r OPKG_PRIO OPKG_LINK OPKG_ALT
 +do ln -f -s "${OPKG_ALT}" "${OPKG_LINK}"
 +done
 } }
  
Line 249: Line 259:
 . /etc/profile.d/opkg.sh . /etc/profile.d/opkg.sh
  
-Restore packages automatically+Configure hotplug
 mkdir -p /etc/hotplug.d/online mkdir -p /etc/hotplug.d/online
 cat << "EOF" > /etc/hotplug.d/online/50-opkg-restore cat << "EOF" > /etc/hotplug.d/online/50-opkg-restore
-if [ ! -e /etc/opkg-restore ] \+OPKG_CONF="$(uci -q get opkg.defaults.restore)" 
 +for OPKG_CONF in ${OPKG_CONF} 
 +do if [ ! -e /etc/opkg-restore-"${OPKG_CONF}" ] \
 && lock -n /var/lock/opkg-restore \ && lock -n /var/lock/opkg-restore \
 +&& sleep 10 \
 && opkg update && opkg update
 then . /etc/profile.d/opkg.sh then . /etc/profile.d/opkg.sh
-if uci -q get fstab.rwm /dev/null +opkg restore "${OPKG_CONF}" &> \ 
-&& ! grep -q -e "\s/rwm\s" /etc/mtab +/var/log/opkg-restore-"${OPKG_CONF}" 
-then opkg restore rwm +touch /etc/opkg-restore-"${OPKG_CONF}"
-else opkg restore +
-fi +
-touch /etc/opkg-restore+
 lock -u /var/lock/opkg-restore lock -u /var/lock/opkg-restore
 reboot reboot
 +break
 fi fi
 +done
 EOF EOF
 cat << "EOF" >> /etc/sysupgrade.conf cat << "EOF" >> /etc/sysupgrade.conf
Line 283: Line 295:
 opkg rollback opkg rollback
  
-# Set up a custom Opkg profile +# Set up Opkg profiles 
-uci set opkg.defaults.restore="custom"+uci set opkg.defaults.restore="init custom
 +uci set opkg.init="opkg" 
 +uci add_list opkg.init.ipkg="losetup" 
 +uci add_list opkg.init.ipkg="parted" 
 +uci add_list opkg.init.ipkg="resize2fs"
 uci set opkg.custom="opkg" uci set opkg.custom="opkg"
 uci add_list opkg.custom.rpkg="dnsmasq" uci add_list opkg.custom.rpkg="dnsmasq"
Line 293: Line 309:
 uci commit opkg uci commit opkg
  
-# Check hotplug log +# Check Opkg log 
-logread -e hotplug+tail -f /var/log/opkg-*
  
 # Upgrade packages # Upgrade packages
 opkg update opkg update
 opkg upgr opkg upgr
 +
 +# Maintenance
 +opkg altlink
 +opkg newconf
 </code> </code>
  
 ===== Automated ===== ===== Automated =====
 <code bash> <code bash>
-uclient-fetch -O opkg-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/opkg_extras?codeblock=0"+wget -U "" -O opkg-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/opkg_extras?codeblock=0"
 . ./opkg-extras.sh . ./opkg-extras.sh
 </code> </code>
  
  • Last modified: 2023/11/30 19:06
  • by vgaetera