1
0

sysupgrade 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. add_uci_conffiles() {
  94. local file="$1"
  95. ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  96. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  97. -type f -o -type l 2>/dev/null;
  98. opkg list-changed-conffiles ) | sort -u > "$file"
  99. return 0
  100. }
  101. add_overlayfiles() {
  102. local file="$1"
  103. if [ -d /overlay/upper ]; then
  104. local overlaydir="/overlay/upper"
  105. else
  106. local overlaydir="/overlay"
  107. fi
  108. find $overlaydir/etc/ -type f -o -type l | sed \
  109. -e 's,^/overlay\/upper/,/,' \
  110. -e 's,^/overlay/,/,' \
  111. -e '\,/META_[a-zA-Z0-9]*$,d' \
  112. -e '\,/functions.sh$,d' \
  113. -e '\,/[^/]*-opkg$,d' \
  114. > "$file"
  115. return 0
  116. }
  117. # hooks
  118. sysupgrade_image_check="fwtool_check_image platform_check_image"
  119. sysupgrade_pre_upgrade="fwtool_pre_upgrade"
  120. [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
  121. sysupgrade_init_conffiles="add_uci_conffiles" || \
  122. sysupgrade_init_conffiles="add_overlayfiles"
  123. include /lib/upgrade
  124. [ "$1" = "nand" ] && nand_upgrade_stage2 $@
  125. do_save_conffiles() {
  126. local conf_tar="${1:-$CONF_TAR}"
  127. [ -z "$(rootfs_type)" ] && {
  128. echo "Cannot save config while running from ramdisk."
  129. ask_bool 0 "Abort" && exit
  130. return 0
  131. }
  132. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  133. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  134. v "Saving config files..."
  135. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  136. tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
  137. rm -f "$CONFFILES"
  138. }
  139. if [ $CONF_BACKUP_LIST -eq 1 ]; then
  140. add_uci_conffiles "$CONFFILES"
  141. cat "$CONFFILES"
  142. rm -f "$CONFFILES"
  143. exit 0
  144. fi
  145. if [ -n "$CONF_BACKUP" ]; then
  146. do_save_conffiles "$CONF_BACKUP"
  147. exit $?
  148. fi
  149. if [ -n "$CONF_RESTORE" ]; then
  150. if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
  151. echo "Backup archive '$CONF_RESTORE' not found."
  152. exit 1
  153. fi
  154. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  155. tar -C / -x${TAR_V}zf "$CONF_RESTORE"
  156. exit $?
  157. fi
  158. type platform_check_image >/dev/null 2>/dev/null || {
  159. echo "Firmware upgrade is not implemented for this platform."
  160. exit 1
  161. }
  162. for check in $sysupgrade_image_check; do
  163. ( eval "$check \"\$ARGV\"" ) || {
  164. if [ $FORCE -eq 1 ]; then
  165. echo "Image check '$check' failed but --force given - will update anyway!"
  166. break
  167. else
  168. echo "Image check '$check' failed."
  169. exit 1
  170. fi
  171. }
  172. done
  173. if [ -n "$CONF_IMAGE" ]; then
  174. case "$(get_magic_word $CONF_IMAGE cat)" in
  175. # .gz files
  176. 1f8b) ;;
  177. *)
  178. echo "Invalid config file. Please use only .tar.gz files"
  179. exit 1
  180. ;;
  181. esac
  182. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  183. export SAVE_CONFIG=1
  184. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  185. [ $TEST -eq 1 ] || do_save_conffiles
  186. export SAVE_CONFIG=1
  187. else
  188. export SAVE_CONFIG=0
  189. fi
  190. if [ $TEST -eq 1 ]; then
  191. exit 0
  192. fi
  193. run_hooks "" $sysupgrade_pre_upgrade
  194. # Some platforms/devices may want different sysupgrade process, e.g. without
  195. # killing processes yet or calling ubus system upgrade method.
  196. # This is needed e.g. on NAND devices where we just want to trigger stage1 at
  197. # this point.
  198. if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
  199. platform_pre_upgrade "$ARGV"
  200. fi
  201. ubus call system upgrade
  202. touch /tmp/sysupgrade
  203. if [ ! -f /tmp/failsafe ] ; then
  204. kill_remaining TERM
  205. sleep 3
  206. kill_remaining KILL
  207. fi
  208. if [ -n "$(rootfs_type)" ]; then
  209. v "Switching to ramdisk..."
  210. run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
  211. else
  212. do_upgrade
  213. fi