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:sysupgrade_extras [2021/12/30 09:20] – sysupgrade_fs vgaeteradocs:guide-user:advanced:sysupgrade_extras [2023/12/01 01:36] – [Instructions] minimize write operations vgaetera
Line 1: Line 1:
 ====== Sysupgrade extras ====== ====== Sysupgrade extras ======
-{{page>meta:infobox:wip&noheader&nofooter&noeditbtn}} +{{section>meta:infobox:howto_links#basic_skills&noheader&nofooter&noeditbutton}}
-{{section>meta:infobox:howto_links#cli_skills&noheader&nofooter&noeditbutton}}+
  
 ===== Introduction ===== ===== Introduction =====
Line 8: Line 7:
  
 ===== Features ===== ===== Features =====
-  * Automate upgrading to the latest firmware release. +  * Check the latest firmware release available
-  * Automate expanding the root partition and filesystem.+  * Download the firmware image and verify its checksum. 
 +  * Allow to force/skip upgrade matching the current release. 
 +  * Save/restore the state of enabled/disabled services.
  
 ===== Options ===== ===== Options =====
 ^ Option ^ Description ^ ^ Option ^ Description ^
-| ''**-I**''Initialize Opkg profiles. | +| ''**-A**''Download and upgrade firmware if a newer stable release is available. | 
-| ''**-A**''Initialize automated upgrade. |+| ''**-U**''Download and upgrade firmware for the latest stable release. | 
 +| ''**-S**'' | Save the current state of enabled/disabled services to UCI. | 
 +| ''**-R**'' | Restore the state of enabled/disabled services. |
  
 ===== Instructions ===== ===== Instructions =====
Line 24: Line 27:
 local SYSUP_CMD="${1}" local SYSUP_CMD="${1}"
 case "${SYSUP_CMD}" in case "${SYSUP_CMD}" in
-(-Iopkg save +(-A|-Usysupgrade_proc "${@}" ;; 
-sysupgrade_init ;; +(-Sservice_save ;; 
-(-A. /etc/openwrt_release +(-R) service_restore ;;
-sysupgrade_auto ;;+
 (*) command sysupgrade "${@}" ;; (*) command sysupgrade "${@}" ;;
 esac esac
 } }
  
-sysupgrade_init() { +sysupgrade_proc() { 
-uci -q batch << EOI +/etc/os-release
-set opkg.init='opkg' +
-add_list opkg.init.ipkg='fdisk' +
-add_list opkg.init.ipkg='losetup' +
-add_list opkg.init.ipkg='resize2fs' +
-add_list opkg.init.ipkg='f2fs-tools' +
-commit opkg +
-EOI +
-+
- +
-sysupgrade_auto() {+
 local SYSUP_VER="$(sysupgrade_ver)" local SYSUP_VER="$(sysupgrade_ver)"
 local SYSUP_URL="$(sysupgrade_url)" local SYSUP_URL="$(sysupgrade_url)"
 +local SYSUP_REV="$(sysupgrade_rev)"
 +if [ "${SYSUP_REV}" != "${BUILD_ID}" ] \
 +|| [ "${SYSUP_CMD}" = "-U" ]
 +then shift
 +local SYSUP_DEV="$(sysupgrade_dev)"
 local SYSUP_PROF="$(sysupgrade_prof)" local SYSUP_PROF="$(sysupgrade_prof)"
 local SYSUP_FS="$(sysupgrade_fs)" local SYSUP_FS="$(sysupgrade_fs)"
 local SYSUP_TYPE="$(sysupgrade_type)" local SYSUP_TYPE="$(sysupgrade_type)"
 +local SYSUP_EFI="$(sysupgrade_efi)"
 local SYSUP_IMG="$(sysupgrade_img)" local SYSUP_IMG="$(sysupgrade_img)"
-uclient-fetch -P /tmp "${SYSUP_URL}/${SYSUP_IMG}" +sysupgrade "${@}" "${SYSUP_URL}/${SYSUP_IMG}" 
-case "${SYSUP_IMG}" in +fi
-(*.gz) gunzip -k /tmp/"${SYSUP_IMG}" ;; +
-esac +
-sysupgrade -i /tmp/"${SYSUP_IMG%.gz}"+
 } }
  
 sysupgrade_ver() { sysupgrade_ver() {
-case "${DISTRIB_RELEASE}" in +case "${VERSION_ID}" in 
-(SNAPSHOT) echo "../snapshots" ;; +(snapshot) echo "../snapshots" ;; 
-(*) uclient-fetch -O - \+(*) wget -O - \
 "https://api.github.com/repos/openwrt/openwrt/tags" \ "https://api.github.com/repos/openwrt/openwrt/tags" \
-| jsonfilter -e "@[@]['name']" \+| jsonfilter -e "$[*]['name']" \
 | sed -e "/-rc/d;s/^v//;q" ;; | sed -e "/-rc/d;s/^v//;q" ;;
 esac esac
Line 69: Line 64:
 sysupgrade_url() { sysupgrade_url() {
 echo "https://downloads.openwrt.org/\ echo "https://downloads.openwrt.org/\
-releases/${SYSUP_VER}/targets/${DISTRIB_TARGET}"+releases/${SYSUP_VER}/targets/${OPENWRT_BOARD}
 +
 + 
 +sysupgrade_rev() { 
 +wget -O - "${SYSUP_URL}/version.buildinfo" 
 +
 + 
 +sysupgrade_dev() { 
 +ubus call system board \ 
 +| jsonfilter -e "$['board_name']"
 } }
  
 sysupgrade_prof() { sysupgrade_prof() {
-case "${DISTRIB_TARGET}" in +case "${OPENWRT_BOARD}" in 
-(x86*) echo "generic" ;; +(x86/*) echo "generic" ;; 
-(*) ubus call system board \ +(*) echo "${SYSUP_DEV//,/_}" ;;
-| jsonfilter -e "@['board_name']"+
-| sed -e "s/,/_/" ;;+
 esac esac
 } }
  
 sysupgrade_fs() { sysupgrade_fs() {
-if grep -q -e "\s/\soverlay\s" /etc/mtab +ubus call system board \ 
-then echo "squashfs" +| jsonfilter -e "$['rootfs_type']"
-else echo "ext4" +
-fi +
-# OpenWrt snapshot +
-ubus call system board \ +
-| jsonfilter -e "@['rootfs_type']"+
 } }
  
 sysupgrade_type() { sysupgrade_type() {
-case "${DISTRIB_TARGET}" in +case "${OPENWRT_BOARD}" in 
-(x86*) echo "combined" ;;+(x86/*) echo "combined" ;;
 (*) echo "sysupgrade" ;; (*) echo "sysupgrade" ;;
 esac esac
 +}
 +
 +sysupgrade_efi() {
 +if [ -d /sys/firmware/efi ]
 +then echo "-efi"
 +fi
 } }
  
 sysupgrade_img() { sysupgrade_img() {
-uclient-fetch -O - "${SYSUP_URL}/profiles.json"+wget -O - "${SYSUP_URL}/profiles.json"
-| jsonfilter -e "@['profiles']['${SYSUP_PROF}']['images']+| jsonfilter -e "$['profiles']['${SYSUP_PROF}'] 
-[@['type']='${SYSUP_TYPE}'&&@['filesystem']='${SYSUP_FS}']+['images'][@['type']='${SYSUP_TYPE}${SYSUP_EFI}' 
-['name']"+&&@['filesystem']='${SYSUP_FS}']['name']"
 } }
-EOF 
  
-# Configure uci-defaults +service_save() { 
-cat << "EOF" > /etc/uci-defaults/70-root-resize +local SVC_NAME="$(ls /etc/init.d)" 
-if [ ! -e /etc/root-resize ] \ +local SVC_STATE 
-&& lock -n /var/lock/root-resize \ +for SVC_NAME in ${SVC_NAME
-&& [ -e /etc/opkg-restore-init ] +do if command service "${SVC_NAME}" enabled 
-then +then SVC_STATE="enable
-BOOT="$(sed -n -e "/\s\/boot\s.*$/{s///p;q}" /etc/mtab)" +else SVC_STATE="disable"
-DISK="${BOOT%%[0-9]*}" +
-PART="$((${BOOT##*[^0-9]}+1))+
-ROOT="${DISK}${PART}+
-OFFS="$(fdisk "${DISK}" -l -o "device,start"+
-| sed -n -e "\|^${ROOT}\s*|s///p")" +
-echo -e "p\nd\n${PART}\nn\np\n${PART}\n${OFFS}\n\nn\np\nw"+
-| fdisk "${DISK}" +
-touch /etc/root-resize +
-lock -u /var/lock/root-resize+
 fi fi
-exit 1+uci -q batch << EOI 
 +set system.service='service' 
 +del_list system.service.enable='${SVC_NAME}' 
 +del_list system.service.disable='${SVC_NAME}' 
 +add_list system.service.'${SVC_STATE}'='${SVC_NAME}' 
 +EOI 
 +done 
 +uci commit system 
 +
 + 
 +service_restore() { 
 +local SVC_NAME 
 +local SVC_STATE="enable disable" 
 +for SVC_STATE in ${SVC_STATE} 
 +do SVC_NAME="$(uci -q get system.service."${SVC_STATE}")" 
 +for SVC_NAME in ${SVC_NAME} 
 +do command service "${SVC_NAME}" "${SVC_STATE}" 
 +done 
 +done 
 +}
 EOF EOF
-cat << "EOF"/etc/uci-defaults/80-ext4-resize +/etc/profile.d/sysupgrade.sh 
-if grep -q -e "\s/\sext4\s" /etc/mtab \ + 
-&& [ ! -e /etc/ext4-resize ] \ +Configure startup scripts 
-&& lock -n /var/lock/ext4-resize \ +cat << "EOF" > /etc/uci-defaults/60-service-restore 
-&& [ -e /etc/opkg-restore-init ] +if [ ! -e /etc/service-restore 
-then +then . /etc/profile.d/sysupgrade.sh 
-BOOT="$(sed -n -e "/\s\/boot\s.*$/{s///p;q}" /etc/mtab)" +sysupgrade -R 
-DISK="${BOOT%%[0-9]*}" +touch /etc/service-restore
-PART="$((${BOOT##*[^0-9]}+1))" +
-ROOT="${DISK}${PART}" +
-LOOP="$(losetup -f)" +
-losetup "${LOOP}" "${ROOT}" +
-fsck.ext4 -y "${LOOP}" +
-resize2fs "${LOOP}" +
-touch /etc/ext4-resize +
-lock -u /var/lock/ext4-resize +
-reboot +
-fi +
-exit 1 +
-EOF +
-cat << "EOF" > /etc/uci-defaults/80-f2fs-resize +
-if grep -q -e "\s/\soverlay\s" /etc/mtab \ +
-&& [ ! -e /etc/f2fs-resize \ +
-&& lock -n /var/lock/f2fs-resize \ +
-&& [ -e /etc/opkg-restore-init ] +
-then +
-LOOP="$(losetup -n -O "NAME"+
-| sed -n -e "1p")" +
-ROOT="$(losetup -n -O "BACK-FILE" "${LOOP}"+
-| sed -e "s|^|/dev|")" +
-OFFS="$(losetup -n -O "OFFSET" "${LOOP}")" +
-LOOP="$(losetup -f)" +
-losetup -o "${OFFS}" "${LOOP}" "${ROOT}" +
-fsck.f2fs -f "${LOOP}" +
-mount "${LOOP}" /mnt +
-umount "${LOOP}" +
-resize.f2fs "${LOOP}" +
-touch /etc/f2fs-resize +
-lock -u /var/lock/f2fs-resize +
-reboot+
 fi fi
 exit 1 exit 1
 EOF EOF
 cat << "EOF" >> /etc/sysupgrade.conf cat << "EOF" >> /etc/sysupgrade.conf
-/etc/uci-defaults+/etc/uci-defaults/60-service-restore
 EOF EOF
 </code> </code>
Line 175: Line 155:
 ===== Examples ===== ===== Examples =====
 <code bash> <code bash>
-# Automated Sysupgrade +# Automated interactive Sysupgrade 
-sysupgrade -I +sysupgrade -A -i 
-sysupgrade -A+ 
 +# Forced interactive Sysupgrade 
 +sysupgrade -U -i
 </code> </code>
  
 ===== Automated ===== ===== Automated =====
 <code bash> <code bash>
-uclient-fetch -O sysupgrade-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/sysupgrade_extras?codeblock=0"+wget -U "" -O sysupgrade-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/sysupgrade_extras?codeblock=0"
 . ./sysupgrade-extras.sh . ./sysupgrade-extras.sh
 </code> </code>
  
  • Last modified: 2024/06/12 13:16
  • by vgaetera