Sysupgrade extras

  • This instruction extends the functionality of Sysupgrade on x86 target.
  • Follow the automated section for quick setup.
  • Check the latest firmware release available.
  • Download the firmware image and verify its checksum.
  • Allow to force/skip upgrade matching the current release.
  • Expand the root partition and filesystem after upgrading.
Option Description
-I Initialize Opkg profiles.
-A Upgrade firmware if a newer one is available.
-U Force firmware upgrade.
# Configure profile
mkdir -p /etc/profile.d
cat << "EOF" > /etc/profile.d/sysupgrade.sh
sysupgrade() {
local SYSUP_CMD="${1}"
case "${SYSUP_CMD}" in
(-I) opkg save
sysupgrade_init ;;
(-A) . /etc/openwrt_release
sysupgrade_auto ;;
(-U) local SYSUP_FORCE="1"
sysupgrade -A ;;
(*) command sysupgrade "${@}" ;;
esac
}
 
sysupgrade_init() {
uci -q batch << EOI
set opkg.init='opkg'
add_list opkg.init.ipkg='losetup'
add_list opkg.init.ipkg='parted'
add_list opkg.init.ipkg='resize2fs'
commit opkg
EOI
}
 
sysupgrade_auto() {
local SYSUP_VER="$(sysupgrade_ver)"
local SYSUP_URL="$(sysupgrade_url)"
local SYSUP_REV="$(sysupgrade_rev)"
if [ "${SYSUP_REV}" != "${DISTRIB_REVISION}" ] \
|| [ -n "${SYSUP_FORCE}" ]
then
local SYSUP_DEV="$(sysupgrade_dev)"
local SYSUP_PROF="$(sysupgrade_prof)"
local SYSUP_FS="$(sysupgrade_fs)"
local SYSUP_TYPE="$(sysupgrade_type)"
local SYSUP_EFI="$(sysupgrade_efi)"
local SYSUP_JSON="$(sysupgrade_json)"
local SYSUP_IMG="$(sysupgrade_img)"
local SYSUP_HASH="$(sysupgrade_hash)"
uclient-fetch -P /tmp "${SYSUP_URL}/${SYSUP_IMG}"
sha256sum -c << EOI
${SYSUP_HASH}  /tmp/${SYSUP_IMG}
EOI
case "${SYSUP_IMG}" in
(*.gz) gunzip -k /tmp/"${SYSUP_IMG}" ;;
esac
sysupgrade -i /tmp/"${SYSUP_IMG%.gz}"
fi
}
 
sysupgrade_ver() {
case "${DISTRIB_RELEASE}" in
(SNAPSHOT) echo "../snapshots" ;;
(*) uclient-fetch -O - \
"https://api.github.com/repos/openwrt/openwrt/tags" \
| jsonfilter -e "$[*]['name']" \
| sed -e "/-rc/d;s/^v//;q" ;;
esac
}
 
sysupgrade_url() {
echo "https://downloads.openwrt.org/\
releases/${SYSUP_VER}/targets/${DISTRIB_TARGET}"
}
 
sysupgrade_rev() {
uclient-fetch -O - "${SYSUP_URL}/version.buildinfo"
}
 
sysupgrade_dev() {
ubus call system board \
| jsonfilter -e "$['board_name']"
}
 
sysupgrade_prof() {
case "${DISTRIB_TARGET}" in
(x86*) echo "generic" ;;
(*) echo "${SYSUP_DEV//,/_}" ;;
esac
}
 
sysupgrade_fs() {
ubus call system board \
| jsonfilter -e "$['rootfs_type']"
}
 
sysupgrade_type() {
case "${DISTRIB_TARGET}" in
(x86*) echo "combined" ;;
(*) echo "sysupgrade" ;;
esac
}
 
sysupgrade_efi() {
if [ -d /sys/firmware/efi ]
then echo "-efi"
fi
}
 
sysupgrade_json() {
uclient-fetch -O - "${SYSUP_URL}/profiles.json" \
| jsonfilter -e "$['profiles']['${SYSUP_PROF}']\
['images'][@['type']='${SYSUP_TYPE}${SYSUP_EFI}'\
&&@['filesystem']='${SYSUP_FS}']"
}
 
sysupgrade_img() {
jsonfilter -e "$['name']" << EOI
${SYSUP_JSON}
EOI
}
 
sysupgrade_hash() {
jsonfilter -e "$['sha256']" << EOI
${SYSUP_JSON}
EOI
}
EOF
. /etc/profile.d/sysupgrade.sh
 
# Configure startup scripts
cat << "EOF" > /etc/uci-defaults/70-rootpt-resize
if [ ! -e /etc/rootpt-resize ] \
&& type parted > /dev/null \
&& lock -n /var/lock/rootpt-resize
then
BOOT_DEV="$(sed -n -e "\|\s/boot\s.*$|{s///p;q}" /etc/mtab)"
BOOT_PART="${BOOT_DEV##*[^0-9]}"
DISK_DEV="${BOOT_DEV%${BOOT_PART}}"
parted -l ---pretend-input-tty << EOI
ok
fix
EOI
parted -s "${DISK_DEV%p}" resizepart "$((BOOT_PART+1))" 100%
touch /etc/rootpt-resize
reboot
fi
exit 1
EOF
cat << "EOF" > /etc/uci-defaults/80-rootfs-resize
if [ ! -e /etc/rootfs-resize ] \
&& [ -e /etc/rootpt-resize ] \
&& type losetup > /dev/null \
&& type resize2fs > /dev/null \
&& lock -n /var/lock/rootpt-resize
then
BOOT_DEV="$(sed -n -e "\|\s/boot\s.*$|{s///p;q}" /etc/mtab)"
BOOT_PART="${BOOT_DEV##*[^0-9]}"
DISK_DEV="${BOOT_DEV%${BOOT_PART}}"
ROOT_DEV="${DISK_DEV}$((BOOT_PART+1))"
ROOT_TYPE="$(ubus call system board \
| jsonfilter -e "$['rootfs_type']")"
case "${ROOT_TYPE}" in
(ext4) LOOP_DEV="$(losetup -f)"
losetup "${LOOP_DEV}" "${ROOT_DEV}" ;;
(squashfs) LOOP_DEV="$(losetup -n -l \
| sed -n -e "\|\s.*\s${ROOT_DEV#/dev}\s.*$|{s///p;q}")" ;;
esac
resize2fs -f "${LOOP_DEV}"
touch /etc/rootfs-resize
reboot
fi
exit 1
EOF
cat << "EOF" >> /etc/sysupgrade.conf
/etc/uci-defaults
EOF
# Automated Sysupgrade
sysupgrade -I
sysupgrade -A
uclient-fetch -O sysupgrade-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/sysupgrade_extras?codeblock=0"
. ./sysupgrade-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: 2023/03/24 19:39
  • by vgaetera