sysupgrade 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/system.sh
  4. # initialize defaults
  5. RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
  6. RAMFS_COPY_DATA="" # extra data files
  7. export MTD_CONFIG_ARGS=""
  8. export INTERACTIVE=0
  9. export VERBOSE=1
  10. export SAVE_CONFIG=1
  11. export SAVE_OVERLAY=0
  12. export SAVE_PARTITIONS=1
  13. export DELAY=
  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. # parse options
  23. while [ -n "$1" ]; do
  24. case "$1" in
  25. -i) export INTERACTIVE=1;;
  26. -d) export DELAY="$2"; shift;;
  27. -v) export VERBOSE="$(($VERBOSE + 1))";;
  28. -q) export VERBOSE="$(($VERBOSE - 1))";;
  29. -n) export SAVE_CONFIG=0;;
  30. -c) export SAVE_OVERLAY=1;;
  31. -p) export SAVE_PARTITIONS=0;;
  32. -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
  33. -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
  34. -l|--list-backup) export CONF_BACKUP_LIST=1; break;;
  35. -f) export CONF_IMAGE="$2"; shift;;
  36. -F|--force) export FORCE=1;;
  37. -T|--test) export TEST=1;;
  38. -h|--help) export HELP=1; break;;
  39. -*)
  40. echo "Invalid option: $1"
  41. exit 1
  42. ;;
  43. *) break;;
  44. esac
  45. shift;
  46. done
  47. export CONFFILES=/tmp/sysupgrade.conffiles
  48. export CONF_TAR=/tmp/sysupgrade.tgz
  49. export ARGV="$*"
  50. export ARGC="$#"
  51. [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
  52. cat <<EOF
  53. Usage: $0 [<upgrade-option>...] <image file or URL>
  54. $0 [-q] [-i] <backup-command> <file>
  55. upgrade-option:
  56. -d <delay> add a delay before rebooting
  57. -f <config> restore configuration from .tar.gz (file or url)
  58. -i interactive mode
  59. -c attempt to preserve all changed files in /etc/
  60. -n do not save configuration over reflash
  61. -p do not attempt to restore the partition table after flash.
  62. -T | --test
  63. Verify image and config .tar.gz but do not actually flash.
  64. -F | --force
  65. Flash image even if image checks fail, this is dangerous!
  66. -q less verbose
  67. -v more verbose
  68. -h | --help display this help
  69. backup-command:
  70. -b | --create-backup <file>
  71. create .tar.gz of files specified in sysupgrade.conf
  72. then exit. Does not flash an image. If file is '-',
  73. i.e. stdout, verbosity is set to 0 (i.e. quiet).
  74. -r | --restore-backup <file>
  75. restore a .tar.gz created with sysupgrade -b
  76. then exit. Does not flash an image. If file is '-',
  77. the archive is read from stdin.
  78. -l | --list-backup
  79. list the files that would be backed up when calling
  80. sysupgrade -b. Does not create a backup file.
  81. EOF
  82. exit 1
  83. }
  84. [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
  85. cat <<-EOF
  86. -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
  87. Do not specify both -b|-r and a firmware image.
  88. EOF
  89. exit 1
  90. }
  91. # prevent messages from clobbering the tarball when using stdout
  92. [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
  93. list_conffiles() {
  94. awk '
  95. BEGIN { conffiles = 0 }
  96. /^Conffiles:/ { conffiles = 1; next }
  97. !/^ / { conffiles = 0; next }
  98. conffiles == 1 { print }
  99. ' /usr/lib/opkg/status
  100. }
  101. list_changed_conffiles() {
  102. # Cannot handle spaces in filenames - but opkg cannot either...
  103. list_conffiles | while read file csum; do
  104. [ -r "$file" ] || continue
  105. echo "${csum} ${file}" | sha256sum -sc - || echo "$file"
  106. done
  107. }
  108. add_uci_conffiles() {
  109. local file="$1"
  110. ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  111. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  112. -type f -o -type l 2>/dev/null;
  113. list_changed_conffiles ) | sort -u > "$file"
  114. return 0
  115. }
  116. add_overlayfiles() {
  117. local file="$1"
  118. if [ -d /overlay/upper ]; then
  119. local overlaydir="/overlay/upper"
  120. else
  121. local overlaydir="/overlay"
  122. fi
  123. find $overlaydir/etc/ -type f -o -type l | sed \
  124. -e 's,^/overlay\/upper/,/,' \
  125. -e 's,^/overlay/,/,' \
  126. -e '\,/META_[a-zA-Z0-9]*$,d' \
  127. -e '\,/functions.sh$,d' \
  128. -e '\,/[^/]*-opkg$,d' \
  129. > "$file"
  130. return 0
  131. }
  132. # hooks
  133. sysupgrade_image_check="fwtool_check_image platform_check_image"
  134. sysupgrade_pre_upgrade="fwtool_pre_upgrade"
  135. [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
  136. sysupgrade_init_conffiles="add_uci_conffiles" || \
  137. sysupgrade_init_conffiles="add_overlayfiles"
  138. include /lib/upgrade
  139. [ "$1" = "nand" ] && nand_upgrade_stage2 $@
  140. do_save_conffiles() {
  141. local conf_tar="${1:-$CONF_TAR}"
  142. [ -z "$(rootfs_type)" ] && {
  143. echo "Cannot save config while running from ramdisk."
  144. ask_bool 0 "Abort" && exit
  145. return 0
  146. }
  147. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  148. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  149. v "Saving config files..."
  150. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  151. tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
  152. rm -f "$CONFFILES"
  153. }
  154. if [ $CONF_BACKUP_LIST -eq 1 ]; then
  155. add_uci_conffiles "$CONFFILES"
  156. cat "$CONFFILES"
  157. rm -f "$CONFFILES"
  158. exit 0
  159. fi
  160. if [ -n "$CONF_BACKUP" ]; then
  161. do_save_conffiles "$CONF_BACKUP"
  162. exit $?
  163. fi
  164. if [ -n "$CONF_RESTORE" ]; then
  165. if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
  166. echo "Backup archive '$CONF_RESTORE' not found."
  167. exit 1
  168. fi
  169. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  170. tar -C / -x${TAR_V}zf "$CONF_RESTORE"
  171. exit $?
  172. fi
  173. type platform_check_image >/dev/null 2>/dev/null || {
  174. echo "Firmware upgrade is not implemented for this platform."
  175. exit 1
  176. }
  177. for check in $sysupgrade_image_check; do
  178. ( eval "$check \"\$ARGV\"" ) || {
  179. if [ $FORCE -eq 1 ]; then
  180. echo "Image check '$check' failed but --force given - will update anyway!"
  181. break
  182. else
  183. echo "Image check '$check' failed."
  184. exit 1
  185. fi
  186. }
  187. done
  188. if [ -n "$CONF_IMAGE" ]; then
  189. case "$(get_magic_word $CONF_IMAGE cat)" in
  190. # .gz files
  191. 1f8b) ;;
  192. *)
  193. echo "Invalid config file. Please use only .tar.gz files"
  194. exit 1
  195. ;;
  196. esac
  197. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  198. export SAVE_CONFIG=1
  199. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  200. [ $TEST -eq 1 ] || do_save_conffiles
  201. export SAVE_CONFIG=1
  202. else
  203. export SAVE_CONFIG=0
  204. fi
  205. if [ $TEST -eq 1 ]; then
  206. exit 0
  207. fi
  208. run_hooks "" $sysupgrade_pre_upgrade
  209. # Some platforms/devices may want different sysupgrade process, e.g. without
  210. # killing processes yet or calling ubus system upgrade method.
  211. # This is needed e.g. on NAND devices where we just want to trigger stage1 at
  212. # this point.
  213. if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
  214. platform_pre_upgrade "$ARGV"
  215. fi
  216. ubus call system upgrade
  217. touch /tmp/sysupgrade
  218. if [ ! -f /tmp/failsafe ] ; then
  219. kill_remaining TERM
  220. sleep 3
  221. kill_remaining KILL
  222. fi
  223. if [ -n "$(rootfs_type)" ]; then
  224. v "Switching to ramdisk..."
  225. run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
  226. else
  227. do_upgrade
  228. fi