sysupgrade 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/system.sh
  4. # initialize defaults
  5. export MTD_CONFIG_ARGS=""
  6. export INTERACTIVE=0
  7. export VERBOSE=1
  8. export SAVE_CONFIG=1
  9. export SAVE_OVERLAY=0
  10. export SAVE_OVERLAY_PATH=
  11. export SAVE_PARTITIONS=1
  12. export SAVE_INSTALLED_PKGS=0
  13. export SKIP_UNCHANGED=0
  14. export CONF_IMAGE=
  15. export CONF_BACKUP_LIST=0
  16. export CONF_BACKUP=
  17. export CONF_RESTORE=
  18. export NEED_IMAGE=
  19. export HELP=0
  20. export FORCE=0
  21. export TEST=0
  22. export UMOUNT_ETCBACKUP_DIR=0
  23. # parse options
  24. while [ -n "$1" ]; do
  25. case "$1" in
  26. -i) export INTERACTIVE=1;;
  27. -v) export VERBOSE="$(($VERBOSE + 1))";;
  28. -q) export VERBOSE="$(($VERBOSE - 1))";;
  29. -n) export SAVE_CONFIG=0;;
  30. -c) export SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/etc;;
  31. -o) export SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/;;
  32. -p) export SAVE_PARTITIONS=0;;
  33. -k) export SAVE_INSTALLED_PKGS=1;;
  34. -u) export SKIP_UNCHANGED=1;;
  35. -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
  36. -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
  37. -l|--list-backup) export CONF_BACKUP_LIST=1;;
  38. -f) export CONF_IMAGE="$2"; shift;;
  39. -F|--force) export FORCE=1;;
  40. -T|--test) export TEST=1;;
  41. -h|--help) export HELP=1; break;;
  42. -*)
  43. echo "Invalid option: $1" >&2
  44. exit 1
  45. ;;
  46. *) break;;
  47. esac
  48. shift;
  49. done
  50. export CONFFILES=/tmp/sysupgrade.conffiles
  51. export CONF_TAR=/tmp/sysupgrade.tgz
  52. export ETCBACKUP_DIR=/etc/backup
  53. export INSTALLED_PACKAGES=${ETCBACKUP_DIR}/installed_packages.txt
  54. IMAGE="$1"
  55. [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
  56. cat <<EOF
  57. Usage: $0 [<upgrade-option>...] <image file or URL>
  58. $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
  59. upgrade-option:
  60. -f <config> restore configuration from .tar.gz (file or url)
  61. -i interactive mode
  62. -c attempt to preserve all changed files in /etc/
  63. -o attempt to preserve all changed files in /, except those
  64. from packages but including changed confs.
  65. -u skip from backup files that are equal to those in /rom
  66. -n do not save configuration over reflash
  67. -p do not attempt to restore the partition table after flash.
  68. -k include in backup a list of current installed packages at
  69. $INSTALLED_PACKAGES
  70. -T | --test
  71. Verify image and config .tar.gz but do not actually flash.
  72. -F | --force
  73. Flash image even if image checks fail, this is dangerous!
  74. -q less verbose
  75. -v more verbose
  76. -h | --help display this help
  77. backup-command:
  78. -b | --create-backup <file>
  79. create .tar.gz of files specified in sysupgrade.conf
  80. then exit. Does not flash an image. If file is '-',
  81. i.e. stdout, verbosity is set to 0 (i.e. quiet).
  82. -r | --restore-backup <file>
  83. restore a .tar.gz created with sysupgrade -b
  84. then exit. Does not flash an image. If file is '-',
  85. the archive is read from stdin.
  86. -l | --list-backup
  87. list the files that would be backed up when calling
  88. sysupgrade -b. Does not create a backup file.
  89. EOF
  90. exit 1
  91. }
  92. [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
  93. cat <<-EOF
  94. -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
  95. Do not specify both -b|-r and a firmware image.
  96. EOF
  97. exit 1
  98. }
  99. # prevent messages from clobbering the tarball when using stdout
  100. [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
  101. list_conffiles() {
  102. awk '
  103. BEGIN { conffiles = 0 }
  104. /^Conffiles:/ { conffiles = 1; next }
  105. !/^ / { conffiles = 0; next }
  106. conffiles == 1 { print }
  107. ' /usr/lib/opkg/status
  108. }
  109. list_changed_conffiles() {
  110. # Cannot handle spaces in filenames - but opkg cannot either...
  111. list_conffiles | while read file csum; do
  112. [ -r "$file" ] || continue
  113. echo "${csum} ${file}" | sha256sum -sc - || echo "$file"
  114. done
  115. }
  116. add_conffiles() {
  117. local file="$1"
  118. ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  119. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  120. \( -type f -o -type l \) $find_filter 2>/dev/null;
  121. list_changed_conffiles ) | sort -u > "$file"
  122. return 0
  123. }
  124. add_overlayfiles() {
  125. local file="$1"
  126. local packagesfiles=$1.packagesfiles
  127. touch "$packagesfiles"
  128. if [ "$SAVE_OVERLAY_PATH" = / ]; then
  129. local conffiles=$1.conffiles
  130. local keepfiles=$1.keepfiles
  131. list_conffiles | cut -f2 -d ' ' | sort -u > "$conffiles"
  132. # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
  133. # ignore those aready controlled by opkg conffiles
  134. find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  135. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  136. \( -type f -o -type l \) 2>/dev/null | sort -u |
  137. grep -h -v -x -F -f $conffiles > "$keepfiles"
  138. # backup conffiles, but only those changed if '-u'
  139. [ $SKIP_UNCHANGED = 1 ] &&
  140. list_changed_conffiles | sort -u > "$conffiles"
  141. # do not backup files from packages, except those listed
  142. # in conffiles and keep.d
  143. {
  144. find /usr/lib/opkg/info -type f -name "*.list" -exec cat {} \;
  145. find /usr/lib/opkg/info -type f -name "*.control" -exec sed \
  146. -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \; |
  147. cut -f2 -d:
  148. } | grep -v -x -F -f $conffiles |
  149. grep -v -x -F -f $keepfiles | sort -u > "$packagesfiles"
  150. rm -f "$keepfiles" "$conffiles"
  151. fi
  152. # busybox grep bug when file is empty
  153. [ -s "$packagesfiles" ] || echo > $packagesfiles
  154. ( cd /overlay/upper/; find .$SAVE_OVERLAY_PATH \( -type f -o -type l \) $find_filter | sed \
  155. -e 's,^\.,,' \
  156. -e '\,^/etc/board.json$,d' \
  157. -e '\,/[^/]*-opkg$,d' \
  158. -e '\,^/etc/urandom.seed$,d' \
  159. -e "\,^$INSTALLED_PACKAGES$,d" \
  160. -e '\,^/usr/lib/opkg/.*,d' \
  161. ) | grep -v -x -F -f $packagesfiles > "$file"
  162. rm -f "$packagesfiles"
  163. return 0
  164. }
  165. # hooks
  166. sysupgrade_image_check="fwtool_check_signature fwtool_check_image platform_check_image"
  167. if [ $SAVE_OVERLAY = 1 ]; then
  168. [ ! -d /overlay/upper/etc ] && {
  169. echo "Cannot find '/overlay/upper/etc', required for '-c'" >&2
  170. exit 1
  171. }
  172. sysupgrade_init_conffiles="add_overlayfiles"
  173. else
  174. sysupgrade_init_conffiles="add_conffiles"
  175. fi
  176. find_filter=""
  177. if [ $SKIP_UNCHANGED = 1 ]; then
  178. [ ! -d /rom/ ] && {
  179. echo "'/rom/' is required by '-u'"
  180. exit 1
  181. }
  182. find_filter='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
  183. fi
  184. include /lib/upgrade
  185. do_save_conffiles() {
  186. local conf_tar="$1"
  187. [ -z "$(rootfs_type)" ] && {
  188. echo "Cannot save config while running from ramdisk." >&2
  189. ask_bool 0 "Abort" && exit
  190. rm -f "$conf_tar"
  191. return 0
  192. }
  193. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  194. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  195. if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
  196. echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
  197. mkdir -p "$ETCBACKUP_DIR"
  198. # Avoid touching filesystem on each backup
  199. RAMFS="$(mktemp -d -t sysupgrade.XXXXXX)"
  200. mkdir -p "$RAMFS/upper" "$RAMFS/work"
  201. mount -t overlay overlay -o lowerdir=$ETCBACKUP_DIR,upperdir=$RAMFS/upper,workdir=$RAMFS/work $ETCBACKUP_DIR &&
  202. UMOUNT_ETCBACKUP_DIR=1 || {
  203. echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
  204. ask_bool 0 "Abort" && exit
  205. }
  206. # Format: pkg-name<TAB>{rom,overlay,unkown}
  207. # rom is used for pkgs in /rom, even if updated later
  208. find /usr/lib/opkg/info -name "*.control" \( \
  209. \( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
  210. \( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
  211. \( -exec echo {} unknown \; \) \
  212. \) | sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
  213. fi
  214. v "Saving config files..."
  215. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  216. tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
  217. if [ "$?" -ne 0 ]; then
  218. echo "Failed to create the configuration backup."
  219. rm -f "$conf_tar"
  220. exit 1
  221. fi
  222. [ "$UMOUNT_ETCBACKUP_DIR" -eq 1 ] && {
  223. umount "$ETCBACKUP_DIR"
  224. rm -rf "$RAMFS"
  225. }
  226. rm -f "$CONFFILES"
  227. }
  228. if [ $CONF_BACKUP_LIST -eq 1 ]; then
  229. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  230. [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
  231. cat "$CONFFILES"
  232. rm -f "$CONFFILES"
  233. exit 0
  234. fi
  235. if [ -n "$CONF_BACKUP" ]; then
  236. do_save_conffiles "$CONF_BACKUP"
  237. exit $?
  238. fi
  239. if [ -n "$CONF_RESTORE" ]; then
  240. if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
  241. echo "Backup archive '$CONF_RESTORE' not found." >&2
  242. exit 1
  243. fi
  244. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  245. tar -C / -x${TAR_V}zf "$CONF_RESTORE"
  246. exit $?
  247. fi
  248. type platform_check_image >/dev/null 2>/dev/null || {
  249. echo "Firmware upgrade is not implemented for this platform." >&2
  250. exit 1
  251. }
  252. case "$IMAGE" in
  253. http://*|\
  254. https://*)
  255. wget -O/tmp/sysupgrade.img "$IMAGE"
  256. IMAGE=/tmp/sysupgrade.img
  257. ;;
  258. esac
  259. IMAGE="$(readlink -f "$IMAGE")"
  260. case "$IMAGE" in
  261. '')
  262. echo "Image file not found." >&2
  263. exit 1
  264. ;;
  265. /tmp/*) ;;
  266. *)
  267. v "Image not in /tmp, copying..."
  268. cp -f "$IMAGE" /tmp/sysupgrade.img
  269. IMAGE=/tmp/sysupgrade.img
  270. ;;
  271. esac
  272. export ARGV="$IMAGE"
  273. export ARGC=1
  274. for check in $sysupgrade_image_check; do
  275. ( $check "$IMAGE" ) || {
  276. if [ $FORCE -eq 1 ]; then
  277. echo "Image check '$check' failed but --force given - will update anyway!" >&2
  278. break
  279. else
  280. echo "Image check '$check' failed." >&2
  281. exit 1
  282. fi
  283. }
  284. done
  285. if [ -n "$CONF_IMAGE" ]; then
  286. case "$(get_magic_word $CONF_IMAGE cat)" in
  287. # .gz files
  288. 1f8b) ;;
  289. *)
  290. echo "Invalid config file. Please use only .tar.gz files" >&2
  291. exit 1
  292. ;;
  293. esac
  294. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  295. export SAVE_CONFIG=1
  296. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  297. [ $TEST -eq 1 ] || do_save_conffiles "$CONF_TAR"
  298. export SAVE_CONFIG=1
  299. else
  300. [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
  301. export SAVE_CONFIG=0
  302. fi
  303. if [ $TEST -eq 1 ]; then
  304. exit 0
  305. fi
  306. if [ $SAVE_PARTITIONS -eq 0 ]; then
  307. touch /tmp/sysupgrade.always.overwrite.bootdisk.partmap
  308. else
  309. rm -f /tmp/sysupgrade.always.overwrite.bootdisk.partmap
  310. fi
  311. install_bin /sbin/upgraded
  312. v "Commencing upgrade. Closing all shell sessions."
  313. COMMAND='. /lib/functions.sh; include /lib/upgrade; do_upgrade_stage2'
  314. if [ -n "$FAILSAFE" ]; then
  315. printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
  316. lock -u /tmp/.failsafe
  317. else
  318. ubus call system sysupgrade "{
  319. \"prefix\": $(json_string "$RAM_ROOT"),
  320. \"path\": $(json_string "$IMAGE"),
  321. \"command\": $(json_string "$COMMAND")
  322. }"
  323. fi