sysupgrade 6.1 KB

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